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

Restore DBFTPlugin Unit Tests (ConsensusService and Consensus Context) #3473

Draft
wants to merge 27 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
eaa1cca
Initial comit
vncoelho Aug 28, 2024
45c6ffb
Fix paths
vncoelho Aug 28, 2024
c3fee39
Added `Neo.Plugins.DBFTPlugin.Tests` to `Neo` internals
cschuchardt88 Aug 28, 2024
926a64e
Added tests to github workflow
cschuchardt88 Aug 28, 2024
dd666b3
Akka deps
vncoelho Aug 28, 2024
dc3db66
Advancing
vncoelho Aug 28, 2024
48641f9
TestWallet is mocked
vncoelho Aug 28, 2024
941e4f6
Adding more steps
vncoelho Aug 28, 2024
08c8ee6
Merge branch 'master' into dbft-plugin-tests
vncoelho Aug 29, 2024
088c48e
Merge branch 'master' into dbft-plugin-tests
cschuchardt88 Sep 1, 2024
d1cf82c
Fixed Test mocking
cschuchardt88 Sep 1, 2024
6aa44d2
Timestamp testing
vncoelho Sep 2, 2024
626666e
Header and TestProbe
vncoelho Sep 2, 2024
9f7b3f8
format
vncoelho Sep 2, 2024
75300de
Huge advance! Test probe is now working. Now we go
vncoelho Sep 2, 2024
92cd78a
dotnet format
vncoelho Sep 2, 2024
0baef17
Header looks like to be correct now
vncoelho Sep 2, 2024
f95076c
Merge branch 'master' into dbft-plugin-tests
vncoelho Sep 9, 2024
417aab7
Workaround for dbft settings
vncoelho Sep 11, 2024
3a07b5c
Merge branch 'master' into dbft-plugin-tests
vncoelho Sep 12, 2024
b6dec42
build the mock system for solenode
Jim8y Oct 2, 2024
f5615cf
Merge branch 'master' into dbft-plugin-tests
vncoelho Oct 3, 2024
4a1b2af
Merge branch 'dbft-plugin-tests' of github.com:neo-project/neo into d…
Jim8y Oct 4, 2024
917e992
update to 7 nodes network mock system
Jim8y Oct 4, 2024
e842af0
Merge branch 'master' into dbft-plugin-tests
Jim8y Oct 4, 2024
3c8567d
Comment original tests of the PR
vncoelho Oct 7, 2024
19caf6e
Merge branch 'master' into dbft-plugin-tests
Jim8y Oct 15, 2024
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
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ jobs:
dotnet test ./tests/Neo.Plugins.RpcServer.Tests --output ./bin/tests/Neo.Plugins.RpcServer.Tests
dotnet test ./tests/Neo.Plugins.Storage.Tests --output ./bin/tests/Neo.Plugins.Storage.Tests
dotnet test ./tests/Neo.Plugins.ApplicationLogs.Tests --output ./bin/tests/Neo.Plugins.ApplicationLogs.Tests
dotnet test ./tests/Neo.Plugins.DBFTPlugin.Tests --output ./bin/tests/Neo.Plugins.DBFTPlugin.Tests

- name: Coveralls
if: matrix.os == 'ubuntu-latest'
Expand All @@ -109,6 +110,7 @@ jobs:
${{ github.workspace }}/tests/Neo.Plugins.Storage.Tests/TestResults/coverage.info
${{ github.workspace }}/tests/Neo.Plugins.ApplicationLogs.Tests/TestResults/coverage.info
${{ github.workspace }}/tests/Neo.Extensions.Tests/TestResults/coverage.info
${{ github.workspace }}/tests/Neo.Plugins.DBFTPlugin.Tests/TestResults/coverage.info

