What if you could instantly know if someone changed your records, without checking every detail yourself?
Why Blocks, chains, and hashing in Blockchain / Solidity? - Purpose & Use Cases
Imagine you want to keep a diary of all your daily expenses on paper. Every day, you write down what you spent and how much. But if someone changes an old entry, you might never notice. Also, if you lose a page or mix up the order, your records become confusing and unreliable.
Writing records manually is slow and easy to tamper with. You have no way to check if someone changed past entries. It's hard to trust the data because there's no automatic way to link one record to the next or to verify the whole history.
Blocks, chains, and hashing work together like a secure digital diary. Each block holds a group of records and a special code called a hash. This hash links it to the previous block, creating a chain. If anyone tries to change a block, the hash changes too, breaking the chain and revealing tampering instantly.
record1 = 'Bought coffee $3' record2 = 'Bought book $10' # No link or verification between records
block1 = {"data": "Bought coffee $3", "hash": "abc123", "prev_hash": "000000"}
block2 = {"data": "Bought book $10", "hash": "def456", "prev_hash": "abc123"}This concept makes it possible to create trustworthy, tamper-proof records that everyone can verify without needing a middleman.
Cryptocurrencies like Bitcoin use blocks, chains, and hashing to securely record every transaction so no one can cheat or spend the same money twice.
Manual record-keeping is slow and easy to alter.
Blocks group data and hashes link them securely in a chain.
This creates a trusted, tamper-evident history of information.
Practice
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 AQuick Check:
Hash = Unique fingerprint [OK]
- Confusing hash with timestamp
- Thinking hash stores transactions
- Believing hash connects to internet
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 BQuick Check:
Correct hash function call = hash = SHA256(block_data) [OK]
- Adding hash function as string instead of calling it
- Using incorrect syntax for hash function
- Confusing method call with variable
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?Solution
Step 1: Understand hashlib.sha256 and hexdigest()
hashlib.sha256 returns a hash object; hexdigest() converts it to a hex string.Step 2: Identify the output type of hexdigest()
hexdigest() returns a string representing the hash in hexadecimal form.Final Answer:
string -> Option CQuick Check:
hexdigest() output = string [OK]
- Thinking hexdigest() returns bytes
- Confusing output with integer or list
- Ignoring .encode() usage
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)
Solution
Step 1: Check block1 attributes
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 AQuick Check:
Accessing undefined attribute = block1.hash is not defined before use [OK]
- Assuming hash attribute exists by default
- Confusing prev_hash type requirements
- Thinking data type causes error
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 DQuick Check:
Hash(data + prev_hash) [OK]
- Not including prev_hash in hash calculation
- Assigning prev_hash after block creation
- Using plain concatenation without hashing
Ready to prove it?
Pass the test and this pattern moves unknown
