Two-Node Setup of a Private Ethereum on AWS with Contract Deployment (Part 2)

Two-Node Setup of a Private Ethereum on AWS with Contract Deployment (Part 2)

Updated on: May 4th, 2020 2020-05-04 20:20:21
This content has been Fact-Checked.
Recap In Part 1, we have completed a setup of two-node private Ethereum blockchain. Now we deploy a contract on this blockchain, and the accounts of both nodes can access the contract and execute the


In Part 1, we have completed a setup of two-node private Ethereum blockchain. Now we deploy a contract on this blockchain, and the accounts of both nodes can access the contract and execute the functions.

My plan is to deploy the contract on Node 1 and see how Node 2 accesses this contract.

The contract I am using is the “Voting.sol” by Mahesh Murthy in his blog (link). He wrote a very comprehensive guide about Ethereum application. While he deployed the application on testrpc, I plan to deploy the same contract on my private blockchain with the same deployment example. You can easily cross-check the deployment procedure.

Since he has explained in very detail how the code compilation works, I will skip the detail and only refer to some points relevant to my contract deployment. Please refer to his Part 1 (link) on the detail about the code and the compilation.


The Code “Voting.sol”

Voting.sol (source: link)

pragma solidity ^0.4.11;
// We have to specify what version of compiler this code will compile with

