-
Notifications
You must be signed in to change notification settings - Fork 179
/
pubsub_api.go
506 lines (448 loc) · 13.8 KB
/
pubsub_api.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package websockets
import (
"fmt"
"sync"
"github.com/okex/exchain/libs/tendermint/libs/log"
coretypes "github.com/okex/exchain/libs/tendermint/rpc/core/types"
tmtypes "github.com/okex/exchain/libs/tendermint/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/rpc"
"github.com/okex/exchain/libs/cosmos-sdk/client/context"
rpcfilters "github.com/okex/exchain/app/rpc/namespaces/eth/filters"
rpctypes "github.com/okex/exchain/app/rpc/types"
evmtypes "github.com/okex/exchain/x/evm/types"
)
// PubSubAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec
type PubSubAPI struct {
clientCtx context.CLIContext
events *rpcfilters.EventSystem
filtersMu *sync.RWMutex
filters map[rpc.ID]*wsSubscription
logger log.Logger
}
// NewAPI creates an instance of the ethereum PubSub API.
func NewAPI(clientCtx context.CLIContext, log log.Logger) *PubSubAPI {
return &PubSubAPI{
clientCtx: clientCtx,
events: rpcfilters.NewEventSystem(clientCtx.Client),
filtersMu: new(sync.RWMutex),
filters: make(map[rpc.ID]*wsSubscription),
logger: log.With("module", "websocket-client"),
}
}
func (api *PubSubAPI) subscribe(conn *wsConn, params []interface{}) (rpc.ID, error) {
method, ok := params[0].(string)
if !ok {
return "0", fmt.Errorf("invalid parameters")
}
switch method {
case "newHeads":
// TODO: handle extra params
return api.subscribeNewHeads(conn)
case "logs":
if len(params) > 1 {
return api.subscribeLogs(conn, params[1])
}
return api.subscribeLogs(conn, nil)
case "newPendingTransactions":
return api.subscribePendingTransactions(conn)
case "syncing":
return api.subscribeSyncing(conn)
default:
return "0", fmt.Errorf("unsupported method %s", method)
}
}
func (api *PubSubAPI) unsubscribe(id rpc.ID) bool {
api.filtersMu.Lock()
defer api.filtersMu.Unlock()
if api.filters[id] == nil {
api.logger.Debug("client doesn't exist in filters", "ID", id)
return false
}
if api.filters[id].sub != nil {
api.filters[id].sub.Unsubscribe(api.events)
}
close(api.filters[id].unsubscribed)
delete(api.filters, id)
api.logger.Debug("close client channel & delete client from filters", "ID", id)
return true
}
func (api *PubSubAPI) subscribeNewHeads(conn *wsConn) (rpc.ID, error) {
sub, _, err := api.events.SubscribeNewHeads()
if err != nil {
return "", fmt.Errorf("error creating block filter: %s", err.Error())
}
unsubscribed := make(chan struct{})
api.filtersMu.Lock()
api.filters[sub.ID()] = &wsSubscription{
sub: sub,
conn: conn,
unsubscribed: unsubscribed,
}
api.filtersMu.Unlock()
go func(headersCh <-chan coretypes.ResultEvent, errCh <-chan error) {
for {
select {
case event := <-headersCh:
data, ok := event.Data.(tmtypes.EventDataNewBlockHeader)
if !ok {
api.logger.Error(fmt.Sprintf("invalid data type %T, expected EventDataTx", event.Data), "ID", sub.ID())
continue
}
headerWithBlockHash, err := rpctypes.EthHeaderWithBlockHashFromTendermint(&data.Header)
if err != nil {
api.logger.Error("failed to get header with block hash", "error", err)
continue
}
api.filtersMu.RLock()
if f, found := api.filters[sub.ID()]; found {
// write to ws conn
res := &SubscriptionNotification{
Jsonrpc: "2.0",
Method: "eth_subscription",
Params: &SubscriptionResult{
Subscription: sub.ID(),
Result: headerWithBlockHash,
},
}
err = f.conn.WriteJSON(res)
if err != nil {
api.logger.Error("failed to write header", "ID", sub.ID(), "blocknumber", headerWithBlockHash.Number, "error", err)
} else {
api.logger.Debug("successfully write header", "ID", sub.ID(), "blocknumber", headerWithBlockHash.Number)
}
}
api.filtersMu.RUnlock()
if err != nil {
api.unsubscribe(sub.ID())
}
case err := <-errCh:
if err != nil {
api.unsubscribe(sub.ID())
api.logger.Error("websocket recv error, close the conn", "ID", sub.ID(), "error", err)
}
return
case <-unsubscribed:
api.logger.Debug("NewHeads channel is closed", "ID", sub.ID())
return
}
}
}(sub.Event(), sub.Err())
return sub.ID(), nil
}
func (api *PubSubAPI) subscribeLogs(conn *wsConn, extra interface{}) (rpc.ID, error) {
crit := filters.FilterCriteria{}
if extra != nil {
params, ok := extra.(map[string]interface{})
if !ok {
return "", fmt.Errorf("invalid criteria")
}
if params["address"] != nil {
address, ok := params["address"].(string)
addresses, sok := params["address"].([]interface{})
if !ok && !sok {
return "", fmt.Errorf("invalid address; must be address or array of addresses")
}
if ok {
if !common.IsHexAddress(address) {
return "", fmt.Errorf("invalid address")
}
crit.Addresses = []common.Address{common.HexToAddress(address)}
} else if sok {
crit.Addresses = []common.Address{}
for _, addr := range addresses {
address, ok := addr.(string)
if !ok || !common.IsHexAddress(address) {
return "", fmt.Errorf("invalid address")
}
crit.Addresses = append(crit.Addresses, common.HexToAddress(address))
}
}
}
if params["topics"] != nil {
topics, ok := params["topics"].([]interface{})
if !ok {
return "", fmt.Errorf("invalid topics")
}
topicFilterLists, err := resolveTopicList(topics)
if err != nil {
return "", fmt.Errorf("invalid topics")
}
crit.Topics = topicFilterLists
}
}
sub, _, err := api.events.SubscribeLogs(crit)
if err != nil {
return rpc.ID(""), err
}
unsubscribed := make(chan struct{})
api.filtersMu.Lock()
api.filters[sub.ID()] = &wsSubscription{
sub: sub,
conn: conn,
unsubscribed: unsubscribed,
}
api.filtersMu.Unlock()
go func(ch <-chan coretypes.ResultEvent, errCh <-chan error) {
for {
select {
case event := <-ch:
go func(event coretypes.ResultEvent) {
dataTx, ok := event.Data.(tmtypes.EventDataTx)
if !ok {
api.logger.Error(fmt.Sprintf("invalid event data %T, expected EventDataTx", event.Data))
return
}
var resultData evmtypes.ResultData
resultData, err = evmtypes.DecodeResultData(dataTx.TxResult.Result.Data)
if err != nil {
api.logger.Error("failed to decode result data", "error", err)
return
}
logs := rpcfilters.FilterLogs(resultData.Logs, crit.FromBlock, crit.ToBlock, crit.Addresses, crit.Topics)
if len(logs) == 0 {
api.logger.Debug("no matched logs", "ID", sub.ID(), "txhash", resultData.TxHash)
return
}
api.filtersMu.RLock()
if f, found := api.filters[sub.ID()]; found {
// write to ws conn
res := &SubscriptionNotification{
Jsonrpc: "2.0",
Method: "eth_subscription",
Params: &SubscriptionResult{
Subscription: sub.ID(),
},
}
for _, singleLog := range logs {
res.Params.Result = singleLog
err = f.conn.WriteJSON(res)
if err != nil {
api.logger.Error("failed to write log", "ID", sub.ID(), "height", singleLog.BlockNumber, "txhash", singleLog.TxHash, "error", err)
break
}
api.logger.Debug("successfully write log", "ID", sub.ID(), "height", singleLog.BlockNumber, "txhash", singleLog.TxHash)
}
}
api.filtersMu.RUnlock()
if err != nil {
api.unsubscribe(sub.ID())
}
}(event)
case err := <-errCh:
if err != nil {
api.unsubscribe(sub.ID())
api.logger.Error("websocket recv error, close the conn", "ID", sub.ID(), "error", err)
}
return
case <-unsubscribed:
api.logger.Debug("Logs channel is closed", "ID", sub.ID())
return
}
}
}(sub.Event(), sub.Err())
return sub.ID(), nil
}
func resolveTopicList(params []interface{}) ([][]common.Hash, error) {
topicFilterLists := make([][]common.Hash, len(params))
for i, param := range params { // eg: ["0xddf252......f523b3ef", null, ["0x000000......32fea9e4", "0x000000......ab14dc5d"]]
if param == nil {
// 1.1 if the topic is null
topicFilterLists[i] = nil
} else {
// 2.1 judge if the param is the type of string or not
topicStr, ok := param.(string)
// 2.1 judge if the param is the type of string slice or not
topicSlices, sok := param.([]interface{})
if !ok && !sok {
// if both judgement are false, return invalid topics
return topicFilterLists, fmt.Errorf("invalid topics")
}
if ok {
// 2.2 This is string
// 2.3 judge the topic is a valid hex hash or not
if !IsHexHash(topicStr) {
return topicFilterLists, fmt.Errorf("invalid topics")
}
// 2.4 add this topic to topic-hash-lists
topicHash := common.HexToHash(topicStr)
topicFilterLists[i] = []common.Hash{topicHash}
} else if sok {
// 2.2 This is slice of string
topicHashes := make([]common.Hash, len(topicSlices))
for n, topicStr := range topicSlices {
//2.3 judge every topic
topicHash, ok := topicStr.(string)
if !ok || !IsHexHash(topicHash) {
return topicFilterLists, fmt.Errorf("invalid topics")
}
topicHashes[n] = common.HexToHash(topicHash)
}
// 2.4 add this topic slice to topic-hash-lists
topicFilterLists[i] = topicHashes
}
}
}
return topicFilterLists, nil
}
func IsHexHash(s string) bool {
if has0xPrefix(s) {
s = s[2:]
}
return len(s) == 2*common.HashLength && isHex(s)
}
// has0xPrefix validates str begins with '0x' or '0X'.
func has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}
// isHexCharacter returns bool of c being a valid hexadecimal.
func isHexCharacter(c byte) bool {
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
}
// isHex validates whether each byte is valid hexadecimal string.
func isHex(str string) bool {
if len(str)%2 != 0 {
return false
}
for _, c := range []byte(str) {
if !isHexCharacter(c) {
return false
}
}
return true
}
func (api *PubSubAPI) subscribePendingTransactions(conn *wsConn) (rpc.ID, error) {
sub, _, err := api.events.SubscribePendingTxs()
if err != nil {
return "", fmt.Errorf("error creating block filter: %s", err.Error())
}
unsubscribed := make(chan struct{})
api.filtersMu.Lock()
api.filters[sub.ID()] = &wsSubscription{
sub: sub,
conn: conn,
unsubscribed: unsubscribed,
}
api.filtersMu.Unlock()
go func(txsCh <-chan coretypes.ResultEvent, errCh <-chan error) {
for {
select {
case ev := <-txsCh:
data, ok := ev.Data.(tmtypes.EventDataTx)
if !ok {
api.logger.Error(fmt.Sprintf("invalid data type %T, expected EventDataTx", ev.Data), "ID", sub.ID())
continue
}
txHash := common.BytesToHash(data.Tx.Hash(data.Height))
api.filtersMu.RLock()
if f, found := api.filters[sub.ID()]; found {
// write to ws conn
res := &SubscriptionNotification{
Jsonrpc: "2.0",
Method: "eth_subscription",
Params: &SubscriptionResult{
Subscription: sub.ID(),
Result: txHash,
},
}
err = f.conn.WriteJSON(res)
if err != nil {
api.logger.Error("failed to write pending tx", "ID", sub.ID(), "error", err)
} else {
api.logger.Debug("successfully write pending tx", "ID", sub.ID(), "txhash", txHash)
}
}
api.filtersMu.RUnlock()
if err != nil {
api.unsubscribe(sub.ID())
}
case err := <-errCh:
if err != nil {
api.unsubscribe(sub.ID())
api.logger.Error("websocket recv error, close the conn", "ID", sub.ID(), "error", err)
}
return
case <-unsubscribed:
api.logger.Debug("PendingTransactions channel is closed", "ID", sub.ID())
return
}
}
}(sub.Event(), sub.Err())
return sub.ID(), nil
}
func (api *PubSubAPI) subscribeSyncing(conn *wsConn) (rpc.ID, error) {
sub, _, err := api.events.SubscribeNewHeads()
if err != nil {
return "", fmt.Errorf("error creating block filter: %s", err.Error())
}
unsubscribed := make(chan struct{})
api.filtersMu.Lock()
api.filters[sub.ID()] = &wsSubscription{
sub: sub,
conn: conn,
unsubscribed: unsubscribed,
}
api.filtersMu.Unlock()
status, err := api.clientCtx.Client.Status()
if err != nil {
return "", fmt.Errorf("error get sync status: %s", err.Error())
}
startingBlock := hexutil.Uint64(status.SyncInfo.EarliestBlockHeight)
highestBlock := hexutil.Uint64(0)
var result interface{}
go func(headersCh <-chan coretypes.ResultEvent, errCh <-chan error) {
for {
select {
case <-headersCh:
newStatus, err := api.clientCtx.Client.Status()
if err != nil {
api.logger.Error(fmt.Sprintf("error get sync status: %s", err.Error()))
continue
}
if !newStatus.SyncInfo.CatchingUp {
result = false
} else {
result = map[string]interface{}{
"startingBlock": startingBlock,
"currentBlock": hexutil.Uint64(newStatus.SyncInfo.LatestBlockHeight),
"highestBlock": highestBlock,
}
}
api.filtersMu.RLock()
if f, found := api.filters[sub.ID()]; found {
// write to ws conn
res := &SubscriptionNotification{
Jsonrpc: "2.0",
Method: "eth_subscription",
Params: &SubscriptionResult{
Subscription: sub.ID(),
Result: result,
},
}
err = f.conn.WriteJSON(res)
if err != nil {
api.logger.Error("failed to write syncing status", "ID", sub.ID(), "error", err)
} else {
api.logger.Debug("successfully write syncing status", "ID", sub.ID())
}
}
api.filtersMu.RUnlock()
if err != nil {
api.unsubscribe(sub.ID())
}
case err := <-errCh:
if err != nil {
api.unsubscribe(sub.ID())
api.logger.Error("websocket recv error, close the conn", "ID", sub.ID(), "error", err)
}
return
case <-unsubscribed:
api.logger.Debug("Syncing channel is closed", "ID", sub.ID())
return
}
}
}(sub.Event(), sub.Err())
return sub.ID(), nil
}