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