Skip to content

Commit

Permalink
test: fill up lacking unit test
Browse files Browse the repository at this point in the history
(cherry picked from commit 7981427)
  • Loading branch information
limebell committed Jun 29, 2023
1 parent 108cf3b commit 79e20b2
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions Libplanet.Net.Tests/Consensus/VoteSetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using Libplanet.Blocks;
using Libplanet.Consensus;
using Libplanet.Net.Consensus;
using Xunit;

namespace Libplanet.Net.Tests.Consensus
{
public class VoteSetTest
{
[Fact]
public void Majority()
{
var voteSet = new VoteSet(0, 0, VoteFlag.PreCommit, TestUtils.ValidatorSet);
Assert.False(voteSet.HasOneThirdsAny());
Assert.False(voteSet.HasTwoThirdsAny());
Assert.False(voteSet.HasTwoThirdsMajority());
Assert.False(voteSet.TwoThirdsMajority(out var hash0));
Assert.Equal(default, hash0);

var blockHash = new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size));
voteSet.AddVote(new VoteMetadata(
0,
0,
blockHash,
DateTimeOffset.UtcNow,
TestUtils.PrivateKeys[0].PublicKey,
VoteFlag.PreCommit).Sign(TestUtils.PrivateKeys[0]));
Assert.False(voteSet.HasOneThirdsAny());
Assert.False(voteSet.HasTwoThirdsAny());
Assert.False(voteSet.HasTwoThirdsMajority());
Assert.False(voteSet.TwoThirdsMajority(out var hash1));
Assert.Equal(default, hash1);

voteSet.AddVote(new VoteMetadata(
0,
0,
blockHash,
DateTimeOffset.UtcNow,
TestUtils.PrivateKeys[1].PublicKey,
VoteFlag.PreCommit).Sign(TestUtils.PrivateKeys[1]));
Assert.True(voteSet.HasOneThirdsAny());
Assert.False(voteSet.HasTwoThirdsAny());
Assert.False(voteSet.HasTwoThirdsMajority());
Assert.False(voteSet.TwoThirdsMajority(out var hash2));
Assert.Equal(default, hash2);

voteSet.AddVote(new VoteMetadata(
0,
0,
new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size)),
DateTimeOffset.UtcNow,
TestUtils.PrivateKeys[2].PublicKey,
VoteFlag.PreCommit).Sign(TestUtils.PrivateKeys[2]));
Assert.True(voteSet.HasOneThirdsAny());
Assert.True(voteSet.HasTwoThirdsAny());
Assert.False(voteSet.HasTwoThirdsMajority());
Assert.False(voteSet.TwoThirdsMajority(out var hash3));
Assert.Equal(default, hash3);

voteSet.AddVote(new VoteMetadata(
0,
0,
blockHash,
DateTimeOffset.UtcNow,
TestUtils.PrivateKeys[3].PublicKey,
VoteFlag.PreCommit).Sign(TestUtils.PrivateKeys[3]));
Assert.True(voteSet.HasOneThirdsAny());
Assert.True(voteSet.HasTwoThirdsAny());
Assert.True(voteSet.HasTwoThirdsMajority());
Assert.True(voteSet.TwoThirdsMajority(out var hash4));
Assert.Equal(blockHash, hash4);
}
}
}

0 comments on commit 79e20b2

Please sign in to comment.