You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.0 KiB

7 years ago
  1. package types
  2. import (
  3. abci "github.com/tendermint/tendermint/abci/types"
  4. "github.com/tendermint/tendermint/crypto/merkle"
  5. "github.com/tendermint/tendermint/crypto/tmhash"
  6. cmn "github.com/tendermint/tendermint/libs/common"
  7. )
  8. //-----------------------------------------------------------------------------
  9. // ABCIResult is the deterministic component of a ResponseDeliverTx.
  10. // TODO: add Tags
  11. type ABCIResult struct {
  12. Code uint32 `json:"code"`
  13. Data cmn.HexBytes `json:"data"`
  14. }
  15. // Hash returns the canonical hash of the ABCIResult
  16. func (a ABCIResult) Hash() []byte {
  17. bz := tmhash.Sum(cdcEncode(a))
  18. return bz
  19. }
  20. func (a ABCIResult) Bytes() []byte {
  21. return cdcEncode(a)
  22. }
  23. // ABCIResults wraps the deliver tx results to return a proof
  24. type ABCIResults []ABCIResult
  25. // NewResults creates ABCIResults from the list of ResponseDeliverTx.
  26. func NewResults(responses []*abci.ResponseDeliverTx) ABCIResults {
  27. res := make(ABCIResults, len(responses))
  28. for i, d := range responses {
  29. res[i] = NewResultFromResponse(d)
  30. }
  31. return res
  32. }
  33. // NewResultFromResponse creates ABCIResult from ResponseDeliverTx.
  34. func NewResultFromResponse(response *abci.ResponseDeliverTx) ABCIResult {
  35. return ABCIResult{
  36. Code: response.Code,
  37. Data: response.Data,
  38. }
  39. }
  40. // Bytes serializes the ABCIResponse using wire
  41. func (a ABCIResults) Bytes() []byte {
  42. bz, err := cdc.MarshalBinary(a)
  43. if err != nil {
  44. panic(err)
  45. }
  46. return bz
  47. }
  48. // Hash returns a merkle hash of all results
  49. func (a ABCIResults) Hash() []byte {
  50. // NOTE: we copy the impl of the merkle tree for txs -
  51. // we should be consistent and either do it for both or not.
  52. return merkle.SimpleHashFromByteSlices(a.toByteSlices())
  53. }
  54. // ProveResult returns a merkle proof of one result from the set
  55. func (a ABCIResults) ProveResult(i int) merkle.SimpleProof {
  56. _, proofs := merkle.SimpleProofsFromByteSlices(a.toByteSlices())
  57. return *proofs[i]
  58. }
  59. func (a ABCIResults) toByteSlices() [][]byte {
  60. l := len(a)
  61. bzs := make([][]byte, l)
  62. for i := 0; i < l; i++ {
  63. bzs[i] = a[i].Bytes()
  64. }
  65. return bzs
  66. }