Skip to content

Commit

Permalink
Add initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rjnrohit committed Oct 10, 2024
1 parent 558a425 commit 4eeeb9f
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/nethermind-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
- Nethermind.EthStats.Test
- Nethermind.Evm.Test
- Nethermind.Facade.Test
- Nethermind.Flashbots.Test
- Nethermind.HealthChecks.Test
- Nethermind.Hive.Test
- Nethermind.JsonRpc.Test
Expand Down
170 changes: 170 additions & 0 deletions src/Nethermind/Nethermind.Flashbots.Test/FlashbotsModuleTests.Setup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Threading.Tasks;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Blocks;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Consensus;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Blockchain;
using Nethermind.Crypto;
using Nethermind.Db;
using Nethermind.Flashbots;
using Nethermind.Flashbots.Handlers;
using Nethermind.Flashbots.Modules.Flashbots;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.Merge.Plugin;
using Nethermind.Specs;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Specs.Forks;
using NUnit.Framework;

namespace Nethermind.Flasbots.Test;

public partial class FlashbotsModuleTests
{
TestKeyAndAddress? TestKeysAndAddress;

[SetUp]
public void SetUp(){
TestKeysAndAddress = new TestKeyAndAddress();
}

internal class TestKeyAndAddress
{
public PrivateKey privateKey = new PrivateKey("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291");
public Address TestAddr;

public PrivateKey TestValidatorKey = new PrivateKey("28c3cd61b687fdd03488e167a5d84f50269df2a4c29a2cfb1390903aa775c5d0");
public Address TestValidatorAddr;

public PrivateKey TestBuilderKey = new PrivateKey("0bfbbbc68fefd990e61ba645efb84e0a62e94d5fff02c9b1da8eb45fea32b4e0");
public Address TestBuilderAddr;

public UInt256 TestBalance = UInt256.Parse("2000000000000000000");
public byte[] logCode = Bytes.FromHexString("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00");
public TestKeyAndAddress()
{
TestAddr = privateKey.Address;
TestValidatorAddr = TestValidatorKey.Address;
TestBuilderAddr = TestBuilderKey.Address;
}
}

protected virtual MergeTestBlockChain CreateBaseBlockChain(
IFlashbotsConfig flashbotsConfig,
ILogManager? logManager = null)
{
return new MergeTestBlockChain(flashbotsConfig, logManager);
}

protected async Task<MergeTestBlockChain> CreateBlockChain(
IReleaseSpec? releaseSpec = null,
IFlashbotsConfig? flashbotsConfig = null,
ILogManager? logManager = null)
=> await CreateBaseBlockChain(flashbotsConfig ?? new FlashbotsConfig(), logManager).Build(new TestSingleReleaseSpecProvider(releaseSpec ?? London.Instance));

private IFlashbotsRpcModule CreateFlashbotsModule(MergeTestBlockChain chain, ReadOnlyTxProcessingEnv readOnlyTxProcessingEnv)
{
return new FlashbotsRpcModule(
new ValidateSubmissionHandler(
chain.HeaderValidator,
chain.BlockValidator,
readOnlyTxProcessingEnv,
chain.FlashbotsConfig
)
);
}

public class MergeTestBlockChain : TestBlockchain
{
public IFlashbotsConfig FlashbotsConfig;

public IMergeConfig MergeConfig;

public IWithdrawalProcessor? WithdrawalProcessor { get; set; }

public ReadOnlyTxProcessingEnv ReadOnlyTxProcessingEnv { get; set; }

public MergeTestBlockChain(IFlashbotsConfig flashbotsConfig, ILogManager? logManager = null)
{
FlashbotsConfig = flashbotsConfig;
MergeConfig = new MergeConfig() { TerminalTotalDifficulty = "0" };
LogManager = logManager ?? LogManager;
}

public sealed override ILogManager LogManager { get; set; } = LimboLogs.Instance;

public ReadOnlyTxProcessingEnv CreateReadOnlyTxProcessingEnv()
{
ReadOnlyTxProcessingEnv = new ReadOnlyTxProcessingEnv(
WorldStateManager,
BlockTree,
SpecProvider,
LogManager
);
return ReadOnlyTxProcessingEnv;
}

protected override IBlockProcessor CreateBlockProcessor()
{
BlockValidator = CreateBlockValidator();
WithdrawalProcessor = new WithdrawalProcessor(State, LogManager);
IBlockProcessor prcessor = new BlockProcessor(
SpecProvider,
BlockValidator,
NoBlockRewards.Instance,
new BlockProcessor.BlockValidationTransactionsExecutor(TxProcessor, State),
State,
ReceiptStorage,
TxProcessor,
new BeaconBlockRootHandler(TxProcessor),
new BlockhashStore(SpecProvider, State),
LogManager,
WithdrawalProcessor
);

return prcessor;
}

protected IBlockValidator CreateBlockValidator()
{
PoSSwitcher = new PoSSwitcher(MergeConfig, SyncConfig.Default, new MemDb(), BlockTree, SpecProvider, new ChainSpec() { Genesis = Core.Test.Builders.Build.A.Block.WithDifficulty(0).TestObject }, LogManager);
ISealValidator SealValidator = new MergeSealValidator(PoSSwitcher, Always.Valid);
HeaderValidator = new MergeHeaderValidator(
PoSSwitcher,
new HeaderValidator(BlockTree, SealValidator, SpecProvider, LogManager),
BlockTree,
SpecProvider,
SealValidator,
LogManager
);

return new BlockValidator(
new TxValidator(SpecProvider.ChainId),
HeaderValidator,
Always.Valid,
SpecProvider,
LogManager
);
}

protected override async Task<TestBlockchain> Build(ISpecProvider? specProvider = null, UInt256? initialValues = null, bool addBlockOnStart = true)
{
TestBlockchain chain = await base.Build(specProvider, initialValues);
return chain;
}

public async Task<MergeTestBlockChain> Build(ISpecProvider? specProvider = null) =>
(MergeTestBlockChain)await Build(specProvider, null);

}
}
32 changes: 32 additions & 0 deletions src/Nethermind/Nethermind.Flashbots.Test/FlashbotsModuleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Threading.Tasks;
using Nethermind.Consensus.Processing;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Crypto;
using Nethermind.Flashbots.Modules.Flashbots;
using Nethermind.Int256;
using Nethermind.State;

