From e86a13d8384326908adccf8beed9e7f66c2bcd13 Mon Sep 17 00:00:00 2001 From: Alex Lynham Date: Sun, 17 Apr 2022 22:29:30 +0100 Subject: [PATCH 1/3] Add missing sudo and execute to codec --- x/wasm/types/codec.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/x/wasm/types/codec.go b/x/wasm/types/codec.go index 007f42ed4b..1d2114a576 100644 --- a/x/wasm/types/codec.go +++ b/x/wasm/types/codec.go @@ -17,12 +17,14 @@ 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(&PinCodesProposal{}, "wasm/PinCodesProposal", nil) cdc.RegisterConcrete(&UnpinCodesProposal{}, "wasm/UnpinCodesProposal", nil) - cdc.RegisterConcrete(&StoreCodeProposal{}, "wasm/StoreCodeProposal", nil) cdc.RegisterConcrete(&InstantiateContractProposal{}, "wasm/InstantiateContractProposal", nil) cdc.RegisterConcrete(&MigrateContractProposal{}, "wasm/MigrateContractProposal", nil) + cdc.RegisterConcrete(&SudoContractProposal{}, "wasm/SudoContractProposal", nil) + cdc.RegisterConcrete(&ExecuteContractProposal{}, "wasm/ExecuteContractProposal", nil) cdc.RegisterConcrete(&UpdateAdminProposal{}, "wasm/UpdateAdminProposal", nil) cdc.RegisterConcrete(&ClearAdminProposal{}, "wasm/ClearAdminProposal", nil) } @@ -44,6 +46,8 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { &StoreCodeProposal{}, &InstantiateContractProposal{}, &MigrateContractProposal{}, + &SudoContractProposal{}, + &ExecuteContractProposal{}, &UpdateAdminProposal{}, &ClearAdminProposal{}, &PinCodesProposal{}, From 64e1fca042d3f604c9a29c35587da315bdc29152 Mon Sep 17 00:00:00 2001 From: Alex Lynham Date: Sun, 17 Apr 2022 22:56:09 +0100 Subject: [PATCH 2/3] Add a basic validation test --- x/wasm/types/proposal_test.go | 55 +++++++++++++++++++++++++++++++++++ x/wasm/types/test_fixtures.go | 19 ++++++++++++ 2 files changed, 74 insertions(+) diff --git a/x/wasm/types/proposal_test.go b/x/wasm/types/proposal_test.go index dfae82698b..3c7b03f1d4 100644 --- a/x/wasm/types/proposal_test.go +++ b/x/wasm/types/proposal_test.go @@ -317,6 +317,61 @@ func TestValidateMigrateContractProposal(t *testing.T) { } } +func TestValidateSudoContractProposal(t *testing.T) { + var ( + invalidAddress = "invalid address2" + ) + + specs := map[string]struct { + src *SudoContractProposal + expErr bool + }{ + "all good": { + src: SudoContractProposalFixture(), + }, + "msg is nil": { + src: SudoContractProposalFixture(func(p *SudoContractProposal) { + p.Msg = nil + }), + expErr: true, + }, + "msg with invalid json": { + src: SudoContractProposalFixture(func(p *SudoContractProposal) { + p.Msg = []byte("not a json message") + }), + expErr: true, + }, + "base data missing": { + src: SudoContractProposalFixture(func(p *SudoContractProposal) { + p.Title = "" + }), + expErr: true, + }, + "contract missing": { + src: SudoContractProposalFixture(func(p *SudoContractProposal) { + p.Contract = "" + }), + expErr: true, + }, + "contract invalid": { + src: SudoContractProposalFixture(func(p *SudoContractProposal) { + p.Contract = invalidAddress + }), + 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) + } else { + require.NoError(t, err) + } + }) + } +} + func TestValidateUpdateAdminProposal(t *testing.T) { var ( invalidAddress = "invalid address" diff --git a/x/wasm/types/test_fixtures.go b/x/wasm/types/test_fixtures.go index 568c4f7c91..86cc4a5ae7 100644 --- a/x/wasm/types/test_fixtures.go +++ b/x/wasm/types/test_fixtures.go @@ -257,6 +257,25 @@ func MigrateContractProposalFixture(mutators ...func(p *MigrateContractProposal) return p } +func SudoContractProposalFixture(mutators ...func(p *SudoContractProposal)) *SudoContractProposal { + const ( + contractAddr = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr" + anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqs2m6sx4" + ) + + p := &SudoContractProposal{ + Title: "Foo", + Description: "Bar", + Contract: contractAddr, + Msg: []byte(`{"do":"something"}`), + } + + for _, m := range mutators { + m(p) + } + return p +} + func UpdateAdminProposalFixture(mutators ...func(p *UpdateAdminProposal)) *UpdateAdminProposal { const ( contractAddr = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr" From 190b8cc3063ad1ec344884078db4bd53b0ed83bd Mon Sep 17 00:00:00 2001 From: Alex Lynham Date: Thu, 21 Apr 2022 22:46:51 +0100 Subject: [PATCH 3/3] Add ExecuteContractProposal test --- x/wasm/types/proposal_test.go | 63 ++++++++++++++++++++++++++++++++++- x/wasm/types/test_fixtures.go | 25 +++++++++++++- 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/x/wasm/types/proposal_test.go b/x/wasm/types/proposal_test.go index 3c7b03f1d4..6ccd4b1a79 100644 --- a/x/wasm/types/proposal_test.go +++ b/x/wasm/types/proposal_test.go @@ -319,7 +319,7 @@ func TestValidateMigrateContractProposal(t *testing.T) { func TestValidateSudoContractProposal(t *testing.T) { var ( - invalidAddress = "invalid address2" + invalidAddress = "invalid address" ) specs := map[string]struct { @@ -372,6 +372,67 @@ func TestValidateSudoContractProposal(t *testing.T) { } } +func TestValidateExecuteContractProposal(t *testing.T) { + var ( + invalidAddress = "invalid address" + ) + + specs := map[string]struct { + src *ExecuteContractProposal + expErr bool + }{ + "all good": { + src: ExecuteContractProposalFixture(), + }, + "msg is nil": { + src: ExecuteContractProposalFixture(func(p *ExecuteContractProposal) { + p.Msg = nil + }), + expErr: true, + }, + "msg with invalid json": { + src: ExecuteContractProposalFixture(func(p *ExecuteContractProposal) { + p.Msg = []byte("not a valid json message") + }), + expErr: true, + }, + "base data missing": { + src: ExecuteContractProposalFixture(func(p *ExecuteContractProposal) { + p.Title = "" + }), + expErr: true, + }, + "contract missing": { + src: ExecuteContractProposalFixture(func(p *ExecuteContractProposal) { + p.Contract = "" + }), + expErr: true, + }, + "contract invalid": { + src: ExecuteContractProposalFixture(func(p *ExecuteContractProposal) { + p.Contract = invalidAddress + }), + expErr: true, + }, + "run as is invalid": { + src: ExecuteContractProposalFixture(func(p *ExecuteContractProposal) { + p.RunAs = invalidAddress + }), + 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) + } else { + require.NoError(t, err) + } + }) + } +} + func TestValidateUpdateAdminProposal(t *testing.T) { var ( invalidAddress = "invalid address" diff --git a/x/wasm/types/test_fixtures.go b/x/wasm/types/test_fixtures.go index 86cc4a5ae7..ca3a919b5a 100644 --- a/x/wasm/types/test_fixtures.go +++ b/x/wasm/types/test_fixtures.go @@ -260,7 +260,6 @@ func MigrateContractProposalFixture(mutators ...func(p *MigrateContractProposal) func SudoContractProposalFixture(mutators ...func(p *SudoContractProposal)) *SudoContractProposal { const ( contractAddr = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr" - anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqs2m6sx4" ) p := &SudoContractProposal{ @@ -276,6 +275,30 @@ func SudoContractProposalFixture(mutators ...func(p *SudoContractProposal)) *Sud return p } +func ExecuteContractProposalFixture(mutators ...func(p *ExecuteContractProposal)) *ExecuteContractProposal { + const ( + contractAddr = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr" + anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqs2m6sx4" + ) + + p := &ExecuteContractProposal{ + Title: "Foo", + Description: "Bar", + Contract: contractAddr, + RunAs: anyAddress, + Msg: []byte(`{"do":"something"}`), + Funds: sdk.Coins{{ + Denom: "stake", + Amount: sdk.NewInt(1), + }}, + } + + for _, m := range mutators { + m(p) + } + return p +} + func UpdateAdminProposalFixture(mutators ...func(p *UpdateAdminProposal)) *UpdateAdminProposal { const ( contractAddr = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr"