Skip to content

Commit

Permalink
Use IMinerConfig.MinGasPrice as default gas price in block (#3323)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej authored Aug 18, 2021
1 parent bc82be2 commit a71ff07
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
12 changes: 9 additions & 3 deletions src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
using System.Threading.Tasks;
using Nethermind.Api;
using Nethermind.Api.Extensions;
using Nethermind.Consensus;
using Nethermind.Core;
using Nethermind.Init.Steps.Migrations;
using Nethermind.JsonRpc;
using Nethermind.JsonRpc.Modules;
using Nethermind.JsonRpc.Modules.Admin;
using Nethermind.JsonRpc.Modules.DebugModule;
using Nethermind.JsonRpc.Modules.Eth;
using Nethermind.JsonRpc.Modules.Eth.GasPrice;
using Nethermind.JsonRpc.Modules.Evm;
using Nethermind.JsonRpc.Modules.Net;
using Nethermind.JsonRpc.Modules.Parity;
Expand Down Expand Up @@ -89,21 +91,25 @@ public virtual async Task Execute(CancellationToken cancellationToken)
IInitConfig initConfig = _api.Config<IInitConfig>();
IJsonRpcConfig rpcConfig = _api.Config<IJsonRpcConfig>();
INetworkConfig networkConfig = _api.Config<INetworkConfig>();
IMiningConfig miningConfig = _api.Config<IMiningConfig>();

// lets add threads to support parallel eth_getLogs
ThreadPool.GetMinThreads(out int workerThreads, out int completionPortThreads);
ThreadPool.SetMinThreads(workerThreads + Environment.ProcessorCount, completionPortThreads + Environment.ProcessorCount);


GasPriceOracle gasPriceOracle = new GasPriceOracle(_api.BlockTree, _api.SpecProvider, miningConfig.MinGasPrice);
EthModuleFactory ethModuleFactory = new(
_api.TxPool,
_api.TxSender,
_api.Wallet,
_api.BlockTree,
_api.Config<IJsonRpcConfig>(),
rpcConfig,
_api.LogManager,
_api.StateReader,
_api,
_api.SpecProvider);
_api.SpecProvider,
gasPriceOracle);

_api.RpcModuleProvider.RegisterBounded(ethModuleFactory, rpcConfig.EthModuleConcurrentInstances ?? Environment.ProcessorCount, rpcConfig.Timeout);

if (_api.DbProvider == null) throw new StepDependencyException(nameof(_api.DbProvider));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Nethermind.Db.Blooms;
using Nethermind.Trie.Pruning;
using Nethermind.Facade;
using Nethermind.JsonRpc.Modules.Eth.GasPrice;
using Nethermind.State;
using Nethermind.TxPool;
using Nethermind.Wallet;
Expand Down Expand Up @@ -68,7 +69,8 @@ public async Task Initialize()
LimboLogs.Instance,
Substitute.For<IStateReader>(),
Substitute.For<IBlockchainBridgeFactory>(),
Substitute.For<ISpecProvider>()),
Substitute.For<ISpecProvider>(),
Substitute.For<IGasPriceOracle>()),
1, 1000);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public void GasPriceEstimate_NoChangeInHeadBlock_ReturnsPreviousGasPrice()
result.Should().Be((UInt256) 7);
}

[Test]
public void GasPriceEstimate_IfPreviousGasPriceDoesNotExist_FallbackGasPriceSetToDefaultGasPrice()
[TestCase(null)]
[TestCase(100ul)]
public void GasPriceEstimate_IfPreviousGasPriceDoesNotExist_FallbackGasPriceSetToDefaultGasPrice(ulong? gasPrice)
{
IBlockFinder blockFinder = Substitute.For<IBlockFinder>();
ISpecProvider specProvider = Substitute.For<ISpecProvider>();
GasPriceOracle testGasPriceOracle = new(blockFinder, specProvider);
GasPriceOracle testGasPriceOracle = new(blockFinder, specProvider, gasPrice);

testGasPriceOracle.GetGasPriceEstimate();

testGasPriceOracle.FallbackGasPrice.Should().BeEquivalentTo((UInt256?) 20.GWei());
testGasPriceOracle.FallbackGasPrice.Should().BeEquivalentTo((UInt256?) EthGasPriceConstants.DefaultGasPrice);
testGasPriceOracle.FallbackGasPrice.Should().BeEquivalentTo(gasPrice ?? 1.GWei());
}

