From 2e567031de5b425c2ec1d7bef1813e10fa38aee4 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Mon, 11 Apr 2022 13:13:05 +0200 Subject: [PATCH 1/2] Unpack contract details from genesis --- x/wasm/types/genesis.go | 20 +++++++++++++ x/wasm/types/genesis_test.go | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/x/wasm/types/genesis.go b/x/wasm/types/genesis.go index 98df6fd6ad..8b753fa2d1 100644 --- a/x/wasm/types/genesis.go +++ b/x/wasm/types/genesis.go @@ -1,6 +1,7 @@ package types import ( + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -98,3 +99,22 @@ func (m GenesisState_GenMsgs) ValidateBasic() error { func ValidateGenesis(data GenesisState) error { return data.ValidateBasic() } + +var _ codectypes.UnpackInterfacesMessage = GenesisState{} + +// UnpackInterfaces implements codectypes.UnpackInterfaces +func (m GenesisState) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { + for _, v := range m.Contracts { + if err := v.UnpackInterfaces(unpacker); err != nil { + return err + } + } + return nil +} + +var _ codectypes.UnpackInterfacesMessage = &Contract{} + +// UnpackInterfaces implements codectypes.UnpackInterfaces +func (m *Contract) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { + return m.ContractInfo.UnpackInterfaces(unpacker) +} diff --git a/x/wasm/types/genesis_test.go b/x/wasm/types/genesis_test.go index d8da55c56e..208ee7e2e0 100644 --- a/x/wasm/types/genesis_test.go +++ b/x/wasm/types/genesis_test.go @@ -3,6 +3,14 @@ package types import ( "bytes" "testing" + "time" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/stretchr/testify/assert" + "github.com/tendermint/tendermint/libs/rand" "github.com/stretchr/testify/require" ) @@ -170,3 +178,52 @@ func TestContractValidateBasic(t *testing.T) { }) } } + +func TestGenesisContractInfoMarshalUnmarshal(t *testing.T) { + var myAddr sdk.AccAddress = rand.Bytes(ContractAddrLen) + var myOtherAddr sdk.AccAddress = rand.Bytes(ContractAddrLen) + var anyPos = AbsoluteTxPosition{BlockHeight: 1, TxIndex: 2} + + anyTime := time.Now().UTC() + // using gov proposal here as a random protobuf types as it contains an Any type inside for nested unpacking + myExtension, err := govtypes.NewProposal(&govtypes.TextProposal{Title: "bar"}, 1, anyTime, anyTime) + require.NoError(t, err) + myExtension.TotalDeposit = nil + + src := NewContractInfo(1, myAddr, myOtherAddr, "bar", &anyPos) + err = src.SetExtension(&myExtension) + require.NoError(t, err) + + interfaceRegistry := types.NewInterfaceRegistry() + marshaler := codec.NewProtoCodec(interfaceRegistry) + RegisterInterfaces(interfaceRegistry) + // register proposal as extension type + interfaceRegistry.RegisterImplementations( + (*ContractInfoExtension)(nil), + &govtypes.Proposal{}, + ) + // register gov types for nested Anys + govtypes.RegisterInterfaces(interfaceRegistry) + + // when encode + gs := GenesisState{ + Contracts: []Contract{{ + ContractInfo: src, + }}, + } + + bz, err := marshaler.Marshal(&gs) + require.NoError(t, err) + // and decode + var destGs GenesisState + err = marshaler.Unmarshal(bz, &destGs) + require.NoError(t, err) + // then + require.Len(t, destGs.Contracts, 1) + dest := destGs.Contracts[0].ContractInfo + assert.Equal(t, src, dest) + // and sanity check nested any + var destExt govtypes.Proposal + require.NoError(t, dest.ReadExtension(&destExt)) + assert.Equal(t, destExt.GetTitle(), "bar") +} From 5771e61ffd8d5cab4b139ff253efe38a0ba3f5e6 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Tue, 12 Apr 2022 09:59:09 +0200 Subject: [PATCH 2/2] Address linter warnings --- x/wasm/keeper/keeper.go | 2 +- x/wasm/types/genesis.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index ba47392396..5e6fe1b8b2 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -984,7 +984,7 @@ func moduleLogger(ctx sdk.Context) log.Logger { } // Querier creates a new grpc querier instance -func Querier(k *Keeper) *grpcQuerier { +func Querier(k *Keeper) *grpcQuerier { //nolint:revive return NewGrpcQuerier(k.cdc, k.storeKey, k, k.queryGasLimit) } diff --git a/x/wasm/types/genesis.go b/x/wasm/types/genesis.go index 8b753fa2d1..ba973c6f02 100644 --- a/x/wasm/types/genesis.go +++ b/x/wasm/types/genesis.go @@ -103,8 +103,8 @@ func ValidateGenesis(data GenesisState) error { var _ codectypes.UnpackInterfacesMessage = GenesisState{} // UnpackInterfaces implements codectypes.UnpackInterfaces -func (m GenesisState) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { - for _, v := range m.Contracts { +func (s GenesisState) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { + for _, v := range s.Contracts { if err := v.UnpackInterfaces(unpacker); err != nil { return err } @@ -115,6 +115,6 @@ func (m GenesisState) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { var _ codectypes.UnpackInterfacesMessage = &Contract{} // UnpackInterfaces implements codectypes.UnpackInterfaces -func (m *Contract) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { - return m.ContractInfo.UnpackInterfaces(unpacker) +func (c *Contract) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { + return c.ContractInfo.UnpackInterfaces(unpacker) }