-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement wasm module migration script
- Loading branch information
Showing
6 changed files
with
188 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.