From 7488e6080c4fa2064fbd464af8d4e96924b2abc1 Mon Sep 17 00:00:00 2001 From: Viktor Nosov Date: Thu, 19 Jan 2023 17:48:04 +0300 Subject: [PATCH 1/3] rename token.Store interface to token.BalanceStore Signed-off-by: Viktor Nosov --- examples/erc20_utxo/service/allowance/allowance.go | 4 ++-- extensions/token/balance.go | 4 ++-- extensions/token/balance_account.go | 2 +- extensions/token/balance_store.go | 2 +- extensions/token/balance_utxo.go | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/erc20_utxo/service/allowance/allowance.go b/examples/erc20_utxo/service/allowance/allowance.go index ca62b6b..af50990 100644 --- a/examples/erc20_utxo/service/allowance/allowance.go +++ b/examples/erc20_utxo/service/allowance/allowance.go @@ -16,11 +16,11 @@ var ( ) type Service struct { - balance token.Store + balance token.BalanceStore account account.Getter } -func NewService(account account.Getter, balance token.Store) *Service { +func NewService(account account.Getter, balance token.BalanceStore) *Service { return &Service{ account: account, balance: balance, diff --git a/extensions/token/balance.go b/extensions/token/balance.go index 0c99bac..700ab62 100644 --- a/extensions/token/balance.go +++ b/extensions/token/balance.go @@ -10,13 +10,13 @@ import ( type BalanceService struct { Account account.Getter Token TokenGetter - Store Store + Store BalanceStore } func NewBalanceService( accountResolver account.Getter, tokenGetter TokenGetter, - store Store) *BalanceService { + store BalanceStore) *BalanceService { return &BalanceService{ Account: accountResolver, diff --git a/extensions/token/balance_account.go b/extensions/token/balance_account.go index 7e66a30..d087daf 100644 --- a/extensions/token/balance_account.go +++ b/extensions/token/balance_account.go @@ -9,7 +9,7 @@ import ( "github.com/hyperledger-labs/cckit/state" ) -var _ Store = &AccountStore{} +var _ BalanceStore = &AccountStore{} type AccountStore struct { } diff --git a/extensions/token/balance_store.go b/extensions/token/balance_store.go index 91ddc07..28fffd4 100644 --- a/extensions/token/balance_store.go +++ b/extensions/token/balance_store.go @@ -12,7 +12,7 @@ type ( TxId string } - Store interface { + BalanceStore interface { Get(router.Context, *BalanceId) (*Balance, error) GetLocked(router.Context, *BalanceId) (*Balance, error) List(router.Context, *TokenId) ([]*Balance, error) diff --git a/extensions/token/balance_utxo.go b/extensions/token/balance_utxo.go index 748eeef..748fc84 100644 --- a/extensions/token/balance_utxo.go +++ b/extensions/token/balance_utxo.go @@ -9,7 +9,7 @@ import ( "github.com/hyperledger-labs/cckit/state" ) -var _ Store = &UTXOStore{} +var _ BalanceStore = &UTXOStore{} var ( ErrSenderRecipientEqual = errors.New(`sender recipient equal`) From 1a6b6d837d153300aee3addb7f0d73b4dc15c986 Mon Sep 17 00:00:00 2001 From: Viktor Nosov Date: Sun, 15 Oct 2023 12:56:13 +0300 Subject: [PATCH 2/3] label extesion Signed-off-by: Viktor Nosov --- extensions/label/label.go | 57 ++++++++++++++++++++++++++++++++++++ extensions/label/label.proto | 18 ++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 extensions/label/label.go create mode 100644 extensions/label/label.proto diff --git a/extensions/label/label.go b/extensions/label/label.go new file mode 100644 index 0000000..4a7475b --- /dev/null +++ b/extensions/label/label.go @@ -0,0 +1,57 @@ +package label + +func BlankMetadata() *Metadata { + return &Metadata{ + Labels: []*Label{}, + } +} + +func (m *Metadata) MatchLabel(expectLabel *Label) bool { + for _, l := range m.GetLabels() { + if l.Value == expectLabel.Value && l.Key == expectLabel.Key { + return true + } + } + return false +} + +func (m *Metadata) GetLabelByKey(key string) *Label { + for _, l := range m.GetLabels() { + if l.Key == key { + return l + } + } + return nil +} + +func (m *Metadata) GetLabelsByKey(key string) []*Label { + var labels []*Label + for _, l := range m.GetLabels() { + if l.Key == key { + labels = append(labels, l) + } + } + return labels +} + +func (m *Metadata) RemoveLabel(expectLabel *Label) bool { + var ( + i int + l *Label + ) + for i, l = range m.GetLabels() { + if l.Key == expectLabel.Key && l.Value == expectLabel.Value { + break + } + } + m.Labels = append(m.Labels[i:], m.Labels[i+1:]...) + return false +} + +func (m *Metadata) AddLabel(newLabel *Label) error { + if err := newLabel.Validate(); err != nil { + return err + } + m.Labels = append(m.Labels, newLabel) + return nil +} diff --git a/extensions/label/label.proto b/extensions/label/label.proto new file mode 100644 index 0000000..58c5dc7 --- /dev/null +++ b/extensions/label/label.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; + +package extensions.label; + +option go_package = "github.com/hyperledger-labs/cckit/extensions/label"; + +import "mwitkow/go-proto-validators/validator.proto"; + +// label - container for name/value content +message Label { + string key = 1 [(validator.field) = {length_gt: 1}]; + string value = 2 [(validator.field) = {length_gt: 1}]; +} + +// Metadata - type for integrate to other objects +message Metadata { + repeated Label labels = 1; +} \ No newline at end of file From 56fa9e29d3fa139dbe6795253a6628f7199ef90f Mon Sep 17 00:00:00 2001 From: Viktor Nosov Date: Sun, 15 Oct 2023 12:58:02 +0300 Subject: [PATCH 3/3] label extesion Signed-off-by: Viktor Nosov --- extensions/label/label.pb.go | 225 +++++++++++++++++++++++++ extensions/label/label.swagger.json | 49 ++++++ extensions/label/label.validator.pb.go | 37 ++++ 3 files changed, 311 insertions(+) create mode 100644 extensions/label/label.pb.go create mode 100644 extensions/label/label.swagger.json create mode 100644 extensions/label/label.validator.pb.go diff --git a/extensions/label/label.pb.go b/extensions/label/label.pb.go new file mode 100644 index 0000000..3470345 --- /dev/null +++ b/extensions/label/label.pb.go @@ -0,0 +1,225 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc (unknown) +// source: label/label.proto + +package label + +import ( + _ "github.com/mwitkow/go-proto-validators" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// label - container for name/value content +type Label struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` + Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` +} + +func (x *Label) Reset() { + *x = Label{} + if protoimpl.UnsafeEnabled { + mi := &file_label_label_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Label) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Label) ProtoMessage() {} + +func (x *Label) ProtoReflect() protoreflect.Message { + mi := &file_label_label_proto_msgTypes[0] + 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 Label.ProtoReflect.Descriptor instead. +func (*Label) Descriptor() ([]byte, []int) { + return file_label_label_proto_rawDescGZIP(), []int{0} +} + +func (x *Label) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +func (x *Label) GetValue() string { + if x != nil { + return x.Value + } + return "" +} + +// Metadata - type for integrate to other objects +type Metadata struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Labels []*Label `protobuf:"bytes,1,rep,name=labels,proto3" json:"labels,omitempty"` +} + +func (x *Metadata) Reset() { + *x = Metadata{} + if protoimpl.UnsafeEnabled { + mi := &file_label_label_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Metadata) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Metadata) ProtoMessage() {} + +func (x *Metadata) ProtoReflect() protoreflect.Message { + mi := &file_label_label_proto_msgTypes[1] + 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 Metadata.ProtoReflect.Descriptor instead. +func (*Metadata) Descriptor() ([]byte, []int) { + return file_label_label_proto_rawDescGZIP(), []int{1} +} + +func (x *Metadata) GetLabels() []*Label { + if x != nil { + return x.Labels + } + return nil +} + +var File_label_label_proto protoreflect.FileDescriptor + +var file_label_label_proto_rawDesc = []byte{ + 0x0a, 0x11, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x2f, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x12, 0x10, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x2e, + 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x1a, 0x2b, 0x6d, 0x77, 0x69, 0x74, 0x6b, 0x6f, 0x77, 0x2f, 0x67, + 0x6f, 0x2d, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2d, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x6f, + 0x72, 0x73, 0x2f, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x6f, 0x72, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x22, 0x3f, 0x0a, 0x05, 0x4c, 0x61, 0x62, 0x65, 0x6c, 0x12, 0x18, 0x0a, 0x03, 0x6b, + 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x06, 0xe2, 0xdf, 0x1f, 0x02, 0x70, 0x01, + 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x1c, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x42, 0x06, 0xe2, 0xdf, 0x1f, 0x02, 0x70, 0x01, 0x52, 0x05, 0x76, 0x61, + 0x6c, 0x75, 0x65, 0x22, 0x3b, 0x0a, 0x08, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x12, + 0x2f, 0x0a, 0x06, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, + 0x17, 0x2e, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x6c, 0x61, 0x62, + 0x65, 0x6c, 0x2e, 0x4c, 0x61, 0x62, 0x65, 0x6c, 0x52, 0x06, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x73, + 0x42, 0x34, 0x5a, 0x32, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, + 0x79, 0x70, 0x65, 0x72, 0x6c, 0x65, 0x64, 0x67, 0x65, 0x72, 0x2d, 0x6c, 0x61, 0x62, 0x73, 0x2f, + 0x63, 0x63, 0x6b, 0x69, 0x74, 0x2f, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, + 0x2f, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_label_label_proto_rawDescOnce sync.Once + file_label_label_proto_rawDescData = file_label_label_proto_rawDesc +) + +func file_label_label_proto_rawDescGZIP() []byte { + file_label_label_proto_rawDescOnce.Do(func() { + file_label_label_proto_rawDescData = protoimpl.X.CompressGZIP(file_label_label_proto_rawDescData) + }) + return file_label_label_proto_rawDescData +} + +var file_label_label_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_label_label_proto_goTypes = []interface{}{ + (*Label)(nil), // 0: extensions.label.Label + (*Metadata)(nil), // 1: extensions.label.Metadata +} +var file_label_label_proto_depIdxs = []int32{ + 0, // 0: extensions.label.Metadata.labels:type_name -> extensions.label.Label + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] 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_label_label_proto_init() } +func file_label_label_proto_init() { + if File_label_label_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_label_label_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Label); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_label_label_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Metadata); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_label_label_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_label_label_proto_goTypes, + DependencyIndexes: file_label_label_proto_depIdxs, + MessageInfos: file_label_label_proto_msgTypes, + }.Build() + File_label_label_proto = out.File + file_label_label_proto_rawDesc = nil + file_label_label_proto_goTypes = nil + file_label_label_proto_depIdxs = nil +} diff --git a/extensions/label/label.swagger.json b/extensions/label/label.swagger.json new file mode 100644 index 0000000..576f9b3 --- /dev/null +++ b/extensions/label/label.swagger.json @@ -0,0 +1,49 @@ +{ + "swagger": "2.0", + "info": { + "title": "label/label.proto", + "version": "version not set" + }, + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "paths": {}, + "definitions": { + "protobufAny": { + "type": "object", + "properties": { + "type_url": { + "type": "string" + }, + "value": { + "type": "string", + "format": "byte" + } + } + }, + "runtimeError": { + "type": "object", + "properties": { + "error": { + "type": "string" + }, + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/protobufAny" + } + } + } + } + } +} diff --git a/extensions/label/label.validator.pb.go b/extensions/label/label.validator.pb.go new file mode 100644 index 0000000..f81e19b --- /dev/null +++ b/extensions/label/label.validator.pb.go @@ -0,0 +1,37 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: label/label.proto + +package label + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + _ "github.com/mwitkow/go-proto-validators" + github_com_mwitkow_go_proto_validators "github.com/mwitkow/go-proto-validators" + math "math" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +func (this *Label) Validate() error { + if !(len(this.Key) > 1) { + return github_com_mwitkow_go_proto_validators.FieldError("Key", fmt.Errorf(`value '%v' must have a length greater than '1'`, this.Key)) + } + if !(len(this.Value) > 1) { + return github_com_mwitkow_go_proto_validators.FieldError("Value", fmt.Errorf(`value '%v' must have a length greater than '1'`, this.Value)) + } + return nil +} +func (this *Metadata) Validate() error { + for _, item := range this.Labels { + if item != nil { + if err := github_com_mwitkow_go_proto_validators.CallValidatorIfExists(item); err != nil { + return github_com_mwitkow_go_proto_validators.FieldError("Labels", err) + } + } + } + return nil +}