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

李婷婷 Lee Ting Ting
3 min readJul 19, 2017



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

cat genesis.json


{"config":{},"gasLimit": "2000000000","difficulty": "10","alloc": {}}

Press Enter then Ctrl+Z to save and exit

2. Init your Ethereum node with geth init

geth init genesis.json --datadir higeth --networkid 123 --datadir hi
  • Remember to make sure the “hi” folder is empty or does not exist. Also remember to remove this folder if you want to restart your blockchain: rm -rf hi


IPC endpoint opened: /Users/tina/hi/geth.ipc

  • Note: IPC stands for Inter Process Communication. It creates a pipe which enables bi-directional communications on your same computer. Read more here for detailed explanation and its difference with RPC.

3. Open another terminal window and open a geth console connecting to this node

geth attach /Users/tina/hi/geth.ipc  //replace the highlighted part with your ipc address

4. create two accounts and make a transaction

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()

5. Create our js script to fetch and manage your blockchain data. In the final json output, we will have blockInfo, which stores all the details of transaction blocks, length, total block number, and totalCostInEther, which multiplies all the estimate gas cost with gasPrice.

touch test.js

Inside 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');

6. Save and load this script in your geth console

loadScript('/Users/tina/Desktop/test.js') // replace this with your file path

7. Your script is loaded and performed correctly if it returns “true” :)

See the json result by using the following command.



{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"}

Parameters in the transaction block is explain in here. “r”, “s”, and “v” is for the use of digital signature.

There are a few things we can see:

(1) The block number and block hash is the same because we make the two transactions at almost the same time, so they are stored in the same block and mined together.

(2)The “from” and “to” parameter is the address of accounts[0] and accounts[1], respectively.

(3)The value of “nonce” is 0 and 1 as this is the first and second transaction in our blockchain.

(4) “totalCostInEther” is not simply equal to gas*gasPrice as the “gas” value here is the amount of gas provided by the sender.

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.

Thanks for your reading! Suggestions are all welcomed :)



李婷婷 Lee Ting Ting

Founder of Z Institute | Blockchain dev & security audit | Berkeley Blockchain Lab | HKUST | IG: tinaaaaalee | Github: tina1998612