Skip to content

Commit

Permalink
Decorate AuRaTxFilters in order to disable them post merge (#4182)
Browse files Browse the repository at this point in the history
* Decorate AuRaTxFilters in order to disable them post merge

* Fix decorator for MinGasPriceContractTxFilter

* Redesign & Refactor

* Remove unused usings

Co-authored-by: lukasz.rozmej <lukasz.rozmej@gmail.com>
  • Loading branch information
jmederosalvarado and LukaszRozmej authored Jun 22, 2022
1 parent fac8017 commit 7418498
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ namespace Nethermind.Consensus.AuRa.InitializationSteps
{
public static class TxAuRaFilterBuilders
{
/// <summary>
/// Filter decorator.
/// <remarks>
/// Allow to create new filter based on original filter and a potential fallbackFilter if original filter was not used.
/// </remarks>
/// </summary>
public delegate ITxFilter FilterDecorator(ITxFilter originalFilter, ITxFilter? fallbackFilter = null);

/// <summary>
/// Delegate factory method to create final filter for AuRa.
/// </summary>
/// <remarks>
/// This is used to decorate original filter with <see cref="AuRaMergeTxFilter"/> in order to disable it post-merge.
/// </remarks>
public static FilterDecorator CreateFilter { get; set; } = (x, _) => x;

private static ITxFilter CreateBaseAuRaTxFilter(
IMiningConfig miningConfig,
AuRaNethermindApi api,
Expand All @@ -40,15 +56,15 @@ private static ITxFilter CreateBaseAuRaTxFilter(
ITxFilter gasPriceTxFilter = minGasPriceTxFilter;
if (minGasPricesContractDataStore != null)
{
gasPriceTxFilter = new MinGasPriceContractTxFilter(minGasPriceTxFilter, minGasPricesContractDataStore);
gasPriceTxFilter = CreateFilter(new MinGasPriceContractTxFilter(minGasPriceTxFilter, minGasPricesContractDataStore), minGasPriceTxFilter);
}

Address? registrar = api.ChainSpec?.Parameters.Registrar;
if (registrar != null)
{
RegisterContract registerContract = new(api.AbiEncoder, registrar, readOnlyTxProcessorSource);
CertifierContract certifierContract = new(api.AbiEncoder, registerContract, readOnlyTxProcessorSource);
return new TxCertifierFilter(certifierContract, gasPriceTxFilter, specProvider, api.LogManager);
return CreateFilter(new TxCertifierFilter(certifierContract, gasPriceTxFilter, specProvider, api.LogManager));
}

return gasPriceTxFilter;
Expand All @@ -65,7 +81,7 @@ private static ITxFilter CreateBaseAuRaTxFilter(
{
RegisterContract registerContract = new(api.AbiEncoder, registrar, readOnlyTxProcessorSource);
CertifierContract certifierContract = new(api.AbiEncoder, registerContract, readOnlyTxProcessorSource);
return new TxCertifierFilter(certifierContract, baseTxFilter, specProvider, api.LogManager);
return CreateFilter(new TxCertifierFilter(certifierContract, baseTxFilter, specProvider, api.LogManager));
}

return baseTxFilter;
Expand All @@ -81,7 +97,7 @@ private static ITxFilter CreateBaseAuRaTxFilter(
{
api.TxFilterCache ??= new PermissionBasedTxFilter.Cache();

var txPermissionFilter = new PermissionBasedTxFilter(
var txPermissionFilter = CreateFilter(new PermissionBasedTxFilter(
new VersionedTransactionPermissionContract(api.AbiEncoder,
api.ChainSpec.Parameters.TransactionPermissionContract,
api.ChainSpec.Parameters.TransactionPermissionContractTransition ?? 0,
Expand All @@ -90,7 +106,7 @@ private static ITxFilter CreateBaseAuRaTxFilter(
api.LogManager,
api.SpecProvider),
api.TxFilterCache,
api.LogManager);
api.LogManager));

return txPermissionFilter;
}
Expand Down
16 changes: 4 additions & 12 deletions src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
using Nethermind.Merge.Plugin;
using Nethermind.Consensus.Transactions;
using Nethermind.Consensus.AuRa.Transactions;
using Nethermind.Consensus.Producers;
using Nethermind.Consensus.Processing;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.InitializationSteps;
using Nethermind.Consensus.AuRa.Contracts.DataStore;
using Nethermind.Core;
using Nethermind.Db;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Data;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Nethermind.Consensus;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.State;
using Nethermind.Specs.ChainSpecStyle;
using System.Threading.Tasks;
using Nethermind.Api;
using Nethermind.Api.Extensions;
Expand All @@ -43,6 +31,10 @@ public override async Task Init(INethermindApi nethermindApi)
_auraApi = (AuRaNethermindApi)nethermindApi;
_auraApi.PoSSwitcher = _poSSwitcher;
_mergeConfig.Enabled = false; // set MergePlugin as disabled

// this runs before all init steps that use tx filters
TxAuRaFilterBuilders.CreateFilter = (originalFilter, fallbackFilter) =>
new AuRaMergeTxFilter(_poSSwitcher, originalFilter, fallbackFilter);
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeTxFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Nethermind.Core;
using Nethermind.TxPool;
using Nethermind.Consensus.Transactions;
using Nethermind.Consensus;

namespace Nethermind.Merge.AuRa
{
public class AuRaMergeTxFilter : ITxFilter
{
private readonly IPoSSwitcher _poSSwitcher;
private readonly ITxFilter _preMergeTxFilter;
private readonly ITxFilter _postMergeTxFilter;

public AuRaMergeTxFilter(IPoSSwitcher poSSwitcher, ITxFilter preMergeTxFilter, ITxFilter? postMergeTxFilter = null)
{
_poSSwitcher = poSSwitcher;
_preMergeTxFilter = preMergeTxFilter;
_postMergeTxFilter = postMergeTxFilter ?? NullTxFilter.Instance;
}

public AcceptTxResult IsAllowed(Transaction tx, BlockHeader parentHeader) =>
_poSSwitcher.IsPostMerge(parentHeader)
? _postMergeTxFilter.IsAllowed(tx, parentHeader)
: _preMergeTxFilter.IsAllowed(tx, parentHeader);
}
}
12 changes: 6 additions & 6 deletions src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public partial class MergePlugin : IConsensusWrapperPlugin, ISynchronizationPlug
protected IPoSSwitcher _poSSwitcher = NoPoS.Instance;
private IBeaconPivot? _beaconPivot;
private BeaconSync? _beaconSync;
private IBlockCacheService _blockCacheService;
private IBlockCacheService _blockCacheService = null!;
private InvalidChainTracker.InvalidChainTracker? _invalidChainTracker;
private IPeerRefresher _peerRefresher;
private IPeerRefresher _peerRefresher = null!;

private ManualBlockFinalizationManager _blockFinalizationManager = null!;
private IMergeBlockProductionPolicy? _mergeBlockProductionPolicy;
Expand Down Expand Up @@ -246,11 +246,11 @@ public Task InitRpcModules()
}

PayloadPreparationService payloadPreparationService = new(
_postMergeBlockProducer,
improvementContextFactory,
_api.Sealer,
_postMergeBlockProducer,
improvementContextFactory,
_api.Sealer,
_api.TimerFactory,
_api.LogManager,
_api.LogManager,
TimeSpan.FromSeconds(_mergeConfig.SecondsPerSlot));

IEngineRpcModule engineRpcModule = new EngineRpcModule(
Expand Down

0 comments on commit 7418498

Please sign in to comment.