|
|
@ -6,10 +6,8 @@ import ( |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
|
|
|
|
"github.com/pkg/errors" |
|
|
|
"github.com/tendermint/abci/types" |
|
|
|
wire "github.com/tendermint/go-wire" |
|
|
|
"github.com/tendermint/merkleeyes/iavl" |
|
|
|
"github.com/tendermint/iavl" |
|
|
|
cmn "github.com/tendermint/tmlibs/common" |
|
|
|
dbm "github.com/tendermint/tmlibs/db" |
|
|
|
"github.com/tendermint/tmlibs/log" |
|
|
@ -23,11 +21,6 @@ const ( |
|
|
|
|
|
|
|
type PersistentDummyApplication struct { |
|
|
|
app *DummyApplication |
|
|
|
db dbm.DB |
|
|
|
|
|
|
|
// latest received
|
|
|
|
// TODO: move to merkle tree?
|
|
|
|
blockHeader *types.Header |
|
|
|
|
|
|
|
// validator set
|
|
|
|
changes []*types.Validator |
|
|
@ -36,17 +29,17 @@ type PersistentDummyApplication struct { |
|
|
|
} |
|
|
|
|
|
|
|
func NewPersistentDummyApplication(dbDir string) *PersistentDummyApplication { |
|
|
|
db := dbm.NewDB("dummy", "leveldb", dbDir) |
|
|
|
lastBlock := LoadLastBlock(db) |
|
|
|
|
|
|
|
stateTree := iavl.NewIAVLTree(0, db) |
|
|
|
stateTree.Load(lastBlock.AppHash) |
|
|
|
name := "dummy" |
|
|
|
db, err := dbm.NewGoLevelDB(name, dbDir) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
|
|
|
|
// log.Notice("Loaded state", "block", lastBlock.Height, "root", stateTree.Hash())
|
|
|
|
stateTree := iavl.NewVersionedTree(500, db) |
|
|
|
stateTree.Load() |
|
|
|
|
|
|
|
return &PersistentDummyApplication{ |
|
|
|
app: &DummyApplication{state: stateTree}, |
|
|
|
db: db, |
|
|
|
logger: log.NewNopLogger(), |
|
|
|
} |
|
|
|
} |
|
|
@ -57,9 +50,8 @@ func (app *PersistentDummyApplication) SetLogger(l log.Logger) { |
|
|
|
|
|
|
|
func (app *PersistentDummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) { |
|
|
|
resInfo = app.app.Info(req) |
|
|
|
lastBlock := LoadLastBlock(app.db) |
|
|
|
resInfo.LastBlockHeight = lastBlock.Height |
|
|
|
resInfo.LastBlockAppHash = lastBlock.AppHash |
|
|
|
resInfo.LastBlockHeight = app.app.state.LatestVersion() |
|
|
|
resInfo.LastBlockAppHash = app.app.state.Hash() |
|
|
|
return resInfo |
|
|
|
} |
|
|
|
|
|
|
@ -85,19 +77,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 { |
|
|
|
// Save
|
|
|
|
appHash := app.app.state.Save() |
|
|
|
app.logger.Info("Saved state", "root", appHash) |
|
|
|
|
|
|
|
lastBlock := LastBlockInfo{ |
|
|
|
Height: app.blockHeader.Height, |
|
|
|
AppHash: appHash, // this hash will be in the next block header
|
|
|
|
} |
|
|
|
// Save a new version for next height
|
|
|
|
height := app.app.state.LatestVersion() + 1 |
|
|
|
var appHash []byte |
|
|
|
var err error |
|
|
|
|
|
|
|
app.logger.Info("Saving block", "height", lastBlock.Height, "root", lastBlock.AppHash) |
|
|
|
SaveLastBlock(app.db, lastBlock) |
|
|
|
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.logger.Info("Commit block", "height", height, "root", appHash) |
|
|
|
return types.NewResultOK(appHash, "") |
|
|
|
} |
|
|
|
|
|
|
@ -117,9 +111,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) |
|
|
|
} |
|
|
@ -129,41 +120,6 @@ func (app *PersistentDummyApplication) EndBlock(height uint64) (resEndBlock type |
|
|
|
return types.ResponseEndBlock{Diffs: app.changes} |
|
|
|
} |
|
|
|
|
|
|
|
//-----------------------------------------
|
|
|
|
// persist the last block info
|
|
|
|
|
|
|
|
var lastBlockKey = []byte("lastblock") |
|
|
|
|
|
|
|
type LastBlockInfo struct { |
|
|
|
Height uint64 |
|
|
|
AppHash []byte |
|
|
|
} |
|
|
|
|
|
|
|
// Get the last block from the db
|
|
|
|
func LoadLastBlock(db dbm.DB) (lastBlock LastBlockInfo) { |
|
|
|
buf := db.Get(lastBlockKey) |
|
|
|
if len(buf) != 0 { |
|
|
|
r, n, err := bytes.NewReader(buf), new(int), new(error) |
|
|
|
wire.ReadBinaryPtr(&lastBlock, r, 0, n, err) |
|
|
|
if *err != nil { |
|
|
|
cmn.PanicCrisis(errors.Wrap(*err, "cannot load last block (data has been corrupted or its spec has changed)")) |
|
|
|
} |
|
|
|
// TODO: ensure that buf is completely read.
|
|
|
|
} |
|
|
|
|
|
|
|
return lastBlock |
|
|
|
} |
|
|
|
|
|
|
|
func SaveLastBlock(db dbm.DB, lastBlock LastBlockInfo) { |
|
|
|
buf, n, err := new(bytes.Buffer), new(int), new(error) |
|
|
|
wire.WriteBinary(lastBlock, buf, n, err) |
|
|
|
if *err != nil { |
|
|
|
// TODO
|
|
|
|
cmn.PanicCrisis(errors.Wrap(*err, "cannot save last block")) |
|
|
|
} |
|
|
|
db.Set(lastBlockKey, buf.Bytes()) |
|
|
|
} |
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// update validators
|
|
|
|
|
|
|
|