Skip to content

Commit

Permalink
export, validate, import work locally
Browse files Browse the repository at this point in the history
  • Loading branch information
jewei1997 committed Jul 16, 2024
1 parent a221177 commit b08a328
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 17 additions & 10 deletions x/wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState, staki
}
}
}
fmt.Println("Got maxCodeID = ", maxCodeID)

var maxContractID int
for i, contract := range data.Contracts {
Expand All @@ -49,16 +50,21 @@ func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState, staki
}
maxContractID = i + 1 // not ideal but max(contractID) is not persisted otherwise
}
fmt.Println("maxContractID = ", maxContractID)

fmt.Println("Iterating over data.Sequences = ", data.Sequences)
for i, seq := range data.Sequences {
fmt.Println("i = ", i, ", seq = ", seq)
err := keeper.importAutoIncrementID(ctx, seq.IDKey, seq.Value)
if err != nil {
return nil, sdkerrors.Wrapf(err, "sequence number %d", i)
}
}

// sanity check seq values
fmt.Println("types.KeyLastCodeID = ", string(types.KeyLastCodeID))
seqVal := keeper.PeekAutoIncrementID(ctx, types.KeyLastCodeID)
fmt.Println("seqVal = ", seqVal)
if seqVal <= maxCodeID {
return nil, sdkerrors.Wrapf(types.ErrInvalid, "seq %s with value: %d must be greater than: %d ", string(types.KeyLastCodeID), seqVal, maxCodeID)
}
Expand Down Expand Up @@ -138,6 +144,17 @@ func ExportGenesisStream(ctx sdk.Context, keeper *Keeper) <-chan *types.GenesisS
genState.Params = keeper.GetParams(ctx)
ch <- &genState

// Needs to be first because there are invariant checks when importing that need sequences info
for _, k := range [][]byte{types.KeyLastCodeID, types.KeyLastInstanceID} {
var genState types.GenesisState
genState.Params = keeper.GetParams(ctx)
genState.Sequences = append(genState.Sequences, types.Sequence{
IDKey: k,
Value: keeper.PeekAutoIncrementID(ctx, k),
})
ch <- &genState
}

keeper.IterateCodeInfos(ctx, func(codeID uint64, info types.CodeInfo) bool {
var genState types.GenesisState
genState.Params = keeper.GetParams(ctx)
Expand Down Expand Up @@ -188,16 +205,6 @@ func ExportGenesisStream(ctx sdk.Context, keeper *Keeper) <-chan *types.GenesisS
})
fmt.Println("Done with IterateContractInfo")

for _, k := range [][]byte{types.KeyLastCodeID, types.KeyLastInstanceID} {
var genState types.GenesisState
genState.Params = keeper.GetParams(ctx)
genState.Sequences = append(genState.Sequences, types.Sequence{
IDKey: k,
Value: keeper.PeekAutoIncrementID(ctx, k),
})
ch <- &genState
}

close(ch)
}()

Check notice

Code scanning / CodeQL

Spawning a Go routine Note

Spawning a Go routine may be a possible source of non-determinism
return ch
Expand Down
2 changes: 2 additions & 0 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (k Keeper) GetParams(ctx sdk.Context) types.Params {
}

func (k Keeper) SetParams(ctx sdk.Context, ps types.Params) {
fmt.Println("In x/wasm, keeper: trying to set params = ", ps)
k.paramSpace.SetParamSet(ctx, &ps)
}

Expand Down Expand Up @@ -1000,6 +1001,7 @@ func (k Keeper) PeekAutoIncrementID(ctx sdk.Context, lastIDKey []byte) uint64 {

func (k Keeper) importAutoIncrementID(ctx sdk.Context, lastIDKey []byte, val uint64) error {
store := ctx.KVStore(k.storeKey)
fmt.Println("lastIDKey = ", string(lastIDKey))
if store.Has(lastIDKey) {
return sdkerrors.Wrapf(types.ErrDuplicate, "autoincrement id: %s", string(lastIDKey))
}
Expand Down

0 comments on commit b08a328

Please sign in to comment.