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

Perf/spec binary search #3647

Merged
merged 5 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class ChainHeadSpecProvider : IChainHeadSpecProvider
{
private readonly ISpecProvider _specProvider;
private readonly IBlockFinder _blockFinder;
private long _lastHeader = -1;
private IReleaseSpec? _headerSpec = null;
private readonly object _lock = new();

public ChainHeadSpecProvider(ISpecProvider specProvider, IBlockFinder blockFinder)
{
Expand All @@ -42,6 +45,28 @@ public ChainHeadSpecProvider(ISpecProvider specProvider, IBlockFinder blockFinde

public long[] TransitionBlocks => _specProvider.TransitionBlocks;

public IReleaseSpec GetSpec() => GetSpec(_blockFinder.FindBestSuggestedHeader()?.Number ?? 0);
public IReleaseSpec GetCurrentHeadSpec()
{
long headerNumber = _blockFinder.FindBestSuggestedHeader()?.Number ?? 0;

// we are fine with potential concurrency issue here, that the spec will change
// between this if and getting actual header spec
// this is used only in tx pool and this is not a problem there
if (headerNumber == _lastHeader)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potential race condition

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

{
IReleaseSpec releaseSpec = _headerSpec;
if (releaseSpec is not null)
{
return releaseSpec;
}
}

// we want to make sure updates to both fields are consistent though
lock (_lock)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, what if headerNumber changed between ln50 and ln65?
We will overwrite _headerSpec with the previous one.

{
_lastHeader = headerNumber;
return _headerSpec = GetSpec(headerNumber);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public IReleaseSpec GetSpec(long blockNumber)

public long[] TransitionBlocks => _specProvider.TransitionBlocks;

public IReleaseSpec GetSpec() => GetSpec(_fixedBlock);
public IReleaseSpec GetCurrentHeadSpec() => GetSpec(_fixedBlock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ namespace Nethermind.Core.Specs
{
public interface IChainHeadSpecProvider : ISpecProvider
{
IReleaseSpec GetSpec();
IReleaseSpec GetCurrentHeadSpec();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Nethermind.Core.Collections;
using Nethermind.Core.Specs;
using Nethermind.Int256;

Expand Down Expand Up @@ -170,28 +171,15 @@ private void BuildTransitions()

public IReleaseSpec GenesisSpec => _transitions.Length == 0 ? null : _transitions[0].Release;

public IReleaseSpec GetSpec(long blockNumber)
{
if (_transitions.Length == 0)
{
return null;
}
public IReleaseSpec GetSpec(long blockNumber) =>
_transitions.TryGetSearchedItem(blockNumber,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary lambda allocation on GetSpec which is called very often

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed by static method

CompareTransitionOnBlock,
out (long BlockNumber, IReleaseSpec Release) transition)
? transition.Release
: null;

IReleaseSpec spec = _transitions[0].Release;
for (int i = 1; i < _transitions.Length; i++)
{
if (blockNumber >= _transitions[i].BlockNumber)
{
spec = _transitions[i].Release;
}
else
{
break;
}
}

return spec;
}
private static int CompareTransitionOnBlock(long blockNumber, (long BlockNumber, IReleaseSpec Release) transition) =>
blockNumber.CompareTo(transition.BlockNumber);

public long? DaoBlockNumber => _chainSpec.DaoForkBlockNumber;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public DeployedCodeFilter(IChainHeadSpecProvider specProvider, IAccountStateProv
_stateProvider = stateProvider;
}
public (bool Accepted, AddTxResult? Reason) Accept(Transaction tx, TxHandlingOptions txHandlingOptions) =>
_stateProvider.IsInvalidContractSender(_specProvider.GetSpec(), tx.SenderAddress!)
_stateProvider.IsInvalidContractSender(_specProvider.GetCurrentHeadSpec(), tx.SenderAddress!)
? (false, AddTxResult.SenderIsContract)
: (true, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public FeeToLowFilter(IChainHeadInfoProvider headInfo, IAccountStateProvider acc

public (bool Accepted, AddTxResult? Reason) Accept(Transaction tx, TxHandlingOptions handlingOptions)
{
IReleaseSpec spec = _specProvider.GetSpec();
IReleaseSpec spec = _specProvider.GetCurrentHeadSpec();
Account account = _accounts.GetAccount(tx.SenderAddress!);
UInt256 balance = account.Balance;
UInt256 affordableGasPrice = tx.CalculateAffordableGasPrice(spec.IsEip1559Enabled, _headInfo.CurrentBaseFee, balance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public MalformedTxFilter(IChainHeadSpecProvider specProvider, ITxValidator txVal

public (bool Accepted, AddTxResult? Reason) Accept(Transaction tx, TxHandlingOptions txHandlingOptions)
{
IReleaseSpec spec = _specProvider.GetSpec();
IReleaseSpec spec = _specProvider.GetCurrentHeadSpec();
if (!_txValidator.IsWellFormed(tx, spec))
{
// It may happen that other nodes send us transactions that were signed for another chain or don't have enough gas.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public TooExpensiveTxFilter(IChainHeadInfoProvider headInfo, IAccountStateProvid

public (bool Accepted, AddTxResult? Reason) Accept(Transaction tx, TxHandlingOptions handlingOptions)
{
IReleaseSpec spec = _specProvider.GetSpec();
IReleaseSpec spec = _specProvider.GetCurrentHeadSpec();
Account account = _accounts.GetAccount(tx.SenderAddress!);
UInt256 balance = account.Balance;
UInt256 cumulativeCost = UInt256.Zero;
Expand Down
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.TxPool/TxPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private AddTxResult AddCore(Transaction tx, bool isPersistentBroadcast)
{
lock (_locker)
{
bool eip1559Enabled = _specProvider.GetSpec().IsEip1559Enabled;
bool eip1559Enabled = _specProvider.GetCurrentHeadSpec().IsEip1559Enabled;

tx.GasBottleneck = tx.CalculateEffectiveGasPrice(eip1559Enabled, _headInfo.CurrentBaseFee);
bool inserted = _transactions.TryInsert(tx.Hash, tx, out Transaction? removed);
Expand Down Expand Up @@ -363,14 +363,14 @@ private AddTxResult AddCore(Transaction tx, bool isPersistentBroadcast)
{
if (previousTxBottleneck == null)
{
previousTxBottleneck = tx.CalculateAffordableGasPrice(_specProvider.GetSpec().IsEip1559Enabled,
previousTxBottleneck = tx.CalculateAffordableGasPrice(_specProvider.GetCurrentHeadSpec().IsEip1559Enabled,
_headInfo.CurrentBaseFee, balance);
}

if (tx.Nonce == currentNonce + i)
{
UInt256 effectiveGasPrice =
tx.CalculateEffectiveGasPrice(_specProvider.GetSpec().IsEip1559Enabled,
tx.CalculateEffectiveGasPrice(_specProvider.GetCurrentHeadSpec().IsEip1559Enabled,
_headInfo.CurrentBaseFee);
gasBottleneck = UInt256.Min(effectiveGasPrice, previousTxBottleneck ?? 0);
}
Expand Down