2016.02.12

Setting up a Private Multi-node Ethereum Network for Development


(編集注)GMOアプリクラウドにおいて、ブロックチェーン技術「mijin」を用いたバックエンドエンジン開発の発表が行われました。当チームのエンジニアにおいても技術的興味から、複数あるブロックチェーン技術の中でも、海外でメジャーなひとつ「Ethereum」について取り上げたものです。

In ( Edit Note ) GMO app cloud recently announced back-end development utilizing Mijin’s block chain technology .
This article is from a technical interest point of view, for ethereum block chain technology, from one of our engineers.



Firstly: What is Ethereum?

From http:www.ethereum.org: “Ethereum is a decentralized platform that runs smart contracts: applications that run exactly as programmed without any possibility of downtime, censorship, fraud or third party interference…”

Inspired by Bitcoin, and backed by a blockchain, Ethereum is more flexible and general purpose than Bitcoin.
Ethereum runs programs (called contracts in Ethereum-speak) taking input from Ethereum transactions or other contracts. Contracts run within the Ethereum VM. Although Bitcoin has limited stack based scripting ability, there is limited flexibility. According to the documentation, Ethereum VM programs are Turing-complete.

As with bitcoin all blockchain data is replicated to every node, also all contracts run on every node identically.
This understandably limits what can realistically run on an Ethereum network; as stated in the docs, nothing that can’t run on a “single smartphone from 1999“.
But that still allows many interesting possible applications in addition to plain-old currency transactions, such as escrow, smart contracts, voting, prediction markets etc.

A Note about the title

Private means our Ethereum nodes only interact amongst themselves and ignore other Ethereum networks (including the main network). This is a development setup. We have total control of all nodes so we can set development friendly parameters such as blockchain proof-of-work hashing difficulty.

Let’s get started!

These are the steps I used to run multiple Ethereum nodes on a MacBook Pro with 8GB of RAM.
Note that since Ethereum is P2P, you may think of nodes as clients.

Installation

First install Geth the Go Ethereum client software. As an aside, in Old-English Geth means “to go” in the third-person, appropriately since Geth is written in Go.
$ brew tap ethereum/ethereum
$ brew install ethereum
Installation instructions are given here: https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Mac

This installation gave me the following version:
$ geth version
 Geth
 Version: 1.3.1
 Protocol Versions: [63 62 61]
 Network Id: 1
 Go Version: go1.5.1
 OS: darwin
 GOPATH=
 GOROOT=/usr/local/Cellar/go/1.5.1/libexec

Running the nodes

Now we are ready to start our first client:
$ geth --datadir="/tmp/ether1" -verbosity 6 --ipcdisable --port 30301 --rpc --rpcport 8101 --networkid 96781 --nodiscover console 2>> /tmp/ether1/ether.log
The above terminal command starts a node using /tmp/ether1 as it’s data directory. We have noisy logs (reduceable to zero if desired). We disable IPC connections, specify geth port 30301, using RPC on 8101. The network ID is our private Etherium network ID, the default is 1 which is the main Ethereum network. By specifying –nodiscover we disable Ethereum node discovery. Also we attach a JS console (ECMA compliant) to this node. Note that consoles may be attached later to nodes already running by explicitly running the geth attach subcommand.

Similarly we can start another node using slightly adjusted parameters so as not to conflict with our first node.
geth --datadir="/tmp/ether2" --verbosity 6 --ipcdisable --port 30302 --rpc --rpcport 8102 --networkid 96781 --nodiscover console 2>> /tmp/ether2/ether.log
We store blockchain, account data etc in a different directory (/tmp/ether2). We use different ports, but run on the same Ethereum network ID (96781).

Note that if you don’t specify –nodiscover it takes minutes to add another node, an important configuration for development.

Making accounts

Now, we can go ahead and add accounts which will enable us to post transactions or setup contracts.
$ geth --datadir="/tmp/ether1" account new
 Your new account is locked with a password. Please give a password. Do not forget this password.
 Passphrase: mydevpassword
 Repeat Passphrase: mydevpassword
 Address: {72c61b804fedbc0c24c1781a76a1461b38e78a01}
The above command adds an account to our first node and outputs an address for us to use. This is roughly equivalent to a Bitcoin address.

We can confirm via RPC called on the node that the account was created on, using the RPC port specified when running our first node:
$ curl -X POST --data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}' http://127.0.0.1:8101
 {"id":1,"jsonrpc":"2.0","result":["0x72c61b804fedbc0c24c1781a76a1461b38e78a01"]}
The above command uses JSON RPC to interact with our first geth node. Note that this is the API used by browser JS clients to interact with the geth nodes.

Or use the “geth account” subcommand:
$ geth --datadir="/tmp/ether1" account list
 Account #0: {72c61b804fedbc0c24c1781a76a1461b38e78a01}
Or from the geth console:
> eth.accounts
 ["0x72c61b804fedbc0c24c1781a76a1461b38e78a01"]
Now we have setup an Ethereum development P2P network.

Summary

We now have a running 2 node Ethereum network suitable for Distributed App (Ðapp) development. Because we have multiple nodes we can simulate some of the activity we might see on the main Ethereum network. Extra nodes and miners can be added relatively easily by specifying the miner options to the geth command. We have an address (72c61b804fedbc0c24c1781a76a1461b38e78a01) we can use to post transactions or contracts.

What’s next?

The next thing to try could be creating a browser based Ðapp utilizing contracts (written with Solidity) and front-end JS utilizing Web3 the JS Ðapp API.
For Ðapp development there is the Meteor tool. This supports creation of reactive JS browser based Ðapps: https://github.com/ethereum/wiki/wiki/Dapp-using-Meteor#create-your-%C3%90app  (Note that when connecting from web3 JS clients to our nodes we will need to set the –rpccorsdomain parameter when starting the nodes.)

Cheers,
Platform team

We are hiring