-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.go
294 lines (261 loc) · 7.62 KB
/
bundle.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package ethclient
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/VictoriaMetrics/fastcache"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common/hexutil"
)
const (
CacheBlockPrefix = "block:"
CacheReceiptPrefix = "receipt:"
CacheTracePrefix = "trace:"
)
// CallContext dispatches the bundle method to the unifra bundle API or the standard method to the underlying RPC client.
func (ec *Client) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
if isBundleMethod(method) && isBundleArgs(args...) {
// Handle Bundle method
cacheKey := getCacheKey(method, args...)
cacheValue := ec.bundleCache.GetBig(nil, []byte(cacheKey))
if cacheValue != nil {
// Return value from cache
return decodeResult(cacheValue, result)
} else {
// Fetch data from Bundle API
url, err := getBundleURL(ec.apiEndpoint, method, args...)
if err != nil {
return err
}
/*
May be, in the feature, there is a design that some account did not need
authorization. So we should not return error here,just put a empty authrization key
and let backend service to decide error or not.
*/
apikey := ""
paths := strings.Split(ec.apiEndpoint, "/")
if len(paths) >= 2 {
apikey = paths[len(paths)-1]
}
data, err := fetchBundleDataWithContext(ctx, url, apikey)
if err != nil {
return err
}
// Parse data as array of JSON objects and store each object in the cache, through the blockNumber cache key
matched, err := cacheData(ec.bundleCache, method, cacheKey, data)
if err != nil {
return err
}
// Return matched value
return decodeResult(*matched, result)
}
} else {
// Handle standard method using underlying RPC client
return ec.c.CallContext(ctx, result, method, args...)
}
}
func isBundleMethod(method string) bool {
switch method {
case "eth_getBlockByNumber", "eth_getBlockReceipts", "trace_block":
return true
default:
return false
}
}
func isBundleArgs(args ...interface{}) bool {
if len(args) == 0 {
return false
}
// only hex and number are supported
if args[0] == nil {
return false
}
switch args[0].(type) {
case string:
// hex
if strings.HasPrefix(args[0].(string), "0x") {
return true
}
// number
if _, err := strconv.Atoi(args[0].(string)); err == nil {
return true
}
return false
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return true
default:
return false
}
}
// getCacheKey returns the cache key for the given method and arguments.
// Format: block:<blockNumber>, receipt:<blockNumber>, trace:<blockNumber>
func getCacheKey(method string, args ...interface{}) string {
switch method {
case "eth_getBlockByNumber":
return CacheBlockPrefix + args[0].(string)
case "eth_getBlockReceipts":
return CacheReceiptPrefix + args[0].(string)
case "trace_block":
return CacheTracePrefix + args[0].(string)
default:
return ""
}
}
// decodeResult decodes the given data into the given result.
func decodeResult(data []byte, result interface{}) error {
return json.Unmarshal(data, result)
}
// getBundleURL returns the URL for the given bundle method and arguments.
func getBundleURL(apiEndpoint string, method string, args ...interface{}) (string, error) {
if apiEndpoint == "" {
return "", errors.New("getBundleURL: apiEndpoint is empty")
}
// handle / at the end of the apiEndpoint
if apiEndpoint[len(apiEndpoint)-1] == '/' {
apiEndpoint = apiEndpoint[:len(apiEndpoint)-1]
}
baseUrl := fmt.Sprintf("%s/bundle-api", apiEndpoint)
switch method {
case "eth_getBlockByNumber":
blockNumber := hexutil.MustDecodeUint64(args[0].(string))
return fmt.Sprintf("%s/blocks/%d", baseUrl, blockNumber), nil
case "eth_getBlockReceipts":
blockNumber := hexutil.MustDecodeUint64(args[0].(string))
return fmt.Sprintf("%s/receipts/%d", baseUrl, blockNumber), nil
case "trace_block":
blockNumber := hexutil.MustDecodeUint64(args[0].(string))
return fmt.Sprintf("%s/traces/%d", baseUrl, blockNumber), nil
default:
return "", errors.New("getBundleURL: unsupported method")
}
}
type metadata struct {
Type string `json:"type,omitempty"`
Range string `json:"range,omitempty"`
DownloadLink string `json:"download_link,omitempty"`
Error string `json:"error,omitempty"`
}
func fetchBundleDataWithContext(ctx context.Context, url, apikey string) ([]byte, error) {
// Fetch metadata
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
return nil, ethereum.NotFound
}
}
var meta metadata
if err := json.NewDecoder(resp.Body).Decode(&meta); err != nil {
return nil, err
}
if meta.Error != "" {
return nil, errors.New(meta.Error)
}
// Fetch bundle data and uncompress
downloadReq, err := http.NewRequestWithContext(ctx, "GET", meta.DownloadLink, nil)
if err != nil {
return nil, err
}
downloadReq.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apikey))
resp, err = http.DefaultClient.Do(downloadReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
return nil, ethereum.NotFound
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, errors.New(string(data))
}
r, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
defer r.Close()
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
return data, nil
}
func cacheData(cache *fastcache.Cache, method string, cacheKey string, data []byte) (*[]byte, error) {
var matched *[]byte
var blockInfosList []interface{}
switch method {
case "trace_block", "eth_getBlockReceipts":
if err := json.Unmarshal(data, &blockInfosList); err != nil {
return nil, err
}
case "eth_getBlockByNumber":
if err := json.Unmarshal(data, &blockInfosList); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("invalid bundle type: %s", method)
}
var buf bytes.Buffer
for _, blockInfos := range blockInfosList {
var (
blockNumberHex string
k string
)
if method == "trace_block" {
binfos := blockInfos.([]interface{})
if len(binfos) == 0 {
continue
}
bN, ok := binfos[0].(map[string]interface{})["blockNumber"]
if !ok {
return nil, fmt.Errorf("failed to parse block number, blockInfos: %v", blockInfos)
}
blockNumberHex = hexutil.EncodeUint64(uint64(bN.(float64)))
k = fmt.Sprintf("%s%s", CacheTracePrefix, blockNumberHex)
} else if method == "eth_getBlockReceipts" {
binfos := blockInfos.([]interface{})
if len(binfos) == 0 {
continue
}
blockNumberHex, ok := binfos[0].(map[string]interface{})["blockNumber"].(string)
if !ok {
return nil, fmt.Errorf("failed to parse block number, blockInfos: %v", blockInfos)
}
k = CacheReceiptPrefix + blockNumberHex
} else if method == "eth_getBlockByNumber" {
blockNumberHex = blockInfos.(map[string]interface{})["number"].(string)
k = CacheBlockPrefix + blockNumberHex
} else {
return nil, fmt.Errorf("invalid bundle method: %s", method)
}
buf.Reset()
if err := json.NewEncoder(&buf).Encode(blockInfos); err != nil {
return nil, err
}
cacheDataBytes := buf.Bytes()
cache.SetBig([]byte(k), cacheDataBytes)
if k == cacheKey {
matched = new([]byte)
*matched = make([]byte, len(cacheDataBytes))
copy(*matched, cacheDataBytes)
}
}
return matched, nil
}