test and fix InitChain

This commit is contained in:
Ethan Buchman 2018-02-16 20:58:51 -05:00
parent c7df77ce3c
commit f6cea66e2e
3 changed files with 46 additions and 7 deletions

View File

@ -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
}

View File

@ -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)
}
//----------------------

View File

@ -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