In blockchain every block is having three things,

  1. Hash address of the previous block
  2. Hash of the all the transactions those are part of the current block
  3. Nonce, a random value which can be got using hit and trial method. It is a predermined value and once a right value for the block is found then this block can be added into the blockchain.

Please confirm my understanding and correct me if I am wrong.

Now I have a question about the hash address of the previous block.

How do we get the hash address of the previous block while adding a block into block chain?

Thanks for reply.


I reference to the python implementation (https://github.com/ethereum/pyethereum/blob/develop/ethereum/block.py). What you can see there is the following list of tuples:

fields = [
        ('prevhash', hash32),
        ('uncles_hash', hash32),
        ('coinbase', address),
        ('state_root', trie_root),
        ('tx_list_root', trie_root),
        ('receipts_root', trie_root),
        ('bloom', int256),
        ('difficulty', big_endian_int),
        ('number', big_endian_int),
        ('gas_limit', big_endian_int),
        ('gas_used', big_endian_int),
        ('timestamp', big_endian_int),
        ('extra_data', binary),
        ('mixhash', binary),
        ('nonce', binary)

Now we have to go through three steps to get our block hash:

  1. Serialize the Block object.

The field variable is a list containing tuples. This can be considered as an dictionary, containing key to value mappings like 'prevhash' -> hash32. The serialization is in this case a process where a list is created and appended by only the values of the dictionary (in the correct order). So what you are left with after the serialization is a list containing the values for the following descriptive key words:

['prevhash', 'uncles_hash', 'coinbase', 'state_root', 'tx_list_root', 'receipts_root', 'bloom', int256), 'difficulty', 'number', 'gas_limit', 'gas_used', 'timestamp', 'extra_data', 'mixhash', 'nonce']

  1. Apply RLP encoding on the serialized "Block" object.
  2. Get the Keccak-256 hash from the result of the RLP encoding from step 2. The resulting hash is the block hash you were looking for.

You compute it, since every miner has the previous block stored.Usually the value that is used is the hash of the previous block header since it contains all the information required to detect changes in the block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.