In this exercise, you will practice writing code to create blocks, compute block hashes, and verify blocks based on those hashes.
You are provided lines of text from a poem, and you should loop through them and add each line as its own block of data to the provided blockchain.
createBlock(..) function which takes the text for its data, creates an object for the block, and computes its hash, finally returning the block object. Insert this object into the
blocks array for the blockchain.
Each block should have the following fields:
index: an incrementing number that’s the 0-based position of the new block within the
blocksarray; the genesis block has
0, so the next block will have
1, and so on
prevHash: the value of the
hashfield from the last block in the
data: the string value passed into
timestamp: the numeric timestamp (from
Date.now()) of the moment the block is created
hash: the SHA256 hash of the block’s other fields (
Verify that your blockchain includes all 8 lines of the poem, each as separate blocks, for a total of 9 blocks including the genesis block.
verifyChain(..) function that checks all blocks in the chain to ensure the chain is valid, and returns
false accordingly. It may be useful to define a
verifyBlock(..) function that can be called for each block object.
Each block should be checked for the following:
datamust be non-empty
- for the genesis block only, the hash must be
prevHashmust be non-empty
indexmust be an integer >=
hashmust match what recomputing the hash with
In addition to verifying a block, the linkage between one block and its previous block must be checked, throughout the whole chain. That is, the block at position 4 needs to have a
prevHash equal to the
hash of the block at position
3, and so on.
Print out verification that the blockchain is valid after having added all the poem text as blocks.