From e30a99d8a7d5e29ea41738dc5e7ec1c0591a6d60 Mon Sep 17 00:00:00 2001 From: Jayden Lee <41176085+tkxkd0159@users.noreply.github.com> Date: Thu, 2 May 2024 21:12:38 +0900 Subject: [PATCH] feat: addVote --- docs/core/proto-docs.md | 18 ++ proto/lbm/fbridge/v1/event.proto | 9 + x/fbridge/keeper/auth.go | 47 ++++- x/fbridge/keeper/genesis.go | 28 +++ x/fbridge/keeper/msg_server.go | 25 ++- x/fbridge/types/errors.go | 9 + x/fbridge/types/event.pb.go | 299 ++++++++++++++++++++++++++++--- x/fbridge/types/keys.go | 15 +- 8 files changed, 414 insertions(+), 36 deletions(-) create mode 100644 x/fbridge/types/errors.go diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index 66c8bc1cbd..2ed7c9cc1d 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -767,6 +767,7 @@ - [VoteOption](#lbm.fbridge.v1.VoteOption) - [lbm/fbridge/v1/event.proto](#lbm/fbridge/v1/event.proto) + - [EventAddVoteForRole](#lbm.fbridge.v1.EventAddVoteForRole) - [EventClaim](#lbm.fbridge.v1.EventClaim) - [EventConfirmProvision](#lbm.fbridge.v1.EventConfirmProvision) - [EventProvision](#lbm.fbridge.v1.EventProvision) @@ -11560,6 +11561,23 @@ VoteOption enumerates the valid vote options for a given role proposal. + + +### EventAddVoteForRole + + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `voter` | [string](#string) | | the voter address | +| `proposal_id` | [uint64](#uint64) | | the role proposal id | +| `option` | [VoteOption](#lbm.fbridge.v1.VoteOption) | | the vote option | + + + + + + ### EventClaim diff --git a/proto/lbm/fbridge/v1/event.proto b/proto/lbm/fbridge/v1/event.proto index 2d505eaf1c..324a2db2a2 100644 --- a/proto/lbm/fbridge/v1/event.proto +++ b/proto/lbm/fbridge/v1/event.proto @@ -21,6 +21,15 @@ message EventSuggestRole { RoleProposal proposal = 1 [(gogoproto.nullable) = false]; } +message EventAddVoteForRole { + // the voter address + string voter = 1; + // the role proposal id + uint64 proposal_id = 2; + // the vote option + VoteOption option = 3; +} + message EventProvision { // the sequence number of the bridge request uint64 seq = 1; diff --git a/x/fbridge/keeper/auth.go b/x/fbridge/keeper/auth.go index 66d3694131..9477084df0 100644 --- a/x/fbridge/keeper/auth.go +++ b/x/fbridge/keeper/auth.go @@ -36,6 +36,25 @@ func (k Keeper) RegisterRoleProposal(ctx sdk.Context, proposer, target sdk.AccAd return proposal, nil } +func (k Keeper) addVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress, option types.VoteOption) error { + proposal, found := k.GetRoleProposal(ctx, proposalID) + if !found { + return types.ErrUnknownProposal.Wrapf("#%d not found", proposalID) + } + + if ctx.BlockTime().After(proposal.ExpiredAt) { + return types.ErrInactiveProposal.Wrapf("#%d already expired", proposalID) + } + + if err := k.IsValidVoteOption(option); err != nil { + return err + } + + k.setVote(ctx, proposalID, voter, option) + + return nil +} + func (k Keeper) IsValidRole(role types.Role) error { switch role { case types.RoleGuardian, types.RoleOperator, types.RoleJudge: @@ -45,6 +64,15 @@ func (k Keeper) IsValidRole(role types.Role) error { return errors.New("unsupported role") } +func (k Keeper) IsValidVoteOption(option types.VoteOption) error { + switch option { + case types.OptionYes, types.OptionNo: + return nil + } + + return errors.New("unsupported vote option") +} + func (k Keeper) setNextProposalID(ctx sdk.Context, seq uint64) { store := ctx.KVStore(k.storeKey) bz := make([]byte, 8) @@ -112,9 +140,25 @@ func (k Keeper) GetProposals(ctx sdk.Context) (proposals []types.RoleProposal) { return } -func (k Keeper) setRole(ctx sdk.Context, role types.Role, addr sdk.AccAddress) { +func (k Keeper) setVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress, option types.VoteOption) { store := ctx.KVStore(k.storeKey) + bz := make([]byte, 4) + binary.BigEndian.PutUint32(bz, uint32(option)) + store.Set(types.VoterVoteKey(proposalID, voter), bz) +} +func (k Keeper) GetVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) (types.VoteOption, error) { + store := ctx.KVStore(k.storeKey) + bz := store.Get(types.VoterVoteKey(proposalID, voter)) + if bz == nil { + return types.OptionEmpty, types.ErrUnknownVote + } + + return types.VoteOption(binary.BigEndian.Uint32(bz)), nil +} + +func (k Keeper) setRole(ctx sdk.Context, role types.Role, addr sdk.AccAddress) { + store := ctx.KVStore(k.storeKey) bz := make([]byte, 4) binary.BigEndian.PutUint32(bz, uint32(role)) store.Set(types.RoleKey(addr), bz) @@ -122,7 +166,6 @@ func (k Keeper) setRole(ctx sdk.Context, role types.Role, addr sdk.AccAddress) { func (k Keeper) GetRole(ctx sdk.Context, addr sdk.AccAddress) types.Role { store := ctx.KVStore(k.storeKey) - bz := store.Get(types.RoleKey(addr)) if bz == nil { return types.RoleEmpty diff --git a/x/fbridge/keeper/genesis.go b/x/fbridge/keeper/genesis.go index e98a5aef5a..1a30f28c20 100644 --- a/x/fbridge/keeper/genesis.go +++ b/x/fbridge/keeper/genesis.go @@ -1,6 +1,7 @@ package keeper import ( + "encoding/binary" sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fbridge/types" ) @@ -28,5 +29,32 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { NextRoleProposalId: k.GetNextProposalID(ctx), RoleMetadata: k.GetRoleMetadata(ctx), RoleProposals: k.GetProposals(ctx), + Votes: k.GetAllVotes(ctx), } } + +// IterateVotes iterates over the all the votes for role proposals and performs a callback function +func (k Keeper) IterateVotes(ctx sdk.Context, cb func(proposal types.Vote) (stop bool)) { + store := ctx.KVStore(k.storeKey) + + iterator := sdk.KVStorePrefixIterator(store, types.KeyProposalPrefix) + defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { + id, voter := types.SplitVoterVoteKey(iterator.Key()) + opt := types.VoteOption(binary.BigEndian.Uint32(iterator.Value())) + v := types.Vote{ProposalId: id, Voter: voter.String(), Option: opt} + k.cdc.MustUnmarshal(iterator.Value(), &v) + if cb(v) { + break + } + } +} + +// GetAllVotes returns all the votes from the store +func (k Keeper) GetAllVotes(ctx sdk.Context) (votes []types.Vote) { + k.IterateVotes(ctx, func(vote types.Vote) bool { + votes = append(votes, vote) + return false + }) + return +} diff --git a/x/fbridge/keeper/msg_server.go b/x/fbridge/keeper/msg_server.go index c4309f4eaf..f5f3ed0d28 100644 --- a/x/fbridge/keeper/msg_server.go +++ b/x/fbridge/keeper/msg_server.go @@ -103,7 +103,30 @@ func (m msgServer) SuggestRole(goCtx context.Context, msg *types.MsgSuggestRole) } func (m msgServer) AddVoteForRole(goCtx context.Context, msg *types.MsgAddVoteForRole) (*types.MsgAddVoteForRoleResponse, error) { - panic("implement me") + ctx := sdk.UnwrapSDKContext(goCtx) + + voter, err := sdk.AccAddressFromBech32(msg.From) + if err != nil { + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid proposer address (%s)", err) + } + + if err := m.IsValidVoteOption(msg.Option); err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error()) + } + + if err := m.addVote(ctx, msg.ProposalId, voter, msg.Option); err != nil { + return nil, err + } + + if err := ctx.EventManager().EmitTypedEvent(&types.EventAddVoteForRole{ + Voter: msg.From, + ProposalId: msg.ProposalId, + Option: msg.Option, + }); err != nil { + panic(err) + } + + return &types.MsgAddVoteForRoleResponse{}, nil } func (m msgServer) Halt(ctx context.Context, msg *types.MsgHalt) (*types.MsgHaltResponse, error) { diff --git a/x/fbridge/types/errors.go b/x/fbridge/types/errors.go new file mode 100644 index 0000000000..bdbe596ca9 --- /dev/null +++ b/x/fbridge/types/errors.go @@ -0,0 +1,9 @@ +package types + +import sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + +var ( + ErrUnknownProposal = sdkerrors.Register(ModuleName, 2, "unknown proposal") + ErrInactiveProposal = sdkerrors.Register(ModuleName, 3, "inactive proposal") + ErrUnknownVote = sdkerrors.Register(ModuleName, 4, "unknown vote") +) diff --git a/x/fbridge/types/event.pb.go b/x/fbridge/types/event.pb.go index 044154a694..e82390f62a 100644 --- a/x/fbridge/types/event.pb.go +++ b/x/fbridge/types/event.pb.go @@ -139,6 +139,69 @@ func (m *EventSuggestRole) GetProposal() RoleProposal { return RoleProposal{} } +type EventAddVoteForRole struct { + // the voter address + Voter string `protobuf:"bytes,1,opt,name=voter,proto3" json:"voter,omitempty"` + // the role proposal id + ProposalId uint64 `protobuf:"varint,2,opt,name=proposal_id,json=proposalId,proto3" json:"proposal_id,omitempty"` + // the vote option + Option VoteOption `protobuf:"varint,3,opt,name=option,proto3,enum=lbm.fbridge.v1.VoteOption" json:"option,omitempty"` +} + +func (m *EventAddVoteForRole) Reset() { *m = EventAddVoteForRole{} } +func (m *EventAddVoteForRole) String() string { return proto.CompactTextString(m) } +func (*EventAddVoteForRole) ProtoMessage() {} +func (*EventAddVoteForRole) Descriptor() ([]byte, []int) { + return fileDescriptor_a36aa6e56f2275b8, []int{2} +} +func (m *EventAddVoteForRole) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *EventAddVoteForRole) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_EventAddVoteForRole.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *EventAddVoteForRole) XXX_Merge(src proto.Message) { + xxx_messageInfo_EventAddVoteForRole.Merge(m, src) +} +func (m *EventAddVoteForRole) XXX_Size() int { + return m.Size() +} +func (m *EventAddVoteForRole) XXX_DiscardUnknown() { + xxx_messageInfo_EventAddVoteForRole.DiscardUnknown(m) +} + +var xxx_messageInfo_EventAddVoteForRole proto.InternalMessageInfo + +func (m *EventAddVoteForRole) GetVoter() string { + if m != nil { + return m.Voter + } + return "" +} + +func (m *EventAddVoteForRole) GetProposalId() uint64 { + if m != nil { + return m.ProposalId + } + return 0 +} + +func (m *EventAddVoteForRole) GetOption() VoteOption { + if m != nil { + return m.Option + } + return OptionEmpty +} + type EventProvision struct { // the sequence number of the bridge request Seq uint64 `protobuf:"varint,1,opt,name=seq,proto3" json:"seq,omitempty"` @@ -156,7 +219,7 @@ func (m *EventProvision) Reset() { *m = EventProvision{} } func (m *EventProvision) String() string { return proto.CompactTextString(m) } func (*EventProvision) ProtoMessage() {} func (*EventProvision) Descriptor() ([]byte, []int) { - return fileDescriptor_a36aa6e56f2275b8, []int{2} + return fileDescriptor_a36aa6e56f2275b8, []int{3} } func (m *EventProvision) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -229,7 +292,7 @@ func (m *EventConfirmProvision) Reset() { *m = EventConfirmProvision{} } func (m *EventConfirmProvision) String() string { return proto.CompactTextString(m) } func (*EventConfirmProvision) ProtoMessage() {} func (*EventConfirmProvision) Descriptor() ([]byte, []int) { - return fileDescriptor_a36aa6e56f2275b8, []int{3} + return fileDescriptor_a36aa6e56f2275b8, []int{4} } func (m *EventConfirmProvision) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -280,7 +343,7 @@ func (m *EventClaim) Reset() { *m = EventClaim{} } func (m *EventClaim) String() string { return proto.CompactTextString(m) } func (*EventClaim) ProtoMessage() {} func (*EventClaim) Descriptor() ([]byte, []int) { - return fileDescriptor_a36aa6e56f2275b8, []int{4} + return fileDescriptor_a36aa6e56f2275b8, []int{5} } func (m *EventClaim) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -340,6 +403,7 @@ func (m *EventClaim) GetAmount() string { func init() { proto.RegisterType((*EventTransfer)(nil), "lbm.fbridge.v1.EventTransfer") proto.RegisterType((*EventSuggestRole)(nil), "lbm.fbridge.v1.EventSuggestRole") + proto.RegisterType((*EventAddVoteForRole)(nil), "lbm.fbridge.v1.EventAddVoteForRole") proto.RegisterType((*EventProvision)(nil), "lbm.fbridge.v1.EventProvision") proto.RegisterType((*EventConfirmProvision)(nil), "lbm.fbridge.v1.EventConfirmProvision") proto.RegisterType((*EventClaim)(nil), "lbm.fbridge.v1.EventClaim") @@ -348,29 +412,33 @@ func init() { func init() { proto.RegisterFile("lbm/fbridge/v1/event.proto", fileDescriptor_a36aa6e56f2275b8) } var fileDescriptor_a36aa6e56f2275b8 = []byte{ - // 343 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x92, 0xbd, 0x4e, 0xeb, 0x40, - 0x10, 0x85, 0xed, 0x9b, 0xdc, 0x28, 0x77, 0xaf, 0x88, 0x22, 0x0b, 0x90, 0x65, 0x45, 0x26, 0x72, - 0x15, 0x0a, 0x6c, 0x02, 0x3d, 0x45, 0x10, 0x14, 0x54, 0x91, 0xa1, 0xa2, 0xb3, 0x93, 0xb1, 0xb3, - 0xe0, 0xdd, 0x31, 0xbb, 0x1b, 0x0b, 0x5e, 0x81, 0x8a, 0xc7, 0x4a, 0x99, 0x92, 0x0a, 0xa1, 0xe4, - 0x45, 0x90, 0xd7, 0x4e, 0x24, 0x28, 0xe8, 0xd2, 0xcd, 0xd1, 0x39, 0xdf, 0xfc, 0x48, 0x43, 0x9c, - 0x2c, 0x66, 0x41, 0x12, 0x0b, 0x3a, 0x4d, 0x21, 0x28, 0x86, 0x01, 0x14, 0xc0, 0x95, 0x9f, 0x0b, - 0x54, 0x68, 0x75, 0xb2, 0x98, 0xf9, 0xb5, 0xe7, 0x17, 0x43, 0x67, 0x3f, 0xc5, 0x14, 0xb5, 0x15, - 0x94, 0x55, 0x95, 0x72, 0x7a, 0x3f, 0x3a, 0x6c, 0x00, 0xed, 0x7a, 0x8c, 0xec, 0x5d, 0x95, 0x2d, - 0xef, 0x44, 0xc4, 0x65, 0x02, 0xc2, 0xea, 0x92, 0x86, 0x84, 0x27, 0xdb, 0xec, 0x9b, 0x83, 0x66, - 0x58, 0x96, 0xd6, 0x21, 0x69, 0x49, 0xe0, 0x53, 0x10, 0xf6, 0x9f, 0xbe, 0x39, 0xf8, 0x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x00, 0x00, + // 408 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x52, 0x3d, 0x6f, 0xd4, 0x40, + 0x10, 0xf5, 0x12, 0xe7, 0x94, 0x4c, 0xc4, 0x29, 0x32, 0x01, 0x59, 0x56, 0xe4, 0x44, 0xae, 0x42, + 0x81, 0x4d, 0x8e, 0x1e, 0x89, 0x20, 0x22, 0x41, 0x43, 0xb4, 0x20, 0x0a, 0x1a, 0x64, 0x9f, 0xc7, + 0xce, 0x82, 0xbd, 0x63, 0x76, 0xf7, 0x2c, 0xe8, 0xa8, 0xa9, 0xf8, 0x59, 0x29, 0x53, 0x52, 0x21, + 0x74, 0xf7, 0x47, 0x90, 0xd7, 0xf6, 0x49, 0x5c, 0x91, 0xee, 0xba, 0x79, 0x7e, 0x1f, 0xf3, 0xc6, + 0x5a, 0x08, 0xaa, 0xac, 0x4e, 0x8a, 0x4c, 0x89, 0xbc, 0xc4, 0xa4, 0x3d, 0x4f, 0xb0, 0x45, 0x69, + 0xe2, 0x46, 0x91, 0x21, 0x6f, 0x5a, 0x65, 0x75, 0x3c, 0x70, 0x71, 0x7b, 0x1e, 0x1c, 0x95, 0x54, + 0x92, 0xa5, 0x92, 0x6e, 0xea, 0x55, 0xc1, 0xf1, 0x46, 0xc2, 0x68, 0xb0, 0x6c, 0x54, 0xc3, 0xfd, + 0x57, 0x5d, 0xe4, 0x7b, 0x95, 0x4a, 0x5d, 0xa0, 0xf2, 0x0e, 0x61, 0x47, 0xe3, 0x57, 0x9f, 0x9d, + 0xb2, 0x33, 0x97, 0x77, 0xa3, 0xf7, 0x08, 0x26, 0x1a, 0x65, 0x8e, 0xca, 0xbf, 0x77, 0xca, 0xce, + 0xf6, 0xf9, 0x80, 0xbc, 0x00, 0xf6, 0x14, 0xce, 0x51, 0xb4, 0xa8, 0xfc, 0x1d, 0xcb, 0xac, 0x71, + 0xe7, 0x49, 0x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 0x2d, 0x85, 0xb9, 0x5e, 0x64, 0xf1, 0x9c, 0xea, + 0xe4, 0x52, 0x48, 0x3d, 0xbf, 0x16, 0x69, 0x52, 0x0c, 0xc3, 0x13, 0x9d, 0x7f, 0x49, 0xbe, 0xad, + 0x1f, 0xb0, 0xf9, 0xde, 0xa0, 0xce, 0x26, 0xf6, 0xf1, 0x3e, 0xfb, 0x17, 0x00, 0x00, 0xff, 0xff, + 0x96, 0x66, 0x6c, 0xc3, 0x1e, 0x03, 0x00, 0x00, } func (m *EventTransfer) Marshal() (dAtA []byte, err error) { @@ -455,6 +523,46 @@ func (m *EventSuggestRole) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *EventAddVoteForRole) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *EventAddVoteForRole) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *EventAddVoteForRole) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Option != 0 { + i = encodeVarintEvent(dAtA, i, uint64(m.Option)) + i-- + dAtA[i] = 0x18 + } + if m.ProposalId != 0 { + i = encodeVarintEvent(dAtA, i, uint64(m.ProposalId)) + i-- + dAtA[i] = 0x10 + } + if len(m.Voter) > 0 { + i -= len(m.Voter) + copy(dAtA[i:], m.Voter) + i = encodeVarintEvent(dAtA, i, uint64(len(m.Voter))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + func (m *EventProvision) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -634,6 +742,25 @@ func (m *EventSuggestRole) Size() (n int) { return n } +func (m *EventAddVoteForRole) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Voter) + if l > 0 { + n += 1 + l + sovEvent(uint64(l)) + } + if m.ProposalId != 0 { + n += 1 + sovEvent(uint64(m.ProposalId)) + } + if m.Option != 0 { + n += 1 + sovEvent(uint64(m.Option)) + } + return n +} + func (m *EventProvision) Size() (n int) { if m == nil { return 0 @@ -952,6 +1079,126 @@ func (m *EventSuggestRole) Unmarshal(dAtA []byte) error { } return nil } +func (m *EventAddVoteForRole) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: EventAddVoteForRole: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: EventAddVoteForRole: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Voter", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthEvent + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthEvent + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Voter = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field ProposalId", wireType) + } + m.ProposalId = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.ProposalId |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 3: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Option", wireType) + } + m.Option = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Option |= VoteOption(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipEvent(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthEvent + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *EventProvision) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 diff --git a/x/fbridge/types/keys.go b/x/fbridge/types/keys.go index 09d8216e07..4d43b76208 100644 --- a/x/fbridge/types/keys.go +++ b/x/fbridge/types/keys.go @@ -46,13 +46,6 @@ func ProposalKey(proposalID uint64) []byte { return append(KeyProposalPrefix, GetProposalIDBytes(proposalID)...) } -// SplitProposalKey split the proposal key and returns the proposal id -func SplitProposalKey(key []byte) (proposalID uint64) { - kv.AssertKeyLength(key[1:], 8) - - return binary.BigEndian.Uint64(key[1:]) -} - // VotesKey gets the first part of the votes key based on the proposalID func VotesKey(proposalID uint64) []byte { return append(KeyProposalVotePrefix, GetProposalIDBytes(proposalID)...) @@ -63,6 +56,14 @@ func VoterVoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte { return append(VotesKey(proposalID), address.MustLengthPrefix(voterAddr.Bytes())...) } +// SplitVoterVoteKey split the voter key and returns the proposal id and voter address +func SplitVoterVoteKey(key []byte) (uint64, sdk.AccAddress) { + kv.AssertKeyAtLeastLength(key, 11) + proposalID := binary.BigEndian.Uint64(key[1:9]) + voter := sdk.AccAddress(key[10:]) + return proposalID, voter +} + // RoleKey key of a specific role of the address from the store func RoleKey(target sdk.AccAddress) []byte { return append(KeyRolePrefix, address.MustLengthPrefix(target.Bytes())...)