-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add proposal instantiate 2 #1106
Changes from all commits
e6291ad
e06cac8
19ba692
f45ad16
eacab0c
9fa4051
ce1c282
cdec778
a741389
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,8 @@ func NewWasmProposalHandlerX(k types.ContractOpsKeeper, enabledProposalTypes []t | |
return handleStoreCodeProposal(ctx, k, *c) | ||
case *types.InstantiateContractProposal: | ||
return handleInstantiateProposal(ctx, k, *c) | ||
case *types.InstantiateContract2Proposal: | ||
return handleInstantiate2Proposal(ctx, k, *c) | ||
case *types.MigrateContractProposal: | ||
return handleMigrateProposal(ctx, k, *c) | ||
case *types.SudoContractProposal: | ||
|
@@ -111,6 +113,38 @@ func handleInstantiateProposal(ctx sdk.Context, k types.ContractOpsKeeper, p typ | |
return nil | ||
} | ||
|
||
func handleInstantiate2Proposal(ctx sdk.Context, k types.ContractOpsKeeper, p types.InstantiateContract2Proposal) error { | ||
// Validatebasic with proposal | ||
if err := p.ValidateBasic(); err != nil { | ||
return err | ||
} | ||
|
||
// Get runAsAddr as AccAddress | ||
runAsAddr, err := sdk.AccAddressFromBech32(p.RunAs) | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "run as address") | ||
} | ||
|
||
// Get admin address | ||
var adminAddr sdk.AccAddress | ||
if p.Admin != "" { | ||
if adminAddr, err = sdk.AccAddressFromBech32(p.Admin); err != nil { | ||
return sdkerrors.Wrap(err, "admin") | ||
} | ||
} | ||
|
||
_, data, err := k.Instantiate2(ctx, p.CodeID, runAsAddr, adminAddr, p.Msg, p.Label, p.Funds, p.Salt, p.FixMsg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :thu |
||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx.EventManager().EmitEvent(sdk.NewEvent( | ||
types.EventTypeGovContractResult, | ||
sdk.NewAttribute(types.AttributeKeyResultDataHex, hex.EncodeToString(data)), | ||
)) | ||
return nil | ||
} | ||
|
||
func handleStoreAndInstantiateContractProposal(ctx sdk.Context, k types.ContractOpsKeeper, p types.StoreAndInstantiateContractProposal) error { | ||
if err := p.ValidateBasic(); err != nil { | ||
return err | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,70 @@ func TestInstantiateProposal(t *testing.T) { | |
require.NotEmpty(t, em.Events()[2].Attributes[0]) | ||
} | ||
|
||
func TestInstantiate2Proposal(t *testing.T) { | ||
ctx, keepers := CreateTestInput(t, false, "staking") | ||
govKeeper, wasmKeeper := keepers.GovKeeper, keepers.WasmKeeper | ||
wasmKeeper.SetParams(ctx, types.Params{ | ||
CodeUploadAccess: types.AllowNobody, | ||
InstantiateDefaultPermission: types.AccessTypeNobody, | ||
}) | ||
|
||
wasmCode, err := os.ReadFile("./testdata/hackatom.wasm") | ||
require.NoError(t, err) | ||
|
||
codeInfo := types.CodeInfoFixture(types.WithSHA256CodeHash(wasmCode)) | ||
err = wasmKeeper.importCode(ctx, 1, codeInfo, wasmCode) | ||
require.NoError(t, err) | ||
|
||
var ( | ||
oneAddress sdk.AccAddress = bytes.Repeat([]byte{0x1}, types.ContractAddrLen) | ||
otherAddress sdk.AccAddress = bytes.Repeat([]byte{0x2}, types.ContractAddrLen) | ||
label string = "label" | ||
salt []byte = []byte("mySalt") | ||
) | ||
src := types.InstantiateContract2ProposalFixture(func(p *types.InstantiateContract2Proposal) { | ||
p.CodeID = firstCodeID | ||
p.RunAs = oneAddress.String() | ||
p.Admin = otherAddress.String() | ||
p.Label = label | ||
p.Salt = salt | ||
}) | ||
contractAddress := BuildContractAddressPredictable(codeInfo.CodeHash, oneAddress, salt, []byte{}) | ||
|
||
em := sdk.NewEventManager() | ||
|
||
// when stored | ||
storedProposal, err := govKeeper.SubmitProposal(ctx, src) | ||
require.NoError(t, err) | ||
|
||
// and proposal execute | ||
handler := govKeeper.Router().GetRoute(storedProposal.ProposalRoute()) | ||
err = handler(ctx.WithEventManager(em), storedProposal.GetContent()) | ||
require.NoError(t, err) | ||
|
||
cInfo := wasmKeeper.GetContractInfo(ctx, contractAddress) | ||
require.NotNil(t, cInfo) | ||
|
||
assert.Equal(t, uint64(1), cInfo.CodeID) | ||
assert.Equal(t, oneAddress.String(), cInfo.Creator) | ||
assert.Equal(t, otherAddress.String(), cInfo.Admin) | ||
assert.Equal(t, "label", cInfo.Label) | ||
expHistory := []types.ContractCodeHistoryEntry{{ | ||
Operation: types.ContractCodeHistoryOperationTypeInit, | ||
CodeID: src.CodeID, | ||
Updated: types.NewAbsoluteTxPosition(ctx), | ||
Msg: src.Msg, | ||
}} | ||
assert.Equal(t, expHistory, wasmKeeper.GetContractHistory(ctx, contractAddress)) | ||
// and event | ||
require.Len(t, em.Events(), 3, "%#v", em.Events()) | ||
require.Equal(t, types.EventTypeInstantiate, em.Events()[0].Type) | ||
require.Equal(t, types.WasmModuleEventType, em.Events()[1].Type) | ||
require.Equal(t, types.EventTypeGovContractResult, em.Events()[2].Type) | ||
require.Len(t, em.Events()[2].Attributes, 1) | ||
require.NotEmpty(t, em.Events()[2].Attributes[0]) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good test |
||
func TestInstantiateProposal_NoAdmin(t *testing.T) { | ||
ctx, keepers := CreateTestInput(t, false, "staking") | ||
govKeeper, wasmKeeper := keepers.GovKeeper, keepers.WasmKeeper | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 easy to miss