From dac065aa035fa7397167e4788b01aea8fdcd40f8 Mon Sep 17 00:00:00 2001 From: poorphd Date: Fri, 12 Jul 2024 14:40:03 +0900 Subject: [PATCH 1/2] fix: genesis import/export for csr module --- api/canto/csr/v1/genesis.pulsar.go | 278 ++++++++++++++++++++++++++--- proto/canto/csr/v1/genesis.proto | 3 + x/csr/genesis.go | 22 +++ x/csr/keeper/csr.go | 24 +++ x/csr/keeper/genesis_test.go | 95 ++++++++++ x/csr/types/genesis.go | 8 +- x/csr/types/genesis.pb.go | 141 +++++++++++++-- 7 files changed, 535 insertions(+), 36 deletions(-) create mode 100644 x/csr/keeper/genesis_test.go diff --git a/api/canto/csr/v1/genesis.pulsar.go b/api/canto/csr/v1/genesis.pulsar.go index be04a7910..ecdbb1aa8 100644 --- a/api/canto/csr/v1/genesis.pulsar.go +++ b/api/canto/csr/v1/genesis.pulsar.go @@ -13,15 +13,70 @@ import ( sync "sync" ) +var _ protoreflect.List = (*_GenesisState_2_list)(nil) + +type _GenesisState_2_list struct { + list *[]*CSR +} + +func (x *_GenesisState_2_list) Len() int { + if x.list == nil { + return 0 + } + return len(*x.list) +} + +func (x *_GenesisState_2_list) Get(i int) protoreflect.Value { + return protoreflect.ValueOfMessage((*x.list)[i].ProtoReflect()) +} + +func (x *_GenesisState_2_list) Set(i int, value protoreflect.Value) { + valueUnwrapped := value.Message() + concreteValue := valueUnwrapped.Interface().(*CSR) + (*x.list)[i] = concreteValue +} + +func (x *_GenesisState_2_list) Append(value protoreflect.Value) { + valueUnwrapped := value.Message() + concreteValue := valueUnwrapped.Interface().(*CSR) + *x.list = append(*x.list, concreteValue) +} + +func (x *_GenesisState_2_list) AppendMutable() protoreflect.Value { + v := new(CSR) + *x.list = append(*x.list, v) + return protoreflect.ValueOfMessage(v.ProtoReflect()) +} + +func (x *_GenesisState_2_list) Truncate(n int) { + for i := n; i < len(*x.list); i++ { + (*x.list)[i] = nil + } + *x.list = (*x.list)[:n] +} + +func (x *_GenesisState_2_list) NewElement() protoreflect.Value { + v := new(CSR) + return protoreflect.ValueOfMessage(v.ProtoReflect()) +} + +func (x *_GenesisState_2_list) IsValid() bool { + return x.list != nil +} + var ( - md_GenesisState protoreflect.MessageDescriptor - fd_GenesisState_params protoreflect.FieldDescriptor + md_GenesisState protoreflect.MessageDescriptor + fd_GenesisState_params protoreflect.FieldDescriptor + fd_GenesisState_csrs protoreflect.FieldDescriptor + fd_GenesisState_turnstile_address protoreflect.FieldDescriptor ) func init() { file_canto_csr_v1_genesis_proto_init() md_GenesisState = File_canto_csr_v1_genesis_proto.Messages().ByName("GenesisState") fd_GenesisState_params = md_GenesisState.Fields().ByName("params") + fd_GenesisState_csrs = md_GenesisState.Fields().ByName("csrs") + fd_GenesisState_turnstile_address = md_GenesisState.Fields().ByName("turnstile_address") } var _ protoreflect.Message = (*fastReflection_GenesisState)(nil) @@ -95,6 +150,18 @@ func (x *fastReflection_GenesisState) Range(f func(protoreflect.FieldDescriptor, return } } + if len(x.Csrs) != 0 { + value := protoreflect.ValueOfList(&_GenesisState_2_list{list: &x.Csrs}) + if !f(fd_GenesisState_csrs, value) { + return + } + } + if len(x.TurnstileAddress) != 0 { + value := protoreflect.ValueOfBytes(x.TurnstileAddress) + if !f(fd_GenesisState_turnstile_address, value) { + return + } + } } // Has reports whether a field is populated. @@ -112,6 +179,10 @@ func (x *fastReflection_GenesisState) Has(fd protoreflect.FieldDescriptor) bool switch fd.FullName() { case "canto.csr.v1.GenesisState.params": return x.Params != nil + case "canto.csr.v1.GenesisState.csrs": + return len(x.Csrs) != 0 + case "canto.csr.v1.GenesisState.turnstile_address": + return len(x.TurnstileAddress) != 0 default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -130,6 +201,10 @@ func (x *fastReflection_GenesisState) Clear(fd protoreflect.FieldDescriptor) { switch fd.FullName() { case "canto.csr.v1.GenesisState.params": x.Params = nil + case "canto.csr.v1.GenesisState.csrs": + x.Csrs = nil + case "canto.csr.v1.GenesisState.turnstile_address": + x.TurnstileAddress = nil default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -149,6 +224,15 @@ func (x *fastReflection_GenesisState) Get(descriptor protoreflect.FieldDescripto case "canto.csr.v1.GenesisState.params": value := x.Params return protoreflect.ValueOfMessage(value.ProtoReflect()) + case "canto.csr.v1.GenesisState.csrs": + if len(x.Csrs) == 0 { + return protoreflect.ValueOfList(&_GenesisState_2_list{}) + } + listValue := &_GenesisState_2_list{list: &x.Csrs} + return protoreflect.ValueOfList(listValue) + case "canto.csr.v1.GenesisState.turnstile_address": + value := x.TurnstileAddress + return protoreflect.ValueOfBytes(value) default: if descriptor.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -171,6 +255,12 @@ func (x *fastReflection_GenesisState) Set(fd protoreflect.FieldDescriptor, value switch fd.FullName() { case "canto.csr.v1.GenesisState.params": x.Params = value.Message().Interface().(*Params) + case "canto.csr.v1.GenesisState.csrs": + lv := value.List() + clv := lv.(*_GenesisState_2_list) + x.Csrs = *clv.list + case "canto.csr.v1.GenesisState.turnstile_address": + x.TurnstileAddress = value.Bytes() default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -196,6 +286,14 @@ func (x *fastReflection_GenesisState) Mutable(fd protoreflect.FieldDescriptor) p x.Params = new(Params) } return protoreflect.ValueOfMessage(x.Params.ProtoReflect()) + case "canto.csr.v1.GenesisState.csrs": + if x.Csrs == nil { + x.Csrs = []*CSR{} + } + value := &_GenesisState_2_list{list: &x.Csrs} + return protoreflect.ValueOfList(value) + case "canto.csr.v1.GenesisState.turnstile_address": + panic(fmt.Errorf("field turnstile_address of message canto.csr.v1.GenesisState is not mutable")) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -212,6 +310,11 @@ func (x *fastReflection_GenesisState) NewField(fd protoreflect.FieldDescriptor) case "canto.csr.v1.GenesisState.params": m := new(Params) return protoreflect.ValueOfMessage(m.ProtoReflect()) + case "canto.csr.v1.GenesisState.csrs": + list := []*CSR{} + return protoreflect.ValueOfList(&_GenesisState_2_list{list: &list}) + case "canto.csr.v1.GenesisState.turnstile_address": + return protoreflect.ValueOfBytes(nil) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -285,6 +388,16 @@ func (x *fastReflection_GenesisState) ProtoMethods() *protoiface.Methods { l = options.Size(x.Params) n += 1 + l + runtime.Sov(uint64(l)) } + if len(x.Csrs) > 0 { + for _, e := range x.Csrs { + l = options.Size(e) + n += 1 + l + runtime.Sov(uint64(l)) + } + } + l = len(x.TurnstileAddress) + if l > 0 { + n += 1 + l + runtime.Sov(uint64(l)) + } if x.unknownFields != nil { n += len(x.unknownFields) } @@ -314,6 +427,29 @@ func (x *fastReflection_GenesisState) ProtoMethods() *protoiface.Methods { i -= len(x.unknownFields) copy(dAtA[i:], x.unknownFields) } + if len(x.TurnstileAddress) > 0 { + i -= len(x.TurnstileAddress) + copy(dAtA[i:], x.TurnstileAddress) + i = runtime.EncodeVarint(dAtA, i, uint64(len(x.TurnstileAddress))) + i-- + dAtA[i] = 0x1a + } + if len(x.Csrs) > 0 { + for iNdEx := len(x.Csrs) - 1; iNdEx >= 0; iNdEx-- { + encoded, err := options.Marshal(x.Csrs[iNdEx]) + if err != nil { + return protoiface.MarshalOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Buf: input.Buf, + }, err + } + i -= len(encoded) + copy(dAtA[i:], encoded) + i = runtime.EncodeVarint(dAtA, i, uint64(len(encoded))) + i-- + dAtA[i] = 0x12 + } + } if x.Params != nil { encoded, err := options.Marshal(x.Params) if err != nil { @@ -413,6 +549,74 @@ func (x *fastReflection_GenesisState) ProtoMethods() *protoiface.Methods { return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, err } iNdEx = postIndex + case 2: + if wireType != 2 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field Csrs", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + if postIndex > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + x.Csrs = append(x.Csrs, &CSR{}) + if err := options.Unmarshal(dAtA[iNdEx:postIndex], x.Csrs[len(x.Csrs)-1]); err != nil { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field TurnstileAddress", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + if postIndex > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + x.TurnstileAddress = append(x.TurnstileAddress[:0], dAtA[iNdEx:postIndex]...) + if x.TurnstileAddress == nil { + x.TurnstileAddress = []byte{} + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := runtime.Skip(dAtA[iNdEx:]) @@ -468,7 +672,9 @@ type GenesisState struct { unknownFields protoimpl.UnknownFields // params defines all of the parameters of the module - Params *Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params,omitempty"` + Params *Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params,omitempty"` + Csrs []*CSR `protobuf:"bytes,2,rep,name=csrs,proto3" json:"csrs,omitempty"` + TurnstileAddress []byte `protobuf:"bytes,3,opt,name=turnstile_address,json=turnstileAddress,proto3" json:"turnstile_address,omitempty"` } func (x *GenesisState) Reset() { @@ -498,6 +704,20 @@ func (x *GenesisState) GetParams() *Params { return nil } +func (x *GenesisState) GetCsrs() []*CSR { + if x != nil { + return x.Csrs + } + return nil +} + +func (x *GenesisState) GetTurnstileAddress() []byte { + if x != nil { + return x.TurnstileAddress + } + return nil +} + var File_canto_csr_v1_genesis_proto protoreflect.FileDescriptor var file_canto_csr_v1_genesis_proto_rawDesc = []byte{ @@ -506,22 +726,29 @@ var file_canto_csr_v1_genesis_proto_rawDesc = []byte{ 0x6e, 0x74, 0x6f, 0x2e, 0x63, 0x73, 0x72, 0x2e, 0x76, 0x31, 0x1a, 0x14, 0x67, 0x6f, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x6f, 0x67, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x19, 0x63, 0x61, 0x6e, 0x74, 0x6f, 0x2f, 0x63, 0x73, 0x72, 0x2f, 0x76, 0x31, 0x2f, 0x70, - 0x61, 0x72, 0x61, 0x6d, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x42, 0x0a, 0x0c, 0x47, - 0x65, 0x6e, 0x65, 0x73, 0x69, 0x73, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x32, 0x0a, 0x06, 0x70, - 0x61, 0x72, 0x61, 0x6d, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x63, 0x61, - 0x6e, 0x74, 0x6f, 0x2e, 0x63, 0x73, 0x72, 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x61, 0x72, 0x61, 0x6d, - 0x73, 0x42, 0x04, 0xc8, 0xde, 0x1f, 0x00, 0x52, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x42, - 0x97, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x61, 0x6e, 0x74, 0x6f, 0x2e, 0x63, 0x73, - 0x72, 0x2e, 0x76, 0x31, 0x42, 0x0c, 0x47, 0x65, 0x6e, 0x65, 0x73, 0x69, 0x73, 0x50, 0x72, 0x6f, - 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x23, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x73, 0x64, 0x6b, 0x2e, - 0x69, 0x6f, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x63, 0x61, 0x6e, 0x74, 0x6f, 0x2f, 0x63, 0x73, 0x72, - 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x73, 0x72, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x43, 0x43, 0x58, 0xaa, - 0x02, 0x0c, 0x43, 0x61, 0x6e, 0x74, 0x6f, 0x2e, 0x43, 0x73, 0x72, 0x2e, 0x56, 0x31, 0xca, 0x02, - 0x0c, 0x43, 0x61, 0x6e, 0x74, 0x6f, 0x5c, 0x43, 0x73, 0x72, 0x5c, 0x56, 0x31, 0xe2, 0x02, 0x18, - 0x43, 0x61, 0x6e, 0x74, 0x6f, 0x5c, 0x43, 0x73, 0x72, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, - 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x0e, 0x43, 0x61, 0x6e, 0x74, 0x6f, - 0x3a, 0x3a, 0x43, 0x73, 0x72, 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x33, + 0x61, 0x72, 0x61, 0x6d, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x16, 0x63, 0x61, 0x6e, + 0x74, 0x6f, 0x2f, 0x63, 0x73, 0x72, 0x2f, 0x76, 0x31, 0x2f, 0x63, 0x73, 0x72, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x22, 0x9c, 0x01, 0x0a, 0x0c, 0x47, 0x65, 0x6e, 0x65, 0x73, 0x69, 0x73, 0x53, + 0x74, 0x61, 0x74, 0x65, 0x12, 0x32, 0x0a, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x63, 0x61, 0x6e, 0x74, 0x6f, 0x2e, 0x63, 0x73, 0x72, + 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x42, 0x04, 0xc8, 0xde, 0x1f, 0x00, + 0x52, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x2b, 0x0a, 0x04, 0x63, 0x73, 0x72, 0x73, + 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x63, 0x61, 0x6e, 0x74, 0x6f, 0x2e, 0x63, + 0x73, 0x72, 0x2e, 0x76, 0x31, 0x2e, 0x43, 0x53, 0x52, 0x42, 0x04, 0xc8, 0xde, 0x1f, 0x00, 0x52, + 0x04, 0x63, 0x73, 0x72, 0x73, 0x12, 0x2b, 0x0a, 0x11, 0x74, 0x75, 0x72, 0x6e, 0x73, 0x74, 0x69, + 0x6c, 0x65, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, + 0x52, 0x10, 0x74, 0x75, 0x72, 0x6e, 0x73, 0x74, 0x69, 0x6c, 0x65, 0x41, 0x64, 0x64, 0x72, 0x65, + 0x73, 0x73, 0x42, 0x97, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x61, 0x6e, 0x74, 0x6f, + 0x2e, 0x63, 0x73, 0x72, 0x2e, 0x76, 0x31, 0x42, 0x0c, 0x47, 0x65, 0x6e, 0x65, 0x73, 0x69, 0x73, + 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x23, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x73, + 0x64, 0x6b, 0x2e, 0x69, 0x6f, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x63, 0x61, 0x6e, 0x74, 0x6f, 0x2f, + 0x63, 0x73, 0x72, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x73, 0x72, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x43, + 0x43, 0x58, 0xaa, 0x02, 0x0c, 0x43, 0x61, 0x6e, 0x74, 0x6f, 0x2e, 0x43, 0x73, 0x72, 0x2e, 0x56, + 0x31, 0xca, 0x02, 0x0c, 0x43, 0x61, 0x6e, 0x74, 0x6f, 0x5c, 0x43, 0x73, 0x72, 0x5c, 0x56, 0x31, + 0xe2, 0x02, 0x18, 0x43, 0x61, 0x6e, 0x74, 0x6f, 0x5c, 0x43, 0x73, 0x72, 0x5c, 0x56, 0x31, 0x5c, + 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x0e, 0x43, 0x61, + 0x6e, 0x74, 0x6f, 0x3a, 0x3a, 0x43, 0x73, 0x72, 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -540,14 +767,16 @@ var file_canto_csr_v1_genesis_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_canto_csr_v1_genesis_proto_goTypes = []interface{}{ (*GenesisState)(nil), // 0: canto.csr.v1.GenesisState (*Params)(nil), // 1: canto.csr.v1.Params + (*CSR)(nil), // 2: canto.csr.v1.CSR } var file_canto_csr_v1_genesis_proto_depIdxs = []int32{ 1, // 0: canto.csr.v1.GenesisState.params:type_name -> canto.csr.v1.Params - 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 + 2, // 1: canto.csr.v1.GenesisState.csrs:type_name -> canto.csr.v1.CSR + 2, // [2:2] is the sub-list for method output_type + 2, // [2:2] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name } func init() { file_canto_csr_v1_genesis_proto_init() } @@ -556,6 +785,7 @@ func file_canto_csr_v1_genesis_proto_init() { return } file_canto_csr_v1_params_proto_init() + file_canto_csr_v1_csr_proto_init() if !protoimpl.UnsafeEnabled { file_canto_csr_v1_genesis_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*GenesisState); i { diff --git a/proto/canto/csr/v1/genesis.proto b/proto/canto/csr/v1/genesis.proto index d9370ecdd..a00d78331 100644 --- a/proto/canto/csr/v1/genesis.proto +++ b/proto/canto/csr/v1/genesis.proto @@ -3,6 +3,7 @@ package canto.csr.v1; import "gogoproto/gogo.proto"; import "canto/csr/v1/params.proto"; +import "canto/csr/v1/csr.proto"; option go_package = "github.com/Canto-Network/Canto/v7/x/csr/types"; @@ -10,4 +11,6 @@ option go_package = "github.com/Canto-Network/Canto/v7/x/csr/types"; message GenesisState { // params defines all of the parameters of the module Params params = 1 [ (gogoproto.nullable) = false ]; + repeated CSR csrs = 2 [ (gogoproto.nullable) = false ]; + bytes turnstile_address = 3; } diff --git a/x/csr/genesis.go b/x/csr/genesis.go index 2c4089e96..9fe53c9a5 100644 --- a/x/csr/genesis.go +++ b/x/csr/genesis.go @@ -5,6 +5,7 @@ import ( "github.com/Canto-Network/Canto/v7/x/csr/types" sdk "github.com/cosmos/cosmos-sdk/types" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/ethereum/go-ethereum/common" ) // InitGenesis initializes the capability module's state from a provided genesis @@ -12,6 +13,13 @@ import ( func InitGenesis(ctx sdk.Context, k keeper.Keeper, accountKeeper authkeeper.AccountKeeper, genState types.GenesisState) { // this line is used by starport scaffolding # genesis/module/init k.SetParams(ctx, genState.Params) + + for _, csr := range genState.Csrs { + k.SetCSR(ctx, csr) + } + if genState.TurnstileAddress != nil { + k.SetTurnstile(ctx, common.BytesToAddress(genState.TurnstileAddress)) + } // make sure that the csr module account is set on genesis if acc := accountKeeper.GetModuleAccount(ctx, types.ModuleName); acc == nil { // NOTE: shouldn't occur @@ -23,6 +31,20 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, accountKeeper authkeeper.Acco func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { genesis := types.DefaultGenesis() genesis.Params = k.GetParams(ctx) + csrs := k.GetAllCSRs(ctx) + + if len(csrs) == 0 { + genesis.Csrs = []types.CSR{} + } else { + genesis.Csrs = csrs + } + + turnstileAddr, ok := k.GetTurnstile(ctx) + if ok { + genesis.TurnstileAddress = turnstileAddr.Bytes() + } else { + genesis.TurnstileAddress = nil + } return genesis } diff --git a/x/csr/keeper/csr.go b/x/csr/keeper/csr.go index 57a9f1482..78a182585 100644 --- a/x/csr/keeper/csr.go +++ b/x/csr/keeper/csr.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "cosmossdk.io/store/prefix" + storetypes "cosmossdk.io/store/types" "github.com/Canto-Network/Canto/v7/x/csr/types" "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" @@ -27,6 +28,29 @@ func (k Keeper) GetCSR(ctx sdk.Context, nftId uint64) (*types.CSR, bool) { return csr, true } +// Returns all CSR objects in the store. +func (k Keeper) GetAllCSRs(ctx sdk.Context) (csrs []types.CSR) { + k.IterateAllCSRs(ctx, func(csr types.CSR) bool { + csrs = append(csrs, csr) + return false + }) + return +} + +// Iterates over all CSR objects in the store and performs a callback function on each CSR. +func (k Keeper) IterateAllCSRs(ctx sdk.Context, cb func(csr types.CSR) (stop bool)) { + store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + iter := storetypes.KVStorePrefixIterator(store, types.KeyPrefixCSR) + defer iter.Close() + for ; iter.Valid(); iter.Next() { + var csr types.CSR + k.cdc.MustUnmarshal(iter.Value(), &csr) + if cb(csr) { + break + } + } +} + // Returns the NFT ID associated with a smart contract address. If the smart contract address // entered does belong to some NFT, then it will return (id, true), otherwise (0, false). func (k Keeper) GetNFTByContract(ctx sdk.Context, address string) (uint64, bool) { diff --git a/x/csr/keeper/genesis_test.go b/x/csr/keeper/genesis_test.go new file mode 100644 index 000000000..e01b14635 --- /dev/null +++ b/x/csr/keeper/genesis_test.go @@ -0,0 +1,95 @@ +package keeper_test + +import ( + "time" + + "github.com/Canto-Network/Canto/v7/x/csr" + "github.com/Canto-Network/Canto/v7/x/csr/types" + "github.com/evmos/ethermint/tests" +) + +func (suite *KeeperTestSuite) TestDefaultGenesis() { + genState := types.DefaultGenesis() + + csr.InitGenesis(suite.ctx, suite.app.CSRKeeper, suite.app.AccountKeeper, *genState) + exported := csr.ExportGenesis(suite.ctx, suite.app.CSRKeeper) + suite.Require().Equal(genState, exported) +} + +func (suite *KeeperTestSuite) TestImportExportGenesisEmpty() { + _, found := suite.app.CSRKeeper.GetTurnstile(suite.ctx) + suite.Require().False(found) + csrs := suite.app.CSRKeeper.GetAllCSRs(suite.ctx) + suite.Require().Empty(csrs) + + genState := csr.ExportGenesis(suite.ctx, suite.app.CSRKeeper) + suite.Require().Nil(genState.TurnstileAddress) + suite.Require().Empty(genState.Csrs) + + // Copy genState to genState2 and init with it + var genState2 types.GenesisState + bz := suite.app.AppCodec().MustMarshalJSON(genState) + suite.app.AppCodec().MustUnmarshalJSON(bz, &genState2) + csr.InitGenesis(suite.ctx, suite.app.CSRKeeper, suite.app.AccountKeeper, genState2) + + _, found = suite.app.CSRKeeper.GetTurnstile(suite.ctx) + suite.Require().False(found) + csrs = suite.app.CSRKeeper.GetAllCSRs(suite.ctx) + suite.Require().Empty(csrs) + genState3 := csr.ExportGenesis(suite.ctx, suite.app.CSRKeeper) + suite.Equal(*genState, genState2) + suite.Equal(genState2, *genState3) + suite.Require().Nil(genState3.TurnstileAddress) + suite.Require().Empty(genState3.Csrs) +} + +func (suite *KeeperTestSuite) TestInitExportGenesis() { + expGenesis := types.DefaultGenesis() + + csr.InitGenesis(suite.ctx, suite.app.CSRKeeper, suite.app.AccountKeeper, *expGenesis) + genState := csr.ExportGenesis(suite.ctx, suite.app.CSRKeeper) + suite.Require().Equal(expGenesis, genState) + + bz := suite.app.AppCodec().MustMarshalJSON(genState) + + var genState2 types.GenesisState + suite.app.AppCodec().MustUnmarshalJSON(bz, &genState2) + csr.InitGenesis(suite.ctx, suite.app.CSRKeeper, suite.app.AccountKeeper, genState2) + genState3 := csr.ExportGenesis(suite.ctx, suite.app.CSRKeeper) + + suite.Require().Equal(*genState, genState2) + suite.Require().Equal(genState2, *genState3) +} + +func (suite *KeeperTestSuite) TestImportExportGenesis() { + t, _ := time.Parse(time.RFC3339, "2022-01-01T00:00:00Z") + suite.ctx = suite.ctx.WithBlockHeight(1).WithBlockTime(t) + + numberCSRs := 10 + csrs := GenerateCSRs(numberCSRs) + for _, csr := range csrs { + suite.app.CSRKeeper.SetCSR(suite.ctx, csr) + } + + turnstileAddress := tests.GenerateAddress() + suite.app.CSRKeeper.SetTurnstile(suite.ctx, turnstileAddress) + + genState := csr.ExportGenesis(suite.ctx, suite.app.CSRKeeper) + bz := suite.app.AppCodec().MustMarshalJSON(genState) + + // Copy genState to genState2 and init with it + var genState2 types.GenesisState + suite.app.AppCodec().MustUnmarshalJSON(bz, &genState2) + csr.InitGenesis(suite.ctx, suite.app.CSRKeeper, suite.app.AccountKeeper, genState2) + exported := csr.ExportGenesis(suite.ctx, suite.app.CSRKeeper) + suite.Equal(*genState, *exported) + + suite.ctx = suite.ctx.WithBlockHeight(1).WithBlockTime(t) + + c := suite.app.CSRKeeper.GetAllCSRs(suite.ctx) + suite.Equal(csrs, c) + + ta, found := suite.app.CSRKeeper.GetTurnstile(suite.ctx) + suite.True(found) + suite.Equal(turnstileAddress, ta) +} diff --git a/x/csr/types/genesis.go b/x/csr/types/genesis.go index 521cc943e..783f5211c 100644 --- a/x/csr/types/genesis.go +++ b/x/csr/types/genesis.go @@ -5,8 +5,14 @@ const DefaultIndex uint64 = 1 // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { + return NewGenesisState(DefaultParams(), []CSR{}, nil) +} + +func NewGenesisState(params Params, csrs []CSR, turnstileAddress []byte) *GenesisState { return &GenesisState{ - Params: DefaultParams(), + Params: params, + Csrs: csrs, + TurnstileAddress: turnstileAddress, } } diff --git a/x/csr/types/genesis.pb.go b/x/csr/types/genesis.pb.go index 5c91271b8..830cc6add 100644 --- a/x/csr/types/genesis.pb.go +++ b/x/csr/types/genesis.pb.go @@ -26,7 +26,9 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the csr module's genesis state. type GenesisState struct { // params defines all of the parameters of the module - Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"` + Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"` + Csrs []CSR `protobuf:"bytes,2,rep,name=csrs,proto3" json:"csrs"` + TurnstileAddress []byte `protobuf:"bytes,3,opt,name=turnstile_address,json=turnstileAddress,proto3" json:"turnstile_address,omitempty"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -69,6 +71,20 @@ func (m *GenesisState) GetParams() Params { return Params{} } +func (m *GenesisState) GetCsrs() []CSR { + if m != nil { + return m.Csrs + } + return nil +} + +func (m *GenesisState) GetTurnstileAddress() []byte { + if m != nil { + return m.TurnstileAddress + } + return nil +} + func init() { proto.RegisterType((*GenesisState)(nil), "canto.csr.v1.GenesisState") } @@ -76,20 +92,24 @@ func init() { func init() { proto.RegisterFile("canto/csr/v1/genesis.proto", fileDescriptor_4c1065f59845b427) } var fileDescriptor_4c1065f59845b427 = []byte{ - // 200 bytes of a gzipped FileDescriptorProto + // 268 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4a, 0x4e, 0xcc, 0x2b, 0xc9, 0xd7, 0x4f, 0x2e, 0x2e, 0xd2, 0x2f, 0x33, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x01, 0xcb, 0xe9, 0x25, 0x17, 0x17, 0xe9, 0x95, 0x19, 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x25, 0xf4, 0x41, 0x2c, 0x88, 0x1a, 0x29, 0x49, - 0x14, 0xfd, 0x05, 0x89, 0x45, 0x89, 0xb9, 0x50, 0xed, 0x4a, 0x4e, 0x5c, 0x3c, 0xee, 0x10, 0xf3, - 0x82, 0x4b, 0x12, 0x4b, 0x52, 0x85, 0x8c, 0xb8, 0xd8, 0x20, 0xf2, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, - 0xdc, 0x46, 0x22, 0x7a, 0xc8, 0xe6, 0xeb, 0x05, 0x80, 0xe5, 0x9c, 0x58, 0x4e, 0xdc, 0x93, 0x67, - 0x08, 0x82, 0xaa, 0x74, 0x72, 0x3f, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, - 0xe4, 0x18, 0x27, 0x3c, 0x96, 0x63, 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6, 0x63, 0x39, 0x86, 0x28, - 0xdd, 0xf4, 0xcc, 0x92, 0x8c, 0xd2, 0x24, 0xbd, 0xe4, 0xfc, 0x5c, 0x7d, 0x67, 0x90, 0x39, 0xba, - 0x7e, 0xa9, 0x25, 0xe5, 0xf9, 0x45, 0xd9, 0x10, 0x9e, 0x7e, 0x99, 0xb9, 0x7e, 0x05, 0xd8, 0x59, - 0x25, 0x95, 0x05, 0xa9, 0xc5, 0x49, 0x6c, 0x60, 0x37, 0x19, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, - 0x5d, 0xe7, 0x23, 0x60, 0xf0, 0x00, 0x00, 0x00, + 0x14, 0xfd, 0x05, 0x89, 0x45, 0x89, 0xb9, 0x50, 0xed, 0x52, 0x62, 0x28, 0x52, 0x20, 0x53, 0xc0, + 0xe2, 0x4a, 0x73, 0x18, 0xb9, 0x78, 0xdc, 0x21, 0x16, 0x05, 0x97, 0x24, 0x96, 0xa4, 0x0a, 0x19, + 0x71, 0xb1, 0x41, 0x34, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0x70, 0x1b, 0x89, 0xe8, 0x21, 0x5b, 0xac, + 0x17, 0x00, 0x96, 0x73, 0x62, 0x39, 0x71, 0x4f, 0x9e, 0x21, 0x08, 0xaa, 0x52, 0x48, 0x9b, 0x8b, + 0x25, 0xb9, 0xb8, 0xa8, 0x58, 0x82, 0x49, 0x81, 0x59, 0x83, 0xdb, 0x48, 0x10, 0x55, 0x87, 0x73, + 0x70, 0x10, 0x54, 0x39, 0x58, 0x91, 0x90, 0x36, 0x97, 0x60, 0x49, 0x69, 0x51, 0x5e, 0x71, 0x49, + 0x66, 0x4e, 0x6a, 0x7c, 0x62, 0x4a, 0x4a, 0x51, 0x6a, 0x71, 0xb1, 0x04, 0xb3, 0x02, 0xa3, 0x06, + 0x4f, 0x90, 0x00, 0x5c, 0xc2, 0x11, 0x22, 0xee, 0xe4, 0x7e, 0xe2, 0x91, 0x1c, 0xe3, 0x85, 0x47, + 0x72, 0x8c, 0x0f, 0x1e, 0xc9, 0x31, 0x4e, 0x78, 0x2c, 0xc7, 0x70, 0xe1, 0xb1, 0x1c, 0xc3, 0x8d, + 0xc7, 0x72, 0x0c, 0x51, 0xba, 0xe9, 0x99, 0x25, 0x19, 0xa5, 0x49, 0x7a, 0xc9, 0xf9, 0xb9, 0xfa, + 0xce, 0x20, 0xfb, 0x74, 0xfd, 0x52, 0x4b, 0xca, 0xf3, 0x8b, 0xb2, 0x21, 0x3c, 0xfd, 0x32, 0x73, + 0xfd, 0x0a, 0xb0, 0x77, 0x4b, 0x2a, 0x0b, 0x52, 0x8b, 0x93, 0xd8, 0xc0, 0xde, 0x35, 0x06, 0x04, + 0x00, 0x00, 0xff, 0xff, 0xf7, 0xd8, 0x67, 0x34, 0x63, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -112,6 +132,27 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.TurnstileAddress) > 0 { + i -= len(m.TurnstileAddress) + copy(dAtA[i:], m.TurnstileAddress) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.TurnstileAddress))) + i-- + dAtA[i] = 0x1a + } + if len(m.Csrs) > 0 { + for iNdEx := len(m.Csrs) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Csrs[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + } { size, err := m.Params.MarshalToSizedBuffer(dAtA[:i]) if err != nil { @@ -144,6 +185,16 @@ func (m *GenesisState) Size() (n int) { _ = l l = m.Params.Size() n += 1 + l + sovGenesis(uint64(l)) + if len(m.Csrs) > 0 { + for _, e := range m.Csrs { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } + l = len(m.TurnstileAddress) + if l > 0 { + n += 1 + l + sovGenesis(uint64(l)) + } return n } @@ -215,6 +266,74 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Csrs", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Csrs = append(m.Csrs, CSR{}) + if err := m.Csrs[len(m.Csrs)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field TurnstileAddress", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.TurnstileAddress = append(m.TurnstileAddress[:0], dAtA[iNdEx:postIndex]...) + if m.TurnstileAddress == nil { + m.TurnstileAddress = []byte{} + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) From b3103b45128459802f18e561ba1958397a255033 Mon Sep 17 00:00:00 2001 From: poorphd Date: Mon, 15 Jul 2024 17:45:14 +0900 Subject: [PATCH 2/2] fix: genesis import/export for csr --- api/canto/csr/v1/genesis.pulsar.go | 36 ++++++++++++++---------------- proto/canto/csr/v1/genesis.proto | 2 +- x/csr/genesis.go | 9 ++++---- x/csr/keeper/genesis_test.go | 4 ++-- x/csr/types/genesis.go | 4 ++-- x/csr/types/genesis.pb.go | 24 +++++++++----------- 6 files changed, 37 insertions(+), 42 deletions(-) diff --git a/api/canto/csr/v1/genesis.pulsar.go b/api/canto/csr/v1/genesis.pulsar.go index ecdbb1aa8..0dcef6da1 100644 --- a/api/canto/csr/v1/genesis.pulsar.go +++ b/api/canto/csr/v1/genesis.pulsar.go @@ -156,8 +156,8 @@ func (x *fastReflection_GenesisState) Range(f func(protoreflect.FieldDescriptor, return } } - if len(x.TurnstileAddress) != 0 { - value := protoreflect.ValueOfBytes(x.TurnstileAddress) + if x.TurnstileAddress != "" { + value := protoreflect.ValueOfString(x.TurnstileAddress) if !f(fd_GenesisState_turnstile_address, value) { return } @@ -182,7 +182,7 @@ func (x *fastReflection_GenesisState) Has(fd protoreflect.FieldDescriptor) bool case "canto.csr.v1.GenesisState.csrs": return len(x.Csrs) != 0 case "canto.csr.v1.GenesisState.turnstile_address": - return len(x.TurnstileAddress) != 0 + return x.TurnstileAddress != "" default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -204,7 +204,7 @@ func (x *fastReflection_GenesisState) Clear(fd protoreflect.FieldDescriptor) { case "canto.csr.v1.GenesisState.csrs": x.Csrs = nil case "canto.csr.v1.GenesisState.turnstile_address": - x.TurnstileAddress = nil + x.TurnstileAddress = "" default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -232,7 +232,7 @@ func (x *fastReflection_GenesisState) Get(descriptor protoreflect.FieldDescripto return protoreflect.ValueOfList(listValue) case "canto.csr.v1.GenesisState.turnstile_address": value := x.TurnstileAddress - return protoreflect.ValueOfBytes(value) + return protoreflect.ValueOfString(value) default: if descriptor.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -260,7 +260,7 @@ func (x *fastReflection_GenesisState) Set(fd protoreflect.FieldDescriptor, value clv := lv.(*_GenesisState_2_list) x.Csrs = *clv.list case "canto.csr.v1.GenesisState.turnstile_address": - x.TurnstileAddress = value.Bytes() + x.TurnstileAddress = value.Interface().(string) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -314,7 +314,7 @@ func (x *fastReflection_GenesisState) NewField(fd protoreflect.FieldDescriptor) list := []*CSR{} return protoreflect.ValueOfList(&_GenesisState_2_list{list: &list}) case "canto.csr.v1.GenesisState.turnstile_address": - return protoreflect.ValueOfBytes(nil) + return protoreflect.ValueOfString("") default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: canto.csr.v1.GenesisState")) @@ -587,7 +587,7 @@ func (x *fastReflection_GenesisState) ProtoMethods() *protoiface.Methods { if wireType != 2 { return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field TurnstileAddress", wireType) } - var byteLen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow @@ -597,25 +597,23 @@ func (x *fastReflection_GenesisState) ProtoMethods() *protoiface.Methods { } b := dAtA[iNdEx] iNdEx++ - byteLen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if byteLen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength } - postIndex := iNdEx + byteLen + postIndex := iNdEx + intStringLen if postIndex < 0 { return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength } if postIndex > l { return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF } - x.TurnstileAddress = append(x.TurnstileAddress[:0], dAtA[iNdEx:postIndex]...) - if x.TurnstileAddress == nil { - x.TurnstileAddress = []byte{} - } + x.TurnstileAddress = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex @@ -674,7 +672,7 @@ type GenesisState struct { // params defines all of the parameters of the module Params *Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params,omitempty"` Csrs []*CSR `protobuf:"bytes,2,rep,name=csrs,proto3" json:"csrs,omitempty"` - TurnstileAddress []byte `protobuf:"bytes,3,opt,name=turnstile_address,json=turnstileAddress,proto3" json:"turnstile_address,omitempty"` + TurnstileAddress string `protobuf:"bytes,3,opt,name=turnstile_address,json=turnstileAddress,proto3" json:"turnstile_address,omitempty"` } func (x *GenesisState) Reset() { @@ -711,11 +709,11 @@ func (x *GenesisState) GetCsrs() []*CSR { return nil } -func (x *GenesisState) GetTurnstileAddress() []byte { +func (x *GenesisState) GetTurnstileAddress() string { if x != nil { return x.TurnstileAddress } - return nil + return "" } var File_canto_csr_v1_genesis_proto protoreflect.FileDescriptor @@ -736,7 +734,7 @@ var file_canto_csr_v1_genesis_proto_rawDesc = []byte{ 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x63, 0x61, 0x6e, 0x74, 0x6f, 0x2e, 0x63, 0x73, 0x72, 0x2e, 0x76, 0x31, 0x2e, 0x43, 0x53, 0x52, 0x42, 0x04, 0xc8, 0xde, 0x1f, 0x00, 0x52, 0x04, 0x63, 0x73, 0x72, 0x73, 0x12, 0x2b, 0x0a, 0x11, 0x74, 0x75, 0x72, 0x6e, 0x73, 0x74, 0x69, - 0x6c, 0x65, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, + 0x6c, 0x65, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x10, 0x74, 0x75, 0x72, 0x6e, 0x73, 0x74, 0x69, 0x6c, 0x65, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x42, 0x97, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x61, 0x6e, 0x74, 0x6f, 0x2e, 0x63, 0x73, 0x72, 0x2e, 0x76, 0x31, 0x42, 0x0c, 0x47, 0x65, 0x6e, 0x65, 0x73, 0x69, 0x73, diff --git a/proto/canto/csr/v1/genesis.proto b/proto/canto/csr/v1/genesis.proto index a00d78331..827d97456 100644 --- a/proto/canto/csr/v1/genesis.proto +++ b/proto/canto/csr/v1/genesis.proto @@ -12,5 +12,5 @@ message GenesisState { // params defines all of the parameters of the module Params params = 1 [ (gogoproto.nullable) = false ]; repeated CSR csrs = 2 [ (gogoproto.nullable) = false ]; - bytes turnstile_address = 3; + string turnstile_address = 3; } diff --git a/x/csr/genesis.go b/x/csr/genesis.go index 9fe53c9a5..b704827fa 100644 --- a/x/csr/genesis.go +++ b/x/csr/genesis.go @@ -17,8 +17,9 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, accountKeeper authkeeper.Acco for _, csr := range genState.Csrs { k.SetCSR(ctx, csr) } - if genState.TurnstileAddress != nil { - k.SetTurnstile(ctx, common.BytesToAddress(genState.TurnstileAddress)) + if genState.TurnstileAddress != "" { + turnstileAddress := common.HexToAddress(genState.TurnstileAddress) + k.SetTurnstile(ctx, turnstileAddress) } // make sure that the csr module account is set on genesis if acc := accountKeeper.GetModuleAccount(ctx, types.ModuleName); acc == nil { @@ -41,9 +42,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { turnstileAddr, ok := k.GetTurnstile(ctx) if ok { - genesis.TurnstileAddress = turnstileAddr.Bytes() - } else { - genesis.TurnstileAddress = nil + genesis.TurnstileAddress = turnstileAddr.String() } return genesis diff --git a/x/csr/keeper/genesis_test.go b/x/csr/keeper/genesis_test.go index e01b14635..86e0ee76b 100644 --- a/x/csr/keeper/genesis_test.go +++ b/x/csr/keeper/genesis_test.go @@ -23,7 +23,7 @@ func (suite *KeeperTestSuite) TestImportExportGenesisEmpty() { suite.Require().Empty(csrs) genState := csr.ExportGenesis(suite.ctx, suite.app.CSRKeeper) - suite.Require().Nil(genState.TurnstileAddress) + suite.Require().Equal("", genState.TurnstileAddress) suite.Require().Empty(genState.Csrs) // Copy genState to genState2 and init with it @@ -39,7 +39,7 @@ func (suite *KeeperTestSuite) TestImportExportGenesisEmpty() { genState3 := csr.ExportGenesis(suite.ctx, suite.app.CSRKeeper) suite.Equal(*genState, genState2) suite.Equal(genState2, *genState3) - suite.Require().Nil(genState3.TurnstileAddress) + suite.Require().Equal("", genState3.TurnstileAddress) suite.Require().Empty(genState3.Csrs) } diff --git a/x/csr/types/genesis.go b/x/csr/types/genesis.go index 783f5211c..04abf8115 100644 --- a/x/csr/types/genesis.go +++ b/x/csr/types/genesis.go @@ -5,10 +5,10 @@ const DefaultIndex uint64 = 1 // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { - return NewGenesisState(DefaultParams(), []CSR{}, nil) + return NewGenesisState(DefaultParams(), []CSR{}, "") } -func NewGenesisState(params Params, csrs []CSR, turnstileAddress []byte) *GenesisState { +func NewGenesisState(params Params, csrs []CSR, turnstileAddress string) *GenesisState { return &GenesisState{ Params: params, Csrs: csrs, diff --git a/x/csr/types/genesis.pb.go b/x/csr/types/genesis.pb.go index 830cc6add..307d60c83 100644 --- a/x/csr/types/genesis.pb.go +++ b/x/csr/types/genesis.pb.go @@ -28,7 +28,7 @@ type GenesisState struct { // params defines all of the parameters of the module Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"` Csrs []CSR `protobuf:"bytes,2,rep,name=csrs,proto3" json:"csrs"` - TurnstileAddress []byte `protobuf:"bytes,3,opt,name=turnstile_address,json=turnstileAddress,proto3" json:"turnstile_address,omitempty"` + TurnstileAddress string `protobuf:"bytes,3,opt,name=turnstile_address,json=turnstileAddress,proto3" json:"turnstile_address,omitempty"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -78,11 +78,11 @@ func (m *GenesisState) GetCsrs() []CSR { return nil } -func (m *GenesisState) GetTurnstileAddress() []byte { +func (m *GenesisState) GetTurnstileAddress() string { if m != nil { return m.TurnstileAddress } - return nil + return "" } func init() { @@ -104,12 +104,12 @@ var fileDescriptor_4c1065f59845b427 = []byte{ 0x25, 0xb9, 0xb8, 0xa8, 0x58, 0x82, 0x49, 0x81, 0x59, 0x83, 0xdb, 0x48, 0x10, 0x55, 0x87, 0x73, 0x70, 0x10, 0x54, 0x39, 0x58, 0x91, 0x90, 0x36, 0x97, 0x60, 0x49, 0x69, 0x51, 0x5e, 0x71, 0x49, 0x66, 0x4e, 0x6a, 0x7c, 0x62, 0x4a, 0x4a, 0x51, 0x6a, 0x71, 0xb1, 0x04, 0xb3, 0x02, 0xa3, 0x06, - 0x4f, 0x90, 0x00, 0x5c, 0xc2, 0x11, 0x22, 0xee, 0xe4, 0x7e, 0xe2, 0x91, 0x1c, 0xe3, 0x85, 0x47, + 0x67, 0x90, 0x00, 0x5c, 0xc2, 0x11, 0x22, 0xee, 0xe4, 0x7e, 0xe2, 0x91, 0x1c, 0xe3, 0x85, 0x47, 0x72, 0x8c, 0x0f, 0x1e, 0xc9, 0x31, 0x4e, 0x78, 0x2c, 0xc7, 0x70, 0xe1, 0xb1, 0x1c, 0xc3, 0x8d, 0xc7, 0x72, 0x0c, 0x51, 0xba, 0xe9, 0x99, 0x25, 0x19, 0xa5, 0x49, 0x7a, 0xc9, 0xf9, 0xb9, 0xfa, 0xce, 0x20, 0xfb, 0x74, 0xfd, 0x52, 0x4b, 0xca, 0xf3, 0x8b, 0xb2, 0x21, 0x3c, 0xfd, 0x32, 0x73, 0xfd, 0x0a, 0xb0, 0x77, 0x4b, 0x2a, 0x0b, 0x52, 0x8b, 0x93, 0xd8, 0xc0, 0xde, 0x35, 0x06, 0x04, - 0x00, 0x00, 0xff, 0xff, 0xf7, 0xd8, 0x67, 0x34, 0x63, 0x01, 0x00, 0x00, + 0x00, 0x00, 0xff, 0xff, 0x5f, 0x02, 0xf6, 0x4c, 0x63, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -304,7 +304,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field TurnstileAddress", wireType) } - var byteLen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowGenesis @@ -314,25 +314,23 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - byteLen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if byteLen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthGenesis } - postIndex := iNdEx + byteLen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthGenesis } if postIndex > l { return io.ErrUnexpectedEOF } - m.TurnstileAddress = append(m.TurnstileAddress[:0], dAtA[iNdEx:postIndex]...) - if m.TurnstileAddress == nil { - m.TurnstileAddress = []byte{} - } + m.TurnstileAddress = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex