6. Development of Technical Concept

The following picture gives an overview over the technical concept and will be described in detail afterwards.

The picture shows an example how the smart contract works: The PV system transmits an energy token to the grid and the grid pays for the energy with Ethereum. The necessary information are combined in a Smart Contract which is explained by the rest of the figure.

The initiation of a smart contract starts in the Matlab simulation. Obviously, in a real-life scenario this would be implemented in the smart contract's logic. The simulation generates data every 15 seconds and transfers the data to the JavaScript Webserver via UDP. The data includes the prices for the energy and the needed or provided amount of energy. This information is going to be part of the Smart Contract between (for example) the PV system and the grid. Every Smart Contract is deployed with truffle and has an individual identifier (the so-called address) and an ABI. When the Smart Contract is completed, the energy can be provided/used and the price is payed.

1. Setup of Raspberry Pi

The Raspberry Pi has been assembled according to the instructions included in the package and a USB mouse and USB keyboard were connected. In the next step a microSD card was prepared with the use of a laptop. It was first formatted with the SD Formatter 5.0 for SD/SDHC/SDXC Tool application to make it writable. According to another manual, the operating system image (RASPBIAN STRETCH 4.14) was downloaded and transferred to the microSD card. Then the microSD card was inserted into the Raspberry Pi, the Micro-USB power adapter was connected and the Raspberry Pi started. The Raspbian installation was started by Raspbian itself before the language, the keyboard layout etc. were set manually and a user account (user name: pi, password: raspberry) was created.

2. Node Setup

It is recommended to use the Root-Account (command: sudo su) because one needs root permissions for everything. This has to be considered even after a restart of the Raspberry Pi. We will use our Raspberrys for different roles: Four will be used as miners while one will be a bootnode. But first of all we need to install geth - the go implementation of an Ethereum client - on all of our nodes. A big thank you to Salanfe from Hackernoon whose tutorial was used for this project.

1. Installing GETH

First we need to update the current Raspbian image on all our Raspberrys.

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

Afterwards we can install the necessary dependencies: git, go and libgmp3.

$ sudo apt-get install git golang libgmp3-dev

Now we create a new directory within our workspace which for us is "/usr/local/bin" and download the source code from github.

$ mkdir src
$ cd src
$ git clone -b release/1.7 https://github.com/ethereum/go-ethereum.git
$ cd go-ethereum
$ make all
$ sudo cp build/bin/geth /usr/local/bin

We use the make all command because we ran into some trouble with the standard make. In theory this should suffice for the four miners but there is no real disadvantage in using make all. Now that the foundation is laid let's set up the miners.

2. Setting up the Miners

Repeat these steps for each of the four raspberries (miners). First create the workspace. This is due to better clarity.

$ cd /usr/local/bin
$ mkdir node

In our workspace we can use geth to create an account for the individual node.

$ cd node
$ sudo echo "password" >> ethereum_pwd.txt
$ geth --datadir /usr/local/bin/ --password /usr/local/bin/node/ethereum_pwd.txt 
account new

This will generate a new account. This account will be linked to an unique account-address. Save this address in an easy to access file where you collect all the generated addresses for all the accounts. You will need them later.

3. Creating the network

By now you should have created four miners but you still have no Blockchain. For this we use the puppeth script that comes with the geth installation (Another benefit of using make all). This script will generate the genesis block we will be using for our own private Ethereum chain. You can execute this script on any computer that has geth with all executables installed. But remember that we will have to copy the genesis file to every other node.

Let's execute puppeth:

$ cd /usr/bin/local
$ sudo cp build/bin/puppeth /usr/local/bin 
$ puppeth

This will open a dialog. Since the Raspberries have very limited resources we can't really use proof-of-work. Since we are on a private network we will just use proof-of-authority as our consensus algorithm. That's one big advantage of puppeth - we can just say so in the build process. For our PoC we used a block time of five seconds. However there is no reason not to use another time. We need to specify accounts that are allowed to seal (mine) and accounts that are prefunded. For both points we use the four accounts we created beforehand.

