Unpack contract details from genesis (#802)
* Unpack contract details from genesis * Address linter warnings
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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 (s GenesisState) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
||||
for _, v := range s.Contracts {
|
||||
if err := v.UnpackInterfaces(unpacker); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ codectypes.UnpackInterfacesMessage = &Contract{}
|
||||
|
||||
// UnpackInterfaces implements codectypes.UnpackInterfaces
|
||||
func (c *Contract) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
||||
return c.ContractInfo.UnpackInterfaces(unpacker)
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user