Skip to main content

Getting Started with establishing a Private Network for Ethereum

Although there are a considerable amount of references available on establishing a public network using Ethereum, there's quite a few concerning private networks.

Some of the important points to be noted are,


  • The network Id should be any other value than 1. 1 is the default networkId of the main network. So when the networkId is different your nodes will not connect to the main network, thereby creating an isolated, private network.


  • You can either pregenerate or mine your own ether.

  • Should have a custom genesis file.



  • Should have a separate custom data directory for the nodes.


  • Node Discovery should be disabled. This is so that the node is not discoverable by people who do not add you manually. If not, your node may get added to an unknown blockchain having the same networkId and genesis block.

Step 1 - Custom genesis file

The protocol validates if the blockchains on two nodes has the same genesis file in order to identify if its the same blockchain. If it is different they cannot agree on your blockchain.



Step 2 - Launch geth


  • Before launching geth, you need to define a data directory inside the same directory you have the genesis file. This is where all the blockchain data will be saved.

geth --identity "Node1" --rpc --rpcport "8080" --rpccorsdomain "*" --datadir "TestChain" --port "30303" --nodiscover --rpcapi "db,eth,net,web3" --networkid 1999





Now the genesis block will be initialized. Whenever we are using the geth command the previous custom flags should be used as below. Otherwise it will not remember.


geth --identity "Node1" --rpc --rpcport "8080" --rpccorsdomain "*" --datadir "TestChain" --port "30303" --nodiscover --rpcapi "db,eth,net,web3" --networkid 1999 console


If you need to access the geth console in a different terminal, it should be attached by giving geth attach. If it gives an error as below, path to the geth.ipc should be given. This should be inside the data directory specified.






Step 3 - Creating a new account on the Testnet

Create an account by giving the following command in the console.

personal.newAccount("123")

123 can be replaced with your own password for the account.



Purpose of this account is to set this as the account that receives the mining rewards. In order to do that we have to set it as the etherbase. 


Step 4 - Setting the Etherbase

The account we created before should be now set as the etherbase. For that first check which element it is in the accounts list by retrieving all the accounts.


You can retrieve all the accounts by giving,

eth.accounts



Since it is the 0th account, it can be given as follows.


miner.setEtherbase(personal.listAccounts[0])





Step 5 - Mine test Ether

Give the following command to start mining ether.


miner.start()




Step 6 - Adding the account to Genesis File

The following line should be added under the genesis file.


"0x45e0aa3792e3d6c44521e10cd862fb9b830ff78c" : { "balance": "20000000000000000000" }


replacing the account number with yours.

The balance added in order to preallocate ether to the account.


After this rerun your geth private chain command. And get the accounts list and check if your account has been added.

geth account list

Comments

Popular posts from this blog

Admin panel of a Q & A Forum

In a Q & A Forum, when a user posts a question, it should be sent to the administrator for approval in case it contains inappropriate content. After approval it should be removed from this pending approval page and other users should be able to see the question afterwards. To enable this, we should maintain an approval column in our database table of records and for each record approval should be set to false by default. In the Pending approvals page only the records with approval=false should be displayed. Below is  the MySQL  statement for retrieval, $sql="SELECT * FROM topics WHERE approval=false"; To know which post was approved we should embed the post_id to the URL. And the relevant post should be updated as approval=true. Below is the complete code. <?php $sql="SELECT * FROM topics WHERE approval=false"; $query=mysqli_query($conn,$sql); echo '<form name="approve" method="p...

Getting Started with OAuth 2.0 using WSO2 Identity Server 5.3.0 and Playground2 Sample

This blog post provides step by step instructions for trying out OAuth 2.0 using WSO2 Identity Server . Here I use Identity Server 5.3.0 which is the latest released version by the time of this writing. The official documentation for this is available in https://docs.wso2.com/display/IS530/OAuth+2.0+with+WSO2+Playground , however for a beginner, it does not provide all the instructions such as creating a Service Provider with necessary configuration. However, by following the steps below, you can simply setup Identity Server and the playground2 sample webapp and test the entire OAuth 2.0 flow. Creating the Service Provider First step is to create a service provider in Identity Server. This is required because when a client application talks to Identity Server via OAuth 2.0, Identity Server has to identify the client and the incoming traffic. We set this configuration inside the service provider. Login to the Management Console of Identity Server and create a service provi...

Interacting with an Ethereum Smart Contract using Meteor and Ethereum

This blog post contains steps on how to interact with a specific contract on the blockchain extending the application created in the previous blog post [1]. Step 1: Add  frozeman:template-var This is used since we use callback very often. This wrapper can be found at [2]. Give the following command in order to add it to our application. meteor add frozeman:template-var Step 2: Adding the balance to be viewed inside the template Alter your code as below. main.js main.html When you refresh the browser, you should get the below output without having to click the button. Step 3: Writing the Smart Contract in Solidity Solidity is the language that ethereum uses to write smart contracts. In order to write the smart contract for our application we will use the online Solidity compiler which can be found at [3]. Step 4: Deploying the Smart Contract Log in to the MetaMask and make sure you are on the testnet. Select 'Create' in the right side p...