Smart Contract in Azure Ethereum Consortium (Part I)

Gilang Bhagaskara
Aug 22, 2017 · 4 min read

If you are like me, a long time web/mobile developer who is just relatively new to ethereum (or blockchain in general) you might find the concept of a blockchain application, or in cooler terms, a dApp, quite confusing. Not only that you are deploying an application to a serverless environment, but also (to comply with the distributed nature) you are supposed to call the application from no server but from a blockchain node. Ethereum smart contract is easy to code, but for first timers like me it has a very steep learning curve just to deploy and interact with it. With the lack of documentation and the scattered Q and A’s of stackoverflow one can have a hard time just trying to make their code work. Most likely you will get stuck with the testrpc sample codes and be left wondering: am I ready to actually tackle a real-world blockchain problem?

If you are a lucky developer who works with a private ethereum network, it is a more miserable journey. I myself am working on a project on top of Microsoft Azure Ethereum Consortium private blockchain. It was quite easy to follow the installation and initial setup using this guide but there were definitely a lot more to do afterwards. How do I create and deploy a smart contract? How do I use web3.js instead of Metamask? How do I call a function from the smart contract that I just deployed? The questions go on and on and you are left bemused with what to do next.

The dashboard of your new Ethereum deployment

I create this post to share my experience with Ethereum Consortium blockchain as it is very hard to find a complete guide of what to do next after deployment.


The Setup

If you follow the TechNet guide correctly, you will end up with a running ethereum blockchain network with at least a transaction node and two miners. You will have already made a few accounts using your metamask, send a few ether, and probably have deployed the default StateHolder.sol smart contract using the remix online compiler. In this series, I will share you how to use truffle and react to deploy and interact with your smart contract. But first, let’s start with the basics.

To get things started, we will need a few items:

  1. your favorite terminal (i use cmder)
  2. your favorite code editor (i use VSCode)
  3. geth
  4. web3.js
  5. truffle framework

If you have downloaded and installed everything correctly, you are almost ready to code.

There is one thing that the Ethereum Consortium does not tell you: when you attach to its console from public, its API only includes a few modules. To test this out, enter this command in your terminal:

geth attach http://yourServer.region.cloudapp.azure.com:8545

and you will enter the console remotely. You will see that there are only the following modules loaded into the console:

eth:1.0 net:1.0 rpc:1.0 web3:1.0

This is bad because you will not be able to use personal.unlockAccount or personal.newAccount while the command is crucial to let you deploy a contract to your permissioned network. At the same time, it is more secure because once you open a module your node is vulnerable to the public internet.

However, if you ssh into the server and do geth attach inside it, all modules are loaded and the commands are available.

admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

This msdn post helped me through this issue. The solution is to change a file in the server that it uses when starting up, which is $/start-private-blockchain.sh inside the home directory. So let’s edit that file by explicitly stating that you need to load the modules.

Load up vim start-private-blockchain.sh and find the following line:

nohup geth --datadir $GETH_HOME -verbosity $VERBOSITY --bootnodes $BOOTNODE_URLS --maxpeers $MAX_PEERS --nat none --networkid $NETWORK_ID --identity $IDENTITY $MINE_OPTIONS $FAST_SYNC --rpc --rpcaddr "$IPADDR" --rpccorsdomain "*" >> $GETH_LOG_FILE_PATH 2>&1 &

then change it into:

nohup geth --datadir $GETH_HOME -verbosity $VERBOSITY --bootnodes $BOOTNODE_URLS --maxpeers $MAX_PEERS --nat none --networkid $NETWORK_ID --identity $IDENTITY $MINE_OPTIONS $FAST_SYNC --rpc --rpcaddr "$IPADDR" --rpccorsdomain "*" --rpcapi "eth,net,web3,admin,personal" >> $GETH_LOG_FILE_PATH 2>&1 &

We basically just added the --rpcapi command into it. Save the file and restart your VM. After restarting, attach the geth console again, and you will see that all the modules have been added.

Afterwards, you can unlock your account easily.

Next Step

In this post we have set up our development environment and we are ready to develop our app. In our next post, we will learn how to interact with our blockchain using web3.js so if you have any input or questions please feel free to leave a comment below. Thank you!

)

Gilang Bhagaskara

Written by

Blockchain enthusiast. Web & Mobile developer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade