Skip to content

Commit

Permalink
Merge pull request #1630 from CosmWasm/1629_genesis
Browse files Browse the repository at this point in the history
Fix genesis import with predictable addresses
  • Loading branch information
alpe authored Sep 18, 2023
2 parents 7dba5c7 + 7dc2d0c commit c12e85e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 6 additions & 5 deletions x/wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState) ([]ab
}
}

var maxContractID int
for i, contract := range data.Contracts {
contractAddr, err := sdk.AccAddressFromBech32(contract.ContractAddress)
if err != nil {
Expand All @@ -51,7 +50,6 @@ func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState) ([]ab
if err != nil {
return nil, errorsmod.Wrapf(err, "contract number %d", i)
}
maxContractID = i + 1 // not ideal but max(contractID) is not persisted otherwise
}

for i, seq := range data.Sequences {
Expand All @@ -66,9 +64,12 @@ func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState) ([]ab
if seqVal <= maxCodeID {
return nil, errorsmod.Wrapf(types.ErrInvalid, "seq %s with value: %d must be greater than: %d ", string(types.KeySequenceCodeID), seqVal, maxCodeID)
}
seqVal = keeper.PeekAutoIncrementID(ctx, types.KeySequenceInstanceID)
if seqVal <= uint64(maxContractID) {
return nil, errorsmod.Wrapf(types.ErrInvalid, "seq %s with value: %d must be greater than: %d ", string(types.KeySequenceInstanceID), seqVal, maxContractID)
// ensure next classic address is unused so that we know the sequence is good
rCtx, _ := ctx.CacheContext()
seqVal = keeper.PeekAutoIncrementID(rCtx, types.KeySequenceInstanceID)
addr := keeper.ClassicAddressGenerator()(rCtx, seqVal, nil)
if keeper.HasContractInfo(ctx, addr) {
return nil, errorsmod.Wrapf(types.ErrInvalid, "value: %d for seq %s was used already", seqVal, string(types.KeySequenceInstanceID))
}
return nil, nil
}
Expand Down
16 changes: 15 additions & 1 deletion x/wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ func TestGenesisExportImport(t *testing.T) {
}
}

func TestGenesisExportImportWithPredictableAddress(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
k := keepers.WasmKeeper
eCtx, _ := ctx.CacheContext()
codeID := StoreReflectContract(t, eCtx, keepers).CodeID
creator := RandomAccountAddress(t)
_, _, err := keepers.ContractKeeper.Instantiate2(eCtx, codeID, creator, nil, []byte("{}"), "testing", nil, []byte("my_salt"), false)
require.NoError(t, err)
genesisState := ExportGenesis(eCtx, k)
// when imported
_, err = InitGenesis(ctx, k, *genesisState)
require.NoError(t, err)
}

func TestGenesisInit(t *testing.T) {
wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)
Expand Down Expand Up @@ -438,7 +452,7 @@ func TestGenesisInit(t *testing.T) {
Params: types.DefaultParams(),
},
},
"prevent contract id seq init value == count contracts": {
"prevent contract id seq init value not high enough": {
src: types.GenesisState{
Codes: []types.Code{{
CodeID: firstCodeID,
Expand Down

0 comments on commit c12e85e

Please sign in to comment.