Make fields in ResponseInfo be flat

This commit is contained in:
Jae Kwon
2016-12-26 17:44:36 -08:00
committed by Ethan Buchman
parent 0d82d26408
commit 8b76f3dd00
15 changed files with 186 additions and 254 deletions

View File

@ -36,8 +36,8 @@ func NewChainAwareApplication() *ChainAwareApplication {
return &ChainAwareApplication{}
}
func (app *ChainAwareApplication) Info() (string, *types.TMSPInfo, *types.LastBlockInfo, *types.ConfigInfo) {
return "nil", nil, nil, nil
func (app *ChainAwareApplication) Info() types.ResponseInfo {
return types.ResponseInfo{}
}
func (app *ChainAwareApplication) SetOption(key string, value string) (log string) {

View File

@ -17,8 +17,8 @@ func NewCounterApplication(serial bool) *CounterApplication {
return &CounterApplication{serial: serial}
}
func (app *CounterApplication) Info() (string, *types.TMSPInfo, *types.LastBlockInfo, *types.ConfigInfo) {
return Fmt("{\"hashes\":%v,\"txs\":%v}", app.hashCount, app.txCount), nil, nil, nil
func (app *CounterApplication) Info() types.ResponseInfo {
return types.ResponseInfo{Data: Fmt("{\"hashes\":%v,\"txs\":%v}", app.hashCount, app.txCount)}
}
func (app *CounterApplication) SetOption(key string, value string) (log string) {

View File

@ -19,8 +19,8 @@ func NewDummyApplication() *DummyApplication {
return &DummyApplication{state: state}
}
func (app *DummyApplication) Info() (string, *types.TMSPInfo, *types.LastBlockInfo, *types.ConfigInfo) {
return Fmt("{\"size\":%v}", app.state.Size()), nil, nil, nil
func (app *DummyApplication) Info() (resInfo types.ResponseInfo) {
return types.ResponseInfo{Data: Fmt("{\"size\":%v}", app.state.Size())}
}
func (app *DummyApplication) SetOption(key string, value string) (log string) {

View File

@ -72,9 +72,9 @@ func TestPersistentDummyInfo(t *testing.T) {
dummy := NewPersistentDummyApplication(dir)
height := uint64(0)
_, _, lastBlockInfo, _ := dummy.Info()
if lastBlockInfo.BlockHeight != height {
t.Fatalf("expected height of %d, got %d", height, lastBlockInfo.BlockHeight)
resInfo := dummy.Info()
if resInfo.LastBlockHeight != height {
t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
}
// make and apply block
@ -87,9 +87,9 @@ func TestPersistentDummyInfo(t *testing.T) {
dummy.EndBlock(height)
dummy.Commit()
_, _, lastBlockInfo, _ = dummy.Info()
if lastBlockInfo.BlockHeight != height {
t.Fatalf("expected height of %d, got %d", height, lastBlockInfo.BlockHeight)
resInfo = dummy.Info()
if resInfo.LastBlockHeight != height {
t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
}
}

View File

@ -38,7 +38,7 @@ func NewPersistentDummyApplication(dbDir string) *PersistentDummyApplication {
stateTree := merkle.NewIAVLTree(0, db)
stateTree.Load(lastBlock.AppHash)
log.Notice("Loaded state", "block", lastBlock.BlockHeight, "root", stateTree.Hash())
log.Notice("Loaded state", "block", lastBlock.Height, "root", stateTree.Hash())
return &PersistentDummyApplication{
app: &DummyApplication{state: stateTree},
@ -46,10 +46,12 @@ func NewPersistentDummyApplication(dbDir string) *PersistentDummyApplication {
}
}
func (app *PersistentDummyApplication) Info() (string, *types.TMSPInfo, *types.LastBlockInfo, *types.ConfigInfo) {
s, _, _, _ := app.app.Info()
func (app *PersistentDummyApplication) Info() (resInfo types.ResponseInfo) {
resInfo = app.app.Info()
lastBlock := LoadLastBlock(app.db)
return s, nil, &lastBlock, nil
resInfo.LastBlockHeight = lastBlock.Height
resInfo.LastBlockAppHash = lastBlock.AppHash
return resInfo
}
func (app *PersistentDummyApplication) SetOption(key string, value string) (log string) {
@ -79,9 +81,9 @@ func (app *PersistentDummyApplication) Commit() types.Result {
appHash := app.app.state.Save()
log.Info("Saved state", "root", appHash)
lastBlock := types.LastBlockInfo{
BlockHeight: app.blockHeader.Height,
AppHash: appHash, // this hash will be in the next block header
lastBlock := LastBlockInfo{
Height: app.blockHeader.Height,
AppHash: appHash, // this hash will be in the next block header
}
SaveLastBlock(app.db, lastBlock)
return types.NewResultOK(appHash, "")
@ -120,8 +122,13 @@ func (app *PersistentDummyApplication) EndBlock(height uint64) (diffs []*types.V
var lastBlockKey = []byte("lastblock")
type LastBlockInfo struct {
Height uint64
AppHash []byte
}
// Get the last block from the db
func LoadLastBlock(db dbm.DB) (lastBlock types.LastBlockInfo) {
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)
@ -136,8 +143,8 @@ func LoadLastBlock(db dbm.DB) (lastBlock types.LastBlockInfo) {
return lastBlock
}
func SaveLastBlock(db dbm.DB, lastBlock types.LastBlockInfo) {
log.Notice("Saving block", "height", lastBlock.BlockHeight, "root", lastBlock.AppHash)
func SaveLastBlock(db dbm.DB, lastBlock LastBlockInfo) {
log.Notice("Saving block", "height", lastBlock.Height, "root", lastBlock.AppHash)
buf, n, err := new(bytes.Buffer), new(int), new(error)
wire.WriteBinary(lastBlock, buf, n, err)
if *err != nil {

View File

@ -11,8 +11,8 @@ func NewNilApplication() *NilApplication {
return &NilApplication{}
}
func (app *NilApplication) Info() (string, *types.TMSPInfo, *types.LastBlockInfo, *types.ConfigInfo) {
return "nil", nil, nil, nil
func (app *NilApplication) Info() (resInfo types.ResponseInfo) {
return
}
func (app *NilApplication) SetOption(key string, value string) (log string) {