contract Voting {
  /* mapping field below is equivalent to an associative array or hash.
  The key of the mapping is candidate name stored as type bytes32 and value is
  an unsigned integer to store the vote count
  mapping (bytes32 => uint8) public votesReceived;
  /* Solidity doesn't let you pass in an array of strings in the constructor (yet).
  We will use an array of bytes32 instead to store the list of candidates
  bytes32[] public candidateList;

  /* This is the constructor which will be called once when you
  deploy the contract to the blockchain. When we deploy the contract,
  we will pass an array of candidates who will be contesting in the election
  function Voting(bytes32[] candidateNames) {
    candidateList = candidateNames;

  // This function returns the total votes a candidate has received so far
  function totalVotesFor(bytes32 candidate) returns (uint8) {
    if (validCandidate(candidate) == false) throw;
    return votesReceived[candidate];

  // This function increments the vote count for the specified candidate. This
  // is equivalent to casting a vote
  function voteForCandidate(bytes32 candidate) {
    if (validCandidate(candidate) == false) throw;
    votesReceived[candidate] += 1;

  function validCandidate(bytes32 candidate) returns (bool) {
    for(uint i = 0; i < candidateList.length; i++) {
      if (candidateList[i] == candidate) {
        return true;
    return false;


This is a simple contract to illustrate how Solidity code works. Here are some points we will refer to in this part.

  1. The contract requires a list of candidates (see the constructor). We will input this when deploying a new contract.
  2. Two functions are defined in this contract: total Voting For and vote For Candidate. We will use these two functions to interact with the contract from both nodes.


You can find the detail process of compilation (using solc) here. After compilation, we need two pieces of information for contract deployment. They are the Application Binary Interface (ABI) and Bytecode.


Application Binary Interface (ABI)


It contains the methods available in this contract that users can interact with. Here is the ABI for this Voting contract.




It is the actual contract code to be deployed on the blockchain. Here is the bytecode.





Step 1: Install node.js and web3 on both nodes

The interaction to contract is through nodejs and web3js. We need to install nodejs and the web3 modules. I follow the procedure suggested here (link).

Node 1
$ sudo apt-get install python-software-properties
$ curl -sL | sudo -E bash -
$ sudo apt-get install nodejs


To verify successful installation, use $ node -v and $ npm -v, and their version is shown.

Then install web3 module.


Node 1
$ npm install web3


Repeat this step in Node 2 as well.


Step 2: Access Blockchain through web3 in Node Console


We are ready to access the node. Since we need both geth client and node console, we will use split screen again. The top terminal is geth, and the bottom is node console.


To start the geth, depending on whether you still have geth running, either command should bring you back to console.


Node 1 (geth)
$ geth --nodiscover console 2>>eth.log OR
$ geth attach


To start the node console, simply use this


Node 1 (node)
$ node


Two-Node Setup of a Private Ethereum on AWS with Contract Deployment (Part 2)


We are now ready to use node console to access geth by rpc. The default rpc is on http://localhost:8545/.

First, turn on rpc on geth:


Node 1 (geth)
> admin.startRPC()


On node console,


Node 1 (geth)
> admin.startRPC()


To verify if the node can reach geth successfully we can use > web3.eth.accounts on node console. We should see the account that we created on geth console.


Two-Node Setup of a Private Ethereum on AWS with Contract Deployment (Part 2)


Repeat this step on Node 2.


Step 3: Deploy Contract on Node 1


We now deploy the contract on this private blockchain from Node 1.


Node 1 (node)
> abi = <abi shown above>
> VotingContract = web3.eth.contract(JSON.parse(abi))
> byteCode = <bytecode shown above>


As the deployment requires gas, we need to unlock the account and turn on mining


Node 1 (geth)
> personal.unlockAccount(eth.accounts[0])
> miner.start()


Now we deploy the contract. This contract requires a list of candidates. We will include this when deploying the contract.


Node 1 (node)
> deployedContract =['Rama', 'Nick', 'Jose'], { data: byteCode, from: web3.eth.accounts[0], gas: 4700000 })


Now the contract is deployed. And we can execute the functions for verification. We will see the vote increasing when we keep executing the functions.


Node 1 (node)
> deployedContract.voteForCandidate('Rama', {from: web3.eth.accounts[0]})


Two-Node Setup of a Private Ethereum on AWS with Contract Deployment (Part 2)



The contract is successfully deployed. Now we move to Node 2 and see how account in Node 2 can also access this contract.


Step 4: Access Contract from Node 2


Make sure Node 1 and Node 2 have already peered. Verify by > admin.peers on geth console on both nodes. Refer to Part 1 how to do if needed.


The information required for an account in Node 2 accessing this deployed contract is (1) the ABI and (2) its deployed address. We have ABI information already. Now from Node 1 we can obtain this deployed address.


Node 1 (node)
> deployedContract.address


For ABI, we define VotingContract with the same abi on Node 2.


Node 2 (node)
> abi = <abi shown above>
> VotingContract = web3.eth.contract(JSON.parse(abi))


We do not need to deploy a new contract as we did on Node 1. Instead, we define the contract instance based on the VotingContract and the address we obtained from Node 1.


Node 2 (node)
> contractInstance ='0x97a4b975c8fde24d8e40551054342c95676ed959')


Left-hand-side is Node 1, and right-hand-side is Node 2.


Two-Node Setup of a Private Ethereum on AWS with Contract Deployment (Part 2)

Now, contract Instance is the point we can access this deployed contract in Node 2. We can issue the commands just as we do on Node 1:


Node 2 (node)
> contractInstance.voteForCandidate('Rama', {from: web3.eth.accounts[0]})


Don’t forget to unlock the account on Node 2 when voting, as this consumes gas to execute a function on the contract.

Step 5: Verification


By interacting on both nodes, we see accounts on both nodes are acting on the same contract. Here I capture the interaction between the two nodes.


  1. Both nodes see the same result (vote count for Rama is 4).
  2. An account on Node 1 votes for Rama once.
  3. From Node 2 we see the vote count for Rama is now 5.
  4. An account on Node 2 votes for Rama again.
  5. From Node 1 now we see the vote count is now 6.




This setup emulates the real ethereum operation. New nodes can easily join the public chain and can access deployed contract. Of course, in real life, the application is most likely accessed through web interface. Nevertheless, using console helps understanding more the interaction behind.


Once again, thanks for many works by others which inspire and guide my overall setup. Here they are,


  • JJ’s World: Setting up a private Ethereum blockchain (link)
  • Mahesh Murthy: Ethereum for Web Developers (link)
  • MLG Blockchain Consulting: Use Go-Ethereum to Setup an Ethereum Blockchain on AWS (link)


KC began his blockchain journey just a few months ago. His interest is to dig out more detail "how things work", and share with others what he has learnt.

Like what you read? Give us one like or share it to your friends and get +16 bl

Please to comment
newest oldest most voted

marketing dissertation help


dissertation writing company


dissertation dedication examples


phd dissertation editing help


dissertation help service binding

Hungry for knowledge?
New guides and courses each week
Looking to invest?
Market data, analysis, and reports
Just curious?
A community of blockchain experts to help

Get started today

Already have an account? Sign In