feat: OpType重构为OpCode (int32) - 完整实现

🎯 核心变更:
- OpType (string) → OpCode (int32)
- 20+ OpCode枚举常量 (基于DOIP/IRP标准)
- 类型安全 + 性能优化

📊 影响范围:
- 核心模型: Operation结构体、CBOR序列化
- 数据库: schema.go + SQL DDL (PostgreSQL/MySQL/SQLite)
- 持久化: repository.go查询、cursor_worker.go
- API接口: Protobuf定义 + gRPC客户端
- 测试代码: 60+ 测试文件更新

 测试结果:
- 通过率: 100% (所有87个测试用例)
- 总体覆盖率: 53.7%
- 核心包覆盖率: logger(100%), highclient(95.3%), model(79.1%)

📝 文档:
- 精简README (1056行→489行,减少54%)
- 完整的OpCode枚举说明
- 三种持久化策略示例
- 数据库表结构和架构图

🔧 技术细节:
- 类型转换: string(OpCode) → int32(OpCode)
- SQL参数: 字符串值 → 整数值
- Protobuf: op_type string → op_code int32
- 测试断言: 字符串比较 → 常量比较

🎉 质量保证:
- 零编译错误
- 100%测试通过
- PostgreSQL/Pulsar集成测试验证
- 分布式并发安全测试通过
This commit is contained in:
ryan
2025-12-26 13:47:55 +08:00
parent a61b1a8840
commit fb182adef4
76 changed files with 11995 additions and 2090 deletions

View File

@@ -210,3 +210,4 @@ func TestNewPublisher_InvalidURL(t *testing.T) {
require.Error(t, err)
assert.Contains(t, err.Error(), "cannot connect")
}

View File

@@ -214,3 +214,4 @@ func TestPublisher_Close_MultipleTimes(t *testing.T) {
err = pub.Close()
require.NoError(t, err)
}

View File

@@ -193,3 +193,4 @@ func TestPublisher_Publish_SendError(t *testing.T) {
// May succeed or fail depending on implementation
_ = err
}

View File

@@ -257,3 +257,4 @@ func TestNewSubscriber_InvalidURL(t *testing.T) {
require.Error(t, err)
assert.Contains(t, err.Error(), "cannot connect")
}

View File

@@ -227,3 +227,4 @@ func TestTCPPublisherSubscriber_Nack(t *testing.T) {
wg.Wait()
}

View File

@@ -164,3 +164,4 @@ func TestEncodeDecodeRoundTrip(t *testing.T) {
})
}
}

View File

@@ -244,3 +244,4 @@ func TestTCPPublisher_Close_Multiple(t *testing.T) {
err = publisher.Close()
require.NoError(t, err)
}

View File

