Setting up a private Ethereum node on Windows Operating System
Written by Dr. Amol Dhumane
Tasks done :
1. Create and configure the genesis block
2. Initialise the genesis block
3. Attach to the private blockchain using javascript console
4. Check the mining process
5. Transfer some ether from one account in to other account.
--------------------------------------------------------------------------------------------------------------------------
1. Install Geth
2. Install Ganache (it is an Emulator)
3. Open powershell window
4. Create a ‘chainskill/private’ directory using ‘mkdir –p chainskill/private’
5. Chainskill will be the name of our network
6. Now we have to define a genesis block (starting block of blockchain). Creating a genesis block from scratch is painful and reusing an existing one can be a problem with if its version is older than that of your current installation of Geth. Therefore your genesis block file must be compatible with your Geth version. So we will use a tool ‘puppeth’ for creating it.
7. Puppeth is a command line tool that is installed with Geth and it provides various features for creating and monitoring your own Ethereum network.
8. So we will use puppeth to create a genesis block file
9. Go to chainskill/private and run puppet.
10. Give a name to our network. – ‘chainskills’
12. Select option 2
13. Go on pressing enter till you get “Specify your chain/network ID if you want an explicit one”. Give etwork ID as 4224 (as ID 1 is allocated for mainnet, ID 2 is allocated for Modern Testnet—which is obsolete now, ID 3, 4 and 42 are assigned to Ropsten, Rinkeby and Koyan Testnetworks.
14. Select ‘Manage Existing Genesis’ and then select ‘Export Genesis Configuration’
15. This will export the Genesis Block ‘chainskills.json’ into ‘chainskill/private’ directory.
16. Now the work with puppeth is finished. ‘puppeth’ is used for creating and exporting the genesis block.
17. Press [ctrl+c] and exit from puppet.
18. Write ‘ls’ on the powershell to check the ‘chainskills.json’ file in the private folder.
19. Now next step is to do necessary modifications in the ‘chainskills.json’ file
20. Open the ‘chainskills.json’ file using any text editor (like notepad, atom etc)
21. "config" section of genesis block defines the parameters of the chain. “chainId” defines network identifier. “timestamp” field decides the level of difficulty. If it is more, the difficulty level is less else it is high. The “timestamp” field also helps to ensure that the blocks are in the correct order. “GasLimit” decides the maximum value of total gas that transactions in a block can spend. This value is here to limit the processing requirements of each block as well as to limit the size of the block. “difficulty” field defines the difficulty level to validate the block. It directly related to the average number of times the miner has to run the hashing function to solve a consensus puzzle. In private blockchain, keep this value low for reducing the computation time. “coinbase” is the address of the account, that will get the mining rewards. “alloc” is a section where we pre-allocate some funds to wallet addresses. This section is very big, but we can reduce it using the notepad/atom editor. "number" contains the block number. For genesis block it is 0. "gasUsed" is sum of all the gas used by all the transactions in the block. There is no transaction yet, so its 0. "parentHash" contains hash of the parent block. Genesis block has no parent so it is 0.
22. This is the end of monitoring of the genesis block. So close the editor now.
23. Now we will use this genesis file to initialize our own private node
24. Now go to powershell
25. Now we will use this genesis file (chainskills.json) to initialize our private node. Create the private node using ‘geth’ command “geth --datadir . init .\chainskills.json”. Here all the data of the private node will be stored into local (current) directory and ‘init .\chainskills.json’ will be used to initialize the genesis block.
26. So after initialising the genesis block, in our private directory in addition to ‘chainskills.json’ file, we have ‘geth’ and ‘keystore’ directory. ‘Geth’ directory will store all the data related to the chain and ‘keystore’ directory will store all the data related to the accounts.
27. Right now if you check the content of keystore directory, it is blank. There is nothing.
28. Now in the next part of this article, we will create 3 accounts. The first account will receive all the mining rewards and other two accounts will be of ‘buyer’ and ‘seller’ in the dapp (decentralized app) that we will built soon.
29. For creating the new account we will use ‘geth’ command “geth --datadir . account new”
30. Go to ‘keystore’ directory and check its content using ‘ls’ command, you will see 3 files (one for each account) in that folder.
31. To get the list of account use ‘geth --datadir . account list’ command.
32. Even though we have created 3 accounts, it hasn’t started yet. For starting our private node to start the mining of blocks, we will create a script which will define all the required parameters to let us interact with our private node. We will call this script as ‘startnode.cmd’. Create it using atom or notepad editor.
33. Geth --networkid 4224 --mine --miner.threads 2 --datadir . --nodiscover --rpc --rpcport 8545 --port 30303 --rpccorsdomain * --nat any --rpcapi eth,web3,personal,net --allow-insecure-unlock --password ./password.sec
34. Create the password.sec file by putting password of account into it.
35. Execute the script by “.\startnode.cmd”
36. One important thing here to note is ‘why does our node mine even though there are not any transactions in the network?’ ----- first of all it will mine the genesis block. Then you will see that the node will start mining at regular basis even though there is no work/transactions yet. This is because the role of mining process is not just to mine the transactions into blocks, but to create new ether also. This new currency must be created on regular basis. So every few seconds for example, a new block should be created, should be mined to create new quantity of ether, whether there are pending transactions or not. So for usual implementations of this proof-of-work algorithm on Ethereum, even on the main net, you can find blocks that are actually empty and don’t contain any transactions.
37. Now our node start mining, we will now attach a javascript console to it.
38. Open a new powershell window.
39. Use the command “geth attach ipc://./pipe/geth.ipc” to open a javascript console for attaching to our already running node in the earlier powershell window.
40. Check the ‘datadir’ for making sure that we have connected properly to our already running node and also check the ‘coinbase’ also.
41. Use command ‘eth.accounts’ to list all the accounts. (here we have created 3 accounts, so it should display the information of these three accounts).
42. Use command ‘eth.coinbase’ to check the coinbase address.
43. We can also check the balance of the accounts using ‘eth.getBalance(eth.accounts[0])’ or ‘eth.getBalance(eth.accounts[1])’ or ‘eth.getBalance(eth.accounts[2])’ or ‘eth.getBalance(eth.coinbase)’
44. Please note that the getBalance function returns the balance in ‘Wei’ which is equal to 10 to the power -18 Ethers.
45. ‘Wei’ is smallest unit of ether.
46. If we want to convert ‘Wei’ into ‘Ether’, use the command ‘web3.fromWei(eth.getBalance(accounts[0]), “ether”)’
47. We can stop and start the mining process from the javascript console using ‘miner.stop()’ and ‘miner.start()’ commands
48. Please find the list of commands as given below: https://geth.ethereum.org/docs/interface/command-line-options
49. Use command ‘net.version’ to get the network ID.
50. Unlocking account: Unlocking account means opening the private key for signing the transactions. By default, private keys are locked and they can only be used if you do have password and you can unlock them to a certain duration that we can specify in the command.
51. Use command ‘personal.unlockAccount(eth.accounts[0], “geth123”, 300)’. Here “geth123” is a password and “300” is a time (written in seconds) for which we need to keep the account unlocked. After firing command, if it returned ‘true’ value, it means that the account is unlocked successfully.
52. Now lets transfer some Ether from coinbase account to other account. Use command ‘eth.sendTransaction({from: eth.coinbase, to: eth.accounts[1], value: web3.toWei(10,”ether”)}).
53. After executing above command, the ‘transaction hash’ gets displayed on the javascript console.
54. If we went to the previous powershell which is executing the node, we can see that the new transaction is submitted and added into next block.
55. Similarly we can send money to account 1 to account 2 also and we can check the activities in the powershell window running the Ethereum node. Also we can check the balance of accounts using ‘eth.getBalance(accounts[0/1/2])’ commands.



Comments
Post a Comment