From f6cea66e2ea9bfd8fe34b08fb4343626f1ec8a34 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 16 Feb 2018 20:58:51 -0500 Subject: [PATCH] test and fix InitChain --- baseapp/baseapp.go | 13 ++++++++++--- baseapp/baseapp_test.go | 39 ++++++++++++++++++++++++++++++++++++--- store/iavlstore.go | 1 - 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 793727a1bd..1dc63c15ea 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -186,21 +186,28 @@ func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOp } // Implements ABCI +// InitChain runs the initialization logic directly on the CommitMultiStore and commits it. func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) { if app.initChainer == nil { // TODO: should we have some default handling of validators? return } - // get the store and make a context for the initialization - store := app.cms.CacheMultiStore() - ctx := sdk.NewContext(store, abci.Header{}, false, nil) + // make a context for the initialization. + // NOTE: we're writing to the cms directly, without a CacheWrap + ctx := sdk.NewContext(app.cms, abci.Header{}, false, nil) err := app.initChainer(ctx, req) if err != nil { // TODO: something better https://github.com/cosmos/cosmos-sdk/issues/468 cmn.Exit(fmt.Sprintf("error initializing application genesis state: %v", err)) } + + // XXX this commits everything and bumps the version. + // With this, block 1 executes against state with version 1, but results in state with version 2. + // Is that what we want ? + app.cms.Commit() + return } diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 2c1cbd8bdc..fb136904cc 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -29,7 +29,9 @@ func newBaseApp(name string) *BaseApp { } func TestMountStores(t *testing.T) { - app := newBaseApp(t.Name()) + name := t.Name() + app := newBaseApp(name) + assert.Equal(t, name, app.Name()) // make some cap keys capKey1 := sdk.NewKVStoreKey("key1") @@ -51,8 +53,39 @@ func TestLoadVersion(t *testing.T) { // TODO } -func TestInitStater(t *testing.T) { - // TODO +func TestInitChainer(t *testing.T) { + app := newBaseApp(t.Name()) + + // make a cap key and mount the store + capKey := sdk.NewKVStoreKey("main") + app.MountStoresIAVL(capKey) + err := app.LoadLatestVersion(capKey) // needed to make stores non-nil + assert.Nil(t, err) + + key, value := []byte("hello"), []byte("goodbye") + + // initChainer sets a value in the store + var initChainer sdk.InitChainer = func(ctx sdk.Context, req abci.RequestInitChain) sdk.Error { + store := ctx.KVStore(capKey) + store.Set(key, value) + return nil + } + + query := abci.RequestQuery{ + Path: "/main/key", + Data: key, + } + + // initChainer is nil - nothing happens + app.InitChain(abci.RequestInitChain{}) + res := app.Query(query) + assert.Equal(t, 0, len(res.Value)) + + // set initChainer and try again - should see the value + app.SetInitChainer(initChainer) + app.InitChain(abci.RequestInitChain{}) + res = app.Query(query) + assert.Equal(t, value, res.Value) } //---------------------- diff --git a/store/iavlstore.go b/store/iavlstore.go index 52deef367a..e63585a16b 100644 --- a/store/iavlstore.go +++ b/store/iavlstore.go @@ -19,7 +19,6 @@ const ( func LoadIAVLStore(db dbm.DB, id CommitID) (CommitStore, error) { tree := iavl.NewVersionedTree(db, defaultIAVLCacheSize) - fmt.Println("LoadIAVLStore Version ", id.Version) err := tree.LoadVersion(id.Version) if err != nil { return nil, err