Add snapshotter integration tests and minor cleanup
This commit is contained in:
@@ -5,21 +5,14 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
dbm "github.com/tendermint/tm-db"
|
||||
|
||||
"github.com/CosmWasm/wasmd/x/wasm"
|
||||
"github.com/cosmos/cosmos-sdk/snapshots"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
@@ -30,8 +23,18 @@ import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/errors"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
dbm "github.com/tendermint/tm-db"
|
||||
|
||||
"github.com/CosmWasm/wasmd/x/wasm"
|
||||
)
|
||||
|
||||
// DefaultConsensusParams defines the default Tendermint consensus params used in
|
||||
@@ -53,44 +56,28 @@ var DefaultConsensusParams = &abci.ConsensusParams{
|
||||
},
|
||||
}
|
||||
|
||||
func setup(withGenesis bool, invCheckPeriod uint, opts ...wasm.Option) (*WasmApp, GenesisState) {
|
||||
func setup(t testing.TB, withGenesis bool, invCheckPeriod uint, opts ...wasm.Option) (*WasmApp, GenesisState) {
|
||||
nodeHome := t.TempDir()
|
||||
snapshotDir := filepath.Join(nodeHome, "data", "snapshots")
|
||||
snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDir)
|
||||
require.NoError(t, err)
|
||||
snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir)
|
||||
require.NoError(t, err)
|
||||
baseAppOpts := []func(*baseapp.BaseApp){baseapp.SetSnapshotStore(snapshotStore), baseapp.SetSnapshotKeepRecent(2)}
|
||||
db := dbm.NewMemDB()
|
||||
app := NewWasmApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, invCheckPeriod, MakeEncodingConfig(), wasm.EnableAllProposals, EmptyBaseAppOptions{}, opts)
|
||||
app := NewWasmApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, nodeHome, invCheckPeriod, MakeEncodingConfig(), wasm.EnableAllProposals, EmptyBaseAppOptions{}, opts, baseAppOpts...)
|
||||
if withGenesis {
|
||||
return app, NewDefaultGenesisState()
|
||||
}
|
||||
return app, GenesisState{}
|
||||
}
|
||||
|
||||
// Setup initializes a new WasmApp. A Nop logger is set in WasmApp.
|
||||
func Setup(isCheckTx bool) *WasmApp {
|
||||
app, genesisState := setup(!isCheckTx, 5)
|
||||
if !isCheckTx {
|
||||
// init chain must be called to stop deliverState from being nil
|
||||
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Initialize the chain
|
||||
app.InitChain(
|
||||
abci.RequestInitChain{
|
||||
Validators: []abci.ValidatorUpdate{},
|
||||
ConsensusParams: DefaultConsensusParams,
|
||||
AppStateBytes: stateBytes,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
// SetupWithGenesisValSet initializes a new WasmApp with a validator set and genesis accounts
|
||||
// that also act as delegators. For simplicity, each validator is bonded with a delegation
|
||||
// of one consensus engine unit (10^6) in the default token of the WasmApp from first genesis
|
||||
// account. A Nop logger is set in WasmApp.
|
||||
func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, opts []wasm.Option, balances ...banktypes.Balance) *WasmApp {
|
||||
app, genesisState := setup(true, 5, opts...)
|
||||
app, genesisState := setup(t, true, 5, opts...)
|
||||
// set genesis accounts
|
||||
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
|
||||
genesisState[authtypes.ModuleName] = app.appCodec.MustMarshalJSON(authGenesis)
|
||||
@@ -167,37 +154,9 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
|
||||
return app
|
||||
}
|
||||
|
||||
// SetupWithGenesisAccounts initializes a new WasmApp with the provided genesis
|
||||
// accounts and possible balances.
|
||||
func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *WasmApp {
|
||||
app, genesisState := setup(true, 0)
|
||||
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
|
||||
genesisState[authtypes.ModuleName] = app.appCodec.MustMarshalJSON(authGenesis)
|
||||
|
||||
totalSupply := sdk.NewCoins()
|
||||
for _, b := range balances {
|
||||
totalSupply = totalSupply.Add(b.Coins...)
|
||||
}
|
||||
|
||||
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{})
|
||||
genesisState[banktypes.ModuleName] = app.appCodec.MustMarshalJSON(bankGenesis)
|
||||
|
||||
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
app.InitChain(
|
||||
abci.RequestInitChain{
|
||||
Validators: []abci.ValidatorUpdate{},
|
||||
ConsensusParams: DefaultConsensusParams,
|
||||
AppStateBytes: stateBytes,
|
||||
},
|
||||
)
|
||||
|
||||
app.Commit()
|
||||
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1}})
|
||||
|
||||
// SetupWithEmptyStore setup a wasmd app instance with empty DB
|
||||
func SetupWithEmptyStore(t testing.TB) *WasmApp {
|
||||
app, _ := setup(t, false, 0)
|
||||
return app
|
||||
}
|
||||
|
||||
|
||||
@@ -15,41 +15,9 @@ import (
|
||||
"github.com/CosmWasm/wasmd/x/wasm/types"
|
||||
)
|
||||
|
||||
/*
|
||||
API to implement:
|
||||
var _ snapshot.ExtensionSnapshotter = &WasmSnapshotter{}
|
||||
|
||||
// Snapshotter is something that can create and restore snapshots, consisting of streamed binary
|
||||
// chunks - all of which must be read from the channel and closed. If an unsupported format is
|
||||
// given, it must return ErrUnknownFormat (possibly wrapped with fmt.Errorf).
|
||||
type Snapshotter interface {
|
||||
// Snapshot writes snapshot items into the protobuf writer.
|
||||
Snapshot(height uint64, protoWriter protoio.Writer) error
|
||||
|
||||
// Restore restores a state snapshot from the protobuf items read from the reader.
|
||||
// If the ready channel is non-nil, it returns a ready signal (by being closed) once the
|
||||
// restorer is ready to accept chunks.
|
||||
Restore(height uint64, format uint32, protoReader protoio.Reader) (SnapshotItem, error)
|
||||
}
|
||||
|
||||
// ExtensionSnapshotter is an extension Snapshotter that is appended to the snapshot stream.
|
||||
// ExtensionSnapshotter has an unique name and manages it's own internal formats.
|
||||
type ExtensionSnapshotter interface {
|
||||
Snapshotter
|
||||
|
||||
// SnapshotName returns the name of snapshotter, it should be unique in the manager.
|
||||
SnapshotName() string
|
||||
|
||||
// SnapshotFormat returns the default format the extension snapshotter use to encode the
|
||||
// payloads when taking a snapshot.
|
||||
// It's defined within the extension, different from the global format for the whole state-sync snapshot.
|
||||
SnapshotFormat() uint32
|
||||
|
||||
// SupportedFormats returns a list of formats it can restore from.
|
||||
SupportedFormats() []uint32
|
||||
}
|
||||
*/
|
||||
|
||||
// Format 1 is just gzipped wasm byte code for each item payload. No protobuf envelope, no metadata.
|
||||
// SnapshotFormat format 1 is just gzipped wasm byte code for each item payload. No protobuf envelope, no metadata.
|
||||
const SnapshotFormat = 1
|
||||
|
||||
type WasmSnapshotter struct {
|
||||
@@ -78,15 +46,10 @@ func (ws *WasmSnapshotter) SupportedFormats() []uint32 {
|
||||
}
|
||||
|
||||
func (ws *WasmSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) error {
|
||||
// TODO: This seems more correct (historical info), but kills my tests
|
||||
// Since codeIDs and wasm are immutible, it is never wrong to return new wasm data than the
|
||||
// user requests
|
||||
// ------
|
||||
// cacheMS, err := ws.cms.CacheMultiStoreWithVersion(int64(height))
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
cacheMS := ws.cms.CacheMultiStore()
|
||||
cacheMS, err := ws.cms.CacheMultiStoreWithVersion(int64(height))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx := sdk.NewContext(cacheMS, tmproto.Header{}, false, log.NewNopLogger())
|
||||
seenBefore := make(map[string]bool)
|
||||
@@ -129,7 +92,7 @@ func (ws *WasmSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) e
|
||||
func (ws *WasmSnapshotter) Restore(
|
||||
height uint64, format uint32, protoReader protoio.Reader,
|
||||
) (snapshot.SnapshotItem, error) {
|
||||
if format == 1 {
|
||||
if format == SnapshotFormat {
|
||||
return ws.processAllItems(height, protoReader, restoreV1, finalizeV1)
|
||||
}
|
||||
return snapshot.SnapshotItem{}, snapshot.ErrUnknownFormat
|
||||
|
||||
127
x/wasm/keeper/snapshotter_integration_test.go
Normal file
127
x/wasm/keeper/snapshotter_integration_test.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/CosmWasm/wasmd/app"
|
||||
"github.com/CosmWasm/wasmd/x/wasm/keeper"
|
||||
)
|
||||
|
||||
func TestSnapshotter(t *testing.T) {
|
||||
specs := map[string]struct {
|
||||
wasmFiles []string
|
||||
expCodeInfos int
|
||||
}{
|
||||
"single contract": {
|
||||
wasmFiles: []string{"./testdata/reflect.wasm"},
|
||||
expCodeInfos: 1,
|
||||
},
|
||||
"multiple contract": {
|
||||
wasmFiles: []string{"./testdata/reflect.wasm", "./testdata/burner.wasm", "./testdata/reflect.wasm"},
|
||||
expCodeInfos: 3,
|
||||
},
|
||||
"duplicate contracts": {
|
||||
wasmFiles: []string{"./testdata/reflect.wasm", "./testdata/reflect.wasm"},
|
||||
expCodeInfos: 2,
|
||||
},
|
||||
}
|
||||
for name, spec := range specs {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
// setup source app
|
||||
srcWasmApp, genesisAddr := newWasmExampleApp(t)
|
||||
|
||||
// store wasm codes on chain
|
||||
ctx := srcWasmApp.NewUncachedContext(false, tmproto.Header{
|
||||
ChainID: "foo",
|
||||
Height: srcWasmApp.LastBlockHeight() + 1,
|
||||
Time: time.Now(),
|
||||
})
|
||||
wasmKeeper := app.NewTestSupport(t, srcWasmApp).WasmKeeper()
|
||||
contractKeeper := keeper.NewDefaultPermissionKeeper(&wasmKeeper)
|
||||
|
||||
srcCodeIDToChecksum := make(map[uint64][]byte, len(spec.wasmFiles))
|
||||
for i, v := range spec.wasmFiles {
|
||||
wasmCode, err := ioutil.ReadFile(v)
|
||||
require.NoError(t, err)
|
||||
codeID, err := contractKeeper.Create(ctx, genesisAddr, wasmCode, nil)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, uint64(i+1), codeID)
|
||||
hash := sha256.Sum256(wasmCode)
|
||||
srcCodeIDToChecksum[codeID] = hash[:]
|
||||
}
|
||||
// create snapshot
|
||||
srcWasmApp.Commit()
|
||||
snapshotHeight := uint64(srcWasmApp.LastBlockHeight())
|
||||
snapshot, err := srcWasmApp.SnapshotManager().Create(snapshotHeight)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, snapshot)
|
||||
|
||||
// when snapshot imported into dest app instance
|
||||
destWasmApp := app.SetupWithEmptyStore(t)
|
||||
require.NoError(t, destWasmApp.SnapshotManager().Restore(*snapshot))
|
||||
for i := uint32(0); i < snapshot.Chunks; i++ {
|
||||
chunkBz, err := srcWasmApp.SnapshotManager().LoadChunk(snapshot.Height, snapshot.Format, i)
|
||||
require.NoError(t, err)
|
||||
end, err := destWasmApp.SnapshotManager().RestoreChunk(chunkBz)
|
||||
require.NoError(t, err)
|
||||
if end {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// then wasm contracts are imported
|
||||
wasmKeeper = app.NewTestSupport(t, destWasmApp).WasmKeeper()
|
||||
ctx = destWasmApp.NewUncachedContext(false, tmproto.Header{
|
||||
ChainID: "foo",
|
||||
Height: destWasmApp.LastBlockHeight() + 1,
|
||||
Time: time.Now(),
|
||||
})
|
||||
for codeID, checksum := range srcCodeIDToChecksum {
|
||||
bz, err := wasmKeeper.GetByteCode(ctx, codeID)
|
||||
require.NoError(t, err)
|
||||
hash := sha256.Sum256(bz)
|
||||
assert.Equal(t, checksum, hash[:])
|
||||
destCodeInfo := wasmKeeper.GetCodeInfo(ctx, codeID)
|
||||
require.NotNil(t, destCodeInfo)
|
||||
assert.Equal(t, checksum, destCodeInfo.CodeHash)
|
||||
}
|
||||
// and expected number of code infos stored
|
||||
assert.Equal(t, spec.expCodeInfos, len(srcCodeIDToChecksum))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newWasmExampleApp(t *testing.T) (*app.WasmApp, sdk.AccAddress) {
|
||||
var senderPrivKey = ed25519.GenPrivKey()
|
||||
pubKey, err := cryptocodec.ToTmPubKeyInterface(senderPrivKey.PubKey())
|
||||
require.NoError(t, err)
|
||||
|
||||
senderAddr := senderPrivKey.PubKey().Address().Bytes()
|
||||
acc := authtypes.NewBaseAccount(senderAddr, senderPrivKey.PubKey(), 0, 0)
|
||||
amount, ok := sdk.NewIntFromString("10000000000000000000")
|
||||
require.True(t, ok)
|
||||
|
||||
balance := banktypes.Balance{
|
||||
Address: acc.GetAddress().String(),
|
||||
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amount)),
|
||||
}
|
||||
validator := tmtypes.NewValidator(pubKey, 1)
|
||||
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})
|
||||
wasmApp := app.SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, nil, balance)
|
||||
|
||||
return wasmApp, senderAddr
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
protoio "github.com/gogo/protobuf/io"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSnapshoting(t *testing.T) {
|
||||
ctx, keepers := CreateTestInput(t, false, SupportedFeatures)
|
||||
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
|
||||
creator := keepers.Faucet.NewFundedAccount(ctx, deposit...)
|
||||
_, _, bob := keyPubAddr()
|
||||
_, _, fred := keyPubAddr()
|
||||
|
||||
// create a contact
|
||||
codeID, err := keepers.ContractKeeper.Create(ctx, creator, hackatomWasm, nil)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, uint64(1), codeID)
|
||||
|
||||
// instantiate it
|
||||
initMsg := HackatomExampleInitMsg{
|
||||
Verifier: fred,
|
||||
Beneficiary: bob,
|
||||
}
|
||||
initMsgBz, err := json.Marshal(initMsg)
|
||||
require.NoError(t, err)
|
||||
contractAddr, _, err := keepers.ContractKeeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, "demo contract 1", deposit)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr", contractAddr.String())
|
||||
|
||||
// // commit all data to a height (to be snapshotted)
|
||||
// ms := keepers.MultiStore
|
||||
// id := ms.LastCommitID()
|
||||
// fmt.Printf("%#v\n", id)
|
||||
// commitInfo := ms.Commit()
|
||||
// // for debugging
|
||||
// assert.Equal(t, 1, commitInfo)
|
||||
|
||||
// successfully query it
|
||||
queryBz := []byte(`{"verifier":{}}`)
|
||||
res, err := keepers.WasmKeeper.QuerySmart(ctx, contractAddr, queryBz)
|
||||
require.NoError(t, err)
|
||||
expected := fmt.Sprintf(`{"verifier":"%s"}`, fred.String())
|
||||
assert.JSONEq(t, string(res), expected)
|
||||
|
||||
// now, create a snapshoter
|
||||
extension := NewWasmSnapshotter(keepers.MultiStore, keepers.WasmKeeper)
|
||||
|
||||
// create reader to store data
|
||||
buf := bytes.Buffer{}
|
||||
// Note: we ignore height for now (TODO)
|
||||
err = extension.Snapshot(100, protoio.NewFullWriter(&buf))
|
||||
require.NoError(t, err)
|
||||
require.True(t, buf.Len() > 50000)
|
||||
|
||||
// let's try to restore this now
|
||||
_, newKeepers := CreateTestInput(t, false, SupportedFeatures)
|
||||
|
||||
recovery := NewWasmSnapshotter(newKeepers.MultiStore, newKeepers.WasmKeeper)
|
||||
_, err = recovery.Restore(100, 1, protoio.NewFullReader(&buf, buf.Len()))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// failed attempt to copy state
|
||||
// // now, we make a new app with a copy of the "iavl" db, but no contracts
|
||||
// copyCtx, copyKeepers := createTestInput(t, false, SupportedFeatures, types.DefaultWasmConfig(), sharedDB)
|
||||
|
||||
// // contract exists
|
||||
// info := copyKeepers.WasmKeeper.GetContractInfo(ctx, contractAddr)
|
||||
// require.NotNil(t, info)
|
||||
// require.Equal(t, info.CodeID, codeID)
|
||||
|
||||
// // querying the existing contract errors, as there is no wasm file
|
||||
// res, err = copyKeepers.WasmKeeper.QuerySmart(copyCtx, contractAddr, queryBz)
|
||||
// require.Error(t, err)
|
||||
Reference in New Issue
Block a user