tendermint/types/results.go

74 lines
1.9 KiB
Go
Raw Normal View History

package types
import (
2018-06-21 21:59:02 -07:00
abci "github.com/tendermint/tendermint/abci/types"
2018-06-30 22:40:03 -07:00
"github.com/tendermint/tendermint/crypto/merkle"
2018-07-01 19:36:49 -07:00
cmn "github.com/tendermint/tendermint/libs/common"
)
//-----------------------------------------------------------------------------
// ABCIResult is the deterministic component of a ResponseDeliverTx.
2017-12-26 16:53:26 -08:00
// TODO: add Tags
type ABCIResult struct {
2018-02-03 00:42:59 -08:00
Code uint32 `json:"code"`
Data cmn.HexBytes `json:"data"`
}
2017-12-26 16:53:26 -08:00
// Hash returns the canonical hash of the ABCIResult
func (a ABCIResult) Hash() []byte {
bz := aminoHash(a)
return bz
}
// ABCIResults wraps the deliver tx results to return a proof
type ABCIResults []ABCIResult
// NewResults creates ABCIResults from the list of ResponseDeliverTx.
func NewResults(responses []*abci.ResponseDeliverTx) ABCIResults {
res := make(ABCIResults, len(responses))
for i, d := range responses {
res[i] = NewResultFromResponse(d)
}
return res
}
// NewResultFromResponse creates ABCIResult from ResponseDeliverTx.
func NewResultFromResponse(response *abci.ResponseDeliverTx) ABCIResult {
return ABCIResult{
Code: response.Code,
Data: response.Data,
}
}
2018-02-15 11:26:49 -08:00
// Bytes serializes the ABCIResponse using wire
func (a ABCIResults) Bytes() []byte {
bz, err := cdc.MarshalBinary(a)
if err != nil {
panic(err)
}
return bz
}
// Hash returns a merkle hash of all results
func (a ABCIResults) Hash() []byte {
2018-06-30 22:40:03 -07:00
// NOTE: we copy the impl of the merkle tree for txs -
// we should be consistent and either do it for both or not.
2018-02-03 00:23:10 -08:00
return merkle.SimpleHashFromHashers(a.toHashers())
}
// ProveResult returns a merkle proof of one result from the set
func (a ABCIResults) ProveResult(i int) merkle.SimpleProof {
2018-02-03 00:23:10 -08:00
_, proofs := merkle.SimpleProofsFromHashers(a.toHashers())
return *proofs[i]
}
2018-02-03 00:23:10 -08:00
func (a ABCIResults) toHashers() []merkle.Hasher {
l := len(a)
2018-02-03 00:23:10 -08:00
hashers := make([]merkle.Hasher, l)
for i := 0; i < l; i++ {
2018-02-03 00:23:10 -08:00
hashers[i] = a[i]
}
2018-02-03 00:23:10 -08:00
return hashers
}