Skip to content

Commit

Permalink
Make sure height in store increments every block, even if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Oct 10, 2017
1 parent 9092b4d commit 5238448
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
1 change: 0 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ func (app *Basecoin) InitChain(req abci.RequestInitChain) {

// BeginBlock - ABCI
func (app *Basecoin) BeginBlock(req abci.RequestBeginBlock) {
fmt.Println("BeginBlock")
app.height++
// for _, plugin := range app.plugins.GetList() {
// plugin.BeginBlock(app.state, hash, header)
Expand Down
24 changes: 10 additions & 14 deletions app/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
// Store contains the merkle tree, and all info to handle abci requests
type Store struct {
state.State
height uint64
logger log.Logger
}

Expand Down Expand Up @@ -81,14 +82,10 @@ func NewStore(dbName string, cacheSize int, logger log.Logger) (*Store, error) {
State: state.NewState(tree),
logger: logger,
}
res.height = res.State.LatestHeight()
return res, nil
}

// Height gets the last height stored in the database
func (s *Store) Height() uint64 {
return s.State.LatestHeight()
}

// Hash gets the last hash stored in the database
func (s *Store) Hash() []byte {
return s.State.LatestHash()
Expand All @@ -98,32 +95,31 @@ func (s *Store) Hash() []byte {
// The height is the block that holds the transactions, not the apphash itself.
func (s *Store) Info() abci.ResponseInfo {
s.logger.Info("Info synced",
"height", s.Height(),
"height", s.height,
"hash", fmt.Sprintf("%X", s.Hash()))
return abci.ResponseInfo{
Data: cmn.Fmt("size:%v", s.State.Size()),
LastBlockHeight: s.Height() - 1,
LastBlockHeight: s.height - 1,
LastBlockAppHash: s.Hash(),
}
}

// Commit implements abci.Application
func (s *Store) Commit() abci.Result {
height := s.Height() + 1
s.height++

hash, err := s.State.Commit(height)
hash, err := s.State.Commit(s.height)
if err != nil {
return abci.NewError(abci.CodeType_InternalError, err.Error())
}
s.logger.Debug("Commit synced",
"height", height,
"height", s.height,
"hash", fmt.Sprintf("%X", hash),
)

if s.State.Size() == 0 {
return abci.NewResultOK(nil, "Empty hash for empty tree")
}
fmt.Printf("ABCI Commit: %d / %X\n", height, hash)
return abci.NewResultOK(hash, "")
}

Expand All @@ -134,10 +130,10 @@ func (s *Store) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery)

height := reqQuery.Height
if height == 0 {
if tree.Tree.VersionExists(s.Height() - 1) {
height = s.Height() - 1
if tree.Tree.VersionExists(s.height - 1) {
height = s.height - 1
} else {
height = s.Height()
height = s.height
}
}
resQuery.Height = height
Expand Down
11 changes: 2 additions & 9 deletions client/query.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package client

import (
"fmt"

"github.com/pkg/errors"

"github.com/tendermint/go-wire/data"
Expand Down Expand Up @@ -41,9 +39,9 @@ func GetWithProof(key []byte, node client.Client, cert certifiers.Certifier) (
return
}

// AppHash for height H is in header H+1
var check lc.Checkpoint
// check, err = GetCertifiedCheckpoint(int(resp.Height), node, cert)
check, err = GetCertifiedCheckpoint(int(resp.Height+9), node, cert)
check, err = GetCertifiedCheckpoint(int(resp.Height+1), node, cert)
if err != nil {
return
}
Expand Down Expand Up @@ -73,11 +71,6 @@ func GetWithProof(key []byte, node client.Client, cert certifiers.Certifier) (
err = errors.Wrap(err, "Error reading proof")
return
}

fmt.Printf("apphash: %x\n", check.Header.AppHash)
fmt.Printf("proof hash: %x\n", aproof.Root())
fmt.Printf("response height: %d\n", resp.Height)

// Validate the proof against the certified header to ensure data integrity.
err = aproof.Verify(resp.Key, nil, check.Header.AppHash)
if err != nil {
Expand Down

0 comments on commit 5238448

Please sign in to comment.