Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/eth subscription base fee per gas missing #3810

Merged
merged 7 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,12 @@ public virtual async Task Execute(CancellationToken cancellationToken)
_api.TxPool,
_api.ReceiptStorage,
_api.FilterStore,
_api.EthSyncingInfo!);
_api.EthSyncingInfo!,
_api.SpecProvider);

SubscriptionManager subscriptionManager = new(subscriptionFactory, _api.LogManager);

SubscribeRpcModule subscribeRpcModule = new(subscriptionManager);
SubscribeRpcModule subscribeRpcModule = new(subscriptionManager, _api.SpecProvider);
_api.RpcModuleProvider.RegisterSingle<ISubscribeRpcModule>(subscribeRpcModule);

Web3RpcModule web3RpcModule = new(_api.LogManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.Receipts;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Builders;
using Nethermind.Db;
using Nethermind.Db.Blooms;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class SubscribeModuleTests
private ISubscriptionManager _subscriptionManager;
private IJsonRpcDuplexClient _jsonRpcDuplexClient;
private IJsonSerializer _jsonSerializer;
private ISpecProvider _specProvider;

[SetUp]
public void Setup()
Expand All @@ -65,6 +67,7 @@ public void Setup()
_blockTree = Substitute.For<IBlockTree>();
_txPool = Substitute.For<ITxPool>();
_receiptStorage = Substitute.For<IReceiptStorage>();
_specProvider = Substitute.For<ISpecProvider>();
_filterStore = new FilterStore();
_jsonRpcDuplexClient = Substitute.For<IJsonRpcDuplexClient>();
_jsonSerializer = new EthereumJsonSerializer();
Expand All @@ -75,13 +78,14 @@ public void Setup()
_txPool,
_receiptStorage,
_filterStore,
new EthSyncingInfo(_blockTree));
new EthSyncingInfo(_blockTree),
_specProvider);

_subscriptionManager = new SubscriptionManager(
subscriptionFactory,
_logManager);

_subscribeRpcModule = new SubscribeRpcModule(_subscriptionManager);
_subscribeRpcModule = new SubscribeRpcModule(_subscriptionManager, _specProvider);
_subscribeRpcModule.Context = new JsonRpcContext(RpcEndpoint.Ws, _jsonRpcDuplexClient);

// block numbers matching filters in LogsSubscriptions with null arguments will be 33333-77777
Expand All @@ -91,9 +95,9 @@ public void Setup()
_blockTree.FindHeader(Arg.Any<BlockParameter>(), true).Returns(toBlock);
}

private JsonRpcResult GetBlockAddedToMainResult(BlockReplacementEventArgs blockReplacementEventArgs, out string subscriptionId, Filter filter = null)
private JsonRpcResult GetBlockAddedToMainResult(BlockReplacementEventArgs blockReplacementEventArgs, out string subscriptionId, Filter filter = null, ISpecProvider specProvider = null)
{
NewHeadSubscription newHeadSubscription = new(_jsonRpcDuplexClient, _blockTree, _logManager, filter);
NewHeadSubscription newHeadSubscription = new(_jsonRpcDuplexClient, _blockTree, _logManager, filter, specProvider);

JsonRpcResult jsonRpcResult = new();

Expand Down Expand Up @@ -783,6 +787,21 @@ public void NewPendingTransactionsSubscription_on_NewPending_with_includeTransac
expectedResult.Should().Be(serialized);
}