PublishPackage:
if: github.ref == 'refs/heads/master' && startsWith(github.repository, 'neo-project/')
Expand Down
9 changes: 8 additions & 1 deletion neo.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32516.85
MinimumVisualStudioVersion = 10.0.40219.1
Expand Down Expand Up @@ -82,6 +82,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Plugins.ApplicationLogs
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Extensions.Tests", "tests\Neo.Extensions.Tests\Neo.Extensions.Tests.csproj", "{77FDEE2E-9381-4BFC-B9E6-741EDBD6B90F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Plugins.DBFTPlugin.Tests", "tests\Neo.Plugins.DBFTPlugin.Tests\Neo.Plugins.DBFTPlugin.Tests.csproj", "{378132CA-0931-4ADD-A20F-D78AE991C8CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -228,6 +230,10 @@ Global
{77FDEE2E-9381-4BFC-B9E6-741EDBD6B90F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77FDEE2E-9381-4BFC-B9E6-741EDBD6B90F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{77FDEE2E-9381-4BFC-B9E6-741EDBD6B90F}.Release|Any CPU.Build.0 = Release|Any CPU
{378132CA-0931-4ADD-A20F-D78AE991C8CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{378132CA-0931-4ADD-A20F-D78AE991C8CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{378132CA-0931-4ADD-A20F-D78AE991C8CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{378132CA-0931-4ADD-A20F-D78AE991C8CD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -269,6 +275,7 @@ Global
{185ADAFC-BFC6-413D-BC2E-97F9FB0A8AF0} = {C2DC830A-327A-42A7-807D-295216D30DBB}
{8C866DC8-2E55-4399-9563-2F47FD4602EC} = {7F257712-D033-47FF-B439-9D4320D06599}
{77FDEE2E-9381-4BFC-B9E6-741EDBD6B90F} = {EDE05FA8-8E73-4924-BC63-DD117127EEE1}
{378132CA-0931-4ADD-A20F-D78AE991C8CD} = {7F257712-D033-47FF-B439-9D4320D06599}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BCBA19D9-F868-4C6D-8061-A2B91E06E3EC}
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<InternalsVisibleTo Include="Neo.SmartContract.TestEngine" />
<InternalsVisibleTo Include="Neo.Plugins.RpcServer.Tests" />
<InternalsVisibleTo Include="Neo.Plugins.OracleService.Tests" />
<InternalsVisibleTo Include="Neo.Plugins.DBFTPlugin.Tests" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions src/Neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ public NeoSystem(ProtocolSettings settings, IStoreProvider storageProvider, stri
Blockchain.Ask(new Blockchain.Initialize()).Wait();
}


internal NeoSystem(ProtocolSettings settings, IStoreProvider storageProvider, IActorRef localNode, IActorRef blockchain, IActorRef taskManager, IActorRef txRouter)
{
Settings = settings;
GenesisBlock = CreateGenesisBlock(settings);
this.storageProvider = storageProvider;
store = storageProvider.GetStore(null);
MemPool = new MemoryPool(this);

/* Unmerged change from project 'Neo(net8.0)'
Before:
Blockchain =blockchain;
After:
Blockchain = blockchain;
*/
Blockchain = blockchain;
LocalNode = localNode;
TaskManager = taskManager;
TxRouter = txRouter;
}

/// <summary>
/// Creates the genesis block for the NEO blockchain.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Persistence/MemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Neo.Persistence
/// </summary>
public class MemoryStore : IStore
{
private readonly ConcurrentDictionary<byte[], byte[]> _innerData = new(ByteArrayEqualityComparer.Default);
internal readonly ConcurrentDictionary<byte[], byte[]> _innerData = new(ByteArrayEqualityComparer.Default);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Delete(byte[] key)
Expand Down
11 changes: 8 additions & 3 deletions src/Plugins/DBFTPlugin/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

namespace Neo.Plugins.DBFTPlugin.Consensus
{
partial class ConsensusService : UntypedActor
public partial class ConsensusService : UntypedActor
{
public class Start { }
private class Timer { public uint Height; public byte ViewNumber; }
internal class Timer { public uint Height; public byte ViewNumber; }

private readonly ConsensusContext context;
internal readonly ConsensusContext context;
private readonly IActorRef localNode;
private readonly IActorRef taskManager;
private readonly IActorRef blockchain;
Expand All @@ -39,6 +39,8 @@ private class Timer { public uint Height; public byte ViewNumber; }
private uint block_received_index;
private bool started = false;

internal bool IsStarted => started;

/// <summary>
/// This will record the information from last scheduled timer
/// </summary>
Expand Down Expand Up @@ -81,6 +83,7 @@ private void OnPersistCompleted(Block block)

private void InitializeConsensus(byte viewNumber)
{
Console.WriteLine("InitializeConsensus...");
context.Reset(viewNumber);
if (viewNumber > 0)
Log($"View changed: view={viewNumber} primary={context.Validators[context.GetPrimaryIndex((byte)(viewNumber - 1u))]}", LogLevel.Warning);
Expand Down Expand Up @@ -114,6 +117,7 @@ private void InitializeConsensus(byte viewNumber)

protected override void OnReceive(object message)
{
Console.WriteLine("OnReceive some message");
if (message is Start)
{
if (started) return;
Expand Down Expand Up @@ -143,6 +147,7 @@ protected override void OnReceive(object message)

private void OnStart()
{
Console.WriteLine("OnStart - First Log ConsensusService");
Log("OnStart");
started = true;
if (!dbftSettings.IgnoreRecoveryLogs && context.Load())
Expand Down
4 changes: 4 additions & 0 deletions src/Plugins/DBFTPlugin/DBFTPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
</None>
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Neo.Plugins.DBFTPlugin.Tests" />
</ItemGroup>

</Project>
40 changes: 27 additions & 13 deletions src/Plugins/DBFTPlugin/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,35 @@ namespace Neo.Plugins.DBFTPlugin
{
public class Settings : PluginSettings
{
public string RecoveryLogs { get; }
public bool IgnoreRecoveryLogs { get; }
public bool AutoStart { get; }
public uint Network { get; }
public uint MaxBlockSize { get; }
public long MaxBlockSystemFee { get; }
public string RecoveryLogs { get; private set; }
public bool IgnoreRecoveryLogs { get; private set; }
public bool AutoStart { get; private set; }
public uint Network { get; private set; }
public uint MaxBlockSize { get; private set; }
public long MaxBlockSystemFee { get; private set; }

public Settings(IConfigurationSection section) : base(section)
// Modified constructor with default values
public Settings(IConfigurationSection section = null)
: base(section)
{
RecoveryLogs = section.GetValue("RecoveryLogs", "ConsensusState");
IgnoreRecoveryLogs = section.GetValue("IgnoreRecoveryLogs", false);
AutoStart = section.GetValue("AutoStart", false);
Network = section.GetValue("Network", 5195086u);
MaxBlockSize = section.GetValue("MaxBlockSize", 262144u);
MaxBlockSystemFee = section.GetValue("MaxBlockSystemFee", 150000000000L);
// Set default values
RecoveryLogs = "ConsensusState";
IgnoreRecoveryLogs = false;
AutoStart = false;
Network = 5195086u;
MaxBlockSize = 262144u;
MaxBlockSystemFee = 150000000000L;

// Override defaults if section is provided
if (section != null)
{
RecoveryLogs = section.GetValue("RecoveryLogs", RecoveryLogs);
IgnoreRecoveryLogs = section.GetValue("IgnoreRecoveryLogs", IgnoreRecoveryLogs);
AutoStart = section.GetValue("AutoStart", AutoStart);
Network = section.GetValue("Network", Network);
MaxBlockSize = section.GetValue("MaxBlockSize", MaxBlockSize);
MaxBlockSystemFee = section.GetValue("MaxBlockSystemFee", MaxBlockSystemFee);
}
}
}
}
25 changes: 25 additions & 0 deletions tests/Neo.Plugins.DBFTPlugin.Tests/CustomAutoPilot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// CustomAutoPilot.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Akka.Actor;
using Akka.TestKit;
using System;

namespace Neo.Plugins.DBFTPlugin.Tests;

internal class CustomAutoPilot(Action<IActorRef, object> action) : AutoPilot
{
public override AutoPilot Run(IActorRef sender, object message)
{
action(sender, message);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Neo.Plugins.DBFTPlugin.Tests</RootNamespace>
<AssemblyName>Neo.Plugins.DBFTPlugin.Tests</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.5.0" />
<PackageReference Include="Akka.TestKit" Version="1.5.26" />
<PackageReference Include="Akka.TestKit.Xunit2" Version="1.5.26" />
<PackageReference Include="NeoFS.API" Version="3.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Neo\Neo.csproj" />
<ProjectReference Include="..\..\src\Plugins\DBFTPlugin\DBFTPlugin.csproj" />
<ProjectReference Include="..\Neo.UnitTests\Neo.UnitTests.csproj" />
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions tests/Neo.Plugins.DBFTPlugin.Tests/TestBlockchain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// TestBlockchain.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Akka.Actor;
using Neo.Ledger;
using Neo.Persistence;
using System;

namespace Neo.Plugins.DBFTPlugin.Tests
{
public static class TestBlockchain
{
public static readonly NeoSystem TheNeoSystem;
public static readonly UInt160[] DefaultExtensibleWitnessWhiteList;
private static readonly MemoryStore Store = new();

internal class StoreProvider : IStoreProvider
{
public string Name => "TestProvider";

public IStore GetStore(string path) => Store;
}

static TestBlockchain()
{
Console.WriteLine("initialize NeoSystem");
TheNeoSystem = new NeoSystem(TestProtocolSettings.Default, new StoreProvider());
}

internal static void ResetStore()
{
Store.Reset();
TheNeoSystem.Blockchain.Ask(new Blockchain.Initialize()).Wait();
}

internal static DataCache GetTestSnapshot()
{
return TheNeoSystem.GetSnapshotCache().CloneCache();
}
}
}
21 changes: 21 additions & 0 deletions tests/Neo.Plugins.DBFTPlugin.Tests/TestMemoryStoreProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// TestMemoryStoreProvider.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Persistence;

namespace Neo.Plugins.DBFTPlugin.Tests;

public class TestMemoryStoreProvider(MemoryStore memoryStore) : IStoreProvider
{
public MemoryStore MemoryStore { get; init; } = memoryStore;
public string Name => nameof(MemoryStore);
public IStore GetStore(string path) => MemoryStore;
}
Loading
Loading