No Description

Priya Samuel 6e84a96b8a Moved node-test to spikes 2 years ago
..
apps 3e0ef9df9e [+Camilla] Adds voting app example, refactors deployment script 2 years ago
contracts 920346a8e7 Added tests and contracts for Petitions 2 years ago
migrations 920346a8e7 Added tests and contracts for Petitions 2 years ago
node-test 6e84a96b8a Moved node-test to spikes 2 years ago
nodes eaa8786fbc You can run multiple nodes on your local machine 2 years ago
test 920346a8e7 Added tests and contracts for Petitions 2 years ago
README.md 0899b7064d [+Rylan] Tweak readme bit about deploying contracts 2 years ago
go.sh 47ebd21609 Allows to create more nodes 2 years ago
truffle.js 920346a8e7 Added tests and contracts for Petitions 2 years ago

README.md

Ethereum -

Setup private network

Start a private cluster of nodes on your local machine and with other nodes running on the same LAN.

Setup

TODO (Brew install ethereum and geth)

Start the node(s)

The script allows you to start up to 9 nodes on your local machine (node0, node1, node3 etc). To start a node run the script passing start and the node name as arguments:

./go.sh start node0

Attach javascript console

In a new terminal window you can attach a javascript console to interact with the node:

./go.sh console node0

In the console you can:

List accounts
> eth.accounts
Create new account
> personal.newAccount()

(It will ask for a passphrase) You can see the new key pair being created in the folder keystore.

Mine to get ether

Let's get rich!

> miner.start(1)
...... (mine some blocks....)
> miner.stop()
Check balance

You should be richer

> eth.getBalance(eth.accounts[0])

Or convert to ether
> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether");
Connect to other peers

If you have nodes running in the same Ethereum private network (same network id and same genesis file) you can connect them. Let's say you want to connect A <-> B. Find the enode of the node B:

> admin.nodeInfo.enode

"enode://fbd893902e0c6be1614f2d8b764d04d7dac61a2fb5fb54a0473e92360e250cd47dd5ca3063316c30ffdd3610a4d0257b33bf10affdfdc615808c5ae47dc8b2c7@[::]:30303"

If the nodes are on different machines in the same LAN, find the private IP address of node B:

ifconfig | grep inet

If the nodes are on the same machine, find the localhost IP address (probably 127.0.0.1) of node B:

ifconfig | grep netmask | awk '{print $2}'

In node A then add the peer:

> admin.addPeer("enode://fbd893902e0c6be1614f2d8b764d04d7dac61a2fb5fb54a0473e92360e250cd47dd5ca3063316c30ffdd3610a4d0257b33bf10affdfdc615808c5ae47dc8b2c7@[PRIVATE IP ADDRESS or LOCALHOST IP ADDRESS if on the same machine]:30303")

As soon as a node connects to a peer the Ethereum protocol kicks in, trying to understand who has the 'best' version of the blockchain. If the two nodes have different forks (ending chain of blocks), only one will be accepted. If the syncing between nodes has ended, A and B should have the same ending block (check the hash).

> admin.getBlock("latest")
Send ether to another account
> personal.unlockAccount(eth.accounts[0])
> eth.sendTransaction({ from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(5, "ether") })

Deploy contract

You can find example contracts greeter.sol and voting.sol and a deployment script deploy.js in the folder contracts/. The first step to deploy is to compile the contract. To do so install the solcjs dependency:

npm i -g solc

Then use it to compile the contract. In the following command, you can replace contracts/greeter.sol with the path to the contract of your choice:

solcjs --bin --abi contracts/greeter.sol -o build

This will create a folder build with the compiled contract in .abi and .bin formats. The .abi file specifies the methods available (interface) in your contract, the .bin is the bytecode that actually gets deployed onto the blockchain.

In your Geth console load the script to generate function that will deploy the contract (you might need to unlock your account).

> personal.unlockAccount(eth.accounts[0])
> loadScript("contracts/deploy.js")

The generated function deployContract requires abi, bin and data to be passed as arguments. Assign values to these arguments.

> var bin = "<paste the contents of the .bin file in the build folder>"
> var abi = <paste the contents of the .abi file in the build folder>
> var data = <if your contract requires data, paste it here. for greeter example, that will be "hello world">

Execute the deployment function.

> deployContract(abi, bin, data)

You will need miners to accept the contract in the blockchain. Once the contract has been mined you should see in your console:

Contract mined! address: 0x3c96ce5b6760fc61c22f922514f98bed61172f14 transactionHash: 0x6a75e690fbf6381b28b5873db39a7f4714eb55b759702b23f1a02fa8a9ec5218

Note down the contract address as it will be handy to find it again in the future.

Assign the contract object to a variable.

var greeter = web3.eth.contract(add the contract ABI).at(add the contract hash address as a string)

Now in the console you can interact with the functions available in the contract via the variable created:

> greeter.greet()

That was an example of a 'call', the execution of code without permanent changes. A call is free, is immediate and simply reads and returns data. Other than calls you can also do a 'transaction'. This is not free (you need to specify the account paying for it), is not processed immediately (requires mining) and changes the state of the network:

> personal.unlockAccount(eth.accounts[0])
> greeter.setGreeting("Hello Camilla!", {from:eth.accounts[0]})

If you look in the Ethereum storage at the address of the greeting object, you will see the new value stored:

> var address = greeter.address
> var data = eth.getStorageAt(add, 1)
> web3.toAscii(data)

"Hello Camilla!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1a"
Finding the contract object

If you have lost the contract object by exiting the console, or if you are trying to find the contract from a different node, you can do so doing:

var greeter = web3.eth.contract(add the contract ABI).at(add the contract hash address as a string)

Eth-stats

There is a cool monitoring that can be set up for the local nodes network.

Setup front-end

git clone https://github.com/cubedro/eth-netstats
cd eth-netstats
npm i
WS_SECRET=<chosen_secret> npm start

Visit http://localhost:3000 (will stay empty until the back end is ready).

Create start config for back-end

git clone https://github.com/ethersphere/eth-utils.git
cd eth-utils
bash netstatconf.sh <number_of_nodes_in_cluster> decode http://localhost:3000 <chosen_ws_secret> > app.json

Setup back-end

git clone https://github.com/cubedro/eth-net-intelligence-api
cd eth-net-intelligence-api
npm i
npm i -g pm2

Move the app.json file previously created into the eth-net-intelligence-api/ directory.

Then start the back end!

pm2 start app.json

To kill the back end:

pm2 delete app.json