[Test]
public void NewHeadSubscription_with_baseFeePerGas_test()
{
_specProvider.GetSpec(Arg.Any<long>()).IsEip1559Enabled.Returns(true);
Block block = Build.A.Block.Genesis.WithTotalDifficulty(0L).WithBaseFeePerGas(10000).TestObject;
BlockReplacementEventArgs blockReplacementEventArgs = new(block);
JsonRpcResult jsonRpcResult = GetBlockAddedToMainResult(blockReplacementEventArgs, out var subscriptionId, null, _specProvider);

jsonRpcResult.Response.Should().NotBeNull();
string serialized = _jsonSerializer.Serialize(jsonRpcResult.Response);
var expectedResult = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_subscription\",\"params\":{\"subscription\":\"" + subscriptionId + "\",\"result\":{\"author\":\"0x0000000000000000000000000000000000000000\",\"difficulty\":\"0xf4240\",\"extraData\":\"0x010203\",\"gasLimit\":\"0x3d0900\",\"gasUsed\":\"0x0\",\"hash\":\"0xb3157bcccab04639f6393042690a6c9862deebe88c781f911e8dfd265531e9ff\",\"logsBloom\":\"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"miner\":\"0x0000000000000000000000000000000000000000\",\"mixHash\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"nonce\":\"0x00000000000003e8\",\"number\":\"0x0\",\"parentHash\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"receiptsRoot\":\"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\",\"sha3Uncles\":\"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347\",\"size\":\"0x201\",\"stateRoot\":\"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\",\"totalDifficulty\":\"0x0\",\"timestamp\":\"0xf4240\",\"baseFeePerGas\":\"0x2710\",\"transactions\":[],\"transactionsRoot\":\"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\",\"uncles\":[]}}}";
expectedResult.Should().Be(serialized);

}

[Test]
public void DroppedPendingTransactionsSubscription_creating_result()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using Nethermind.Core.Specs;
using Nethermind.JsonRpc.Modules.Eth;

namespace Nethermind.JsonRpc.Modules.Subscribe
{
public interface ISubscriptionFactory
{
Subscription CreateSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, SubscriptionType subscriptionType, Filter? filter);
Subscription CreateSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, SubscriptionType subscriptionType, Filter? filter, ISpecProvider? specProvider);
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
//

using System;
using Nethermind.Core.Specs;
using Nethermind.JsonRpc.Modules.Eth;

