Skip to content

Commit

Permalink
Merge pull request #3846 from greymistcube/refactor/context-state
Browse files Browse the repository at this point in the history
♻️ Changed `Context.Start()` to throw an `Exception` in an invalid state
  • Loading branch information
greymistcube authored Jun 25, 2024
2 parents 901ecb2 + a0966f4 commit 407eb80
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ To be released.
- (Libplanet.Net) Changed `Context()` to accept additional `BlockCommit?`
typed argument. Removed `lastCommit` parameter from `Context.Start()`.
[[#3833], [#3845]]
- (Libplanet.Net) Changed `Context.Start()` to throw an
`InvalidOperationException` when `Context` is not in a valid state.
[[#3846]]

### Backward-incompatible network protocol changes

Expand Down Expand Up @@ -58,6 +61,7 @@ To be released.
[#3811]: https://github.com/planetarium/libplanet/pull/3811
[#3833]: https://github.com/planetarium/libplanet/issues/3833
[#3845]: https://github.com/planetarium/libplanet/pull/3845
[#3846]: https://github.com/planetarium/libplanet/pull/3846


Version 4.6.1
Expand Down
9 changes: 9 additions & 0 deletions src/Libplanet.Net/Consensus/Context.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ public partial class Context
/// <summary>
/// Starts round #0 of consensus for <see cref="Height"/>.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when <see cref="Step"/>
/// is not <see cref="ConsensusStep.Default"/>.</exception>
public void Start()
{
if (Step != ConsensusStep.Default)
{
throw new InvalidOperationException(
$"Context cannot be started unless its state is {ConsensusStep.Default} " +
$"but its current step is {Step}");
}

_logger.Information(
"Starting context for height #{Height}",
Height);
Expand Down
26 changes: 22 additions & 4 deletions test/Libplanet.Net.Tests/Consensus/ContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ContextTest(ITestOutputHelper output)
}

[Fact(Timeout = Timeout)]
public async void StartAsProposer()
public async Task StartAsProposer()
{
var proposalSent = new AsyncAutoResetEvent();
var stepChangedToPreVote = new AsyncAutoResetEvent();
Expand Down Expand Up @@ -77,7 +77,7 @@ public async void StartAsProposer()
}

[Fact(Timeout = Timeout)]
public async void StartAsProposerWithLastCommit()
public async Task StartAsProposerWithLastCommit()
{
var stepChangedToPreVote = new AsyncAutoResetEvent();
ConsensusProposalMsg? proposal = null;
Expand Down Expand Up @@ -125,6 +125,24 @@ public async void StartAsProposerWithLastCommit()
Assert.Equal(lastCommit, proposed.LastCommit);
}

[Fact]
public async Task CannotStartTwice()
{
var stepChanged = new AsyncAutoResetEvent();
var (_, context) = TestUtils.CreateDummyContext();
context.StateChanged += (_, eventArgs) =>
{
if (eventArgs.Step == ConsensusStep.Propose)
{
stepChanged.Set();
}
};
context.Start();

await stepChanged.WaitAsync();
Assert.Throws<InvalidOperationException>(() => context.Start());
}

[Fact(Timeout = Timeout)]
public async Task CanAcceptMessagesAfterCommitFailure()
{
Expand Down Expand Up @@ -211,7 +229,7 @@ public async Task CanAcceptMessagesAfterCommitFailure()
VoteFlag.PreCommit).Sign(TestUtils.PrivateKeys[i])));
}

await Task.WhenAll(stepChangedToEndCommit.WaitAsync());
await stepChangedToEndCommit.WaitAsync();

// Check context has only three votes.
BlockCommit? commit = context.GetBlockCommit();
Expand Down Expand Up @@ -459,7 +477,7 @@ void BroadcastMessage(ConsensusMsg message) =>
/// </para>
/// </summary>
[Fact]
public async void CanReplaceProposal()
public async Task CanReplaceProposal()
{
var codec = new Codec();
var privateKeys = Enumerable.Range(0, 4).Select(_ => new PrivateKey()).ToArray();
Expand Down

0 comments on commit 407eb80

Please sign in to comment.