Skip to content

Commit

Permalink
Merge pull request #198 from CosmWasm/better_genesis_import
Browse files Browse the repository at this point in the history
Better genesis import
  • Loading branch information
alpe authored Jul 20, 2020
2 parents 65e4e3a + 882d817 commit e59b38a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 26 deletions.
17 changes: 7 additions & 10 deletions x/wasm/internal/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,20 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {

genState.Params = keeper.GetParams(ctx)

maxCodeID := keeper.GetNextCodeID(ctx)
for i := uint64(1); i < maxCodeID; i++ {
if !keeper.containsCodeInfo(ctx, i) {
continue
}
bytecode, err := keeper.GetByteCode(ctx, i)
keeper.IterateCodeInfos(ctx, func(codeID uint64, info types.CodeInfo) bool {
bytecode, err := keeper.GetByteCode(ctx, codeID)
if err != nil {
panic(err)
}
genState.Codes = append(genState.Codes, types.Code{
CodeID: i,
CodeInfo: *keeper.GetCodeInfo(ctx, i),
CodeID: codeID,
CodeInfo: info,
CodesBytes: bytecode,
})
}
return false
})

keeper.ListContractInfo(ctx, func(addr sdk.AccAddress, contract types.ContractInfo) bool {
keeper.IterateContractInfo(ctx, func(addr sdk.AccAddress, contract types.ContractInfo) bool {
contractStateIterator := keeper.GetContractState(ctx, addr)
var state []types.Model
for ; contractStateIterator.Valid(); contractStateIterator.Next() {
Expand Down
58 changes: 46 additions & 12 deletions x/wasm/internal/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestGenesisExportImport(t *testing.T) {
require.NoError(t, err)

// reset contract history in source DB for comparision with dest DB
srcKeeper.ListContractInfo(srcCtx, func(address sdk.AccAddress, info wasmTypes.ContractInfo) bool {
srcKeeper.IterateContractInfo(srcCtx, func(address sdk.AccAddress, info wasmTypes.ContractInfo) bool {
info.ResetFromGenesis(srcCtx)
srcKeeper.setContractInfo(srcCtx, address, &info)
return false
Expand All @@ -91,7 +91,7 @@ func TestGenesisExportImport(t *testing.T) {
dstIT := dstCtx.KVStore(dstStoreKeys[j]).Iterator(nil, nil)

for i := 0; srcIT.Valid(); i++ {
require.True(t, dstIT.Valid(), "destination DB has less elements than source. Missing: %q", srcIT.Key())
require.True(t, dstIT.Valid(), "[%s] destination DB has less elements than source. Missing: %s", srcStoreKeys[j].Name(), srcIT.Key())
require.Equal(t, srcIT.Key(), dstIT.Key(), i)
require.Equal(t, srcIT.Value(), dstIT.Value(), "[%s] element (%d): %s", srcStoreKeys[j].Name(), i, srcIT.Key())
srcIT.Next()
Expand Down Expand Up @@ -438,24 +438,58 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) {
keeper, ctx, _, dstCleanup := setupKeeper(t)
defer dstCleanup()

ctx = ctx.WithBlockHeight(0).WithGasMeter(sdk.NewInfiniteGasMeter())
var importState wasmTypes.GenesisState
err = json.Unmarshal([]byte(fmt.Sprintf(genesis, base64.StdEncoding.EncodeToString(wasmCode))), &importState)
require.NoError(t, err)
require.NoError(t, importState.ValidateBasic())

// when
InitGenesis(ctx, keeper, importState)

contractAddr, err := sdk.AccAddressFromBech32("cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5")
// verify wasm code
gotWasmCode, err := keeper.GetByteCode(ctx, 1)
require.NoError(t, err)
contractInfo := keeper.GetContractInfo(ctx, contractAddr)
require.NotNil(t, contractInfo)
require.Len(t, contractInfo.ContractCodeHistory, 1)
exp := []types.ContractCodeHistoryEntry{{
Operation: types.GenesisContractCodeHistoryType,
CodeID: 1,
Updated: types.NewAbsoluteTxPosition(ctx),
},
assert.Equal(t, wasmCode, gotWasmCode)

// verify code info
gotCodeInfo := keeper.GetCodeInfo(ctx, 1)
require.NotNil(t, gotCodeInfo)
codeCreatorAddr, _ := sdk.AccAddressFromBech32("cosmos1qtu5n0cnhfkjj6l2rq97hmky9fd89gwca9yarx")
codeHash := sha256.Sum256(wasmCode)
expCodeInfo := types.CodeInfo{
CodeHash: codeHash[:],
Creator: codeCreatorAddr,
Source: "https://example.com",
Builder: "foo/bar:tag",
InstantiateConfig: wasmTypes.AccessConfig{
Type: types.OnlyAddress,
Address: codeCreatorAddr,
},
}
assert.Equal(t, expCodeInfo, *gotCodeInfo)

// verify contract
contractAddr, _ := sdk.AccAddressFromBech32("cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5")
gotContractInfo := keeper.GetContractInfo(ctx, contractAddr)
require.NotNil(t, gotContractInfo)
contractCreatorAddr, _ := sdk.AccAddressFromBech32("cosmos13x849jzd03vne42ynpj25hn8npjecxqrjghd8x")
adminAddr, _ := sdk.AccAddressFromBech32("cosmos1h5t8zxmjr30e9dqghtlpl40f2zz5cgey6esxtn")

expContractInfo := types.ContractInfo{
CodeID: 1,
Creator: contractCreatorAddr,
Admin: adminAddr,
Label: "ȀĴnZV芢毤",
Created: &types.AbsoluteTxPosition{BlockHeight: 0, TxIndex: 0},
ContractCodeHistory: []types.ContractCodeHistoryEntry{{
Operation: types.GenesisContractCodeHistoryType,
CodeID: 1,
Updated: types.NewAbsoluteTxPosition(ctx),
},
},
}
assert.Equal(t, exp, contractInfo.ContractCodeHistory)
assert.Equal(t, expContractInfo, *gotContractInfo)
}

func setupKeeper(t *testing.T) (Keeper, sdk.Context, []sdk.StoreKey, func()) {
Expand Down
15 changes: 14 additions & 1 deletion x/wasm/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func (k Keeper) setContractInfo(ctx sdk.Context, contractAddress sdk.AccAddress,
store.Set(types.GetContractAddressKey(contractAddress), k.cdc.MustMarshalBinaryBare(contract))
}

func (k Keeper) ListContractInfo(ctx sdk.Context, cb func(sdk.AccAddress, types.ContractInfo) bool) {
func (k Keeper) IterateContractInfo(ctx sdk.Context, cb func(sdk.AccAddress, types.ContractInfo) bool) {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.ContractKeyPrefix)
iter := prefixStore.Iterator(nil, nil)
for ; iter.Valid(); iter.Next() {
Expand Down Expand Up @@ -509,6 +509,19 @@ func (k Keeper) containsCodeInfo(ctx sdk.Context, codeID uint64) bool {
return store.Has(types.GetCodeKey(codeID))
}

func (k Keeper) IterateCodeInfos(ctx sdk.Context, cb func(uint64, types.CodeInfo) bool) {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.CodeKeyPrefix)
iter := prefixStore.Iterator(nil, nil)
for ; iter.Valid(); iter.Next() {
var c types.CodeInfo
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &c)
// cb returns true to stop early
if cb(binary.BigEndian.Uint64(iter.Key()), c) {
return
}
}
}

func (k Keeper) GetByteCode(ctx sdk.Context, codeID uint64) ([]byte, error) {
store := ctx.KVStore(k.storeKey)
var codeInfo types.CodeInfo
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/internal/keeper/proposal_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestInstantiateProposal(t *testing.T) {
assert.Equal(t, "testing", cInfo.Label)
expHistory := []types.ContractCodeHistoryEntry{{
Operation: types.InitContractCodeHistoryType,
CodeID: src.Code,
CodeID: src.CodeID,
Updated: types.NewAbsoluteTxPosition(ctx),
Msg: src.InitMsg,
}}
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestMigrateProposal(t *testing.T) {
Updated: types.NewAbsoluteTxPosition(ctx),
}, {
Operation: types.MigrateContractCodeHistoryType,
CodeID: src.Code,
CodeID: src.CodeID,
Updated: types.NewAbsoluteTxPosition(ctx),
Msg: src.MigrateMsg,
}}
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func queryContractListByCode(ctx sdk.Context, codeIDstr string, req abci.Request
}

var contracts []ContractInfoWithAddress
keeper.ListContractInfo(ctx, func(addr sdk.AccAddress, info types.ContractInfo) bool {
keeper.IterateContractInfo(ctx, func(addr sdk.AccAddress, info types.ContractInfo) bool {
if info.CodeID == codeID {
// and add the address
infoWithAddress := ContractInfoWithAddress{
Expand Down

0 comments on commit e59b38a

Please sign in to comment.