namespace Nethermind.JsonRpc.Modules.Subscribe
{
public interface ISubscriptionManager
{
string AddSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, SubscriptionType subscriptionType, Filter? filter = null);
string AddSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, SubscriptionType subscriptionType, Filter? filter = null, ISpecProvider? specProvider = null);
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved
bool RemoveSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, string subscriptionId);
void RemoveClientSubscriptions(object? sender, EventArgs e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Threading.Tasks;
using Nethermind.Blockchain;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.JsonRpc.Modules.Eth;
using Nethermind.Logging;

Expand All @@ -28,14 +29,16 @@ public class NewHeadSubscription : Subscription
{
private readonly IBlockTree _blockTree;
private readonly bool _includeTransactions;
private readonly ISpecProvider _specProvider;


public NewHeadSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, IBlockTree? blockTree, ILogManager? logManager, Filter? filter = null)
public NewHeadSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, IBlockTree? blockTree, ILogManager? logManager, Filter? filter = null, ISpecProvider? specProvider = null)
: base(jsonRpcDuplexClient)
{
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
_logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
_includeTransactions = filter?.IncludeTransactions ?? false;
if (specProvider != null) _specProvider = specProvider;

_blockTree.BlockAddedToMain += OnBlockAddedToMain;
if(_logger.IsTrace) _logger.Trace($"NewHeads subscription {Id} will track BlockAddedToMain");
Expand All @@ -45,7 +48,7 @@ private void OnBlockAddedToMain(object? sender, BlockReplacementEventArgs e)
{
ScheduleAction(() =>
{
JsonRpcResult result = CreateSubscriptionMessage(new BlockForRpc(e.Block, _includeTransactions));
JsonRpcResult result = CreateSubscriptionMessage(new BlockForRpc(e.Block, _includeTransactions, _specProvider));
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved

JsonRpcDuplexClient.SendJsonRpcResult(result);
if(_logger.IsTrace) _logger.Trace($"NewHeads subscription {Id} printed new block");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@
//

using System;
using Nethermind.Core.Specs;
using Nethermind.JsonRpc.Modules.Eth;

namespace Nethermind.JsonRpc.Modules.Subscribe
{
public class SubscribeRpcModule : ISubscribeRpcModule
{
private readonly ISubscriptionManager _subscriptionManager;
private readonly ISpecProvider _specProvider;

public SubscribeRpcModule(ISubscriptionManager subscriptionManager)
public SubscribeRpcModule(ISubscriptionManager subscriptionManager, ISpecProvider specProvider)
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved
{
_subscriptionManager = subscriptionManager ?? throw new ArgumentNullException(nameof(subscriptionManager));
_specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
}


public ResultWrapper<string> eth_subscribe(string subscriptionName, Filter arguments = null)
{
if (Enum.TryParse(typeof(SubscriptionType), subscriptionName, true, out var subscriptionType))
{
return ResultWrapper<string>.Success(_subscriptionManager.AddSubscription(Context.DuplexClient, (SubscriptionType)subscriptionType, arguments));
return ResultWrapper<string>.Success(_subscriptionManager.AddSubscription(Context.DuplexClient, (SubscriptionType)subscriptionType, arguments, _specProvider));
}
return ResultWrapper<string>.Fail($"Wrong subscription type: {subscriptionName}.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Nethermind.Blockchain;
using Nethermind.Blockchain.Filters;
using Nethermind.Blockchain.Receipts;
using Nethermind.Core.Specs;
using Nethermind.Facade.Eth;
using Nethermind.JsonRpc.Modules.Eth;
using Nethermind.Logging;
Expand All @@ -34,28 +35,31 @@ public class SubscriptionFactory : ISubscriptionFactory
private readonly IReceiptStorage _receiptStorage;
private readonly IFilterStore _filterStore;
private readonly IEthSyncingInfo _ethSyncingInfo;
private readonly ISpecProvider _specProvider;

public SubscriptionFactory(
ILogManager? logManager,
IBlockTree? blockTree,
ITxPool? txPool,
IReceiptStorage? receiptStorage,
IFilterStore? filterStore,
IEthSyncingInfo ethSyncingInfo)
IEthSyncingInfo ethSyncingInfo,
ISpecProvider specProvider)
{
_logManager = logManager ?? throw new ArgumentNullException(nameof(logManager));
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
_txPool = txPool ?? throw new ArgumentNullException(nameof(txPool));
_receiptStorage = receiptStorage ?? throw new ArgumentNullException(nameof(receiptStorage));
_filterStore = filterStore ?? throw new ArgumentNullException(nameof(filterStore));
_ethSyncingInfo = ethSyncingInfo ?? throw new ArgumentNullException(nameof(ethSyncingInfo));
_specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
}
public Subscription CreateSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, SubscriptionType subscriptionType, Filter? filter)
public Subscription CreateSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, SubscriptionType subscriptionType, Filter? filter, ISpecProvider? specProvider)
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved
{
switch (subscriptionType)
{
case SubscriptionType.NewHeads:
return new NewHeadSubscription(jsonRpcDuplexClient, _blockTree, _logManager, filter);
return new NewHeadSubscription(jsonRpcDuplexClient, _blockTree, _logManager, filter, _specProvider);
case SubscriptionType.Logs:
return new LogsSubscription(jsonRpcDuplexClient, _receiptStorage, _filterStore, _blockTree, _logManager, filter);
case SubscriptionType.NewPendingTransactions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Nethermind.Core.Specs;
using Nethermind.JsonRpc.Modules.Eth;
using Nethermind.Logging;

Expand All @@ -39,9 +40,9 @@ public SubscriptionManager(ISubscriptionFactory? subscriptionFactory, ILogManage
_logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
}

public string AddSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, SubscriptionType subscriptionType, Filter? filter = null)
public string AddSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, SubscriptionType subscriptionType, Filter? filter = null, ISpecProvider? specProvider = null)
{
Subscription subscription = _subscriptionFactory.CreateSubscription(jsonRpcDuplexClient, subscriptionType, filter);
Subscription subscription = _subscriptionFactory.CreateSubscription(jsonRpcDuplexClient, subscriptionType, filter, specProvider);
AddToDictionary(subscription);
AddOrUpdateClientsBag(subscription);

Expand Down