From 20462ef1b171c8911565f12b7eec58bd72c59361 Mon Sep 17 00:00:00 2001 From: s2quake Date: Tue, 22 Oct 2024 10:16:44 +0900 Subject: [PATCH] test: Add test code for additional changes --- .../Consensus/HeightVoteSetTest.cs | 17 +++++++++++++++ .../Messages/MessageTest.cs | 2 +- .../Action/ActionEvaluatorTest.cs | 4 ++-- .../Blocks/BlockMetadataTest.cs | 2 +- .../Blocks/PreEvaluationBlockHeaderTest.cs | 20 +++++++++--------- .../Consensus/ValidatorSetTest.cs | 2 +- test/Libplanet.Tests/TestUtils.cs | 21 ++++++++++++------- 7 files changed, 46 insertions(+), 22 deletions(-) diff --git a/test/Libplanet.Net.Tests/Consensus/HeightVoteSetTest.cs b/test/Libplanet.Net.Tests/Consensus/HeightVoteSetTest.cs index a17cebe7170..d5d97eb2784 100644 --- a/test/Libplanet.Net.Tests/Consensus/HeightVoteSetTest.cs +++ b/test/Libplanet.Net.Tests/Consensus/HeightVoteSetTest.cs @@ -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( + () => _heightVoteSet.AddVote(preVote)); + Assert.Equal("ValidatorPower of the vote cannot be null", exception.Message); + } + [Fact] public void GetCount() { diff --git a/test/Libplanet.Net.Tests/Messages/MessageTest.cs b/test/Libplanet.Net.Tests/Messages/MessageTest.cs index 106a1cf46b5..3d24f1230c8 100644 --- a/test/Libplanet.Net.Tests/Messages/MessageTest.cs +++ b/test/Libplanet.Net.Tests/Messages/MessageTest.cs @@ -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); } diff --git a/test/Libplanet.Tests/Action/ActionEvaluatorTest.cs b/test/Libplanet.Tests/Action/ActionEvaluatorTest.cs index 7f7a4b118d6..3ad465f8bb3 100644 --- a/test/Libplanet.Tests/Action/ActionEvaluatorTest.cs +++ b/test/Libplanet.Tests/Action/ActionEvaluatorTest.cs @@ -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); diff --git a/test/Libplanet.Tests/Blocks/BlockMetadataTest.cs b/test/Libplanet.Tests/Blocks/BlockMetadataTest.cs index 6c1e625bebc..1f1810421f6 100644 --- a/test/Libplanet.Tests/Blocks/BlockMetadataTest.cs +++ b/test/Libplanet.Tests/Blocks/BlockMetadataTest.cs @@ -207,7 +207,7 @@ public void DerivePreEvaluationHash() HashDigest hash = GenesisMetadata.DerivePreEvaluationHash(); AssertBytesEqual( - FromHex("4b9d55d06e4db1d693d6843f67b5e818fa819a8f29cde18582631b8c8f6f11cb"), + FromHex("9ff328716814fed03de454dfc0a9d9aeb0077ad0c393513bf29895a45ded13aa"), hash.ByteArray); } diff --git a/test/Libplanet.Tests/Blocks/PreEvaluationBlockHeaderTest.cs b/test/Libplanet.Tests/Blocks/PreEvaluationBlockHeaderTest.cs index cde6d1a6d9d..e7cea982f7d 100644 --- a/test/Libplanet.Tests/Blocks/PreEvaluationBlockHeaderTest.cs +++ b/test/Libplanet.Tests/Blocks/PreEvaluationBlockHeaderTest.cs @@ -181,8 +181,8 @@ public void VerifySignature() // Same as block1.MakeSignature(_contents.Block1Key, arbitraryHash) ImmutableArray validSig = ByteUtil.ParseHexToImmutable( - "304402203ed4c76c5196b0fe280ea0235ba05211cfd4a96d2660e2c6883d11ddb49c47" + - "e90220095958c7c019db2bb71e9f5fc857ff6c94c2cfdf8484f08050694451ddfe569c"); + "3045022100f975e902971092f16dbbb1fe6b7c956de648a8cd62346dbadc07e5fca4ce3" + + "07a02200987a349f0763efd0448659ed66c6bd0ad0971dd57fbb89c672aed592fbd70d6"); AssertBytesEqual( validSig, block1.MakeSignature(_contents.Block1Key, arbitraryHash)); @@ -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)) @@ -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) ) diff --git a/test/Libplanet.Tests/Consensus/ValidatorSetTest.cs b/test/Libplanet.Tests/Consensus/ValidatorSetTest.cs index 07136c03fa5..d46ba0502c5 100644 --- a/test/Libplanet.Tests/Consensus/ValidatorSetTest.cs +++ b/test/Libplanet.Tests/Consensus/ValidatorSetTest.cs @@ -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); diff --git a/test/Libplanet.Tests/TestUtils.cs b/test/Libplanet.Tests/TestUtils.cs index f695f0d5587..3cdb122ebcd 100644 --- a/test/Libplanet.Tests/TestUtils.cs +++ b/test/Libplanet.Tests/TestUtils.cs @@ -374,17 +374,24 @@ public static T ToAction(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) @@ -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(