|
| 1 | +# Blockchain |
| 2 | + |
| 3 | +## What is Blockchain? |
| 4 | + |
| 5 | +A blockchain is a distributed ledger technology that maintains a chain of blocks, where each block contains cryptographically secured data. Each block is linked to the previous block through a hash reference, creating an immutable chain. Any attempt to modify a past block would change its hash, breaking the chain and making the tampering detectable. |
| 6 | + |
| 7 | +Key characteristics: |
| 8 | + |
| 9 | +- **Immutability**: Once data is recorded, it cannot be altered without detection. |
| 10 | +- **Transparency**: All transactions are visible to participants. |
| 11 | +- **Decentralization**: No single point of control. |
| 12 | +- **Security**: Uses cryptographic hashing to secure data. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +## Why Use Blockchain? |
| 17 | + |
| 18 | +1. **Data Integrity**: Ensures that records cannot be secretly modified. |
| 19 | +2. **Auditability**: Complete history of all transactions is preserved. |
| 20 | +3. **Trust**: Eliminates the need for a trusted intermediary. |
| 21 | +4. **Security**: Cryptographic hashing makes the data tamper-evident. |
| 22 | +5. **Transparency**: All participants have access to the same ledger. |
| 23 | + |
| 24 | +## How to Use It |
| 25 | + |
| 26 | +### Basic Setup |
| 27 | + |
| 28 | +Import the required classes: |
| 29 | + |
| 30 | +```javascript |
| 31 | +import Blockchain from './Blockchain'; |
| 32 | +import BlockchainTransaction from './BlockchainTransaction'; |
| 33 | +``` |
| 34 | + |
| 35 | +### Creating a Blockchain |
| 36 | + |
| 37 | +Create a new blockchain instance with a specified difficulty level, e.g. 3: |
| 38 | + |
| 39 | +```javascript |
| 40 | +const blockchain = new Blockchain(3); |
| 41 | +``` |
| 42 | + |
| 43 | +**Constructor Parameter**: |
| 44 | + |
| 45 | +- `difficulty` (number, default: 2): The number of leading zeros required in a block's hash during mining. Higher number means higher difficulty. |
| 46 | + |
| 47 | +### Adding Transactions |
| 48 | + |
| 49 | +Create transactions and add them to the pending transactions pool: |
| 50 | + |
| 51 | +```javascript |
| 52 | +const transaction1 = new BlockchainTransaction({ |
| 53 | + from: 'Alice', |
| 54 | + to: 'Bob', |
| 55 | + amount: 50, |
| 56 | + description: 'Payment for services', |
| 57 | +}); |
| 58 | +blockchain.addTransaction(transaction1); |
| 59 | + |
| 60 | +const transaction2 = new BlockchainTransaction({ |
| 61 | + from: 'Bob', |
| 62 | + to: 'Charlie', |
| 63 | + amount: 25, |
| 64 | +}); |
| 65 | +blockchain.addTransaction(transaction2); |
| 66 | +``` |
| 67 | + |
| 68 | +**Transaction Class Properties**: |
| 69 | + |
| 70 | +- `from`: The sender's address |
| 71 | +- `to`: The recipient's address |
| 72 | +- `amount`: The transaction amount (must be positive) |
| 73 | +- `description`: Optional description of the transaction |
| 74 | + |
| 75 | +### Mining Blocks |
| 76 | + |
| 77 | +Mine pending transactions into a new block: |
| 78 | + |
| 79 | +```javascript |
| 80 | +const minedBlock = blockchain.minePendingTransactions('Miner1'); |
| 81 | +``` |
| 82 | + |
| 83 | +### Block Structure |
| 84 | + |
| 85 | +Each block contains: |
| 86 | + |
| 87 | +```javascript |
| 88 | +index; // Position in blockchain |
| 89 | +timestamp; // Creation time in ISO format |
| 90 | +data; // Array of transactions in this block |
| 91 | +previousHash; // Hash of the previous block |
| 92 | +hash; // Current block's hash |
| 93 | +nonce; // Number used in proof-of-work calculation |
| 94 | +``` |
| 95 | + |
| 96 | +### Validating the Blockchain |
| 97 | + |
| 98 | +Verify the integrity of the entire blockchain: |
| 99 | + |
| 100 | +```javascript |
| 101 | +if (blockchain.isChainValid()) { |
| 102 | + // Blockchain is valid |
| 103 | +} else { |
| 104 | + // Blockchain has been tampered with! |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Checking Balances |
| 109 | + |
| 110 | +Get the balance of an address by analyzing all transactions: |
| 111 | + |
| 112 | +```javascript |
| 113 | +const aliceBalance = blockchain.getBalance('Alice'); |
| 114 | +console.log(aliceBalance); // 10 |
| 115 | +``` |
| 116 | + |
| 117 | +### Retrieving Transactions |
| 118 | + |
| 119 | +Find all transactions involving a specific address: |
| 120 | + |
| 121 | +```javascript |
| 122 | +const aliceTransactions = blockchain.getTransactionsForAddress('Alice'); |
| 123 | +console.log(aliceTransactions) |
| 124 | +// [ |
| 125 | +// { block: 1, transaction: {...} }, |
| 126 | +// { block: 2, transaction: {...} } |
| 127 | +// ] |
| 128 | +``` |
| 129 | + |
| 130 | +### Getting Latest Block |
| 131 | + |
| 132 | +Access the most recent block in the chain: |
| 133 | + |
| 134 | +```javascript |
| 135 | +const latestBlock = blockchain.getLatestBlock(); |
| 136 | +``` |
| 137 | + |
| 138 | +## Complete Example |
| 139 | + |
| 140 | +```javascript |
| 141 | +import Blockchain from './Blockchain.js'; |
| 142 | +import BlockchainTransaction from './BlockchainTransaction.js'; |
| 143 | + |
| 144 | +const blockchain = new Blockchain(2); |
| 145 | + |
| 146 | +blockchain.addTransaction(new BlockchainTransaction({ |
| 147 | + from: 'Alice', |
| 148 | + to: 'Bob', |
| 149 | + amount: 30, |
| 150 | +})); |
| 151 | +blockchain.addTransaction(new BlockchainTransaction({ |
| 152 | + from: 'Bob', |
| 153 | + to: 'Charlie', |
| 154 | + amount: 15, |
| 155 | +})); |
| 156 | + |
| 157 | +blockchain.minePendingTransactions('Miner1'); |
| 158 | + |
| 159 | +blockchain.addTransaction(new BlockchainTransaction({ |
| 160 | + from: 'Charlie', |
| 161 | + to: 'Alice', |
| 162 | + amount: 10, |
| 163 | +})); |
| 164 | + |
| 165 | +blockchain.minePendingTransactions('Miner2'); |
| 166 | +``` |
| 167 | + |
| 168 | +## Implementation Details |
| 169 | + |
| 170 | +### Mining and Difficulty |
| 171 | + |
| 172 | +The `mineBlock(difficulty)` method in BlockchainBlock implements proof-of-work: |
| 173 | + |
| 174 | +- It increments the `nonce` value until the resulting `hash` has the required number of leading zeros. |
| 175 | +- Higher difficulty values require exponentially more computational work. |
| 176 | +- This mechanism secures the blockchain by making it computationally expensive to tamper with blocks. |
| 177 | + |
| 178 | +### Chain Validation |
| 179 | + |
| 180 | +The `isChainValid()` method ensures: |
| 181 | + |
| 182 | +- Each block's hash matches its calculated hash (prevents tampering). |
| 183 | +- Each block's `previousHash` matches the previous block's `hash` (maintains chain integrity). |
| 184 | +- The entire chain is unbroken. |
| 185 | + |
| 186 | +### Pending Transactions |
| 187 | + |
| 188 | +Transactions are held in `pendingTransactions` until they are included in a mined block. This allows: |
| 189 | + |
| 190 | +- Multiple transactions to be batched together. |
| 191 | +- Efficient block creation. |
| 192 | +- Flexibility in mining timing. |
| 193 | + |
| 194 | +## Security Considerations |
| 195 | + |
| 196 | +- **Difficulty**: Increase `difficulty` for higher security (but slower mining). |
| 197 | +- **Validation**: Always call `isChainValid()` before trusting the blockchain. |
| 198 | +- **Address Verification**: This implementation doesn't include cryptographic signatures. In production, use digital signatures to verify transaction authenticity. |
| 199 | +- **Double Spending**: Implement balance checks before accepting transactions in production systems. |
| 200 | + |
| 201 | +## References |
| 202 | + |
| 203 | +This documentation draws from fundamental blockchain concepts and implementation details. Key sources include: |
| 204 | + |
| 205 | +- **Bitcoin Whitepaper**: Nakamoto, S. (2008). "Bitcoin: A Peer-to-Peer Electronic Cash System." Available at: https://bitcoin.org/bitcoin.pdf |
| 206 | +- **Mastering Bitcoin**: Antonopoulos, A. M. (2017). "Mastering Bitcoin: Programming the Open Blockchain." O'Reilly Media. |
| 207 | +- **Blockchain Technology Overview**: Swan, M. (2015). "Blockchain: Blueprint for a New Economy." O'Reilly Media. |
| 208 | +- **Ethereum Documentation**: https://ethereum.org/en/developers/docs/ |
| 209 | +- **Wikipedia - Blockchain**: https://en.wikipedia.org/wiki/Blockchain |
0 commit comments