/
results.go
116 lines (98 loc) · 2.84 KB
/
results.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package types
import (
gobytes "bytes"
abci "github.com/furyaxyz/fuxchain/libs/tendermint/abci/types"
"github.com/furyaxyz/fuxchain/libs/tendermint/libs/bytes"
"github.com/furyaxyz/fuxchain/libs/tendermint/crypto/merkle"
"github.com/tendermint/go-amino"
)
//-----------------------------------------------------------------------------
// ABCIResult is the deterministic component of a ResponseDeliverTx.
// TODO: add tags and other fields
// https://github.com/tendermint/tendermint/issues/1007
type ABCIResult struct {
Code uint32 `json:"code"`
Data bytes.HexBytes `json:"data"`
}
func (a ABCIResult) AminoSize() int {
var size int
if a.Code != 0 {
size += 1 + amino.UvarintSize(uint64(a.Code))
}
if len(a.Data) > 0 {
size += 1 + amino.UvarintSize(uint64(len(a.Data))) + len(a.Data)
}
return size
}
func (a ABCIResult) MarshalToAmino(_ *amino.Codec) ([]byte, error) {
buf := &gobytes.Buffer{}
buf.Grow(a.AminoSize())
if a.Code != 0 {
const pbKey = 1<<3 | byte(amino.Typ3_Varint)
err := amino.EncodeUvarintWithKeyToBuffer(buf, uint64(a.Code), pbKey)
if err != nil {
return nil, err
}
}
if len(a.Data) != 0 {
const pbKey = 2<<3 | byte(amino.Typ3_ByteLength)
err := amino.EncodeByteSliceWithKeyToBuffer(buf, a.Data, pbKey)
if err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}
// Bytes returns the amino encoded ABCIResult
func (a ABCIResult) Bytes() []byte {
bz, err := a.MarshalToAmino(cdc)
if err != nil {
return cdcEncode(a)
} else {
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,
}
}
// Bytes serializes the ABCIResponse using amino
func (a ABCIResults) Bytes() []byte {
bz, err := cdc.MarshalBinaryLengthPrefixed(a)
if err != nil {
panic(err)
}
return bz
}
// Hash returns a merkle hash of all results
func (a ABCIResults) Hash() []byte {
// NOTE: we copy the impl of the merkle tree for txs -
// we should be consistent and either do it for both or not.
return merkle.SimpleHashFromByteSlices(a.toByteSlices())
}
// ProveResult returns a merkle proof of one result from the set
func (a ABCIResults) ProveResult(i int) merkle.SimpleProof {
_, proofs := merkle.SimpleProofsFromByteSlices(a.toByteSlices())
return *proofs[i]
}
func (a ABCIResults) toByteSlices() [][]byte {
l := len(a)
bzs := make([][]byte, l)
for i := 0; i < l; i++ {
bzs[i] = a[i].Bytes()
}
return bzs
}