Skip to content

Commit

Permalink
fix after rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchhill committed Jul 19, 2024
1 parent 65a39a7 commit e106570
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using Nethermind.Consensus.Producers;
using Nethermind.Config;
using Nethermind.Network.Contract.P2P;
using Nethermind.Consensus.Transactions;

namespace Nethermind.AccountAbstraction;

Expand Down Expand Up @@ -347,7 +348,7 @@ public ValueTask DisposeAsync()
return ValueTask.CompletedTask;
}

public Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPlugin)
public Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPlugin, ITxSource? txSource)
{
if (!Enabled) throw new InvalidOperationException("Account Abstraction plugin is disabled");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

using System.Threading.Tasks;
using Nethermind.Consensus;
using Nethermind.Consensus.Transactions;

namespace Nethermind.Api.Extensions
{
public interface IConsensusWrapperPlugin : INethermindPlugin
{
Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPlugin);
Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPlugin, ITxSource? txSource);
bool Enabled { get; }
}
}
53 changes: 49 additions & 4 deletions src/Nethermind/Nethermind.Core/Collections/ArrayPoolList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,49 @@ void ICollection.CopyTo(Array array, int index)
}

public int Count { get; private set; } = 0;
public void ReduceCount(int count)
{
GuardDispose();
var oldCount = Count;
if (count == oldCount) return;

if (count > oldCount)
{
ThrowOnlyReduce(count);
}

Count = count;
if (count < _capacity / 2)
{
// Reduced to less than half of the capacity, resize the array.
T[] newArray = _arrayPool.Rent(count);
_array.AsSpan(0, count).CopyTo(newArray);
T[] oldArray = Interlocked.Exchange(ref _array, newArray);
_capacity = newArray.Length;
_arrayPool.Return(oldArray);
}
else if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
// Release any references to the objects in the array that are no longer in use.
Array.Clear(_array, count, oldCount - count);
}

void ThrowOnlyReduce(int count)
{
throw new ArgumentException($"Count can only be reduced. {count} is larger than {Count}", nameof(count));
}
}

public void Sort(Comparison<T> comparison)
{
ArgumentNullException.ThrowIfNull(comparison);
GuardDispose();

if (Count > 1)
{
_array.AsSpan(0, Count).Sort(comparison);
}
}

public int Capacity => _capacity;

Expand Down Expand Up @@ -311,12 +354,14 @@ public void Dispose()

~ArrayPoolList()
{
// if (_capacity != 0 && !_disposed)
// {
// throw new InvalidOperationException($"{nameof(ArrayPoolList<T>)} hasn't been disposed. Created {_creationStackTrace}");
// }
if (_capacity != 0 && !_disposed)
{
throw new InvalidOperationException($"{nameof(ArrayPoolList<T>)} hasn't been disposed. Created {_creationStackTrace}");
}
}
#endif

public Span<T> AsSpan() => _array.AsSpan(0, Count);

public ReadOnlyMemory<T> AsMemory() => new(_array, 0, Count);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected virtual async Task<IBlockProducer> BuildProducer()
{
foreach (IConsensusWrapperPlugin wrapperPlugin in _api.GetConsensusWrapperPlugins())
{
return await wrapperPlugin.InitBlockProducer(consensusPlugin);
return await wrapperPlugin.InitBlockProducer(consensusPlugin, null);
}

return await consensusPlugin.InitBlockProducer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

using System;
using System.Collections.Generic;
<<<<<<< HEAD
using System.Threading.Tasks;
=======
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Nethermind.Blockchain.Blocks;
>>>>>>> 30f4f62fb4 (merge shutter into release branch)
using Nethermind.Blockchain.Synchronization;
using Nethermind.Config;
using Nethermind.Consensus;
Expand Down Expand Up @@ -43,6 +38,7 @@
using Nethermind.Specs;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Specs.Forks;
using Nethermind.State;
using Nethermind.Synchronization.ParallelSync;
using NSubstitute;
using NUnit.Framework;
Expand Down Expand Up @@ -309,8 +305,9 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT
LogManager);


BlockProducerEnv blockProducerEnv = blockProducerEnvFactory.Create(_useShutter ? new TestShutterTxSource() : null);
PostMergeBlockProducer postMergeBlockProducer = blockProducerFactory.Create(blockProducerEnv);
ITxSource? txSource = _useShutter ? new TestShutterTxSource() : null;
BlockProducerEnv blockProducerEnv = blockProducerEnvFactory.Create(txSource);
PostMergeBlockProducer postMergeBlockProducer = blockProducerFactory.Create(blockProducerEnv, BlockProductionTrigger, txSource);
PostMergeBlockProducer = postMergeBlockProducer;
PayloadPreparationService ??= new PayloadPreparationService(
postMergeBlockProducer,
Expand Down
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ originalFilter is MinGasPriceContractTxFilter ? originalFilter
}
}

public override Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPlugin)
public override Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPlugin, ITxSource? txSource)
{
_api.BlockProducerEnvFactory = new AuRaMergeBlockProducerEnvFactory(
_auraApi!,
Expand All @@ -77,7 +77,7 @@ public override Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensu
_api.Config<IBlocksConfig>(),
_api.LogManager);

return base.InitBlockProducer(consensusPlugin);
return base.InitBlockProducer(consensusPlugin, txSource);
}

