Skip to content

Instantly share code, notes, and snippets.

@oneamitj
Created April 12, 2018 04:54
Show Gist options
  • Save oneamitj/8760ce93f5bc77fc21ed45d798606f6c to your computer and use it in GitHub Desktop.
Save oneamitj/8760ce93f5bc77fc21ed45d798606f6c to your computer and use it in GitHub Desktop.
Setting up multi node private blockchain network.

Setting up local Ethereum chain (Primary Host Node)

  1. Install ethereum client (geth).

    • Mac

        brew tap ethereum/ethereum
        brew install ethereum
      
    • Linux (Debian)

        sudo apt-get install software-properties-common
        sudo add-apt-repository -y ppa:ethereum/ethereum
        sudo apt-get update
        sudo apt-get install ethereum
      
    • Windows install from here

  2. Install Mist Browser from here

  3. Note: edit all values wrapped in <> (no <> after edit)

  4. Initialize Private BlockChain

     geth --identity "testethChain" --rpc --rpcaddr "0.0.0.0" --rpcport "3333" --rpccorsdomain "*" --datadir "</full/path/to/store/blockchain>" --port "33333" --nodiscover --rpcapi "admin,miner,personal,db,eth,net,web3" --networkid <anyIntMoreThan2> init </full/path/to/genesis.json>
    
     geth --identity "testethChain" --rpc --rpcaddr "0.0.0.0" --rpcport "3333" --rpccorsdomain "*" --datadir "/home/amitj/testeth/ethereum"  --port "33333" --nodiscover --rpcapi "admin,miner,personal,db,eth,net,web3" --networkid 333 init /home/amitj/testeth/ethereum/genesis.json
    
     genesis.json Example:
    
     {
         "nonce": "0x0000000000000033",
         "timestamp": "0x00",
         "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
         "extraData": "0x00",
         "gasLimit": "0x33000000",
         "difficulty": "0x3000",
         "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
         "coinbase": "0x3333333333333333333333333333333333333333",
         "alloc": {     },
         "config": {
           "chainId": 33,
           "homesteadBlock": 0,
           "eip155Block": 0,
           "eip158Block": 0
             
         }
     }
    
  5. Start Ethereum client as private net

     geth --fast --cache 512 --ipcpath "</full/path/to/store/blockchain/testchain.ipc>" --identity "testethChain" --rpc --rpcaddr "0.0.0.0" --rpcport "3333" --rpccorsdomain "*" --datadir "<dir/path/as/in/initialize>" --port "33333" --nodiscover --rpcapi "admin,miner,personal,db,eth,net,web3" --networkid <idAsInInitialize> console
    
     geth --fast --cache 512 --ipcpath "/home/amitj/testeth/ethereum/testchain.ipc" --identity "testethChain" --rpc --rpcaddr "0.0.0.0" --rpcport "3333" --rpccorsdomain "*" --datadir "/home/amitj/testeth/ethereum" --port "33333" --nodiscover --rpcapi "admin,miner,personal,db,eth,net,web3" --networkid 333 console
    
  6. Connect to Ethereum client in next terminal, used as work geth console

     geth attach ipc:</full/path/to/store/blockchain/testchain.ipc>
    
     geth attach ipc:/home/amitj/testeth/ethereum/testchain.ipc
    
  7. Create new user & unlock it

     personal.newAccount("password")
     personal.unlockAccount(personal.listAccounts[0], "password")
    
  8. Start a miner

     miner.setEtherbase(listAccounts[0]) // Set miner
     miner.start() // Start miner session
     miner.stop() // Stop miner session
    
  9. Start Mist browser using (another) terminal, correct gethpath as per your system

     mist --rpc --rpcaddr "0.0.0.0" --rpcport "3333" </full/path/to/store/blockchain/testchain.ipc> --gethpath /usr/local/bin/geth
    
  10. Lots of help here

Connect to Existing private net (Connect to host node)

  1. Follow step 1 of above.

  2. Copy same genesis.json (step 4 above) used previously to another nodes' machine.

     {
         "nonce": "0x0000000000000033",
         "timestamp": "0x00",
         "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
         "extraData": "0x00",
         "gasLimit": "0x33000000",
         "difficulty": "0x3000",
         "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
         "coinbase": "0x3333333333333333333333333333333333333333",
         "alloc": {     },
         "config": {
           "chainId": 33,
           "homesteadBlock": 0,
           "eip155Block": 0,
           "eip158Block": 0
             
         }
     }
    
  3. Initialize blockchain in next machine with same networkid.

     geth --datadir="</any/path/dir>" init genesis.json
    
     geth --datadir="/home/amitj/testeth/ethereum/node2/chain" init genesis.json
    
  4. Now, open geth console with same networkid.

     geth --ipcpath="</any/path/dir/testchain.ipc>" --rpc --rpccorsdomain "*"  --datadir="</any/path/dir>" --networkid 333 --nodiscover console
    
     geth --ipcpath="/home/amitj/testeth/ethereum/node2/testchain.ipc" --rpc --rpccorsdomain "*"  --datadir="/home/amitj/testeth/ethereum/node2/chain" --networkid 333 --nodiscover console
    
  5. Using geth console, Connect to hosted private ethereum node. If you already dont have node info, use admin.nodeInfo.enode in geth console of host node to find

     admin.addPeer(<connection node info>)
    
     admin.addPeer("enode://85a14cfdff34ac51e51186229235d131edd4a5f62284eac9a3a998e1b0bf087a82a44d83010949ac899ffd355683522b88a87db78fcd235293b9e6e1b06ec139@192.168.0.102:33333?discport=0")
    
  6. Check if successfully connected.

     admin.peers
    
  7. Attach new geth console to this chain for executing geth commands

     geth attach ipc:</any/path/dir/testchain.ipc>
    
     geth attach ipc:/home/amitj/testeth/ethereum/node2/testchain.ipc
    

