forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth.go
706 lines (602 loc) · 22.8 KB
/
eth.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
package jsonrpc
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/jackc/pgx/v4"
)
// Eth contains implementations for the "eth" RPC endpoints
type Eth struct {
cfg Config
pool jsonRPCTxPool
state stateInterface
gpe gasPriceEstimator
storage storageInterface
txMan dbTxManager
}
// BlockNumber returns current block number
func (e *Eth) BlockNumber() (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
lastBlockNumber, err := e.state.GetLastL2BlockNumber(ctx, dbTx)
if err != nil {
return "0x0", newRPCError(defaultErrorCode, "failed to get the last block number from state")
}
return hex.EncodeUint64(lastBlockNumber), nil
})
}
// Call executes a new message call immediately and returns the value of
// executed contract and potential error.
// Note, this function doesn't make any changes in the state/blockchain and is
// useful to execute view/pure methods and retrieve values.
func (e *Eth) Call(arg *txnArgs, number *BlockNumber) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
// If the caller didn't supply the gas limit in the message, then we set it to maximum possible => block gas limit
if arg.Gas == nil || *arg.Gas == argUint64(0) {
header, err := e.getBlockHeader(ctx, *number, dbTx)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get block header", err)
}
gas := argUint64(header.GasLimit)
arg.Gas = &gas
}
tx := arg.ToTransaction()
blockNumber, rpcErr := number.getNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
result := e.state.ProcessUnsignedTransaction(ctx, tx, arg.From, blockNumber, dbTx)
if result.Failed() {
return rpcErrorResponse(defaultErrorCode, "failed to execute call", result.Err)
}
return argBytesPtr(result.ReturnValue), nil
})
}
// ChainId returns the chain id of the client
func (e *Eth) ChainId() (interface{}, rpcError) { //nolint:revive
return hex.EncodeUint64(ChainID), nil
}
// EstimateGas generates and returns an estimate of how much gas is necessary to
// allow the transaction to complete.
// The transaction will not be added to the blockchain.
// Note that the estimate may be significantly more than the amount of gas actually
// used by the transaction, for a variety of reasons including EVM mechanics and
// node performance.
func (e *Eth) EstimateGas(arg *txnArgs, rawNum *BlockNumber) (interface{}, rpcError) {
tx := arg.ToTransaction()
gasEstimation, err := e.state.EstimateGas(tx, arg.From)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to estimate gas", err)
}
return hex.EncodeUint64(gasEstimation), nil
}
// GasPrice returns the average gas price based on the last x blocks
func (e *Eth) GasPrice() (interface{}, rpcError) {
ctx := context.Background()
gasPrice, err := e.gpe.GetAvgGasPrice(ctx)
if err != nil {
return "0x0", nil
}
if gasPrice != nil {
return hex.EncodeUint64(gasPrice.Uint64()), nil
}
return hex.EncodeUint64(0), nil
}
// GetBalance returns the account's balance at the referenced block
func (e *Eth) GetBalance(address common.Address, number *BlockNumber) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
blockNumber, rpcErr := number.getNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
balance, err := e.state.GetBalance(ctx, address, blockNumber, dbTx)
if errors.Is(err, state.ErrNotFound) {
return hex.EncodeUint64(0), nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get balance from state", err)
}
return hex.EncodeBig(balance), nil
})
}
// GetBlockByHash returns information about a block by hash
func (e *Eth) GetBlockByHash(hash common.Hash, fullTx bool) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
block, err := e.state.GetL2BlockByHash(ctx, hash, dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get block by hash from state", err)
}
rpcBlock := l2BlockToRPCBlock(block, fullTx)
return rpcBlock, nil
})
}
// GetBlockByNumber returns information about a block by block number
func (e *Eth) GetBlockByNumber(number BlockNumber, fullTx bool) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
if number == PendingBlockNumber {
lastBlock, err := e.state.GetLastL2Block(ctx, dbTx)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "couldn't load last block from state to compute the pending block", err)
}
header := types.CopyHeader(lastBlock.Header())
header.ParentHash = lastBlock.Hash()
header.Number = big.NewInt(0).SetUint64(lastBlock.Number().Uint64() + 1)
header.TxHash = types.EmptyRootHash
header.UncleHash = types.EmptyUncleHash
block := types.NewBlockWithHeader(header)
rpcBlock := l2BlockToRPCBlock(block, fullTx)
return rpcBlock, nil
}
var err error
blockNumber, rpcErr := number.getNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
block, err := e.state.GetL2BlockByNumber(ctx, blockNumber, dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, fmt.Sprintf("couldn't load block from state by number %v", blockNumber), err)
}
rpcBlock := l2BlockToRPCBlock(block, fullTx)
return rpcBlock, nil
})
}
// GetCode returns account code at given block number
func (e *Eth) GetCode(address common.Address, number *BlockNumber) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
var err error
blockNumber, rpcErr := number.getNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
code, err := e.state.GetCode(ctx, address, blockNumber, dbTx)
if errors.Is(err, state.ErrNotFound) {
return "0x", nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get code", err)
}
return argBytes(code), nil
})
}
// GetCompilers eth_getCompilers
func (e *Eth) GetCompilers() (interface{}, rpcError) {
return []interface{}{}, nil
}
// GetFilterChanges polling method for a filter, which returns
// an array of logs which occurred since last poll.
func (e *Eth) GetFilterChanges(filterID argUint64) (interface{}, rpcError) {
filter, err := e.storage.GetFilter(uint64(filterID))
if errors.Is(err, ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get filter from storage", err)
}
switch filter.Type {
case FilterTypeBlock:
{
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
res, err := e.state.GetL2BlockHashesSince(ctx, filter.LastPoll, dbTx)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get block hashes", err)
}
rpcErr := e.updateFilterLastPoll(filter.ID)
if rpcErr != nil {
return nil, rpcErr
}
if len(res) == 0 {
return nil, nil
}
return res, nil
})
}
case FilterTypePendingTx:
{
res, err := e.pool.GetPendingTxHashesSince(context.Background(), filter.LastPoll)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get pending transaction hashes", err)
}
rpcErr := e.updateFilterLastPoll(filter.ID)
if rpcErr != nil {
return nil, rpcErr
}
if len(res) == 0 {
return nil, nil
}
return res, nil
}
case FilterTypeLog:
{
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
filterParameters := &LogFilter{}
err = json.Unmarshal([]byte(filter.Parameters), filterParameters)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to read filter parameters", err)
}
filterParameters.Since = &filter.LastPoll
resInterface, err := e.internalGetLogs(ctx, dbTx, filterParameters)
if err != nil {
return nil, err
}
rpcErr := e.updateFilterLastPoll(filter.ID)
if rpcErr != nil {
return nil, rpcErr
}
res := resInterface.([]rpcLog)
if len(res) == 0 {
return nil, nil
}
return res, nil
})
}
default:
return nil, nil
}
}
// GetFilterLogs returns an array of all logs mlocking filter
// with given id.
func (e *Eth) GetFilterLogs(filterID argUint64) (interface{}, rpcError) {
filter, err := e.storage.GetFilter(uint64(filterID))
if errors.Is(err, ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get filter from storage", err)
}
if filter.Type != FilterTypeLog {
return nil, nil
}
filterParameters := &LogFilter{}
err = json.Unmarshal([]byte(filter.Parameters), filterParameters)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to read filter parameters", err)
}
filterParameters.Since = nil
return e.GetLogs(filterParameters)
}
// GetLogs returns a list of logs accordingly to the provided filter
func (e *Eth) GetLogs(filter *LogFilter) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
return e.internalGetLogs(ctx, dbTx, filter)
})
}
func (e *Eth) internalGetLogs(ctx context.Context, dbTx pgx.Tx, filter *LogFilter) (interface{}, rpcError) {
var err error
fromBlock, rpcErr := filter.FromBlock.getNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
toBlock, rpcErr := filter.ToBlock.getNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
logs, err := e.state.GetLogs(ctx, fromBlock, toBlock, filter.Addresses, filter.Topics, filter.BlockHash, filter.Since, dbTx)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get logs from state", err)
}
result := make([]rpcLog, 0, len(logs))
for _, l := range logs {
result = append(result, logToRPCLog(*l))
}
return result, nil
}
// GetStorageAt gets the value stored for an specific address and position
func (e *Eth) GetStorageAt(address common.Address, position common.Hash, number *BlockNumber) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
var err error
blockNumber, rpcErr := number.getNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
value, err := e.state.GetStorageAt(ctx, address, position.Big(), blockNumber, dbTx)
if errors.Is(err, state.ErrNotFound) {
return argBytesPtr(common.Hash{}.Bytes()), nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get storage value from state", err)
}
return argBytesPtr(common.BigToHash(value).Bytes()), nil
})
}
// GetTransactionByBlockHashAndIndex returns information about a transaction by
// block hash and transaction index position.
func (e *Eth) GetTransactionByBlockHashAndIndex(hash common.Hash, index Index) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
tx, err := e.state.GetTransactionByL2BlockHashAndIndex(ctx, hash, uint64(index), dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get transaction", err)
}
receipt, err := e.state.GetTransactionReceipt(ctx, tx.Hash(), dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get transaction receipt", err)
}
return toRPCTransaction(tx, receipt.BlockNumber, receipt.BlockHash, uint64(receipt.TransactionIndex)), nil
})
}
// GetTransactionByBlockNumberAndIndex returns information about a transaction by
// block number and transaction index position.
func (e *Eth) GetTransactionByBlockNumberAndIndex(number *BlockNumber, index Index) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
var err error
blockNumber, rpcErr := number.getNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
tx, err := e.state.GetTransactionByL2BlockNumberAndIndex(ctx, blockNumber, uint64(index), dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get transaction", err)
}
receipt, err := e.state.GetTransactionReceipt(ctx, tx.Hash(), dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get transaction receipt", err)
}
return toRPCTransaction(tx, receipt.BlockNumber, receipt.BlockHash, uint64(receipt.TransactionIndex)), nil
})
}
// GetTransactionByHash returns a transaction by his hash
func (e *Eth) GetTransactionByHash(hash common.Hash) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
tx, err := e.state.GetTransactionByHash(ctx, hash, dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to load transaction by hash from state", err)
}
receipt, err := e.state.GetTransactionReceipt(ctx, hash, dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to load transaction receipt from state", err)
}
return toRPCTransaction(tx, receipt.BlockNumber, receipt.BlockHash, uint64(receipt.TransactionIndex)), nil
})
}
// GetTransactionCount returns account nonce
func (e *Eth) GetTransactionCount(address common.Address, number *BlockNumber) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
var err error
blockNumber, rpcErr := number.getNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
nonce, err := e.state.GetNonce(ctx, address, blockNumber, dbTx)
if errors.Is(err, state.ErrNotFound) {
return hex.EncodeUint64(0), nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to count transactions", err)
}
return hex.EncodeUint64(nonce), nil
})
}
// GetBlockTransactionCountByHash returns the number of transactions in a
// block from a block mlocking the given block hash.
func (e *Eth) GetBlockTransactionCountByHash(hash common.Hash) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
c, err := e.state.GetL2BlockTransactionCountByHash(ctx, hash, dbTx)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to count transactions", err)
}
return argUint64(c), nil
})
}
// GetBlockTransactionCountByNumber returns the number of transactions in a
// block from a block mlocking the given block number.
func (e *Eth) GetBlockTransactionCountByNumber(number *BlockNumber) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
var err error
blockNumber, rpcErr := number.getNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
c, err := e.state.GetL2BlockTransactionCountByNumber(ctx, blockNumber, dbTx)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to count transactions", err)
}
return argUint64(c), nil
})
}
// GetTransactionReceipt returns a transaction receipt by his hash
func (e *Eth) GetTransactionReceipt(hash common.Hash) (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
tx, err := e.state.GetTransactionByHash(ctx, hash, dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get tx from state", err)
}
r, err := e.state.GetTransactionReceipt(ctx, hash, dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get tx receipt from state", err)
}
receipt, err := receiptToRPCReceipt(*tx, r)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to build the receipt response", err)
}
return receipt, nil
})
}
// NewBlockFilter creates a filter in the node, to notify when
// a new block arrives. To check if the state has changed,
// call eth_getFilterChanges.
func (e *Eth) NewBlockFilter() (interface{}, rpcError) {
id, err := e.storage.NewBlockFilter()
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to create new block filter", err)
}
return argUint64(id), nil
}
// NewFilter creates a filter object, based on filter options,
// to notify when the state changes (logs). To check if the state
// has changed, call eth_getFilterChanges.
func (e *Eth) NewFilter(filter *LogFilter) (interface{}, rpcError) {
id, err := e.storage.NewLogFilter(*filter)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to create new log filter", err)
}
return argUint64(id), nil
}
// NewPendingTransactionFilter creates a filter in the node, to
// notify when new pending transactions arrive. To check if the
// state has changed, call eth_getFilterChanges.
func (e *Eth) NewPendingTransactionFilter(filterID argUint64) (interface{}, rpcError) {
id, err := e.storage.NewPendingTransactionFilter()
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to create new pending transaction filter", err)
}
return argUint64(id), nil
}
// SendRawTransaction has two different ways to handle new transactions:
// - for Sequencer nodes it tries to add the tx to the pool
// - for Non-Sequencer nodes it relays the Tx to the Sequencer node
func (e *Eth) SendRawTransaction(input string) (interface{}, rpcError) {
if e.cfg.SequencerNodeURI != "" {
return e.relayTxToSequencerNode(input)
} else {
return e.tryToAddTxToPool(input)
}
}
func (e *Eth) relayTxToSequencerNode(input string) (interface{}, rpcError) {
res, err := JSONRPCCall(e.cfg.SequencerNodeURI, "eth_sendRawTransaction", input)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to relay tx to the sequencer node", err)
}
if res.Error != nil {
return rpcErrorResponse(res.Error.Code, res.Error.Message, nil)
}
txHash := res.Result
return txHash, nil
}
func (e *Eth) tryToAddTxToPool(input string) (interface{}, rpcError) {
tx, err := hexToTx(input)
if err != nil {
return rpcErrorResponse(invalidParamsErrorCode, "invalid tx input", err)
}
log.Debugf("adding TX to the pool: %v", tx.Hash().Hex())
if err := e.pool.AddTx(context.Background(), *tx); err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to add TX to the pool", err)
}
log.Infof("TX added to the pool: %v", tx.Hash().Hex())
return tx.Hash().Hex(), nil
}
// UninstallFilter uninstalls a filter with given id. Should
// always be called when wlock is no longer needed. Additionally
// Filters timeout when they aren’t requested with
// eth_getFilterChanges for a period of time.
func (e *Eth) UninstallFilter(filterID argUint64) (interface{}, rpcError) {
uninstalled, err := e.storage.UninstallFilter(uint64(filterID))
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to uninstall filter", err)
}
return uninstalled, nil
}
// Syncing returns an object with data about the sync status or false.
// https://eth.wiki/json-rpc/API#eth_syncing
func (e *Eth) Syncing() (interface{}, rpcError) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
syncInfo, err := e.state.GetSyncingInfo(ctx, dbTx)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get syncing info from state", err)
}
if syncInfo.CurrentBlockNumber == syncInfo.LastBlockNumberSeen {
return false, nil
}
return struct {
S argUint64 `json:"startingBlock"`
C argUint64 `json:"currentBlock"`
H argUint64 `json:"highestBlock"`
}{
S: argUint64(syncInfo.InitialSyncingBlock),
C: argUint64(syncInfo.CurrentBlockNumber),
H: argUint64(syncInfo.LastBlockNumberSeen),
}, nil
})
}
// GetUncleByBlockHashAndIndex returns information about a uncle of a
// block by hash and uncle index position
func (e *Eth) GetUncleByBlockHashAndIndex() (interface{}, rpcError) {
return nil, nil
}
// GetUncleByBlockNumberAndIndex returns information about a uncle of a
// block by number and uncle index position
func (e *Eth) GetUncleByBlockNumberAndIndex() (interface{}, rpcError) {
return nil, nil
}
// GetUncleCountByBlockHash returns the number of uncles in a block
// mlocking the given block hash
func (e *Eth) GetUncleCountByBlockHash() (interface{}, rpcError) {
return "0x0", nil
}
// GetUncleCountByBlockNumber returns the number of uncles in a block
// mlocking the given block number
func (e *Eth) GetUncleCountByBlockNumber() (interface{}, rpcError) {
return "0x0", nil
}
// ProtocolVersion returns the protocol version.
func (e *Eth) ProtocolVersion() (interface{}, rpcError) {
return "0x0", nil
}
func hexToTx(str string) (*types.Transaction, error) {
tx := new(types.Transaction)
b, err := hex.DecodeHex(str)
if err != nil {
return nil, err
}
if err := tx.UnmarshalBinary(b); err != nil {
return nil, err
}
return tx, nil
}
func (e *Eth) getBlockHeader(ctx context.Context, number BlockNumber, dbTx pgx.Tx) (*types.Header, error) {
switch number {
case LatestBlockNumber:
block, err := e.state.GetLastL2Block(ctx, dbTx)
if err != nil {
return nil, err
}
return block.Header(), nil
case EarliestBlockNumber:
header, err := e.state.GetL2BlockHeaderByNumber(ctx, uint64(0), dbTx)
if err != nil {
return nil, err
}
return header, nil
case PendingBlockNumber:
lastBlock, err := e.state.GetLastL2Block(ctx, dbTx)
if err != nil {
return nil, err
}
parentHash := lastBlock.Hash()
number := lastBlock.Number().Uint64() + 1
header := &types.Header{
ParentHash: parentHash,
Number: big.NewInt(0).SetUint64(number),
Difficulty: big.NewInt(0),
GasLimit: lastBlock.Header().GasLimit,
}
return header, nil
default:
return e.state.GetL2BlockHeaderByNumber(ctx, uint64(number), dbTx)
}
}
func (e *Eth) updateFilterLastPoll(filterID uint64) rpcError {
err := e.storage.UpdateFilterLastPoll(filterID)
if err != nil {
return newRPCError(defaultErrorCode, "failed to update last time the filter changes were requested")
}
return nil
}