Skip to content
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 StoreAndInstantiateContract gov proposal #1074

Merged
merged 21 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- [InstantiateContractProposal](#cosmwasm.wasm.v1.InstantiateContractProposal)
- [MigrateContractProposal](#cosmwasm.wasm.v1.MigrateContractProposal)
- [PinCodesProposal](#cosmwasm.wasm.v1.PinCodesProposal)
- [StoreAndInstantiateContractProposal](#cosmwasm.wasm.v1.StoreAndInstantiateContractProposal)
- [StoreCodeProposal](#cosmwasm.wasm.v1.StoreCodeProposal)
- [SudoContractProposal](#cosmwasm.wasm.v1.SudoContractProposal)
- [UnpinCodesProposal](#cosmwasm.wasm.v1.UnpinCodesProposal)
Expand Down Expand Up @@ -810,6 +811,31 @@ wasmvm cache.



<a name="cosmwasm.wasm.v1.StoreAndInstantiateContractProposal"></a>

### StoreAndInstantiateContractProposal
StoreAndInstantiateContractProposal gov proposal content type to store
and instantiate the contract.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `title` | [string](#string) | | Title is a short summary |
| `description` | [string](#string) | | Description is a human readable text |
| `run_as` | [string](#string) | | RunAs is the address that is passed to the contract's environment as sender |
| `wasm_byte_code` | [bytes](#bytes) | | WASMByteCode can be raw or gzip compressed |
| `instantiate_permission` | [AccessConfig](#cosmwasm.wasm.v1.AccessConfig) | | InstantiatePermission to apply on contract creation, optional |
| `unpin_code` | [bool](#bool) | | UnpinCode code on upload, optional |
| `admin` | [string](#string) | | Admin is an optional address that can execute migrations |
| `label` | [string](#string) | | Label is optional metadata to be stored with a constract instance. |
| `msg` | [bytes](#bytes) | | Msg json encoded message to be passed to the contract on instantiation |
| `funds` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | Funds coins that are transferred to the contract on instantiation |






<a name="cosmwasm.wasm.v1.StoreCodeProposal"></a>

### StoreCodeProposal
Expand Down
28 changes: 28 additions & 0 deletions proto/cosmwasm/wasm/v1/proposal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,31 @@ message UpdateInstantiateConfigProposal {
repeated AccessConfigUpdate access_config_updates = 3
[ (gogoproto.nullable) = false ];
}

// StoreAndInstantiateContractProposal gov proposal content type to store
// and instantiate the contract.
message StoreAndInstantiateContractProposal {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This looks complete

// Title is a short summary
string title = 1;
// Description is a human readable text
string description = 2;
// RunAs is the address that is passed to the contract's environment as sender
string run_as = 3;
// WASMByteCode can be raw or gzip compressed
bytes wasm_byte_code = 4 [ (gogoproto.customname) = "WASMByteCode" ];
// InstantiatePermission to apply on contract creation, optional
AccessConfig instantiate_permission = 5;
// UnpinCode code on upload, optional
bool unpin_code = 6;
// Admin is an optional address that can execute migrations
string admin = 7;
// Label is optional metadata to be stored with a constract instance.
string label = 8;
// Msg json encoded message to be passed to the contract on instantiation
bytes msg = 9 [ (gogoproto.casttype) = "RawContractMessage" ];
// Funds coins that are transferred to the contract on instantiation
repeated cosmos.base.v1beta1.Coin funds = 10 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}
3 changes: 2 additions & 1 deletion x/wasm/Governance.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ We have added 9 new wasm specific proposal types that cover the contract's live
* `ClearAdminProposal` - clear admin for a contract to prevent further migrations
* `PinCodes` - pin the given code ids in cache. This trades memory for reduced startup time and lowers gas cost
* `UnpinCodes` - unpin the given code ids from the cache. This frees up memory and returns to standard speed and gas cost
* `UpdateInstantiateConfigProposal` - update instantiate permissions to a list of given code ids.
* `UpdateInstantiateConfigProposal` - update instantiate permissions to a list of given code ids.
* `StoreAndInstantiateContractProposal` - upload and instantiate a wasm contract.

For details see the proposal type [implementation](https://github.com/CosmWasm/wasmd/blob/master/x/wasm/types/proposal.go)

Expand Down
24 changes: 24 additions & 0 deletions x/wasm/keeper/contract_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ type decoratedKeeper interface {
authZ AuthorizationPolicy,
) (sdk.AccAddress, []byte, error)

createAndInstantiate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to optimize for this worflow. Let's stick with the existing create and instantiate methods when used as a library

ctx sdk.Context,
creator, admin sdk.AccAddress,
wasmCode []byte,
instantiateAccess *types.AccessConfig,
initMsg []byte,
label string,
deposit sdk.Coins,
addressGenerator AddressGenerator,
authZ AuthorizationPolicy,
) (sdk.AccAddress, []byte, error)

migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller sdk.AccAddress, newCodeID uint64, msg []byte, authZ AuthorizationPolicy) ([]byte, error)
setContractAdmin(ctx sdk.Context, contractAddress, caller, newAdmin sdk.AccAddress, authZ AuthorizationPolicy) error
pinCode(ctx sdk.Context, codeID uint64) error
Expand Down Expand Up @@ -128,3 +140,15 @@ func (p PermissionedKeeper) SetContractInfoExtension(ctx sdk.Context, contract s
func (p PermissionedKeeper) SetAccessConfig(ctx sdk.Context, codeID uint64, caller sdk.AccAddress, newConfig types.AccessConfig) error {
return p.nested.setAccessConfig(ctx, codeID, caller, newConfig, p.authZPolicy)
}

func (p PermissionedKeeper) CreateAndInstantiate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove. No need to expose this

ctx sdk.Context,
creator, admin sdk.AccAddress,
wasmCode []byte,
instantiateAccess *types.AccessConfig,
initMsg []byte,
label string,
deposit sdk.Coins,
) (sdk.AccAddress, []byte, error) {
return p.nested.createAndInstantiate(ctx, creator, admin, wasmCode, instantiateAccess, initMsg, label, deposit, p.nested.ClassicAddressGenerator(), p.authZPolicy)
}
18 changes: 18 additions & 0 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,24 @@ func (k Keeper) instantiate(
return contractAddress, data, nil
}

func (k Keeper) createAndInstantiate(
ctx sdk.Context,
creator, admin sdk.AccAddress,
wasmCode []byte,
instantiateAccess *types.AccessConfig,
initMsg []byte,
label string,
deposit sdk.Coins,
addressGenerator AddressGenerator,
authZ AuthorizationPolicy,
) (sdk.AccAddress, []byte, error) {
codeID, _, err := k.create(ctx, creator, wasmCode, instantiateAccess, authZ)
if err != nil {
return nil, nil, err
}
return k.instantiate(ctx, codeID, creator, admin, initMsg, label, deposit, addressGenerator, authZ)
}

// Execute executes the contract instance
func (k Keeper) execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller sdk.AccAddress, msg []byte, coins sdk.Coins) ([]byte, error) {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "execute")
Expand Down
40 changes: 40 additions & 0 deletions x/wasm/keeper/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func NewWasmProposalHandlerX(k types.ContractOpsKeeper, enabledProposalTypes []t
return handleUnpinCodesProposal(ctx, k, *c)
case *types.UpdateInstantiateConfigProposal:
return handleUpdateInstantiateConfigProposal(ctx, k, *c)
case *types.StoreAndInstantiateContractProposal:
return handleStoreAndInstantiateContractProposal(ctx, k, *c)
default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized wasm proposal content type: %T", c)
}
Expand Down Expand Up @@ -103,6 +105,44 @@ func handleInstantiateProposal(ctx sdk.Context, k types.ContractOpsKeeper, p typ
return nil
}

func handleStoreAndInstantiateContractProposal(ctx sdk.Context, k types.ContractOpsKeeper, p types.StoreAndInstantiateContractProposal) error {
if err := p.ValidateBasic(); err != nil {
return err
}
runAsAddr, err := sdk.AccAddressFromBech32(p.RunAs)
if err != nil {
return sdkerrors.Wrap(err, "run as address")
}
var adminAddr sdk.AccAddress
if p.Admin != "" {
if adminAddr, err = sdk.AccAddressFromBech32(p.Admin); err != nil {
return sdkerrors.Wrap(err, "admin")
}
}

codeID, _, err := k.Create(ctx, runAsAddr, p.WASMByteCode, p.InstantiatePermission)
if err != nil {
return err
}

if !p.UnpinCode {
if err := k.PinCode(ctx, codeID); err != nil {
return err
}
}

_, data, err := k.Instantiate(ctx, codeID, runAsAddr, adminAddr, p.Msg, p.Label, p.Funds)
if err != nil {
return err
}

ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeGovContractResult,
sdk.NewAttribute(types.AttributeKeyResultDataHex, hex.EncodeToString(data)),
))
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

}

func handleMigrateProposal(ctx sdk.Context, k types.ContractOpsKeeper, p types.MigrateContractProposal) error {
if err := p.ValidateBasic(); err != nil {
return err
Expand Down
60 changes: 60 additions & 0 deletions x/wasm/keeper/proposal_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,66 @@ func TestInstantiateProposal_NoAdmin(t *testing.T) {
require.NotEmpty(t, em.Events()[2].Attributes[0])
}

func TestStoreAndInstantiateContractProposal(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)

var (
oneAddress sdk.AccAddress = bytes.Repeat([]byte{0x1}, types.ContractAddrLen)
otherAddress sdk.AccAddress = bytes.Repeat([]byte{0x2}, types.ContractAddrLen)
)

src := types.StoreAndInstantiateContractProposalFixture(func(p *types.StoreAndInstantiateContractProposal) {
p.WASMByteCode = wasmCode
p.RunAs = oneAddress.String()
p.Admin = otherAddress.String()
p.Label = "testing"
})
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)

// then
contractAddr, err := sdk.AccAddressFromBech32("cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr")
require.NoError(t, err)

cInfo := wasmKeeper.GetContractInfo(ctx, contractAddr)
require.NotNil(t, cInfo)
assert.Equal(t, oneAddress.String(), cInfo.Creator)
assert.Equal(t, otherAddress.String(), cInfo.Admin)
assert.Equal(t, "testing", cInfo.Label)
expHistory := []types.ContractCodeHistoryEntry{{
Operation: types.ContractCodeHistoryOperationTypeInit,
CodeID: cInfo.CodeID,
Updated: types.NewAbsoluteTxPosition(ctx),
Msg: src.Msg,
}}
assert.Equal(t, expHistory, wasmKeeper.GetContractHistory(ctx, contractAddr))
// and event
require.Len(t, em.Events(), 5, "%#v", em.Events())
require.Equal(t, types.EventTypeStoreCode, em.Events()[0].Type)
require.Equal(t, types.EventTypePinCode, em.Events()[1].Type)
require.Equal(t, types.EventTypeInstantiate, em.Events()[2].Type)
require.Equal(t, types.WasmModuleEventType, em.Events()[3].Type)
require.Equal(t, types.EventTypeGovContractResult, em.Events()[4].Type)
require.Len(t, em.Events()[4].Attributes, 1)
require.NotEmpty(t, em.Events()[4].Attributes[0])
}
alpe marked this conversation as resolved.
Show resolved Hide resolved

func TestMigrateProposal(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, "staking")
govKeeper, wasmKeeper := keepers.GovKeeper, keepers.WasmKeeper
Expand Down
2 changes: 2 additions & 0 deletions x/wasm/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { //nolint:staticcheck
cdc.RegisterConcrete(&UpdateAdminProposal{}, "wasm/UpdateAdminProposal", nil)
cdc.RegisterConcrete(&ClearAdminProposal{}, "wasm/ClearAdminProposal", nil)
cdc.RegisterConcrete(&UpdateInstantiateConfigProposal{}, "wasm/UpdateInstantiateConfigProposal", nil)
cdc.RegisterConcrete(&StoreAndInstantiateContractProposal{}, "wasm/StoreAndInstantiateContractProposal", nil)
}

func RegisterInterfaces(registry types.InterfaceRegistry) {
Expand Down Expand Up @@ -56,6 +57,7 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
&PinCodesProposal{},
&UnpinCodesProposal{},
&UpdateInstantiateConfigProposal{},
&StoreAndInstantiateContractProposal{},
alpe marked this conversation as resolved.
Show resolved Hide resolved
)

registry.RegisterInterface("ContractInfoExtension", (*ContractInfoExtension)(nil))
Expand Down
10 changes: 10 additions & 0 deletions x/wasm/types/exported_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ type ContractOpsKeeper interface {
fixMsg bool,
) (sdk.AccAddress, []byte, error)

CreateAndInstantiate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to export this.

ctx sdk.Context,
creator, admin sdk.AccAddress,
wasmCode []byte,
instantiateAccess *AccessConfig,
initMsg []byte,
label string,
deposit sdk.Coins,
) (sdk.AccAddress, []byte, error)

// Execute executes the contract instance
Execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller sdk.AccAddress, msg []byte, coins sdk.Coins) ([]byte, error)

Expand Down
Loading