Blocks, chains, and hashing help keep digital information safe and connected. They make sure data can't be changed without everyone knowing.
Blocks, chains, and hashing in Blockchain / Solidity
Block {
index: number
timestamp: string
data: any
previousHash: string
hash: string
}
Chain = array of Blocks
hash = hashFunction(blockData)A block holds data and links to the previous block using a hash.
The chain is a list of blocks connected by hashes.
block = {
index: 1,
timestamp: '2024-06-01T12:00:00Z',
data: 'First block data',
previousHash: '0',
hash: 'abc123'
}block = {
index: 2,
timestamp: '2024-06-01T12:05:00Z',
data: 'Second block data',
previousHash: 'abc123',
hash: 'def456'
}This program creates a simple blockchain with a genesis block and two more blocks. Each block stores data and links to the previous block using a hash. The hash is created by combining the block's details and running it through a secure hash function.
import hashlib import time class Block: def __init__(self, index, timestamp, data, previous_hash): self.index = index self.timestamp = timestamp self.data = data self.previous_hash = previous_hash self.hash = self.calculate_hash() def calculate_hash(self): block_string = f'{self.index}{self.timestamp}{self.data}{self.previous_hash}' return hashlib.sha256(block_string.encode()).hexdigest() class Blockchain: def __init__(self): self.chain = [self.create_genesis_block()] def create_genesis_block(self): return Block(0, time.strftime('%Y-%m-%d %H:%M:%S'), 'Genesis Block', '0') def get_latest_block(self): return self.chain[-1] def add_block(self, data): latest_block = self.get_latest_block() new_index = latest_block.index + 1 new_timestamp = time.strftime('%Y-%m-%d %H:%M:%S') new_block = Block(new_index, new_timestamp, data, latest_block.hash) self.chain.append(new_block) # Create blockchain and add blocks my_blockchain = Blockchain() my_blockchain.add_block('First real block data') my_blockchain.add_block('Second real block data') # Print the blockchain for block in my_blockchain.chain: print(f'Block {block.index}:') print(f' Timestamp: {block.timestamp}') print(f' Data: {block.data}') print(f' Previous Hash: {block.previous_hash}') print(f' Hash: {block.hash}\n')
Hashing makes sure each block is unique and any change breaks the chain.
Each block points to the previous one, so the chain is linked like a real chain.
Changing one block means changing all after it, which is very hard to do secretly.
Blocks hold data and connect using hashes.
Chains link blocks so data stays safe and unchangeable.
Hashing creates a unique fingerprint for each block.
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
