We will walk through the process of setting up a Blockchain development environment in this blog.
In this exercise, we will do the following
- Setup a minimal Private Ethereum Blockchain Network with one mining node and one transaction node in Microsoft Azure
- Setup the basic smart contract development environment on your local computer
- Write and deploy a simple sample contract in the local machine
- Deploy the same in the Private Ethereum Blockchain Network
We have a lot to do, so let’s get started …
If you choose to skip the Private Ethereum Blockchain creation and wanted to just test in your local environment you can skip the Step 1 of this Excercise.
Step 1: Setting up a Private Ethereum Blockchain in Microsoft Azure
Setting up your Azure account is beyond the scope of this exercise, it is free to sign up, and you receive a 100$ credit when you set up your Azure account which will be more than enough for you to complete this tutorial
- Go to https://portal.azure.com and click on Create a Resource and select Blockchain, from the Featured, choose Eethereum Consortium Blockchain option
- In the Basics page Enter the following and Click OK
-
-
- Resource prefix
Put a String that will be used as a base for naming all the resources that will be created for this tutorial - VM username
Leave the default which is “gethadmin“ - Authentication type
Let’s make it simple by selecting Password option - Password
Type in a password that you will remember - Resource Group
Create a new Resource group and name it - Location
Select an Azure location of your choice
- Resource prefix
-
-
- In the Network Size and Performance Page Enter the following and Click OK
- Number of Consortium members: 2
This means the number of members of this blockchain. Each of this participants gets a mining node. - Mining Nodes: 1
This means you will have 1 mining node per participants in this Blockchain. This node will be responsible for mining blocks in your blockchain - Mining Node Storage Performance: Standard
Selecting the default and cheapest option - Mining node Storage replication: LRS
Select the default value - Mining node virtual machine size: 2X Standard D1 V2
We do not want a very powerful machine for this tutorial so let’s get a small one - Transaction Nodes: 1
This node will be submitting its transactions this Blockchain - Transaction Node Storage Performance: Standard
Selecting the default and cheapest option - Transaction Node Storage replication: LRS
Select the default value - Transaction Node virtual machine size: 1X Standard D1 V2
We do not want a very powerful machine for this tutorial so let’s get a small one
- Number of Consortium members: 2
- In the Ethereum Settings Options enter the following and Click OK
- Network ID: 111222333
This id is used to name this private Blockchain network, Only the nodes with the same id can peer with each other. - Custom Genesis Block: No
Custom Genesis Block can be provided as a JSON if you are creating a Blockchain for a specific use. Since its the basic tutorial lets allow the mining node to create our Genesis Block. - Ethereum Account Password: <Enter a password of your choice>
This will be the password of default ethereum account. Note this password down; we will use this later - Ethereum private key passphrase:
This passphrase will be used to create the private key of the default ethereum account
- Network ID: 111222333
- View the Summary on the next page, Click Ok
- Accept the Terms and Conditions and Click OK
- Let Azure do its job and wait for approximately 10 mins to see your Blockchain Network Completely in the Azure Portal
- Click on the Name of the Resource Group that you created -> Deployment and select Microsoft-azure-blockchain.azure-blockchain–XXXXXXXXX to look at the details of your blockchain
- Note down the following information from the Details page
- Admin Site URL
- ETHEREUM-RPC-ENDPOINT
- SSH-TO-FIRST -TX-NODE
- Copy paste the URL of admin site into your browser, and you should see something similar to the below image
- Our Blockchain Network is up and running. However, we need to we need to unlock the default account of this Blockchain to make any modifications to it. To unlock the default account, we have to SSH into the Transaction node.
- Open the Terminal on your Mac or Linux system or use Powershell in windows to SSH. Use the SSH-TO-FIRST -TX-NODE value that we copied from our previous step to Open an SSH Connection.
Ignore the warning that you will receive when you first SSH to the transaction node about the authenticity of this host.Use the password that we provided for Ethereum Account Password: in the previous step - Type in the Following Command to connect to Geth in the Transaction node.
$ geth attach
Geth is a javascript runtime environment (JSRE) provided by Ethereum to interact with Ethereum. You can find more details about Geth here
- In the Geth Console type in the following command to unlock the default account.
personal.unlockAccount(eth.coinbase)
eth in this context is a shortcut for web3.eth
- This will ask for the Passphrase, Provide the passphrase we created in the previous step. You should get a response “true” if the passphrase is correct
That’s it your Ethereum Private Blockchain is ready to roll
Step 2: Setting up your Local Development Environment with Ethereum Essentials
Lets now set up your local development environment with the essentials. We will need to do the following.
- Download and install Visual Studio Code from https://code.visualstudio.com
Install the following plugins for Visual Studio Code
- Download and install NPM (Node Package Manager)
Go to https://nodejs.org/en/ and download the latest LTS Version of Node. At the time of writing this blog 8.9.4 is the current version, so we are going with that. - Install the following Node packages using NPM (For opening npm use console in mac and Linux or use Powershell with option Run as Administrator in Windows)
- windows-build-tools (Only on Windows)
Windows-build-tools are used to compile native node modules in windows. you can find more information about windows-build-tools at https://www.npmjs.com/package/windows-build-toolsnpm install --global --production windows-build-tools
- truffle
Truffle is a development environment for ethereum; We will use this to create and compile our smart contract. you can find information about truffle at https://www.npmjs.com/package/truffle
Use the following command for windowsnpm install -g truffle
and this one for Mac or Linux
sudo npm install -g truffle
- Ethereum-testrpc
testrpc is an Ethereum client used for testing and development. More information about testrpc can be found at https://www.npmjs.com/package/ethereumjs-testrpc
Use the following command for windowsnpm install -g ethereumjs-testrpc
and this one for Mac or Linux
sudo npm install -g ethereumjs-testrpc
- windows-build-tools (Only on Windows)
Step 3: Create your first Smart Contract
In this step, we will create a smart contract, test it locally in testrpc and deploy it to your newly created Blockchain network and run it.
We will be writing this contract using Solidity. Learn more about Solidity and its syntax at https://solidity.readthedocs.io/en/develop/index.html
The Smart Contract that we are going to just print HelloWorld. We will use Truffle to do the following
- Initialize the project
- Add a Smart Contract
- Compile the contract
- Deploy the contract to local testrpc server and to the Private blockchain
-
Setting up the Project
- Create a new folder called “HelloWorld” on your local machine and open the PowerShell or terminal window pointing to HelloWorld folder
- Initiate the Project by calling the unbox command.
$ truffle unbox
Truffle Boxes are boilerplates that you can use for kickstarting your development. You can read more about truffle boxes from http://truffleframework.com/boxes/
unbox command without any arguments creates a default MetaCoin contract.
Since we are not dealing with MetaCoin in this exercise, we will clean this project up in a later step. Open the folder using the Visual Studio Code, and you should see a structure similar to this.
-
Create a new Smart contract
You can either create a contract manually, or you can use truffle to create a new contract. We are going to use truffle to create the new contract.
Type in the following command in the terminal / PowerShell$ truffle create contract HelloWorld
If you look in the contracts folder of your project, you can see that Truffle had created a new Solidity file HelloWorld.sol Another Option is to create a new file manually in contracts folder with the name HelloWorld.sol
Your Contract name must match with the Solidity file that you create for that contract.
-
Write the Logic of our Smart Contract
Truffle “create contract” has created a basic skeleton for our contract. Since the logic of our smart contract is very simple to copy paste the following code to the HelloWorld.sol
pragma solidity ^0.4.4; contract HelloWorld { function HelloWorld() { // constructor } function sayHello() public returns (string) { return ("Hello World!!!"); } }
Before we compile our contract, Let’s clean up our project. Delete the following files from the contacts folder.
- ConvertLib.sol
- MetaCoin.sol
-
Compile our Smart Contract
Since we are using truffle to compile our contract, type in the following command in the terminal to compile our smart contract
$ truffle compile
Truffle will compile the code and will generate a build folder in the project. If Examine the build/contracts folder You will find HelloWorld.json which has the deployable binary.
-
Deploy Smart contract in local testrpc
We are using truffle to deploy our contract, to let the truffle know that we are deploying this contract in our local folder we first have to make sure that we have localhost as a destination in the truffle.js file. You can find the truffle.js file in the root folder of your project. Truffle unbox command should have pre-populated with the localhost network. Open your truffle.js and make sure that you have the following entry.
module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*" // Match any network id } } };
Truffle requires having a “Migrations contract” to use the migrate feature. You would have noticed that we did not delete “Migrations.sol” in the contracts folder when we did the clean up in the previous s step. Open 2_deploy_contracts.js from migrations folder. This is the file that truffle uses to determine the contracts to deploy, Clean up this file to remove the reference to the files that we deleted and add a reference to our HelloWorld Contract. Change the 2_deploy_contracts.js to this.
var HelloWorld = artifacts.require("./HelloWorld.sol") module.exports = function(deployer) { deployer.deploy(HelloWorld); };
We now need to start our local testrpc server. Open a new Terminal / PowerShell window and type the following command
$ testrpc
That will start your testrpc server, testrpc comes with 9 accounts, and the first one is the default account. The private keys listed below are the private keys of the accounts. testrpc runs on port 8545
Run the following command in the terminal to deploy the contract to testrpc server.
$ truffle migrate
If you see a screen similar to this means your contract has been successfully published to the testrpc server. -
Execute the Smart Contract
We will use truffle console to execute our HelloWorld contract.
Truffle console is a javascript console that can be used to interact with smart contracts.
Type the following command on the terminal to open truffle console.$ truffle console
- Since this is a javascript console define a variable that will hold the reference to the contract.
var hwcontract
Just like the Google Chrome or any other javascript consoles when you define a variable you will get a message “undefined” You can ignore it.
- In the testrpc, we need to Get a reference to our contract asynchronously since truffle executes the commands line by line make sure that the complete
HelloWorld.deployed().then(function(deployed){hwcontract=deployed;})
- Execute the contract in the asynchronously
hwcontract.sayHello.call()
You should be able to see the Hello World in the console
Congratulations you had successfully executed your first smart contract in testrpc.
- Since this is a javascript console define a variable that will hold the reference to the contract.
-
Deploy HelloWorld contract to our Private Blockchain
To deploy this contract to our newly created blockchain, we need to update the truffle.js with the network information of the blockchain network that we had created in our previous step.
Update the truffle.js with the following snippet.
Replace the xxxx with the hostname of ETHEREUM-RPC-ENDPOINT that we copied in the previous step.
Remember the host value should be just the hostname of our endpoint. So do not add the https or the port name to the host value, just add hostname.network_id is the id that we entered as network id when we created our blockchain
module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*" // Match any network id },prod: { host: "xxxxxx", port: 8545, network_id: "111222333" } } };
- We need to unlock the default account of our blockchain before we start the deployment, Repeat the steps 12, 13, 14 and 15 from Step 1 to unlock the default account
- Now Use the truffle with network parameter to migrate out the contract to the network.
Go to the terminal issue and type in the following command$ truffle migrate --network prod
This might take some time.
Once the migration is completed, you should see a screen like this
-
Execute your Smart contract in the real Blockchain
We will use truffle console here also to execute our contract.
In order to connect to a remote network we need to use use the network parameter while starting the truffle console.
Type the following command on the terminal to open truffle console.$ truffle console --network prod
- Like in the previous step define a variable that will hold the reference to the contract.
var phwcontract
- Get a reference to our contract
HelloWorld.deployed().then(function(deployed){phwcontract=deployed;})
- Execute the contract. If you notice here instead of calling the contract asynchronously, we are directly executing the contract here.
pcontract.sayHello()
The execution on the real blockchain is going to be a lot slower than the testrpc. However, you should be able to see the response like this in your console.
- Like in the previous step define a variable that will hold the reference to the contract.
Congratulations, you successfully created and deployed your first smart contract. This is just a beginning. We will do more exciting programs in the future.
You must be logged in to post a comment.