mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-06 08:07:11 +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:
committed by
Ethan Buchman
parent
58c5df729b
commit
d65234ed51
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user