Skip to content

Commit

Permalink
test: Add test code for additional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Oct 22, 2024
1 parent cf2feb6 commit 20462ef
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 22 deletions.
17 changes: 17 additions & 0 deletions test/Libplanet.Net.Tests/Consensus/HeightVoteSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ public void CannotAddMultipleVotesPerRoundPerValidator()
() => _heightVoteSet.AddVote(preCommit1));
}

[Fact]
public void CannotAddVoteWithoutValidatorPower()
{
var preVote = new VoteMetadata(
2,
0,
default,
DateTimeOffset.UtcNow,
TestUtils.PrivateKeys[0].PublicKey,
null,
VoteFlag.PreVote).Sign(TestUtils.PrivateKeys[0]);

var exception = Assert.Throws<InvalidVoteException>(
() => _heightVoteSet.AddVote(preVote));
Assert.Equal("ValidatorPower of the vote cannot be null", exception.Message);
}

[Fact]
public void GetCount()
{
Expand Down
2 changes: 1 addition & 1 deletion test/Libplanet.Net.Tests/Messages/MessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void GetId()
var message = new BlockHeaderMsg(genesis.Hash, genesis.Header);
Assert.Equal(
new MessageId(ByteUtil.ParseHex(
"1d4296f8e28bfc873a5e72cbbd17454d7cf2dbee86c2481e4876e236f8ae2dee")),
"cdbba63b249a138675ad881cbdffe70b5ad15b156a924f5cf120010e0aab3e74")),
message.Id);
}

Expand Down
4 changes: 2 additions & 2 deletions test/Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,8 @@ DumbAction MakeAction(Address address, char identifier, Address? transferTo = nu
// have to be updated, since the order may change due to different PreEvaluationHash.
expectations = new (int TxIdx, int ActionIdx, string[] UpdatedStates, Address Signer)[]
{
(1, 0, new[] { "A", "B", "C", "E", null }, _txFx.Address2),
(0, 0, new[] { "A,D", "B", "C", "E", null }, _txFx.Address1),
(0, 0, new[] { "A,D", "B", "C", null, null }, _txFx.Address1),
(1, 0, new[] { "A,D", "B", "C", "E", null }, _txFx.Address2),
(2, 0, new[] { "A,D", "B", "C", "E", "F" }, _txFx.Address3),
};
Assert.Equal(expectations.Length, evals.Length);
Expand Down
2 changes: 1 addition & 1 deletion test/Libplanet.Tests/Blocks/BlockMetadataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void DerivePreEvaluationHash()

HashDigest<SHA256> hash = GenesisMetadata.DerivePreEvaluationHash();
AssertBytesEqual(
FromHex("4b9d55d06e4db1d693d6843f67b5e818fa819a8f29cde18582631b8c8f6f11cb"),
FromHex("9ff328716814fed03de454dfc0a9d9aeb0077ad0c393513bf29895a45ded13aa"),
hash.ByteArray);
}

Expand Down
20 changes: 10 additions & 10 deletions test/Libplanet.Tests/Blocks/PreEvaluationBlockHeaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public void VerifySignature()

// Same as block1.MakeSignature(_contents.Block1Key, arbitraryHash)
ImmutableArray<byte> validSig = ByteUtil.ParseHexToImmutable(
"304402203ed4c76c5196b0fe280ea0235ba05211cfd4a96d2660e2c6883d11ddb49c47" +
"e90220095958c7c019db2bb71e9f5fc857ff6c94c2cfdf8484f08050694451ddfe569c");
"3045022100f975e902971092f16dbbb1fe6b7c956de648a8cd62346dbadc07e5fca4ce3" +
"07a02200987a349f0763efd0448659ed66c6bd0ad0971dd57fbb89c672aed592fbd70d6");
AssertBytesEqual(
validSig,
block1.MakeSignature(_contents.Block1Key, arbitraryHash));
Expand Down Expand Up @@ -215,22 +215,22 @@ public void DeriveBlockHash()
_contents.GenesisMetadata,
_contents.GenesisMetadata.DerivePreEvaluationHash());
AssertBytesEqual(
fromHex("6f8f1d4ca79463e85f56b2927ac4414bd8b0ac86942a2af44fa71ac10c61c3d5"),
fromHex("d790aa3b2d985d58e6fe6547336ca9d2bfdb749a27cd58a17dbfd0c6880da8e3"),
genesis.DeriveBlockHash(default, null)
);
AssertBytesEqual(
fromHex("59a9d5a9f3de4859f557437f3667f91cda146b6e980bda8cb4f0c1b9eff3bd43"),
fromHex("47b5227dfdd99af4faf9ae9e82ef3b615063179d275081eae4c122685bbf7dcb"),
genesis.DeriveBlockHash(
default,
genesis.MakeSignature(_contents.GenesisKey, default)
)
);
AssertBytesEqual(
fromHex("0b5ea8cf0a678075b9438cdf7d0a01bf61ec62c05d9320c597a255f63196f437"),
fromHex("2c45bb52e4c7d79caa28da9b63ec0f492262836c975bfa5bb27f62e96f2aad99"),
genesis.DeriveBlockHash(arbitraryHash, null)
);
AssertBytesEqual(
fromHex("8f8e5bdcbff809c89fcd295adba98c194babacb6ed69639b13cdc1b990b6c8e0"),
fromHex("e985fcdce3f73dee90a4eaec9399283f856bb6f9326e4300bbe1d6126ff7ad55"),
genesis.DeriveBlockHash(
arbitraryHash,
genesis.MakeSignature(_contents.GenesisKey, arbitraryHash))
Expand All @@ -240,19 +240,19 @@ public void DeriveBlockHash()
_contents.Block1Metadata,
_contents.Block1Metadata.DerivePreEvaluationHash());
AssertBytesEqual(
fromHex("b15d91c7df2b6e568110dc0e0547e79218645fef159d90ca87ea6d347c814be7"),
fromHex("ade696a646c9e4321cc90160807cba3d15d7cd28556d2dfb4103e8730a46038c"),
block1.DeriveBlockHash(default, null)
);
AssertBytesEqual(
fromHex("de112dcbe27aa0f040ee9dc93c8681bb58b514e52f769f72d60cbaaf203962e8"),
fromHex("b3157a151d2168653e21ffc850f9d1a96bca6310275cccbeb9bd705f67c2e1c9"),
block1.DeriveBlockHash(default, block1.MakeSignature(_contents.Block1Key, default))
);
AssertBytesEqual(
fromHex("1c3481fade04f4f82734f497fcdafb85745134506eacac288acb25f5d989be06"),
fromHex("3fd4ee37ed2fc5dae5a9533984f06b3975e176bdaa70689a3c14acd8b4ea384d"),
block1.DeriveBlockHash(arbitraryHash, null)
);
AssertBytesEqual(
fromHex("37cc2ae1e59e01192d14dc6d09b9d52e4fbc8cdf11db2fa181f000fd2c00b6c1"),
fromHex("83ceb4d1e5bbc385daaebfd044a5e4ba65bf1d8b63ef0aabe4d68fc5642b4516"),
block1.DeriveBlockHash(
arbitraryHash, block1.MakeSignature(_contents.Block1Key, arbitraryHash)
)
Expand Down
2 changes: 1 addition & 1 deletion test/Libplanet.Tests/Consensus/ValidatorSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void ValidateBlockCommitValidators()
var blockCommitWithUnorderedVotes =
new BlockCommit(height, round, hash, unorderedVotes);
var blockCommitWithInvalidPowerVotes =
new BlockCommit(height, round, hash, unorderedVotes);
new BlockCommit(height, round, hash, invalidPowerVotes);
var blockCommitWithInsufficientVotes =
new BlockCommit(height, round, hash, orderedVotes.Take(5).ToImmutableArray());
var validBlockCommit = new BlockCommit(height, round, hash, orderedVotes);
Expand Down
21 changes: 14 additions & 7 deletions test/Libplanet.Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,17 +374,24 @@ public static T ToAction<T>(IValue plainValue)

public static BlockCommit CreateBlockCommit(
Block block,
bool deterministicTimestamp = false) =>
block.Index > 0 &&
block.ProtocolVersion >= BlockMetadata.PBFTProtocolVersion
? CreateBlockCommit(block.Hash, block.Index, 0, deterministicTimestamp)
: null;
bool deterministicTimestamp = false)
{
if (block.Index <= 0 || block.ProtocolVersion < BlockMetadata.PBFTProtocolVersion)
{
return null;
}

var useValidatorPower = block.ProtocolVersion >= BlockMetadata.EvidenceProtocolVersion;
return CreateBlockCommit(
block.Hash, block.Index, 0, deterministicTimestamp, useValidatorPower);
}

public static BlockCommit CreateBlockCommit(
BlockHash blockHash,
long height,
int round,
bool deterministicTimestamp = false)
bool deterministicTimestamp = false,
bool useValidatorPower = true)
{
// Index #1 block cannot have lastCommit: There was no consensus of genesis block.
if (height == 0)
Expand All @@ -400,7 +407,7 @@ public static BlockCommit CreateBlockCommit(
blockHash,
deterministicTimestamp ? DateTimeOffset.UnixEpoch : DateTimeOffset.UtcNow,
key.PublicKey,
ValidatorSet.GetValidator(key.PublicKey).Power,
useValidatorPower ? ValidatorSet.GetValidator(key.PublicKey).Power : null,
VoteFlag.PreCommit).Sign(key)).ToImmutableArray();

return new BlockCommit(
Expand Down

0 comments on commit 20462ef

Please sign in to comment.