Skip to content

Commit

Permalink
Merge pull request #808 from envoylabs/sudo_proposals
Browse files Browse the repository at this point in the history
Fix SudoContractProposal and ExecuteContractProposal
  • Loading branch information
webmaster128 authored Apr 22, 2022
2 parents dc5ef6f + 190b8cc commit d5ef3ba
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 1 deletion.
6 changes: 5 additions & 1 deletion x/wasm/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -44,6 +46,8 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
&StoreCodeProposal{},
&InstantiateContractProposal{},
&MigrateContractProposal{},
&SudoContractProposal{},
&ExecuteContractProposal{},
&UpdateAdminProposal{},
&ClearAdminProposal{},
&PinCodesProposal{},
Expand Down
116 changes: 116 additions & 0 deletions x/wasm/types/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,122 @@ func TestValidateMigrateContractProposal(t *testing.T) {
}
}

func TestValidateSudoContractProposal(t *testing.T) {
var (
invalidAddress = "invalid address"
)

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 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"
Expand Down
42 changes: 42 additions & 0 deletions x/wasm/types/test_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,48 @@ func MigrateContractProposalFixture(mutators ...func(p *MigrateContractProposal)
return p
}

func SudoContractProposalFixture(mutators ...func(p *SudoContractProposal)) *SudoContractProposal {
const (
contractAddr = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr"
)

p := &SudoContractProposal{
Title: "Foo",
Description: "Bar",
Contract: contractAddr,
Msg: []byte(`{"do":"something"}`),
}

for _, m := range mutators {
m(p)
}
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"
Expand Down

0 comments on commit d5ef3ba

Please sign in to comment.