区块链基础 阶段一 ✅ 验证通过
万维网初期,用户是内容的消费者。静态HTML页面,单向信息传递,没有互动。
社交媒体崛起,用户成为内容的创造者,但平台成为数据的控制者。
区块链赋予用户数据主权——你创作的内容,你真正拥有。
区块链 = 去中心化的分布式账本,由一系列按时间顺序排列的数据块组成,每个块包含一批交易记录,通过密码学链接形成链条。
| 特性 | 说明 | 类比 |
|---|---|---|
| 🔒 去中心化 | 无中心服务器,节点对等 | 没有行长的银行 |
| 🧱 不可篡改 | 修改任一区块需改所有后续块 | 刻在石头上的账本 |
| 👁️ 公开透明 | 所有交易对所有人可见 | 玻璃房里的保险箱 |
| 🤝 共识驱动 | 网络通过规则达成一致 | 民主投票而非独裁 |
| 🔗 可追溯 | 每笔交易有完整历史链 | 带时间戳的快递追踪 |
// blockchain.js — 简易区块链实现 ✅验证通过
const crypto = require('crypto');
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.nonce = 0;
this.hash = this.calculateHash();
}
calculateHash() {
return crypto.createHash('sha256')
.update(this.index + this.previousHash +
this.timestamp + JSON.stringify(this.data) +
this.nonce)
.digest('hex');
}
// 简易PoW挖矿:找到前n个0的哈希
mineBlock(difficulty) {
const target = '0'.repeat(difficulty);
while (this.hash.substring(0, difficulty) !== target) {
this.nonce++;
this.hash = this.calculateHash();
}
console.log(`✅ 区块 mined: ${this.hash}`);
}
}
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
this.difficulty = 2; // 难度:哈希前2位为0
}
createGenesisBlock() {
return new Block(0, "2024-01-01", "Genesis Block", "0");
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.mineBlock(this.difficulty);
this.chain.push(newBlock);
}
// 验证链完整性
isChainValid() {
for (let i = 1; i < this.chain.length; i++) {
const curr = this.chain[i];
const prev = this.chain[i - 1];
// 验证哈希是否正确
if (curr.hash !== curr.calculateHash()) {
console.log(`❌ 区块${i}哈希不匹配`);
return false;
}
// 验证前驱链接
if (curr.previousHash !== prev.hash) {
console.log(`❌ 区块${i}前驱哈希断裂`);
return false;
}
}
return true;
}
}
// ✅ 运行测试
const myChain = new Blockchain();
console.log("⛏ 开始挖矿...");
myChain.addBlock(new Block(1, "2024-06-01", { from: "Alice", to: "Bob", amount: 50 }));
myChain.addBlock(new Block(2, "2024-06-02", { from: "Bob", to: "Carol", amount: 25 }));
console.log(`\n🔗 链有效性: ${myChain.isChainValid() ? '✅ 有效' : '❌ 无效'}`);
console.log(`\n📊 区块链总长度: ${myChain.chain.length} 个区块`);
// 尝试篡改
console.log("\n⚠️ 尝试篡改区块1的数据...");
myChain.chain[1].data = { from: "Alice", to: "Eve", amount: 9999 };
console.log(`🔗 篡改后链有效性: ${myChain.isChainValid() ? '✅ 有效' : '❌ 无效(防篡改生效)'}`);
⛏ 开始挖矿...
✅ 区块 mined: 00d3a7b2e8f1c4...
✅ 区块 mined: 009f1e5c3a7b2d...
🔗 链有效性: ✅ 有效
📊 区块链总长度: 3 个区块
⚠️ 尝试篡改区块1的数据...
❌ 区块1哈希不匹配
🔗 篡改后链有效性: ❌ 无效(防篡改生效)
| 组件 | Web2等价物 | Web3实现 |
|---|---|---|
| 身份认证 | 用户名/密码 | 私钥/助记词 |
| 数据存储 | AWS/阿里云 | IPFS/Arweave |
| 支付 | 支付宝/PayPal | ETH/USDC |
| 计算 | 云服务器 | 智能合约(EVM) |
| 域名 | DNS | ENS |
| 内容分发 | CDN | Filecoin |
| 维度 | 公链 | 联盟链 | 私链 |
|---|---|---|---|
| 准入 | 任何人 | 授权成员 | 单一组织 |
| 代表 | Bitcoin, Ethereum | Hyperledger | 企业内部链 |
| 去中心化 | ⭐⭐⭐ | ⭐⭐ | ⭐ |
| 性能 | 低(~15TPS) | 中(~1000TPS) | 高(~10000TPS) |
| 安全模型 | 经济博弈 | 信任假设 | 内部控制 |
1. Web3相比Web2最核心的区别是什么?
2. 区块链的"不可篡改性"主要依赖什么?
3. 如果修改区块链中第3个区块的数据,会发生什么?
4. PoW共识机制的核心思想是什么?
5. 以太坊从PoW转向PoS的主要原因是?
0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045(Vitalik的地址)blockchain.jsnode blockchain.js你已理解Web3的核心哲学和区块链的基础架构!
从中心化到去中心化的思维转变,是你进入Web3世界的第一步。
✅ Web演进理解 ✅ 区块链5大特性 ✅ 简易链实现 ✅ 防篡改验证