Skip to content

Commit

Permalink
Deduplicate via generics and inheritence
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 committed May 29, 2024
1 parent bd6a882 commit 4322d4f
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 653 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
using Nethermind.Core.Specs;
using Nethermind.Facade;
using Nethermind.Facade.Eth;
using Nethermind.JsonRpc.Data;
using Nethermind.JsonRpc.Modules.Eth.GasPrice;
using Nethermind.JsonRpc.Modules.Eth.FeeHistory;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.TxPool;
using Nethermind.Wallet;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Nethermind.JsonRpc.Modules.Eth
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Threading.Tasks;
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.Receipts;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Evm;
using Nethermind.Facade;
using Nethermind.Facade.Eth;
using Nethermind.JsonRpc.Data;
using Nethermind.JsonRpc.Modules.Eth.FeeHistory;
using Nethermind.JsonRpc.Modules.Eth.GasPrice;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.TxPool;
using Nethermind.Wallet;

namespace Nethermind.JsonRpc.Modules.Eth;

public class EthRpcModule : EthRpcModule<ReceiptForRpc>, IEthRpcModule
{
public 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) : base(

rpcConfig,
blockchainBridge,
blockFinder,
receiptFinder,
stateReader,
txPool,
txSender,
wallet,
logManager,
specProvider,
gasPriceOracle,
ethSyncingInfo,
feeHistoryOracle)
{
}

