two slices in GenState
This commit is contained in:
@@ -13,24 +13,20 @@ import (
|
||||
//
|
||||
// CONTRACT: all types of accounts must have been already initialized/created
|
||||
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {
|
||||
for id, info := range data.CodeInfos {
|
||||
bytecode := data.CodesBytes[id]
|
||||
newId, err := keeper.Create(ctx, info.Creator, bytecode)
|
||||
for _, code := range data.Codes {
|
||||
newId, err := keeper.Create(ctx, code.CodeInfo.Creator, code.CodesBytes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
newInfo := keeper.GetCodeInfo(ctx, newId)
|
||||
if !bytes.Equal(info.CodeHash, newInfo.CodeHash) {
|
||||
if !bytes.Equal(code.CodeInfo.CodeHash, newInfo.CodeHash) {
|
||||
panic("code hashes not same")
|
||||
}
|
||||
}
|
||||
|
||||
for i, addr := range data.ContractAddresses {
|
||||
info := data.ContractInfos[i]
|
||||
state := data.ContractStates[i]
|
||||
|
||||
keeper.setContractInfo(ctx, addr, info)
|
||||
keeper.setContractState(ctx, addr, state)
|
||||
for _, contract := range data.Contracts {
|
||||
keeper.setContractInfo(ctx, contract.ContractAddress, contract.ContractInfo)
|
||||
keeper.setContractState(ctx, contract.ContractAddress, contract.ContractState)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -39,20 +35,19 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {
|
||||
func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
|
||||
var genState types.GenesisState
|
||||
|
||||
maxCodeID := keeper.GetNextCodeID(ctx, types.KeyLastCodeID)
|
||||
maxCodeID := keeper.GetNextCodeID(ctx)
|
||||
for i := uint64(1); i < maxCodeID; i++ {
|
||||
genState.CodeInfos = append(genState.CodeInfos, *keeper.GetCodeInfo(ctx, i))
|
||||
bytecode, err := keeper.GetByteCode(ctx, i)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
genState.CodesBytes = append(genState.CodesBytes, bytecode)
|
||||
genState.Codes = append(genState.Codes, types.CodeData{
|
||||
CodeInfo: *keeper.GetCodeInfo(ctx, i),
|
||||
CodesBytes: bytecode,
|
||||
})
|
||||
}
|
||||
|
||||
keeper.ListContractInfo(ctx, func(addr sdk.AccAddress, contract types.Contract) bool {
|
||||
genState.ContractAddresses = append(genState.ContractAddresses, addr)
|
||||
genState.ContractInfos = append(genState.ContractInfos, contract)
|
||||
|
||||
contractStateIterator := keeper.GetContractState(ctx, addr)
|
||||
var state []types.Model
|
||||
for ; contractStateIterator.Valid(); contractStateIterator.Next() {
|
||||
@@ -62,7 +57,12 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
|
||||
}
|
||||
state = append(state, m)
|
||||
}
|
||||
genState.ContractStates = append(genState.ContractStates, state)
|
||||
|
||||
genState.Contracts = append(genState.Contracts, types.ContractData{
|
||||
ContractAddress: addr,
|
||||
ContractInfo: contract,
|
||||
ContractState: state,
|
||||
})
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
@@ -341,9 +341,9 @@ func (k Keeper) generateContractAddress(ctx sdk.Context, codeID uint64) sdk.AccA
|
||||
return addrFromUint64(contractID)
|
||||
}
|
||||
|
||||
func (k Keeper) GetNextID(ctx sdk.Context, lastIDKey []byte) uint64 {
|
||||
func (k Keeper) GetNextCodeID(ctx sdk.Context) uint64 {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
bz := store.Get(lastIDKey)
|
||||
bz := store.Get(types.KeyLastCodeID)
|
||||
id := uint64(1)
|
||||
if bz != nil {
|
||||
id = binary.BigEndian.Uint64(bz)
|
||||
|
||||
@@ -6,24 +6,25 @@ import (
|
||||
|
||||
// GenesisState is the struct representation of the export genesis
|
||||
type GenesisState struct {
|
||||
CodeInfos []CodeInfo `json:"code_infos"`
|
||||
CodesBytes [][]byte `json:"code_bytes"`
|
||||
ContractAddresses []sdk.AccAddress `json:"contract_addresses"`
|
||||
ContractInfos []Contract `json:"contract_infos"`
|
||||
ContractStates [][]Model `json:"contract_states"`
|
||||
Codes []CodeData `json:"codes"`
|
||||
Contracts []ContractData `json:"contracts"`
|
||||
}
|
||||
|
||||
// CodeData struct encompasses CodeInfo and CodeBytes
|
||||
type CodeData struct {
|
||||
CodeInfo CodeInfo `json:"code_info"`
|
||||
CodesBytes []byte `json:"code_bytes"`
|
||||
}
|
||||
|
||||
// ContractData struct encompasses ContractAddress, ContractInfo, and ContractState
|
||||
type ContractData struct {
|
||||
ContractAddress sdk.AccAddress `json:"contract_address"`
|
||||
ContractInfo Contract `json:"contract_info"`
|
||||
ContractState []Model `json:"contract_state"`
|
||||
}
|
||||
|
||||
// ValidateGenesis performs basic validation of supply genesis data returning an
|
||||
// error for any failed validation criteria.
|
||||
func ValidateGenesis(data GenesisState) error {
|
||||
if len(data.CodesBytes) != len(data.CodeInfos) {
|
||||
return ErrInvalidGenesis("length of Codes != length of Code Infos")
|
||||
}
|
||||
if len(data.ContractAddresses) != len(data.ContractInfos) {
|
||||
return ErrInvalidGenesis("invalid number of Contract Infos")
|
||||
}
|
||||
if len(data.ContractAddresses) != len(data.ContractStates) {
|
||||
return ErrInvalidGenesis("invalid number of Contract States")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user