protected override PostMergeBlockProducerFactory CreateBlockProducerFactory()
Expand Down Expand Up @@ -121,7 +121,7 @@ protected override BlockProducerEnv CreateBlockProducerEnv()
_eonUpdateHandler = (_, e) => shutterEon.Update(e.Block.Header);
_api.BlockTree!.NewHeadBlock += _eonUpdateHandler;

shutterTxSource = new ShutterTxSource(_api.LogFinder!, _api.FilterStore!, readOnlyTxProcessingEnvFactory, _api.AbiEncoder, _shutterConfig, _api.SpecProvider!, _api.EthereumEcdsa!, readOnlyBlockTree, validatorsInfo, _api.LogManager);
shutterTxSource = new ShutterTxSource(_api.LogFinder!, readOnlyTxProcessingEnvFactory, _api.AbiEncoder, _shutterConfig, _api.SpecProvider!, _api.EthereumEcdsa!, readOnlyBlockTree, validatorsInfo, _api.LogManager);

ShutterMessageHandler shutterMessageHandler = new(_shutterConfig, shutterTxSource, shutterEon, _api.LogManager);
_shutterP2P = new(shutterMessageHandler.OnDecryptionKeysReceived, _shutterConfig, _api.LogManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Init_merge_plugin_does_not_throw_exception(bool enabled)
Assert.DoesNotThrowAsync(async () => await _plugin.Init(_context));
Assert.DoesNotThrowAsync(async () => await _plugin.InitNetworkProtocol());
Assert.DoesNotThrowAsync(async () => await _plugin.InitSynchronization());
Assert.DoesNotThrowAsync(async () => await _plugin.InitBlockProducer(_consensusPlugin!));
Assert.DoesNotThrowAsync(async () => await _plugin.InitBlockProducer(_consensusPlugin!, null));
Assert.DoesNotThrowAsync(async () => await _plugin.InitRpcModules());
Assert.DoesNotThrowAsync(async () => await _plugin.DisposeAsync());
}
Expand All @@ -111,7 +111,7 @@ public async Task Initializes_correctly()
ISyncConfig syncConfig = _context.Config<ISyncConfig>();
Assert.IsTrue(syncConfig.NetworkingEnabled);
Assert.IsTrue(_context.GossipPolicy.CanGossipBlocks);
await _plugin.InitBlockProducer(_consensusPlugin!);
await _plugin.InitBlockProducer(_consensusPlugin!, null);
Assert.IsInstanceOf<MergeBlockProducer>(_context.BlockProducer);
await _plugin.InitRpcModules();
_context.RpcModuleProvider!.Received().Register(Arg.Is<IRpcModulePool<IEngineRpcModule>>(m => m is SingletonModulePool<IEngineRpcModule>));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
<<<<<<< HEAD
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.FSharp.Control;
using Nethermind.Api.Extensions;
=======
using System.Diagnostics;
>>>>>>> 30f4f62fb4 (merge shutter into release branch)
using Nethermind.Consensus;
using Nethermind.Consensus.Producers;
using Nethermind.Consensus.Transactions;
using Nethermind.Core;
using Nethermind.Merge.Plugin.BlockProduction;
using Nethermind.Merge.Plugin.Handlers;
Expand All @@ -33,7 +32,7 @@ protected virtual BlockProducerEnv CreateBlockProducerEnv()
return _api.BlockProducerEnvFactory.Create();
}

public virtual IBlockProducer InitBlockProducer(IBlockProducerFactory baseBlockProducerFactory, ITxSource? txSource)
public virtual async Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPlugin, ITxSource? txSource)
{
if (MergeEnabled)
{
Expand All @@ -55,8 +54,10 @@ public virtual IBlockProducer InitBlockProducer(IBlockProducerFactory baseBlockP

if (_logger.IsInfo) _logger.Info("Starting Merge block producer & sealer");

_blockProductionTrigger = new BuildBlocksWhenRequested();

IBlockProducer? blockProducer = _mergeBlockProductionPolicy.ShouldInitPreMergeBlockProduction()
? await consensusPlugin.InitBlockProducer()
? await consensusPlugin.InitBlockProducer(_blockProductionTrigger, txSource)
: null;
_manualTimestamper ??= new ManualTimestamper();
BlockProducerEnv blockProducerEnv = CreateBlockProducerEnv();
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Mev.Test/MevPluginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task Can_initialize_block_producer()
IConsensusPlugin consensusPlugin = Substitute.For<IConsensusPlugin>();
consensusPlugin.InitBlockProducer().Returns(Substitute.For<IBlockProducer>());

Task<IBlockProducer> blockProducer = plugin.InitBlockProducer(consensusPlugin);
Task<IBlockProducer> blockProducer = plugin.InitBlockProducer(consensusPlugin, null);

blockProducer.Result.Should().BeOfType(typeof(MevBlockProducer));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Mev/MevPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public Task InitRpcModules()
return Task.CompletedTask;
}

public async Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPlugin)
public async Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPlugin, ITxSource? txSource)
{
if (!Enabled)
{
Expand Down

0 comments on commit e106570

Please sign in to comment.