-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathEthRpcModule.cs
749 lines (645 loc) · 32.3 KB
/
EthRpcModule.cs
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DotNetty.Buffers;
using Nethermind.Blockchain.Filters;
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.Receipts;
using Nethermind.Core;
using Nethermind.Core.Collections;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Evm;
using Nethermind.Facade;
using Nethermind.Facade.Eth;
using Nethermind.Facade.Filters;
using Nethermind.Facade.Proxy.Models.Simulate;
using Nethermind.Int256;
using Nethermind.JsonRpc.Data;
using Nethermind.JsonRpc.Modules.Eth.FeeHistory;
using Nethermind.JsonRpc.Modules.Eth.GasPrice;
using Nethermind.Logging;
using Nethermind.Network.P2P;
using Nethermind.Serialization.Rlp;
using Nethermind.State;
using Nethermind.State.Proofs;
using Nethermind.Synchronization.ParallelSync;
using Nethermind.TxPool;
using Nethermind.Wallet;
using Block = Nethermind.Core.Block;
using BlockHeader = Nethermind.Core.BlockHeader;
using Signature = Nethermind.Core.Crypto.Signature;
using Transaction = Nethermind.Core.Transaction;
namespace Nethermind.JsonRpc.Modules.Eth;
public partial class EthRpcModule(
IJsonRpcConfig rpcConfig,
IBlockchainBridge blockchainBridge,
IBlockFinder blockFinder,
IReceiptFinder receiptFinder,
IStateReader stateReader,
ITxPool txPool,
ITxSender txSender,
IWallet wallet,
ILogManager logManager,
ISpecProvider specProvider,
IGasPriceOracle gasPriceOracle,
IEthSyncingInfo ethSyncingInfo,
IFeeHistoryOracle feeHistoryOracle,
ulong? secondsPerSlot) : IEthRpcModule
{
protected readonly Encoding _messageEncoding = Encoding.UTF8;
protected readonly IJsonRpcConfig _rpcConfig = rpcConfig ?? throw new ArgumentNullException(nameof(rpcConfig));
protected readonly IBlockchainBridge _blockchainBridge = blockchainBridge ?? throw new ArgumentNullException(nameof(blockchainBridge));
protected readonly IBlockFinder _blockFinder = blockFinder ?? throw new ArgumentNullException(nameof(blockFinder));
protected readonly IReceiptFinder _receiptFinder = receiptFinder ?? throw new ArgumentNullException(nameof(receiptFinder));
protected readonly IStateReader _stateReader = stateReader ?? throw new ArgumentNullException(nameof(stateReader));
protected readonly ITxPool _txPool = txPool ?? throw new ArgumentNullException(nameof(txPool));
protected readonly ITxSender _txSender = txSender ?? throw new ArgumentNullException(nameof(txSender));
protected readonly IWallet _wallet = wallet ?? throw new ArgumentNullException(nameof(wallet));
protected readonly ISpecProvider _specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
protected readonly ILogger _logger = logManager.GetClassLogger();
protected readonly IGasPriceOracle _gasPriceOracle = gasPriceOracle ?? throw new ArgumentNullException(nameof(gasPriceOracle));
protected readonly IEthSyncingInfo _ethSyncingInfo = ethSyncingInfo ?? throw new ArgumentNullException(nameof(ethSyncingInfo));
protected readonly IFeeHistoryOracle _feeHistoryOracle = feeHistoryOracle ?? throw new ArgumentNullException(nameof(feeHistoryOracle));
protected readonly ulong _secondsPerSlot = secondsPerSlot ?? throw new ArgumentNullException(nameof(secondsPerSlot));
private static bool HasStateForBlock(IBlockchainBridge blockchainBridge, BlockHeader header)
{
return blockchainBridge.HasStateForRoot(header.StateRoot!);
}
public ResultWrapper<string> eth_protocolVersion()
{
int highestVersion = P2PProtocolInfoProvider.GetHighestVersionOfEthProtocol();
return ResultWrapper<string>.Success(highestVersion.ToHexString());
}
public ResultWrapper<SyncingResult> eth_syncing()
{
return ResultWrapper<SyncingResult>.Success(_ethSyncingInfo.GetFullInfo());
}
public ResultWrapper<byte[]> eth_snapshot()
{
return ResultWrapper<byte[]>.Fail("eth_snapshot not supported");
}
public ResultWrapper<Address> eth_coinbase()
{
return ResultWrapper<Address>.Success(Address.Zero);
}
public ResultWrapper<UInt256?> eth_gasPrice()
{
return ResultWrapper<UInt256?>.Success(_gasPriceOracle.GetGasPriceEstimate());
}
public ResultWrapper<UInt256?> eth_blobBaseFee()
{
if (_blockFinder.Head?.Header?.ExcessBlobGas is null)
{
return ResultWrapper<UInt256?>.Success(UInt256.Zero);
}
if (!BlobGasCalculator.TryCalculateBlobGasPricePerUnit(_blockFinder.Head?.Header?.ExcessBlobGas ?? 0,
out UInt256 blobGasPricePerUnit))
{
return ResultWrapper<UInt256?>.Fail("Unable to calculate the current blob base fee");
}
return ResultWrapper<UInt256?>.Success(blobGasPricePerUnit);
}
public ResultWrapper<UInt256?> eth_maxPriorityFeePerGas()
{
UInt256 gasPriceWithBaseFee = _gasPriceOracle.GetMaxPriorityGasFeeEstimate();
return ResultWrapper<UInt256?>.Success(gasPriceWithBaseFee);
}
public ResultWrapper<FeeHistoryResults> eth_feeHistory(int blockCount, BlockParameter newestBlock, double[]? rewardPercentiles = null)
{
return _feeHistoryOracle.GetFeeHistory(blockCount, newestBlock, rewardPercentiles);
}
public ResultWrapper<IEnumerable<Address>> eth_accounts()
{
try
{
Address[] result = _wallet.GetAccounts();
Address[] data = result.ToArray();
return ResultWrapper<IEnumerable<Address>>.Success(data.ToArray());
}
catch (Exception)
{
return ResultWrapper<IEnumerable<Address>>.Fail("Error while getting key addresses from wallet.");
}
}
public Task<ResultWrapper<long?>> eth_blockNumber()
{
long number = _blockchainBridge.HeadBlock?.Number ?? 0;
return Task.FromResult(ResultWrapper<long?>.Success(number));
}
public Task<ResultWrapper<UInt256?>> eth_getBalance(Address address, BlockParameter? blockParameter = null)
{
SearchResult<BlockHeader> searchResult = _blockFinder.SearchForHeader(blockParameter);
if (searchResult.IsError)
{
return Task.FromResult(GetFailureResult<UInt256?, BlockHeader>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedHeadersYet()));
}
BlockHeader header = searchResult.Object;
if (!HasStateForBlock(_blockchainBridge, header!))
{
return Task.FromResult(GetStateFailureResult<UInt256?>(header));
}
_stateReader.TryGetAccount(header!.StateRoot!, address, out AccountStruct account);
return Task.FromResult(ResultWrapper<UInt256?>.Success(account.Balance));
}
public ResultWrapper<byte[]> eth_getStorageAt(Address address, UInt256 positionIndex,
BlockParameter? blockParameter = null)
{
SearchResult<BlockHeader> searchResult = _blockFinder.SearchForHeader(blockParameter);
if (searchResult.IsError)
{
return GetFailureResult<byte[], BlockHeader>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedHeadersYet());
}
BlockHeader? header = searchResult.Object;
ReadOnlySpan<byte> storage = _stateReader.GetStorage(header!.StateRoot!, address, positionIndex);
return ResultWrapper<byte[]>.Success(storage.IsEmpty ? Bytes32.Zero.Unwrap() : storage!.PadLeft(32));
}
public Task<ResultWrapper<UInt256>> eth_getTransactionCount(Address address, BlockParameter? blockParameter)
{
if (blockParameter == BlockParameter.Pending)
{
UInt256 pendingNonce = _txPool.GetLatestPendingNonce(address);
return Task.FromResult(ResultWrapper<UInt256>.Success(pendingNonce));
}
SearchResult<BlockHeader> searchResult = _blockFinder.SearchForHeader(blockParameter);
if (searchResult.IsError)
{
return Task.FromResult(GetFailureResult<UInt256, BlockHeader>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedHeadersYet()));
}
BlockHeader header = searchResult.Object;
if (!HasStateForBlock(_blockchainBridge, header!))
{
return Task.FromResult(GetStateFailureResult<UInt256>(header));
}
_stateReader.TryGetAccount(header!.StateRoot!, address, out AccountStruct account);
return Task.FromResult(ResultWrapper<UInt256>.Success(account.Nonce));
}
public ResultWrapper<UInt256?> eth_getBlockTransactionCountByHash(Hash256 blockHash)
{
SearchResult<Block> searchResult = _blockFinder.SearchForBlock(new BlockParameter(blockHash));
return searchResult.IsError
? GetFailureResult<UInt256?, Block>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedBodiesYet())
: ResultWrapper<UInt256?>.Success((UInt256)searchResult.Object!.Transactions.Length);
}
public ResultWrapper<UInt256?> eth_getBlockTransactionCountByNumber(BlockParameter blockParameter)
{
SearchResult<Block> searchResult = _blockFinder.SearchForBlock(blockParameter);
return searchResult.IsError
? GetFailureResult<UInt256?, Block>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedBodiesYet())
: ResultWrapper<UInt256?>.Success((UInt256)searchResult.Object!.Transactions.Length);
}
public ResultWrapper<UInt256?> eth_getUncleCountByBlockHash(Hash256 blockHash)
{
SearchResult<Block> searchResult = _blockFinder.SearchForBlock(new BlockParameter(blockHash));
return searchResult.IsError
? GetFailureResult<UInt256?, Block>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedBodiesYet())
: ResultWrapper<UInt256?>.Success((UInt256)searchResult.Object!.Uncles.Length);
}
public ResultWrapper<UInt256?> eth_getUncleCountByBlockNumber(BlockParameter? blockParameter)
{
SearchResult<Block> searchResult = _blockFinder.SearchForBlock(blockParameter);
return searchResult.IsError
? GetFailureResult<UInt256?, Block>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedBodiesYet())
: ResultWrapper<UInt256?>.Success((UInt256)searchResult.Object!.Uncles.Length);
}
public ResultWrapper<byte[]> eth_getCode(Address address, BlockParameter? blockParameter = null)
{
SearchResult<BlockHeader> searchResult = _blockFinder.SearchForHeader(blockParameter);
if (searchResult.IsError)
{
return GetFailureResult<byte[], BlockHeader>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedHeadersYet());
}
BlockHeader header = searchResult.Object;
return !HasStateForBlock(_blockchainBridge, header!)
? GetStateFailureResult<byte[]>(header)
: ResultWrapper<byte[]>.Success(
_stateReader.TryGetAccount(header!.StateRoot!, address, out AccountStruct account)
? _stateReader.GetCode(account.CodeHash)
: Array.Empty<byte>());
}
public ResultWrapper<byte[]> eth_sign(Address addressData, byte[] message)
{
Signature sig;
try
{
Address address = addressData;
string messageText = _messageEncoding.GetString(message);
const string signatureTemplate = "\x19Ethereum Signed Message:\n{0}{1}";
string signatureText = string.Format(signatureTemplate, messageText.Length, messageText);
sig = _wallet.Sign(Keccak.Compute(signatureText), address);
}
catch (SecurityException e)
{
return ResultWrapper<byte[]>.Fail(e.Message, ErrorCodes.AccountLocked);
}
catch (Exception)
{
return ResultWrapper<byte[]>.Fail($"Unable to sign as {addressData}");
}
if (_logger.IsTrace) _logger.Trace($"eth_sign request {addressData}, {message}, result: {sig}");
return ResultWrapper<byte[]>.Success(sig.Bytes);
}
public virtual Task<ResultWrapper<Hash256>> eth_sendTransaction(TransactionForRpc rpcTx)
{
Transaction tx = rpcTx.ToTransactionWithDefaults(_blockchainBridge.GetChainId());
TxHandlingOptions options = rpcTx.Nonce is null ? TxHandlingOptions.ManagedNonce : TxHandlingOptions.None;
return SendTx(tx, options);
}
public virtual async Task<ResultWrapper<Hash256>> eth_sendRawTransaction(byte[] transaction)
{
try
{
Transaction tx = Rlp.Decode<Transaction>(transaction,
RlpBehaviors.AllowUnsigned | RlpBehaviors.SkipTypedWrapping | RlpBehaviors.InMempoolForm);
return await SendTx(tx);
}
catch (RlpException)
{
return ResultWrapper<Hash256>.Fail("Invalid RLP.", ErrorCodes.TransactionRejected);
}
}
private async Task<ResultWrapper<Hash256>> SendTx(Transaction tx,
TxHandlingOptions txHandlingOptions = TxHandlingOptions.None)
{
try
{
(Hash256 txHash, AcceptTxResult? acceptTxResult) =
await _txSender.SendTransaction(tx, txHandlingOptions | TxHandlingOptions.PersistentBroadcast);
return acceptTxResult.Equals(AcceptTxResult.Accepted)
? ResultWrapper<Hash256>.Success(txHash)
: ResultWrapper<Hash256>.Fail(acceptTxResult?.ToString() ?? string.Empty, ErrorCodes.TransactionRejected);
}
catch (SecurityException e)
{
return ResultWrapper<Hash256>.Fail(e.Message, ErrorCodes.AccountLocked);
}
catch (Exception e)
{
if (_logger.IsError) _logger.Error("Failed to send transaction.", e);
return ResultWrapper<Hash256>.Fail(e.Message, ErrorCodes.TransactionRejected);
}
}
public ResultWrapper<string> eth_call(TransactionForRpc transactionCall, BlockParameter? blockParameter = null) =>
new CallTxExecutor(_blockchainBridge, _blockFinder, _rpcConfig)
.ExecuteTx(transactionCall, blockParameter);
public ResultWrapper<IReadOnlyList<SimulateBlockResult>> eth_simulateV1(SimulatePayload<TransactionForRpc> payload, BlockParameter? blockParameter = null) =>
new SimulateTxExecutor(_blockchainBridge, _blockFinder, _rpcConfig, _secondsPerSlot)
.Execute(payload, blockParameter);
public ResultWrapper<UInt256?> eth_estimateGas(TransactionForRpc transactionCall, BlockParameter? blockParameter) =>
new EstimateGasTxExecutor(_blockchainBridge, _blockFinder, _rpcConfig)
.ExecuteTx(transactionCall, blockParameter);
public ResultWrapper<AccessListForRpc?> eth_createAccessList(TransactionForRpc transactionCall, BlockParameter? blockParameter = null, bool optimize = true) =>
new CreateAccessListTxExecutor(_blockchainBridge, _blockFinder, _rpcConfig, optimize)
.ExecuteTx(transactionCall, blockParameter);
public ResultWrapper<BlockForRpc> eth_getBlockByHash(Hash256 blockHash, bool returnFullTransactionObjects)
{
return GetBlock(new BlockParameter(blockHash), returnFullTransactionObjects);
}
public ResultWrapper<BlockForRpc> eth_getBlockByNumber(BlockParameter blockParameter,
bool returnFullTransactionObjects)
{
return GetBlock(blockParameter, returnFullTransactionObjects);
}
protected virtual ResultWrapper<BlockForRpc?> GetBlock(BlockParameter blockParameter, bool returnFullTransactionObjects)
{
SearchResult<Block> searchResult = _blockFinder.SearchForBlock(blockParameter, true);
if (searchResult.IsError)
{
return GetFailureResult<BlockForRpc, Block>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedBodiesYet());
}
Block? block = searchResult.Object;
if (returnFullTransactionObjects && block is not null)
{
_blockchainBridge.RecoverTxSenders(block);
}
return ResultWrapper<BlockForRpc?>.Success(block is null
? null
: new BlockForRpc(block, returnFullTransactionObjects, _specProvider));
}
public ResultWrapper<TransactionForRpc?> eth_getTransactionByHash(Hash256 transactionHash)
{
(TxReceipt? receipt, Transaction? transaction, UInt256? baseFee) = _blockchainBridge.GetTransaction(transactionHash, checkTxnPool: true);
if (transaction is null)
{
return ResultWrapper<TransactionForRpc?>.Success(null);
}
RecoverTxSenderIfNeeded(transaction);
TransactionForRpc transactionModel = new(receipt?.BlockHash, receipt?.BlockNumber, receipt?.Index, transaction, baseFee);
if (_logger.IsTrace) _logger.Trace($"eth_getTransactionByHash request {transactionHash}, result: {transactionModel.Hash}");
return ResultWrapper<TransactionForRpc?>.Success(transactionModel);
}
public ResultWrapper<string?> eth_getRawTransactionByHash(Hash256 transactionHash)
{
Transaction? transaction = _blockchainBridge.GetTransaction(transactionHash, checkTxnPool: true).Transaction;
if (transaction is null)
{
return ResultWrapper<string?>.Success(null);
}
IByteBuffer buffer = PooledByteBufferAllocator.Default.Buffer(TxDecoder.Instance.GetLength(transaction, RlpBehaviors.None));
using NettyRlpStream stream = new(buffer);
TxDecoder.Instance.Encode(stream, transaction);
return ResultWrapper<string?>.Success(buffer.AsSpan().ToHexString(false));
}
public ResultWrapper<TransactionForRpc[]> eth_pendingTransactions()
{
Transaction[] transactions = _txPool.GetPendingTransactions();
TransactionForRpc[] transactionsModels = new TransactionForRpc[transactions.Length];
for (int i = 0; i < transactions.Length; i++)
{
Transaction transaction = transactions[i];
RecoverTxSenderIfNeeded(transaction);
transactionsModels[i] = new TransactionForRpc(transaction);
transactionsModels[i].BlockHash = Keccak.Zero;
}
if (_logger.IsTrace) _logger.Trace($"eth_pendingTransactions request, result: {transactionsModels.Length}");
return ResultWrapper<TransactionForRpc[]>.Success(transactionsModels);
}
public ResultWrapper<TransactionForRpc> eth_getTransactionByBlockHashAndIndex(Hash256 blockHash,
UInt256 positionIndex)
{
SearchResult<Block> searchResult = _blockFinder.SearchForBlock(new BlockParameter(blockHash));
if (searchResult.IsError)
{
return GetFailureResult<TransactionForRpc, Block>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedBodiesYet());
}
Block block = searchResult.Object;
if (positionIndex < 0 || positionIndex > block!.Transactions.Length - 1)
{
return ResultWrapper<TransactionForRpc>.Fail("Position Index is incorrect", ErrorCodes.InvalidParams);
}
Transaction transaction = block.Transactions[(int)positionIndex];
RecoverTxSenderIfNeeded(transaction);
TransactionForRpc transactionModel = new(block.Hash, block.Number, (int)positionIndex, transaction, block.BaseFeePerGas);
return ResultWrapper<TransactionForRpc>.Success(transactionModel);
}
public ResultWrapper<TransactionForRpc> eth_getTransactionByBlockNumberAndIndex(BlockParameter blockParameter,
UInt256 positionIndex)
{
SearchResult<Block> searchResult = _blockFinder.SearchForBlock(blockParameter);
if (searchResult.IsError)
{
return GetFailureResult<TransactionForRpc, Block>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedBodiesYet());
}
Block? block = searchResult.Object;
if (positionIndex < 0 || positionIndex > block!.Transactions.Length - 1)
{
return ResultWrapper<TransactionForRpc>.Fail("Position Index is incorrect", ErrorCodes.InvalidParams);
}
Transaction transaction = block.Transactions[(int)positionIndex];
RecoverTxSenderIfNeeded(transaction);
TransactionForRpc transactionModel = new(block.Hash, block.Number, (int)positionIndex, transaction, block.BaseFeePerGas);
if (_logger.IsDebug)
_logger.Debug(
$"eth_getTransactionByBlockNumberAndIndex request {blockParameter}, index: {positionIndex}, result: {transactionModel.Hash}");
return ResultWrapper<TransactionForRpc>.Success(transactionModel);
}
public ResultWrapper<BlockForRpc> eth_getUncleByBlockHashAndIndex(Hash256 blockHash, UInt256 positionIndex)
{
return GetUncle(new BlockParameter(blockHash), positionIndex);
}
public ResultWrapper<BlockForRpc> eth_getUncleByBlockNumberAndIndex(BlockParameter blockParameter,
UInt256 positionIndex)
{
return GetUncle(blockParameter, positionIndex);
}
private ResultWrapper<BlockForRpc> GetUncle(BlockParameter blockParameter, UInt256 positionIndex)
{
SearchResult<Block> searchResult = _blockFinder.SearchForBlock(blockParameter);
if (searchResult.IsError)
{
return GetFailureResult<BlockForRpc, Block>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedBodiesYet());
}
Block block = searchResult.Object;
if (positionIndex < 0 || positionIndex > block!.Uncles.Length - 1)
{
return ResultWrapper<BlockForRpc>.Fail("Position Index is incorrect", ErrorCodes.InvalidParams);
}
BlockHeader uncleHeader = block.Uncles[(int)positionIndex];
return ResultWrapper<BlockForRpc>.Success(new BlockForRpc(new Block(uncleHeader), false, _specProvider));
}
public ResultWrapper<UInt256?> eth_newFilter(Filter filter)
{
BlockParameter fromBlock = filter.FromBlock;
BlockParameter toBlock = filter.ToBlock;
int filterId = _blockchainBridge.NewFilter(fromBlock, toBlock, filter.Address, filter.Topics);
return ResultWrapper<UInt256?>.Success((UInt256)filterId);
}
public ResultWrapper<UInt256?> eth_newBlockFilter()
{
int filterId = _blockchainBridge.NewBlockFilter();
return ResultWrapper<UInt256?>.Success((UInt256)filterId);
}
public ResultWrapper<UInt256?> eth_newPendingTransactionFilter()
{
int filterId = _blockchainBridge.NewPendingTransactionFilter();
return ResultWrapper<UInt256?>.Success((UInt256)filterId);
}
public ResultWrapper<bool?> eth_uninstallFilter(UInt256 filterId)
{
_blockchainBridge.UninstallFilter((int)filterId);
return ResultWrapper<bool?>.Success(true);
}
public ResultWrapper<IEnumerable<object>> eth_getFilterChanges(UInt256 filterId)
{
int id = (int)filterId;
FilterType filterType = _blockchainBridge.GetFilterType(id);
switch (filterType)
{
case FilterType.BlockFilter:
{
return _blockchainBridge.FilterExists(id)
? ResultWrapper<IEnumerable<object>>.Success(_blockchainBridge.GetBlockFilterChanges(id))
: ResultWrapper<IEnumerable<object>>.Fail($"Filter not found", ErrorCodes.InvalidInput);
}
case FilterType.PendingTransactionFilter:
{
return _blockchainBridge.FilterExists(id)
? ResultWrapper<IEnumerable<object>>.Success(_blockchainBridge.GetPendingTransactionFilterChanges(id))
: ResultWrapper<IEnumerable<object>>.Fail($"Filter not found", ErrorCodes.InvalidInput);
}
case FilterType.LogFilter:
{
return _blockchainBridge.FilterExists(id)
? ResultWrapper<IEnumerable<object>>.Success(_blockchainBridge.GetLogFilterChanges(id).ToArray())
: ResultWrapper<IEnumerable<object>>.Fail($"Filter not found", ErrorCodes.InvalidInput);
}
default:
{
return ResultWrapper<IEnumerable<object>>.Fail($"Filter type {filterType} is not supported.", ErrorCodes.InvalidInput);
}
}
}
public ResultWrapper<IEnumerable<FilterLog>> eth_getFilterLogs(UInt256 filterId)
{
CancellationTokenSource cancellationTokenSource = new(_rpcConfig.Timeout);
CancellationToken cancellationToken = cancellationTokenSource.Token;
try
{
int id = filterId <= int.MaxValue ? (int)filterId : -1;
bool filterFound = _blockchainBridge.TryGetLogs(id, out IEnumerable<FilterLog> filterLogs, cancellationToken);
if (id < 0 || !filterFound)
{
cancellationTokenSource.Dispose();
return ResultWrapper<IEnumerable<FilterLog>>.Fail($"Filter with id: {filterId} does not exist.");
}
else
{
return ResultWrapper<IEnumerable<FilterLog>>.Success(GetLogs(filterLogs, cancellationTokenSource));
}
}
catch (ResourceNotFoundException exception)
{
cancellationTokenSource.Dispose();
return GetFailureResult<IEnumerable<FilterLog>>(exception, _ethSyncingInfo.SyncMode.HaveNotSyncedReceiptsYet());
}
}
public ResultWrapper<IEnumerable<FilterLog>> eth_getLogs(Filter filter)
{
// because of lazy evaluation of enumerable, we need to do the validation here first
CancellationTokenSource cancellationTokenSource = new(_rpcConfig.Timeout);
CancellationToken cancellationToken = cancellationTokenSource.Token;
SearchResult<BlockHeader> fromBlockResult;
SearchResult<BlockHeader> toBlockResult;
if (filter.FromBlock == filter.ToBlock)
{
fromBlockResult = toBlockResult = _blockFinder.SearchForHeader(filter.ToBlock);
}
else
{
toBlockResult = _blockFinder.SearchForHeader(filter.ToBlock);
if (toBlockResult.IsError)
{
cancellationTokenSource.Dispose();
return GetFailureResult<IEnumerable<FilterLog>, BlockHeader>(toBlockResult, _ethSyncingInfo.SyncMode.HaveNotSyncedHeadersYet());
}
cancellationToken.ThrowIfCancellationRequested();
fromBlockResult = _blockFinder.SearchForHeader(filter.FromBlock);
}
if (fromBlockResult.IsError)
{
cancellationTokenSource.Dispose();
return GetFailureResult<IEnumerable<FilterLog>, BlockHeader>(fromBlockResult, _ethSyncingInfo.SyncMode.HaveNotSyncedHeadersYet());
}
cancellationToken.ThrowIfCancellationRequested();
BlockHeader fromBlock = fromBlockResult.Object!;
BlockHeader toBlock = toBlockResult.Object!;
long fromBlockNumber = fromBlock.Number;
long toBlockNumber = toBlock.Number;
if (fromBlockNumber > toBlockNumber && toBlockNumber != 0)
{
cancellationTokenSource.Dispose();
return ResultWrapper<IEnumerable<FilterLog>>.Fail($"From block {fromBlockNumber} is later than to block {toBlockNumber}.", ErrorCodes.InvalidParams);
}
try
{
LogFilter logFilter = _blockchainBridge.GetFilter(filter.FromBlock, filter.ToBlock, filter.Address, filter.Topics);
IEnumerable<FilterLog> filterLogs = _blockchainBridge.GetLogs(logFilter, fromBlock, toBlock, cancellationToken);
ArrayPoolList<FilterLog> logs = new(_rpcConfig.MaxLogsPerResponse);
using (cancellationTokenSource)
{
foreach (FilterLog log in filterLogs)
{
logs.Add(log);
if (JsonRpcContext.Current.Value?.IsAuthenticated != true // not authenticated
&& _rpcConfig.MaxLogsPerResponse != 0 // not unlimited
&& logs.Count > _rpcConfig.MaxLogsPerResponse)
{
logs.Dispose();
return ResultWrapper<IEnumerable<FilterLog>>.Fail($"Too many logs requested. Max logs per response is {_rpcConfig.MaxLogsPerResponse}.", ErrorCodes.LimitExceeded);
}
}
}
return ResultWrapper<IEnumerable<FilterLog>>.Success(logs);
}
catch (ResourceNotFoundException exception)
{
return GetFailureResult<IEnumerable<FilterLog>>(exception, _ethSyncingInfo.SyncMode.HaveNotSyncedReceiptsYet());
}
}
// https://github.com/ethereum/EIPs/issues/1186
public ResultWrapper<AccountProof> eth_getProof(Address accountAddress, UInt256[] storageKeys,
BlockParameter blockParameter)
{
SearchResult<BlockHeader> searchResult = _blockFinder.SearchForHeader(blockParameter);
if (searchResult.IsError)
{
return GetFailureResult<AccountProof, BlockHeader>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedHeadersYet());
}
BlockHeader header = searchResult.Object;
if (!HasStateForBlock(_blockchainBridge, header!))
{
return GetStateFailureResult<AccountProof>(header);
}
AccountProofCollector accountProofCollector = new(accountAddress, storageKeys);
_blockchainBridge.RunTreeVisitor(accountProofCollector, header!.StateRoot!);
return ResultWrapper<AccountProof>.Success(accountProofCollector.BuildResult());
}
public ResultWrapper<ulong> eth_chainId()
{
try
{
ulong chainId = _blockchainBridge.GetChainId();
return ResultWrapper<ulong>.Success(chainId);
}
catch (Exception ex)
{
return ResultWrapper<ulong>.Fail(ex.Message, ErrorCodes.InternalError, 0L);
}
}
protected void RecoverTxSenderIfNeeded(Transaction transaction)
{
transaction.SenderAddress ??= _blockchainBridge.RecoverTxSender(transaction);
}
private static IEnumerable<FilterLog> GetLogs(IEnumerable<FilterLog> logs, CancellationTokenSource cancellationTokenSource)
{
using (cancellationTokenSource)
{
foreach (FilterLog log in logs)
{
yield return log;
}
}
}
public ResultWrapper<AccountForRpc?> eth_getAccount(Address accountAddress, BlockParameter? blockParameter)
{
SearchResult<BlockHeader> searchResult = _blockFinder.SearchForHeader(blockParameter);
if (searchResult.IsError)
{
return GetFailureResult<AccountForRpc?, BlockHeader>(searchResult, _ethSyncingInfo.SyncMode.HaveNotSyncedHeadersYet());
}
BlockHeader header = searchResult.Object;
return !HasStateForBlock(_blockchainBridge, header!)
? GetStateFailureResult<AccountForRpc?>(header)
: ResultWrapper<AccountForRpc?>.Success(
_stateReader.TryGetAccount(header!.StateRoot!, accountAddress, out AccountStruct account)
? new AccountForRpc(account)
: null);
}
protected static ResultWrapper<TResult> GetFailureResult<TResult, TSearch>(SearchResult<TSearch> searchResult, bool isTemporary) where TSearch : class =>
ResultWrapper<TResult>.Fail(searchResult, isTemporary && searchResult.ErrorCode == ErrorCodes.ResourceNotFound);
private static ResultWrapper<TResult> GetFailureResult<TResult>(ResourceNotFoundException exception, bool isTemporary) =>
ResultWrapper<TResult>.Fail(exception.Message, ErrorCodes.ResourceNotFound, isTemporary);
private ResultWrapper<TResult> GetStateFailureResult<TResult>(BlockHeader header) =>
ResultWrapper<TResult>.Fail($"No state available for block {header.ToString(BlockHeader.Format.FullHashAndNumber)}", ErrorCodes.ResourceUnavailable, _ethSyncingInfo.SyncMode.HaveNotSyncedStateYet());
public ResultWrapper<ReceiptForRpc?> eth_getTransactionReceipt(Hash256 txHash)
{
(TxReceipt? receipt, TxGasInfo? gasInfo, int logIndexStart) = _blockchainBridge.GetReceiptAndGasInfo(txHash);
if (receipt is null || gasInfo is null)
{
return ResultWrapper<ReceiptForRpc>.Success(null);
}
if (_logger.IsTrace) _logger.Trace($"eth_getTransactionReceipt request {txHash}, result: {txHash}");
return ResultWrapper<ReceiptForRpc>.Success(new(txHash, receipt, gasInfo.Value, logIndexStart));
}
public ResultWrapper<ReceiptForRpc[]?> eth_getBlockReceipts(BlockParameter blockParameter)
{
return _receiptFinder.GetBlockReceipts(blockParameter, _blockFinder, _specProvider);
}
}