主要更新: 1. 数据库持久化功能 - 支持三种策略:仅落库、既落库又存证、仅存证 - 实现 Cursor Worker 异步扫描和存证机制 - 实现 Retry Worker 失败重试机制 - 支持 PostgreSQL、MySQL、SQLite 等多种数据库 - 添加 ClientIP 和 ServerIP 字段(可空,仅落库) 2. 集群并发安全 - 使用 SELECT FOR UPDATE SKIP LOCKED 防止重复处理 - 实现 CAS (Compare-And-Set) 原子状态更新 - 添加 updated_at 字段支持并发控制 3. Cursor 初始化优化 - 自动基于历史数据初始化 cursor - 确保不遗漏任何历史记录 - 修复 UPSERT 逻辑 4. 测试完善 - 添加 E2E 集成测试(含 Pulsar 消费者验证) - 添加 PostgreSQL 集成测试 - 添加 Pulsar 集成测试 - 添加集群并发安全测试 - 添加 Cursor 初始化验证测试 - 补充大量单元测试,提升覆盖率 5. 工具脚本 - 添加数据库迁移脚本 - 添加 Cursor 状态检查工具 - 添加 Cursor 初始化工具 - 添加 Pulsar 消息验证工具 6. 文档清理 - 删除冗余文档,只保留根目录 README 测试结果: - 所有 E2E 测试通过(100%) - 数据库持久化与异步存证流程验证通过 - 集群环境下的并发安全性验证通过 - Cursor 自动初始化和历史数据处理验证通过
147 lines
3.8 KiB
Go
147 lines
3.8 KiB
Go
package model
|
|
|
|
import (
|
|
"go.yandata.net/iod/iod/go-trustlog/api/grpc/pb"
|
|
)
|
|
|
|
// MerkleTreeProofItem 表示Merkle树证明项.
|
|
type MerkleTreeProofItem struct {
|
|
Floor uint32 // 层级
|
|
Hash string // 哈希值
|
|
Left bool // 是否为左节点
|
|
}
|
|
|
|
// Proof 表示取证证明.
|
|
type Proof struct {
|
|
ColItems []*MerkleTreeProofItem // 集合项证明
|
|
RawItems []*MerkleTreeProofItem // 原始项证明
|
|
ColRootItem []*MerkleTreeProofItem // 集合根项证明
|
|
RawRootItem []*MerkleTreeProofItem // 原始根项证明
|
|
Sign string // 签名
|
|
Version string // 版本号
|
|
}
|
|
|
|
// ProofFromProtobuf 将protobuf的Proof转换为model.Proof.
|
|
func ProofFromProtobuf(pbProof *pb.Proof) *Proof {
|
|
if pbProof == nil {
|
|
return nil
|
|
}
|
|
|
|
proof := &Proof{
|
|
Sign: pbProof.GetSign(),
|
|
Version: pbProof.GetVersion(),
|
|
}
|
|
|
|
// 转换 ColItems
|
|
if pbColItems := pbProof.GetColItems(); len(pbColItems) > 0 {
|
|
proof.ColItems = make([]*MerkleTreeProofItem, 0, len(pbColItems))
|
|
for _, item := range pbColItems {
|
|
proof.ColItems = append(proof.ColItems, &MerkleTreeProofItem{
|
|
Floor: item.GetFloor(),
|
|
Hash: item.GetHash(),
|
|
Left: item.GetLeft(),
|
|
})
|
|
}
|
|
}
|
|
|
|
// 转换 RawItems
|
|
if pbRawItems := pbProof.GetRawItems(); len(pbRawItems) > 0 {
|
|
proof.RawItems = make([]*MerkleTreeProofItem, 0, len(pbRawItems))
|
|
for _, item := range pbRawItems {
|
|
proof.RawItems = append(proof.RawItems, &MerkleTreeProofItem{
|
|
Floor: item.GetFloor(),
|
|
Hash: item.GetHash(),
|
|
Left: item.GetLeft(),
|
|
})
|
|
}
|
|
}
|
|
|
|
// 转换 ColRootItem
|
|
if pbColRootItem := pbProof.GetColRootItem(); len(pbColRootItem) > 0 {
|
|
proof.ColRootItem = make([]*MerkleTreeProofItem, 0, len(pbColRootItem))
|
|
for _, item := range pbColRootItem {
|
|
proof.ColRootItem = append(proof.ColRootItem, &MerkleTreeProofItem{
|
|
Floor: item.GetFloor(),
|
|
Hash: item.GetHash(),
|
|
Left: item.GetLeft(),
|
|
})
|
|
}
|
|
}
|
|
|
|
// 转换 RawRootItem
|
|
if pbRawRootItem := pbProof.GetRawRootItem(); len(pbRawRootItem) > 0 {
|
|
proof.RawRootItem = make([]*MerkleTreeProofItem, 0, len(pbRawRootItem))
|
|
for _, item := range pbRawRootItem {
|
|
proof.RawRootItem = append(proof.RawRootItem, &MerkleTreeProofItem{
|
|
Floor: item.GetFloor(),
|
|
Hash: item.GetHash(),
|
|
Left: item.GetLeft(),
|
|
})
|
|
}
|
|
}
|
|
|
|
return proof
|
|
}
|
|
|
|
// ProofToProtobuf 将model.Proof转换为protobuf的Proof.
|
|
func ProofToProtobuf(proof *Proof) *pb.Proof {
|
|
if proof == nil {
|
|
return nil
|
|
}
|
|
|
|
pbProof := &pb.Proof{
|
|
Sign: proof.Sign,
|
|
Version: proof.Version,
|
|
}
|
|
|
|
// 转换 ColItems
|
|
if len(proof.ColItems) > 0 {
|
|
pbProof.ColItems = make([]*pb.MerkleTreeProofItem, 0, len(proof.ColItems))
|
|
for _, item := range proof.ColItems {
|
|
pbProof.ColItems = append(pbProof.ColItems, &pb.MerkleTreeProofItem{
|
|
Floor: item.Floor,
|
|
Hash: item.Hash,
|
|
Left: item.Left,
|
|
})
|
|
}
|
|
}
|
|
|
|
// 转换 RawItems
|
|
if len(proof.RawItems) > 0 {
|
|
pbProof.RawItems = make([]*pb.MerkleTreeProofItem, 0, len(proof.RawItems))
|
|
for _, item := range proof.RawItems {
|
|
pbProof.RawItems = append(pbProof.RawItems, &pb.MerkleTreeProofItem{
|
|
Floor: item.Floor,
|
|
Hash: item.Hash,
|
|
Left: item.Left,
|
|
})
|
|
}
|
|
}
|
|
|
|
// 转换 ColRootItem
|
|
if len(proof.ColRootItem) > 0 {
|
|
pbProof.ColRootItem = make([]*pb.MerkleTreeProofItem, 0, len(proof.ColRootItem))
|
|
for _, item := range proof.ColRootItem {
|
|
pbProof.ColRootItem = append(pbProof.ColRootItem, &pb.MerkleTreeProofItem{
|
|
Floor: item.Floor,
|
|
Hash: item.Hash,
|
|
Left: item.Left,
|
|
})
|
|
}
|
|
}
|
|
|
|
// 转换 RawRootItem
|
|
if len(proof.RawRootItem) > 0 {
|
|
pbProof.RawRootItem = make([]*pb.MerkleTreeProofItem, 0, len(proof.RawRootItem))
|
|
for _, item := range proof.RawRootItem {
|
|
pbProof.RawRootItem = append(pbProof.RawRootItem, &pb.MerkleTreeProofItem{
|
|
Floor: item.Floor,
|
|
Hash: item.Hash,
|
|
Left: item.Left,
|
|
})
|
|
}
|
|
}
|
|
|
|
return pbProof
|
|
}
|