Two-Node Setup of a Private Ethereum on AWS with Contract Deployment (Part 2)
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)
This is a simple contract to illustrate how Solidity code works. Here are some points we will refer to in this part.
- The contract requires a list of candidates (see the constructor). We will input this when deploying a new contract.
- 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).
To verify successful installation, use $ node -v and $ npm -v, and their version is shown.
Then install web3 module.
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.
To start the node console, simply use this
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:
On node console,
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.
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.
As the deployment requires gas, we need to unlock the account and turn on mining
Now we deploy the contract. This contract requires a list of candidates. We will include this when deploying the contract.
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.
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.
For ABI, we define VotingContract with the same abi on Node 2.
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.
Left-hand-side is Node 1, and right-hand-side is Node 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:
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.
- Both nodes see the same result (vote count for Rama is 4).
- An account on Node 1 votes for Rama once.
- From Node 2 we see the vote count for Rama is now 5.
- An account on Node 2 votes for Rama again.
- 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)
Create an account to access our exclusive point system, get instant notifications for new courses, workshops, free webinars and start interacting with our enthusiastic blockchain community. Don’t miss out and join right now!
Already have an account? Sign In