[TestCase(3)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
using NUnit.Framework;
using BlockTree = Nethermind.Blockchain.BlockTree;
using System.Threading.Tasks;
using Nethermind.JsonRpc.Modules.Eth.GasPrice;

namespace Nethermind.JsonRpc.Test.Modules
{
Expand All @@ -64,7 +65,8 @@ public async Task Initialize()
LimboLogs.Instance,
Substitute.For<IStateReader>(),
Substitute.For<IBlockchainBridgeFactory>(),
Substitute.For<ISpecProvider>());
Substitute.For<ISpecProvider>(),
Substitute.For<IGasPriceOracle>());
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using Nethermind.Blockchain;
using Nethermind.Consensus;
using Nethermind.Core.Specs;
using Nethermind.Facade;
using Nethermind.JsonRpc.Data;
Expand All @@ -39,7 +40,9 @@ public class EthModuleFactory : ModuleFactoryBase<IEthRpcModule>
private readonly ITxSender _txSender;
private readonly IWallet _wallet;
private readonly IJsonRpcConfig _rpcConfig;
private readonly IMiningConfig _miningConfig;
private readonly ISpecProvider _specProvider;
private readonly IGasPriceOracle _gasPriceOracle;

public EthModuleFactory(
ITxPool txPool,
Expand All @@ -50,7 +53,8 @@ public EthModuleFactory(
ILogManager logManager,
IStateReader stateReader,
IBlockchainBridgeFactory blockchainBridgeFactory,
ISpecProvider specProvider)
ISpecProvider specProvider,
IGasPriceOracle gasPriceOracle)
{
_txPool = txPool ?? throw new ArgumentNullException(nameof(txPool));
_txSender = txSender ?? throw new ArgumentNullException(nameof(txSender));
Expand All @@ -60,6 +64,7 @@ public EthModuleFactory(
_stateReader = stateReader ?? throw new ArgumentNullException(nameof(stateReader));
_blockchainBridgeFactory = blockchainBridgeFactory ?? throw new ArgumentNullException(nameof(blockchainBridgeFactory));
_specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
_gasPriceOracle = gasPriceOracle ?? throw new ArgumentNullException(nameof(gasPriceOracle));
_blockTree = blockTree.AsReadOnly();
}

Expand All @@ -75,7 +80,7 @@ public override IEthRpcModule Create()
_wallet,
_logManager,
_specProvider,
new GasPriceOracle(_blockTree, _specProvider));
_gasPriceOracle);
}

public static List<JsonConverter> Converters = new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static class EthGasPriceConstants
{
public const int PercentileOfSortedTxs = 60; //Percentile of sortedTxList indexes to choose as gas price
public const int DefaultBlocksLimit = 20; //Limit for how many blocks we check txs in to add to sortedTxList
public static readonly UInt256 DefaultGasPrice = 20.GWei(); //Tx price added to sortedTxList for a block that has 0 txs (now adds 1 price)
public const int TxLimitFromABlock = 3; //Maximum number of tx we can add to sortedTxList from one block
public const int DefaultIgnoreUnder = 2; //Effective Gas Prices under this are ignored
public static readonly UInt256 MaxGasPrice = 500.GWei(); //Maximum gas price we can return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
using System.Collections.Generic;
using System.Linq;
using Nethermind.Blockchain.Find;
using Nethermind.Consensus;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Int256;

Expand All @@ -28,7 +30,8 @@ namespace Nethermind.JsonRpc.Modules.Eth.GasPrice
public class GasPriceOracle : IGasPriceOracle
{
private readonly IBlockFinder _blockFinder;
public UInt256 FallbackGasPrice => LastGasPrice ?? EthGasPriceConstants.DefaultGasPrice;
private readonly UInt256 _minGasPrice;
public UInt256 FallbackGasPrice => LastGasPrice ?? _minGasPrice;
private ISpecProvider SpecProvider { get; }
public UInt256? LastGasPrice { get; set; }
public Block? LastHeadBlock { get; set; }
Expand All @@ -38,9 +41,11 @@ public class GasPriceOracle : IGasPriceOracle

public GasPriceOracle(
IBlockFinder blockFinder,
ISpecProvider specProvider)
ISpecProvider specProvider,
UInt256? minGasPrice = null)
{
_blockFinder = blockFinder;
_minGasPrice = minGasPrice ?? new MiningConfig().MinGasPrice;
SpecProvider = specProvider;
}

Expand Down Expand Up @@ -126,12 +131,12 @@ private IEnumerable<UInt256> GetGasPricesFromRecentBlocks(IEnumerable<Block> blo
}
}

private static UInt256 GetGasPriceAtPercentile(List<UInt256> txGasPriceList)
private UInt256 GetGasPriceAtPercentile(List<UInt256> txGasPriceList)
{
int roundedIndex = GetRoundedIndexAtPercentile(txGasPriceList.Count);

return roundedIndex < 0
? EthGasPriceConstants.DefaultGasPrice
? _minGasPrice
: txGasPriceList[roundedIndex];
}

Expand Down

0 comments on commit a71ff07

Please sign in to comment.