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

Shutter: refactor API dependency injection #7671

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
29 changes: 4 additions & 25 deletions src/Nethermind/Nethermind.Shutter.Test/ShutterApiSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,32 @@

using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Nethermind.Abi;
using Nethermind.Api;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.Receipts;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Builders;
using Nethermind.Crypto;
using Nethermind.Facade.Find;
using Nethermind.KeyStore.Config;
using Nethermind.Logging;
using Nethermind.Shutter.Config;
using Nethermind.State;
using NSubstitute;

namespace Nethermind.Shutter.Test;

public class ShutterApiSimulator(
ShutterEventSimulator eventSimulator,
IAbiEncoder abiEncoder,
IBlockTree blockTree,
IEthereumEcdsa ecdsa,
ILogFinder logFinder,
IReceiptStorage receiptStorage,
ILogManager logManager,
ISpecProvider specProvider,
ITimestamper timestamper,
IWorldStateManager worldStateManager,
IFileSystem fileSystem,
IKeyStoreConfig keyStoreConfig,
IShutterConfig cfg,
INethermindApi api,
Dictionary<ulong, byte[]> validatorsInfo,
Random rnd
) : ShutterApi(abiEncoder, blockTree, ecdsa, logFinder, receiptStorage,
logManager, specProvider, timestamper, worldStateManager, fileSystem,
keyStoreConfig, cfg, validatorsInfo, ShutterTestsCommon.SlotLength, IPAddress.None)
Random rnd) : ShutterApi(api, validatorsInfo, ShutterTestsCommon.SlotLength)
{
public int EonUpdateCalled = 0;
public int KeysValidated = 0;
public ShutterTransactions? LoadedTransactions;

private readonly Random _rnd = rnd;
private readonly IReceiptStorage _receiptStorage = receiptStorage;
private readonly IReceiptStorage _receiptStorage = api.ReceiptStorage!;

public (List<ShutterEventSimulator.Event> events, Dto.DecryptionKeys keys) AdvanceSlot(int eventCount, int? keyCount = null)
{
Expand Down
40 changes: 23 additions & 17 deletions src/Nethermind/Nethermind.Shutter.Test/ShutterTestsCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO.Abstractions;
using Nethermind.Abi;
using Nethermind.Api;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Receipts;
using Nethermind.Core;
Expand Down Expand Up @@ -50,26 +51,31 @@ class ShutterTestsCommon

public static ShutterApiSimulator InitApi(Random rnd, ITimestamper? timestamper = null, ShutterEventSimulator? eventSimulator = null)
{
IWorldStateManager worldStateManager = Substitute.For<IWorldStateManager>();
ILogFinder logFinder = Substitute.For<ILogFinder>();
IBlockTree blockTree = Substitute.For<IBlockTree>();
IReceiptStorage receiptStorage = Substitute.For<IReceiptStorage>();
return new(
eventSimulator ?? InitEventSimulator(rnd),
AbiEncoder, blockTree, Ecdsa, logFinder, receiptStorage,
LogManager, SpecProvider, timestamper ?? Substitute.For<ITimestamper>(),
worldStateManager, Substitute.For<IFileSystem>(),
Substitute.For<IKeyStoreConfig>(), Cfg, [], rnd
);
INethermindApi api = Substitute.For<INethermindApi>();
api.AbiEncoder.Returns(AbiEncoder);
api.EthereumEcdsa.Returns(Ecdsa);
api.Timestamper.Returns(timestamper ?? Substitute.For<ITimestamper>());
api.Config<IShutterConfig>().Returns(Cfg);

return new(eventSimulator ?? InitEventSimulator(rnd), api, [], rnd);
}

public static ShutterApiSimulator InitApi(Random rnd, MergeTestBlockchain chain, ITimestamper? timestamper = null, ShutterEventSimulator? eventSimulator = null)
=> new(
eventSimulator ?? InitEventSimulator(rnd),
AbiEncoder, chain.BlockTree.AsReadOnly(), chain.EthereumEcdsa, chain.LogFinder, chain.ReceiptStorage,
chain.LogManager, chain.SpecProvider, timestamper ?? chain.Timestamper, chain.WorldStateManager,
Substitute.For<IFileSystem>(), Substitute.For<IKeyStoreConfig>(), Cfg, [], rnd
);
{
INethermindApi api = Substitute.For<INethermindApi>();
api.AbiEncoder.Returns(AbiEncoder);
api.EthereumEcdsa.Returns(chain.EthereumEcdsa);
api.Timestamper.Returns(timestamper ?? Substitute.For<ITimestamper>());
api.BlockTree.Returns(chain.BlockTree);
api.LogFinder.Returns(chain.LogFinder);
api.ReceiptStorage.Returns(chain.ReceiptStorage);
api.LogManager.Returns(chain.LogManager);
api.SpecProvider.Returns(chain.SpecProvider);
api.WorldStateManager.Returns(chain.WorldStateManager);
api.Config<IShutterConfig>().Returns(Cfg);

return new(eventSimulator ?? InitEventSimulator(rnd), api, [], rnd);
}

public static ShutterEventSimulator InitEventSimulator(Random rnd)
=> new(
Expand Down
67 changes: 28 additions & 39 deletions src/Nethermind/Nethermind.Shutter/ShutterApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@
using System.Threading.Tasks;
using Multiformats.Address;
using Nethermind.Abi;
using Nethermind.Api;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.Receipts;
using Nethermind.Consensus;
using Nethermind.Consensus.Processing;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Crypto;
using Nethermind.Facade.Find;
using Nethermind.KeyStore.Config;
using Nethermind.Logging;
using Nethermind.Shutter.Config;
using Nethermind.State;

namespace Nethermind.Shutter;

Expand Down Expand Up @@ -50,60 +47,52 @@ public class ShutterApi : IShutterApi
private readonly IShutterConfig _cfg;
private readonly TimeSpan _blockWaitCutoff;

public ShutterApi(
IAbiEncoder abiEncoder,
IBlockTree blockTree,
IEthereumEcdsa ecdsa,
ILogFinder logFinder,
IReceiptFinder receiptFinder,
ILogManager logManager,
ISpecProvider specProvider,
ITimestamper timestamper,
IWorldStateManager worldStateManager,
IFileSystem fileSystem,
IKeyStoreConfig keyStoreConfig,
IShutterConfig cfg,
Dictionary<ulong, byte[]> validatorsInfo,
TimeSpan slotLength,
IPAddress ip
)
public ShutterApi(INethermindApi api, Dictionary<ulong, byte[]> validatorsInfo, TimeSpan slotLength)
{
_cfg = cfg;
_blockTree = blockTree;
_readOnlyBlockTree = blockTree.AsReadOnly();
_abiEncoder = abiEncoder;
_logManager = logManager;
_cfg = api.Config<IShutterConfig>();
_blockTree = api.BlockTree!;
_readOnlyBlockTree = _blockTree.AsReadOnly();
_abiEncoder = api.AbiEncoder;
_logManager = api.LogManager;
_fileSystem = api.FileSystem;
_keyStoreConfig = api.Config<IKeyStoreConfig>();
_slotLength = slotLength;
_fileSystem = fileSystem;
_keyStoreConfig = keyStoreConfig;
_blockUpToDateCutoff = slotLength;
_blockWaitCutoff = _slotLength / 3;

_txProcessingEnvFactory = new(worldStateManager, blockTree, specProvider, logManager);
_txProcessingEnvFactory = new(api.WorldStateManager!, _blockTree, api.SpecProvider, _logManager);

Time = InitTime(specProvider, timestamper);
TxLoader = new(logFinder, _cfg, Time, specProvider, ecdsa, abiEncoder, logManager);
Time = InitTime(api.SpecProvider!, api.Timestamper);
TxLoader = new(
api.LogFinder!,
_cfg,
Time,
api.SpecProvider!,
api.EthereumEcdsa!,
_abiEncoder,
_logManager
);
Eon = InitEon();
BlockHandler = new ShutterBlockHandler(
specProvider.ChainId,
api.SpecProvider!.ChainId,
_cfg,
_txProcessingEnvFactory,
blockTree,
abiEncoder,
receiptFinder,
_blockTree,
_abiEncoder,
api.ReceiptFinder!,
validatorsInfo,
Eon,
TxLoader,
Time,
logManager,
_logManager,
_slotLength,
BlockWaitCutoff);

TxSource = new ShutterTxSource(TxLoader, _cfg, Time, logManager);
TxSource = new ShutterTxSource(TxLoader, _cfg, Time, _logManager);

KeyValidator = new ShutterKeyValidator(_cfg, Eon, logManager);
KeyValidator = new ShutterKeyValidator(_cfg, Eon, _logManager);

InitP2P(ip);
InitP2P(api.IpResolver!.ExternalIp);
}

public Task StartP2P(Multiaddress[] bootnodeP2PAddresses, CancellationTokenSource? cancellationTokenSource = null)
Expand Down
17 changes: 2 additions & 15 deletions src/Nethermind/Nethermind.Shutter/ShutterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Threading;
using Nethermind.Config;
using Multiformats.Address;
using Nethermind.KeyStore.Config;

namespace Nethermind.Shutter;

Expand Down Expand Up @@ -100,21 +99,9 @@ public IBlockProducer InitBlockProducer(IBlockProducerFactory consensusPlugin, I
}

_shutterApi = new ShutterApi(
_api.AbiEncoder,
_api.BlockTree,
_api.EthereumEcdsa,
_api.LogFinder,
_api.ReceiptFinder,
_api.LogManager,
_api.SpecProvider,
_api.Timestamper,
_api.WorldStateManager,
_api.FileSystem,
_api.Config<IKeyStoreConfig>(),
_shutterConfig,
_api,
validatorsInfo,
TimeSpan.FromSeconds(_blocksConfig!.SecondsPerSlot),
_api.IpResolver.ExternalIp
TimeSpan.FromSeconds(_blocksConfig!.SecondsPerSlot)
);

_ = _shutterApi.StartP2P(bootnodeP2PAddresses, _cts);
Expand Down
9 changes: 5 additions & 4 deletions src/Nethermind/Nethermind.Shutter/ShutterTxLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ public class ShutterTxLoader(
ILogManager logManager)
{
private readonly ShutterLogScanner _logScanner = new(
new(new Address(cfg.SequencerContractAddress!)),
logFinder,
logManager,
abiEncoder);
new SequencerContract(new Address(cfg.SequencerContractAddress!)),
logFinder,
logManager,
abiEncoder
);

private readonly ShutterEventQueue _events = new(cfg.EncryptedGasLimit, logManager);
private ulong _txPointer = ulong.MaxValue;
Expand Down