public override 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 override ResultWrapper<ReceiptForRpc[]> eth_getBlockReceipts(BlockParameter blockParameter)
{
return _receiptFinder.GetBlockReceipts(blockParameter, _blockFinder, _specProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Nethermind.JsonRpc.Modules.Eth
{
public partial class EthRpcModule
public partial class EthRpcModule<TReceiptForRpc>
{
private abstract class TxExecutor<TResult>
{
Expand Down
61 changes: 24 additions & 37 deletions src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@

namespace Nethermind.JsonRpc.Modules.Eth;

public partial class EthRpcModule : IEthRpcModule
public abstract partial class EthRpcModule<TReceiptForRpc> : IEthRpcModule<TReceiptForRpc> where TReceiptForRpc : ReceiptForRpc
{
private readonly Encoding _messageEncoding = Encoding.UTF8;
private readonly IJsonRpcConfig _rpcConfig;
private readonly IBlockchainBridge _blockchainBridge;
private readonly IBlockFinder _blockFinder;
private readonly IReceiptFinder _receiptFinder;
private readonly IStateReader _stateReader;
private readonly ITxPool _txPoolBridge;
private readonly ITxSender _txSender;
private readonly IWallet _wallet;
private readonly ISpecProvider _specProvider;
private readonly ILogger _logger;
private readonly IGasPriceOracle _gasPriceOracle;
private readonly IEthSyncingInfo _ethSyncingInfo;

private readonly IFeeHistoryOracle _feeHistoryOracle;
protected readonly Encoding _messageEncoding = Encoding.UTF8;
protected readonly IJsonRpcConfig _rpcConfig;
protected readonly IBlockchainBridge _blockchainBridge;
protected readonly IBlockFinder _blockFinder;
protected readonly IReceiptFinder _receiptFinder;
protected readonly IStateReader _stateReader;
protected readonly ITxPool _txPoolBridge;
protected readonly ITxSender _txSender;
protected readonly IWallet _wallet;
protected readonly ISpecProvider _specProvider;
protected readonly ILogger _logger;
protected readonly IGasPriceOracle _gasPriceOracle;
protected readonly IEthSyncingInfo _ethSyncingInfo;

protected readonly IFeeHistoryOracle _feeHistoryOracle;
private static bool HasStateForBlock(IBlockchainBridge blockchainBridge, BlockHeader header)
{
return blockchainBridge.HasStateForRoot(header.StateRoot!);
Expand Down Expand Up @@ -193,7 +193,7 @@ public ResultWrapper<byte[]> eth_getStorageAt(Address address, UInt256 positionI
return ResultWrapper<byte[]>.Success(storage.IsEmpty ? Bytes32.Zero.Unwrap() : storage!.PadLeft(32));
}

public Task<ResultWrapper<UInt256>> eth_getTransactionCount(Address address, BlockParameter blockParameter)
public Task<ResultWrapper<UInt256>> eth_getTransactionCount(Address address, BlockParameter? blockParameter)
{
if (blockParameter == BlockParameter.Pending)
{
Expand Down Expand Up @@ -233,11 +233,6 @@ public Task<ResultWrapper<UInt256>> eth_getTransactionCount(Address address, Blo
: ResultWrapper<UInt256?>.Success((UInt256)searchResult.Object!.Transactions.Length);
}

public ResultWrapper<ReceiptForRpc[]> eth_getBlockReceipts(BlockParameter blockParameter)
{
return _receiptFinder.GetBlockReceipts(blockParameter, _blockFinder, _specProvider);
}

public ResultWrapper<UInt256?> eth_getUncleCountByBlockHash(Hash256 blockHash)
{
SearchResult<Block> searchResult = _blockFinder.SearchForBlock(new BlockParameter(blockHash));
Expand Down Expand Up @@ -295,14 +290,14 @@ public ResultWrapper<byte[]> eth_sign(Address addressData, byte[] message)
return ResultWrapper<byte[]>.Success(sig.Bytes);
}

public Task<ResultWrapper<Hash256>> eth_sendTransaction(TransactionForRpc rpcTx)
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 async Task<ResultWrapper<Hash256>> eth_sendRawTransaction(byte[] transaction)
public virtual async Task<ResultWrapper<Hash256>> eth_sendRawTransaction(byte[] transaction)
{
try
{
Expand Down Expand Up @@ -343,7 +338,7 @@ public ResultWrapper<string> eth_call(TransactionForRpc transactionCall, BlockPa
new CallTxExecutor(_blockchainBridge, _blockFinder, _rpcConfig)
.ExecuteTx(transactionCall, blockParameter);

public ResultWrapper<UInt256?> eth_estimateGas(TransactionForRpc transactionCall, BlockParameter blockParameter) =>
public ResultWrapper<UInt256?> eth_estimateGas(TransactionForRpc transactionCall, BlockParameter? blockParameter) =>
new EstimateGasTxExecutor(_blockchainBridge, _blockFinder, _rpcConfig)
.ExecuteTx(transactionCall, blockParameter);

Expand Down Expand Up @@ -468,18 +463,6 @@ public ResultWrapper<TransactionForRpc> eth_getTransactionByBlockNumberAndIndex(
return ResultWrapper<TransactionForRpc>.Success(transactionModel);
}

public Task<ResultWrapper<ReceiptForRpc>> eth_getTransactionReceipt(Hash256 txHash)
{
(TxReceipt? receipt, TxGasInfo? gasInfo, int logIndexStart) = _blockchainBridge.GetReceiptAndGasInfo(txHash);
if (receipt is null || gasInfo is null)
{
return Task.FromResult(ResultWrapper<ReceiptForRpc>.Success(null));
}

if (_logger.IsTrace) _logger.Trace($"eth_getTransactionReceipt request {txHash}, result: {txHash}");
return Task.FromResult(ResultWrapper<ReceiptForRpc>.Success(new(txHash, receipt, gasInfo.Value, logIndexStart)));
}

public ResultWrapper<BlockForRpc> eth_getUncleByBlockHashAndIndex(Hash256 blockHash, UInt256 positionIndex)
{
return GetUncle(new BlockParameter(blockHash), positionIndex);
Expand Down Expand Up @@ -730,4 +713,8 @@ private static ResultWrapper<TResult> GetFailureResult<TResult>(ResourceNotFound

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 abstract ResultWrapper<TReceiptForRpc[]?> eth_getBlockReceipts([JsonRpcParameter(ExampleValue = "latest")] BlockParameter blockParameter);

public abstract ResultWrapper<TReceiptForRpc?> eth_getTransactionReceipt([JsonRpcParameter(ExampleValue = "[\"0x80757153e93d1b475e203406727b62a501187f63e23b8fa999279e219ee3be71\"]")] Hash256 txHashData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
namespace Nethermind.JsonRpc.Modules.Eth
{
[RpcModule(ModuleType.Eth)]
public interface IEthRpcModule : IRpcModule
public interface IEthRpcModule : IEthRpcModule<ReceiptForRpc> { }

public interface IEthRpcModule<TReceiptForRpc> : IRpcModule where TReceiptForRpc : ReceiptForRpc
{
[JsonRpcMethod(IsImplemented = true,
Description = "Returns ChainID",
Expand Down Expand Up @@ -111,7 +113,7 @@ public interface IEthRpcModule : IRpcModule
[JsonRpcMethod(Description = "Get receipts from all transactions from particular block, more efficient than fetching the receipts one-by-one.",
IsImplemented = true,
ExampleResponse = "{\"jsonrpc\":\"2.0\",\"result\":[{\"transactionHash\":\"0x681c2b6f99e37fd6fe6046db8b51ec3460d699cacd6a376143fd5842ac50621f\",\"transactionIndex\":\"0x0\",\"blockHash\":\"0x29f141925d2d8e357ae5b6040c97aa12d7ac6dfcbe2b20e7b616d8907ac8e1f3\",\"blockNumber\":\"0x3\",\"cumulativeGasUsed\":\"0x5208\",\"gasUsed\":\"0x5208\",\"effectiveGasPrice\":\"0x1\",\"from\":\"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099\",\"to\":\"0x942921b14f1b1c385cd7e0cc2ef7abe5598c8358\",\"contractAddress\":null,\"logs\":[],\"logsBloom\":\"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"status\":\"0x1\",\"type\":\"0x0\"},{\"transactionHash\":\"0x7126cf20a0ad8bd51634837d9049615c34c1bff5e1a54e5663f7e23109bff48b\",\"transactionIndex\":\"0x1\",\"blockHash\":\"0x29f141925d2d8e357ae5b6040c97aa12d7ac6dfcbe2b20e7b616d8907ac8e1f3\",\"blockNumber\":\"0x3\",\"cumulativeGasUsed\":\"0xa410\",\"gasUsed\":\"0x5208\",\"effectiveGasPrice\":\"0x1\",\"from\":\"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099\",\"to\":\"0x942921b14f1b1c385cd7e0cc2ef7abe5598c8358\",\"contractAddress\":null,\"logs\":[],\"logsBloom\":\"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"status\":\"0x1\",\"type\":\"0x0\"}],\"id\":67}")]
ResultWrapper<ReceiptForRpc[]> eth_getBlockReceipts([JsonRpcParameter(ExampleValue = "latest")] BlockParameter blockParameter);
ResultWrapper<TReceiptForRpc[]?> eth_getBlockReceipts([JsonRpcParameter(ExampleValue = "latest")] BlockParameter blockParameter);

[JsonRpcMethod(IsImplemented = true,
Description = "Returns number of uncles in the block by block hash",
Expand Down Expand Up @@ -212,7 +214,7 @@ ResultWrapper<TransactionForRpc> eth_getTransactionByBlockNumberAndIndex(
Description = "Retrieves a transaction receipt by tx hash",
IsSharable = true,
ExampleResponse = "{\"transactionHash\":\"0x80757153e93d1b475e203406727b62a501187f63e23b8fa999279e219ee3be71\",\"transactionIndex\":\"0x7\",\"blockHash\":\"0x42def051b21038905cd2a2bc28d460a94df2249466847f0e1bcb4be4eb21891a\",\"blockNumber\":\"0x4e3f39\",\"cumulativeGasUsed\":\"0x62c9d\",\"gasUsed\":\"0xe384\",\"effectiveGasPrice\":\"0x12a05f200\",\"from\":\"0x0afe0a94415e8974052e7e6cfab19ee1c2ef4f69\",\"to\":\"0x19e8c84d4943e58b035626b064cfc76ee13ee6cb\",\"contractAddress\":null,\"logs\":[{\"removed\":false,\"logIndex\":\"0x0\",\"transactionIndex\":\"0x7\",\"transactionHash\":\"0x80757153e93d1b475e203406727b62a501187f63e23b8fa999279e219ee3be71\",\"blockHash\":\"0x42def051b21038905cd2a2bc28d460a94df2249466847f0e1bcb4be4eb21891a\",\"blockNumber\":\"0x4e3f39\",\"address\":\"0x2ac3c1d3e24b45c6c310534bc2dd84b5ed576335\",\"data\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"topics\":[\"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef\",\"0x00000000000000000000000019e8c84d4943e58b035626b064cfc76ee13ee6cb\",\"0x00000000000000000000000028078300a459a9e136f872285654cdc74463041e\"]},{\"removed\":false,\"logIndex\":\"0x1\",\"transactionIndex\":\"0x7\",\"transactionHash\":\"0x80757153e93d1b475e203406727b62a501187f63e23b8fa999279e219ee3be71\",\"blockHash\":\"0x42def051b21038905cd2a2bc28d460a94df2249466847f0e1bcb4be4eb21891a\",\"blockNumber\":\"0x4e3f39\",\"address\":\"0x19e8c84d4943e58b035626b064cfc76ee13ee6cb\",\"data\":\"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007735940000000000000000000000000000000000000000000000000000000000000000000\",\"topics\":[\"0x950494fc3642fae5221b6c32e0e45765c95ebb382a04a71b160db0843e74c99f\",\"0x0000000000000000000000000afe0a94415e8974052e7e6cfab19ee1c2ef4f69\",\"0x00000000000000000000000028078300a459a9e136f872285654cdc74463041e\",\"0x0000000000000000000000000afe0a94415e8974052e7e6cfab19ee1c2ef4f69\"]}],\"logsBloom\":\"0x00000000000000000000000000000000000000000000000020000000000000800000000000000000000400000000000000000000000000000000000000002000000000000000000000000008000000000000000000000000000000000000000000000002002000000000000000000000000000000000000000000812000000000000000000000000000001000000000000000000000008000400008000000000000000000000000000000000000000000000000000000000800000000000000000000002000000000000000000000000000000000000100000000000000000002000000000000000000000000010000000000000000000000400000000020000\",\"status\":\"0x1\",\"type\":\"0x0\"}")]
Task<ResultWrapper<ReceiptForRpc>> eth_getTransactionReceipt([JsonRpcParameter(ExampleValue = "[\"0x80757153e93d1b475e203406727b62a501187f63e23b8fa999279e219ee3be71\"]")] Hash256 txHashData);
ResultWrapper<TReceiptForRpc?> eth_getTransactionReceipt([JsonRpcParameter(ExampleValue = "[\"0x80757153e93d1b475e203406727b62a501187f63e23b8fa999279e219ee3be71\"]")] Hash256 txHashData);

[JsonRpcMethod(IsImplemented = true,
Description = "Retrieves an uncle block header by block hash and uncle index",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public void Return(string methodName, IRpcModule rpcModule)

private static IDictionary<string, (MethodInfo, bool, RpcEndpoint)> GetMethodDict(Type type)
{
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
BindingFlags methodFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
IEnumerable<MethodInfo> methods = type.GetMethods(methodFlags).Concat(type.GetInterfaces().SelectMany(i => i.GetMethods(methodFlags)));
return methods.ToDictionary(
x => x.Name.Trim(),
x =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Threading.Tasks;
using Nethermind.Api;
using Nethermind.Config;
using Nethermind.Consensus;
Expand Down
3 changes: 0 additions & 3 deletions src/Nethermind/Nethermind.Optimism/OptimismBlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Blocks;
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.Receipts;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Evm;
using Nethermind.Evm.Tracing;
using Nethermind.Logging;
using Nethermind.State;
Expand Down
Loading

0 comments on commit 4322d4f

Please sign in to comment.