系统支持将监听到的区块和交易数据保存到本地,并提供类似全节点的查询接口。这是一个轻量级的"模拟全节点"功能:
- ✅ 只监听和存储:不参与共识,不验证区块,只是保存数据
- ✅ 文件系统存储:使用 JSON 文件存储,简单可靠
- ✅ 查询接口:提供 JSON-RPC 和 RESTful API 接口
- ✅ 多链支持:支持所有已实现的区块链
在 config/config.yaml 中添加存储配置:
storage:
enabled: true # 启用区块存储
data_dir: "./data/blocks" # 数据存储目录(相对路径或绝对路径)
server:
port: 8080 # RPC 服务端口(可选,用于查询接口)启用后会创建以下目录结构:
data/blocks/
├── ethereum/
│ ├── blocks/ # 区块数据
│ │ ├── 12345678.json
│ │ ├── 12345679.json
│ │ └── ...
│ ├── transactions/ # 交易数据(按区块号组织)
│ │ ├── 12345678/
│ │ │ ├── 0xabc123...json
│ │ │ └── ...
│ │ └── ...
│ └── index/ # 索引文件
│ ├── latest.json # 最新区块索引
│ └── addresses/ # 地址索引
├── bnb/
│ └── ...
├── solana/
│ └── ...
└── ...
端点: http://localhost:8080/rpc
# 获取最新区块
curl -X POST http://localhost:8080/rpc \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "getLatestBlock",
"params": ["ethereum"],
"id": 1
}'
# 根据区块号获取区块
curl -X POST http://localhost:8080/rpc \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "getBlockByNumber",
"params": ["ethereum", 12345678],
"id": 2
}'
# 根据区块哈希获取区块
curl -X POST http://localhost:8080/rpc \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "getBlockByHash",
"params": ["ethereum", "0x1234..."],
"id": 3
}'
# 获取交易
curl -X POST http://localhost:8080/rpc \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "getTransaction",
"params": ["ethereum", "0xabc123..."],
"id": 4
}'
# 获取区块中的所有交易
curl -X POST http://localhost:8080/rpc \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "getBlockTransactions",
"params": ["ethereum", 12345678],
"id": 5
}'GET /api/v1/blocks/{chain}/{blockNumber}
示例:
curl http://localhost:8080/api/v1/blocks/ethereum/12345678GET /api/v1/transactions/{chain}/{txHash}
示例:
curl http://localhost:8080/api/v1/transactions/ethereum/0xabc123...GET /api/v1/chains/{chain}
示例:
curl http://localhost:8080/api/v1/chains/ethereum响应示例:
{
"chain": "ethereum",
"latest_block": 12345678,
"latest_hash": "0x1234...",
"timestamp": "2024-01-01T12:00:00Z",
"tx_count": 150
}所有已实现的链都支持存储:
ethereum- 以太坊bnb- BNB Chainsolana- Solanasui- Suitron- Tronbtc- Bitcoin
- 监听区块:监听器检测到新区块时,自动保存区块信息
- 保存交易:每笔监听到的交易都会保存到对应区块目录
- 定期同步:每 30 秒同步一次最新区块信息
- 索引维护:自动维护区块哈希索引和最新区块索引
- 存储空间:区块数据会持续增长,建议定期清理旧数据或限制存储范围
- 性能:文件系统存储适合小规模使用,大规模建议使用数据库
- 数据完整性:只保存监听到的数据,不会补全历史数据
- 启动同步:首次启动只从当前开始保存,不会同步历史区块
如需清理旧数据,可以直接删除 data/blocks/{chain}/blocks/ 目录下的旧区块文件。
注意:清理后索引会自动重建,但已删除的区块无法恢复(除非重新监听)。