Blockchain across Oracle
上QQ阅读APP看书,第一时间看更新

Hashing a block

Before a block can be finalized, meaning creating the block and its header structure, designated participants (sometimes called miners, forgers, or endorsers) in the peer-to-peer network need to approve the transactions, for example, by solving a mathematical problem in the case of Bitcoin. This answer, an arbitrary integer called a nonce, is then distributed to other participants that validate the answer. To understand the nonce, let's look at the following example. Let's say that we have a JSON payload of a transport order:

{
"transportOrder": {
"shippingDate": "2017-7-31T18:00:34.011Z",
"originWarehouse": 12,
"destStore": 3,
"products": [{
"eanCode": "8806088280486",
"productName": "Samsung UE40K5600",
"productCode": "UE40K5600AWXXN",
"amount": 4
},
{}
]
}
}

If the payload is hashed with the SHA(2)-256 hash algorithm, the outcome would be as follows:

93eec6bc317252e5682f9201831c087c1e25ee5b9d84215a2e64fca0909c9696

Both the transaction and the block are hashed using a hash function, such as SHA-256 or KECCAK-256. However, when hashing a block, there is a little twist. With most public blockchains out there at present, you are rewarded when finalizing a block with a mathematical puzzle that needs to be solved. For example, you need to hash the block in a particular way such that the output hash starts with a (variable) length of zeros. The length of zeros can be based on the current difficulty level. Even if you are not rewarded, it helps with hardening the hash for security reasons.

For example, when the current difficulty level (result of a formula: https://en.bitcoin.it/wiki/Difficulty) expects the hash to start with four zeros, then you need an arbitrary number to be added to the blocks, payload in order to generate a valid target hash. In the following screenshot, the nonce is not yet calculated, and the hash does not start with the requisite number of zeros, which makes the hash invalid:

This example calculation can be executed using the blockchain demo at https://anders.com/blockchain

The hash needs to solve a mathematical puzzle to calculate the correct nonce so that the hash starts with four zeros. When executing the mathematical puzzle, it tries integers in sequence. The speed at which these calculations can be executed depends on the amount of computing power that you have available.

After solving the correct nonce, the block hash is valid since it starts with four zeros

The nonce that resolves to a valid hash becomes part of the block header and is distributed to other participants that can validate the block based on the solution provided.