-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
abi_codec.go
213 lines (192 loc) · 6.77 KB
/
abi_codec.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package encoding
import (
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/pkg/errors"
)
type ReportCodec interface {
EncodeReport(requests []*ProcessedRequest) ([]byte, error)
DecodeReport(raw []byte) ([]*ProcessedRequest, error)
}
type reportCodecV0 struct {
reportTypes abi.Arguments
}
type reportCodecV1 struct {
reportTypes abi.Arguments
}
func NewReportCodec(contractVersion uint32) (ReportCodec, error) {
switch contractVersion {
case 0: // deprecated
reportTypes, err := getReportTypesV0()
if err != nil {
return nil, err
}
return &reportCodecV0{reportTypes: reportTypes}, nil
case 1:
reportTypes, err := getReportTypesV1()
if err != nil {
return nil, err
}
return &reportCodecV1{reportTypes: reportTypes}, nil
default:
return nil, fmt.Errorf("unknown contract version: %d", contractVersion)
}
}
func SliceToByte32(slice []byte) ([32]byte, error) {
if len(slice) != 32 {
return [32]byte{}, fmt.Errorf("input length is not 32 bytes: %d", len(slice))
}
var res [32]byte
copy(res[:], slice[:32])
return res, nil
}
func getReportTypesV0() (abi.Arguments, error) {
bytes32ArrType, err := abi.NewType("bytes32[]", "", []abi.ArgumentMarshaling{})
if err != nil {
return nil, fmt.Errorf("unable to create an ABI type object for bytes32[]")
}
bytesArrType, err := abi.NewType("bytes[]", "", []abi.ArgumentMarshaling{})
if err != nil {
return nil, fmt.Errorf("unable to create an ABI type object for bytes[]")
}
return abi.Arguments([]abi.Argument{
{Name: "ids", Type: bytes32ArrType},
{Name: "results", Type: bytesArrType},
{Name: "errors", Type: bytesArrType},
}), nil
}
func (c *reportCodecV0) EncodeReport(requests []*ProcessedRequest) ([]byte, error) {
size := len(requests)
if size == 0 {
return []byte{}, nil
}
ids := make([][32]byte, size)
results := make([][]byte, size)
errors := make([][]byte, size)
for i := 0; i < size; i++ {
var err error
ids[i], err = SliceToByte32(requests[i].RequestID)
if err != nil {
return nil, err
}
results[i] = requests[i].Result
errors[i] = requests[i].Error
}
return c.reportTypes.Pack(ids, results, errors)
}
func (c *reportCodecV0) DecodeReport(raw []byte) ([]*ProcessedRequest, error) {
reportElems := map[string]interface{}{}
if err := c.reportTypes.UnpackIntoMap(reportElems, raw); err != nil {
return nil, errors.WithMessage(err, "unable to unpack elements from raw report")
}
idsIface, idsOK := reportElems["ids"]
resultsIface, resultsOK := reportElems["results"]
errorsIface, errorsOK := reportElems["errors"]
if !idsOK || !resultsOK || !errorsOK {
return nil, fmt.Errorf("missing arrays in raw report, ids: %v, results: %v, errors: %v", idsOK, resultsOK, errorsOK)
}
ids, idsOK := idsIface.([][32]byte)
results, resultsOK := resultsIface.([][]byte)
errors, errorsOK := errorsIface.([][]byte)
if !idsOK || !resultsOK || !errorsOK {
return nil, fmt.Errorf("unable to cast part of raw report into array type, ids: %v, results: %v, errors: %v", idsOK, resultsOK, errorsOK)
}
size := len(ids)
if len(results) != size || len(errors) != size {
return nil, fmt.Errorf("unequal sizes of arrays parsed from raw report, ids: %v, results: %v, errors: %v", len(ids), len(results), len(errors))
}
if size == 0 {
return []*ProcessedRequest{}, nil
}
decoded := make([]*ProcessedRequest, size)
for i := 0; i < size; i++ {
decoded[i] = &ProcessedRequest{
RequestID: ids[i][:],
Result: results[i],
Error: errors[i],
}
}
return decoded, nil
}
func getReportTypesV1() (abi.Arguments, error) {
bytes32ArrType, err := abi.NewType("bytes32[]", "", []abi.ArgumentMarshaling{})
if err != nil {
return nil, fmt.Errorf("unable to create an ABI type object for bytes32[]")
}
bytesArrType, err := abi.NewType("bytes[]", "", []abi.ArgumentMarshaling{})
if err != nil {
return nil, fmt.Errorf("unable to create an ABI type object for bytes[]")
}
return abi.Arguments([]abi.Argument{
{Name: "ids", Type: bytes32ArrType},
{Name: "results", Type: bytesArrType},
{Name: "errors", Type: bytesArrType},
{Name: "onchain_metadata", Type: bytesArrType},
{Name: "processing_metadata", Type: bytesArrType},
}), nil
}
func (c *reportCodecV1) EncodeReport(requests []*ProcessedRequest) ([]byte, error) {
size := len(requests)
if size == 0 {
return []byte{}, nil
}
ids := make([][32]byte, size)
results := make([][]byte, size)
errors := make([][]byte, size)
onchainMetadata := make([][]byte, size)
processingMetadata := make([][]byte, size)
for i := 0; i < size; i++ {
var err error
ids[i], err = SliceToByte32(requests[i].RequestID)
if err != nil {
return nil, err
}
results[i] = requests[i].Result
errors[i] = requests[i].Error
onchainMetadata[i] = requests[i].OnchainMetadata
processingMetadata[i] = requests[i].CoordinatorContract
// CallbackGasLimit is not ABI-encoded
}
return c.reportTypes.Pack(ids, results, errors, onchainMetadata, processingMetadata)
}
func (c *reportCodecV1) DecodeReport(raw []byte) ([]*ProcessedRequest, error) {
reportElems := map[string]interface{}{}
if err := c.reportTypes.UnpackIntoMap(reportElems, raw); err != nil {
return nil, errors.WithMessage(err, "unable to unpack elements from raw report")
}
idsIface, idsOK := reportElems["ids"]
resultsIface, resultsOK := reportElems["results"]
errorsIface, errorsOK := reportElems["errors"]
onchainMetaIface, onchainMetaOK := reportElems["onchain_metadata"]
processingMetaIface, processingMetaOK := reportElems["processing_metadata"]
if !idsOK || !resultsOK || !errorsOK || !onchainMetaOK || !processingMetaOK {
return nil, fmt.Errorf("missing arrays in raw report, ids: %v, results: %v, errors: %v", idsOK, resultsOK, errorsOK)
}
ids, idsOK := idsIface.([][32]byte)
results, resultsOK := resultsIface.([][]byte)
errors, errorsOK := errorsIface.([][]byte)
onchainMeta, onchainMetaOK := onchainMetaIface.([][]byte)
processingMeta, processingMetaOK := processingMetaIface.([][]byte)
if !idsOK || !resultsOK || !errorsOK || !onchainMetaOK || !processingMetaOK {
return nil, fmt.Errorf("unable to cast part of raw report into array type, ids: %v, results: %v, errors: %v", idsOK, resultsOK, errorsOK)
}
size := len(ids)
if len(results) != size || len(errors) != size || len(onchainMeta) != size || len(processingMeta) != size {
return nil, fmt.Errorf("unequal sizes of arrays parsed from raw report, ids: %v, results: %v, errors: %v", len(ids), len(results), len(errors))
}
if size == 0 {
return []*ProcessedRequest{}, nil
}
decoded := make([]*ProcessedRequest, size)
for i := 0; i < size; i++ {
decoded[i] = &ProcessedRequest{
RequestID: ids[i][:],
Result: results[i],
Error: errors[i],
OnchainMetadata: onchainMeta[i],
CoordinatorContract: processingMeta[i],
// CallbackGasLimit is not ABI-encoded
}
}
return decoded, nil
}