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
1.8 KiB

  1. package types
  2. import (
  3. "fmt"
  4. "golang.org/x/crypto/ripemd160"
  5. abci "github.com/tendermint/abci/types"
  6. wire "github.com/tendermint/go-wire"
  7. "github.com/tendermint/go-wire/data"
  8. "github.com/tendermint/tmlibs/merkle"
  9. )
  10. //-----------------------------------------------------------------------------
  11. // ABCIResult is the deterministic component of a ResponseDeliverTx.
  12. type ABCIResult struct {
  13. Code uint32 `json:"code"`
  14. Data data.Bytes `json:"data"`
  15. }
  16. // Hash returns the canonical json hash of the ABCIResult
  17. func (a ABCIResult) Hash() []byte {
  18. // stupid canonical json output, easy to check in any language
  19. bs := fmt.Sprintf(`{"code":%d,"data":"%s"}`, a.Code, a.Data)
  20. var hasher = ripemd160.New()
  21. hasher.Write([]byte(bs))
  22. return hasher.Sum(nil)
  23. }
  24. // ABCIResults wraps the deliver tx results to return a proof
  25. type ABCIResults []ABCIResult
  26. // NewResults creates ABCIResults from ResponseDeliverTx
  27. func NewResults(del []*abci.ResponseDeliverTx) ABCIResults {
  28. res := make(ABCIResults, len(del))
  29. for i, d := range del {
  30. res[i] = NewResultFromResponse(d)
  31. }
  32. return res
  33. }
  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 go-wire
  41. func (a ABCIResults) Bytes() []byte {
  42. return wire.BinaryBytes(a)
  43. }
  44. // Hash returns a merkle hash of all results
  45. func (a ABCIResults) Hash() []byte {
  46. return merkle.SimpleHashFromHashables(a.toHashables())
  47. }
  48. // ProveResult returns a merkle proof of one result from the set
  49. func (a ABCIResults) ProveResult(i int) merkle.SimpleProof {
  50. _, proofs := merkle.SimpleProofsFromHashables(a.toHashables())
  51. return *proofs[i]
  52. }
  53. func (a ABCIResults) toHashables() []merkle.Hashable {
  54. l := len(a)
  55. hashables := make([]merkle.Hashable, l)
  56. for i := 0; i < l; i++ {
  57. hashables[i] = a[i]
  58. }
  59. return hashables
  60. }