📖 Scenario: You are building a simple blockchain to understand how blocks link together using hashes. Each block will store some data and the hash of the previous block, creating a secure chain.
🎯 Goal: Create a small blockchain with three blocks. Each block should contain data, the hash of the previous block, and its own hash. Finally, print the hashes of all blocks to see the chain.
📋 What You'll Learn
Create a dictionary called block1 with keys data and prev_hash and exact values 'First block' and '0'
Create a variable called hash1 that stores the SHA-256 hash of the string formed by concatenating block1['data'] and block1['prev_hash']
Create a dictionary called block2 with keys data and prev_hash and exact values 'Second block' and hash1
Create a variable called hash2 that stores the SHA-256 hash of the string formed by concatenating block2['data'] and block2['prev_hash']
Create a dictionary called block3 with keys data and prev_hash and exact values 'Third block' and hash2
Create a variable called hash3 that stores the SHA-256 hash of the string formed by concatenating block3['data'] and block3['prev_hash']
Print the three hashes hash1, hash2, and hash3 each on its own line
💡 Why This Matters
🌍 Real World
Blockchains are used to securely store data in a chain of blocks, making it hard to change past information without breaking the chain.
💼 Career
Understanding blocks, chains, and hashing is essential for jobs in blockchain development, cybersecurity, and data integrity.
Progress0 / 4 steps
1
Create the first block
Create a dictionary called block1 with keys data and prev_hash. Set data to 'First block' and prev_hash to '0'.
Blockchain / Solidity
Hint
Use curly braces {} to create a dictionary with the exact keys and values.
2
Hash the first block
Import the hashlib module. Create a variable called hash1 that stores the SHA-256 hash of the string formed by concatenating block1['data'] and block1['prev_hash']. Use .encode() before hashing and .hexdigest() to get the hash string.
Blockchain / Solidity
Hint
Use hashlib.sha256(...).hexdigest() to get the hash string.
3
Create and hash the second block
Create a dictionary called block2 with keys data and prev_hash. Set data to 'Second block' and prev_hash to hash1. Then create a variable called hash2 that stores the SHA-256 hash of the string formed by concatenating block2['data'] and block2['prev_hash'], encoded and converted to hex digest.
Blockchain / Solidity
Hint
Use the same hashing method as before but with block2 and hash1.
4
Create and hash the third block, then print all hashes
Create a dictionary called block3 with keys data and prev_hash. Set data to 'Third block' and prev_hash to hash2. Then create a variable called hash3 that stores the SHA-256 hash of the string formed by concatenating block3['data'] and block3['prev_hash'], encoded and converted to hex digest. Finally, print hash1, hash2, and hash3 each on its own line.
Blockchain / Solidity
Hint
Remember to print each hash on its own line using three separate print() statements.
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
Step 1: Understand what a hash does
A hash is a unique code generated from the block's data, like a fingerprint.
Step 2: Identify the purpose of the hash in blockchain
The hash ensures the block's data cannot be changed without detection.
Final Answer:
To create a unique fingerprint of the block's data -> Option A
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
Step 1: Recognize the correct hash function usage
SHA256 is a common hash function applied to block data to get the hash.
Step 2: Check each option for correct syntax
hash = SHA256(block_data) correctly applies SHA256 to block_data. Others misuse the function or syntax.
Final Answer:
hash = SHA256(block_data) -> Option B
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:
block1 has data and prev_hash but no attribute named 'hash'.
Step 2: Identify usage of undefined attribute
block2 tries to use block1.hash which does not exist, causing an error.
Final Answer:
block1.hash is not defined before use -> Option A
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
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.
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.
Final Answer:
self.hash = hashlib.sha256((data + prev_hash).encode()).hexdigest() -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.