Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a hash in a blockchain block?
easy
A. To create a unique fingerprint of the block's data
B. To store the block's timestamp
C. To list all transactions in the block
D. To connect the block to the internet

Solution

  1. Step 1: Understand what a hash does

    A hash is a unique code generated from the block's data, like a fingerprint.
  2. Step 2: Identify the purpose of the hash in blockchain

    The hash ensures the block's data cannot be changed without detection.
  3. Final Answer:

    To create a unique fingerprint of the block's data -> Option A
  4. Quick Check:

    Hash = Unique fingerprint [OK]
Hint: Hash = unique code for block data [OK]
Common Mistakes:
  • Confusing hash with timestamp
  • Thinking hash stores transactions
  • Believing hash connects to internet
2. Which of the following is the correct way to represent a block's hash in code?
easy
A. hash = block_data + SHA256
B. hash = SHA256(block_data)
C. hash = SHA256(block_data + hash)
D. hash = block_data.hash()

Solution

  1. Step 1: Recognize the correct hash function usage

    SHA256 is a common hash function applied to block data to get the hash.
  2. Step 2: Check each option for correct syntax

    hash = SHA256(block_data) correctly applies SHA256 to block_data. Others misuse the function or syntax.
  3. Final Answer:

    hash = SHA256(block_data) -> Option B
  4. Quick Check:

    Correct hash function call = hash = SHA256(block_data) [OK]
Hint: Hash function called with data inside parentheses [OK]
Common Mistakes:
  • Adding hash function as string instead of calling it
  • Using incorrect syntax for hash function
  • Confusing method call with variable
3. Consider this simplified Python code for creating a blockchain block hash:
import hashlib
block_data = 'Block 1 data'
hash_object = hashlib.sha256(block_data.encode())
block_hash = hash_object.hexdigest()
print(block_hash)
What will be the output type of block_hash?
medium
A. integer
B. bytes
C. string
D. list

Solution

  1. Step 1: Understand hashlib.sha256 and hexdigest()

    hashlib.sha256 returns a hash object; hexdigest() converts it to a hex string.
  2. Step 2: Identify the output type of hexdigest()

    hexdigest() returns a string representing the hash in hexadecimal form.
  3. Final Answer:

    string -> Option C
  4. Quick Check:

    hexdigest() output = string [OK]
Hint: hexdigest() returns string, digest() returns bytes [OK]
Common Mistakes:
  • Thinking hexdigest() returns bytes
  • Confusing output with integer or list
  • Ignoring .encode() usage
4. The following code tries to link blocks in a chain by storing the previous block's hash. What is the error?
class Block:
    def __init__(self, data, prev_hash):
        self.data = data
        self.prev_hash = prev_hash

block1 = Block('First block', None)
block2 = Block('Second block', block1.hash)
medium
A. block1.hash is not defined before use
B. prev_hash should be a list, not a single value
C. None cannot be used as prev_hash
D. block2 data should be a number, not string

Solution

  1. Step 1: Check block1 attributes

    block1 has data and prev_hash but no attribute named 'hash'.
  2. Step 2: Identify usage of undefined attribute

    block2 tries to use block1.hash which does not exist, causing an error.
  3. Final Answer:

    block1.hash is not defined before use -> Option A
  4. Quick Check:

    Accessing undefined attribute = block1.hash is not defined before use [OK]
Hint: Check if attribute exists before using it [OK]
Common Mistakes:
  • Assuming hash attribute exists by default
  • Confusing prev_hash type requirements
  • Thinking data type causes error
5. You want to create a simple blockchain where each block stores its own hash and the previous block's hash. Which code snippet correctly computes and links the hashes for two blocks?
hard
A. import hashlib class Block: def __init__(self, data, prev_hash): self.data = data self.prev_hash = prev_hash self.hash = hashlib.sha256(data.encode()).hexdigest() block1 = Block('Block 1', '0') block2 = Block('Block 2', block1.hash)
B. import hashlib class Block: def __init__(self, data): self.data = data self.hash = hashlib.sha256(data.encode()).hexdigest() block1 = Block('Block 1') block2 = Block('Block 2') block2.prev_hash = block1.hash
C. class Block: def __init__(self, data, prev_hash): self.data = data self.prev_hash = prev_hash self.hash = data + prev_hash block1 = Block('Block 1', '0') block2 = Block('Block 2', block1.hash)
D. import hashlib class Block: def __init__(self, data, prev_hash): self.data = data self.prev_hash = prev_hash self.hash = hashlib.sha256((data + prev_hash).encode()).hexdigest() block1 = Block('Block 1', '0') block2 = Block('Block 2', block1.hash)

Solution

  1. Step 1: Check hash calculation includes previous hash

    The correct implementation computes the hash from both data and prev_hash: self.hash = hashlib.sha256((data + prev_hash).encode()).hexdigest(), ensuring secure linking.
  2. Step 2: Verify other options

    B: prev_hash set after hash computation (hash ignores prev_hash); C: no hashing, just concatenation; D: hash ignores prev_hash.
  3. Final Answer:

    self.hash = hashlib.sha256((data + prev_hash).encode()).hexdigest() -> Option D
  4. Quick Check:

    Hash(data + prev_hash) [OK]
Hint: Hash must include previous hash to link blocks [OK]
Common Mistakes:
  • Not including prev_hash in hash calculation
  • Assigning prev_hash after block creation
  • Using plain concatenation without hashing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard