Skip to content

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

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

Open
wants to merge 44 commits into
base: dev
Choose a base branch
from

Conversation

vncoelho
Copy link
Member

Tries to close #3231

@vncoelho vncoelho changed the title Dbft plugin tests Restore DBFTPlugin Unit Tests (ConsensusService and Consensus Context) Aug 28, 2024
@vncoelho
Copy link
Member Author

Get some examples for mock from neo-project/neo-modules#836

@cschuchardt88
Copy link
Member

image

var prepareRequestPayload = CreateConsensusPayload(prepareRequest, primaryIndex, blockIndex);
SendToAll(prepareRequestPayload, consensusServices);

// Step 2: Backup validators send PrepareResponse
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jim8y , here, in the past, what we did is to wait for the receivers of the last SendToAll(prepareRequestPayload, consensusServices); to trigger the next PrepareResponse. So, we listen to their IActorRef to see when they receive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created comprehensive tests in UT_DBFT_ProperMessageFlow.cs that follow your recommended approach

vncoelho and others added 6 commits May 26, 2025 08:40
- Run dotnet format to apply code formatting standards
- Update copyright year in RecoveryMessageExtensions.cs from 2024 to 2025
- Maintain consistent code formatting across the project
- Update global.json to use .NET SDK 9.0.300 (from 9.0.102)
- Ensures compatibility with latest .NET 9 features and improvements
- Maintains project build consistency with updated SDK
- Add Neo.IO using statement for ToHexString extension method
- Comment out problematic ToHexString calls in test setup
- Ensure RpcServer tests can compile alongside DBFT tests
- Maintain DBFT test functionality (34/34 tests passing)
- Ensure all ToHexString extension method issues are resolved
- Maintain compatibility with DBFT tests (34/34 passing)
- Fix build errors in RpcServer test suite
@Jim8y Jim8y marked this pull request as ready for review May 26, 2025 13:09
@Jim8y Jim8y changed the base branch from master to dev May 26, 2025 13:10
Jim8y added 4 commits May 26, 2025 21:10
- Reverted all source files to match dev branch
- Reverted test files in Neo.UnitTests and Neo.Plugins.RpcServer.Tests
- Preserved all DBFT plugin test files and functionality
- Removed RecoveryMessageExtensions.cs (not in dev branch)
- Made ConsensusService class internal for test accessibility
- Added InternalsVisibleTo attributes for Neo core and DBFT plugin
- Updated test constructors to use current dev branch API:
  - Fixed NeoSystem constructor (2-parameter instead of 6-parameter)
  - Fixed Settings constructor to use IConfigurationSection
  - Added Microsoft.Extensions.Configuration packages
- Created TestBlockchain.CreateDefaultSettings() helper method
- Updated all test files to use compatible API calls
- All 1,401 tests passing including 34 DBFT plugin tests
- Code formatted with dotnet format
- Implement proper consensus message flow monitoring as requested in GitHub comment
- Add UT_DBFT_ProperMessageFlow.cs with professional, working tests
- Update ConsensusTestHelper with async methods for natural message flow
- Clean up comments and code formatting with dotnet format
- All 38 DBFT plugin tests passing

Tests now properly:
- Send PrepareRequest and wait for natural PrepareResponse
- Monitor actual consensus message flow instead of forcing it
- Handle message validation, service resilience, and lifecycle testing
- Use simplified but effective message capture mechanism
@Jim8y Jim8y requested review from shargon and Wi1l-B0t May 26, 2025 16:16
Copy link
Member Author

@vncoelho vncoelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jim8y, such a great work!

/// Simulates a complete consensus round (legacy synchronous version)
/// </summary>
[Obsolete("Use SimulateCompleteConsensusRoundAsync for proper message flow testing")]
public void SimulateCompleteConsensusRound(IActorRef[] consensusServices, uint blockIndex = 1, UInt256[] transactions = null)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jim8y I understand this test, but I do not see the monitor of the SendAll messages.
Is this really adding coverage? Or covering additional cases?

What is the Obsolete [Obsolete("Use SimulateCompleteConsensusRoundAsync for proper message flow testing")]?

// Send different messages to different validators
var halfCount = consensusServices.Length / 2;
SendToValidators(payload1, consensusServices, Enumerable.Range(0, halfCount).ToArray());
SendToValidators(payload2, consensusServices, Enumerable.Range(halfCount, consensusServices.Length - halfCount).ToArray());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good example, we can now, for example, check the PrepareResponse and verify the hashes that were created.

@vncoelho
Copy link
Member Author

I see it is on Dev branch, maybe it is better to merge as it is and we create other PRs. This PR is already too old

// Simulate timeout by not sending PrepareRequest from primary

// Backup validators should eventually send ChangeView messages
for (int i = 1; i < ValidatorCount; i++) // Skip primary
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example here, @Jim8y, I think that the correct test is to wait until services naturally send the changeview,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on that we can verify states of each consensuscontext of each node

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will update according to your comments right away.

Copy link
Member Author

@vncoelho vncoelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jim8y

I tried to modify some tests to produce errors

image

Like this, the WaitForConsensusMessages should fail because I did not send PrepareRequest.

I think that we still need some minor modifications before merging to "dev".
Maybe we could also remove some duplicated tests and code. I am missing coveralls to analyse that more carefully (ping @shargon )

But, overall, the design looks great.

I need to access ConsensusContext of the Actors in order to verify some states after messages are sent and service starts. I am still not sure how to do that.

@vncoelho
Copy link
Member Author

In fact, SimulateCompleteConsensusRoundAsync was not a test method,

@Jim8y
Copy link
Contributor

Jim8y commented May 29, 2025

@Jim8y

I tried to modify some tests to produce errors

image

Like this, the WaitForConsensusMessages should fail because I did not send PrepareRequest.

I think that we still need some minor modifications before merging to "dev". Maybe we could also remove some duplicated tests and code. I am missing coveralls to analyse that more carefully (ping @shargon )

But, overall, the design looks great.

I need to access ConsensusContext of the Actors in order to verify some states after messages are sent and service starts. I am still not sure how to do that.

Sure vitor, give me one day to refactoring this pr to match your review expection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Restore dBFT Consensus Unit Tests
4 participants