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

Reduce number of reads from AccountState inside TxPool::SubmitTx #4820

Merged
merged 3 commits into from
Nov 11, 2022
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
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.Consensus/TxFilterAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using System;
using Nethermind.Blockchain;
Expand All @@ -38,7 +38,7 @@ public TxFilterAdapter(IBlockTree blockTree, ITxFilter txFilter, ILogManager log
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
}

public AcceptTxResult Accept(Transaction tx, TxHandlingOptions txHandlingOptions)
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions)
{
if (tx is not GeneratedTransaction)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.TxPool/Filters/AlreadyKnownTxFilter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using Nethermind.Core;

Expand All @@ -34,7 +34,7 @@ public AlreadyKnownTxFilter(HashCache hashCache)
_hashCache = hashCache;
}

public AcceptTxResult Accept(Transaction tx, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
{
if (_hashCache.Get(tx.Hash!))
{
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.TxPool/Filters/DeployedCodeFilter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using Nethermind.Core;
using Nethermind.Core.Specs;
Expand All @@ -33,7 +33,7 @@ public DeployedCodeFilter(IChainHeadSpecProvider specProvider, IAccountStateProv
_specProvider = specProvider;
_stateProvider = stateProvider;
}
public AcceptTxResult Accept(Transaction tx, TxHandlingOptions txHandlingOptions) =>
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions) =>
_stateProvider.IsInvalidContractSender(_specProvider.GetCurrentHeadSpec(), tx.SenderAddress!)
? AcceptTxResult.SenderIsContract
: AcceptTxResult.Accepted;
Expand Down
17 changes: 7 additions & 10 deletions src/Nethermind/Nethermind.TxPool/Filters/FeeTooLowFilter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using System.Diagnostics;
using Nethermind.Core;
Expand All @@ -31,24 +31,21 @@ internal class FeeTooLowFilter : IIncomingTxFilter
{
private readonly IChainHeadSpecProvider _specProvider;
private readonly IChainHeadInfoProvider _headInfo;
private readonly IAccountStateProvider _accounts;
private readonly TxDistinctSortedPool _txs;
private readonly ILogger _logger;

public FeeTooLowFilter(IChainHeadInfoProvider headInfo, IAccountStateProvider accountStateProvider, TxDistinctSortedPool txs, ILogManager logManager)
public FeeTooLowFilter(IChainHeadInfoProvider headInfo, TxDistinctSortedPool txs, ILogManager logManager)
{
_specProvider = headInfo.SpecProvider;
_headInfo = headInfo;
_accounts = accountStateProvider;
_txs = txs;
_logger = logManager.GetClassLogger();
}

public AcceptTxResult Accept(Transaction tx, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
{
IReleaseSpec spec = _specProvider.GetCurrentHeadSpec();
Account account = _accounts.GetAccount(tx.SenderAddress!);
UInt256 balance = account.Balance;
UInt256 balance = state.SenderAccount.Balance;
UInt256 affordableGasPrice = tx.CalculateAffordableGasPrice(spec.IsEip1559Enabled, _headInfo.CurrentBaseFee, balance);
bool isNotLocal = (handlingOptions & TxHandlingOptions.PersistentBroadcast) != TxHandlingOptions.PersistentBroadcast;

Expand Down
16 changes: 7 additions & 9 deletions src/Nethermind/Nethermind.TxPool/Filters/GapNonceFilter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using Nethermind.Core;
using Nethermind.Int256;
Expand All @@ -29,21 +29,19 @@ namespace Nethermind.TxPool.Filters
internal class GapNonceFilter : IIncomingTxFilter
{
private readonly TxDistinctSortedPool _txs;
private readonly IAccountStateProvider _accounts;
private readonly ILogger _logger;

public GapNonceFilter(IAccountStateProvider accountStateProvider, TxDistinctSortedPool txs, ILogger logger)
public GapNonceFilter(TxDistinctSortedPool txs, ILogger logger)
{
_txs = txs;
_accounts = accountStateProvider;
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
{
int numberOfSenderTxsInPending = _txs.GetBucketCount(tx.SenderAddress);
bool isTxPoolFull = _txs.IsFull();
UInt256 currentNonce = _accounts.GetAccount(tx.SenderAddress!).Nonce;
UInt256 currentNonce = state.SenderAccount.Nonce;
long nextNonceInOrder = (long)currentNonce + numberOfSenderTxsInPending;
bool isTxNonceNextInOrder = tx.Nonce <= nextNonceInOrder;
if (isTxPoolFull && !isTxNonceNextInOrder)
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.TxPool/Filters/GasLimitTxFilter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using System;
using Nethermind.Core;
Expand All @@ -38,7 +38,7 @@ public GasLimitTxFilter(IChainHeadInfoProvider chainHeadInfoProvider, ITxPoolCon
_configuredGasLimit = txPoolConfig.GasLimit ?? long.MaxValue;
}

public AcceptTxResult Accept(Transaction tx, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
{
long gasLimit = Math.Min(_chainHeadInfoProvider.BlockGasLimit ?? long.MaxValue, _configuredGasLimit);
if (tx.GasLimit > gasLimit)
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.TxPool/Filters/IIncomingTxFilter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using Nethermind.Core;

Expand All @@ -25,6 +25,6 @@ namespace Nethermind.TxPool.Filters
/// </summary>
public interface IIncomingTxFilter
{
AcceptTxResult Accept(Transaction tx, TxHandlingOptions txHandlingOptions);
AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions);
}
}
16 changes: 7 additions & 9 deletions src/Nethermind/Nethermind.TxPool/Filters/LowNonceFilter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using Nethermind.Core;
using Nethermind.Int256;
Expand All @@ -26,22 +26,20 @@ namespace Nethermind.TxPool.Filters
/// </summary>
internal class LowNonceFilter : IIncomingTxFilter
{
private readonly IAccountStateProvider _accounts;
private readonly ILogger _logger;

public LowNonceFilter(IAccountStateProvider accountStateProvider, ILogger logger)
public LowNonceFilter(ILogger logger)
{
_accounts = accountStateProvider;
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
{
// As we have limited number of transaction that we store in mem pool its fairly easy to fill it up with
// high-priority garbage transactions. We need to filter them as much as possible to use the tx pool space
// efficiently. One call to get account from state is not that costly and it only happens after previous checks.
// This was modeled by OpenEthereum behavior.
Account account = _accounts.GetAccount(tx.SenderAddress!);
Account account = state.SenderAccount;
UInt256 currentNonce = account.Nonce;
if (tx.Nonce < currentNonce)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.TxPool/Filters/MalformedTxFilter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using Nethermind.Core;
using Nethermind.Core.Specs;
Expand All @@ -37,7 +37,7 @@ public MalformedTxFilter(IChainHeadSpecProvider specProvider, ITxValidator txVal
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxHandlingOptions txHandlingOptions)
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions)
{
IReleaseSpec spec = _specProvider.GetCurrentHeadSpec();
if (!_txValidator.IsWellFormed(tx, spec))
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.TxPool/Filters/NullHashTxFilter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using Nethermind.Core;

Expand All @@ -26,7 +26,7 @@ namespace Nethermind.TxPool.Filters
/// </summary>
internal class NullHashTxFilter : IIncomingTxFilter
{
public AcceptTxResult Accept(Transaction tx, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
{
if (tx.Hash is null)
{
Expand Down
Loading