mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 06:42:16 +00:00
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:
parent
58c5df729b
commit
d65234ed51
@ -152,6 +152,15 @@ func (c *HTTP) Block(height *int64) (*ctypes.ResultBlock, error) {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *HTTP) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) {
|
||||||
|
result := new(ctypes.ResultBlockResults)
|
||||||
|
_, err := c.rpc.Call("block_results", map[string]interface{}{"height": height}, result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "Block Result")
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *HTTP) Commit(height *int64) (*ctypes.ResultCommit, error) {
|
func (c *HTTP) Commit(height *int64) (*ctypes.ResultCommit, error) {
|
||||||
result := new(ctypes.ResultCommit)
|
result := new(ctypes.ResultCommit)
|
||||||
_, err := c.rpc.Call("commit", map[string]interface{}{"height": height}, result)
|
_, err := c.rpc.Call("commit", map[string]interface{}{"height": height}, result)
|
||||||
|
@ -45,6 +45,7 @@ type ABCIClient interface {
|
|||||||
// signatures and prove anything about the chain
|
// signatures and prove anything about the chain
|
||||||
type SignClient interface {
|
type SignClient interface {
|
||||||
Block(height *int64) (*ctypes.ResultBlock, error)
|
Block(height *int64) (*ctypes.ResultBlock, error)
|
||||||
|
BlockResults(height *int64) (*ctypes.ResultBlockResults, error)
|
||||||
Commit(height *int64) (*ctypes.ResultCommit, error)
|
Commit(height *int64) (*ctypes.ResultCommit, error)
|
||||||
Validators(height *int64) (*ctypes.ResultValidators, error)
|
Validators(height *int64) (*ctypes.ResultValidators, error)
|
||||||
Tx(hash []byte, prove bool) (*ctypes.ResultTx, error)
|
Tx(hash []byte, prove bool) (*ctypes.ResultTx, error)
|
||||||
|
@ -100,6 +100,10 @@ func (Local) Block(height *int64) (*ctypes.ResultBlock, error) {
|
|||||||
return core.Block(height)
|
return core.Block(height)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (Local) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) {
|
||||||
|
return core.BlockResults(height)
|
||||||
|
}
|
||||||
|
|
||||||
func (Local) Commit(height *int64) (*ctypes.ResultCommit, error) {
|
func (Local) Commit(height *int64) (*ctypes.ResultCommit, error) {
|
||||||
return core.Commit(height)
|
return core.Commit(height)
|
||||||
}
|
}
|
||||||
|
@ -155,7 +155,6 @@ func TestAppCalls(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// make sure we can lookup the tx with proof
|
// make sure we can lookup the tx with proof
|
||||||
// ptx, err := c.Tx(bres.Hash, true)
|
|
||||||
ptx, err := c.Tx(bres.Hash, true)
|
ptx, err := c.Tx(bres.Hash, true)
|
||||||
require.Nil(err, "%d: %+v", i, err)
|
require.Nil(err, "%d: %+v", i, err)
|
||||||
assert.EqualValues(txh, ptx.Height)
|
assert.EqualValues(txh, ptx.Height)
|
||||||
@ -168,6 +167,15 @@ func TestAppCalls(t *testing.T) {
|
|||||||
assert.True(len(appHash) > 0)
|
assert.True(len(appHash) > 0)
|
||||||
assert.EqualValues(apph, block.BlockMeta.Header.Height)
|
assert.EqualValues(apph, block.BlockMeta.Header.Height)
|
||||||
|
|
||||||
|
// now check the results
|
||||||
|
blockResults, err := c.BlockResults(&apph)
|
||||||
|
require.Nil(err, "%d: %+v", i, err)
|
||||||
|
assert.Equal(apph, blockResults.Height)
|
||||||
|
if assert.Equal(1, len(blockResults.Results)) {
|
||||||
|
// check success code
|
||||||
|
assert.EqualValues(0, blockResults.Results[0].Code)
|
||||||
|
}
|
||||||
|
|
||||||
// check blockchain info, now that we know there is info
|
// check blockchain info, now that we know there is info
|
||||||
// TODO: is this commented somewhere that they are returned
|
// TODO: is this commented somewhere that they are returned
|
||||||
// in order of descending height???
|
// in order of descending height???
|
||||||
|
@ -312,3 +312,66 @@ func Commit(heightPtr *int64) (*ctypes.ResultCommit, error) {
|
|||||||
commit := blockStore.LoadBlockCommit(height)
|
commit := blockStore.LoadBlockCommit(height)
|
||||||
return ctypes.NewResultCommit(header, commit, true), nil
|
return ctypes.NewResultCommit(header, commit, true), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BlockResults gets ABCIResults at a given height.
|
||||||
|
// If no height is provided, it will fetch the latest block.
|
||||||
|
//
|
||||||
|
// Results are for the tx of the last block with the same index.
|
||||||
|
// Thus response.results[5] is the results of executing
|
||||||
|
// getBlock(h-1).Txs[5]
|
||||||
|
//
|
||||||
|
// ```shell
|
||||||
|
// curl 'localhost:46657/block_results?height=10'
|
||||||
|
// ```
|
||||||
|
//
|
||||||
|
// ```go
|
||||||
|
// client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
|
||||||
|
// info, err := client.BlockResults(10)
|
||||||
|
// ```
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// > The above command returns JSON structured like this:
|
||||||
|
//
|
||||||
|
// ```json
|
||||||
|
// {
|
||||||
|
// "height": 88,
|
||||||
|
// "results": [
|
||||||
|
// {
|
||||||
|
// "code": 0,
|
||||||
|
// "data": "CAFE00F00D"
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// "code": 102,
|
||||||
|
// "data": ""
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
// ```
|
||||||
|
func BlockResults(heightPtr *int64) (*ctypes.ResultBlockResults, error) {
|
||||||
|
var height int64
|
||||||
|
if heightPtr != nil {
|
||||||
|
height = *heightPtr
|
||||||
|
if height <= 0 {
|
||||||
|
return nil, fmt.Errorf("Height must be greater than 0")
|
||||||
|
}
|
||||||
|
storeHeight := blockStore.Height()
|
||||||
|
if height > storeHeight {
|
||||||
|
return nil, fmt.Errorf("Height must be less than or equal to the current blockchain height")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
height = blockStore.Height()
|
||||||
|
}
|
||||||
|
|
||||||
|
// load the results
|
||||||
|
state := consensusState.GetState()
|
||||||
|
results, err := state.LoadResults(height)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
res := &ctypes.ResultBlockResults{
|
||||||
|
Height: height,
|
||||||
|
Results: results,
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
@ -17,6 +17,7 @@ var Routes = map[string]*rpc.RPCFunc{
|
|||||||
"blockchain": rpc.NewRPCFunc(BlockchainInfo, "minHeight,maxHeight"),
|
"blockchain": rpc.NewRPCFunc(BlockchainInfo, "minHeight,maxHeight"),
|
||||||
"genesis": rpc.NewRPCFunc(Genesis, ""),
|
"genesis": rpc.NewRPCFunc(Genesis, ""),
|
||||||
"block": rpc.NewRPCFunc(Block, "height"),
|
"block": rpc.NewRPCFunc(Block, "height"),
|
||||||
|
"block_results": rpc.NewRPCFunc(BlockResults, "height"),
|
||||||
"commit": rpc.NewRPCFunc(Commit, "height"),
|
"commit": rpc.NewRPCFunc(Commit, "height"),
|
||||||
"tx": rpc.NewRPCFunc(Tx, "hash,prove"),
|
"tx": rpc.NewRPCFunc(Tx, "hash,prove"),
|
||||||
"tx_search": rpc.NewRPCFunc(TxSearch, "query,prove"),
|
"tx_search": rpc.NewRPCFunc(TxSearch, "query,prove"),
|
||||||
|
@ -33,6 +33,11 @@ type ResultCommit struct {
|
|||||||
CanonicalCommit bool `json:"canonical"`
|
CanonicalCommit bool `json:"canonical"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ResultBlockResults struct {
|
||||||
|
Height int64 `json:"height"`
|
||||||
|
Results types.ABCIResults `json:"results"`
|
||||||
|
}
|
||||||
|
|
||||||
// NewResultCommit is a helper to initialize the ResultCommit with
|
// NewResultCommit is a helper to initialize the ResultCommit with
|
||||||
// the embedded struct
|
// the embedded struct
|
||||||
func NewResultCommit(header *types.Header, commit *types.Commit,
|
func NewResultCommit(header *types.Header, commit *types.Commit,
|
||||||
|
@ -8,13 +8,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
abci "github.com/tendermint/abci/types"
|
abci "github.com/tendermint/abci/types"
|
||||||
"github.com/tendermint/go-wire/data"
|
|
||||||
"golang.org/x/crypto/ripemd160"
|
|
||||||
|
|
||||||
cmn "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
dbm "github.com/tendermint/tmlibs/db"
|
dbm "github.com/tendermint/tmlibs/db"
|
||||||
"github.com/tendermint/tmlibs/log"
|
"github.com/tendermint/tmlibs/log"
|
||||||
"github.com/tendermint/tmlibs/merkle"
|
|
||||||
|
|
||||||
wire "github.com/tendermint/go-wire"
|
wire "github.com/tendermint/go-wire"
|
||||||
|
|
||||||
@ -79,9 +76,9 @@ type State struct {
|
|||||||
LastHeightConsensusParamsChanged int64
|
LastHeightConsensusParamsChanged int64
|
||||||
|
|
||||||
// Store LastABCIResults along with hash
|
// Store LastABCIResults along with hash
|
||||||
LastResults ABCIResults // TODO: remove??
|
LastResults types.ABCIResults // TODO: remove??
|
||||||
LastResultHash []byte // this is the one for the next block to propose
|
LastResultHash []byte // this is the one for the next block to propose
|
||||||
LastLastResultHash []byte // this verifies the last block?
|
LastLastResultHash []byte // this verifies the last block?
|
||||||
|
|
||||||
// The latest AppHash we've received from calling abci.Commit()
|
// The latest AppHash we've received from calling abci.Commit()
|
||||||
AppHash []byte
|
AppHash []byte
|
||||||
@ -311,8 +308,8 @@ func (s *State) saveConsensusParamsInfo() {
|
|||||||
s.db.SetSync(calcConsensusParamsKey(nextHeight), paramsInfo.Bytes())
|
s.db.SetSync(calcConsensusParamsKey(nextHeight), paramsInfo.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadResults loads the ABCIResults for a given height.
|
// LoadResults loads the types.ABCIResults for a given height.
|
||||||
func (s *State) LoadResults(height int64) (ABCIResults, error) {
|
func (s *State) LoadResults(height int64) (types.ABCIResults, error) {
|
||||||
resInfo := s.loadResults(height)
|
resInfo := s.loadResults(height)
|
||||||
if resInfo == nil {
|
if resInfo == nil {
|
||||||
return nil, ErrNoResultsForHeight{height}
|
return nil, ErrNoResultsForHeight{height}
|
||||||
@ -320,13 +317,13 @@ func (s *State) LoadResults(height int64) (ABCIResults, error) {
|
|||||||
return resInfo, nil
|
return resInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) loadResults(height int64) ABCIResults {
|
func (s *State) loadResults(height int64) types.ABCIResults {
|
||||||
buf := s.db.Get(calcResultsKey(height))
|
buf := s.db.Get(calcResultsKey(height))
|
||||||
if len(buf) == 0 {
|
if len(buf) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
v := new(ABCIResults)
|
v := new(types.ABCIResults)
|
||||||
err := wire.ReadBinaryBytes(buf, v)
|
err := wire.ReadBinaryBytes(buf, v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
|
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
|
||||||
@ -396,7 +393,7 @@ func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader typ
|
|||||||
header.Time,
|
header.Time,
|
||||||
nextValSet,
|
nextValSet,
|
||||||
nextParams,
|
nextParams,
|
||||||
NewResults(abciResponses.DeliverTx))
|
types.NewResults(abciResponses.DeliverTx))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,7 +401,7 @@ func (s *State) setBlockAndValidators(height int64,
|
|||||||
newTxs int64, blockID types.BlockID, blockTime time.Time,
|
newTxs int64, blockID types.BlockID, blockTime time.Time,
|
||||||
valSet *types.ValidatorSet,
|
valSet *types.ValidatorSet,
|
||||||
params types.ConsensusParams,
|
params types.ConsensusParams,
|
||||||
results ABCIResults) {
|
results types.ABCIResults) {
|
||||||
|
|
||||||
s.LastBlockHeight = height
|
s.LastBlockHeight = height
|
||||||
s.LastBlockTotalTx += newTxs
|
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
|
// ValidatorsInfo represents the latest validator set, or the last height it changed
|
||||||
type ValidatorsInfo struct {
|
type ValidatorsInfo struct {
|
||||||
ValidatorSet *types.ValidatorSet
|
ValidatorSet *types.ValidatorSet
|
||||||
|
@ -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.
|
// TestResultsSaveLoad tests saving and loading abci results.
|
||||||
func TestResultsSaveLoad(t *testing.T) {
|
func TestResultsSaveLoad(t *testing.T) {
|
||||||
tearDown, _, state := setupTestCase(t)
|
tearDown, _, state := setupTestCase(t)
|
||||||
@ -324,17 +290,17 @@ func TestResultsSaveLoad(t *testing.T) {
|
|||||||
// height is implied index+2
|
// height is implied index+2
|
||||||
// as block 1 is created from genesis
|
// as block 1 is created from genesis
|
||||||
added []*abci.ResponseDeliverTx
|
added []*abci.ResponseDeliverTx
|
||||||
expected ABCIResults
|
expected types.ABCIResults
|
||||||
}{
|
}{
|
||||||
0: {
|
0: {
|
||||||
[]*abci.ResponseDeliverTx{},
|
[]*abci.ResponseDeliverTx{},
|
||||||
ABCIResults{},
|
types.ABCIResults{},
|
||||||
},
|
},
|
||||||
1: {
|
1: {
|
||||||
[]*abci.ResponseDeliverTx{
|
[]*abci.ResponseDeliverTx{
|
||||||
{Code: 32, Data: []byte("Hello"), Log: "Huh?"},
|
{Code: 32, Data: []byte("Hello"), Log: "Huh?"},
|
||||||
},
|
},
|
||||||
ABCIResults{
|
types.ABCIResults{
|
||||||
{32, []byte("Hello")},
|
{32, []byte("Hello")},
|
||||||
}},
|
}},
|
||||||
2: {
|
2: {
|
||||||
@ -346,13 +312,13 @@ func TestResultsSaveLoad(t *testing.T) {
|
|||||||
abci.KVPairString("build", "stuff"),
|
abci.KVPairString("build", "stuff"),
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
ABCIResults{
|
types.ABCIResults{
|
||||||
{383, []byte{}},
|
{383, []byte{}},
|
||||||
{0, []byte("Gotcha!")},
|
{0, []byte("Gotcha!")},
|
||||||
}},
|
}},
|
||||||
3: {
|
3: {
|
||||||
nil,
|
nil,
|
||||||
ABCIResults{},
|
types.ABCIResults{},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
70
types/results.go
Normal file
70
types/results.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/ripemd160"
|
||||||
|
|
||||||
|
abci "github.com/tendermint/abci/types"
|
||||||
|
wire "github.com/tendermint/go-wire"
|
||||||
|
"github.com/tendermint/go-wire/data"
|
||||||
|
"github.com/tendermint/tmlibs/merkle"
|
||||||
|
)
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
41
types/results_test.go
Normal file
41
types/results_test.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user