How Blockchain Works: Basics and Solidity Example
A
blockchain is a linked list of blocks where each block contains data and a hash of the previous block, ensuring security and immutability. In Solidity, smart contracts can interact with blockchain data, but the blockchain itself works by chaining blocks with cryptographic hashes to prevent tampering.Syntax
A blockchain consists of blocks linked by hashes. Each block contains:
data: Information stored in the block.previousHash: The hash of the previous block, linking blocks together.hash: The current block's hash, calculated from its data and previousHash.
This structure ensures that changing one block changes all following hashes, making tampering easy to detect.
solidity
struct Block {
uint256 index;
uint256 timestamp;
string data;
bytes32 previousHash;
bytes32 hash;
}
function calculateHash(uint256 index, uint256 timestamp, string memory data, bytes32 previousHash) public pure returns (bytes32) {
return keccak256(abi.encodePacked(index, timestamp, data, previousHash));
}Example
This example shows a simple blockchain in Solidity where blocks are added with data and linked by hashes. It demonstrates how each block stores the previous block's hash to maintain chain integrity.
solidity
pragma solidity ^0.8.19; contract SimpleBlockchain { struct Block { uint256 index; uint256 timestamp; string data; bytes32 previousHash; bytes32 hash; } Block[] public blockchain; constructor() { // Create genesis block _addBlock("Genesis Block", bytes32(0)); } function _addBlock(string memory data, bytes32 previousHash) internal { uint256 index = blockchain.length; uint256 timestamp = block.timestamp; bytes32 hash = keccak256(abi.encodePacked(index, timestamp, data, previousHash)); blockchain.push(Block(index, timestamp, data, previousHash, hash)); } function addBlock(string memory data) public { Block memory lastBlock = blockchain[blockchain.length - 1]; _addBlock(data, lastBlock.hash); } function getBlock(uint256 index) public view returns (Block memory) { require(index < blockchain.length, "Block does not exist"); return blockchain[index]; } }
Common Pitfalls
1. Not linking blocks correctly: Forgetting to use the previous block's hash breaks the chain's security.
2. Mutable data: Changing block data after creation invalidates the chain, so data must be immutable.
3. Using weak hash functions: Always use strong cryptographic hashes like keccak256 in Solidity.
solidity
/* Wrong: Not linking previous hash */ function _addBlockWrong(string memory data) internal { uint256 index = blockchain.length; uint256 timestamp = block.timestamp; bytes32 hash = keccak256(abi.encodePacked(index, timestamp, data)); // Missing previousHash blockchain.push(Block(index, timestamp, data, bytes32(0), hash)); } /* Right: Include previous hash */ function _addBlockRight(string memory data, bytes32 previousHash) internal { uint256 index = blockchain.length; uint256 timestamp = block.timestamp; bytes32 hash = keccak256(abi.encodePacked(index, timestamp, data, previousHash)); blockchain.push(Block(index, timestamp, data, previousHash, hash)); }
Quick Reference
- Block: Stores data, timestamp, previous hash, and current hash.
- Hash: Calculated with
keccak256over block contents. - Chain: Blocks linked by previous hashes to ensure immutability.
- Smart Contracts: Can read and write blockchain data but do not create the blockchain itself.
Key Takeaways
Blockchain links blocks using cryptographic hashes to secure data.
Each block stores the previous block's hash to maintain chain integrity.
In Solidity, use keccak256 to calculate block hashes.
Smart contracts interact with blockchain data but do not build the chain.
Always ensure block data is immutable to prevent tampering.