区块链是一种去中心化的分布式账本技术,由中本聪(Satoshi Nakamoto)于 2008 年在比特币白皮书中首次提出。区块链通过 密码学、共识机制和分布式存储,实现了数据的不可篡改、透明可追溯和去中心化信任。
区块链的核心定位是 去中心化的信任基础设施。它提供了:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// 合约定义
contract SimpleStorage {
// 状态变量
uint256 private storedData;
// 事件
event DataStored(address indexed caller, uint256 value);
// 函数
function set(uint256 x) public {
storedData = x;
emit DataStored(msg.sender, x);
}
function get() public view returns (uint256) {
return storedData;
}
}
// 代币合约(ERC-20)
contract MyToken {
string public name = "My Token";
string public symbol = "MTK";
uint8 public decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
constructor(uint256 _initialSupply) {
totalSupply = _initialSupply * 10 ** uint256(decimals);
balanceOf[msg.sender] = totalSupply;
}
function transfer(address to, uint256 value) public returns (bool) {
require(balanceOf[msg.sender] >= value, "余额不足");
balanceOf[msg.sender] -= value;
balanceOf[to] += value;
emit Transfer(msg.sender, to, value);
return true;
}
function approve(address spender, uint256 value) public returns (bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
function transferFrom(address from, address to, uint256 value) public returns (bool) {
require(balanceOf[from] >= value, "余额不足");
require(allowance[from][msg.sender] >= value, "授权不足");
balanceOf[from] -= value;
balanceOf[to] += value;
allowance[from][msg.sender] -= value;
emit Transfer(from, to, value);
return true;
}
}
// 所有权控制
contract Ownable {
address public owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "不是所有者");
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0), "无效地址");
owner = newOwner;
}
}
// 暂停/紧急停止
contract Pausable is Ownable {
bool public paused;
modifier whenNotPaused() {
require(!paused, "合约已暂停");
_;
}
function pause() public onlyOwner {
paused = true;
}
function unpause() public onlyOwner {
paused = false;
}
}
// 销毁合约
contract Destroyable is Ownable {
function destroy() public onlyOwner {
selfdestruct(payable(owner));
}
}
// 使用 Hardhat 部署
// hardhat.config.js
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.19",
networks: {
sepolia: {
url: process.env.ALCHEMY_URL,
accounts: [process.env.PRIVATE_KEY]
}
}
};
// 部署脚本 deploy.js
async function main() {
const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
const contract = await SimpleStorage.deploy();
await contract.waitForDeployment();
console.log("合约地址:", await contract.getAddress());
// 调用合约
await contract.set(42);
const value = await contract.get();
console.log("存储值:", value);
}
// 使用 Web3.js 交互
const Web3 = require("web3");
const web3 = new Web3("https://mainnet.infura.io/v3/YOUR_KEY");
const contractABI = [...];
const contractAddress = "0x...";
const contract = new web3.eth.Contract(contractABI, contractAddress);
// 读取数据
const value = await contract.methods.get().call();
console.log(value);
// 写入数据
const accounts = await web3.eth.getAccounts();
await contract.methods.set(42).send({ from: accounts[0] });
// 防重入攻击示例
contract SecureWithdrawal {
mapping(address => uint256) public balances;
function withdraw() public {
uint256 amount = balances[msg.sender];
require(amount > 0, "余额不足");
// 先更新状态(Checks-Effects-Interactions 模式)
balances[msg.sender] = 0;
// 再发送 ETH
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "转账失败");
}
}
| 平台 | 共识机制 | 智能合约 | TPS | 特点 |
|---|---|---|---|---|
| Bitcoin | PoW | 脚本 | ~7 | 数字黄金 |
| Ethereum | PoS | Solidity | ~15 | 智能合约平台 |
| Solana | PoS+历史证明 | Rust | ~65k | 高性能 |
| Polygon | PoS | Solidity | ~7k | Layer 2 |
| 蚂蚁链 | PBFT | Solidity | ~10k | 联盟链 |
密码学基础、分布式系统、JavaScript/Python
区块链原理、比特币、以太坊、智能合约
Solidity 开发、DApp 开发、Web3 交互
DeFi 协议、NFT 开发、Layer 2、安全审计
区块链是信任机器和价值互联网。
它用 去中心化、不可篡改、智能合约 重新定义了信任和价值的传递方式。区块链是 Web3.0 的核心基础设施。
"区块链让信任变得去中心化和自动化。" ⛓️