diff --git a/middleware/auth.go b/middleware/auth.go index be02309..4b339cf 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -34,7 +34,7 @@ func UnaryAuth(config *config.Config) grpc.UnaryServerInterceptor { var signature string setRecordReq, ok := req.(*proto.SetRecordRequest) if ok { - toVerify = fmt.Sprintf("%v-%v-%x-%v", setRecordReq.Record.Id, setRecordReq.Record.Version, setRecordReq.Record.Data, setRecordReq.RequestTime) + toVerify = fmt.Sprintf("%x-%v-%v", setRecordReq.RecordData, setRecordReq.RecordVersion, setRecordReq.RequestTime) signature = setRecordReq.Signature } diff --git a/proto/sync.pb.go b/proto/sync.pb.go index 46fb89b..1a149d6 100644 --- a/proto/sync.pb.go +++ b/proto/sync.pb.go @@ -20,101 +20,6 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -type SetRecordStatus int32 - -const ( - SetRecordStatus_SUCCESS SetRecordStatus = 0 - SetRecordStatus_CONFLICT SetRecordStatus = 1 -) - -// Enum value maps for SetRecordStatus. -var ( - SetRecordStatus_name = map[int32]string{ - 0: "SUCCESS", - 1: "CONFLICT", - } - SetRecordStatus_value = map[string]int32{ - "SUCCESS": 0, - "CONFLICT": 1, - } -) - -func (x SetRecordStatus) Enum() *SetRecordStatus { - p := new(SetRecordStatus) - *p = x - return p -} - -func (x SetRecordStatus) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (SetRecordStatus) Descriptor() protoreflect.EnumDescriptor { - return file_sync_proto_enumTypes[0].Descriptor() -} - -func (SetRecordStatus) Type() protoreflect.EnumType { - return &file_sync_proto_enumTypes[0] -} - -func (x SetRecordStatus) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use SetRecordStatus.Descriptor instead. -func (SetRecordStatus) EnumDescriptor() ([]byte, []int) { - return file_sync_proto_rawDescGZIP(), []int{0} -} - -type ChangeType int32 - -const ( - ChangeType_ACK ChangeType = 0 - ChangeType_RECORD ChangeType = 1 - ChangeType_DISCONNECT ChangeType = 2 -) - -// Enum value maps for ChangeType. -var ( - ChangeType_name = map[int32]string{ - 0: "ACK", - 1: "RECORD", - 2: "DISCONNECT", - } - ChangeType_value = map[string]int32{ - "ACK": 0, - "RECORD": 1, - "DISCONNECT": 2, - } -) - -func (x ChangeType) Enum() *ChangeType { - p := new(ChangeType) - *p = x - return p -} - -func (x ChangeType) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (ChangeType) Descriptor() protoreflect.EnumDescriptor { - return file_sync_proto_enumTypes[1].Descriptor() -} - -func (ChangeType) Type() protoreflect.EnumType { - return &file_sync_proto_enumTypes[1] -} - -func (x ChangeType) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use ChangeType.Descriptor instead. -func (ChangeType) EnumDescriptor() ([]byte, []int) { - return file_sync_proto_rawDescGZIP(), []int{1} -} - type Record struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -183,9 +88,10 @@ type SetRecordRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Record *Record `protobuf:"bytes,1,opt,name=record,proto3" json:"record,omitempty"` - RequestTime uint32 `protobuf:"varint,2,opt,name=request_time,json=requestTime,proto3" json:"request_time,omitempty"` - Signature string `protobuf:"bytes,3,opt,name=signature,proto3" json:"signature,omitempty"` + RecordData []byte `protobuf:"bytes,1,opt,name=record_data,json=recordData,proto3" json:"record_data,omitempty"` + RecordVersion float32 `protobuf:"fixed32,2,opt,name=record_version,json=recordVersion,proto3" json:"record_version,omitempty"` + RequestTime uint32 `protobuf:"varint,3,opt,name=request_time,json=requestTime,proto3" json:"request_time,omitempty"` + Signature string `protobuf:"bytes,4,opt,name=signature,proto3" json:"signature,omitempty"` } func (x *SetRecordRequest) Reset() { @@ -220,13 +126,20 @@ func (*SetRecordRequest) Descriptor() ([]byte, []int) { return file_sync_proto_rawDescGZIP(), []int{1} } -func (x *SetRecordRequest) GetRecord() *Record { +func (x *SetRecordRequest) GetRecordData() []byte { if x != nil { - return x.Record + return x.RecordData } return nil } +func (x *SetRecordRequest) GetRecordVersion() float32 { + if x != nil { + return x.RecordVersion + } + return 0 +} + func (x *SetRecordRequest) GetRequestTime() uint32 { if x != nil { return x.RequestTime @@ -246,8 +159,7 @@ type SetRecordReply struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Status SetRecordStatus `protobuf:"varint,1,opt,name=status,proto3,enum=sync.SetRecordStatus" json:"status,omitempty"` - NewId int64 `protobuf:"varint,2,opt,name=new_id,json=newId,proto3" json:"new_id,omitempty"` + NewId int64 `protobuf:"varint,2,opt,name=new_id,json=newId,proto3" json:"new_id,omitempty"` } func (x *SetRecordReply) Reset() { @@ -282,13 +194,6 @@ func (*SetRecordReply) Descriptor() ([]byte, []int) { return file_sync_proto_rawDescGZIP(), []int{2} } -func (x *SetRecordReply) GetStatus() SetRecordStatus { - if x != nil { - return x.Status - } - return SetRecordStatus_SUCCESS -} - func (x *SetRecordReply) GetNewId() int64 { if x != nil { return x.NewId @@ -461,61 +366,6 @@ func (x *ListenChangesRequest) GetSignature() string { return "" } -type Change struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Type ChangeType `protobuf:"varint,1,opt,name=type,proto3,enum=sync.ChangeType" json:"type,omitempty"` - Record *Record `protobuf:"bytes,2,opt,name=record,proto3,oneof" json:"record,omitempty"` -} - -func (x *Change) Reset() { - *x = Change{} - if protoimpl.UnsafeEnabled { - mi := &file_sync_proto_msgTypes[6] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *Change) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Change) ProtoMessage() {} - -func (x *Change) ProtoReflect() protoreflect.Message { - mi := &file_sync_proto_msgTypes[6] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use Change.ProtoReflect.Descriptor instead. -func (*Change) Descriptor() ([]byte, []int) { - return file_sync_proto_rawDescGZIP(), []int{6} -} - -func (x *Change) GetType() ChangeType { - if x != nil { - return x.Type - } - return ChangeType_ACK -} - -func (x *Change) GetRecord() *Record { - if x != nil { - return x.Record - } - return nil -} - var File_sync_proto protoreflect.FileDescriptor var file_sync_proto_rawDesc = []byte{ @@ -524,64 +374,51 @@ var file_sync_proto_rawDesc = []byte{ 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x02, 0x52, 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x79, 0x0a, 0x10, 0x53, 0x65, - 0x74, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x24, - 0x0a, 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, - 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, 0x06, 0x72, 0x65, - 0x63, 0x6f, 0x72, 0x64, 0x12, 0x21, 0x0a, 0x0c, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x5f, - 0x74, 0x69, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0b, 0x72, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, - 0x74, 0x75, 0x72, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, - 0x61, 0x74, 0x75, 0x72, 0x65, 0x22, 0x56, 0x0a, 0x0e, 0x53, 0x65, 0x74, 0x52, 0x65, 0x63, 0x6f, - 0x72, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x2d, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, - 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x15, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x53, - 0x65, 0x74, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x06, - 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x15, 0x0a, 0x06, 0x6e, 0x65, 0x77, 0x5f, 0x69, 0x64, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x6e, 0x65, 0x77, 0x49, 0x64, 0x22, 0x6e, 0x0a, - 0x12, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x72, 0x6f, 0x6d, 0x5f, 0x69, 0x64, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x66, 0x72, 0x6f, 0x6d, 0x49, 0x64, 0x12, 0x21, 0x0a, 0x0c, - 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x0d, 0x52, 0x0b, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x12, - 0x1c, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x03, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x22, 0x3a, 0x0a, - 0x10, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x52, 0x65, 0x70, 0x6c, - 0x79, 0x12, 0x26, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, - 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, - 0x52, 0x07, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x22, 0x57, 0x0a, 0x14, 0x4c, 0x69, 0x73, - 0x74, 0x65, 0x6e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x12, 0x21, 0x0a, 0x0c, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x5f, 0x74, 0x69, 0x6d, - 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0b, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x54, 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, - 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, - 0x72, 0x65, 0x22, 0x64, 0x0a, 0x06, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x12, 0x24, 0x0a, 0x04, - 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x10, 0x2e, 0x73, 0x79, 0x6e, - 0x63, 0x2e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, - 0x70, 0x65, 0x12, 0x29, 0x0a, 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, - 0x48, 0x00, 0x52, 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x88, 0x01, 0x01, 0x42, 0x09, 0x0a, - 0x07, 0x5f, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x2a, 0x2c, 0x0a, 0x0f, 0x53, 0x65, 0x74, 0x52, - 0x65, 0x63, 0x6f, 0x72, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x0b, 0x0a, 0x07, 0x53, - 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x43, 0x4f, 0x4e, 0x46, - 0x4c, 0x49, 0x43, 0x54, 0x10, 0x01, 0x2a, 0x31, 0x0a, 0x0a, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, - 0x54, 0x79, 0x70, 0x65, 0x12, 0x07, 0x0a, 0x03, 0x41, 0x43, 0x4b, 0x10, 0x00, 0x12, 0x0a, 0x0a, - 0x06, 0x52, 0x45, 0x43, 0x4f, 0x52, 0x44, 0x10, 0x01, 0x12, 0x0e, 0x0a, 0x0a, 0x44, 0x49, 0x53, - 0x43, 0x4f, 0x4e, 0x4e, 0x45, 0x43, 0x54, 0x10, 0x02, 0x32, 0xc7, 0x01, 0x0a, 0x06, 0x53, 0x79, - 0x6e, 0x63, 0x65, 0x72, 0x12, 0x3b, 0x0a, 0x09, 0x53, 0x65, 0x74, 0x52, 0x65, 0x63, 0x6f, 0x72, - 0x64, 0x12, 0x16, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x53, 0x65, 0x74, 0x52, 0x65, 0x63, 0x6f, - 0x72, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x73, 0x79, 0x6e, 0x63, - 0x2e, 0x53, 0x65, 0x74, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, - 0x00, 0x12, 0x41, 0x0a, 0x0b, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, - 0x12, 0x18, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x68, 0x61, 0x6e, - 0x67, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x73, 0x79, 0x6e, - 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x52, 0x65, 0x70, - 0x6c, 0x79, 0x22, 0x00, 0x12, 0x3d, 0x0a, 0x0d, 0x4c, 0x69, 0x73, 0x74, 0x65, 0x6e, 0x43, 0x68, - 0x61, 0x6e, 0x67, 0x65, 0x73, 0x12, 0x1a, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x4c, 0x69, 0x73, - 0x74, 0x65, 0x6e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x1a, 0x0c, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x22, - 0x00, 0x30, 0x01, 0x42, 0x22, 0x5a, 0x20, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, - 0x6d, 0x2f, 0x62, 0x72, 0x65, 0x65, 0x7a, 0x2f, 0x64, 0x61, 0x74, 0x61, 0x2d, 0x73, 0x79, 0x6e, - 0x63, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x9b, 0x01, 0x0a, 0x10, 0x53, + 0x65, 0x74, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, + 0x1f, 0x0a, 0x0b, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0a, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x44, 0x61, 0x74, 0x61, + 0x12, 0x25, 0x0a, 0x0e, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, + 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x02, 0x52, 0x0d, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, + 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x21, 0x0a, 0x0c, 0x72, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0b, 0x72, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x69, + 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, + 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x22, 0x27, 0x0a, 0x0e, 0x53, 0x65, 0x74, 0x52, + 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x15, 0x0a, 0x06, 0x6e, 0x65, + 0x77, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x6e, 0x65, 0x77, 0x49, + 0x64, 0x22, 0x6e, 0x0a, 0x12, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x72, 0x6f, 0x6d, 0x5f, + 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x66, 0x72, 0x6f, 0x6d, 0x49, 0x64, + 0x12, 0x21, 0x0a, 0x0c, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x5f, 0x74, 0x69, 0x6d, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0b, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, + 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, + 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, + 0x65, 0x22, 0x3a, 0x0a, 0x10, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, + 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x26, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, + 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x52, 0x65, + 0x63, 0x6f, 0x72, 0x64, 0x52, 0x07, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x22, 0x57, 0x0a, + 0x14, 0x4c, 0x69, 0x73, 0x74, 0x65, 0x6e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x21, 0x0a, 0x0c, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0b, 0x72, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, + 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x69, 0x67, + 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x32, 0xc7, 0x01, 0x0a, 0x06, 0x53, 0x79, 0x6e, 0x63, 0x65, + 0x72, 0x12, 0x3b, 0x0a, 0x09, 0x53, 0x65, 0x74, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x12, 0x16, + 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x53, 0x65, 0x74, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x53, 0x65, + 0x74, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x41, + 0x0a, 0x0b, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x12, 0x18, 0x2e, + 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x4c, + 0x69, 0x73, 0x74, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, + 0x00, 0x12, 0x3d, 0x0a, 0x0d, 0x4c, 0x69, 0x73, 0x74, 0x65, 0x6e, 0x43, 0x68, 0x61, 0x6e, 0x67, + 0x65, 0x73, 0x12, 0x1a, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x65, 0x6e, + 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0c, + 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x2e, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x22, 0x00, 0x30, 0x01, + 0x42, 0x22, 0x5a, 0x20, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x62, + 0x72, 0x65, 0x65, 0x7a, 0x2f, 0x64, 0x61, 0x74, 0x61, 0x2d, 0x73, 0x79, 0x6e, 0x63, 0x2f, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -596,36 +433,28 @@ func file_sync_proto_rawDescGZIP() []byte { return file_sync_proto_rawDescData } -var file_sync_proto_enumTypes = make([]protoimpl.EnumInfo, 2) -var file_sync_proto_msgTypes = make([]protoimpl.MessageInfo, 7) +var file_sync_proto_msgTypes = make([]protoimpl.MessageInfo, 6) var file_sync_proto_goTypes = []any{ - (SetRecordStatus)(0), // 0: sync.SetRecordStatus - (ChangeType)(0), // 1: sync.ChangeType - (*Record)(nil), // 2: sync.Record - (*SetRecordRequest)(nil), // 3: sync.SetRecordRequest - (*SetRecordReply)(nil), // 4: sync.SetRecordReply - (*ListChangesRequest)(nil), // 5: sync.ListChangesRequest - (*ListChangesReply)(nil), // 6: sync.ListChangesReply - (*ListenChangesRequest)(nil), // 7: sync.ListenChangesRequest - (*Change)(nil), // 8: sync.Change + (*Record)(nil), // 0: sync.Record + (*SetRecordRequest)(nil), // 1: sync.SetRecordRequest + (*SetRecordReply)(nil), // 2: sync.SetRecordReply + (*ListChangesRequest)(nil), // 3: sync.ListChangesRequest + (*ListChangesReply)(nil), // 4: sync.ListChangesReply + (*ListenChangesRequest)(nil), // 5: sync.ListenChangesRequest } var file_sync_proto_depIdxs = []int32{ - 2, // 0: sync.SetRecordRequest.record:type_name -> sync.Record - 0, // 1: sync.SetRecordReply.status:type_name -> sync.SetRecordStatus - 2, // 2: sync.ListChangesReply.changes:type_name -> sync.Record - 1, // 3: sync.Change.type:type_name -> sync.ChangeType - 2, // 4: sync.Change.record:type_name -> sync.Record - 3, // 5: sync.Syncer.SetRecord:input_type -> sync.SetRecordRequest - 5, // 6: sync.Syncer.ListChanges:input_type -> sync.ListChangesRequest - 7, // 7: sync.Syncer.ListenChanges:input_type -> sync.ListenChangesRequest - 4, // 8: sync.Syncer.SetRecord:output_type -> sync.SetRecordReply - 6, // 9: sync.Syncer.ListChanges:output_type -> sync.ListChangesReply - 8, // 10: sync.Syncer.ListenChanges:output_type -> sync.Change - 8, // [8:11] is the sub-list for method output_type - 5, // [5:8] is the sub-list for method input_type - 5, // [5:5] is the sub-list for extension type_name - 5, // [5:5] is the sub-list for extension extendee - 0, // [0:5] is the sub-list for field type_name + 0, // 0: sync.ListChangesReply.changes:type_name -> sync.Record + 1, // 1: sync.Syncer.SetRecord:input_type -> sync.SetRecordRequest + 3, // 2: sync.Syncer.ListChanges:input_type -> sync.ListChangesRequest + 5, // 3: sync.Syncer.ListenChanges:input_type -> sync.ListenChangesRequest + 2, // 4: sync.Syncer.SetRecord:output_type -> sync.SetRecordReply + 4, // 5: sync.Syncer.ListChanges:output_type -> sync.ListChangesReply + 0, // 6: sync.Syncer.ListenChanges:output_type -> sync.Record + 4, // [4:7] is the sub-list for method output_type + 1, // [1:4] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name } func init() { file_sync_proto_init() } @@ -706,33 +535,19 @@ func file_sync_proto_init() { return nil } } - file_sync_proto_msgTypes[6].Exporter = func(v any, i int) any { - switch v := v.(*Change); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } } - file_sync_proto_msgTypes[6].OneofWrappers = []any{} type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_sync_proto_rawDesc, - NumEnums: 2, - NumMessages: 7, + NumEnums: 0, + NumMessages: 6, NumExtensions: 0, NumServices: 1, }, GoTypes: file_sync_proto_goTypes, DependencyIndexes: file_sync_proto_depIdxs, - EnumInfos: file_sync_proto_enumTypes, MessageInfos: file_sync_proto_msgTypes, }.Build() File_sync_proto = out.File diff --git a/proto/sync.proto b/proto/sync.proto index c07af04..0dfb3b4 100644 --- a/proto/sync.proto +++ b/proto/sync.proto @@ -16,16 +16,12 @@ message Record { } message SetRecordRequest { - Record record = 1; - uint32 request_time = 2; - string signature = 3; -} -enum SetRecordStatus { - SUCCESS = 0; - CONFLICT = 1; + bytes record_data = 1; + float record_version = 2; + uint32 request_time = 3; + string signature = 4; } message SetRecordReply { - SetRecordStatus status = 1; int64 new_id = 2; } diff --git a/store/sync_storage.go b/store/sync_storage.go index 5c6c9e5..54f9f25 100644 --- a/store/sync_storage.go +++ b/store/sync_storage.go @@ -3,7 +3,6 @@ package store import ( "context" "database/sql" - "errors" "fmt" "os" "path" @@ -17,15 +16,13 @@ import ( _ "github.com/mattn/go-sqlite3" ) -var ErrSetConflict = errors.New("set conflict") - type StoredRecord struct { Id int64 Version float32 Data []byte } type SyncStorage interface { - SetRecord(ctx context.Context, userRecordId int64, version float32, data []byte) (int64, error) + SetRecord(ctx context.Context, version float32, data []byte) (int64, error) ListChanges(ctx context.Context, fromId int64) ([]StoredRecord, error) } @@ -61,23 +58,7 @@ func Connect(file string) (*SQLiteSyncStorage, error) { return &SQLiteSyncStorage{db: db}, nil } -func (s *SQLiteSyncStorage) SetRecord(ctx context.Context, userRecordId int64, version float32, data []byte) (int64, error) { - var latestRecordId int64 - err := s.db.QueryRow(` - SELECT id - FROM RECORDS - ORDER BY id DESC - LIMIT 1 - `).Scan(&latestRecordId) - if err != sql.ErrNoRows { - if err != nil { - return 0, fmt.Errorf("failed to currenet version %w", err) - } - if latestRecordId+1 != userRecordId { - return 0, ErrSetConflict - } - } - +func (s *SQLiteSyncStorage) SetRecord(ctx context.Context, version float32, data []byte) (int64, error) { res, err := s.db.Exec("INSERT INTO RECORDS (version, data) VALUES (?, ?)", version, data) if err != nil { return 0, fmt.Errorf("failed to insert record: %w", err) diff --git a/store/sync_storage_test.go b/store/sync_storage_test.go index 66831e1..bac263b 100644 --- a/store/sync_storage_test.go +++ b/store/sync_storage_test.go @@ -12,36 +12,19 @@ func TestAddRecords(t *testing.T) { require.NoError(t, err, "failed to connect") records := []StoredRecord{ - {Id: 0, Version: 0.1, Data: []byte("{}")}, {Id: 1, Version: 0.1, Data: []byte("{}")}, + {Id: 2, Version: 0.1, Data: []byte("{}")}, } - newId, err := storage.SetRecord(context.Background(), records[0].Id, records[0].Version, records[0].Data) + newId, err := storage.SetRecord(context.Background(), records[0].Version, records[0].Data) require.NoError(t, err, "failed to call SetRecord 0") - require.Equal(t, newId, int64(0)) + require.Equal(t, newId, records[0].Id) - newId, err = storage.SetRecord(context.Background(), records[1].Id, records[1].Version, records[1].Data) + newId, err = storage.SetRecord(context.Background(), records[1].Version, records[1].Data) require.NoError(t, err, "failed to call SetRecord 1") - require.Equal(t, newId, int64(1)) + require.Equal(t, newId, records[1].Id) fetchedRecords, err := storage.ListChanges(context.Background(), 0) require.NoError(t, err, "failed to call list changes") require.Equal(t, fetchedRecords, records) } - -func TestConflict(t *testing.T) { - storage, err := Connect("file:testconflict?mode=memory&cache=shared") - require.NoError(t, err, "failed to connect") - - record := StoredRecord{ - Id: 0, Version: 0.1, Data: []byte("{}"), - } - - newId, err := storage.SetRecord(context.Background(), record.Id, record.Version, record.Data) - require.NoError(t, err, "failed to call SetRecord 0") - require.Equal(t, newId, int64(0)) - - newId, err = storage.SetRecord(context.Background(), record.Id, record.Version, record.Data) - require.Error(t, err, "Second call of SetRecord 0 is supposed to fail due to set conflict") - require.Equal(t, err, ErrSetConflict) -} diff --git a/sync_server_test.go b/sync_server_test.go index 7e772be..8f370ff 100644 --- a/sync_server_test.go +++ b/sync_server_test.go @@ -57,30 +57,15 @@ func TestSyncService(t *testing.T) { if setRecordRequest, ok := testCase.request.(*proto.SetRecordRequest); ok { testSetRecord(t, privateKey, client1, setRecordRequest, testCase) - if testCase.reply.(*proto.SetRecordReply).Status != proto.SetRecordStatus_SUCCESS { - continue - } - // Test that the expected value matches the one received from the stream record1, err := changes_stream1.Recv() require.NoError(t, err, "failed to receive changes") - - received_json, err := json.Marshal(record1) - require.NoError(t, err, "failed to serialize received record") - - expected_json, err := json.Marshal(testCase.request.(*proto.SetRecordRequest).Record) - require.NoError(t, err, "failed to serialize expected record") - - require.Equal(t, received_json, expected_json) + require.Equal(t, record1.Data, testCase.request.(*proto.SetRecordRequest).RecordData) // Test that the second client also received a valid value record2, err := changes_stream2.Recv() require.NoError(t, err, "failed to receive changes") - - received_json, err = json.Marshal(record2) - require.NoError(t, err, "failed to serialize received record") - - require.Equal(t, received_json, expected_json) + require.Equal(t, record2.Data, testCase.request.(*proto.SetRecordRequest).RecordData) } if listChangesRequest, ok := testCase.request.(*proto.ListChangesRequest); ok { testListChanges(t, privateKey, client1, listChangesRequest, testCase) @@ -106,15 +91,11 @@ func testCases() []testCase { { name: "first record insert", request: &proto.SetRecordRequest{ - Record: &proto.Record{ - Id: 1, - Version: 0.1, - Data: []byte("{}"), - }, + RecordData: []byte("{}"), + RecordVersion: 0.1, }, reply: &proto.SetRecordReply{ - Status: proto.SetRecordStatus_SUCCESS, - NewId: 1, + NewId: 1, }, }, @@ -122,30 +103,11 @@ func testCases() []testCase { { name: "second record insert", request: &proto.SetRecordRequest{ - Record: &proto.Record{ - Id: 2, - Version: 0.1, - Data: []byte("{}"), - }, - }, - reply: &proto.SetRecordReply{ - Status: proto.SetRecordStatus_SUCCESS, - NewId: 2, - }, - }, - - // test conflict - { - name: "test conflict", - request: &proto.SetRecordRequest{ - Record: &proto.Record{ - Id: 1, - Version: 0.1, - Data: []byte("{}"), - }, + RecordData: []byte("{}"), + RecordVersion: 0.1, }, reply: &proto.SetRecordReply{ - Status: proto.SetRecordStatus_CONFLICT, + NewId: 2, }, }, @@ -184,7 +146,7 @@ func listenChanges(t *testing.T, privateKey *btcec.PrivateKey, client proto.Sync func testSetRecord(t *testing.T, privateKey *btcec.PrivateKey, client proto.SyncerClient, request *proto.SetRecordRequest, test testCase) { requestTime := time.Now().Unix() - toSign := fmt.Sprintf("%v-%v-%x-%v", request.Record.Id, request.Record.Version, request.Record.Data, requestTime) + toSign := fmt.Sprintf("%x-%v-%v", request.RecordData, request.RecordVersion, requestTime) signature, err := middleware.SignMessage(privateKey, []byte(toSign)) require.NoError(t, err, "failed to sign message") request.RequestTime = uint32(requestTime) diff --git a/syncer_server.go b/syncer_server.go index 67ffa41..2fb94a5 100644 --- a/syncer_server.go +++ b/syncer_server.go @@ -30,16 +30,10 @@ type PersistentSyncerServer struct { func (s *PersistentSyncerServer) SetRecord(c context.Context, msg *proto.SetRecordRequest) (*proto.SetRecordReply, error) { newId, err := c.Value(middleware.USER_DB_CONTEXT_KEY).(*store.SQLiteSyncStorage).SetRecord( c, - msg.Record.Id, - msg.Record.Version, - msg.Record.Data, + msg.RecordVersion, + msg.RecordData, ) if err != nil { - if err == store.ErrSetConflict { - return &proto.SetRecordReply{ - Status: proto.SetRecordStatus_CONFLICT, - }, nil - } return nil, err } @@ -48,11 +42,14 @@ func (s *PersistentSyncerServer) SetRecord(c context.Context, msg *proto.SetReco if _, exists := s.users[pubkey]; !exists { addUser(s, pubkey) } - s.users[pubkey].records_channel <- msg.Record + s.users[pubkey].records_channel <- &proto.Record{ + Id: newId, + Data: msg.RecordData, + Version: msg.RecordVersion, + } return &proto.SetRecordReply{ - Status: proto.SetRecordStatus_SUCCESS, - NewId: newId, + NewId: newId, }, nil }