0
0
Blockchain / Solidityprogramming~10 mins

Blocks, chains, and hashing in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Blocks, chains, and hashing
Create Block Data
Calculate Hash of Block
Add Hash to Block
Link Block to Previous Block's Hash
Add Block to Chain
Repeat for Next Block
Blocks hold data and a hash. Each block links to the previous block's hash, forming a chain.
Execution Sample
Blockchain / Solidity
block1 = {"data": "First block", "prev_hash": "0"}
block1["hash"] = hash(str(block1))

block2 = {"data": "Second block", "prev_hash": block1["hash"]}
block2["hash"] = hash(str(block2))
Creates two blocks, each with data and a hash linking to the previous block's hash.
Execution Table
StepActionBlock DataPrevious HashCalculated HashChain State
1Create block1 data{"data": "First block", "prev_hash": "0"}"0"N/A[]
2Calculate block1 hash{"data": "First block", "prev_hash": "0"}"0"hash1[]
3Add hash to block1{"data": "First block", "prev_hash": "0", "hash": "hash1"}"0"hash1[]
4Add block1 to chain{"data": "First block", "prev_hash": "0", "hash": "hash1"}"0"hash1[block1]
5Create block2 data with prev_hash{"data": "Second block", "prev_hash": "hash1"}"hash1"N/A[block1]
6Calculate block2 hash{"data": "Second block", "prev_hash": "hash1"}"hash1"hash2[block1]
7Add hash to block2{"data": "Second block", "prev_hash": "hash1", "hash": "hash2"}"hash1"hash2[block1]
8Add block2 to chain{"data": "Second block", "prev_hash": "hash1", "hash": "hash2"}"hash1"hash2[block1, block2]
💡 All blocks created and linked; chain contains two blocks.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8
block1{}{"data": "First block", "prev_hash": "0", "hash": "hash1"}{"data": "First block", "prev_hash": "0", "hash": "hash1"}{"data": "First block", "prev_hash": "0", "hash": "hash1"}{"data": "First block", "prev_hash": "0", "hash": "hash1"}
block2{}{}{}{"data": "Second block", "prev_hash": "hash1", "hash": "hash2"}{"data": "Second block", "prev_hash": "hash1", "hash": "hash2"}
chain[][][block1][block1][block1, block2]
Key Moments - 2 Insights
Why does each block include the previous block's hash?
Including the previous block's hash links blocks together, making the chain secure and tamper-evident, as shown in steps 5 and 6 where block2 uses block1's hash.
What happens if the data in a block changes after hashing?
If block data changes, its hash changes too, breaking the chain link. This is why the hash is calculated after setting data, as in steps 2 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the previous hash value used when creating block2?
A"hash1"
B"0"
C"hash2"
Dnull
💡 Hint
Check Step 5 in the execution table where block2's previous hash is set.
At which step is block1 added to the chain?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Chain State' column in the execution table to see when block1 appears.
If block1's data changed after step 4, what would happen to block2's previous hash?
AIt would remain the same
BIt would automatically update
CIt would become invalid
DIt would be null
💡 Hint
Refer to the key moment about data change affecting hashes and chain integrity.
Concept Snapshot
Blocks store data and a hash.
Each block includes the previous block's hash.
Hashes link blocks into a secure chain.
Changing data changes the hash, breaking the chain.
This creates a tamper-evident blockchain.
Full Transcript
This visual trace shows how blocks are created with data and a previous hash. Each block's hash is calculated from its data and previous hash, then added to the block. Blocks are linked by including the previous block's hash, forming a chain. The chain grows as new blocks are added. Changing block data changes its hash, breaking the chain link and ensuring security.