Transaction via geth console

  1. Create transaction

     tx = {from: <hash_of_sender_account>, to: <hash_of_receiver_account>, value: web3.toWei(<amount_to_send>, <coin_unit>)}
    
     tx = {from: "0x2a1476cd7681e4b45b89b0792019d481bca469c0", to: "0x59429d6106e98dc914622315e408b7aa4998d8f3", value: web3.toWei(1.23, "ether")}
    
  2. Push transaction to network

     personal.sendTransaction(tx, <sender_passphrase>)
    
     personal.sendTransaction(tx, "1")
    
  3. Check account's balance

     web3.fromWei(eth.getBalance(primary), <coin_unit>)
     
     web3.fromWei(eth.getBalance(primary), "ether")
    

Deploy solidity SmartContract

Using Mist Browser

  1. Download and install Mist Browser from here
  2. Mantain atleast one account with atleast 1 Ether (start miner to generate ether)
  3. Select CONTRACTS tap in top right
  4. Click DEPLOY NEW CONTRACT
  5. Select account to deploy contract with (must have atleast 1 ETH)
  6. Paste solidity code in TextCode box, a Select option will appear (right side of textbox)
  7. Select Contract to deploy and fill init values if any.
  8. Click Deploy and enter account passphrase
  9. If any miner is running in the network, you can view deployed Contract in CONTRACTS tab.

Using geth console

  1. Follow this tutorial

Connect to contract deployed in BlockChain (using ABI/JSON Interface)

  1. JSON Interface can be found in Mist Browser CONTRACT_INFO page after deployed.

  2. Geth code to connect to SmartContract

     var myContract = eth.contract(<JSON_Interface>).at(<Contract_Address>);
    
    
     var smartMultiReward = eth.contract([ { "constant": true, "inputs": [ { "name": "user", "type": "address" } ], "name": "showRewardTypes", "outputs": [ { "name": "", "type": "bytes32[]", "value": [] } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "spender", "type": "address" }, { "name": "value", "type": "uint256" }, { "name": "rewardType", "type": "bytes32" }, { "name": "spentOnMsg", "type": "string" } ], "name": "spendReward", "outputs": [], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "user", "type": "address" }, { "name": "rewardType", "type": "bytes32" } ], "name": "showMyReward", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "value", "type": "uint256" }, { "name": "rewardType", "type": "bytes32" } ], "name": "setAvailRewardPoints", "outputs": [], "payable": false, "type": "function" }, { "constant": true, "inputs": [], "name": "owner", "outputs": [ { "name": "", "type": "address", "value": "0x2a1476cd7681e4b45b89b0792019d481bca469c0" } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "sender", "type": "address" }, { "name": "receiver", "type": "address" }, { "name": "value", "type": "uint256" }, { "name": "rewardType", "type": "bytes32" }, { "name": "senderMsg", "type": "string" } ], "name": "sendReward", "outputs": [], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "to", "type": "address" }, { "name": "value", "type": "uint256" }, { "name": "rewardType", "type": "bytes32" }, { "name": "rewardReasonMsg", "type": "string" } ], "name": "giveReward", "outputs": [], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "rewardType", "type": "bytes32" } ], "name": "showAvailReward", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "type": "function" }, { "inputs": [ { "name": "initValue", "type": "uint256", "index": 0, "typeShort": "uint", "bits": "256", "displayName": "init Value", "template": "elements_input_uint", "value": "999999999" }, { "name": "rewardType", "type": "bytes32", "index": 1, "typeShort": "bytes", "bits": "32", "displayName": "reward Type", "template": "elements_input_bytes", "value": "0xabc" } ], "payable": false, "type": "constructor" }, { "payable": false, "type": "fallback" }, { "anonymous": false, "inputs": [ { "indexed": false, "name": "sender", "type": "address" }, { "indexed": false, "name": "receiver", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" }, { "indexed": false, "name": "rewardType", "type": "bytes32" }, { "indexed": false, "name": "rewardEvent", "type": "string" }, { "indexed": false, "name": "reason", "type": "string" } ], "name": "BroadcastReward", "type": "event" } ].at("0x169E12BD25063AD6b3df3F12444859b998f64a3a");
    

SmartContract function transaction via geth

  1. Call contract constant function

     myContract.myConstantFunc(param1 [, param2, ...])
    
     hamroCertif.viewCertificateByCourse("0xae4b913898661b4ace005b760ea784423558c87c", "AdvancedBlockchain")
    
  2. Unlock Account for transaction

     personal.unlockAccount(<address>)
    
     personal.unlockAccount(eth.accounts[0])
    
  3. Call transaction function

     myContract.myTransactionFunc.sendTransaction(param1 [, param2, ...], {from: "0x", gasPrice: <price in wei>, [from: "0x", gas: <gas limit to use>]}
    
     hamroCertif.assignCertificate.sendTransaction(0xae4b913898661b4ace005b760ea784423558c87c, CertifID", "CourseName", "Today", "SomeDetail", {from: eth.accounts[0], gasPrice: 10000000000})
    

Load JS Code

  1. In geth console

     loadScript("/path/to/gethcode.js");
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment