From 34c79a22e9e40ad5fb911cacc9508c82ea0298d2 Mon Sep 17 00:00:00 2001 From: Pratik Date: Sat, 21 Jan 2023 18:16:56 +0530 Subject: [PATCH] enabled ibc on voting module --- app/app.go | 7 + proto/voting/genesis.proto | 9 +- proto/voting/packet.proto | 18 + .../mande-chain/mandechain.voting/index.ts | 6 +- .../module/types/voting/genesis.ts | 34 +- .../module/types/voting/packet.ts | 116 ++++ x/voting/genesis.go | 13 + x/voting/genesis_test.go | 3 + x/voting/keeper/keeper.go | 12 + x/voting/module.go | 2 + x/voting/module_ibc.go | 228 ++++++++ x/voting/module_simulation.go | 1 + x/voting/types/errors.go | 3 + x/voting/types/events_ibc.go | 11 + x/voting/types/genesis.go | 5 + x/voting/types/genesis.pb.go | 121 +++-- x/voting/types/genesis_test.go | 2 +- x/voting/types/keys.go | 11 + x/voting/types/packet.pb.go | 499 ++++++++++++++++++ 19 files changed, 1053 insertions(+), 48 deletions(-) create mode 100644 proto/voting/packet.proto create mode 100644 vue/src/store/generated/mande-chain/mandechain.voting/module/types/voting/packet.ts create mode 100644 x/voting/module_ibc.go create mode 100644 x/voting/types/events_ibc.go create mode 100644 x/voting/types/packet.pb.go diff --git a/app/app.go b/app/app.go index 2b7d5a6..383e039 100644 --- a/app/app.go +++ b/app/app.go @@ -234,6 +234,7 @@ type App struct { ScopedTransferKeeper capabilitykeeper.ScopedKeeper ScopedMonitoringKeeper capabilitykeeper.ScopedKeeper + ScopedVotingKeeper capabilitykeeper.ScopedKeeper VotingKeeper votingmodulekeeper.Keeper ScopedOracleKeeper capabilitykeeper.ScopedKeeper OracleKeeper oraclemodulekeeper.Keeper @@ -408,11 +409,16 @@ func New( scopedOracleKeeper, ) oracleModule := oraclemodule.NewAppModule(appCodec, app.OracleKeeper, app.AccountKeeper, app.BankKeeper) + scopedVotingKeeper := app.CapabilityKeeper.ScopeToModule(votingmoduletypes.ModuleName) + app.ScopedVotingKeeper = scopedVotingKeeper app.VotingKeeper = *votingmodulekeeper.NewKeeper( appCodec, keys[votingmoduletypes.StoreKey], keys[votingmoduletypes.MemStoreKey], app.GetSubspace(votingmoduletypes.ModuleName), + app.IBCKeeper.ChannelKeeper, + &app.IBCKeeper.PortKeeper, + scopedVotingKeeper, app.AccountKeeper, app.BankKeeper, @@ -428,6 +434,7 @@ func New( ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferIBCModule) ibcRouter.AddRoute(monitoringptypes.ModuleName, monitoringModule) ibcRouter.AddRoute(oraclemoduletypes.ModuleName, oracleModule) + ibcRouter.AddRoute(votingmoduletypes.ModuleName, votingModule) // this line is used by starport scaffolding # ibc/app/router app.IBCKeeper.SetRouter(ibcRouter) diff --git a/proto/voting/genesis.proto b/proto/voting/genesis.proto index 6da8fc8..bfc95de 100644 --- a/proto/voting/genesis.proto +++ b/proto/voting/genesis.proto @@ -14,9 +14,10 @@ option go_package = "mande-chain/x/voting/types"; // GenesisState defines the voting module's genesis state. message GenesisState { Params params = 1 [(gogoproto.nullable) = false]; - repeated VoteBook voteBookList = 2 [(gogoproto.nullable) = false]; - repeated AggregateVotesCasted aggregateVotesCastedList = 3 [(gogoproto.nullable) = false]; - repeated AggregateVotesReceived aggregateVotesReceivedList = 4 [(gogoproto.nullable) = false]; - repeated Credibility credibilityList = 5 [(gogoproto.nullable) = false]; + string port_id = 2; + repeated VoteBook voteBookList = 3 [(gogoproto.nullable) = false]; + repeated AggregateVotesCasted aggregateVotesCastedList = 4 [(gogoproto.nullable) = false]; + repeated AggregateVotesReceived aggregateVotesReceivedList = 5 [(gogoproto.nullable) = false]; + repeated Credibility credibilityList = 6 [(gogoproto.nullable) = false]; // this line is used by starport scaffolding # genesis/proto/state } diff --git a/proto/voting/packet.proto b/proto/voting/packet.proto new file mode 100644 index 0000000..bbe056f --- /dev/null +++ b/proto/voting/packet.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; +package mandechain.voting; + +// this line is used by starport scaffolding # proto/packet/import + +option go_package = "mande-chain/x/voting/types"; + +message VotingPacketData { + oneof packet { + NoData noData = 1; + // this line is used by starport scaffolding # ibc/packet/proto/field + } +} + +message NoData { +} + +// this line is used by starport scaffolding # ibc/packet/proto/message \ No newline at end of file diff --git a/vue/src/store/generated/mande-chain/mandechain.voting/index.ts b/vue/src/store/generated/mande-chain/mandechain.voting/index.ts index b363512..51ba5eb 100755 --- a/vue/src/store/generated/mande-chain/mandechain.voting/index.ts +++ b/vue/src/store/generated/mande-chain/mandechain.voting/index.ts @@ -4,11 +4,13 @@ import { AggregateVotesCasted } from "./module/types/voting/aggregate_votes_cast import { AggregateVotesReceived } from "./module/types/voting/aggregate_votes_received" import { Credibility } from "./module/types/voting/credibility" import { AppliedX } from "./module/types/voting/credibility" +import { VotingPacketData } from "./module/types/voting/packet" +import { NoData } from "./module/types/voting/packet" import { Params } from "./module/types/voting/params" import { VoteBook } from "./module/types/voting/vote_book" -export { AggregateVotesCasted, AggregateVotesReceived, Credibility, AppliedX, Params, VoteBook }; +export { AggregateVotesCasted, AggregateVotesReceived, Credibility, AppliedX, VotingPacketData, NoData, Params, VoteBook }; async function initTxClient(vuexGetters) { return await txClient(vuexGetters['common/wallet/signer'], { @@ -60,6 +62,8 @@ const getDefaultState = () => { AggregateVotesReceived: getStructure(AggregateVotesReceived.fromPartial({})), Credibility: getStructure(Credibility.fromPartial({})), AppliedX: getStructure(AppliedX.fromPartial({})), + VotingPacketData: getStructure(VotingPacketData.fromPartial({})), + NoData: getStructure(NoData.fromPartial({})), Params: getStructure(Params.fromPartial({})), VoteBook: getStructure(VoteBook.fromPartial({})), diff --git a/vue/src/store/generated/mande-chain/mandechain.voting/module/types/voting/genesis.ts b/vue/src/store/generated/mande-chain/mandechain.voting/module/types/voting/genesis.ts index 27785ae..00b823e 100644 --- a/vue/src/store/generated/mande-chain/mandechain.voting/module/types/voting/genesis.ts +++ b/vue/src/store/generated/mande-chain/mandechain.voting/module/types/voting/genesis.ts @@ -11,6 +11,7 @@ export const protobufPackage = "mandechain.voting"; /** GenesisState defines the voting module's genesis state. */ export interface GenesisState { params: Params | undefined; + port_id: string; voteBookList: VoteBook[]; aggregateVotesCastedList: AggregateVotesCasted[]; aggregateVotesReceivedList: AggregateVotesReceived[]; @@ -18,24 +19,27 @@ export interface GenesisState { credibilityList: Credibility[]; } -const baseGenesisState: object = {}; +const baseGenesisState: object = { port_id: "" }; export const GenesisState = { encode(message: GenesisState, writer: Writer = Writer.create()): Writer { if (message.params !== undefined) { Params.encode(message.params, writer.uint32(10).fork()).ldelim(); } + if (message.port_id !== "") { + writer.uint32(18).string(message.port_id); + } for (const v of message.voteBookList) { - VoteBook.encode(v!, writer.uint32(18).fork()).ldelim(); + VoteBook.encode(v!, writer.uint32(26).fork()).ldelim(); } for (const v of message.aggregateVotesCastedList) { - AggregateVotesCasted.encode(v!, writer.uint32(26).fork()).ldelim(); + AggregateVotesCasted.encode(v!, writer.uint32(34).fork()).ldelim(); } for (const v of message.aggregateVotesReceivedList) { - AggregateVotesReceived.encode(v!, writer.uint32(34).fork()).ldelim(); + AggregateVotesReceived.encode(v!, writer.uint32(42).fork()).ldelim(); } for (const v of message.credibilityList) { - Credibility.encode(v!, writer.uint32(42).fork()).ldelim(); + Credibility.encode(v!, writer.uint32(50).fork()).ldelim(); } return writer; }, @@ -55,19 +59,22 @@ export const GenesisState = { message.params = Params.decode(reader, reader.uint32()); break; case 2: - message.voteBookList.push(VoteBook.decode(reader, reader.uint32())); + message.port_id = reader.string(); break; case 3: + message.voteBookList.push(VoteBook.decode(reader, reader.uint32())); + break; + case 4: message.aggregateVotesCastedList.push( AggregateVotesCasted.decode(reader, reader.uint32()) ); break; - case 4: + case 5: message.aggregateVotesReceivedList.push( AggregateVotesReceived.decode(reader, reader.uint32()) ); break; - case 5: + case 6: message.credibilityList.push( Credibility.decode(reader, reader.uint32()) ); @@ -91,6 +98,11 @@ export const GenesisState = { } else { message.params = undefined; } + if (object.port_id !== undefined && object.port_id !== null) { + message.port_id = String(object.port_id); + } else { + message.port_id = ""; + } if (object.voteBookList !== undefined && object.voteBookList !== null) { for (const e of object.voteBookList) { message.voteBookList.push(VoteBook.fromJSON(e)); @@ -129,6 +141,7 @@ export const GenesisState = { const obj: any = {}; message.params !== undefined && (obj.params = message.params ? Params.toJSON(message.params) : undefined); + message.port_id !== undefined && (obj.port_id = message.port_id); if (message.voteBookList) { obj.voteBookList = message.voteBookList.map((e) => e ? VoteBook.toJSON(e) : undefined @@ -171,6 +184,11 @@ export const GenesisState = { } else { message.params = undefined; } + if (object.port_id !== undefined && object.port_id !== null) { + message.port_id = object.port_id; + } else { + message.port_id = ""; + } if (object.voteBookList !== undefined && object.voteBookList !== null) { for (const e of object.voteBookList) { message.voteBookList.push(VoteBook.fromPartial(e)); diff --git a/vue/src/store/generated/mande-chain/mandechain.voting/module/types/voting/packet.ts b/vue/src/store/generated/mande-chain/mandechain.voting/module/types/voting/packet.ts new file mode 100644 index 0000000..b193a8e --- /dev/null +++ b/vue/src/store/generated/mande-chain/mandechain.voting/module/types/voting/packet.ts @@ -0,0 +1,116 @@ +/* eslint-disable */ +import { Writer, Reader } from "protobufjs/minimal"; + +export const protobufPackage = "mandechain.voting"; + +export interface VotingPacketData { + /** this line is used by starport scaffolding # ibc/packet/proto/field */ + noData: NoData | undefined; +} + +export interface NoData {} + +const baseVotingPacketData: object = {}; + +export const VotingPacketData = { + encode(message: VotingPacketData, writer: Writer = Writer.create()): Writer { + if (message.noData !== undefined) { + NoData.encode(message.noData, writer.uint32(10).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): VotingPacketData { + const reader = input instanceof Uint8Array ? new Reader(input) : input; + let end = length === undefined ? reader.len : reader.pos + length; + const message = { ...baseVotingPacketData } as VotingPacketData; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.noData = NoData.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): VotingPacketData { + const message = { ...baseVotingPacketData } as VotingPacketData; + if (object.noData !== undefined && object.noData !== null) { + message.noData = NoData.fromJSON(object.noData); + } else { + message.noData = undefined; + } + return message; + }, + + toJSON(message: VotingPacketData): unknown { + const obj: any = {}; + message.noData !== undefined && + (obj.noData = message.noData ? NoData.toJSON(message.noData) : undefined); + return obj; + }, + + fromPartial(object: DeepPartial): VotingPacketData { + const message = { ...baseVotingPacketData } as VotingPacketData; + if (object.noData !== undefined && object.noData !== null) { + message.noData = NoData.fromPartial(object.noData); + } else { + message.noData = undefined; + } + return message; + }, +}; + +const baseNoData: object = {}; + +export const NoData = { + encode(_: NoData, writer: Writer = Writer.create()): Writer { + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): NoData { + const reader = input instanceof Uint8Array ? new Reader(input) : input; + let end = length === undefined ? reader.len : reader.pos + length; + const message = { ...baseNoData } as NoData; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(_: any): NoData { + const message = { ...baseNoData } as NoData; + return message; + }, + + toJSON(_: NoData): unknown { + const obj: any = {}; + return obj; + }, + + fromPartial(_: DeepPartial): NoData { + const message = { ...baseNoData } as NoData; + return message; + }, +}; + +type Builtin = Date | Function | Uint8Array | string | number | undefined; +export type DeepPartial = T extends Builtin + ? T + : T extends Array + ? Array> + : T extends ReadonlyArray + ? ReadonlyArray> + : T extends {} + ? { [K in keyof T]?: DeepPartial } + : Partial; diff --git a/x/voting/genesis.go b/x/voting/genesis.go index 5645afd..1b86fe7 100644 --- a/x/voting/genesis.go +++ b/x/voting/genesis.go @@ -25,6 +25,18 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) for _, elem := range genState.CredibilityList { k.SetCredibility(ctx, elem) } + k.SetPort(ctx, genState.PortId) + // Only try to bind to port if it is not already bound, since we may already own + // port capability from capability InitGenesis + if !k.IsBound(ctx, genState.PortId) { + // module binds to the port on InitChain + // and claims the returned capability + err := k.BindPort(ctx, genState.PortId) + if err != nil { + panic("could not claim port capability: " + err.Error()) + } + } + // this line is used by starport scaffolding # genesis/module/init k.SetParams(ctx, genState.Params) } @@ -33,6 +45,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { genesis := types.DefaultGenesis() genesis.Params = k.GetParams(ctx) + genesis.PortId = k.GetPort(ctx) genesis.VoteBookList = k.GetAllVoteBook(ctx) genesis.AggregateVotesCastedList = k.GetAllAggregateVotesCasted(ctx) diff --git a/x/voting/genesis_test.go b/x/voting/genesis_test.go index daef995..047f70a 100644 --- a/x/voting/genesis_test.go +++ b/x/voting/genesis_test.go @@ -13,6 +13,7 @@ import ( func TestGenesis(t *testing.T) { genesisState := types.GenesisState{ Params: types.DefaultParams(), + PortId: types.PortID, VoteBookList: []types.VoteBook{ { @@ -57,6 +58,8 @@ func TestGenesis(t *testing.T) { nullify.Fill(&genesisState) nullify.Fill(got) + require.Equal(t, genesisState.PortId, got.PortId) + require.ElementsMatch(t, genesisState.VoteBookList, got.VoteBookList) require.ElementsMatch(t, genesisState.AggregateVotesCastedList, got.AggregateVotesCastedList) require.ElementsMatch(t, genesisState.AggregateVotesReceivedList, got.AggregateVotesReceivedList) diff --git a/x/voting/keeper/keeper.go b/x/voting/keeper/keeper.go index 6164f85..e7aa5e5 100644 --- a/x/voting/keeper/keeper.go +++ b/x/voting/keeper/keeper.go @@ -11,10 +11,12 @@ import ( paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" oracletypes "mande-chain/x/oracle/types" "mande-chain/x/voting/types" + "github.com/ignite/cli/ignite/pkg/cosmosibckeeper" ) type ( Keeper struct { + *cosmosibckeeper.Keeper cdc codec.BinaryCodec storeKey sdk.StoreKey memKey sdk.StoreKey @@ -32,6 +34,9 @@ func NewKeeper( storeKey, memKey sdk.StoreKey, ps paramtypes.Subspace, + channelKeeper cosmosibckeeper.ChannelKeeper, + portKeeper cosmosibckeeper.PortKeeper, + scopedKeeper cosmosibckeeper.ScopedKeeper, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, stakingKeeper types.StakingKeeper, oracleKeeper types.OracleKeeper, ) *Keeper { @@ -41,6 +46,13 @@ func NewKeeper( } return &Keeper{ + Keeper: cosmosibckeeper.NewKeeper( + types.PortKey, + storeKey, + channelKeeper, + portKeeper, + scopedKeeper, + ), cdc: cdc, storeKey: storeKey, diff --git a/x/voting/module.go b/x/voting/module.go index 1e85a1c..6c5b87a 100644 --- a/x/voting/module.go +++ b/x/voting/module.go @@ -19,11 +19,13 @@ import ( "mande-chain/x/voting/client/cli" "mande-chain/x/voting/keeper" "mande-chain/x/voting/types" + porttypes "github.com/cosmos/ibc-go/v3/modules/core/05-port/types" ) var ( _ module.AppModule = AppModule{} _ module.AppModuleBasic = AppModuleBasic{} + _ porttypes.IBCModule = AppModule{} ) // ---------------------------------------------------------------------------- diff --git a/x/voting/module_ibc.go b/x/voting/module_ibc.go new file mode 100644 index 0000000..0f7321f --- /dev/null +++ b/x/voting/module_ibc.go @@ -0,0 +1,228 @@ +package voting + +import ( + "fmt" + + "mande-chain/x/voting/types" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types" + porttypes "github.com/cosmos/ibc-go/v3/modules/core/05-port/types" + host "github.com/cosmos/ibc-go/v3/modules/core/24-host" + ibcexported "github.com/cosmos/ibc-go/v3/modules/core/exported" +) + +// OnChanOpenInit implements the IBCModule interface +func (am AppModule) OnChanOpenInit( + ctx sdk.Context, + order channeltypes.Order, + connectionHops []string, + portID string, + channelID string, + chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, + version string, +) error { + + // Require portID is the portID module is bound to + boundPort := am.keeper.GetPort(ctx) + if boundPort != portID { + return sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort) + } + + if version != types.Version { + return sdkerrors.Wrapf(types.ErrInvalidVersion, "got %s, expected %s", version, types.Version) + } + + // Claim channel capability passed back by IBC module + if err := am.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil { + return err + } + + return nil +} + +// OnChanOpenTry implements the IBCModule interface +func (am AppModule) OnChanOpenTry( + ctx sdk.Context, + order channeltypes.Order, + connectionHops []string, + portID, + channelID string, + chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, + counterpartyVersion string, +) (string, error) { + + // Require portID is the portID module is bound to + boundPort := am.keeper.GetPort(ctx) + if boundPort != portID { + return "", sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort) + } + + if counterpartyVersion != types.Version { + return "", sdkerrors.Wrapf(types.ErrInvalidVersion, "invalid counterparty version: got: %s, expected %s", counterpartyVersion, types.Version) + } + + // Module may have already claimed capability in OnChanOpenInit in the case of crossing hellos + // (ie chainA and chainB both call ChanOpenInit before one of them calls ChanOpenTry) + // If module can already authenticate the capability then module already owns it so we don't need to claim + // Otherwise, module does not have channel capability and we must claim it from IBC + if !am.keeper.AuthenticateCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)) { + // Only claim channel capability passed back by IBC module if we do not already own it + if err := am.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil { + return "", err + } + } + + return types.Version, nil +} + +// OnChanOpenAck implements the IBCModule interface +func (am AppModule) OnChanOpenAck( + ctx sdk.Context, + portID, + channelID string, + _, + counterpartyVersion string, +) error { + if counterpartyVersion != types.Version { + return sdkerrors.Wrapf(types.ErrInvalidVersion, "invalid counterparty version: %s, expected %s", counterpartyVersion, types.Version) + } + return nil +} + +// OnChanOpenConfirm implements the IBCModule interface +func (am AppModule) OnChanOpenConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + return nil +} + +// OnChanCloseInit implements the IBCModule interface +func (am AppModule) OnChanCloseInit( + ctx sdk.Context, + portID, + channelID string, +) error { + // Disallow user-initiated channel closing for channels + return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel") +} + +// OnChanCloseConfirm implements the IBCModule interface +func (am AppModule) OnChanCloseConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + return nil +} + +// OnRecvPacket implements the IBCModule interface +func (am AppModule) OnRecvPacket( + ctx sdk.Context, + modulePacket channeltypes.Packet, + relayer sdk.AccAddress, +) ibcexported.Acknowledgement { + var ack channeltypes.Acknowledgement + + // this line is used by starport scaffolding # oracle/packet/module/recv + + var modulePacketData types.VotingPacketData + if err := modulePacketData.Unmarshal(modulePacket.GetData()); err != nil { + return channeltypes.NewErrorAcknowledgement(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error()).Error()) + } + + // Dispatch packet + switch packet := modulePacketData.Packet.(type) { + // this line is used by starport scaffolding # ibc/packet/module/recv + default: + errMsg := fmt.Sprintf("unrecognized %s packet type: %T", types.ModuleName, packet) + return channeltypes.NewErrorAcknowledgement(errMsg) + } + + // NOTE: acknowledgement will be written synchronously during IBC handler execution. + return ack +} + +// OnAcknowledgementPacket implements the IBCModule interface +func (am AppModule) OnAcknowledgementPacket( + ctx sdk.Context, + modulePacket channeltypes.Packet, + acknowledgement []byte, + relayer sdk.AccAddress, +) error { + var ack channeltypes.Acknowledgement + if err := types.ModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet acknowledgement: %v", err) + } + + // this line is used by starport scaffolding # oracle/packet/module/ack + + var modulePacketData types.VotingPacketData + if err := modulePacketData.Unmarshal(modulePacket.GetData()); err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error()) + } + + var eventType string + + // Dispatch packet + switch packet := modulePacketData.Packet.(type) { + // this line is used by starport scaffolding # ibc/packet/module/ack + default: + errMsg := fmt.Sprintf("unrecognized %s packet type: %T", types.ModuleName, packet) + return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) + } + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + eventType, + sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), + sdk.NewAttribute(types.AttributeKeyAck, fmt.Sprintf("%v", ack)), + ), + ) + + switch resp := ack.Response.(type) { + case *channeltypes.Acknowledgement_Result: + ctx.EventManager().EmitEvent( + sdk.NewEvent( + eventType, + sdk.NewAttribute(types.AttributeKeyAckSuccess, string(resp.Result)), + ), + ) + case *channeltypes.Acknowledgement_Error: + ctx.EventManager().EmitEvent( + sdk.NewEvent( + eventType, + sdk.NewAttribute(types.AttributeKeyAckError, resp.Error), + ), + ) + } + + return nil +} + +// OnTimeoutPacket implements the IBCModule interface +func (am AppModule) OnTimeoutPacket( + ctx sdk.Context, + modulePacket channeltypes.Packet, + relayer sdk.AccAddress, +) error { + var modulePacketData types.VotingPacketData + if err := modulePacketData.Unmarshal(modulePacket.GetData()); err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error()) + } + + // Dispatch packet + switch packet := modulePacketData.Packet.(type) { + // this line is used by starport scaffolding # ibc/packet/module/timeout + default: + errMsg := fmt.Sprintf("unrecognized %s packet type: %T", types.ModuleName, packet) + return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) + } + + return nil +} diff --git a/x/voting/module_simulation.go b/x/voting/module_simulation.go index 88cb758..74a35e1 100644 --- a/x/voting/module_simulation.go +++ b/x/voting/module_simulation.go @@ -43,6 +43,7 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) { } votingGenesis := types.GenesisState{ Params: types.DefaultParams(), + PortId: types.PortID, // this line is used by starport scaffolding # simapp/module/genesisState } simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&votingGenesis) diff --git a/x/voting/types/errors.go b/x/voting/types/errors.go index f7c605c..a7a779a 100644 --- a/x/voting/types/errors.go +++ b/x/voting/types/errors.go @@ -16,4 +16,7 @@ var ( ErrNotEnoughMand = sdkerrors.Register(ModuleName, 7, "not enough MAND to use for voting") ErrZeroVoteCount = sdkerrors.Register(ModuleName, 8, "invalid vote count, cannot be zero") ErrReceiverIsNotAValidator = sdkerrors.Register(ModuleName, 9, "validator not found, receiver is not a validator") + ErrSample = sdkerrors.Register(ModuleName, 10, "sample error") + ErrInvalidPacketTimeout = sdkerrors.Register(ModuleName, 11, "invalid packet timeout") + ErrInvalidVersion = sdkerrors.Register(ModuleName, 12, "invalid version") ) diff --git a/x/voting/types/events_ibc.go b/x/voting/types/events_ibc.go new file mode 100644 index 0000000..07c66a4 --- /dev/null +++ b/x/voting/types/events_ibc.go @@ -0,0 +1,11 @@ +package types + +// IBC events +const ( + EventTypeTimeout = "timeout" + // this line is used by starport scaffolding # ibc/packet/event + + AttributeKeyAckSuccess = "success" + AttributeKeyAck = "acknowledgement" + AttributeKeyAckError = "error" +) diff --git a/x/voting/types/genesis.go b/x/voting/types/genesis.go index 84cfaab..303ec3e 100644 --- a/x/voting/types/genesis.go +++ b/x/voting/types/genesis.go @@ -2,6 +2,7 @@ package types import ( "fmt" + host "github.com/cosmos/ibc-go/v3/modules/core/24-host" ) // DefaultIndex is the default capability global index @@ -10,6 +11,7 @@ const DefaultIndex uint64 = 1 // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { return &GenesisState{ + PortId: PortID, VoteBookList: []VoteBook{}, AggregateVotesCastedList: []AggregateVotesCasted{}, AggregateVotesReceivedList: []AggregateVotesReceived{}, @@ -22,6 +24,9 @@ func DefaultGenesis() *GenesisState { // Validate performs basic genesis state validation returning an error upon any // failure. func (gs GenesisState) Validate() error { + if err := host.PortIdentifierValidator(gs.PortId); err != nil { + return err + } // Check for duplicated index in voteBook voteBookIndexMap := make(map[string]struct{}) diff --git a/x/voting/types/genesis.pb.go b/x/voting/types/genesis.pb.go index ca30b04..602d79e 100644 --- a/x/voting/types/genesis.pb.go +++ b/x/voting/types/genesis.pb.go @@ -26,10 +26,11 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the voting module's genesis state. type GenesisState struct { Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"` - VoteBookList []VoteBook `protobuf:"bytes,2,rep,name=voteBookList,proto3" json:"voteBookList"` - AggregateVotesCastedList []AggregateVotesCasted `protobuf:"bytes,3,rep,name=aggregateVotesCastedList,proto3" json:"aggregateVotesCastedList"` - AggregateVotesReceivedList []AggregateVotesReceived `protobuf:"bytes,4,rep,name=aggregateVotesReceivedList,proto3" json:"aggregateVotesReceivedList"` - CredibilityList []Credibility `protobuf:"bytes,5,rep,name=credibilityList,proto3" json:"credibilityList"` + PortId string `protobuf:"bytes,2,opt,name=port_id,json=portId,proto3" json:"port_id,omitempty"` + VoteBookList []VoteBook `protobuf:"bytes,3,rep,name=voteBookList,proto3" json:"voteBookList"` + AggregateVotesCastedList []AggregateVotesCasted `protobuf:"bytes,4,rep,name=aggregateVotesCastedList,proto3" json:"aggregateVotesCastedList"` + AggregateVotesReceivedList []AggregateVotesReceived `protobuf:"bytes,5,rep,name=aggregateVotesReceivedList,proto3" json:"aggregateVotesReceivedList"` + CredibilityList []Credibility `protobuf:"bytes,6,rep,name=credibilityList,proto3" json:"credibilityList"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -72,6 +73,13 @@ func (m *GenesisState) GetParams() Params { return Params{} } +func (m *GenesisState) GetPortId() string { + if m != nil { + return m.PortId + } + return "" +} + func (m *GenesisState) GetVoteBookList() []VoteBook { if m != nil { return m.VoteBookList @@ -107,29 +115,31 @@ func init() { func init() { proto.RegisterFile("voting/genesis.proto", fileDescriptor_8897b2b6121b6765) } var fileDescriptor_8897b2b6121b6765 = []byte{ - // 349 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x91, 0x41, 0x4b, 0x32, 0x41, - 0x18, 0xc7, 0x77, 0x5f, 0x7d, 0x3d, 0x8c, 0x42, 0x34, 0x49, 0x6c, 0x1b, 0x4c, 0x52, 0x44, 0x76, - 0x68, 0x17, 0x2c, 0xe8, 0x9c, 0x12, 0x5d, 0x22, 0xc2, 0xa0, 0x43, 0x17, 0x19, 0xd7, 0x61, 0x1a, - 0xcc, 0x1d, 0xd9, 0x19, 0x24, 0xbf, 0x45, 0x9f, 0xa7, 0x4f, 0xe0, 0xd1, 0x63, 0xa7, 0x08, 0xfd, - 0x22, 0xb1, 0xcf, 0x3e, 0x59, 0xd6, 0x5a, 0x37, 0x99, 0xe7, 0xf7, 0xff, 0xfd, 0x7d, 0xf6, 0x21, - 0xd5, 0x91, 0xb6, 0x2a, 0x96, 0xa1, 0x14, 0xb1, 0x30, 0xca, 0x04, 0xc3, 0x44, 0x5b, 0x4d, 0xd7, - 0x07, 0x3c, 0xee, 0x89, 0xe8, 0x9e, 0xab, 0x38, 0xc8, 0x00, 0xbf, 0x2a, 0xb5, 0xd4, 0x30, 0x0d, - 0xd3, 0x5f, 0x19, 0xe8, 0x6f, 0x60, 0x7c, 0xc8, 0x13, 0x3e, 0xc0, 0xb4, 0xbf, 0x89, 0x8f, 0x23, - 0x6d, 0x45, 0xa7, 0xab, 0x75, 0x1f, 0xdf, 0xf7, 0xf0, 0x9d, 0x4b, 0x99, 0x08, 0xc9, 0xad, 0xe8, - 0xa4, 0x84, 0xe9, 0x44, 0xdc, 0x58, 0xd1, 0x43, 0x68, 0x7f, 0x05, 0x94, 0x88, 0x48, 0xa8, 0xd1, - 0x02, 0xf3, 0x10, 0x8b, 0x12, 0xd1, 0x53, 0x5d, 0xf5, 0xa0, 0xec, 0x38, 0x9b, 0xec, 0x3e, 0x17, - 0x48, 0xe5, 0x22, 0xdb, 0xe6, 0xc6, 0x72, 0x2b, 0xe8, 0x29, 0x29, 0x65, 0x7f, 0xcf, 0x73, 0x6b, - 0x6e, 0xbd, 0xdc, 0xd8, 0x0a, 0x7e, 0x6c, 0x17, 0x5c, 0x03, 0xd0, 0x2c, 0x4e, 0x5e, 0x77, 0x9c, - 0x36, 0xe2, 0xf4, 0x9c, 0x54, 0xd2, 0xee, 0xa6, 0xd6, 0xfd, 0x4b, 0x65, 0xac, 0xf7, 0xaf, 0x56, - 0xa8, 0x97, 0x1b, 0xdb, 0x39, 0xf1, 0x5b, 0xc4, 0x50, 0xb0, 0x14, 0xa3, 0x8a, 0x78, 0x8b, 0x65, - 0x52, 0xd0, 0xb4, 0x60, 0x5f, 0x50, 0x16, 0x40, 0x79, 0x90, 0xa3, 0x3c, 0xcb, 0x89, 0xa0, 0x7e, - 0xa5, 0x8e, 0x6a, 0xe2, 0x2f, 0xcf, 0xda, 0xf8, 0xd5, 0xa0, 0xac, 0x08, 0x65, 0x87, 0x7f, 0x96, - 0x7d, 0x84, 0xb0, 0xee, 0x17, 0x25, 0xbd, 0x22, 0x6b, 0x5f, 0x2e, 0x00, 0x2d, 0xff, 0xa1, 0x85, - 0xe5, 0xb4, 0xb4, 0x3e, 0x49, 0x54, 0x7f, 0x0f, 0x37, 0x4f, 0x26, 0x33, 0xe6, 0x4e, 0x67, 0xcc, - 0x7d, 0x9b, 0x31, 0xf7, 0x69, 0xce, 0x9c, 0xe9, 0x9c, 0x39, 0x2f, 0x73, 0xe6, 0xdc, 0xf9, 0xe0, - 0x3b, 0x02, 0x61, 0xf8, 0x18, 0xe2, 0xf9, 0xed, 0x78, 0x28, 0x4c, 0xb7, 0x04, 0x97, 0x3f, 0x7e, - 0x0f, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xea, 0xab, 0xde, 0xcd, 0x02, 0x00, 0x00, + // 369 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x92, 0x41, 0x4b, 0xf3, 0x30, + 0x18, 0xc7, 0xdb, 0x77, 0x7b, 0x2b, 0x66, 0x03, 0xb1, 0x0e, 0xad, 0x15, 0xe2, 0x50, 0xc4, 0x79, + 0xb0, 0x85, 0x29, 0x78, 0x76, 0x43, 0x44, 0x10, 0x91, 0x09, 0x1e, 0xbc, 0x94, 0xac, 0x0d, 0x31, + 0xcc, 0x35, 0xa5, 0x09, 0xc3, 0x7d, 0x0b, 0x3f, 0xd6, 0x8e, 0x03, 0x2f, 0x9e, 0x44, 0xb6, 0x2f, + 0x22, 0x4d, 0x1f, 0xa7, 0xd3, 0x4e, 0x6f, 0x5b, 0xf2, 0xfb, 0xff, 0xfe, 0x79, 0xd2, 0xa0, 0xda, + 0x40, 0x28, 0x1e, 0x33, 0x9f, 0xd1, 0x98, 0x4a, 0x2e, 0xbd, 0x24, 0x15, 0x4a, 0xd8, 0xab, 0x7d, + 0x12, 0x47, 0x34, 0xbc, 0x27, 0x3c, 0xf6, 0x72, 0xc0, 0xad, 0x31, 0xc1, 0x84, 0xde, 0xf5, 0xb3, + 0x5f, 0x39, 0xe8, 0xae, 0x41, 0x3c, 0x21, 0x29, 0xe9, 0x43, 0xda, 0x5d, 0x87, 0xc5, 0x81, 0x50, + 0x34, 0xe8, 0x0a, 0xd1, 0x83, 0xf5, 0x5d, 0x58, 0x27, 0x8c, 0xa5, 0x94, 0x11, 0x45, 0x83, 0x8c, + 0x90, 0x41, 0x48, 0xa4, 0xa2, 0x11, 0x40, 0x7b, 0x0b, 0xa0, 0x94, 0x86, 0x94, 0x0f, 0x66, 0x98, + 0x03, 0x58, 0x98, 0xd2, 0x88, 0x77, 0xf9, 0x03, 0x57, 0xc3, 0x7c, 0x67, 0xe7, 0xb9, 0x84, 0xaa, + 0xe7, 0xf9, 0x34, 0x37, 0x8a, 0x28, 0x6a, 0x9f, 0x20, 0x2b, 0x3f, 0x9e, 0x63, 0xd6, 0xcd, 0x46, + 0xa5, 0xb9, 0xe9, 0xfd, 0x98, 0xce, 0xbb, 0xd6, 0x40, 0xab, 0x3c, 0x7a, 0xdd, 0x36, 0x3a, 0x80, + 0xdb, 0x1b, 0x68, 0x29, 0x11, 0xa9, 0x0a, 0x78, 0xe4, 0xfc, 0xab, 0x9b, 0x8d, 0xe5, 0x8e, 0x95, + 0xfd, 0xbd, 0x88, 0xec, 0x33, 0x54, 0xcd, 0x0e, 0xd5, 0x12, 0xa2, 0x77, 0xc9, 0xa5, 0x72, 0x4a, + 0xf5, 0x52, 0xa3, 0xd2, 0xdc, 0x2a, 0xf0, 0xde, 0x02, 0x06, 0xe6, 0xb9, 0x98, 0xcd, 0x91, 0x33, + 0x9b, 0x32, 0x03, 0x65, 0x5b, 0x5f, 0x84, 0x56, 0x96, 0xb5, 0x72, 0xbf, 0x40, 0x79, 0x5a, 0x10, + 0x01, 0xfd, 0x42, 0x9d, 0x2d, 0x90, 0x3b, 0xbf, 0xd7, 0x81, 0xeb, 0xd4, 0x65, 0xff, 0x75, 0xd9, + 0xc1, 0x9f, 0x65, 0x1f, 0x21, 0xa8, 0xfb, 0x45, 0x69, 0x5f, 0xa1, 0x95, 0x2f, 0x9f, 0x46, 0xb7, + 0x58, 0xba, 0x05, 0x17, 0xb4, 0xb4, 0x3f, 0x49, 0x50, 0x7f, 0x0f, 0xb7, 0x8e, 0x47, 0x13, 0x6c, + 0x8e, 0x27, 0xd8, 0x7c, 0x9b, 0x60, 0xf3, 0x69, 0x8a, 0x8d, 0xf1, 0x14, 0x1b, 0x2f, 0x53, 0x6c, + 0xdc, 0xb9, 0xda, 0x77, 0xa8, 0x85, 0xfe, 0xa3, 0x0f, 0xef, 0x42, 0x0d, 0x13, 0x2a, 0xbb, 0x96, + 0x7e, 0x12, 0x47, 0xef, 0x01, 0x00, 0x00, 0xff, 0xff, 0x6e, 0x78, 0xc6, 0x1c, 0xe6, 0x02, 0x00, + 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -163,7 +173,7 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintGenesis(dAtA, i, uint64(size)) } i-- - dAtA[i] = 0x2a + dAtA[i] = 0x32 } } if len(m.AggregateVotesReceivedList) > 0 { @@ -177,7 +187,7 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintGenesis(dAtA, i, uint64(size)) } i-- - dAtA[i] = 0x22 + dAtA[i] = 0x2a } } if len(m.AggregateVotesCastedList) > 0 { @@ -191,7 +201,7 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintGenesis(dAtA, i, uint64(size)) } i-- - dAtA[i] = 0x1a + dAtA[i] = 0x22 } } if len(m.VoteBookList) > 0 { @@ -205,9 +215,16 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintGenesis(dAtA, i, uint64(size)) } i-- - dAtA[i] = 0x12 + dAtA[i] = 0x1a } } + if len(m.PortId) > 0 { + i -= len(m.PortId) + copy(dAtA[i:], m.PortId) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.PortId))) + i-- + dAtA[i] = 0x12 + } { size, err := m.Params.MarshalToSizedBuffer(dAtA[:i]) if err != nil { @@ -240,6 +257,10 @@ func (m *GenesisState) Size() (n int) { _ = l l = m.Params.Size() n += 1 + l + sovGenesis(uint64(l)) + l = len(m.PortId) + if l > 0 { + n += 1 + l + sovGenesis(uint64(l)) + } if len(m.VoteBookList) > 0 { for _, e := range m.VoteBookList { l = e.Size() @@ -336,6 +357,38 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field PortId", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + 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 ErrInvalidLengthGenesis + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.PortId = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 3: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field VoteBookList", wireType) } @@ -369,7 +422,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 3: + case 4: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field AggregateVotesCastedList", wireType) } @@ -403,7 +456,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 4: + case 5: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field AggregateVotesReceivedList", wireType) } @@ -437,7 +490,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 5: + case 6: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field CredibilityList", wireType) } diff --git a/x/voting/types/genesis_test.go b/x/voting/types/genesis_test.go index e171309..0a2f921 100644 --- a/x/voting/types/genesis_test.go +++ b/x/voting/types/genesis_test.go @@ -21,7 +21,7 @@ func TestGenesisState_Validate(t *testing.T) { { desc: "valid genesis state", genState: &types.GenesisState{ - + PortId: types.PortID, VoteBookList: []types.VoteBook{ { Index: "0", diff --git a/x/voting/types/keys.go b/x/voting/types/keys.go index 4ae0d02..fb95356 100644 --- a/x/voting/types/keys.go +++ b/x/voting/types/keys.go @@ -15,6 +15,17 @@ const ( // MemStoreKey defines the in-memory store key MemStoreKey = "mem_voting" + + // Version defines the current version the IBC module supports + Version = "voting-1" + + // PortID is the default port id that module binds to + PortID = ModuleName +) + +var ( + // PortKey defines the key to store the port ID in store + PortKey = KeyPrefix("voting-port-") ) func KeyPrefix(p string) []byte { diff --git a/x/voting/types/packet.pb.go b/x/voting/types/packet.pb.go new file mode 100644 index 0000000..1453738 --- /dev/null +++ b/x/voting/types/packet.pb.go @@ -0,0 +1,499 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: voting/packet.proto + +package types + +import ( + fmt "fmt" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +type VotingPacketData struct { + // Types that are valid to be assigned to Packet: + // *VotingPacketData_NoData + Packet isVotingPacketData_Packet `protobuf_oneof:"packet"` +} + +func (m *VotingPacketData) Reset() { *m = VotingPacketData{} } +func (m *VotingPacketData) String() string { return proto.CompactTextString(m) } +func (*VotingPacketData) ProtoMessage() {} +func (*VotingPacketData) Descriptor() ([]byte, []int) { + return fileDescriptor_0fa2bbbf306f6ae3, []int{0} +} +func (m *VotingPacketData) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *VotingPacketData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_VotingPacketData.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 *VotingPacketData) XXX_Merge(src proto.Message) { + xxx_messageInfo_VotingPacketData.Merge(m, src) +} +func (m *VotingPacketData) XXX_Size() int { + return m.Size() +} +func (m *VotingPacketData) XXX_DiscardUnknown() { + xxx_messageInfo_VotingPacketData.DiscardUnknown(m) +} + +var xxx_messageInfo_VotingPacketData proto.InternalMessageInfo + +type isVotingPacketData_Packet interface { + isVotingPacketData_Packet() + MarshalTo([]byte) (int, error) + Size() int +} + +type VotingPacketData_NoData struct { + NoData *NoData `protobuf:"bytes,1,opt,name=noData,proto3,oneof" json:"noData,omitempty"` +} + +func (*VotingPacketData_NoData) isVotingPacketData_Packet() {} + +func (m *VotingPacketData) GetPacket() isVotingPacketData_Packet { + if m != nil { + return m.Packet + } + return nil +} + +func (m *VotingPacketData) GetNoData() *NoData { + if x, ok := m.GetPacket().(*VotingPacketData_NoData); ok { + return x.NoData + } + return nil +} + +// XXX_OneofWrappers is for the internal use of the proto package. +func (*VotingPacketData) XXX_OneofWrappers() []interface{} { + return []interface{}{ + (*VotingPacketData_NoData)(nil), + } +} + +type NoData struct { +} + +func (m *NoData) Reset() { *m = NoData{} } +func (m *NoData) String() string { return proto.CompactTextString(m) } +func (*NoData) ProtoMessage() {} +func (*NoData) Descriptor() ([]byte, []int) { + return fileDescriptor_0fa2bbbf306f6ae3, []int{1} +} +func (m *NoData) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *NoData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_NoData.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 *NoData) XXX_Merge(src proto.Message) { + xxx_messageInfo_NoData.Merge(m, src) +} +func (m *NoData) XXX_Size() int { + return m.Size() +} +func (m *NoData) XXX_DiscardUnknown() { + xxx_messageInfo_NoData.DiscardUnknown(m) +} + +var xxx_messageInfo_NoData proto.InternalMessageInfo + +func init() { + proto.RegisterType((*VotingPacketData)(nil), "mandechain.voting.VotingPacketData") + proto.RegisterType((*NoData)(nil), "mandechain.voting.NoData") +} + +func init() { proto.RegisterFile("voting/packet.proto", fileDescriptor_0fa2bbbf306f6ae3) } + +var fileDescriptor_0fa2bbbf306f6ae3 = []byte{ + // 164 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0xcb, 0x2f, 0xc9, + 0xcc, 0x4b, 0xd7, 0x2f, 0x48, 0x4c, 0xce, 0x4e, 0x2d, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, + 0x12, 0xcc, 0x4d, 0xcc, 0x4b, 0x49, 0x4d, 0xce, 0x48, 0xcc, 0xcc, 0xd3, 0x83, 0xc8, 0x2b, 0x05, + 0x72, 0x09, 0x84, 0x81, 0x59, 0x01, 0x60, 0x85, 0x2e, 0x89, 0x25, 0x89, 0x42, 0xc6, 0x5c, 0x6c, + 0x79, 0xf9, 0x20, 0x96, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0xb7, 0x91, 0xa4, 0x1e, 0x86, 0x3e, 0x3d, + 0x3f, 0xb0, 0x02, 0x0f, 0x86, 0x20, 0xa8, 0x52, 0x27, 0x0e, 0x2e, 0x36, 0x88, 0x5d, 0x4a, 0x1c, + 0x5c, 0x6c, 0x10, 0x59, 0x27, 0x93, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, + 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, + 0x92, 0x02, 0x9b, 0xa8, 0x0b, 0x36, 0x52, 0xbf, 0x42, 0x1f, 0xea, 0xd8, 0x92, 0xca, 0x82, 0xd4, + 0xe2, 0x24, 0x36, 0xb0, 0x63, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x08, 0x07, 0x6e, 0x0c, + 0xc3, 0x00, 0x00, 0x00, +} + +func (m *VotingPacketData) 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 *VotingPacketData) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *VotingPacketData) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Packet != nil { + { + size := m.Packet.Size() + i -= size + if _, err := m.Packet.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + } + } + return len(dAtA) - i, nil +} + +func (m *VotingPacketData_NoData) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *VotingPacketData_NoData) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.NoData != nil { + { + size, err := m.NoData.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintPacket(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} +func (m *NoData) 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 *NoData) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *NoData) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + return len(dAtA) - i, nil +} + +func encodeVarintPacket(dAtA []byte, offset int, v uint64) int { + offset -= sovPacket(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *VotingPacketData) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Packet != nil { + n += m.Packet.Size() + } + return n +} + +func (m *VotingPacketData_NoData) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.NoData != nil { + l = m.NoData.Size() + n += 1 + l + sovPacket(uint64(l)) + } + return n +} +func (m *NoData) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + return n +} + +func sovPacket(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozPacket(x uint64) (n int) { + return sovPacket(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *VotingPacketData) 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 ErrIntOverflowPacket + } + 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: VotingPacketData: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: VotingPacketData: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field NoData", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowPacket + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthPacket + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthPacket + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := &NoData{} + if err := v.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + m.Packet = &VotingPacketData_NoData{v} + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipPacket(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthPacket + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *NoData) 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 ErrIntOverflowPacket + } + 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: NoData: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: NoData: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + default: + iNdEx = preIndex + skippy, err := skipPacket(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthPacket + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipPacket(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowPacket + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowPacket + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowPacket + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthPacket + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupPacket + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthPacket + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthPacket = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowPacket = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupPacket = fmt.Errorf("proto: unexpected end of group") +)