Blocks, chains, and hashing help keep digital information safe and connected. They make sure data can't be changed without everyone knowing.
0
0
Blocks, chains, and hashing in Blockchain / Solidity
Introduction
When you want to store transactions securely like in digital money.
When you need to keep records that everyone can trust without a middleman.
When you want to track items in a supply chain so no one can cheat.
When you want to create a secure digital identity system.
When you want to build apps where data must be permanent and tamper-proof.
Syntax
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.
Examples
This is the first block, called the genesis block. It has no previous block, so previousHash is '0'.
Blockchain / Solidity
block = {
index: 1,
timestamp: '2024-06-01T12:00:00Z',
data: 'First block data',
previousHash: '0',
hash: 'abc123'
}This block links to the first block by using its hash in previousHash.
Blockchain / Solidity
block = {
index: 2,
timestamp: '2024-06-01T12:05:00Z',
data: 'Second block data',
previousHash: 'abc123',
hash: 'def456'
}Sample Program
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.
Blockchain / Solidity
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')
OutputSuccess
Important Notes
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.
Summary
Blocks hold data and connect using hashes.
Chains link blocks so data stays safe and unchangeable.
Hashing creates a unique fingerprint for each block.