Skip to content

hufaei/Go-simChain

Repository files navigation

simchain-go

Go Version License Platform

一个极简的 PoW 区块链模拟器

快速开始架构设计TCP 模式测试验证


📖 简介

simchain-go 是一个本地极简 PoW 区块链模拟器,用于学习和测试区块链核心概念:

  • ⛏️ PoW 挖矿 - 可配置难度的工作量证明
  • 🔗 链同步 - Headers-first 同步协议
  • 🌐 P2P 网络 - 支持 TCP 多进程或 inproc 单进程模式
  • 💾 持久化 - 主链数据落盘与重启恢复
  • 🔒 身份绑定 - Ed25519 握手验证

⚠️ 声明: 本项目部分代码基于 AI 辅助生成,仅供学习测试使用,不适用于生产环境。


🚀 快速开始

构建

# 构建节点
go build -o simchain-node ./cmd/node

# 构建客户端
go build -o simchain-cli ./cmd/cli

Inproc 模式(单进程多节点)

# 启动 2 节点仿真,运行 30 秒
./simchain-node --nodes=2 --difficulty=16 --duration=30s

TCP 模式(多进程)

# 终端 1: Seed 节点
./simchain-node --transport=tcp --listen=127.0.0.1:50001 --duration=0

# 终端 2: 普通节点
./simchain-node --transport=tcp --listen=127.0.0.1:50002 --seeds=127.0.0.1:50001 --duration=0

# 终端 3: 普通节点
./simchain-node --transport=tcp --listen=127.0.0.1:50003 --seeds=127.0.0.1:50001 --duration=0

🏗️ 架构设计

┌─────────────────────────────────────────────────────┐
│                   Node (编排者)                      │
│  ┌────────┐ ┌─────────┐ ┌───────┐ ┌───────────────┐ │
│  │ Miner  │ │ Mempool │ │ Chain │ │    Syncer     │ │
│  └────────┘ └─────────┘ └───────┘ └───────────────┘ │
│                        │                            │
│  ┌─────────────────────┴──────────────────────────┐ │
│  │              Transport (inproc/TCP)            │ │
│  └────────────────────────────────────────────────┘ │
│                        │                            │
│  ┌─────────────────────┴──────────────────────────┐ │
│  │                    Store                        │ │
│  └────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────┘

详细设计文档:


🌐 TCP 多进程模式

参数说明

参数 说明 默认值
--transport 传输方式 inproc|tcp inproc
--listen TCP 监听地址 -
--seeds Seed 节点列表(逗号分隔) -
--data-dir 数据目录 data/tcp-<port>
--difficulty PoW 难度(前导零 bits) 16
--duration 运行时长(0 = 永久) 30s
--tx-interval 交易注入间隔 1s
--max-tx-per-block 每块最大交易数 50
--miner-sleep 挖矿间隔 10ms

演示脚本

Windows (PowerShell)
.\scripts\run-tcp-demo.ps1 -BasePort 50001 -Nodes 3 -DurationSec 20 -Difficulty 16
Linux/macOS (Bash)
BASE_PORT=50001 NODES=3 DURATION_SEC=20 ./scripts/run-tcp-demo.sh

CLI 客户端

# 提交交易
./simchain-cli --connect=127.0.0.1:50001 --cmd=submit --data="hello world"

🧪 测试

单元测试

go test ./...

集成测试

go test ./internal/integration -run TestTCPE2E -v

验证脚本

Mempool 容量测试 (testPool.ps1)
# 前置: 启动节点
./simchain-node --transport=tcp --listen=127.0.0.1:50001 --duration=0

# 运行测试 (另一终端)
./testPool.ps1

预期: Mempool 增长至 5000 后稳定,拒绝后续交易。

TCP 连接限制测试 (verify_v3c.py)
# 连接数限制
python verify_v3c.py tcp-limit --port 50001 --count 10

# Slowloris 防护
python verify_v3c.py slowloris --port 50001

📁 项目结构

simchain-go/
├── cmd/
│   ├── node/          # 节点主程序
│   └── cli/           # 命令行客户端
├── internal/
│   ├── blockchain/    # 链索引与 reorg
│   ├── mempool/       # 交易池
│   ├── syncer/        # 同步状态机
│   ├── transport/     # 网络传输 (inproc/TCP)
│   ├── store/         # 持久化
│   └── ...
├── scripts/           # 演示脚本
├── data/              # 运行时数据
└── *.md               # 设计文档

完整结构见 FILE_TREE.md


📜 协议

TCP 消息编码与 framing 约定见 PROTOCOL.md


📄 License

MIT

About

基于GO学习开发极简区块链本地使用

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages