Validate genesis messages
This commit is contained in:
@@ -63,7 +63,7 @@ func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState, staki
|
||||
return nil, nil
|
||||
}
|
||||
for _, genTx := range data.GenMsgs {
|
||||
msg := asMsg(genTx)
|
||||
msg := genTx.AsMsg()
|
||||
if msg == nil {
|
||||
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "unknown message")
|
||||
}
|
||||
@@ -75,19 +75,6 @@ func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState, staki
|
||||
return stakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
||||
}
|
||||
|
||||
func asMsg(genTx types.GenesisState_GenMsgs) sdk.Msg {
|
||||
if msg := genTx.GetStoreCode(); msg != nil {
|
||||
return msg
|
||||
}
|
||||
if msg := genTx.GetInstantiateContract(); msg != nil {
|
||||
return msg
|
||||
}
|
||||
if msg := genTx.GetExecuteContract(); msg != nil {
|
||||
return msg
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExportGenesis returns a GenesisState for a given context and keeper.
|
||||
func ExportGenesis(ctx sdk.Context, keeper *Keeper) *types.GenesisState {
|
||||
var genState types.GenesisState
|
||||
|
||||
@@ -32,6 +32,11 @@ func (s GenesisState) ValidateBasic() error {
|
||||
return sdkerrors.Wrapf(err, "sequence: %d", i)
|
||||
}
|
||||
}
|
||||
for i := range s.GenMsgs {
|
||||
if err := s.GenMsgs[i].ValidateBasic(); err != nil {
|
||||
return sdkerrors.Wrapf(err, "gen message: %d", i)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -67,6 +72,28 @@ func (c Contract) ValidateBasic() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AsMsg returns the underlying cosmos-sdk message instance. Null when can not be mapped to a known type.
|
||||
func (m GenesisState_GenMsgs) AsMsg() sdk.Msg {
|
||||
if msg := m.GetStoreCode(); msg != nil {
|
||||
return msg
|
||||
}
|
||||
if msg := m.GetInstantiateContract(); msg != nil {
|
||||
return msg
|
||||
}
|
||||
if msg := m.GetExecuteContract(); msg != nil {
|
||||
return msg
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m GenesisState_GenMsgs) ValidateBasic() error {
|
||||
msg := m.AsMsg()
|
||||
if msg == nil {
|
||||
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "unknown message")
|
||||
}
|
||||
return msg.ValidateBasic()
|
||||
}
|
||||
|
||||
// ValidateGenesis performs basic validation of supply genesis data returning an
|
||||
// error for any failed validation criteria.
|
||||
func ValidateGenesis(data GenesisState) error {
|
||||
|
||||
@@ -15,7 +15,7 @@ message GenesisState {
|
||||
repeated Sequence sequences = 4 [(gogoproto.nullable) = false, (gogoproto.jsontag) = "sequences,omitempty"];
|
||||
repeated GenMsgs gen_msgs = 5 [(gogoproto.nullable) = false, (gogoproto.jsontag) = "gen_msgs,omitempty"];
|
||||
|
||||
// GenMsgs define the messages that can be executed during genesis phase.
|
||||
// GenMsgs define the messages that can be executed during genesis phase in order.
|
||||
// The intention is to have more human readable data that is auditable.
|
||||
message GenMsgs {
|
||||
// sum is a single message
|
||||
|
||||
@@ -39,6 +39,30 @@ func TestValidateGenesisState(t *testing.T) {
|
||||
},
|
||||
expError: true,
|
||||
},
|
||||
"genesis store code message invalid": {
|
||||
srcMutator: func(s *GenesisState) {
|
||||
s.GenMsgs[0].GetStoreCode().WASMByteCode = nil
|
||||
},
|
||||
expError: true,
|
||||
},
|
||||
"genesis instantiate contract message invalid": {
|
||||
srcMutator: func(s *GenesisState) {
|
||||
s.GenMsgs[1].GetInstantiateContract().CodeID = 0
|
||||
},
|
||||
expError: true,
|
||||
},
|
||||
"genesis execute contract message invalid": {
|
||||
srcMutator: func(s *GenesisState) {
|
||||
s.GenMsgs[2].GetExecuteContract().Sender = "invalid"
|
||||
},
|
||||
expError: true,
|
||||
},
|
||||
"genesis invalid message type": {
|
||||
srcMutator: func(s *GenesisState) {
|
||||
s.GenMsgs[0].Sum = nil
|
||||
},
|
||||
expError: true,
|
||||
},
|
||||
}
|
||||
for msg, spec := range specs {
|
||||
t.Run(msg, func(t *testing.T) {
|
||||
|
||||
@@ -14,6 +14,7 @@ func GenesisFixture(mutators ...func(*GenesisState)) GenesisState {
|
||||
numCodes = 2
|
||||
numContracts = 2
|
||||
numSequences = 2
|
||||
numMsg = 3
|
||||
)
|
||||
|
||||
fixture := GenesisState{
|
||||
@@ -34,6 +35,11 @@ func GenesisFixture(mutators ...func(*GenesisState)) GenesisState {
|
||||
Value: uint64(i),
|
||||
}
|
||||
}
|
||||
fixture.GenMsgs = []GenesisState_GenMsgs{
|
||||
{Sum: &GenesisState_GenMsgs_StoreCode{StoreCode: MsgStoreCodeFixture()}},
|
||||
{Sum: &GenesisState_GenMsgs_InstantiateContract{InstantiateContract: MsgInstantiateContractFixture()}},
|
||||
{Sum: &GenesisState_GenMsgs_ExecuteContract{ExecuteContract: MsgExecuteContractFixture()}},
|
||||
}
|
||||
for _, m := range mutators {
|
||||
m(&fixture)
|
||||
}
|
||||
@@ -120,6 +126,61 @@ func WithSHA256CodeHash(wasmCode []byte) func(info *CodeInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
func MsgStoreCodeFixture(mutators ...func(*MsgStoreCode)) *MsgStoreCode {
|
||||
var wasmIdent = []byte("\x00\x61\x73\x6D")
|
||||
const anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du"
|
||||
r := &MsgStoreCode{
|
||||
Sender: anyAddress,
|
||||
WASMByteCode: wasmIdent,
|
||||
Source: "https://example.com/code",
|
||||
Builder: "foo/bar:latest",
|
||||
InstantiatePermission: &AllowEverybody,
|
||||
}
|
||||
for _, m := range mutators {
|
||||
m(r)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func MsgInstantiateContractFixture(mutators ...func(*MsgInstantiateContract)) *MsgInstantiateContract {
|
||||
const anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du"
|
||||
r := &MsgInstantiateContract{
|
||||
Sender: anyAddress,
|
||||
Admin: anyAddress,
|
||||
CodeID: 1,
|
||||
Label: "testing",
|
||||
InitMsg: []byte(`{"foo":"bar"}`),
|
||||
InitFunds: sdk.Coins{{
|
||||
Denom: "stake",
|
||||
Amount: sdk.NewInt(1),
|
||||
}},
|
||||
}
|
||||
for _, m := range mutators {
|
||||
m(r)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func MsgExecuteContractFixture(mutators ...func(*MsgExecuteContract)) *MsgExecuteContract {
|
||||
const (
|
||||
anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du"
|
||||
firstContractAddress = "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5"
|
||||
)
|
||||
r := &MsgExecuteContract{
|
||||
Sender: anyAddress,
|
||||
Contract: firstContractAddress,
|
||||
Msg: []byte(`{"do":"something"}`),
|
||||
SentFunds: sdk.Coins{{
|
||||
Denom: "stake",
|
||||
Amount: sdk.NewInt(1),
|
||||
}},
|
||||
}
|
||||
for _, m := range mutators {
|
||||
m(r)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func StoreCodeProposalFixture(mutators ...func(*StoreCodeProposal)) *StoreCodeProposal {
|
||||
const anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du"
|
||||
p := &StoreCodeProposal{
|
||||
|
||||
Reference in New Issue
Block a user