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..453e68c447f 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")), + "70b510a01be2ac0fbe87f69000e2d2b07cbd4dacf45b644c09f16f1490aafba4")), message.Id); } diff --git a/test/Libplanet.Tests/Action/ActionEvaluatorTest.cs b/test/Libplanet.Tests/Action/ActionEvaluatorTest.cs index 7f7a4b118d6..64d9d208c0f 100644 --- a/test/Libplanet.Tests/Action/ActionEvaluatorTest.cs +++ b/test/Libplanet.Tests/Action/ActionEvaluatorTest.cs @@ -1,3 +1,4 @@ +#pragma warning disable MEN003 // Method is too long using System; using System.Collections; using System.Collections.Generic; @@ -532,6 +533,34 @@ DumbAction MakeAction(Address address, char identifier, Address? transferTo = nu (0, 0, new[] { "A", null, "C", null, null }, _txFx.Address1), // Adds "B" (0, 1, new[] { "A", "B", "C", null, null }, _txFx.Address1), // Adds "C" }; + +#if DEBUG + // This code was created by ilgyu. + // You can preview the result of the test in the debug console. + // If this test fails, you can copy the result from the debug console + // and paste it to the upper part of the test code. + System.Diagnostics.Trace.WriteLine("------- 1"); + foreach (var eval in evals) + { + int txIdx = block1Txs.Select( + (e, idx) => new { e, idx }).First( + x => x.e.Id.Equals(eval.InputContext.TxId)).idx; + int actionIdx = block1Txs[txIdx].Actions.Select( + (e, idx) => new { e, idx }).First( + x => x.e.Equals(eval.Action.PlainValue)).idx; + string updatedStates = "new[] { " + string.Join(", ", addresses.Select( + eval.OutputState.GetAccount(ReservedAddresses.LegacyAccount).GetState) + .Select(x => x is Text t ? '"' + t.Value + '"' : "null")) + " }"; + string signerIdx = "_txFx.Address" + (addresses.Select( + (e, idx) => new { e, idx }).First( + x => x.e.Equals(eval.InputContext.Signer)).idx + 1); + System.Diagnostics.Trace.WriteLine( + $"({txIdx}, {actionIdx}, {updatedStates}, {signerIdx}),"); + } + + System.Diagnostics.Trace.WriteLine("---------"); +#endif // DEBUG + Assert.Equal(expectations.Length, evals.Length); foreach (var (expect, eval) in expectations.Zip(evals, (x, y) => (x, y))) { @@ -649,10 +678,38 @@ 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), - (2, 0, new[] { "A,D", "B", "C", "E", "F" }, _txFx.Address3), + (0, 0, new[] { "A,D", "B", "C", null, null }, _txFx.Address1), + (2, 0, new[] { "A,D", "B", "C", null, "F" }, _txFx.Address3), + (1, 0, new[] { "A,D", "B", "C", "E", "F" }, _txFx.Address2), }; + +#if DEBUG + // This code was created by ilgyu. + // You can preview the result of the test in the debug console. + // If this test fails, you can copy the result from the debug console + // and paste it to the upper part of the test code. + System.Diagnostics.Trace.WriteLine("------- 2"); + foreach (var eval in evals) + { + int txIdx = block2Txs.Select( + (e, idx) => new { e, idx }).First( + x => x.e.Id.Equals(eval.InputContext.TxId)).idx; + int actionIdx = block2Txs[txIdx].Actions.Select( + (e, idx) => new { e, idx }).First( + x => x.e.Equals(eval.Action.PlainValue)).idx; + string updatedStates = "new[] { " + string.Join(", ", addresses.Select( + eval.OutputState.GetAccount(ReservedAddresses.LegacyAccount).GetState) + .Select(x => x is Text t ? '"' + t.Value + '"' : "null")) + " }"; + string signerIdx = "_txFx.Address" + (addresses.Select( + (e, idx) => new { e, idx }).First( + x => x.e.Equals(eval.InputContext.Signer)).idx + 1); + System.Diagnostics.Trace.WriteLine( + $"({txIdx}, {actionIdx}, {updatedStates}, {signerIdx}),"); + } + + System.Diagnostics.Trace.WriteLine("---------"); +#endif // DEBUG + Assert.Equal(expectations.Length, evals.Length); foreach (var (expect, eval) in expectations.Zip(evals, (x, y) => (x, y))) { 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(