Article count:1384 Read by:1794800

Account Entry

OSC Classroom|Exploring Ethereum with Raspberry Pi - Part 1: Getting Started

Latest update time:2018-05-04
    Reads:


Install and run the Ethereum blockchain client on the Pi 3 Model B.

Blockchain is an incredibly hot topic right now, and has been for some time, in large part because of the wealth that cryptocurrencies — primarily Bitcoin — have generated seemingly overnight for their early adopters, and the new economic possibilities they have opened up.

However, blockchain can be used for more than just cryptocurrency, and as Greig points out in his recent article, its distributed nature and ability to provide a tamper-proof record of verified transactions can be used in IoT applications. What is exciting about the Ethereum platform is that it is more than just simple cryptocurrency and secure transactions, it also provides a distributed computing platform.

In this post, we will take a brief look at the Ethereum architecture and try to introduce some of the main concepts and components before moving on to installing the client software on the Raspberry Pi. To be clear, we will not be mining any cryptocurrencies, as you do need a powerful GPU for this, but it is possible to create a simple sandbox with the Pi to experiment - this also shows that blockchain technology is indeed possible on embedded platforms.

Ethereum 101

contract mortal {    /* Define variable owner of the type address */    address owner;    /* This function is executed at initialization and
sets the owner of the contract */
   function mortal() { owner = msg.sender; }    /* Function to recover the funds on the contract */    function kill() { if (msg.sender == owner)
selfdestruct(owner); }}

contract greeter is mortal {    /* Define variable greeting of the type string */    string greeting;        /* This runs when the contract is executed */    function greeter(string _greeting) public {        greeting = _greeting;    }    /* Main function */    function greet() constant returns (string) {        return greeting;    }}

An example of an Ethereum smart contract. Source: ethereum.org

A blockchain is a distributed record book that is usually managed through a peer-to-peer network and grows in size as more records or blocks are added. Blocks usually contain timestamps of transaction data and are encrypted using a hashing algorithm so that once the data is committed, it cannot be modified.

The Ethereum platform has its own cryptocurrency, called ether , but it also takes blockchain technology one step further, creating a decentralized platform for smart contracts — contracts that contain code and blockchain functions and are able to interact with other contracts, make decisions, store data, and send ether to others.

Smart contracts are implemented in the Solidity language, which is based on JavaScript. The Solidity compiler is used to compile smart contracts into bytecode - just like code execution using JavaScript or Python, Java, and Android - which is then executed by the Ethereum Virtual Machine. Executing transactions in smart contracts incurs a certain cost, which we will introduce in a future article.

Ethereum provides many different client applications, the original reference implementation geth is written in Go. Some of them can mine ether. There is also standalone mining software, and it seems that there are also GUI clients and IDEs for distributed applications.

In addition to the main public Ethereum blockchain network, mainnet , there are test networks for experimental purposes, and you can also create your own private network.

Install geth

Assuming we have Raspbian installed, we can start by updating the installed packages to the latest versions.

$ sudo apt-get update $ sudo apt-get dist-upgrade

I ran out of memory the first time I compiled the Ethereum client. A good way to free up memory for memory-intensive tasks is to reduce the amount allocated to the GPU. In fact, if you don't need a graphical desktop, simply configure your system to boot into the command line.

$ sudo raspi-config

  • 3 → Boot

  • B1 → Desktop / CLI

  • B1 → Console

  • 7 → Advanced

  • A3 → Memory Split

  • 16 (MB)

  • Reboot

Next, if we install the relevant installation packages.

$ sudo apt-get install git golang libgmp3-dev

After that, if we grab the source code of geth, an official language implementation of an Ethereum node, we compile the code and copy the executable file to /usr/local/bin/.

$ mkdir src $ cd src $ git clone -b release/1.7 https://github.com/ethereum/

go-ethereum.git $ cd go-ethereum $ make $ sudo cp build/bin/geth /usr/local/bin/

Create an account and test it

First, if we use geth to create a new account.

$ geth account new

This will generate a new set of encryption keys and protect the private key with a passphrase. Please note that if you use this account to mine cryptocurrency and make meaningful transactions, you will want to make sure to back up your keys and prevent your private key from being accessed.

Now start the node.

geth --syncmode light --cache 64 --maxpeers 12

If we run geth without any arguments, it starts a node and tries to sync the entire public mainnet blockchain. With a size of over 50GB and growing, this is probably not a good idea for an embedded computer. So, we start the node in light sync mode. This only pulls block headers on demand as they appear, along with the rest of the blockchain.

To force the node to exit, just press CTRL-C to run it as a service at startup:

$ sudo vi /etc/systemd/system/geth@.service

(Replace "vi" with your favorite text editor)

Then enter:

[Unit]Description=Ethereum daemon Requires=network.target[Service]Type=simple User=%I ExecStart=/usr/local/bin/geth --syncmode light --cache 64
--maxpeers
12Restart=on-failure[Install]WantedBy=multi-user.target

Save the file and run the Ethereum node as the "pi" user:

$ sudo systemctl enable geth@pi.service $ sudo systemctl start geth@pi.service

With our Ethereum node running as a service, we can now connect to it using:

$ geth attach

This gives us an interactive JavaScript console. From here we can call functions, for example:

> eth.accounts

Lists current accounts.

Get information about the connector:

> admin.peers

Please note that the lightweight client protocol is still under development, somewhat experimental, and relies on full peers/nodes to support it. As such, it may not be fully operational on the Ethereum mainnet blockchain at the time of writing. That said, things are moving fast, and this could easily change in the not too distant future.

Next Steps

So far, we have just installed the client software, created an account, started the node and observed the peer connection. In a future article, we will look at actually executing transactions.

Click

Read the original article

, learn more about the product



Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号