Please specify a network name to administer (no spaces, please)
> SmartEnergy
What would you like to do? (default = stats)
 1. Show network stats
 2. Configure new genesis
 3. Track new remote server
 4. Deploy network components
> 2
Which consensus engine to use? (default = clique)
 1. Ethash - proof-of-work
 2. Clique - proof-of-authority
> 2
How many seconds should blocks take? (default = 15)
> 5
Which accounts are allowed to seal? (mandatory at least one)
> 0x..... // copy the accounts you created on the miners
> 0x.....
> 0x.....
> 0x.....
> 0x //hit enter once
Which accounts should be pre-funded? (advisable at least one)
> 0x..... // same input as before
> 0x.....
> 0x.....
> 0x.....
> 0x //hit enter once
Specify your chain/network ID if you want an explicit one (default = random)
> 1337 // for example. Do not use anything from 1 to 10
Anything fun to embed into the genesis block? (max 32 bytes)
>
What would you like to do? (default = stats)
 1. Show network stats
 2. Manage existing genesis
 3. Track new remote server
 4. Deploy network components
> 2
1. Modify existing fork rules
2. Export genesis configuration
> 2
Which file to save the genesis into? (default = devnet.json)
> genesis.json

After this you can stop puppeth with CTRL + C.

We could potentially just leave the gas-price at 0 so we would not need to prefund the accounts. However this could potentially result in unwanted behaviour (like nodes refusing to execute smart contracts). Prefunding the accounts makes them so rich that running out of Ether should not be a problem in the near future.

4. Initializing the nodes

The genesis.json file we just created must now be copied to every node. Be careful that you do not change it as every node has to be initialized with the exact SAME genesis-file. Copy the file to our geth directory /usr/local/bin. Execute the following command on every node.

$ cd /usr/local/bin
$ geth --datadir node/ init genesis.json

5. Create a bootnode

Now we may have a network but our nodes do not know how to find each other. For this we will use the already mentioned bootnode. Potentially you could just do the following on one of the four nodes we configured to be miners. However this would require some extra attention to IP-addresses and ports. Therefore we used an extra Raspberry for the bootnode service. A bootnode's only purpose is to inform the other nodes about nodes in the network similar to what a router does in the standard IP-world. Yet another advantage of using the make all installation of geth: it comes with a bootnode executable.

$ cd /usr/bin/local
$ sudo cp build/bin/bootnode /usr/local/bin 
$ bootnode -genkey boot.key

This creates a value called the enode uniquely identifying your bootnode and we store this enode in the boot.key file. The keys are currently still in the overall keystore directory. We have to copy them to their respective node directories.

Obviously your keystore file has some name unique to it. It is definitely not called UTC-... .

$ cp keystore/UTC-..... /usr/local/bin/node/keystore

3. Startup Network

1. Start the bootnode service

You are now ready to start your network. Let's start our bootnode first.

$ cd /usr/local/bin
$ bootnode -nodekey boot.key -verbosity 9 -addr :30310

After we start our nodes we will see them ping-ponging each other in this window.

2. Starting the nodes

Now we will use one big command to start the individual nodes. The command is specific for the respective nodes. Therefore there are some variables (labelled with { } brackets) in it that are explained below. Do the following for every sealer and your network should be up and running.

This command should be executed as one. The line breaks are only used for sake of visualization.

$ cd /usr/local/bin
$ geth --datadir node/ --syncmode 'full' --port 30311 --rpc 
--rpcaddr '{ip-address}' --rpcport 8501 
--rpcapi 'personal,db,eth,net,web3,txpool,miner' 
--bootnodes 'enode://{bootnode}' --networkid 1337 --gasprice '1' 
--unlock '0x{account}' --password node/ethereum_pwd.txt --mine
  • ip-address: The node's ip-address

  • bootnode: The enode value that was saved in the bootnodes boot.key file

  • account: The account that was created on the individual node

The bootnode value can also be seen after starting the bootnode-service.

Last updated