From 0567f4a0a307744984d848944d08112e1b55c05a Mon Sep 17 00:00:00 2001 From: Pino' Surace Date: Tue, 13 Dec 2022 17:24:50 +0100 Subject: [PATCH] Fix review comments and add unit tests --- x/wasm/types/codec.go | 2 ++ x/wasm/types/tx.go | 10 ++++++- x/wasm/types/tx.pb.go | 2 +- x/wasm/types/tx_test.go | 62 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/x/wasm/types/codec.go b/x/wasm/types/codec.go index d1c2b9eb9..7d8ebcbe4 100644 --- a/x/wasm/types/codec.go +++ b/x/wasm/types/codec.go @@ -19,6 +19,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { //nolint:staticcheck cdc.RegisterConcrete(&MsgMigrateContract{}, "wasm/MsgMigrateContract", nil) cdc.RegisterConcrete(&MsgUpdateAdmin{}, "wasm/MsgUpdateAdmin", nil) cdc.RegisterConcrete(&MsgClearAdmin{}, "wasm/MsgClearAdmin", nil) + cdc.RegisterConcrete(&MsgUpdateInstantiateConfig{}, "wasm/MsgUpdateInstantiateConfig", nil) cdc.RegisterConcrete(&PinCodesProposal{}, "wasm/PinCodesProposal", nil) cdc.RegisterConcrete(&UnpinCodesProposal{}, "wasm/UnpinCodesProposal", nil) @@ -61,6 +62,7 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { &MsgClearAdmin{}, &MsgIBCCloseChannel{}, &MsgIBCSend{}, + &MsgUpdateInstantiateConfig{}, ) registry.RegisterImplementations( (*govtypes.Content)(nil), diff --git a/x/wasm/types/tx.go b/x/wasm/types/tx.go index 6a67a95ef..5739721ff 100644 --- a/x/wasm/types/tx.go +++ b/x/wasm/types/tx.go @@ -406,7 +406,15 @@ func (msg MsgUpdateInstantiateConfig) Type() string { func (msg MsgUpdateInstantiateConfig) ValidateBasic() error { if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil { - return err + return sdkerrors.Wrap(err, "sender") + } + + if msg.CodeID == 0 { + return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "code id is required") + } + + if msg.NewInstantiatePermission == nil { + return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "instantiate permission is required") } if err := msg.NewInstantiatePermission.ValidateBasic(); err != nil { diff --git a/x/wasm/types/tx.pb.go b/x/wasm/types/tx.pb.go index 2ba189cbf..ed9d4add6 100644 --- a/x/wasm/types/tx.pb.go +++ b/x/wasm/types/tx.pb.go @@ -704,7 +704,7 @@ var xxx_messageInfo_MsgClearAdminResponse proto.InternalMessageInfo type MsgUpdateInstantiateConfig struct { // Sender is the that actor that signed the messages Sender string `protobuf:"bytes,1,opt,name=sender,proto3" json:"sender,omitempty"` - // CodeID references the new WASM code + // CodeID references the stored WASM code CodeID uint64 `protobuf:"varint,2,opt,name=code_id,json=codeId,proto3" json:"code_id,omitempty"` // NewInstantiatePermission is the new access control NewInstantiatePermission *AccessConfig `protobuf:"bytes,3,opt,name=new_instantiate_permission,json=newInstantiatePermission,proto3" json:"new_instantiate_permission,omitempty"` diff --git a/x/wasm/types/tx_test.go b/x/wasm/types/tx_test.go index a3c29b4f8..10c5c8b52 100644 --- a/x/wasm/types/tx_test.go +++ b/x/wasm/types/tx_test.go @@ -679,3 +679,65 @@ func TestMsgJsonSignBytes(t *testing.T) { }) } } + +func TestMsgUpdateInstantiateConfig(t *testing.T) { + bad, err := sdk.AccAddressFromHex("012345") + require.NoError(t, err) + badAddress := bad.String() + // proper address size + goodAddress := sdk.AccAddress(make([]byte, 20)).String() + anotherGoodAddress := sdk.AccAddress(bytes.Repeat([]byte{0x2}, 20)).String() + + specs := map[string]struct { + src MsgUpdateInstantiateConfig + expErr bool + }{ + "all good": { + src: MsgUpdateInstantiateConfig{ + Sender: goodAddress, + CodeID: 1, + NewInstantiatePermission: &AccessConfig{Permission: AccessTypeOnlyAddress, Address: anotherGoodAddress}, + }, + }, + "bad sender": { + src: MsgUpdateInstantiateConfig{ + Sender: badAddress, + CodeID: 1, + NewInstantiatePermission: &AccessConfig{Permission: AccessTypeOnlyAddress, Address: anotherGoodAddress}, + }, + expErr: true, + }, + "invalid NewInstantiatePermission": { + src: MsgUpdateInstantiateConfig{ + Sender: goodAddress, + CodeID: 1, + NewInstantiatePermission: &AccessConfig{Permission: AccessTypeOnlyAddress, Address: badAddress}, + }, + expErr: true, + }, + "missing code id": { + src: MsgUpdateInstantiateConfig{ + Sender: goodAddress, + NewInstantiatePermission: &AccessConfig{Permission: AccessTypeOnlyAddress, Address: anotherGoodAddress}, + }, + expErr: true, + }, + "missing NewInstantiatePermission": { + src: MsgUpdateInstantiateConfig{ + Sender: goodAddress, + CodeID: 1, + }, + expErr: true, + }, + } + for msg, spec := range specs { + t.Run(msg, func(t *testing.T) { + err := spec.src.ValidateBasic() + if spec.expErr { + require.Error(t, err) + return + } + require.NoError(t, err) + }) + } +}