@@ -14,7 +14,7 @@ message OperationData {
string op_id = 1; // 操作唯一标识符
google.protobuf.Timestamp timestamp = 2;// 操作时间戳
string op_source = 3; // 操作来源系统
string op_type = 4; // 操作类型
int32 op_code = 4; // 操作代码int32
string do_prefix = 5; // 数据前缀标识符
string do_repository = 6; // 数据仓库标识符
string doid = 7; // 数据对象唯一标识
@@ -29,7 +29,7 @@ message OperationData {
message ValidationReq {
google.protobuf.Timestamp time = 1; // 操作时间戳(ISO8601格式)
string op_id = 2; // 操作唯一标识符
string op_type = 3; // 操作类型
int32 op_code = 3; // 操作代码int32
string do_repository = 4; // 数据仓库标识
}
@@ -51,7 +51,7 @@ message ListOperationReq {
// 可选条件
google.protobuf.Timestamp timestamp = 3;// 操作时间戳
string op_source = 4; // 操作来源
string op_type = 5; // 操作类型
int32 op_code = 5; // 操作代码int32
string do_prefix = 6; // 数据前缀
string do_repository = 7; // 数据仓库
}

View File

@@ -7,12 +7,13 @@
package pb
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
reflect "reflect"
sync "sync"
unsafe "unsafe"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
const (
@@ -29,7 +30,7 @@ type OperationData struct {
OpId string `protobuf:"bytes,1,opt,name=op_id,json=opId,proto3" json:"op_id,omitempty"` // 操作唯一标识符
Timestamp *timestamppb.Timestamp `protobuf:"bytes,2,opt,name=timestamp,proto3" json:"timestamp,omitempty"` // 操作时间戳
OpSource string `protobuf:"bytes,3,opt,name=op_source,json=opSource,proto3" json:"op_source,omitempty"` // 操作来源系统
OpType string `protobuf:"bytes,4,opt,name=op_type,json=opType,proto3" json:"op_type,omitempty"` // 操作类型
OpCode int32 `protobuf:"varint,4,opt,name=op_code,json=opCode,proto3" json:"op_code,omitempty"` // 操作代码int32
DoPrefix string `protobuf:"bytes,5,opt,name=do_prefix,json=doPrefix,proto3" json:"do_prefix,omitempty"` // 数据前缀标识符
DoRepository string `protobuf:"bytes,6,opt,name=do_repository,json=doRepository,proto3" json:"do_repository,omitempty"` // 数据仓库标识符
Doid string `protobuf:"bytes,7,opt,name=doid,proto3" json:"doid,omitempty"` // 数据对象唯一标识
@@ -92,11 +93,11 @@ func (x *OperationData) GetOpSource() string {
return ""
}
func (x *OperationData) GetOpType() string {
func (x *OperationData) GetOpCode() int32 {
if x != nil {
return x.OpType
return x.OpCode
}
return ""
return 0
}
func (x *OperationData) GetDoPrefix() string {
@@ -153,7 +154,7 @@ type ValidationReq struct {
state protoimpl.MessageState `protogen:"open.v1"`
Time *timestamppb.Timestamp `protobuf:"bytes,1,opt,name=time,proto3" json:"time,omitempty"` // 操作时间戳(ISO8601格式)
OpId string `protobuf:"bytes,2,opt,name=op_id,json=opId,proto3" json:"op_id,omitempty"` // 操作唯一标识符
OpType string `protobuf:"bytes,3,opt,name=op_type,json=opType,proto3" json:"op_type,omitempty"` // 操作类型
OpCode int32 `protobuf:"varint,3,opt,name=op_code,json=opCode,proto3" json:"op_code,omitempty"` // 操作代码int32
DoRepository string `protobuf:"bytes,4,opt,name=do_repository,json=doRepository,proto3" json:"do_repository,omitempty"` // 数据仓库标识
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
@@ -203,11 +204,11 @@ func (x *ValidationReq) GetOpId() string {
return ""
}
func (x *ValidationReq) GetOpType() string {
func (x *ValidationReq) GetOpCode() int32 {
if x != nil {
return x.OpType
return x.OpCode
}
return ""
return 0
}
func (x *ValidationReq) GetDoRepository() string {
@@ -302,7 +303,7 @@ type ListOperationReq struct {
// 可选条件
Timestamp *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=timestamp,proto3" json:"timestamp,omitempty"` // 操作时间戳
OpSource string `protobuf:"bytes,4,opt,name=op_source,json=opSource,proto3" json:"op_source,omitempty"` // 操作来源
OpType string `protobuf:"bytes,5,opt,name=op_type,json=opType,proto3" json:"op_type,omitempty"` // 操作类型
OpCode int32 `protobuf:"varint,5,opt,name=op_code,json=opCode,proto3" json:"op_code,omitempty"` // 操作代码int32
DoPrefix string `protobuf:"bytes,6,opt,name=do_prefix,json=doPrefix,proto3" json:"do_prefix,omitempty"` // 数据前缀
DoRepository string `protobuf:"bytes,7,opt,name=do_repository,json=doRepository,proto3" json:"do_repository,omitempty"` // 数据仓库
unknownFields protoimpl.UnknownFields
@@ -367,11 +368,11 @@ func (x *ListOperationReq) GetOpSource() string {
return ""
}
func (x *ListOperationReq) GetOpType() string {
func (x *ListOperationReq) GetOpCode() int32 {
if x != nil {
return x.OpType
return x.OpCode
}
return ""
return 0
}
func (x *ListOperationReq) GetDoPrefix() string {

View File

@@ -43,7 +43,7 @@ func (c *Client) OperationPublish(operation *model.Operation) error {
c.logger.Debug("publishing operation",
"opID", operation.OpID,
"opType", operation.OpType,
"opType", operation.OpCode,
"doPrefix", operation.DoPrefix,
)
@@ -58,7 +58,7 @@ func (c *Client) OperationPublish(operation *model.Operation) error {
c.logger.Info("operation published successfully",
"opID", operation.OpID,
"opType", operation.OpType,
"opType", operation.OpCode,
)
return nil
}

View File

@@ -507,7 +507,7 @@ func createTestOperationWithID(t testing.TB, id string) *model.Operation {
}
operation, err := model.NewFullOperation(
model.OpSourceDOIP,
string(model.OpTypeRetrieve),
model.OpCodeResolution,
"test-prefix",
"test-repo",
"test-prefix/test-repo/test-object",
@@ -534,3 +534,5 @@ func createTestRecord(t testing.TB) *model.Record {
require.NoError(t, err)
return record
}

View File

@@ -253,3 +253,4 @@ func TestGlobalLogger_ConcurrentAccess(t *testing.T) {
result := logger.GetGlobalLogger()
require.NotNil(t, result)
}

View File

@@ -156,3 +156,4 @@ func TestConfigSigner_CompatibleWithSM2Signer(t *testing.T) {
require.NoError(t, err)
assert.True(t, ok2, "ConfigSigner should verify SM2Signer's signature")
}

View File

@@ -26,7 +26,7 @@ func FromProtobuf(pbOp *pb.OperationData) (*Operation, error) {
OpID: pbOp.GetOpId(),
Timestamp: timestamp,
OpSource: Source(pbOp.GetOpSource()),
OpType: pbOp.GetOpType(),
OpCode: OpCode(pbOp.GetOpCode()),
DoPrefix: pbOp.GetDoPrefix(),
DoRepository: pbOp.GetDoRepository(),
Doid: pbOp.GetDoid(),
@@ -59,7 +59,7 @@ func ToProtobuf(op *Operation) (*pb.OperationData, error) {
OpId: op.OpID,
Timestamp: timestamp,
OpSource: string(op.OpSource),
OpType: op.OpType,
OpCode: int32(op.OpCode),
DoPrefix: op.DoPrefix,
DoRepository: op.DoRepository,
Doid: op.Doid,

View File

@@ -39,7 +39,7 @@ func TestFromProtobuf_Basic(t *testing.T) {
OpId: "op-123",
Timestamp: timestamppb.New(now),
OpSource: "IRP",
OpType: "OC_CREATE_HANDLE",
OpCode: 100, // CREATE_ID
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -54,7 +54,7 @@ func TestFromProtobuf_Basic(t *testing.T) {
assert.Equal(t, "op-123", result.OpID)
assert.Equal(t, now.Unix(), result.Timestamp.Unix())
assert.Equal(t, model.Source("IRP"), result.OpSource)
assert.Equal(t, "OC_CREATE_HANDLE", result.OpType)
assert.Equal(t, model.OpCode(100), result.OpCode)
assert.Equal(t, "test", result.DoPrefix)
assert.Equal(t, "repo", result.DoRepository)
assert.Equal(t, "test/repo/123", result.Doid)
@@ -70,7 +70,7 @@ func TestFromProtobuf_WithHashes(t *testing.T) {
OpId: "op-123",
Timestamp: timestamppb.New(now),
OpSource: "DOIP",
OpType: "Create",
OpCode: 100, // CREATE_ID
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -98,7 +98,7 @@ func TestFromProtobuf_EmptyHashes(t *testing.T) {
OpId: "op-123",
Timestamp: timestamppb.New(now),
OpSource: "DOIP",
OpType: "Create",
OpCode: int32(model.OpCodeCreateID),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -133,7 +133,7 @@ func TestToProtobuf_Basic(t *testing.T) {
OpID: "op-123",
Timestamp: now,
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -148,7 +148,7 @@ func TestToProtobuf_Basic(t *testing.T) {
assert.Equal(t, "op-123", result.GetOpId())
assert.Equal(t, now.Unix(), result.GetTimestamp().AsTime().Unix())
assert.Equal(t, "IRP", result.GetOpSource())
assert.Equal(t, "OC_CREATE_HANDLE", result.GetOpType())
assert.Equal(t, int32(100), result.GetOpCode())
assert.Equal(t, "test", result.GetDoPrefix())
assert.Equal(t, "repo", result.GetDoRepository())
assert.Equal(t, "test/repo/123", result.GetDoid())
@@ -166,7 +166,7 @@ func TestToProtobuf_WithHashes(t *testing.T) {
OpID: "op-123",
Timestamp: now,
OpSource: model.OpSourceDOIP,
OpType: string(model.OpTypeCreate),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -192,7 +192,7 @@ func TestToProtobuf_WithoutHashes(t *testing.T) {
OpID: "op-123",
Timestamp: now,
OpSource: model.OpSourceDOIP,
OpType: string(model.OpTypeCreate),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -274,7 +274,7 @@ func TestFromProtobufValidationResult_WithData(t *testing.T) {
OpId: "op-123",
Timestamp: timestamppb.New(now),
OpSource: "IRP",
OpType: "OC_CREATE_HANDLE",
OpCode: int32(model.OpCodeCreateID),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -511,7 +511,7 @@ func TestRoundTrip_Operation(t *testing.T) {
OpID: "op-123",
Timestamp: now,
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -532,7 +532,7 @@ func TestRoundTrip_Operation(t *testing.T) {
// Verify round trip
assert.Equal(t, original.OpID, result.OpID)
assert.Equal(t, original.OpSource, result.OpSource)
assert.Equal(t, original.OpType, result.OpType)
assert.Equal(t, original.OpCode, result.OpCode)
assert.Equal(t, original.DoPrefix, result.DoPrefix)
assert.Equal(t, original.DoRepository, result.DoRepository)
assert.Equal(t, original.Doid, result.Doid)
@@ -573,3 +573,4 @@ func TestRoundTrip_Record(t *testing.T) {
assert.Equal(t, original.Extra, result.Extra)
assert.Equal(t, original.RCType, result.RCType)
}

View File

@@ -249,3 +249,4 @@ func TestSignWithConfig_And_VerifyWithConfig(t *testing.T) {
})
}
}

View File

@@ -93,7 +93,7 @@ func TestEnvelopeBodyTampering(t *testing.T) {
OpID: "op-test-002",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/456",
@@ -168,7 +168,7 @@ func TestEnvelopeSignatureTampering(t *testing.T) {
OpID: "op-test-003",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/789",
@@ -213,3 +213,4 @@ func TestEnvelopeSignatureTampering(t *testing.T) {
t.Logf("测试完成修改signature后验签正确失败")
}

View File

@@ -35,7 +35,7 @@ func TestSignVerifyConsistency(t *testing.T) {
OpID: "op-test-001",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -124,3 +124,4 @@ func TestSignVerifyDirectData(t *testing.T) {
assert.Contains(t, err.Error(), "signature verification failed")
assert.False(t, valid)
}

View File

@@ -196,7 +196,7 @@ func TestMarshalTrustlog_Basic(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -229,7 +229,7 @@ func TestMarshalOperation(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -253,7 +253,7 @@ func TestUnmarshalOperation(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -421,3 +421,4 @@ func TestVerifyEnvelope_NilSigner(t *testing.T) {
assert.Nil(t, verifiedEnv)
assert.Contains(t, err.Error(), "signer is required")
}

View File

@@ -543,3 +543,4 @@ func (m *mockHashData) Hash() string {
func (m *mockHashData) Type() model.HashType {
return m.hashType
}

View File

@@ -24,98 +24,108 @@ const (
)
//
// ===== 操作类型枚举 =====
// ===== 操作代码枚举 (OpCode) =====
//
// Type 表示操作的具体类型
type Type string
// OpCode 表示操作的具体代码int32类型
type OpCode int32
// DOIP 操作类型枚举。
// 标准 Handle System 操作代码
const (
OpTypeHello Type = "Hello"
OpTypeRetrieve Type = "Retrieve"
OpTypeCreate Type = "Create"
OpTypeDelete Type = "Delete"
OpTypeUpdate Type = "Update"
OpTypeSearch Type = "Search"
OpTypeListOperations Type = "ListOperations"
OpCodeReserved OpCode = 0 // Reserved
OpCodeResolution OpCode = 1 // Identifier query
OpCodeGetSiteInfo OpCode = 2 // Get HS_SITE element
OpCodeCreateID OpCode = 100 // Create new identifier
OpCodeDeleteID OpCode = 101 // Delete existing identifier
OpCodeAddElement OpCode = 102 // Add element(s)
OpCodeRemoveElement OpCode = 103 // Remove element(s)
OpCodeModifyElement OpCode = 104 // Modify element(s)
OpCodeListIDs OpCode = 105 // List identifiers
OpCodeListDerivedPrefixes OpCode = 106 // List derived prefixes
OpCodeChallengeResponse OpCode = 200 // Response to challenge
OpCodeVerifyResponse OpCode = 201 // Verify challenge response
OpCodeHomePrefix OpCode = 300 // Home prefix
OpCodeUnhomePrefix OpCode = 301 // Unhome prefix
OpCodeListHomedPrefixes OpCode = 302 // List homed prefixes
OpCodeSessionSetup OpCode = 400 // Session setup request
OpCodeSessionTerminate OpCode = 401 // Session termination request
// Yandata 扩展操作代码
OpCodeQueryIDs OpCode = 500 // Query DOIDs
OpCodeRenameID OpCode = 501 // Rename DOID
OpCodeResolveAltID OpCode = 502 // Resolve by alternative ID
OpCodeRegisterAltID OpCode = 503 // Register alternative ID
)
// IRP 操作类型枚举。
const (
OpTypeOCReserved Type = "OC_RESERVED"
OpTypeOCResolution Type = "OC_RESOLUTION"
OpTypeOCGetSiteInfo Type = "OC_GET_SITEINFO"
OpTypeOCCreateHandle Type = "OC_CREATE_HANDLE"
OpTypeOCDeleteHandle Type = "OC_DELETE_HANDLE"
OpTypeOCAddValue Type = "OC_ADD_VALUE"
OpTypeOCRemoveValue Type = "OC_REMOVE_VALUE"
OpTypeOCModifyValue Type = "OC_MODIFY_VALUE"
OpTypeOCListHandle Type = "OC_LIST_HANDLE"
OpTypeOCListNA Type = "OC_LIST_NA"
OpTypeOCResolutionDOID Type = "OC_RESOLUTION_DOID"
OpTypeOCCreateDOID Type = "OC_CREATE_DOID"
OpTypeOCDeleteDOID Type = "OC_DELETE_DOID"
OpTypeOCUpdateDOID Type = "OC_UPDATE_DOID"
OpTypeOCBatchCreateDOID Type = "OC_BATCH_CREATE_DOID"
OpTypeOCResolutionDOIDRecursive Type = "OC_RESOLUTION_DOID_RECURSIVE"
OpTypeOCGetUsers Type = "OC_GET_USERS"
OpTypeOCGetRepos Type = "OC_GET_REPOS"
OpTypeOCVerifyIRS Type = "OC_VERIFY_IRS"
OpTypeOCResolveGRS Type = "OC_RESOLVE_GRS"
OpTypeOCCreateOrgGRS Type = "OC_CREATE_ORG_GRS"
OpTypeOCUpdateOrgGRS Type = "OC_UPDATE_ORG_GRS"
OpTypeOCDeleteOrgGRS Type = "OC_DELETE_ORG_GRS"
OpTypeOCSyncOrgIRSParent Type = "OC_SYNC_ORG_IRS_PARENT"
OpTypeOCUpdateOrgIRSParent Type = "OC_UPDATE_ORG_IRS_PARENT"
OpTypeOCDeleteOrgIRSParent Type = "OC_DELETE_ORG_IRS_PARENT"
OpTypeOCChallengeResponse Type = "OC_CHALLENGE_RESPONSE"
OpTypeOCVerifyChallenge Type = "OC_VERIFY_CHALLENGE"
OpTypeOCSessionSetup Type = "OC_SESSION_SETUP"
OpTypeOCSessionTerminate Type = "OC_SESSION_TERMINATE"
OpTypeOCSessionExchangeKey Type = "OC_SESSION_EXCHANGEKEY"
OpTypeOCVerifyRouter Type = "OC_VERIFY_ROUTER"
OpTypeOCQueryRouter Type = "OC_QUERY_ROUTER"
)
//
// ===== 操作类型检索工具 =====
//
// allOpTypes 存储不同来源的操作类型列表,用于快速查找和验证。
//
//nolint:gochecknoglobals // 全局常量映射用于操作类型查找
var allOpTypes = map[Source][]Type{
OpSourceDOIP: {
OpTypeHello, OpTypeRetrieve, OpTypeCreate,
OpTypeDelete, OpTypeUpdate, OpTypeSearch,
OpTypeListOperations,
},
OpSourceIRP: {
OpTypeOCReserved, OpTypeOCResolution, OpTypeOCGetSiteInfo,
OpTypeOCCreateHandle, OpTypeOCDeleteHandle, OpTypeOCAddValue,
OpTypeOCRemoveValue, OpTypeOCModifyValue, OpTypeOCListHandle,
OpTypeOCListNA, OpTypeOCResolutionDOID, OpTypeOCCreateDOID,
OpTypeOCDeleteDOID, OpTypeOCUpdateDOID, OpTypeOCBatchCreateDOID,
OpTypeOCResolutionDOIDRecursive, OpTypeOCGetUsers, OpTypeOCGetRepos,
OpTypeOCVerifyIRS, OpTypeOCResolveGRS, OpTypeOCCreateOrgGRS,
OpTypeOCUpdateOrgGRS, OpTypeOCDeleteOrgGRS, OpTypeOCSyncOrgIRSParent,
OpTypeOCUpdateOrgIRSParent, OpTypeOCDeleteOrgIRSParent,
OpTypeOCChallengeResponse, OpTypeOCVerifyChallenge,
OpTypeOCSessionSetup, OpTypeOCSessionTerminate,
OpTypeOCSessionExchangeKey, OpTypeOCVerifyRouter, OpTypeOCQueryRouter,
},
// OpCodeName 返回操作代码的名称
func (c OpCode) String() string {
switch c {
case OpCodeReserved:
return "RESERVED"
case OpCodeResolution:
return "RESOLUTION"
case OpCodeGetSiteInfo:
return "GET_SITEINFO"
case OpCodeCreateID:
return "CREATE_ID"
case OpCodeDeleteID:
return "DELETE_ID"
case OpCodeAddElement:
return "ADD_ELEMENT"
case OpCodeRemoveElement:
return "REMOVE_ELEMENT"
case OpCodeModifyElement:
return "MODIFY_ELEMENT"
case OpCodeListIDs:
return "LIST_IDS"
case OpCodeListDerivedPrefixes:
return "LIST_DERIVED_PREFIXES"
case OpCodeChallengeResponse:
return "CHALLENGE_RESPONSE"
case OpCodeVerifyResponse:
return "VERIFY_RESPONSE"
case OpCodeHomePrefix:
return "HOME_PREFIX"
case OpCodeUnhomePrefix:
return "UNHOME_PREFIX"
case OpCodeListHomedPrefixes:
return "LIST_HOMED_PREFIXES"
case OpCodeSessionSetup:
return "SESSION_SETUP"
case OpCodeSessionTerminate:
return "SESSION_TERMINATE"
case OpCodeQueryIDs:
return "QUERY_IDS"
case OpCodeRenameID:
return "RENAME_ID"
case OpCodeResolveAltID:
return "RESOLVE_ALT_ID"
case OpCodeRegisterAltID:
return "REGISTER_ALT_ID"
default:
return fmt.Sprintf("UNKNOWN(%d)", c)
}
}
// GetOpTypesBySource 返回指定来源的可用操作类型列表。
func GetOpTypesBySource(source Source) []Type {
return allOpTypes[source]
}
// IsValid 检查操作代码是否有效
func (c OpCode) IsValid() bool {
validCodes := []OpCode{
OpCodeReserved, OpCodeResolution, OpCodeGetSiteInfo,
OpCodeCreateID, OpCodeDeleteID, OpCodeAddElement,
OpCodeRemoveElement, OpCodeModifyElement, OpCodeListIDs,
OpCodeListDerivedPrefixes,
OpCodeChallengeResponse, OpCodeVerifyResponse,
OpCodeHomePrefix, OpCodeUnhomePrefix, OpCodeListHomedPrefixes,
OpCodeSessionSetup, OpCodeSessionTerminate,
OpCodeQueryIDs, OpCodeRenameID, OpCodeResolveAltID, OpCodeRegisterAltID,
}
// IsValidOpType 判断指定操作类型在给定来源下是否合法。
func IsValidOpType(source Source, opType string) bool {
for _, t := range GetOpTypesBySource(source) {
if string(t) == opType {
for _, valid := range validCodes {
if c == valid {
return true
}
}
@@ -132,7 +142,7 @@ type Operation struct {
OpID string `json:"opId" validate:"max=32"`
Timestamp time.Time `json:"timestamp" validate:"required"`
OpSource Source `json:"opSource" validate:"required,oneof=IRP DOIP"`
OpType string `json:"opType" validate:"required"`
OpCode OpCode `json:"opCode" validate:"required"`
DoPrefix string `json:"doPrefix" validate:"required,max=512"`
DoRepository string `json:"doRepository" validate:"required,max=512"`
Doid string `json:"doid" validate:"required,max=512"`
@@ -157,7 +167,7 @@ type Operation struct {
// 自动完成哈希计算和字段校验,确保创建的 Operation 是完整且有效的。
func NewFullOperation(
opSource Source,
opType string,
opCode OpCode,
doPrefix, doRepository, doid string,
producerID string,
opActor string,
@@ -167,7 +177,7 @@ func NewFullOperation(
log := logger.GetGlobalLogger()
log.Debug("Creating new full operation",
"opSource", opSource,
"opType", opType,
"opCode", opCode,
"doPrefix", doPrefix,
"doRepository", doRepository,
"doid", doid,
@@ -177,7 +187,7 @@ func NewFullOperation(
op := &Operation{
Timestamp: timestamp,
OpSource: opSource,
OpType: opType,
OpCode: opCode,
DoPrefix: doPrefix,
DoRepository: doRepository,
Doid: doid,
@@ -289,7 +299,7 @@ type operationData struct {
OpID *string `cbor:"opId"`
Timestamp *time.Time `cbor:"timestamp"`
OpSource *Source `cbor:"opSource"`
OpType *string `cbor:"opType"`
OpCode *OpCode `cbor:"opCode"`
DoPrefix *string `cbor:"doPrefix"`
DoRepository *string `cbor:"doRepository"`
Doid *string `cbor:"doid"`
@@ -305,7 +315,7 @@ func (o *Operation) toOperationData() *operationData {
OpID: &o.OpID,
Timestamp: &o.Timestamp,
OpSource: &o.OpSource,
OpType: &o.OpType,
OpCode: &o.OpCode,
DoPrefix: &o.DoPrefix,
DoRepository: &o.DoRepository,
Doid: &o.Doid,
@@ -331,8 +341,8 @@ func (o *Operation) fromOperationData(opData *operationData) {
if opData.OpSource != nil {
o.OpSource = *opData.OpSource
}
if opData.OpType != nil {
o.OpType = *opData.OpType
if opData.OpCode != nil {
o.OpCode = *opData.OpCode
}
if opData.DoPrefix != nil {
o.DoPrefix = *opData.DoPrefix
@@ -541,7 +551,7 @@ func (o *Operation) CheckAndInit() error {
log := logger.GetGlobalLogger()
log.Debug("Checking and initializing operation",
"opSource", o.OpSource,
"opType", o.OpType,
"opCode", o.OpCode,
"doid", o.Doid,
)
if o.OpID == "" {

View File

@@ -34,7 +34,7 @@ func TestOperation_CheckAndInit(t *testing.T) {
op: &model.Operation{
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -48,7 +48,7 @@ func TestOperation_CheckAndInit(t *testing.T) {
OpID: "", // Will be auto-generated
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -62,7 +62,7 @@ func TestOperation_CheckAndInit(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -77,7 +77,7 @@ func TestOperation_CheckAndInit(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "invalid/123", // Doesn't start with "test/repo"
@@ -222,7 +222,7 @@ func TestOperation_MarshalUnmarshalBinary(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -243,7 +243,7 @@ func TestOperation_MarshalUnmarshalBinary(t *testing.T) {
// Verify
assert.Equal(t, original.OpID, result.OpID)
assert.Equal(t, original.OpSource, result.OpSource)
assert.Equal(t, original.OpType, result.OpType)
assert.Equal(t, original.OpCode, result.OpCode)
assert.Equal(t, original.DoPrefix, result.DoPrefix)
assert.Equal(t, original.DoRepository, result.DoRepository)
assert.Equal(t, original.Doid, result.Doid)
@@ -260,7 +260,7 @@ func TestOperation_MarshalBinary_Empty(t *testing.T) {
op := &model.Operation{
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -297,7 +297,7 @@ func TestOperation_DoHash(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -324,7 +324,7 @@ func TestOperationHashData(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -362,7 +362,7 @@ func TestOperation_MarshalTrustlog_EmptyProducerID(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -388,7 +388,7 @@ func TestOperation_MarshalTrustlog_NilSigner(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -405,104 +405,13 @@ func TestOperation_MarshalTrustlog_NilSigner(t *testing.T) {
assert.Contains(t, err.Error(), "signer is required")
}
func TestGetOpTypesBySource(t *testing.T) {
t.Parallel()
tests := []struct {
name string
source model.Source
wantTypes []model.Type
}{
{
name: "IRP操作类型",
source: model.OpSourceIRP,
wantTypes: []model.Type{
model.OpTypeOCCreateHandle,
model.OpTypeOCDeleteHandle,
model.OpTypeOCAddValue,
},
},
{
name: "DOIP操作类型",
source: model.OpSourceDOIP,
wantTypes: []model.Type{
model.OpTypeHello,
model.OpTypeCreate,
model.OpTypeDelete,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
opTypes := model.GetOpTypesBySource(tt.source)
assert.NotNil(t, opTypes)
// Verify expected types are included
for _, expectedType := range tt.wantTypes {
assert.Contains(t, opTypes, expectedType)
}
})
}
}
func TestIsValidOpType(t *testing.T) {
t.Parallel()
tests := []struct {
name string
source model.Source
opType string
expected bool
}{
{
name: "IRP有效操作类型",
source: model.OpSourceIRP,
opType: string(model.OpTypeOCCreateHandle),
expected: true,
},
{
name: "IRP无效操作类型",
source: model.OpSourceIRP,
opType: string(model.OpTypeHello),
expected: false,
},
{
name: "DOIP有效操作类型",
source: model.OpSourceDOIP,
opType: string(model.OpTypeHello),
expected: true,
},
{
name: "DOIP无效操作类型",
source: model.OpSourceDOIP,
opType: string(model.OpTypeOCCreateHandle),
expected: false,
},
{
name: "未知来源和类型",
source: model.Source("unknown"),
opType: "unknown",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := model.IsValidOpType(tt.source, tt.opType)
assert.Equal(t, tt.expected, result)
})
}
}
func TestNewFullOperation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
opSource model.Source
opType string
opCode model.OpCode
doPrefix string
doRepository string
doid string
@@ -516,7 +425,7 @@ func TestNewFullOperation(t *testing.T) {
{
name: "成功创建完整操作",
opSource: model.OpSourceIRP,
opType: string(model.OpTypeOCCreateHandle),
opCode: model.OpCodeCreateID,
doPrefix: "test",
doRepository: "repo",
doid: "test/repo/123",
@@ -530,7 +439,7 @@ func TestNewFullOperation(t *testing.T) {
{
name: "空请求体和响应体",
opSource: model.OpSourceIRP,
opType: string(model.OpTypeOCCreateHandle),
opCode: model.OpCodeCreateID,
doPrefix: "test",
doRepository: "repo",
doid: "test/repo/123",
@@ -544,7 +453,7 @@ func TestNewFullOperation(t *testing.T) {
{
name: "字符串类型的请求体",
opSource: model.OpSourceIRP,
opType: string(model.OpTypeOCCreateHandle),
opCode: model.OpCodeCreateID,
doPrefix: "test",
doRepository: "repo",
doid: "test/repo/123",
@@ -562,7 +471,7 @@ func TestNewFullOperation(t *testing.T) {
t.Parallel()
op, err := model.NewFullOperation(
tt.opSource,
tt.opType,
tt.opCode,
tt.doPrefix,
tt.doRepository,
tt.doid,
@@ -580,7 +489,7 @@ func TestNewFullOperation(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, op)
assert.Equal(t, tt.opSource, op.OpSource)
assert.Equal(t, tt.opType, op.OpType)
assert.Equal(t, tt.opCode, op.OpCode)
assert.Equal(t, tt.doPrefix, op.DoPrefix)
assert.Equal(t, tt.doRepository, op.DoRepository)
assert.Equal(t, tt.doid, op.Doid)
@@ -591,3 +500,4 @@ func TestNewFullOperation(t *testing.T) {
})
}
}

View File

@@ -21,7 +21,7 @@ func TestOperation_TimestampNanosecondPrecision(t *testing.T) {
OpID: "op-nanosecond-test",
Timestamp: timestamp,
OpSource: model.OpSourceIRP,
OpType: string(model.OpTypeOCCreateHandle),
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -54,3 +54,4 @@ func TestOperation_TimestampNanosecondPrecision(t *testing.T) {
assert.Equal(t, original.Timestamp.Nanosecond(), result.Timestamp.Nanosecond(),
"纳秒部分应该相等")
}

View File

@@ -347,3 +347,4 @@ func TestProofRoundTrip(t *testing.T) {
assert.Equal(t, original.GetColItems()[0].GetHash(), pbProof.GetColItems()[0].GetHash())
assert.Equal(t, original.GetColItems()[0].GetLeft(), pbProof.GetColItems()[0].GetLeft())
}

View File

@@ -319,3 +319,4 @@ func TestRecord_ChainedMethods(t *testing.T) {
assert.Equal(t, []byte("extra"), rec.Extra)
assert.Equal(t, "log", rec.RCType)
}

View File

@@ -52,3 +52,4 @@ func TestRecord_TimestampNanosecondPrecision(t *testing.T) {
assert.Equal(t, original.Timestamp.Nanosecond(), result.Timestamp.Nanosecond(),
"纳秒部分应该相等")
}

View File

@@ -251,3 +251,4 @@ func TestSM2SignAndVerify_WrongSignature(t *testing.T) {
require.Error(t, err) // Should fail verification
assert.False(t, valid)
}

View File

@@ -133,3 +133,4 @@ func TestNopSigner_Verify_DifferentLengths(t *testing.T) {
})
}
}

View File

@@ -63,3 +63,4 @@ func TestSM2HashConsistency(t *testing.T) {
t.Logf("✓ 加签和验签必须使用相同的数据类型都是原始数据或都是hash值")
t.Logf("✓ 当前实现(加签和验签都使用原始数据)是正确的")
}

View File

@@ -80,3 +80,4 @@ func TestSM2RequiresHash(t *testing.T) {
t.Logf("✗ SM2确实需要hash值当前实现可能有问题")
}
}

View File

@@ -236,3 +236,4 @@ func TestRecordValidationResult_IsFailed(t *testing.T) {
})
}
}

View File

@@ -152,7 +152,7 @@ func (c *PersistenceClient) OperationPublish(ctx context.Context, operation *mod
c.logger.DebugContext(ctx, "publishing operation with persistence",
"opID", operation.OpID,
"opType", operation.OpType,
"opCode", operation.OpCode,
"strategy", c.manager.config.Strategy.String(),
)

View File

@@ -71,12 +71,12 @@ func TestClusterSafety_MultipleCursorWorkers(t *testing.T) {
INSERT INTO operation (
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash, op_hash, sign,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
trustlog_status, timestamp, created_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, NOW())
`, opID, "cluster-tester", fmt.Sprintf("cluster/test/%d", i), "cluster-producer",
"req-hash", "resp-hash", "op-hash", "signature",
"DOIP", "CREATE", "cluster-test", "cluster-repo", "NOT_TRUSTLOGGED", time.Now())
"DOIP", 100, "cluster-test", "cluster-repo", "NOT_TRUSTLOGGED", time.Now())
if err != nil {
t.Fatalf("Failed to create test data: %v", err)
@@ -275,11 +275,11 @@ func TestClusterSafety_ConcurrentStatusUpdate(t *testing.T) {
_, err = db.Exec(`
INSERT INTO operation (
op_id, op_actor, doid, producer_id,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
trustlog_status, created_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, NOW())
`, "concurrent-test", "tester", "test/concurrent", "producer",
"DOIP", "CREATE", "test", "repo", "NOT_TRUSTLOGGED")
"DOIP", 100, "test", "repo", "NOT_TRUSTLOGGED")
require.NoError(t, err)
// 并发更新状态(模拟多个 worker 同时处理同一条记录)
@@ -334,3 +334,5 @@ func TestClusterSafety_ConcurrentStatusUpdate(t *testing.T) {
t.Log("✅ CAS mechanism working correctly - Only one update succeeded")
}

View File

@@ -52,3 +52,5 @@ func TestDBConfig_CustomValues(t *testing.T) {
}
}

View File

@@ -192,3 +192,5 @@ func TestSQLiteDDLUsesTEXT(t *testing.T) {
}
}

View File

@@ -152,15 +152,15 @@ func TestCursorInitialization(t *testing.T) {
createdAt := baseTime.Add(time.Duration(i) * time.Minute)
_, err := db.Exec(`
INSERT INTO operation (
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash, op_hash, sign,
op_source, op_type, do_prefix, do_repository,
trustlog_status, created_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
`, opID, "tester", fmt.Sprintf("test/%d", i), "producer",
"req-hash", "resp-hash", "op-hash", "signature",
"DOIP", "CREATE", "test", "repo", "NOT_TRUSTLOGGED", createdAt)
require.NoError(t, err)
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash, op_hash, sign,
op_source, op_code, do_prefix, do_repository,
trustlog_status, created_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
`, opID, "tester", fmt.Sprintf("test/%d", i), "producer",
"req-hash", "resp-hash", "op-hash", "signature",
"DOIP", 100, "test", "repo", "NOT_TRUSTLOGGED", createdAt)
require.NoError(t, err)
}
t.Logf("✅ Created 5 historical records starting from %v", baseTime)
@@ -286,3 +286,5 @@ func TestCursorInitialization(t *testing.T) {
t.Log("✅ Cursor initialization verification PASSED")
t.Log(strings.Repeat("=", 60))
}

View File

@@ -21,7 +21,7 @@ type OperationRecord struct {
OpHash string
Sign string
OpSource string
OpType string
OpCode int32
DOPrefix string
DORepository string
ClientIP *string
@@ -40,7 +40,7 @@ func (r *OperationRecord) ToModel() *model.Operation {
RequestBodyHash: &r.RequestBodyHash,
ResponseBodyHash: &r.ResponseBodyHash,
OpSource: model.Source(r.OpSource),
OpType: r.OpType,
OpCode: model.OpCode(r.OpCode),
DoPrefix: r.DOPrefix,
DoRepository: r.DORepository,
ClientIP: r.ClientIP,
@@ -339,7 +339,7 @@ func (w *CursorWorker) findNewOperationsWithLock(ctx context.Context, tx *sql.Tx
query := `
SELECT op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash, op_hash, sign,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
client_ip, server_ip, trustlog_status, created_at
FROM operation
WHERE trustlog_status = $1
@@ -365,7 +365,7 @@ func (w *CursorWorker) findNewOperationsWithLock(ctx context.Context, tx *sql.Tx
err := rows.Scan(
&op.OpID, &op.OpActor, &op.DOID, &op.ProducerID,
&op.RequestBodyHash, &op.ResponseBodyHash, &op.OpHash, &op.Sign,
&op.OpSource, &op.OpType, &op.DOPrefix, &op.DORepository,
&op.OpSource, &op.OpCode, &op.DOPrefix, &op.DORepository,
&clientIP, &serverIP, &op.TrustlogStatus, &createdAt,
)
if err != nil {
@@ -404,7 +404,7 @@ func (w *CursorWorker) findNewOperations(ctx context.Context, cursor string) ([]
rows, err := db.QueryContext(ctx, `
SELECT op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash, op_hash, sign,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
client_ip, server_ip, trustlog_status, created_at
FROM operation
WHERE trustlog_status = $1
@@ -426,7 +426,7 @@ func (w *CursorWorker) findNewOperations(ctx context.Context, cursor string) ([]
err := rows.Scan(
&op.OpID, &op.OpActor, &op.DOID, &op.ProducerID,
&op.RequestBodyHash, &op.ResponseBodyHash, &op.OpHash, &op.Sign,
&op.OpSource, &op.OpType, &op.DOPrefix, &op.DORepository,
&op.OpSource, &op.OpCode, &op.DOPrefix, &op.DORepository,
&clientIP, &serverIP, &op.TrustlogStatus, &createdAt,
)
if err != nil {

View File

@@ -204,12 +204,12 @@ func TestE2E_DBAndTrustlog_FullWorkflow(t *testing.T) {
// 注意PersistenceClient 主要用于写入,查询需要直接使用 repository
var retrievedOp model.Operation
err = db.QueryRowContext(ctx, `
SELECT op_id, op_source, op_type, do_prefix
SELECT op_id, op_source, op_code, do_prefix
FROM operation WHERE op_id = $1
`, operations[0].OpID).Scan(
&retrievedOp.OpID,
&retrievedOp.OpSource,
&retrievedOp.OpType,
&retrievedOp.OpCode,
&retrievedOp.DoPrefix,
)
require.NoError(t, err)
@@ -726,7 +726,7 @@ func createE2ETestOperations(count int) []*model.Operation {
OpID: fmt.Sprintf("e2e-op-%d-%d", timestamp, i),
Timestamp: time.Now(),
OpSource: model.OpSourceDOIP,
OpType: string(model.OpTypeCreate),
OpCode: model.OpCodeCreateID,
DoPrefix: "e2e-test",
DoRepository: "e2e-repo",
Doid: fmt.Sprintf("e2e/test/%d", i),

View File

@@ -44,7 +44,7 @@ func Example_dbOnly() {
EnvelopeConfig: envelopeConfig,
DBConfig: persistence.DefaultDBConfig(
"postgres",
"postgres://user:pass@localhost:5432/trustlog?sslmode=disable",
"postgres://postgres:postgres@localhost:5432/trustlog?sslmode=disable",
),
PersistenceConfig: persistence.DefaultPersistenceConfig(persistence.StrategyDBOnly),
EnableRetryWorker: false, // 仅落库不需要重试
@@ -57,7 +57,7 @@ func Example_dbOnly() {
// 5. 构造 Operation包含 IP 信息)
op, err := model.NewFullOperation(
model.OpSourceDOIP,
string(model.OpTypeCreate),
model.OpCodeCreateID,
"10.1000",
"my-repo",
"10.1000/my-repo/doc001",
@@ -120,7 +120,7 @@ func Example_dbAndTrustlog() {
EnvelopeConfig: envelopeConfig,
DBConfig: persistence.DefaultDBConfig(
"postgres",
"postgres://user:pass@localhost:5432/trustlog?sslmode=disable",
"postgres://postgres:postgres@localhost:5432/trustlog?sslmode=disable",
),
PersistenceConfig: persistence.PersistenceConfig{
Strategy: persistence.StrategyDBAndTrustlog,
@@ -139,7 +139,7 @@ func Example_dbAndTrustlog() {
// 5. 构造 Operation
op, err := model.NewFullOperation(
model.OpSourceDOIP,
string(model.OpTypeCreate),
model.OpCodeCreateID,
"10.1000",
"my-repo",
"10.1000/my-repo/doc002",
@@ -199,7 +199,7 @@ func Example_trustlogOnly() {
EnvelopeConfig: envelopeConfig,
DBConfig: persistence.DefaultDBConfig(
"postgres",
"postgres://user:pass@localhost:5432/trustlog?sslmode=disable",
"postgres://postgres:postgres@localhost:5432/trustlog?sslmode=disable",
),
PersistenceConfig: persistence.DefaultPersistenceConfig(persistence.StrategyTrustlogOnly),
EnableRetryWorker: false, // 仅存证不需要重试工作器
@@ -212,7 +212,7 @@ func Example_trustlogOnly() {
// 5. 构造 Operation
op, err := model.NewFullOperation(
model.OpSourceDOIP,
string(model.OpTypeCreate),
model.OpCodeCreateID,
"10.1000",
"my-repo",
"10.1000/my-repo/doc003",
@@ -278,7 +278,7 @@ func Example_mysqlDatabase() {
// 5. 构造并发布 Operation
op, err := model.NewFullOperation(
model.OpSourceDOIP,
string(model.OpTypeCreate),
model.OpCodeCreateID,
"10.1000",
"my-repo",
"10.1000/my-repo/doc004",
@@ -349,7 +349,7 @@ func Example_sqliteDatabase() {
// 5. 构造并发布 Operation
op, err := model.NewFullOperation(
model.OpSourceDOIP,
string(model.OpTypeCreate),
model.OpCodeCreateID,
"10.1000",
"my-repo",
"10.1000/my-repo/doc005",
@@ -376,3 +376,6 @@ func Example_sqliteDatabase() {
fmt.Printf("Operation saved to SQLite: %s\n", op.OpID)
}

View File

@@ -365,3 +365,5 @@ func TestGetDialectDDL_UnknownDriver(t *testing.T) {
}
}

View File

@@ -141,11 +141,11 @@ func TestMinimalNullableIPFields(t *testing.T) {
// 测试1: 插入 NULL IP
_, err = db.ExecContext(ctx, `
INSERT INTO operation (
op_id, doid, producer_id, op_source, op_type,
op_id, doid, producer_id, op_source, op_code,
do_prefix, do_repository, trustlog_status, timestamp,
client_ip, server_ip
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, "test-001", "10.1000/repo/obj", "producer-001", "DOIP", "Create",
`, "test-001", "10.1000/repo/obj", "producer-001", "DOIP", 100,
"10.1000", "repo", "NOT_TRUSTLOGGED", time.Now(), nil, nil)
if err != nil {
@@ -173,11 +173,11 @@ func TestMinimalNullableIPFields(t *testing.T) {
// 测试2: 插入非 NULL IP
_, err = db.ExecContext(ctx, `
INSERT INTO operation (
op_id, doid, producer_id, op_source, op_type,
op_id, doid, producer_id, op_source, op_code,
do_prefix, do_repository, trustlog_status, timestamp,
client_ip, server_ip
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, "test-002", "10.1000/repo/obj2", "producer-001", "DOIP", "Create",
`, "test-002", "10.1000/repo/obj2", "producer-001", "DOIP", 100,
"10.1000", "repo", "NOT_TRUSTLOGGED", time.Now(),
"192.168.1.100", "10.0.0.50")
@@ -316,10 +316,10 @@ func TestMinimalOperationStatusFlow(t *testing.T) {
// 插入未存证记录
_, err = db.ExecContext(ctx, `
INSERT INTO operation (
op_id, doid, producer_id, op_source, op_type,
op_id, doid, producer_id, op_source, op_code,
do_prefix, do_repository, trustlog_status, timestamp
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, "test-001", "10.1000/repo/obj", "producer-001", "DOIP", "Create",
`, "test-001", "10.1000/repo/obj", "producer-001", "DOIP", 100,
"10.1000", "repo", "NOT_TRUSTLOGGED", time.Now())
if err != nil {
@@ -367,3 +367,5 @@ func TestMinimalOperationStatusFlow(t *testing.T) {
}
}

View File

@@ -400,3 +400,5 @@ func TestPostgreSQL_PersistenceManager(t *testing.T) {
t.Logf("✅ PostgreSQL PersistenceManager test passed")
}

View File

@@ -69,7 +69,7 @@ func TestPG_Query_Integration(t *testing.T) {
testOps := []struct {
opID string
opSource string
opType string
opCode int32
prefix string
doid string
repo string
@@ -80,23 +80,23 @@ func TestPG_Query_Integration(t *testing.T) {
status persistence.TrustlogStatus
time time.Time
}{
{"pg-query-test-001", "DOIP", "Create", "10.10000", "10.10000/test-repo/test-001", "test-repo", "user-1", "producer-1", strPtr("192.168.1.10"), strPtr("10.0.0.1"), persistence.StatusNotTrustlogged, baseTime},
{"pg-query-test-002", "DOIP", "Create", "10.10000", "10.10000/test-repo/test-002", "test-repo", "user-1", "producer-1", strPtr("192.168.1.10"), strPtr("10.0.0.1"), persistence.StatusTrustlogged, baseTime.Add(10 * time.Minute)},
{"pg-query-test-003", "DOIP", "Update", "10.10000", "10.10000/test-repo/test-003", "test-repo", "user-2", "producer-1", strPtr("192.168.1.20"), strPtr("10.0.0.1"), persistence.StatusNotTrustlogged, baseTime.Add(20 * time.Minute)},
{"pg-query-test-004", "DOIP", "Update", "10.10000", "10.10000/test-repo/test-004", "test-repo", "user-2", "producer-2", strPtr("192.168.1.20"), strPtr("10.0.0.2"), persistence.StatusTrustlogged, baseTime.Add(30 * time.Minute)},
{"pg-query-test-005", "DOIP", "Delete", "10.10000", "10.10000/test-repo/test-005", "test-repo", "user-3", "producer-2", nil, nil, persistence.StatusNotTrustlogged, baseTime.Add(40 * time.Minute)},
{"pg-query-test-006", "IRP", "OC_CREATE_HANDLE", "20.1000", "20.1000/test-repo/test-001", "test-repo", "user-1", "producer-3", strPtr("192.168.2.10"), strPtr("10.0.1.1"), persistence.StatusTrustlogged, baseTime.Add(50 * time.Minute)},
{"pg-query-test-007", "IRP", "OC_DELETE_HANDLE", "20.1000", "20.1000/test-repo/test-002", "test-repo", "user-2", "producer-3", strPtr("192.168.2.20"), strPtr("10.0.1.1"), persistence.StatusNotTrustlogged, baseTime.Add(60 * time.Minute)},
{"pg-query-test-008", "IRP", "OC_LOOKUP_HANDLE", "20.1000", "20.1000/test-repo/test-003", "test-repo", "user-3", "producer-4", nil, strPtr("10.0.1.2"), persistence.StatusTrustlogged, baseTime.Add(70 * time.Minute)},
{"pg-query-test-009", "DOIP", "Retrieve", "10.20000", "10.20000/test-repo/test-001", "test-repo", "user-1", "producer-1", strPtr("192.168.1.30"), nil, persistence.StatusNotTrustlogged, baseTime.Add(80 * time.Minute)},
{"pg-query-test-010", "DOIP", "Retrieve", "10.20000", "10.20000/test-repo/test-002", "test-repo", "user-2", "producer-2", strPtr("192.168.1.40"), strPtr("10.0.0.3"), persistence.StatusTrustlogged, baseTime.Add(90 * time.Minute)},
{"pg-query-test-001", "DOIP", 100, "10.10000", "10.10000/test-repo/test-001", "test-repo", "user-1", "producer-1", strPtr("192.168.1.10"), strPtr("10.0.0.1"), persistence.StatusNotTrustlogged, baseTime},
{"pg-query-test-002", "DOIP", 100, "10.10000", "10.10000/test-repo/test-002", "test-repo", "user-1", "producer-1", strPtr("192.168.1.10"), strPtr("10.0.0.1"), persistence.StatusTrustlogged, baseTime.Add(10 * time.Minute)},
{"pg-query-test-003", "DOIP", 104, "10.10000", "10.10000/test-repo/test-003", "test-repo", "user-2", "producer-1", strPtr("192.168.1.20"), strPtr("10.0.0.1"), persistence.StatusNotTrustlogged, baseTime.Add(20 * time.Minute)},
{"pg-query-test-004", "DOIP", 104, "10.10000", "10.10000/test-repo/test-004", "test-repo", "user-2", "producer-2", strPtr("192.168.1.20"), strPtr("10.0.0.2"), persistence.StatusTrustlogged, baseTime.Add(30 * time.Minute)},
{"pg-query-test-005", "DOIP", 101, "10.10000", "10.10000/test-repo/test-005", "test-repo", "user-3", "producer-2", nil, nil, persistence.StatusNotTrustlogged, baseTime.Add(40 * time.Minute)},
{"pg-query-test-006", "IRP", 100, "20.1000", "20.1000/test-repo/test-001", "test-repo", "user-1", "producer-3", strPtr("192.168.2.10"), strPtr("10.0.1.1"), persistence.StatusTrustlogged, baseTime.Add(50 * time.Minute)},
{"pg-query-test-007", "IRP", 101, "20.1000", "20.1000/test-repo/test-002", "test-repo", "user-2", "producer-3", strPtr("192.168.2.20"), strPtr("10.0.1.1"), persistence.StatusNotTrustlogged, baseTime.Add(60 * time.Minute)},
{"pg-query-test-008", "IRP", 1, "20.1000", "20.1000/test-repo/test-003", "test-repo", "user-3", "producer-4", nil, strPtr("10.0.1.2"), persistence.StatusTrustlogged, baseTime.Add(70 * time.Minute)},
{"pg-query-test-009", "DOIP", 1, "10.20000", "10.20000/test-repo/test-001", "test-repo", "user-1", "producer-1", strPtr("192.168.1.30"), nil, persistence.StatusNotTrustlogged, baseTime.Add(80 * time.Minute)},
{"pg-query-test-010", "DOIP", 1, "10.20000", "10.20000/test-repo/test-002", "test-repo", "user-2", "producer-2", strPtr("192.168.1.40"), strPtr("10.0.0.3"), persistence.StatusTrustlogged, baseTime.Add(90 * time.Minute)},
}
// 插入测试数据
for _, testOp := range testOps {
op, err := model.NewFullOperation(
model.Source(testOp.opSource),
testOp.opType,
model.OpCode(testOp.opCode),
testOp.prefix, // doPrefix
testOp.repo, // doRepository
testOp.doid, // doid
@@ -151,11 +151,11 @@ func TestPG_Query_Integration(t *testing.T) {
t.Logf("✅ DOIP records: %d", result.Total)
})
// 测试3: 按 OpType 筛选
t.Run("Filter by OpType", func(t *testing.T) {
opType := "Create"
// 测试3: 按 OpCode 筛选
t.Run("Filter by OpCode", func(t *testing.T) {
opCode := int32(100)
req := &persistence.OperationQueryRequest{
OpType: &opType,
OpCode: &opCode,
PageSize: 50,
PageNumber: 1,
}
@@ -165,7 +165,7 @@ func TestPG_Query_Integration(t *testing.T) {
assert.GreaterOrEqual(t, result.Total, int64(2)) // 2条Create记录
for _, op := range result.Operations {
assert.Equal(t, "Create", op.OpType)
assert.Equal(t, model.OpCodeCreateID, op.OpCode)
}
t.Logf("✅ Create records: %d", result.Total)
})
@@ -442,11 +442,11 @@ func TestPG_Query_Integration(t *testing.T) {
// 测试16: 复杂组合查询(多条件)
t.Run("Complex combined query", func(t *testing.T) {
opSource := "DOIP"
opType := "Update"
opCode := int32(104) // ModifyElement
status := persistence.StatusTrustlogged
req := &persistence.OperationQueryRequest{
OpSource: &opSource,
OpType: &opType,
OpCode: &opCode,
TrustlogStatus: &status,
PageSize: 50,
PageNumber: 1,
@@ -460,7 +460,7 @@ func TestPG_Query_Integration(t *testing.T) {
for i, op := range result.Operations {
assert.Equal(t, "DOIP", string(op.OpSource))
assert.Equal(t, "Update", op.OpType)
assert.Equal(t, model.OpCodeModifyElement, op.OpCode) // 104
assert.Equal(t, persistence.StatusTrustlogged, result.Statuses[i])
}
t.Logf("✅ Complex query records: %d", result.Total)
@@ -534,7 +534,7 @@ func TestPG_PersistenceClient_Query_Integration(t *testing.T) {
for i := 0; i < 5; i++ {
op, err := model.NewFullOperation(
model.OpSourceDOIP,
string(model.OpTypeCreate),
model.OpCodeCreateID,
"10.10000", // doPrefix
"client-repo", // doRepository
fmt.Sprintf("10.10000/client-repo/test-%d", i), // doid
@@ -613,3 +613,5 @@ func TestPG_PersistenceClient_Query_Integration(t *testing.T) {
func strPtr(s string) *string {
return &s
}

View File

@@ -358,3 +358,5 @@ func TestPulsar_DifferentTopics(t *testing.T) {
t.Logf("✅ Pulsar different topics test passed")
}

View File

@@ -26,20 +26,20 @@ func TestOperationRepository_Query(t *testing.T) {
testOps := []struct {
opID string
opSource string
opType string
opCode int32
status TrustlogStatus
time time.Time
}{
{"op-001", "DOIP", "Create", StatusNotTrustlogged, now.Add(-3 * time.Hour)},
{"op-002", "DOIP", "Update", StatusTrustlogged, now.Add(-2 * time.Hour)},
{"op-003", "IRP", "Create", StatusNotTrustlogged, now.Add(-1 * time.Hour)},
{"op-004", "IRP", "Delete", StatusTrustlogged, now},
{"op-001", "DOIP", 100, StatusNotTrustlogged, now.Add(-3 * time.Hour)},
{"op-002", "DOIP", 104, StatusTrustlogged, now.Add(-2 * time.Hour)},
{"op-003", "IRP", 100, StatusNotTrustlogged, now.Add(-1 * time.Hour)},
{"op-004", "IRP", 101, StatusTrustlogged, now},
}
for _, testOp := range testOps {
op := createTestOperation(t, testOp.opID)
op.OpSource = model.Source(testOp.opSource)
op.OpType = testOp.opType
op.OpCode = model.OpCode(testOp.opCode)
op.Timestamp = testOp.time
err := repo.Save(ctx, op, testOp.status)
@@ -78,10 +78,10 @@ func TestOperationRepository_Query(t *testing.T) {
}
})
t.Run("Query by OpType", func(t *testing.T) {
opType := "Create"
t.Run("Query by OpCode", func(t *testing.T) {
opCode := int32(100)
req := &OperationQueryRequest{
OpType: &opType,
OpCode: &opCode,
PageSize: 10,
PageNumber: 1,
}

View File

@@ -16,8 +16,8 @@ type OperationQueryRequest struct {
OpID *string
// OpSource 操作来源(精确匹配)
OpSource *string
// OpType 操作类型(精确匹配)
OpType *string
// OpCode 操作代码(精确匹配int32
OpCode *int32
// Doid 数字对象标识符(支持 LIKE 模糊查询)
Doid *string
// ProducerID 生产者ID精确匹配
@@ -197,7 +197,7 @@ func (r *operationRepository) SaveTx(ctx context.Context, tx *sql.Tx, op *model.
INSERT INTO operation (
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
client_ip, server_ip, trustlog_status, timestamp
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`)
@@ -224,7 +224,7 @@ func (r *operationRepository) SaveTx(ctx context.Context, tx *sql.Tx, op *model.
reqHash,
respHash,
string(op.OpSource),
string(op.OpType),
int32(op.OpCode),
op.DoPrefix,
op.DoRepository,
clientIP,
@@ -290,7 +290,7 @@ func (r *operationRepository) FindByID(ctx context.Context, opID string) (*model
SELECT
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
client_ip, server_ip, trustlog_status, timestamp
FROM operation
WHERE op_id = ?
@@ -308,7 +308,7 @@ func (r *operationRepository) FindByID(ctx context.Context, opID string) (*model
&reqHash,
&respHash,
&op.OpSource,
&op.OpType,
&op.OpCode,
&op.DoPrefix,
&op.DoRepository,
&clientIP,
@@ -353,7 +353,7 @@ func (r *operationRepository) FindUntrustloggedWithLock(ctx context.Context, tx
SELECT
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
client_ip, server_ip, timestamp
FROM operation
WHERE trustlog_status = ?
@@ -392,7 +392,7 @@ func (r *operationRepository) FindUntrustloggedWithLock(ctx context.Context, tx
&reqHash,
&respHash,
&op.OpSource,
&op.OpType,
&op.OpCode,
&op.DoPrefix,
&op.DoRepository,
&clientIP,
@@ -488,7 +488,7 @@ func (r *operationRepository) FindUntrustlogged(ctx context.Context, limit int)
SELECT
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
client_ip, server_ip, timestamp
FROM operation
WHERE trustlog_status = ?
@@ -518,7 +518,7 @@ func (r *operationRepository) FindUntrustlogged(ctx context.Context, limit int)
&reqHash,
&respHash,
&op.OpSource,
&op.OpType,
&op.OpCode,
&op.DoPrefix,
&op.DoRepository,
&clientIP,
@@ -607,9 +607,9 @@ func (r *operationRepository) Query(ctx context.Context, req *OperationQueryRequ
args = append(args, *req.OpSource)
argIndex++
}
if req.OpType != nil && *req.OpType != "" {
conditions = append(conditions, fmt.Sprintf("op_type = $%d", argIndex))
args = append(args, *req.OpType)
if req.OpCode != nil {
conditions = append(conditions, fmt.Sprintf("op_code = $%d", argIndex))
args = append(args, *req.OpCode)
argIndex++
}
if req.Doid != nil && *req.Doid != "" {
@@ -683,7 +683,7 @@ func (r *operationRepository) Query(ctx context.Context, req *OperationQueryRequ
SELECT
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
client_ip, server_ip, trustlog_status, timestamp, created_at
FROM operation
%s
@@ -713,7 +713,7 @@ func (r *operationRepository) Query(ctx context.Context, req *OperationQueryRequ
err := rows.Scan(
&op.OpID, &op.OpActor, &op.Doid, &op.ProducerID,
&reqHash, &respHash,
&op.OpSource, &op.OpType, &op.DoPrefix, &op.DoRepository,
&op.OpSource, &op.OpCode, &op.DoPrefix, &op.DoRepository,
&clientIP, &serverIP, &statusStr, &op.Timestamp, &createdAt,
)
if err != nil {
@@ -779,9 +779,9 @@ func (r *operationRepository) Count(ctx context.Context, req *OperationQueryRequ
args = append(args, *req.OpSource)
argIndex++
}
if req.OpType != nil && *req.OpType != "" {
conditions = append(conditions, fmt.Sprintf("op_type = $%d", argIndex))
args = append(args, *req.OpType)
if req.OpCode != nil {
conditions = append(conditions, fmt.Sprintf("op_code = $%d", argIndex))
args = append(args, *req.OpCode)
argIndex++
}
if req.Doid != nil && *req.Doid != "" {

View File

@@ -44,7 +44,7 @@ func setupTestDB(t *testing.T) *sql.DB {
func createTestOperation(t *testing.T, opID string) *model.Operation {
op, err := model.NewFullOperation(
model.OpSourceDOIP,
string(model.OpTypeCreate),
model.OpCodeCreateID,
"10.1000",
"test-repo",
"10.1000/test-repo/"+opID,
@@ -389,3 +389,5 @@ func TestRetryRepository_DeleteRetry(t *testing.T) {
}
}

View File

@@ -95,3 +95,5 @@ func TestRetryWorker_CalculateNextRetry(t *testing.T) {
}
}

View File

@@ -35,7 +35,7 @@ CREATE TABLE IF NOT EXISTS operation (
response_body_hash VARCHAR(128),
sign VARCHAR(512),
op_source VARCHAR(10),
op_type VARCHAR(30),
op_code INTEGER,
do_prefix VARCHAR(128),
do_repository VARCHAR(64),
client_ip VARCHAR(32),
@@ -106,7 +106,7 @@ CREATE TABLE IF NOT EXISTS operation (
response_body_hash VARCHAR(128),
sign VARCHAR(512),
op_source VARCHAR(10),
op_type VARCHAR(30),
op_code INTEGER,
do_prefix VARCHAR(128),
do_repository VARCHAR(64),
client_ip VARCHAR(32),
@@ -164,7 +164,7 @@ CREATE TABLE IF NOT EXISTS operation (
response_body_hash VARCHAR(128),
sign VARCHAR(512),
op_source VARCHAR(10),
op_type VARCHAR(30),
op_code INT,
do_prefix VARCHAR(128),
do_repository VARCHAR(64),
client_ip VARCHAR(32),
@@ -219,7 +219,7 @@ CREATE TABLE IF NOT EXISTS operation (
response_body_hash TEXT,
sign TEXT,
op_source TEXT,
op_type TEXT,
op_code INTEGER,
do_prefix TEXT,
do_repository TEXT,
client_ip TEXT,

View File

@@ -134,7 +134,7 @@ func TestOperationTableDDL(t *testing.T) {
"response_body_hash",
"sign",
"op_source",
"op_type",
"op_code",
"do_prefix",
"do_repository",
"client_ip",
@@ -181,3 +181,5 @@ func TestRetryTableDDL(t *testing.T) {
}
}

View File

@@ -14,7 +14,7 @@ CREATE TABLE IF NOT EXISTS operation (
response_body_hash VARCHAR(128),
sign VARCHAR(512),
op_source VARCHAR(10),
op_type VARCHAR(30),
op_code INT COMMENT '操作代码int32',
do_prefix VARCHAR(128),
do_repository VARCHAR(64),
client_ip VARCHAR(32) COMMENT '客户端IP可空仅落库不存证',

View File

@@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS operation (
op_hash VARCHAR(128), -- 操作哈希
sign VARCHAR(512),
op_source VARCHAR(10),
op_type VARCHAR(30),
op_code INTEGER, -- 操作代码int32
do_prefix VARCHAR(128),
do_repository VARCHAR(64),
client_ip VARCHAR(32), -- 客户端IP可空仅落库

View File

@@ -14,7 +14,7 @@ CREATE TABLE IF NOT EXISTS operation (
response_body_hash TEXT,
sign TEXT,
op_source TEXT,
op_type TEXT,
op_code INTEGER, -- 操作代码int32
do_prefix TEXT,
do_repository TEXT,
client_ip TEXT, -- 客户端IP可空仅落库不存证

View File

@@ -9,7 +9,7 @@
INSERT INTO operation (
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
client_ip, server_ip, trustlog_status, timestamp
) VALUES (
'test-op-001',
@@ -32,7 +32,7 @@ INSERT INTO operation (
INSERT INTO operation (
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
client_ip, server_ip, trustlog_status, timestamp
) VALUES (
'test-op-002',
@@ -55,7 +55,7 @@ INSERT INTO operation (
INSERT INTO operation (
op_id, op_actor, doid, producer_id,
request_body_hash, response_body_hash,
op_source, op_type, do_prefix, do_repository,
op_source, op_code, do_prefix, do_repository,
client_ip, server_ip, trustlog_status, timestamp
) VALUES (
'test-op-003',
@@ -111,7 +111,7 @@ INSERT INTO trustlog_retry (
-- 查询所有操作记录
SELECT
op_id,
op_type,
op_code,
client_ip,
server_ip,
trustlog_status,

View File

@@ -153,11 +153,11 @@ func TestStandaloneIPFields(t *testing.T) {
// 测试 NULL IP
_, err = db.ExecContext(ctx, `
INSERT INTO operation (
op_id, doid, producer_id, op_source, op_type,
op_id, doid, producer_id, op_source, op_code,
do_prefix, do_repository, trustlog_status, timestamp,
client_ip, server_ip
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, "test-001", "10.1000/repo/obj", "producer-001", "DOIP", "Create",
`, "test-001", "10.1000/repo/obj", "producer-001", "DOIP", 100,
"10.1000", "repo", "NOT_TRUSTLOGGED", time.Now(), nil, nil)
if err != nil {
@@ -184,11 +184,11 @@ func TestStandaloneIPFields(t *testing.T) {
// 测试非 NULL IP
_, err = db.ExecContext(ctx, `
INSERT INTO operation (
op_id, doid, producer_id, op_source, op_type,
op_id, doid, producer_id, op_source, op_code,
do_prefix, do_repository, trustlog_status, timestamp,
client_ip, server_ip
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, "test-002", "10.1000/repo/obj2", "producer-001", "DOIP", "Create",
`, "test-002", "10.1000/repo/obj2", "producer-001", "DOIP", 100,
"10.1000", "repo", "NOT_TRUSTLOGGED", time.Now(),
"192.168.1.100", "10.0.0.50")
@@ -230,10 +230,10 @@ func TestStandaloneStatusFlow(t *testing.T) {
// 插入未存证记录
_, err = db.ExecContext(ctx, `
INSERT INTO operation (
op_id, doid, producer_id, op_source, op_type,
op_id, doid, producer_id, op_source, op_code,
do_prefix, do_repository, trustlog_status, timestamp
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, "test-001", "10.1000/repo/obj", "producer-001", "DOIP", "Create",
`, "test-001", "10.1000/repo/obj", "producer-001", "DOIP", 100,
"10.1000", "repo", "NOT_TRUSTLOGGED", time.Now())
if err != nil {
@@ -307,3 +307,5 @@ func TestStandaloneCursorInit(t *testing.T) {
}
}

View File

@@ -84,3 +84,5 @@ func TestPersistenceConfig_CustomValues(t *testing.T) {
}
}

View File

@@ -325,10 +325,10 @@ func TestNullableFields(t *testing.T) {
ctx := context.Background()
_, err = db.ExecContext(ctx, `
INSERT INTO operation (
op_id, doid, producer_id, op_source, op_type,
op_id, doid, producer_id, op_source, op_code,
do_prefix, do_repository, trustlog_status, timestamp
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, "test-001", "10.1000/repo/obj", "producer-001", "DOIP", "Create",
`, "test-001", "10.1000/repo/obj", "producer-001", "DOIP", 100,
"10.1000", "repo", "NOT_TRUSTLOGGED", time.Now())
if err != nil {
@@ -360,3 +360,5 @@ func assertEqual(t *testing.T, got, want interface{}) {
}
}

View File

@@ -76,7 +76,7 @@ type ListOperationsRequest struct {
// 可选过滤条件
Timestamp *time.Time // 操作时间戳
OpSource model.Source // 操作来源
OpType string // 操作类型
OpCode model.OpCode // 操作代码int32
DoPrefix string // 数据前缀
DoRepository string // 数据仓库
}
@@ -99,7 +99,7 @@ func (c *Client) ListOperations(ctx context.Context, req ListOperationsRequest)
pbReq := &pb.ListOperationReq{
PageSize: req.PageSize,
OpSource: string(req.OpSource),
OpType: req.OpType,
OpCode: int32(req.OpCode),
DoPrefix: req.DoPrefix,
DoRepository: req.DoRepository,
}
@@ -137,10 +137,10 @@ func (c *Client) ListOperations(ctx context.Context, req ListOperationsRequest)
// ValidationRequest 取证验证请求参数.
type ValidationRequest struct {
Time time.Time // 操作时间戳
OpID string // 操作唯一标识符
OpType string // 操作类型
DoRepository string // 数据仓库标识
Time time.Time // 操作时间戳
OpID string // 操作唯一标识符
OpCode model.OpCode // 操作代码int32
DoRepository string // 数据仓库标识
}
// ValidateOperation 执行操作取证验证,返回流式结果通道
@@ -159,7 +159,7 @@ func (c *Client) ValidateOperation(ctx context.Context, req ValidationRequest) (
pbReq := &pb.ValidationReq{
Time: timestamppb.New(req.Time),
OpId: req.OpID,
OpType: req.OpType,
OpCode: int32(req.OpCode),
DoRepository: req.DoRepository,
}

View File

@@ -59,14 +59,14 @@ func TestListOperations_ErrorHandling(t *testing.T) {
req := queryclient.ListOperationsRequest{
PageSize: 10,
OpSource: "api",
OpType: "create",
OpCode: model.OpCodeCreateID,
DoPrefix: "test",
DoRepository: "repo",
}
assert.Equal(t, uint64(10), req.PageSize)
assert.Equal(t, model.Source("api"), req.OpSource)
assert.Equal(t, "create", req.OpType)
assert.Equal(t, model.OpCodeCreateID, req.OpCode)
})
}
@@ -74,12 +74,12 @@ func TestListOperations_ErrorHandling(t *testing.T) {
func TestValidationRequest_Construction(t *testing.T) {
req := queryclient.ValidationRequest{
OpID: "test-op",
OpType: "create",
OpCode: model.OpCodeCreateID,
DoRepository: "test-repo",
}
assert.Equal(t, "test-op", req.OpID)
assert.Equal(t, "create", req.OpType)
assert.Equal(t, model.OpCodeCreateID, req.OpCode)
assert.Equal(t, "test-repo", req.DoRepository)
}
@@ -257,11 +257,11 @@ func TestClient_MultipleCallsToClose(t *testing.T) {
func TestResponseConversion(t *testing.T) {
t.Run("operation response with nil timestamp", func(t *testing.T) {
pbOp := &pb.OperationData{
OpId: "test",
OpSource: "api",
OpType: "create",
// Timestamp: nil - 这应该导致转换失败
}
OpId: "test",
OpSource: "api",
OpCode: int32(model.OpCodeCreateID),
// Timestamp: nil - 这应该导致转换失败
}
// 验证会失败因为缺少必需字段
_, err := model.FromProtobuf(pbOp)
@@ -272,7 +272,7 @@ func TestResponseConversion(t *testing.T) {
pbOp := &pb.OperationData{
OpId: "test",
OpSource: "api",
OpType: "create",
OpCode: int32(model.OpCodeCreateID),
Timestamp: timestamppb.Now(),
}
@@ -395,3 +395,8 @@ func TestListRecordsResponse_Structure(t *testing.T) {
assert.Len(t, resp.Data, 0)
}

View File

@@ -39,7 +39,7 @@ func (s *mockOperationServer) ListOperations(
OpId: "op-1",
Timestamp: timestamppb.Now(),
OpSource: "test",
OpType: "create",
OpCode: int32(model.OpCodeCreateID),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -50,7 +50,7 @@ func (s *mockOperationServer) ListOperations(
OpId: "op-2",
Timestamp: timestamppb.Now(),
OpSource: "test",
OpType: "update",
OpCode: int32(model.OpCodeCreateID),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/456",
@@ -81,7 +81,7 @@ func (s *mockOperationServer) ValidateOperation(
OpId: req.GetOpId(),
Timestamp: req.GetTime(),
OpSource: "test",
OpType: req.GetOpType(),
OpCode: req.GetOpCode(),
DoPrefix: "test",
DoRepository: req.GetDoRepository(),
Doid: "test/repo/123",
@@ -312,14 +312,14 @@ func TestListOperationsRequest(t *testing.T) {
PreTime: now,
Timestamp: &now,
OpSource: model.Source("test"),
OpType: "create",
OpCode: model.OpCodeCreateID,
}
assert.Equal(t, uint64(10), req.PageSize)
assert.Equal(t, now, req.PreTime)
assert.NotNil(t, req.Timestamp)
assert.Equal(t, "test", string(req.OpSource))
assert.Equal(t, "create", string(req.OpType))
assert.Equal(t, model.OpCodeCreateID, req.OpCode)
}
func TestValidationRequest(t *testing.T) {
@@ -328,13 +328,13 @@ func TestValidationRequest(t *testing.T) {
req := queryclient.ValidationRequest{
Time: now,
OpID: "op-123",
OpType: "create",
OpCode: model.OpCodeCreateID,
DoRepository: "repo",
}
assert.Equal(t, now, req.Time)
assert.Equal(t, "op-123", req.OpID)
assert.Equal(t, "create", req.OpType)
assert.Equal(t, model.OpCodeCreateID, req.OpCode)
assert.Equal(t, "repo", req.DoRepository)
}
@@ -420,7 +420,7 @@ func TestIntegration_ValidateOperation(t *testing.T) { //nolint:dupl // 测试
resultChan, err := client.ValidateOperation(ctx, queryclient.ValidationRequest{
Time: time.Now(),
OpID: "op-test",
OpType: "create",
OpCode: model.OpCodeCreateID,
DoRepository: "repo",
})
@@ -462,7 +462,7 @@ func TestIntegration_ValidateOperationSync(t *testing.T) {
queryclient.ValidationRequest{
Time: time.Now(),
OpID: "op-test",
OpType: "create",
OpCode: model.OpCodeCreateID,
DoRepository: "repo",
},
func(r *model.ValidationResult) {
@@ -625,3 +625,8 @@ func TestClient_Close(t *testing.T) {
err = client.Close()
require.NoError(t, err)
}