Skip to content

Commit

Permalink
implement wasm module migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
yun-yeo committed Apr 26, 2021
1 parent dc54ff2 commit 9c9a023
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 110 deletions.
1 change: 0 additions & 1 deletion docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2197,7 +2197,6 @@ ContractInfo stores a WASM contract instance
| `code_id` | [uint64](#uint64) | | CodeID is the reference to the stored Wasm code |
| `init_msg` | [bytes](#bytes) | | InitMsg is the raw message used when instantiating a contract |
| `migratable` | [bool](#bool) | | Migratable is the flag to specify the contract migratability |
| `ibc_port_id` | [string](#string) | | IBCPortID is the ID used in ibc communication |



Expand Down
2 changes: 0 additions & 2 deletions proto/terra/wasm/v1beta1/wasm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,4 @@ message ContractInfo {
bytes init_msg = 4 [(gogoproto.moretags) = "yaml:\"init_msg\"", (gogoproto.casttype) = "encoding/json.RawMessage"];
// Migratable is the flag to specify the contract migratability
bool migratable = 5 [(gogoproto.moretags) = "yaml:\"migratable\""];
// IBCPortID is the ID used in ibc communication
string ibc_port_id = 6 [(gogoproto.moretags) = "yaml:\"ibc_port_id\"", (gogoproto.customname) = "IBCPortID"];
}
74 changes: 74 additions & 0 deletions x/wasm/legacy/v04/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// DONTCOVER
// nolint
package v04

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
ModuleName = "wasm"
)

type (
// Params wasm parameters
Params struct {
MaxContractSize uint64 `json:"max_contract_size" yaml:"max_contract_size"` // allowed max contract bytes size
MaxContractGas uint64 `json:"max_contract_gas" yaml:"max_contract_gas"` // allowed max gas usages per each contract execution
MaxContractMsgSize uint64 `json:"max_contract_msg_size" yaml:"max_contract_msg_size"` // allowed max contract exe msg bytes size
}

// GenesisState is the struct representation of the export genesis
GenesisState struct {
Params Params `json:"params" yaml:"params"`
LastCodeID uint64 `json:"last_code_id" yaml:"last_code_id"`
LastInstanceID uint64 `json:"last_instance_id" yaml:"last_instance_id"`
Codes []Code `json:"codes" yaml:"codes"`
Contracts []Contract `json:"contracts" yaml:"contracts"`
}
)

// Code struct encompasses CodeInfo and CodeBytes
type Code struct {
CodeInfo CodeInfo `json:"code_info"`
CodesBytes []byte `json:"code_bytes"`
}

// CodeInfo is data for the uploaded contract WASM code
type CodeInfo struct {
CodeID uint64 `json:"code_id"`
CodeHash []byte `json:"code_hash"`
Creator sdk.AccAddress `json:"creator"`
}

// Contract struct encompasses ContractAddress, ContractInfo, and ContractState
type Contract struct {
ContractInfo ContractInfo `json:"contract_info"`
ContractStore []Model `json:"contract_store"`
}

// ContractInfo stores a WASM contract instance
type ContractInfo struct {
Address sdk.AccAddress `json:"address"`
Owner sdk.AccAddress `json:"owner"`
CodeID uint64 `json:"code_id"`
InitMsg []byte `json:"init_msg"`
Migratable bool `json:"migratable"`
}

// Model is a struct that holds a KV pair
type Model struct {
Key []byte `json:"key"`
Value []byte `json:"value"`
}

// NewGenesisState creates a new GenesisState object
func NewGenesisState(params Params, lastCodeID, lastInstanceID uint64, codes []Code, contracts []Contract) GenesisState {
return GenesisState{
Params: params,
LastCodeID: lastCodeID,
LastInstanceID: lastInstanceID,
Codes: codes,
Contracts: contracts,
}
}
67 changes: 67 additions & 0 deletions x/wasm/legacy/v05/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package v05

import (
v04wasm "github.com/terra-project/core/x/wasm/legacy/v04"
v05wasm "github.com/terra-project/core/x/wasm/types"
)

// Migrate accepts exported v0.4 x/wasm and
// migrates it to v0.5 x/wasm genesis state. The migration includes:
//
// - Add new params for event and data size limit to x/wasm genesis state.
// - Re-encode in v0.5 GenesisState.
func Migrate(
wasmGenState v04wasm.GenesisState,
) *v05wasm.GenesisState {
codes := make([]v05wasm.Code, len(wasmGenState.Codes))
for i, c := range wasmGenState.Codes {
codes[i] = v05wasm.Code{
CodeInfo: v05wasm.CodeInfo{
CodeID: c.CodeInfo.CodeID,
CodeHash: c.CodeInfo.CodeHash,
Creator: c.CodeInfo.Creator.String(),
},
CodeBytes: c.CodesBytes,
}
}

contracts := make([]v05wasm.Contract, len(wasmGenState.Contracts))
for i, c := range wasmGenState.Contracts {
models := make([]v05wasm.Model, len(c.ContractStore))
for j, m := range c.ContractStore {
models[j] = v05wasm.Model{
Key: m.Key,
Value: m.Value,
}
}

contracts[i] = v05wasm.Contract{
ContractInfo: v05wasm.ContractInfo{
CodeID: c.ContractInfo.CodeID,
Address: c.ContractInfo.Address.String(),
Owner: c.ContractInfo.Owner.String(),
Migratable: c.ContractInfo.Migratable,
InitMsg: c.ContractInfo.InitMsg,
},
ContractStore: models,
}
}

return &v05wasm.GenesisState{
Params: v05wasm.Params{
MaxContractSize: wasmGenState.Params.MaxContractSize,
MaxContractMsgSize: wasmGenState.Params.MaxContractMsgSize,
MaxContractGas: wasmGenState.Params.MaxContractGas,
MaxContractDataSize: 256,
EventParams: v05wasm.EventParams{
MaxAttributeNum: 16,
MaxAttributeKeyLength: 64,
MaxAttributeValueLength: 256,
},
},
Codes: codes,
Contracts: contracts,
LastCodeID: wasmGenState.LastCodeID,
LastInstanceID: wasmGenState.LastInstanceID,
}
}
3 changes: 0 additions & 3 deletions x/wasm/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ const (

// RouterKey is the msg router key for the wasm module
RouterKey = ModuleName

// IBCPortIDPrefix is the prefix of IBC Port ID for wasm module
IBCPortIDPrefix = ModuleName + "."
)

// Keys for wasm store
Expand Down
151 changes: 47 additions & 104 deletions x/wasm/types/wasm.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9c9a023

Please sign in to comment.