Beginners guide to Ethereum (5) — Manage information of your blockchain & what is inside a block


In this tutorial, I will first set up a blockchain on private network, make a few transactions and load a script to fetch information of the blocks with transactions and make some simple analyzations. You can take this as an example on how to increase productivity by writing scripts with your own functions and settings.

Start coding!

  1. Create a genesis file which sets the mining difficulty low
{"config":{},"gasLimit": "2000000000","difficulty": "10","alloc": {}}
geth init genesis.json --datadir higeth --networkid 123 --datadir hi
geth attach /Users/tina/hi/geth.ipc  //replace the highlighted part with your ipc address
for(var i=0;i<2;i++) personal.newAccount('')accounts = eth.accountspersonal.unlockAccount(accounts[0], '')miner.start() //get some funds in your coinbase address to execute transactions//wait a few seconds for miningeth.sendTransaction({from: accounts[0], to: accounts[1], value: 100}) 
eth.sendTransaction({from: accounts[0], to: accounts[1], value: 300})
//wait a few seconds for miningminer.stop()
touch test.js
data = []; // An array storing all transaction blocks detailsjson = {}; // the final output we want totalCost = 0; // total gas cost in Wei(a unit)for(var i=1;i<=web3.eth.getBlock("latest").number;i++) {    web3.eth.getBlock(i, function(error, result){        data.push.apply(data, result.transactions); // merge the result.transaction array into the "data" array(It's an array because there may be multiple transactions in one block)    });}json.length = data.length;json.blockInfo =[];for(var i=0;i<data.length;i++){    block = eth.getTransaction(data[i]);    json.blockInfo.push(block);    totalCost += block.gasPrice * web3.eth.estimateGas({from: block.from, to:});}json.totalCostInEther = web3.fromWei(totalCost, 'ether');
loadScript('/Users/tina/Desktop/test.js') // replace this with your file path
{blockInfo: [{blockHash: "0x2d42f2cdaeceb1a46d349c6bcdcb8fb93654444e6bb3c98aa53b147f542f5aaa",blockNumber: 8,from: "0x47f011ee8dee42f52ff2c1bd9990421e2db30af4",gas: 90000,gasPrice: 18000000000,hash: "0x4624057c4c1b3add9cf86c7c7132fbd6e9cf550b4d068019987799892d52712b",input: "0x",nonce: 0,r: "0x510fb16aa71dd9643c81f2af9b7c08f8c28edb2ca0ef366672ef4994aa06944e",s: "0x59f9653d526e13923fc190cbcf864689c428f4b9fcec9d768b48cca237c1fdfe",to: "0xc6da0e6b73866997073f4ba9e2203717c129baab",transactionIndex: 0,v: "0x1b",value: 100 // the money in Wei you sent from accounts[0] to accounts[1]}, {blockHash: "0x2d42f2cdaeceb1a46d349c6bcdcb8fb93654444e6bb3c98aa53b147f542f5aaa",blockNumber: 8,from: "0x47f011ee8dee42f52ff2c1bd9990421e2db30af4",gas: 90000,gasPrice: 18000000000,hash: "0x8f7f1e4fa9ffe84794d20b0d9d20571b51cc70c25a428c7e2c00c4d4c3a7f610",input: "0x",nonce: 1,r: "0x30469d1dc3eb9e4a3e857537c59e56ae19b34cdffce78fc7abaa24da4429d43f",s: "0x52909d07d25653a8f416db76c306b84328842953c5a9ef9aff0c678e6ebb787d",to: "0xc6da0e6b73866997073f4ba9e2203717c129baab",transactionIndex: 1,v: "0x1c",value: 300}],length: 2,totalCostInEther: "0.000756036"}

Wrap up

Now you have created your first script for your blockchain which manages all the transaction data:) You can also write your own script to play around with other data in your chain.

Founder of Z Institute | Researcher @ Evernew Cap | Berkeley Blockchain Lab | HKUST | NTU | Model ( | Projects:

Founder of Z Institute | Researcher @ Evernew Cap | Berkeley Blockchain Lab | HKUST | NTU | Model ( | Projects: