Skip to content

Commit

Permalink
Ex/Import missing LastInstanceID key
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jun 29, 2020
1 parent 077d99d commit f71a9bb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
11 changes: 11 additions & 0 deletions x/wasm/internal/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {
keeper.setContractState(ctx, contract.ContractAddress, contract.ContractState)
}

for _, seq := range data.Sequences {
keeper.setAutoIncrementID(ctx, seq.IDKey, seq.Value)
}
}

// ExportGenesis returns a GenesisState for a given context and keeper.
Expand Down Expand Up @@ -67,5 +70,13 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
return false
})

// types.KeyLastCodeID is updated via keeper create
for _, k := range [][]byte{types.KeyLastInstanceID} {
genState.Sequences = append(genState.Sequences, types.Sequence{
IDKey: k,
Value: keeper.peekAutoIncrementID(ctx, k),
})
}

return genState
}
5 changes: 3 additions & 2 deletions x/wasm/internal/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ func TestGenesisExportImport(t *testing.T) {
wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm")
require.NoError(t, err)

// store some test data
f := fuzz.New().Funcs(FuzzAddr, FuzzAbsoluteTxPosition, FuzzContractInfo, FuzzStateModel)
for i := 0; i < 20; i++ {
for i := 0; i < 25; i++ {
var (
codeInfo types.CodeInfo
contract types.ContractInfo
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestGenesisExportImport(t *testing.T) {
for i := 0; srcIT.Valid(); i++ {
require.True(t, dstIT.Valid(), "destination DB has less elements than source. Missing: %q", srcIT.Key())
require.Equal(t, srcIT.Key(), dstIT.Key(), i)
require.Equal(t, srcIT.Value(), dstIT.Value(), i)
require.Equal(t, srcIT.Value(), dstIT.Value(), "element (%d): %s", i, srcIT.Key())
srcIT.Next()
dstIT.Next()
}
Expand Down
17 changes: 17 additions & 0 deletions x/wasm/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,23 @@ func (k Keeper) autoIncrementID(ctx sdk.Context, lastIDKey []byte) uint64 {
return id
}

// peekAutoIncrementID reads the current value without incrementing it.
func (k Keeper) peekAutoIncrementID(ctx sdk.Context, lastIDKey []byte) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(lastIDKey)
id := uint64(1)
if bz != nil {
id = binary.BigEndian.Uint64(bz)
}
return id
}

func (k Keeper) setAutoIncrementID(ctx sdk.Context, lastIDKey []byte, val uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(val)
store.Set(lastIDKey, bz)
}

func addrFromUint64(id uint64) sdk.AccAddress {
addr := make([]byte, 20)
addr[0] = 'C'
Expand Down
6 changes: 6 additions & 0 deletions x/wasm/internal/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package types

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

type Sequence struct {
IDKey []byte `json:"id_key"`
Value uint64 `json:"value"`
}

// GenesisState is the struct representation of the export genesis
type GenesisState struct {
Codes []Code `json:"codes"`
Contracts []Contract `json:"contracts"`
Sequences []Sequence `json:"sequences"`
}

// Code struct encompasses CodeInfo and CodeBytes
Expand Down

0 comments on commit f71a9bb

Please sign in to comment.