-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from sikkatech/master
Added genesis functions
- Loading branch information
Showing
9 changed files
with
159 additions
and
51 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 was deleted.
Oops, something went wrong.
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,71 @@ | ||
package keeper | ||
|
||
import ( | ||
"bytes" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmwasm/wasmd/x/wasm/internal/types" | ||
// authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" | ||
// "github.com/cosmwasm/wasmd/x/wasm/internal/types" | ||
) | ||
|
||
// InitGenesis sets supply information for genesis. | ||
// | ||
// CONTRACT: all types of accounts must have been already initialized/created | ||
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) { | ||
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(code.CodeInfo.CodeHash, newInfo.CodeHash) { | ||
panic("code hashes not same") | ||
} | ||
} | ||
|
||
for _, contract := range data.Contracts { | ||
keeper.setContractInfo(ctx, contract.ContractAddress, contract.ContractInfo) | ||
keeper.setContractState(ctx, contract.ContractAddress, contract.ContractState) | ||
} | ||
|
||
} | ||
|
||
// ExportGenesis returns a GenesisState for a given context and keeper. | ||
func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState { | ||
var genState types.GenesisState | ||
|
||
maxCodeID := keeper.GetNextCodeID(ctx) | ||
for i := uint64(1); i < maxCodeID; i++ { | ||
bytecode, err := keeper.GetByteCode(ctx, i) | ||
if err != nil { | ||
panic(err) | ||
} | ||
genState.Codes = append(genState.Codes, types.Code{ | ||
CodeInfo: *keeper.GetCodeInfo(ctx, i), | ||
CodesBytes: bytecode, | ||
}) | ||
} | ||
|
||
keeper.ListContractInfo(ctx, func(addr sdk.AccAddress, contract types.ContractInfo) bool { | ||
contractStateIterator := keeper.GetContractState(ctx, addr) | ||
var state []types.Model | ||
for ; contractStateIterator.Valid(); contractStateIterator.Next() { | ||
m := types.Model{ | ||
Key: string(contractStateIterator.Key()), | ||
Value: string(contractStateIterator.Value()), | ||
} | ||
state = append(state, m) | ||
} | ||
|
||
genState.Contracts = append(genState.Contracts, types.Contract{ | ||
ContractAddress: addr, | ||
ContractInfo: contract, | ||
ContractState: state, | ||
}) | ||
|
||
return false | ||
}) | ||
|
||
return genState | ||
} |
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
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,28 @@ | ||
package types | ||
|
||
import sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
// GenesisState is the struct representation of the export genesis | ||
type GenesisState struct { | ||
Codes []Code `json:"codes"` | ||
Contracts []Contract `json:"contracts"` | ||
} | ||
|
||
// Code struct encompasses CodeInfo and CodeBytes | ||
type Code struct { | ||
CodeInfo CodeInfo `json:"code_info"` | ||
CodesBytes []byte `json:"code_bytes"` | ||
} | ||
|
||
// Contract struct encompasses ContractAddress, ContractInfo, and ContractState | ||
type Contract struct { | ||
ContractAddress sdk.AccAddress `json:"contract_address"` | ||
ContractInfo ContractInfo `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 { | ||
return nil | ||
} |
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