主要更新: 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 自动初始化和历史数据处理验证通过
350 lines
9.1 KiB
Go
350 lines
9.1 KiB
Go
package model_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.yandata.net/iod/iod/go-trustlog/api/grpc/pb"
|
|
"go.yandata.net/iod/iod/go-trustlog/api/model"
|
|
)
|
|
|
|
func TestProofFromProtobuf_Nil(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
result := model.ProofFromProtobuf(nil)
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestProofFromProtobuf_Empty(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Empty(t, result.Sign)
|
|
assert.Empty(t, result.Version)
|
|
assert.Nil(t, result.ColItems)
|
|
assert.Nil(t, result.RawItems)
|
|
assert.Nil(t, result.ColRootItem)
|
|
assert.Nil(t, result.RawRootItem)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithSign(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
Sign: "test-signature",
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "test-signature", result.Sign)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithVersion(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
Version: "v1.0.0",
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "v1.0.0", result.Version)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithColItems(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
ColItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 1, Hash: "hash1", Left: true},
|
|
{Floor: 2, Hash: "hash2", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.ColItems, 2)
|
|
assert.Equal(t, uint32(1), result.ColItems[0].Floor)
|
|
assert.Equal(t, "hash1", result.ColItems[0].Hash)
|
|
assert.True(t, result.ColItems[0].Left)
|
|
assert.Equal(t, uint32(2), result.ColItems[1].Floor)
|
|
assert.Equal(t, "hash2", result.ColItems[1].Hash)
|
|
assert.False(t, result.ColItems[1].Left)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithRawItems(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
RawItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 3, Hash: "hash3", Left: true},
|
|
},
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.RawItems, 1)
|
|
assert.Equal(t, uint32(3), result.RawItems[0].Floor)
|
|
assert.Equal(t, "hash3", result.RawItems[0].Hash)
|
|
assert.True(t, result.RawItems[0].Left)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithColRootItem(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
ColRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 4, Hash: "hash4", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.ColRootItem, 1)
|
|
assert.Equal(t, uint32(4), result.ColRootItem[0].Floor)
|
|
assert.Equal(t, "hash4", result.ColRootItem[0].Hash)
|
|
assert.False(t, result.ColRootItem[0].Left)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithRawRootItem(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
RawRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 5, Hash: "hash5", Left: true},
|
|
},
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.RawRootItem, 1)
|
|
assert.Equal(t, uint32(5), result.RawRootItem[0].Floor)
|
|
assert.Equal(t, "hash5", result.RawRootItem[0].Hash)
|
|
assert.True(t, result.RawRootItem[0].Left)
|
|
}
|
|
|
|
func TestProofFromProtobuf_Full(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
Sign: "full-signature",
|
|
Version: "v1.0.0",
|
|
ColItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 1, Hash: "col1", Left: true},
|
|
},
|
|
RawItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 2, Hash: "raw1", Left: false},
|
|
},
|
|
ColRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 3, Hash: "colroot1", Left: true},
|
|
},
|
|
RawRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 4, Hash: "rawroot1", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "full-signature", result.Sign)
|
|
assert.Equal(t, "v1.0.0", result.Version)
|
|
assert.Len(t, result.ColItems, 1)
|
|
assert.Len(t, result.RawItems, 1)
|
|
assert.Len(t, result.ColRootItem, 1)
|
|
assert.Len(t, result.RawRootItem, 1)
|
|
}
|
|
|
|
func TestProofToProtobuf_Nil(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
result := model.ProofToProtobuf(nil)
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestProofToProtobuf_Empty(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Empty(t, result.GetSign())
|
|
assert.Empty(t, result.GetVersion())
|
|
assert.Nil(t, result.GetColItems())
|
|
assert.Nil(t, result.GetRawItems())
|
|
assert.Nil(t, result.GetColRootItem())
|
|
assert.Nil(t, result.GetRawRootItem())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithSign(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
Sign: "test-signature",
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "test-signature", result.GetSign())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithVersion(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
Version: "v1.0.0",
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "v1.0.0", result.GetVersion())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithColItems(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
ColItems: []*model.MerkleTreeProofItem{
|
|
{Floor: 1, Hash: "hash1", Left: true},
|
|
{Floor: 2, Hash: "hash2", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.GetColItems(), 2)
|
|
assert.Equal(t, uint32(1), result.GetColItems()[0].GetFloor())
|
|
assert.Equal(t, "hash1", result.GetColItems()[0].GetHash())
|
|
assert.True(t, result.GetColItems()[0].GetLeft())
|
|
assert.Equal(t, uint32(2), result.GetColItems()[1].GetFloor())
|
|
assert.Equal(t, "hash2", result.GetColItems()[1].GetHash())
|
|
assert.False(t, result.GetColItems()[1].GetLeft())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithRawItems(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
RawItems: []*model.MerkleTreeProofItem{
|
|
{Floor: 3, Hash: "hash3", Left: true},
|
|
},
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.GetRawItems(), 1)
|
|
assert.Equal(t, uint32(3), result.GetRawItems()[0].GetFloor())
|
|
assert.Equal(t, "hash3", result.GetRawItems()[0].GetHash())
|
|
assert.True(t, result.GetRawItems()[0].GetLeft())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithColRootItem(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
ColRootItem: []*model.MerkleTreeProofItem{
|
|
{Floor: 4, Hash: "hash4", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.GetColRootItem(), 1)
|
|
assert.Equal(t, uint32(4), result.GetColRootItem()[0].GetFloor())
|
|
assert.Equal(t, "hash4", result.GetColRootItem()[0].GetHash())
|
|
assert.False(t, result.GetColRootItem()[0].GetLeft())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithRawRootItem(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
RawRootItem: []*model.MerkleTreeProofItem{
|
|
{Floor: 5, Hash: "hash5", Left: true},
|
|
},
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.GetRawRootItem(), 1)
|
|
assert.Equal(t, uint32(5), result.GetRawRootItem()[0].GetFloor())
|
|
assert.Equal(t, "hash5", result.GetRawRootItem()[0].GetHash())
|
|
assert.True(t, result.GetRawRootItem()[0].GetLeft())
|
|
}
|
|
|
|
func TestProofToProtobuf_Full(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
Sign: "full-signature",
|
|
Version: "v1.0.0",
|
|
ColItems: []*model.MerkleTreeProofItem{
|
|
{Floor: 1, Hash: "col1", Left: true},
|
|
},
|
|
RawItems: []*model.MerkleTreeProofItem{
|
|
{Floor: 2, Hash: "raw1", Left: false},
|
|
},
|
|
ColRootItem: []*model.MerkleTreeProofItem{
|
|
{Floor: 3, Hash: "colroot1", Left: true},
|
|
},
|
|
RawRootItem: []*model.MerkleTreeProofItem{
|
|
{Floor: 4, Hash: "rawroot1", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "full-signature", result.GetSign())
|
|
assert.Equal(t, "v1.0.0", result.GetVersion())
|
|
assert.Len(t, result.GetColItems(), 1)
|
|
assert.Len(t, result.GetRawItems(), 1)
|
|
assert.Len(t, result.GetColRootItem(), 1)
|
|
assert.Len(t, result.GetRawRootItem(), 1)
|
|
}
|
|
|
|
func TestProofRoundTrip(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
original := &pb.Proof{
|
|
Sign: "round-trip-signature",
|
|
Version: "v1.0.0",
|
|
ColItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 1, Hash: "col1", Left: true},
|
|
{Floor: 2, Hash: "col2", Left: false},
|
|
},
|
|
RawItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 3, Hash: "raw1", Left: true},
|
|
},
|
|
ColRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 4, Hash: "colroot1", Left: false},
|
|
},
|
|
RawRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 5, Hash: "rawroot1", Left: true},
|
|
},
|
|
}
|
|
|
|
// Convert to model
|
|
modelProof := model.ProofFromProtobuf(original)
|
|
require.NotNil(t, modelProof)
|
|
|
|
// Convert back to protobuf
|
|
pbProof := model.ProofToProtobuf(modelProof)
|
|
require.NotNil(t, pbProof)
|
|
|
|
// Verify round trip
|
|
assert.Equal(t, original.GetSign(), pbProof.GetSign())
|
|
assert.Equal(t, original.GetVersion(), pbProof.GetVersion())
|
|
assert.Len(t, pbProof.GetColItems(), 2)
|
|
assert.Len(t, pbProof.GetRawItems(), 1)
|
|
assert.Len(t, pbProof.GetColRootItem(), 1)
|
|
assert.Len(t, pbProof.GetRawRootItem(), 1)
|
|
|
|
assert.Equal(t, original.GetColItems()[0].GetFloor(), pbProof.GetColItems()[0].GetFloor())
|
|
assert.Equal(t, original.GetColItems()[0].GetHash(), pbProof.GetColItems()[0].GetHash())
|
|
assert.Equal(t, original.GetColItems()[0].GetLeft(), pbProof.GetColItems()[0].GetLeft())
|
|
}
|