namespace Nethermind.Flasbots.Test;

public partial class FlashbotsModuleTests
{

public async Task TestValidateBuilderSubmissionV3 ()
{
using MergeTestBlockChain chain = await CreateBlockChain();
ReadOnlyTxProcessingEnv readOnlyTxProcessingEnv = chain.CreateReadOnlyTxProcessingEnv();
IFlashbotsRpcModule rpc = CreateFlashbotsModule(chain, readOnlyTxProcessingEnv);
BlockHeader currentHeader = chain.BlockTree.Head.Header;
IWorldState State = chain.State;

UInt256 nonce = State.GetNonce(TestKeysAndAddress.TestAddr);

Transaction tx1 = new Transaction(

);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Nullable>annotations</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit.Analyzers">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Nethermind.JsonRpc.Test\Nethermind.JsonRpc.Test.csproj" />
<ProjectReference Include="..\Nethermind.Consensus\Nethermind.Consensus.csproj" />
<ProjectReference Include="..\Nethermind.Core.Test\Nethermind.Core.Test.csproj" />
<ProjectReference Include="..\Nethermind.Merge.Test\Nethermind.Merge.Test.csproj" />
<ProjectReference Include="..\Nethermind.Flashbots\Nethermind.Flashbots.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Flashbots/Flashbots.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public Task InitRpcModules()
_api.LogManager
);
ValidateSubmissionHandler validateSubmissionHandler = new ValidateSubmissionHandler(
_api.HeaderValidator?? throw new ArgumentNullException(nameof(_api.HeaderValidator)),
_api.BlockValidator ?? throw new ArgumentNullException(nameof(_api.BlockValidator)),
readOnlyTxProcessingEnv,
_flashbotsConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ValidateSubmissionHandler

private readonly ReadOnlyTxProcessingEnv _txProcessingEnv;
private readonly IBlockTree _blockTree;
private readonly IHeaderValidator _headerValidator;
private readonly IBlockValidator _blockValidator;
private readonly ILogger _logger;

Expand All @@ -44,10 +45,12 @@ public class ValidateSubmissionHandler
private readonly IReceiptStorage _receiptStorage = new InMemoryReceiptStorage();

public ValidateSubmissionHandler(
IHeaderValidator headerValidator,
IBlockValidator blockValidator,
ReadOnlyTxProcessingEnv txProcessingEnv,
IFlashbotsConfig flashbotsConfig)
{
_headerValidator = headerValidator;
_blockValidator = blockValidator;
_txProcessingEnv = txProcessingEnv;
_blockTree = _txProcessingEnv.BlockTree;
Expand Down Expand Up @@ -234,7 +237,7 @@ private bool ValidatePayload(Block block, Address feeRecipient, UInt256 expected

private bool ValidateBlockMetadata(Block block, long registerGasLimit, BlockHeader parentHeader, out string? error)
{
if (!HeaderValidator.ValidateHash(block.Header))
if (!_headerValidator.Validate(block.Header))
{
error = $"Invalid block header hash {block.Header.Hash}";
return false;
Expand Down Expand Up @@ -362,8 +365,9 @@ private BlockProcessor CreateBlockProcessor(IWorldState stateProvider, ITransact
new BlockProcessor.BlockValidationTransactionsExecutor(transactionProcessor, stateProvider),
stateProvider,
_receiptStorage,
transactionProcessor,
new BeaconBlockRootHandler(transactionProcessor),
new BlockhashStore(_txProcessingEnv.SpecProvider, stateProvider),
beaconBlockRootHandler: new BeaconBlockRootHandler(transactionProcessor),
logManager: _txProcessingEnv.LogManager,
withdrawalProcessor: new WithdrawalProcessor(stateProvider, _txProcessingEnv.LogManager!),
receiptsRootCalculator: new ReceiptsRootCalculator()
Expand Down
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.sln
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nethermind.ExternalSigner.P
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nethermind.Flashbots", "Nethermind.Flashbots\Nethermind.Flashbots.csproj", "{580DB104-AE89-444F-BD99-7FE0C84C615C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nethermind.Flasbots.Test", "Nethermind.Flashbots.Test\Nethermind.Flasbots.Test.csproj", "{370C4088-0DB9-401A-872F-E72E3272AB54}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -606,6 +608,10 @@ Global
{580DB104-AE89-444F-BD99-7FE0C84C615C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{580DB104-AE89-444F-BD99-7FE0C84C615C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{580DB104-AE89-444F-BD99-7FE0C84C615C}.Release|Any CPU.Build.0 = Release|Any CPU
{370C4088-0DB9-401A-872F-E72E3272AB54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{370C4088-0DB9-401A-872F-E72E3272AB54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{370C4088-0DB9-401A-872F-E72E3272AB54}.Release|Any CPU.ActiveCfg = Release|Any CPU
{370C4088-0DB9-401A-872F-E72E3272AB54}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 4eeeb9f

Please sign in to comment.