Skip to content

Commit

Permalink
feat: replace MessageLog to VoteSet
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Jun 27, 2023
1 parent f873720 commit f2086b1
Show file tree
Hide file tree
Showing 39 changed files with 1,834 additions and 875 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public async void UseLastCommitCacheIfHeightContextIsEmpty()
}

// Retry: This calculates delta time.
[RetryFact]
[RetryFact(10, Timeout = Timeout)]
public async void NewHeightDelay()
{
var newHeightDelay = TimeSpan.FromSeconds(1);
Expand Down
8 changes: 4 additions & 4 deletions Libplanet.Net.Tests/Consensus/ConsensusContextProposerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public async void IncreaseRoundWhenTimeout()
TestUtils.CreateVote(
TestUtils.PrivateKeys[2],
1,
hash: null,
hash: default,
flag: VoteFlag.PreVote)));

consensusContext.HandleMessage(
new ConsensusPreVoteMsg(
vote: TestUtils.CreateVote(
TestUtils.PrivateKeys[3],
1,
hash: null,
hash: default,
flag: VoteFlag.PreVote)));

await timeoutProcessed.WaitAsync();
Expand All @@ -74,15 +74,15 @@ public async void IncreaseRoundWhenTimeout()
TestUtils.CreateVote(
TestUtils.PrivateKeys[2],
1,
hash: null,
hash: default,
flag: VoteFlag.PreCommit)));

consensusContext.HandleMessage(
new ConsensusPreCommitMsg(
vote: TestUtils.CreateVote(
TestUtils.PrivateKeys[3],
1,
hash: null,
hash: default,
flag: VoteFlag.PreCommit)));

await timeoutProcessed.WaitAsync();
Expand Down
8 changes: 4 additions & 4 deletions Libplanet.Net.Tests/Consensus/ConsensusContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public async void NewHeightIncreasing()
Assert.NotNull(proposal?.BlockHash);

consensusContext.HandleMessage(new ConsensusPreCommitMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[0], 3, hash: proposal?.BlockHash, flag: VoteFlag.PreCommit)));
TestUtils.PrivateKeys[0], 3, hash: proposal.BlockHash, flag: VoteFlag.PreCommit)));
consensusContext.HandleMessage(new ConsensusPreCommitMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[1], 3, hash: proposal?.BlockHash, flag: VoteFlag.PreCommit)));
TestUtils.PrivateKeys[1], 3, hash: proposal.BlockHash, flag: VoteFlag.PreCommit)));
consensusContext.HandleMessage(new ConsensusPreCommitMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 3, hash: proposal?.BlockHash, flag: VoteFlag.PreCommit)));
TestUtils.PrivateKeys[2], 3, hash: proposal.BlockHash, flag: VoteFlag.PreCommit)));

// Waiting for commit.
await heightThreeStepChangedToEndCommit.WaitAsync();
Expand Down Expand Up @@ -229,7 +229,7 @@ public async void VoteSetGetOnlyProposeCommitHash()
TestUtils.PrivateKeys[x],
1,
0,
proposal?.BlockHash,
proposal.BlockHash,
VoteFlag.PreCommit)));

foreach (var vote in votes)
Expand Down
53 changes: 27 additions & 26 deletions Libplanet.Net.Tests/Consensus/ContextNonProposerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public async Task EnterPreVoteBlockOneThird()

context.Start();
context.ProduceMessage(
TestUtils.CreateConsensusPropose(
block, TestUtils.PrivateKeys[2], round: 1));
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 1, 1, hash: block.Hash, flag: VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
Expand Down Expand Up @@ -162,7 +163,8 @@ public async void EnterPreCommitNilTwoThird()
};
context.MessageBroadcasted += (_, message) =>
{
if (message is ConsensusPreCommitMsg preCommitMsg && preCommitMsg.BlockHash is null)
if (message is ConsensusPreCommitMsg preCommitMsg &&
preCommitMsg.BlockHash.Equals(default))
{
preCommitSent.Set();
}
Expand All @@ -174,15 +176,15 @@ public async void EnterPreCommitNilTwoThird()
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[1], 1, 0, hash: null, VoteFlag.PreVote)));
TestUtils.PrivateKeys[1], 1, 0, hash: default, VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 1, 0, hash: null, VoteFlag.PreVote)));
TestUtils.PrivateKeys[2], 1, 0, hash: default, VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[3], 1, 0, hash: null, VoteFlag.PreVote)));
TestUtils.PrivateKeys[3], 1, 0, hash: default, VoteFlag.PreVote)));

await Task.WhenAll(preCommitSent.WaitAsync(), stepChangedToPreCommit.WaitAsync());
Assert.Equal(ConsensusStep.PreCommit, context.Step);
Expand Down Expand Up @@ -212,7 +214,7 @@ public async Task EnterPreVoteNilOnInvalidBlockHeader()
};
context.MessageBroadcasted += (_, message) =>
{
if (message is ConsensusPreVoteMsg vote && vote.PreVote.BlockHash is null)
if (message is ConsensusPreVoteMsg vote && vote.PreVote.BlockHash.Equals(default))
{
nilPreVoteSent.Set();
}
Expand Down Expand Up @@ -285,7 +287,7 @@ public async Task EnterPreVoteNilOnInvalidBlockContent()
};
context.MessageBroadcasted += (_, message) =>
{
if (message is ConsensusPreVoteMsg vote && vote.PreVote.BlockHash is null)
if (message is ConsensusPreVoteMsg vote && vote.PreVote.BlockHash.Equals(default))
{
nilPreVoteSent.Set();
}
Expand Down Expand Up @@ -347,12 +349,13 @@ public async Task EnterPreVoteNilOnInvalidAction()
};
context.MessageBroadcasted += (_, message) =>
{
if (message is ConsensusPreVoteMsg vote && vote.PreVote.BlockHash is null)
if (message is ConsensusPreVoteMsg vote && vote.PreVote.BlockHash.Equals(default))
{
nilPreVoteSent.Set();
}
else if (
message is ConsensusPreCommitMsg commit && commit.PreCommit.BlockHash is null)
message is ConsensusPreCommitMsg commit &&
commit.PreCommit.BlockHash.Equals(default))
{
nilPreCommitSent.Set();
}
Expand Down Expand Up @@ -404,11 +407,11 @@ public async Task EnterPreVoteNilOnInvalidAction()
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 1, 0, null, VoteFlag.PreVote)));
TestUtils.PrivateKeys[2], 1, 0, default, VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[3], 1, 0, null, VoteFlag.PreVote)));
TestUtils.PrivateKeys[3], 1, 0, default, VoteFlag.PreVote)));
await nilPreCommitSent.WaitAsync();
Assert.Equal(ConsensusStep.PreCommit, context.Step);
}
Expand All @@ -431,15 +434,13 @@ public async Task EnterPreVoteNilOneThird()
context.Start();

context.ProduceMessage(
TestUtils.CreateConsensusPropose(
block, TestUtils.PrivateKeys[1], round: 0));
context.ProduceMessage(
TestUtils.CreateConsensusPropose(
block, TestUtils.PrivateKeys[2], round: 1));
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 1, 1, hash: default, flag: VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[3], 1, 1, hash: null, flag: VoteFlag.PreVote)));
TestUtils.PrivateKeys[3], 1, 1, hash: default, flag: VoteFlag.PreVote)));

await stepChangedToRoundOnePreVote.WaitAsync();
Assert.Equal(ConsensusStep.PreVote, context.Step);
Expand Down Expand Up @@ -511,21 +512,21 @@ public async Task UponRulesCheckAfterTimeout()
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 1, 0, hash: null, flag: VoteFlag.PreVote)));
TestUtils.PrivateKeys[2], 1, 0, hash: default, flag: VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[3], 1, 0, hash: null, flag: VoteFlag.PreVote)));
TestUtils.PrivateKeys[3], 1, 0, hash: default, flag: VoteFlag.PreVote)));

// Two additional votes should be enough to trigger precommit timeout timer.
context.ProduceMessage(
new ConsensusPreCommitMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 1, 0, hash: null, flag: VoteFlag.PreCommit)));
TestUtils.PrivateKeys[2], 1, 0, hash: default, flag: VoteFlag.PreCommit)));
context.ProduceMessage(
new ConsensusPreCommitMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[3], 1, 0, hash: null, flag: VoteFlag.PreCommit)));
TestUtils.PrivateKeys[3], 1, 0, hash: default, flag: VoteFlag.PreCommit)));

context.Start();

Expand Down Expand Up @@ -564,11 +565,11 @@ public async Task TimeoutPreVote()
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 1, 0, hash: null, flag: VoteFlag.PreVote)));
TestUtils.PrivateKeys[2], 1, 0, hash: default, flag: VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[3], 1, 0, hash: null, flag: VoteFlag.PreVote)));
TestUtils.PrivateKeys[3], 1, 0, hash: default, flag: VoteFlag.PreVote)));

// Wait for timeout.
await timeoutProcessed.WaitAsync();
Expand Down Expand Up @@ -598,10 +599,10 @@ public async Task TimeoutPreCommit()
TestUtils.PrivateKeys[1], 1, 0, hash: block.Hash, flag: VoteFlag.PreCommit)));
context.ProduceMessage(
new ConsensusPreCommitMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 1, 0, hash: null, flag: VoteFlag.PreCommit)));
TestUtils.PrivateKeys[2], 1, 0, hash: default, flag: VoteFlag.PreCommit)));
context.ProduceMessage(
new ConsensusPreCommitMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[3], 1, 0, hash: null, flag: VoteFlag.PreCommit)));
TestUtils.PrivateKeys[3], 1, 0, hash: default, flag: VoteFlag.PreCommit)));

// Wait for timeout.
await timeoutProcessed.WaitAsync();
Expand Down
27 changes: 13 additions & 14 deletions Libplanet.Net.Tests/Consensus/ContextProposerTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading.Tasks;
using Libplanet.Blocks;
using Libplanet.Consensus;
Expand Down Expand Up @@ -57,16 +56,16 @@ public async Task EnterPreCommitNil()
context.Start();
context.ProduceMessage(
new ConsensusPreVoteMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[0], 1, hash: null, flag: VoteFlag.PreVote)));
TestUtils.PrivateKeys[0], 1, hash: default, flag: VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 1, hash: null, flag: VoteFlag.PreVote)));
TestUtils.PrivateKeys[2], 1, hash: default, flag: VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[3], 1, hash: null, flag: VoteFlag.PreVote)));
TestUtils.PrivateKeys[3], 1, hash: default, flag: VoteFlag.PreVote)));

await Task.WhenAll(preCommitSent.WaitAsync(), stepChangedToPreCommit.WaitAsync());
Assert.Null(preCommit?.BlockHash);
Assert.Equal(default(BlockHash), preCommit?.BlockHash);
Assert.Equal(1, context.Height);
Assert.Equal(0, context.Round);
Assert.Equal(ConsensusStep.PreCommit, context.Step);
Expand Down Expand Up @@ -113,19 +112,19 @@ public async void EnterPreCommitBlock()
new ConsensusPreVoteMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[0],
1,
hash: proposal?.BlockHash,
hash: proposal.BlockHash,
flag: VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[2],
1,
hash: proposal?.BlockHash,
hash: proposal.BlockHash,
flag: VoteFlag.PreVote)));
context.ProduceMessage(
new ConsensusPreVoteMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[3],
1,
hash: proposal?.BlockHash,
hash: proposal.BlockHash,
flag: VoteFlag.PreVote)));

await Task.WhenAll(preCommitSent.WaitAsync(), stepChangedToPreCommit.WaitAsync());
Expand Down Expand Up @@ -153,15 +152,15 @@ public async void EnterNewRoundNil()
context.ProduceMessage(
new ConsensusPreCommitMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[0], 1, hash: null, flag: VoteFlag.PreCommit)));
TestUtils.PrivateKeys[0], 1, hash: default, flag: VoteFlag.PreCommit)));
context.ProduceMessage(
new ConsensusPreCommitMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 1, hash: null, flag: VoteFlag.PreCommit)));
TestUtils.PrivateKeys[2], 1, hash: default, flag: VoteFlag.PreCommit)));
context.ProduceMessage(
new ConsensusPreCommitMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[3], 1, hash: null, flag: VoteFlag.PreCommit)));
TestUtils.PrivateKeys[3], 1, hash: default, flag: VoteFlag.PreCommit)));

await roundChangedToOne.WaitAsync();
Assert.Equal(1, context.Height);
Expand Down Expand Up @@ -242,7 +241,7 @@ public async void EnterPreVoteNil()
};
context.MessageBroadcasted += (_, message) =>
{
if (message is ConsensusPreVoteMsg vote && vote.PreVote.BlockHash is null)
if (message is ConsensusPreVoteMsg vote && vote.PreVote.BlockHash.Equals(default))
{
nilPreVoteSent.Set();
}
Expand Down Expand Up @@ -342,8 +341,8 @@ public async void VoteNilOnSelfProposedInvalidBlock()
(Bencodex.Types.Dictionary)codec.Decode(proposal?.Proposal.MarshaledBlock!));
Assert.Equal(context.Height + 1, proposedBlock.Index);
await preVoteSent.WaitAsync();
Assert.Null(preVote?.BlockHash);
Assert.Null(preVote?.PreVote.BlockHash);
Assert.Equal(default(BlockHash), preVote?.BlockHash);
Assert.Equal(default(BlockHash), preVote?.PreVote.BlockHash);
}
}
}
Loading

0 comments on commit f2086b1

Please sign in to comment.