feat: 完善数据库持久化与存证功能

主要更新:

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 自动初始化和历史数据处理验证通过
This commit is contained in:
ryan
2025-12-24 15:31:11 +08:00
parent 88f80ffa5e
commit 4b72a37120
60 changed files with 6160 additions and 1313 deletions

View File

@@ -7,7 +7,7 @@ import (
_ "github.com/crpt/go-crpt/ed25519" // 注册 Ed25519
_ "github.com/crpt/go-crpt/sm2" // 注册 SM2
"go.yandata.net/iod/iod/trustlog-sdk/api/logger"
"go.yandata.net/iod/iod/go-trustlog/api/logger"
)
// ConfigSigner 基于配置的通用签名器

View File

@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
func TestNewConfigSigner_SM2(t *testing.T) {

View File

@@ -6,7 +6,7 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
"go.yandata.net/iod/iod/trustlog-sdk/api/grpc/pb"
"go.yandata.net/iod/iod/go-trustlog/api/grpc/pb"
)
// FromProtobuf 将protobuf的OperationData转换为model.Operation.

View File

@@ -8,8 +8,8 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
"go.yandata.net/iod/iod/trustlog-sdk/api/grpc/pb"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/grpc/pb"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
func TestFromProtobuf_Nil(t *testing.T) {

View File

@@ -11,7 +11,7 @@ import (
_ "github.com/crpt/go-crpt/ed25519" // Import Ed25519
_ "github.com/crpt/go-crpt/sm2" // Import SM2
"go.yandata.net/iod/iod/trustlog-sdk/api/logger"
"go.yandata.net/iod/iod/go-trustlog/api/logger"
)
// SignatureAlgorithm 定义支持的签名算法类型.

View File

@@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
func TestCryptoConfig_Validate(t *testing.T) {

View File

@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
// TestSignVerifyDataConsistency 详细测试加签和验签的数据一致性.

View File

@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
// TestSignVerifyConsistency 测试加签和验签的一致性

View File

@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
func TestNewEnvelopeConfig(t *testing.T) {

View File

@@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
func TestGetHashTool(t *testing.T) {

View File

@@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
func TestOperation_Key(t *testing.T) {

View File

@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
// TestOperation_TimestampNanosecondPrecision 验证 Operation 的时间戳在 CBOR 序列化/反序列化后能保留纳秒精度

View File

@@ -1,7 +1,7 @@
package model
import (
"go.yandata.net/iod/iod/trustlog-sdk/api/grpc/pb"
"go.yandata.net/iod/iod/go-trustlog/api/grpc/pb"
)
// MerkleTreeProofItem 表示Merkle树证明项.

View File

@@ -6,8 +6,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/grpc/pb"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"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) {

View File

@@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
func TestRecord_Key(t *testing.T) {

View File

@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
// TestRecord_TimestampNanosecondPrecision 验证 Record 的时间戳在 CBOR 序列化/反序列化后能保留纳秒精度

View File

@@ -8,7 +8,7 @@ import (
"github.com/crpt/go-crpt"
_ "github.com/crpt/go-crpt/sm2" // Import SM2 to register it
"go.yandata.net/iod/iod/trustlog-sdk/api/logger"
"go.yandata.net/iod/iod/go-trustlog/api/logger"
)
var (

View File

@@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
func TestComputeSignature_EmptyPrivateKey(t *testing.T) {

View File

@@ -3,7 +3,7 @@ package model
import (
"bytes"
"go.yandata.net/iod/iod/trustlog-sdk/api/logger"
"go.yandata.net/iod/iod/go-trustlog/api/logger"
)
// Signer 签名器接口,用于抽象不同的签名算法实现。
@@ -127,10 +127,16 @@ func NewNopSigner() *NopSigner {
return &NopSigner{}
}
// Sign 直接返回原数据,不做任何签名操作。
func (n *NopSigner) Sign(_ []byte) ([]byte, error) {
return ([]byte)("test"), nil
// Sign 直接返回原数据的副本,不做任何签名操作。
func (n *NopSigner) Sign(data []byte) ([]byte, error) {
log := logger.GetGlobalLogger()
log.Debug("NopSigner: signing data (returning copy)",
"dataLength", len(data),
)
// 返回数据副本
result := make([]byte, len(data))
copy(result, data)
return result, nil
}
// Verify 验证签名是否等于原数据。

View File

@@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
func TestNewSM2Signer(t *testing.T) {

View File

@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
// TestSM2HashConsistency 验证SM2加签和验签的一致性

View File

@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
// TestSM2RequiresHash 测试SM2是否要求预先hash数据

View File

@@ -5,7 +5,7 @@ import (
"github.com/stretchr/testify/assert"
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
"go.yandata.net/iod/iod/go-trustlog/api/model"
)
func TestValidationResult_IsProcessing(t *testing.T) {