Add /block_results?height=H as rpc endpoint

Expose it in rpc client
Move ABCIResults into tendermint/types from tendermint/state
This commit is contained in:
Ethan Frey
2017-12-26 19:24:25 -05:00
committed by Ethan Buchman
parent 58c5df729b
commit d65234ed51
11 changed files with 217 additions and 110 deletions
+9 -70
View File
@@ -8,13 +8,10 @@ import (
"time"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-wire/data"
"golang.org/x/crypto/ripemd160"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
"github.com/tendermint/tmlibs/merkle"
wire "github.com/tendermint/go-wire"
@@ -79,9 +76,9 @@ type State struct {
LastHeightConsensusParamsChanged int64
// Store LastABCIResults along with hash
LastResults ABCIResults // TODO: remove??
LastResultHash []byte // this is the one for the next block to propose
LastLastResultHash []byte // this verifies the last block?
LastResults types.ABCIResults // TODO: remove??
LastResultHash []byte // this is the one for the next block to propose
LastLastResultHash []byte // this verifies the last block?
// The latest AppHash we've received from calling abci.Commit()
AppHash []byte
@@ -311,8 +308,8 @@ func (s *State) saveConsensusParamsInfo() {
s.db.SetSync(calcConsensusParamsKey(nextHeight), paramsInfo.Bytes())
}
// LoadResults loads the ABCIResults for a given height.
func (s *State) LoadResults(height int64) (ABCIResults, error) {
// LoadResults loads the types.ABCIResults for a given height.
func (s *State) LoadResults(height int64) (types.ABCIResults, error) {
resInfo := s.loadResults(height)
if resInfo == nil {
return nil, ErrNoResultsForHeight{height}
@@ -320,13 +317,13 @@ func (s *State) LoadResults(height int64) (ABCIResults, error) {
return resInfo, nil
}
func (s *State) loadResults(height int64) ABCIResults {
func (s *State) loadResults(height int64) types.ABCIResults {
buf := s.db.Get(calcResultsKey(height))
if len(buf) == 0 {
return nil
}
v := new(ABCIResults)
v := new(types.ABCIResults)
err := wire.ReadBinaryBytes(buf, v)
if err != nil {
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
@@ -396,7 +393,7 @@ func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader typ
header.Time,
nextValSet,
nextParams,
NewResults(abciResponses.DeliverTx))
types.NewResults(abciResponses.DeliverTx))
return nil
}
@@ -404,7 +401,7 @@ func (s *State) setBlockAndValidators(height int64,
newTxs int64, blockID types.BlockID, blockTime time.Time,
valSet *types.ValidatorSet,
params types.ConsensusParams,
results ABCIResults) {
results types.ABCIResults) {
s.LastBlockHeight = height
s.LastBlockTotalTx += newTxs
@@ -455,64 +452,6 @@ func (a *ABCIResponses) Bytes() []byte {
//-----------------------------------------------------------------------------
// ABCIResult is just the essential info to prove
// success/failure of a DeliverTx
type ABCIResult struct {
Code uint32 `json:"code"`
Data data.Bytes `json:"data"`
}
// Hash creates a canonical json hash of the ABCIResult
func (a ABCIResult) Hash() []byte {
// stupid canonical json output, easy to check in any language
bs := fmt.Sprintf(`{"code":%d,"data":"%s"}`, a.Code, a.Data)
var hasher = ripemd160.New()
hasher.Write([]byte(bs))
return hasher.Sum(nil)
}
// ABCIResults wraps the deliver tx results to return a proof
type ABCIResults []ABCIResult
// NewResults creates ABCIResults from ResponseDeliverTx
func NewResults(del []*abci.ResponseDeliverTx) ABCIResults {
res := make(ABCIResults, len(del))
for i, d := range del {
res[i] = ABCIResult{
Code: d.Code,
Data: d.Data,
}
}
return res
}
// Bytes serializes the ABCIResponse using go-wire
func (a ABCIResults) Bytes() []byte {
return wire.BinaryBytes(a)
}
// Hash returns a merkle hash of all results
func (a ABCIResults) Hash() []byte {
return merkle.SimpleHashFromHashables(a.toHashables())
}
// ProveResult returns a merkle proof of one result from the set
func (a ABCIResults) ProveResult(i int) merkle.SimpleProof {
_, proofs := merkle.SimpleProofsFromHashables(a.toHashables())
return *proofs[i]
}
func (a ABCIResults) toHashables() []merkle.Hashable {
l := len(a)
hashables := make([]merkle.Hashable, l)
for i := 0; i < l; i++ {
hashables[i] = a[i]
}
return hashables
}
//-----------------------------------------------------------------------------
// ValidatorsInfo represents the latest validator set, or the last height it changed
type ValidatorsInfo struct {
ValidatorSet *types.ValidatorSet
+5 -39
View File
@@ -279,40 +279,6 @@ func TestConsensusParamsChangesSaveLoad(t *testing.T) {
}
}
func TestABCIResults(t *testing.T) {
a := ABCIResult{Code: 0, Data: nil}
b := ABCIResult{Code: 0, Data: []byte{}}
c := ABCIResult{Code: 0, Data: []byte("one")}
d := ABCIResult{Code: 14, Data: nil}
e := ABCIResult{Code: 14, Data: []byte("foo")}
f := ABCIResult{Code: 14, Data: []byte("bar")}
// nil and []byte{} should produce same hash
assert.Equal(t, a.Hash(), b.Hash())
// a and b should be the same, don't go in results
results := ABCIResults{a, c, d, e, f}
// make sure each result hashes properly
var last []byte
for i, res := range results {
h := res.Hash()
assert.NotEqual(t, last, h, "%d", i)
last = h
}
// make sure that we can get a root hash from results
// and verify proofs
root := results.Hash()
assert.NotEmpty(t, root)
for i, res := range results {
proof := results.ProveResult(i)
valid := proof.Verify(i, len(results), res.Hash(), root)
assert.True(t, valid, "%d", i)
}
}
// TestResultsSaveLoad tests saving and loading abci results.
func TestResultsSaveLoad(t *testing.T) {
tearDown, _, state := setupTestCase(t)
@@ -324,17 +290,17 @@ func TestResultsSaveLoad(t *testing.T) {
// height is implied index+2
// as block 1 is created from genesis
added []*abci.ResponseDeliverTx
expected ABCIResults
expected types.ABCIResults
}{
0: {
[]*abci.ResponseDeliverTx{},
ABCIResults{},
types.ABCIResults{},
},
1: {
[]*abci.ResponseDeliverTx{
{Code: 32, Data: []byte("Hello"), Log: "Huh?"},
},
ABCIResults{
types.ABCIResults{
{32, []byte("Hello")},
}},
2: {
@@ -346,13 +312,13 @@ func TestResultsSaveLoad(t *testing.T) {
abci.KVPairString("build", "stuff"),
}},
},
ABCIResults{
types.ABCIResults{
{383, []byte{}},
{0, []byte("Gotcha!")},
}},
3: {
nil,
ABCIResults{},
types.ABCIResults{},
},
}