-
Notifications
You must be signed in to change notification settings - Fork 31
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 #142 from CosmWasm/genesis_io_tests
Add genesis ex-/import for `lastContractId` sequence
- Loading branch information
Showing
6 changed files
with
161 additions
and
0 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 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,92 @@ | ||
package keeper | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/CosmWasm/wasmd/x/wasm/internal/types" | ||
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/internal/types" | ||
"github.com/cosmos/cosmos-sdk/store" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
"github.com/cosmos/cosmos-sdk/x/staking" | ||
fuzz "github.com/google/gofuzz" | ||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
func TestGenesisExportImport(t *testing.T) { | ||
srcKeeper, srcCtx, srcCleanup := setupKeeper(t) | ||
defer srcCleanup() | ||
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 < 25; i++ { | ||
var ( | ||
codeInfo types.CodeInfo | ||
contract types.ContractInfo | ||
stateModels []types.Model | ||
) | ||
f.Fuzz(&codeInfo) | ||
f.Fuzz(&contract) | ||
f.Fuzz(&stateModels) | ||
|
||
codeID, err := srcKeeper.Create(srcCtx, codeInfo.Creator, wasmCode, codeInfo.Source, codeInfo.Builder) | ||
require.NoError(t, err) | ||
contract.CodeID = codeID | ||
contractAddr := srcKeeper.generateContractAddress(srcCtx, codeID) | ||
srcKeeper.setContractInfo(srcCtx, contractAddr, &contract) | ||
srcKeeper.setContractState(srcCtx, contractAddr, stateModels) | ||
} | ||
|
||
// export | ||
genesisState := ExportGenesis(srcCtx, srcKeeper) | ||
|
||
// re-import | ||
dstKeeper, dstCtx, dstCleanup := setupKeeper(t) | ||
defer dstCleanup() | ||
InitGenesis(dstCtx, dstKeeper, genesisState) | ||
|
||
// compare whole DB | ||
srcIT := srcCtx.KVStore(srcKeeper.storeKey).Iterator(nil, nil) | ||
dstIT := dstCtx.KVStore(dstKeeper.storeKey).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.Equal(t, srcIT.Key(), dstIT.Key(), i) | ||
require.Equal(t, srcIT.Value(), dstIT.Value(), "element (%d): %s", i, srcIT.Key()) | ||
srcIT.Next() | ||
dstIT.Next() | ||
} | ||
require.False(t, dstIT.Valid()) | ||
} | ||
|
||
func setupKeeper(t *testing.T) (Keeper, sdk.Context, func()) { | ||
tempDir, err := ioutil.TempDir("", "wasm") | ||
require.NoError(t, err) | ||
cleanup := func() { os.RemoveAll(tempDir) } | ||
//t.Cleanup(cleanup) todo: add with Go 1.14 | ||
|
||
keyContract := sdk.NewKVStoreKey(wasmTypes.StoreKey) | ||
db := dbm.NewMemDB() | ||
ms := store.NewCommitMultiStore(db) | ||
ms.MountStoreWithDB(keyContract, sdk.StoreTypeIAVL, db) | ||
require.NoError(t, ms.LoadLatestVersion()) | ||
|
||
ctx := sdk.NewContext(ms, abci.Header{ | ||
Height: 1234567, | ||
Time: time.Date(2020, time.April, 22, 12, 0, 0, 0, time.UTC), | ||
}, false, log.NewNopLogger()) | ||
|
||
cdc := MakeTestCodec() | ||
wasmConfig := wasmTypes.DefaultWasmConfig() | ||
|
||
srcKeeper := NewKeeper(cdc, keyContract, auth.AccountKeeper{}, nil, staking.Keeper{}, nil, tempDir, wasmConfig, "", nil, nil) | ||
return srcKeeper, ctx, cleanup | ||
} |
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,34 @@ | ||
package keeper | ||
|
||
import ( | ||
"github.com/CosmWasm/wasmd/x/wasm/internal/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
fuzz "github.com/google/gofuzz" | ||
tmBytes "github.com/tendermint/tendermint/libs/bytes" | ||
) | ||
|
||
func FuzzAddr(m *sdk.AccAddress, c fuzz.Continue) { | ||
*m = make([]byte, 20) | ||
c.Read(*m) | ||
} | ||
func FuzzAbsoluteTxPosition(m *types.AbsoluteTxPosition, c fuzz.Continue) { | ||
m.BlockHeight = int64(c.RandUint64()) // can't be negative | ||
m.TxIndex = c.RandUint64() | ||
} | ||
|
||
func FuzzContractInfo(m *types.ContractInfo, c fuzz.Continue) { | ||
const maxSize = 1024 | ||
m.CodeID = c.RandUint64() | ||
FuzzAddr(&m.Creator, c) | ||
FuzzAddr(&m.Admin, c) | ||
m.Label = c.RandString() | ||
m.InitMsg = make([]byte, c.RandUint64()%maxSize) | ||
c.Read(m.InitMsg) | ||
c.Fuzz(&m.Created) | ||
c.Fuzz(&m.LastUpdated) | ||
m.PreviousCodeID = c.RandUint64() | ||
} | ||
func FuzzStateModel(m *types.Model, c fuzz.Continue) { | ||
m.Key = tmBytes.HexBytes(c.RandString()) | ||
c.Fuzz(&m.Value) | ||
} |
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