Reading — step 1 of 5
Read
Chain Validation
A blockchain is only as trustworthy as the code that checks it. Nodes don't believe a chain — they re-derive it. The rule of validation is: never trust a stored field you can recompute. A block arrives claiming "my hash is X" — you hash it yourself and compare. It claims "I follow block N−1" — you check that its prev_hash equals the hash you computed for N−1, not the one N−1 claims for itself.
Walk genesis-to-tip and report the first failure. If someone edits block N's data, the recomputed hash stops matching at N. If they also update N's stored hash, the link check fails at N+1 instead. To fully cover their tracks they must rewrite every block from N to the tip — which is exactly the cost Proof of Work later makes prohibitive.
Genesis is special
Block 0 has no parent, so its prev_hash is fixed by convention to 64 zeros, and real chains hardcode the entire genesis block into the client binary — it's the trust anchor everything else hangs from. Bitcoin's genesis famously embeds the newspaper headline "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks" — both a timestamp proof (the block can't predate the headline) and a mission statement.
The subtle case: internally consistent, contextually invalid
One test hands you a genesis block whose stored hash is correct — SHA-256 of 0|111...1|genesis really is the stored value — but whose prev_hash is sixty-four 1s. Self-consistent, yet still INVALID block 0, because validity is a property of the block in its chain position, not of the block alone. The mirror-image test stores a hash of all zeros on a genesis block whose true hash is something else: linkage is fine, self-hash check fails. You need both checks; each test kills one of them.
Your exercise
Each input line is <index> <prev_hash> <data> <hash>. Print VALID, or INVALID block <index> for the first offender. Note the preimage here is <index>|<prev_hash>|<data> — three fields, pipe-joined, no nonce (mining comes later; don't import the previous lesson's four-field format). Two graded mistakes dominate: (1) checking links but never recomputing hashes — the all-zero-stored-hash genesis test slips through and you print VALID instead of INVALID block 0; (2) output format drift — the grader matches INVALID block 1 letter-for-letter: capital INVALID, lowercase block, single spaces. Invalid Block #1 scores zero.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…