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.

73 lines
2.0 KiB

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