/
api.go
690 lines (590 loc) · 21.7 KB
/
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
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
package eth
import (
"bytes"
"context"
"crypto/sha512"
"database/sql"
"errors"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/rlp"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature/secp256k1"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/accounts"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/core"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/evm"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
"github.com/oasisprotocol/emerald-web3-gateway/conf"
"github.com/oasisprotocol/emerald-web3-gateway/indexer"
"github.com/oasisprotocol/emerald-web3-gateway/rpc/utils"
)
func estimateGasDummySigSpec() types.SignatureAddressSpec {
pk := sha512.Sum512_256([]byte("estimateGas: dummy sigspec"))
signer := secp256k1.NewSigner(pk[:])
return types.NewSignatureAddressSpecSecp256k1Eth(signer.Public().(secp256k1.PublicKey))
}
var (
ErrInternalError = errors.New("internal error")
ErrIndexOutOfRange = errors.New("index out of range")
ErrMalformedTransaction = errors.New("malformed transaction")
ErrMalformedBlockNumber = errors.New("malformed blocknumber")
ErrInvalidRequest = errors.New("invalid request")
// estimateGasSigSpec is a dummy signature spec used by the estimate gas method, as
// otherwise transactions without signature would be underestimated.
estimateGasSigSpec = estimateGasDummySigSpec()
)
const (
revertErrorPrefix = "reverted: "
)
// PublicAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
type PublicAPI struct {
ctx context.Context
client client.RuntimeClient
backend indexer.Backend
chainID uint32
Logger *logging.Logger
methodLimits *conf.MethodLimits
}
// NewPublicAPI creates an instance of the public ETH Web3 API.
func NewPublicAPI(
ctx context.Context,
client client.RuntimeClient,
logger *logging.Logger,
chainID uint32,
backend indexer.Backend,
methodLimits *conf.MethodLimits,
) *PublicAPI {
return &PublicAPI{
ctx: ctx,
client: client,
chainID: chainID,
Logger: logger,
backend: backend,
methodLimits: methodLimits,
}
}
// handleStorageError handles the internal storage errors.
func handleStorageError(logger *logging.Logger, err error) error {
if err == nil {
return nil
}
if errors.Is(err, sql.ErrNoRows) {
// By web3 spec an empty response should be returned if the queried block, transaction
// is not existing.
logger.Debug("no results found", "err", err)
return nil
}
logger.Error("internal storage error", "err", err)
return ErrInternalError
}
// roundParamFromBlockNum converts special BlockNumber values to the corresponding special round numbers.
func (api *PublicAPI) roundParamFromBlockNum(logger *logging.Logger, blockNum ethrpc.BlockNumber) (uint64, error) {
switch blockNum {
case ethrpc.PendingBlockNumber:
// Oasis does not expose a pending block. Use the latest.
return client.RoundLatest, nil
case ethrpc.LatestBlockNumber:
return client.RoundLatest, nil
case ethrpc.EarliestBlockNumber:
var earliest uint64
clrBlk, err := api.client.GetLastRetainedBlock(api.ctx)
if err != nil {
logger.Error("failed to get last retained block from client", "err", err)
return 0, ErrInternalError
}
ilrRound, err := api.backend.QueryLastRetainedRound()
if err != nil {
logger.Error("failed to get last retained block from indexer", "err", err)
return 0, ErrInternalError
}
if clrBlk.Header.Round < ilrRound {
earliest = ilrRound
} else {
earliest = clrBlk.Header.Round
}
return earliest, nil
default:
if int64(blockNum) < 0 {
logger.Debug("malformed block number", "block_number", blockNum)
return 0, ErrMalformedBlockNumber
}
return uint64(blockNum), nil
}
}
// GetBlockByNumber returns the block identified by number.
func (api *PublicAPI) GetBlockByNumber(blockNum ethrpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {
logger := api.Logger.With("method", "eth_getBlockByNumber", "block_number", blockNum, "full_tx", fullTx)
logger.Debug("request")
round, err := api.roundParamFromBlockNum(logger, blockNum)
if err != nil {
return nil, err
}
blk, err := api.backend.GetBlockByRound(round)
if err != nil {
return nil, handleStorageError(logger, err)
}
return utils.ConvertToEthBlock(blk, fullTx), nil
}
// GetBlockTransactionCountByNumber returns the number of transactions in the block.
func (api *PublicAPI) GetBlockTransactionCountByNumber(blockNum ethrpc.BlockNumber) (hexutil.Uint, error) {
logger := api.Logger.With("method", "eth_getBlockTransactionCountByNumber", "block_number", blockNum)
logger.Debug("request")
round, err := api.roundParamFromBlockNum(logger, blockNum)
if err != nil {
return 0, err
}
n, err := api.backend.GetBlockTransactionCountByRound(round)
if err != nil {
return 0, handleStorageError(logger, err)
}
return hexutil.Uint(n), nil
}
func (api *PublicAPI) GetStorageAt(address common.Address, position hexutil.Big, blockNrOrHash ethrpc.BlockNumberOrHash) (hexutil.Big, error) {
logger := api.Logger.With("method", "eth_getStorageAt", "address", address, "position", position, "block_or_hash", blockNrOrHash)
logger.Debug("request")
round, err := api.getBlockRound(logger, blockNrOrHash)
if err != nil {
return hexutil.Big{}, err
}
// EVM module takes index as H256, which needs leading zeros.
position256 := make([]byte, 32)
// Unmarshalling to hexutil.Big rejects overlong inputs. Verify in `TestRejectOverlong`.
position.ToInt().FillBytes(position256)
ethmod := evm.NewV1(api.client)
res, err := ethmod.Storage(api.ctx, round, address[:], position256)
if err != nil {
logger.Error("failed to query storage", "err", err)
return hexutil.Big{}, ErrInternalError
}
// Some apps expect no leading zeros, so output as big integer.
var resultBI big.Int
resultBI.SetBytes(res)
return hexutil.Big(resultBI), nil
}
// GetBalance returns the provided account's balance up to the provided block number.
func (api *PublicAPI) GetBalance(address common.Address, blockNrOrHash ethrpc.BlockNumberOrHash) (*hexutil.Big, error) {
logger := api.Logger.With("method", "eth_getBalance", "address", address, "block_or_hash", blockNrOrHash)
logger.Debug("request")
ethmod := evm.NewV1(api.client)
round, err := api.getBlockRound(logger, blockNrOrHash)
if err != nil {
return nil, err
}
res, err := ethmod.Balance(api.ctx, round, address[:])
if err != nil {
logger.Error("ethmod.Balance failed", "round", round, "err", err)
return nil, ErrInternalError
}
return (*hexutil.Big)(res.ToBigInt()), nil
}
// nolint:revive,stylecheck
// ChainId return the EIP-155 chain id for the current network.
func (api *PublicAPI) ChainId() (*hexutil.Big, error) {
logger := api.Logger.With("method", "eth_chainId")
logger.Debug("request")
return (*hexutil.Big)(big.NewInt(int64(api.chainID))), nil
}
// GasPrice returns a suggestion for a gas price for legacy transactions.
func (api *PublicAPI) GasPrice() (*hexutil.Big, error) {
logger := api.Logger.With("method", "eth_gasPrice")
logger.Debug("request")
coremod := core.NewV1(api.client)
mgp, err := coremod.MinGasPrice(api.ctx)
if err != nil {
logger.Error("core.MinGasPrice failed", "err", err)
return nil, ErrInternalError
}
nativeMGP := mgp[types.NativeDenomination]
return (*hexutil.Big)(nativeMGP.ToBigInt()), nil
}
// GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash.
func (api *PublicAPI) GetBlockTransactionCountByHash(blockHash common.Hash) (hexutil.Uint, error) {
logger := api.Logger.With("method", "eth_getBlockTransactionCountByHash", "block_hash", blockHash.Hex())
logger.Debug("request")
n, err := api.backend.GetBlockTransactionCountByHash(blockHash)
if err != nil {
return 0, handleStorageError(logger, err)
}
return hexutil.Uint(n), nil
}
// GetTransactionCount returns the number of transactions the given address has sent for the given block number.
func (api *PublicAPI) GetTransactionCount(ethAddr common.Address, blockNrOrHash ethrpc.BlockNumberOrHash) (*hexutil.Uint64, error) {
logger := api.Logger.With("method", "eth_getBlockTransactionCount", "address", ethAddr, "block_or_hash", blockNrOrHash)
logger.Debug("request")
accountsMod := accounts.NewV1(api.client)
accountsAddr := types.NewAddressRaw(types.AddressV0Secp256k1EthContext, ethAddr[:])
round, err := api.getBlockRound(logger, blockNrOrHash)
if err != nil {
return nil, err
}
nonce, err := accountsMod.Nonce(api.ctx, round, accountsAddr)
if err != nil {
logger.Error("accounts.Nonce failed", "err", err)
return nil, ErrInternalError
}
return (*hexutil.Uint64)(&nonce), nil
}
// GetCode returns the contract code at the given address and block number.
func (api *PublicAPI) GetCode(address common.Address, blockNrOrHash ethrpc.BlockNumberOrHash) (hexutil.Bytes, error) {
logger := api.Logger.With("method", "eth_getCode", "address", address, "block_or_hash", blockNrOrHash)
logger.Debug("request")
ethmod := evm.NewV1(api.client)
round, err := api.getBlockRound(logger, blockNrOrHash)
if err != nil {
return nil, err
}
res, err := ethmod.Code(api.ctx, round, address[:])
if err != nil {
logger.Error("ethmod.Code failed", "err", err)
return nil, err
}
return res, nil
}
type RevertError struct {
error
Reason string `json:"reason"`
}
// ErrorData returns the ABI encoded error reason.
func (e *RevertError) ErrorData() interface{} {
return e.Reason
}
// NewRevertError returns an revert error with ABI encoded revert reason.
func (api *PublicAPI) NewRevertError(revertErr error) *RevertError {
// ABI encoded function.
abiReason := []byte{0x08, 0xc3, 0x79, 0xa0} // Keccak256("Error(string)")
// ABI encode the revert Reason string.
revertReason := strings.TrimPrefix(revertErr.Error(), revertErrorPrefix)
typ, _ := abi.NewType("string", "", nil)
unpacked, err := (abi.Arguments{{Type: typ}}).Pack(revertReason)
if err != nil {
api.Logger.Error("failed to encode revert error", "revert_reason", revertReason, "err", err)
return &RevertError{
error: revertErr,
}
}
abiReason = append(abiReason, unpacked...)
return &RevertError{
error: revertErr,
Reason: hexutil.Encode(abiReason),
}
}
// Call executes the given transaction on the state for the given block number.
// This function doesn't make any changes in the evm state of blockchain.
func (api *PublicAPI) Call(args utils.TransactionArgs, blockNrOrHash ethrpc.BlockNumberOrHash, _ *utils.StateOverride) (hexutil.Bytes, error) {
logger := api.Logger.With("method", "eth_call", "block_or_hash", blockNrOrHash)
logger.Debug("request", "args", args)
var (
amount = []byte{0}
input = []byte{}
sender = common.Address{1}
gasPrice = []byte{1}
// This gas cap should be enough for SimulateCall an ethereum transaction
gas uint64 = 30_000_000
)
round, err := api.getBlockRound(logger, blockNrOrHash)
if err != nil {
return nil, err
}
if args.To == nil {
return []byte{}, errors.New("to address not specified")
}
if args.GasPrice != nil {
gasPrice = args.GasPrice.ToInt().Bytes()
}
if args.Gas != nil {
gas = uint64(*args.Gas)
}
if args.Value != nil {
amount = args.Value.ToInt().Bytes()
}
if args.Data != nil {
input = *args.Data
}
if args.From != nil {
sender = *args.From
}
res, err := evm.NewV1(api.client).SimulateCall(
api.ctx,
round,
gasPrice,
gas,
sender.Bytes(),
args.To.Bytes(),
amount,
input,
)
if err != nil {
if strings.HasPrefix(err.Error(), revertErrorPrefix) {
revertErr := api.NewRevertError(err)
logger.Debug("failed to execute SimulateCall, reverted", "err", err, "reason", revertErr.Reason)
return nil, revertErr
}
logger.Debug("failed to execute SimulateCall", "err", err)
return nil, err
}
logger.Debug("response", "args", args, "resp", res)
return res, nil
}
// SendRawTransaction send a raw Ethereum transaction.
func (api *PublicAPI) SendRawTransaction(data hexutil.Bytes) (common.Hash, error) {
logger := api.Logger.With("method", "eth_sendRawTransaction")
logger.Debug("request", "length", len(data))
// Decode the Ethereum transaction.
ethTx := ðtypes.Transaction{}
if err := rlp.DecodeBytes(data, ethTx); err != nil {
logger.Debug("failed to decode raw transaction data", "err", err)
return common.Hash{}, ErrMalformedTransaction
}
// Generate an Ethereum transaction that is handled by the EVM module.
utx := types.UnverifiedTransaction{
Body: data,
AuthProofs: []types.AuthProof{
{Module: "evm.ethereum.v0"},
},
}
err := api.client.SubmitTxNoWait(api.ctx, &utx)
if err != nil {
logger.Debug("failed to submit transaction", "err", err)
return ethTx.Hash(), err
}
return ethTx.Hash(), nil
}
// EstimateGas returns an estimate of gas usage for the given transaction .
func (api *PublicAPI) EstimateGas(args utils.TransactionArgs, blockNum *ethrpc.BlockNumber) (hexutil.Uint64, error) {
logger := api.Logger.With("method", "eth_estimateGas", "block_number", blockNum)
logger.Debug("request", "args", args)
if args.From == nil {
// This may make sense if from not specified
args.From = &common.Address{}
}
if args.Value == nil {
args.Value = (*hexutil.Big)(big.NewInt(0))
}
if args.Data == nil {
args.Data = (*hexutil.Bytes)(&[]byte{})
}
ethTxValue := args.Value.ToInt().Bytes()
ethTxData := args.Data
var tx *types.Transaction
round := client.RoundLatest
if blockNum != nil {
var err error
round, err = api.roundParamFromBlockNum(logger, *blockNum)
if err != nil {
return 0, err
}
}
if args.To == nil {
// evm.create
tx = evm.NewV1(api.client).Create(ethTxValue, *ethTxData).AppendAuthSignature(estimateGasSigSpec, 0).GetTransaction()
} else {
// evm.call
tx = evm.NewV1(api.client).Call(args.To.Bytes(), ethTxValue, *ethTxData).AppendAuthSignature(estimateGasSigSpec, 0).GetTransaction()
}
var ethAddress [20]byte
copy(ethAddress[:], args.From[:])
gas, err := core.NewV1(api.client).EstimateGasForCaller(api.ctx, round, types.CallerAddress{EthAddress: ðAddress}, tx)
if err != nil {
logger.Debug("failed", "err", err)
return 0, err
}
logger.Debug("result", "gas", gas)
return hexutil.Uint64(gas), nil
}
// GetBlockByHash returns the block identified by hash.
func (api *PublicAPI) GetBlockByHash(blockHash common.Hash, fullTx bool) (map[string]interface{}, error) {
logger := api.Logger.With("method", "eth_getBlockByHash", "block_hash", blockHash, "full_tx", fullTx)
logger.Debug("request")
blk, err := api.backend.GetBlockByHash(blockHash)
if err != nil {
return nil, handleStorageError(logger, err)
}
return utils.ConvertToEthBlock(blk, fullTx), nil
}
// GetTransactionByHash returns the transaction identified by hash.
func (api *PublicAPI) GetTransactionByHash(hash common.Hash) (*utils.RPCTransaction, error) {
logger := api.Logger.With("method", "eth_getTransactionByHash", "hash", hash)
logger.Debug("request")
dbTx, err := api.backend.QueryTransaction(hash)
if err != nil {
return nil, handleStorageError(logger, err)
}
return utils.NewRPCTransaction(dbTx), nil
}
// GetTransactionByBlockHashAndIndex returns the transaction for the given block hash and index.
func (api *PublicAPI) GetTransactionByBlockHashAndIndex(blockHash common.Hash, index hexutil.Uint) (*utils.RPCTransaction, error) {
logger := api.Logger.With("method", "eth_getTransactionByBlockHashAndIndex", "block_hash", blockHash, "index", index)
logger.Debug("request")
dbBlock, err := api.backend.GetBlockByHash(blockHash)
if err != nil {
return nil, handleStorageError(logger, err)
}
if l := uint(len(dbBlock.Transactions)); l <= uint(index) {
logger.Debug("invalid block transaction index", "num_txs", l)
return nil, ErrIndexOutOfRange
}
return utils.NewRPCTransaction(dbBlock.Transactions[index]), nil
}
// GetTransactionByBlockNumberAndIndex returns the transaction identified by number and index.
func (api *PublicAPI) GetTransactionByBlockNumberAndIndex(blockNum ethrpc.BlockNumber, index hexutil.Uint) (*utils.RPCTransaction, error) {
logger := api.Logger.With("method", "eth_getTransactionByNumberAndIndex", "block_number", blockNum, "index", index)
logger.Debug("request")
round, err := api.roundParamFromBlockNum(logger, blockNum)
if err != nil {
return nil, err
}
blockHash, err := api.backend.QueryBlockHash(round)
if err != nil {
return nil, handleStorageError(logger, err)
}
return api.GetTransactionByBlockHashAndIndex(blockHash, index)
}
// GetTransactionReceipt returns the transaction receipt by hash.
func (api *PublicAPI) GetTransactionReceipt(txHash common.Hash) (map[string]interface{}, error) {
logger := api.Logger.With("method", "eth_getTransactionReceipt", "hash", txHash)
logger.Debug("request")
receipt, err := api.backend.GetTransactionReceipt(txHash)
if err != nil {
return nil, handleStorageError(logger, err)
}
return receipt, nil
}
// getStartEndRounds is a helper for fetching start and end rounds parameters.
func (api *PublicAPI) getStartEndRounds(logger *logging.Logger, filter filters.FilterCriteria) (uint64, uint64, error) {
if filter.BlockHash != nil {
round, err := api.backend.QueryBlockRound(*filter.BlockHash)
if err != nil {
return 0, 0, fmt.Errorf("query block round: %w", err)
}
return round, round, nil
}
start := client.RoundLatest
end := client.RoundLatest
if filter.FromBlock != nil {
round, err := api.roundParamFromBlockNum(logger, ethrpc.BlockNumber(filter.FromBlock.Int64()))
if err != nil {
return 0, 0, err
}
start = round
}
if filter.ToBlock != nil {
round, err := api.roundParamFromBlockNum(logger, ethrpc.BlockNumber(filter.ToBlock.Int64()))
if err != nil {
return 0, 0, err
}
end = round
}
return start, end, nil
}
// GetLogs returns the ethereum logs.
func (api *PublicAPI) GetLogs(filter filters.FilterCriteria) ([]*ethtypes.Log, error) {
logger := api.Logger.With("method", "eth_getLogs")
logger.Debug("request", "filter", filter)
startRoundInclusive, endRoundInclusive, err := api.getStartEndRounds(logger, filter)
if err != nil {
return nil, fmt.Errorf("error getting start and end rounds: %w", err)
}
if endRoundInclusive < startRoundInclusive {
return nil, fmt.Errorf("%w: end round greater than start round", ErrInvalidRequest)
}
if limit := api.methodLimits.GetLogsMaxRounds; limit != 0 && endRoundInclusive-startRoundInclusive > limit {
return nil, fmt.Errorf("%w: max allowed of rounds in logs query is: %d", ErrInvalidRequest, limit)
}
ethLogs := []*ethtypes.Log{}
dbLogs, err := api.backend.GetLogs(startRoundInclusive, endRoundInclusive)
if err != nil {
logger.Error("failed to get logs", "err", err)
return ethLogs, ErrInternalError
}
ethLogs = utils.DB2EthLogs(dbLogs)
// Early return if no further filtering.
if len(filter.Addresses) == 0 && len(filter.Topics) == 0 {
logger.Debug("response", "rsp", ethLogs)
return ethLogs, nil
}
filtered := make([]*ethtypes.Log, 0, len(ethLogs))
for _, log := range ethLogs {
// Filter by address.
addressMatch := len(filter.Addresses) == 0
for _, addr := range filter.Addresses {
if bytes.Equal(addr[:], log.Address[:]) {
addressMatch = true
break
}
}
if !addressMatch {
continue
}
// Filter by topics.
if !utils.TopicsMatch(log, filter.Topics) {
continue
}
// Log matched all filters.
filtered = append(filtered, log)
}
logger.Debug("response", "rsp", filtered, "all_logs", ethLogs)
return filtered, nil
}
// GetBlockHash returns the block hash by the given number.
func (api *PublicAPI) GetBlockHash(blockNum ethrpc.BlockNumber, _ bool) (common.Hash, error) {
logger := api.Logger.With("method", "eth_getBlockHash", "block_num", blockNum)
logger.Debug("request")
round, err := api.roundParamFromBlockNum(logger, blockNum)
if err != nil {
return [32]byte{}, err
}
return api.backend.QueryBlockHash(round)
}
// BlockNumber returns the latest block number.
func (api *PublicAPI) BlockNumber() (hexutil.Uint64, error) {
logger := api.Logger.With("method", "eth_getBlockNumber")
logger.Debug("request")
blockNumber, err := api.backend.BlockNumber()
if err != nil {
logger.Error("getting latest block number failed", "err", err)
return 0, ErrInternalError
}
logger.Debug("response", "blockNumber", blockNumber)
return hexutil.Uint64(blockNumber), nil
}
// Accounts returns the list of accounts available to this node.
func (api *PublicAPI) Accounts() ([]common.Address, error) {
logger := api.Logger.With("method", "eth_getAccounts")
logger.Debug("request")
addresses := make([]common.Address, 0)
return addresses, nil
}
// Mining returns whether or not this node is currently mining. Always false.
func (api *PublicAPI) Mining() bool {
logger := api.Logger.With("method", "eth_mining")
logger.Debug("request")
return false
}
// Hashrate returns the current node's hashrate. Always 0.
func (api *PublicAPI) Hashrate() hexutil.Uint64 {
logger := api.Logger.With("method", "eth_hashrate")
logger.Debug("request")
return 0
}
// getBlockRound returns the block round from BlockNumberOrHash.
func (api *PublicAPI) getBlockRound(logger *logging.Logger, blockNrOrHash ethrpc.BlockNumberOrHash) (uint64, error) {
switch {
// case if block number and blockhash is specified are handling by the BlockNumberOrHash type.
case blockNrOrHash.BlockHash == nil && blockNrOrHash.BlockNumber == nil:
return 0, fmt.Errorf("types BlockHash and BlockNumber cannot be both nil")
case blockNrOrHash.BlockHash != nil:
return api.backend.QueryBlockRound(*blockNrOrHash.BlockHash)
case blockNrOrHash.BlockNumber != nil:
return api.roundParamFromBlockNum(logger, *blockNrOrHash.BlockNumber)
default:
return 0, nil
}
}