From a4443ddb0c29227c619c03c660acc4b4bdf2ec38 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 23 Oct 2017 00:06:50 -0400 Subject: [PATCH] update persistent dummy for versioned iavl --- example/dummy/dummy_test.go | 12 +++++++++--- example/dummy/persistent_dummy.go | 28 +++++++++------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/example/dummy/dummy_test.go b/example/dummy/dummy_test.go index 41e033bc0..fbc095e6d 100644 --- a/example/dummy/dummy_test.go +++ b/example/dummy/dummy_test.go @@ -81,6 +81,8 @@ func TestPersistentDummyInfo(t *testing.T) { dummy := NewPersistentDummyApplication(dir) height := uint64(0) + dummy.InitChain(types.RequestInitChain{[]*types.Validator{randVal(0)}}) + resInfo := dummy.Info(types.RequestInfo{}) if resInfo.LastBlockHeight != height { t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight) @@ -103,6 +105,12 @@ func TestPersistentDummyInfo(t *testing.T) { } +func randVal(i int) *types.Validator { + pubkey := crypto.GenPrivKeyEd25519FromSecret([]byte(cmn.Fmt("test%d", i))).PubKey().Bytes() + power := cmn.RandInt() + return &types.Validator{pubkey, uint64(power)} +} + // add a validator, remove a validator, update a validator func TestValSetChanges(t *testing.T) { dir, err := ioutil.TempDir("/tmp", "abci-dummy-test") // TODO @@ -116,9 +124,7 @@ func TestValSetChanges(t *testing.T) { nInit := 5 vals := make([]*types.Validator, total) for i := 0; i < total; i++ { - pubkey := crypto.GenPrivKeyEd25519FromSecret([]byte(cmn.Fmt("test%d", i))).PubKey().Bytes() - power := cmn.RandInt() - vals[i] = &types.Validator{pubkey, uint64(power)} + vals[i] = randVal(i) } // iniitalize with the first nInit dummy.InitChain(types.RequestInitChain{vals[:nInit]}) diff --git a/example/dummy/persistent_dummy.go b/example/dummy/persistent_dummy.go index 20b68b9aa..de4aaa569 100644 --- a/example/dummy/persistent_dummy.go +++ b/example/dummy/persistent_dummy.go @@ -23,11 +23,6 @@ const ( type PersistentDummyApplication struct { app *DummyApplication - // latest received - // TODO: move to merkle tree? - blockHeader *types.Header - height uint64 - // validator set changes []*types.Validator @@ -61,7 +56,7 @@ func (app *PersistentDummyApplication) SetLogger(l log.Logger) { func (app *PersistentDummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) { resInfo = app.app.Info(req) - resInfo.LastBlockHeight = app.height + resInfo.LastBlockHeight = app.app.state.LatestVersion() resInfo.LastBlockAppHash = app.app.state.Hash() return resInfo } @@ -88,24 +83,21 @@ func (app *PersistentDummyApplication) CheckTx(tx []byte) types.Result { return app.app.CheckTx(tx) } +// Commit will panic if InitChain was not called func (app *PersistentDummyApplication) Commit() types.Result { - h := app.blockHeader.Height - // Save a new version + // Save a new version for next height + height := app.app.state.LatestVersion() + 1 var appHash []byte var err error - if app.app.state.Size() > 0 { - appHash, err = app.app.state.SaveVersion(h) - if err != nil { - // if this wasn't a dummy app, we'd do something smarter - panic(err) - } - app.logger.Info("Saved state", "root", appHash) + appHash, err = app.app.state.SaveVersion(height) + if err != nil { + // if this wasn't a dummy app, we'd do something smarter + panic(err) } - app.height = h - app.logger.Info("Commit block", "height", h, "root", appHash) + app.logger.Info("Commit block", "height", height, "root", appHash) return types.NewResultOK(appHash, "") } @@ -125,8 +117,6 @@ func (app *PersistentDummyApplication) InitChain(params types.RequestInitChain) // Track the block hash and header information func (app *PersistentDummyApplication) BeginBlock(params types.RequestBeginBlock) { - // update latest block info - app.blockHeader = params.Header // reset valset changes app.changes = make([]*types.Validator, 0)