From 5f5108e68967db43bea1070ac3a9e11aca5aacf4 Mon Sep 17 00:00:00 2001 From: s2quake Date: Tue, 22 Oct 2024 10:15:37 +0900 Subject: [PATCH] refactor: Refactor ValidateBlockCommitValidators methof of ValidatorSet --- src/Libplanet.Types/Consensus/ValidatorSet.cs | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Libplanet.Types/Consensus/ValidatorSet.cs b/src/Libplanet.Types/Consensus/ValidatorSet.cs index f323e222fc8..a3832e3aa7b 100644 --- a/src/Libplanet.Types/Consensus/ValidatorSet.cs +++ b/src/Libplanet.Types/Consensus/ValidatorSet.cs @@ -263,30 +263,39 @@ public Validator GetProposer(long height, int round) /// /// If is null, power check is ignored. /// The to check. - /// Thrown when some votes in the - /// does not have non-null power. /// Thrown when validators from /// is different from validators of this. + /// Thrown when vote's power in the + /// does not equal to validator power. public void ValidateBlockCommitValidators(BlockCommit blockCommit) { - ValidatorSet validatorSetFromCommit = new ValidatorSet(blockCommit.Votes.Select( - v => v.ValidatorPower is BigInteger power - ? new Validator(v.ValidatorPublicKey, power) - : throw new InvalidBlockCommitException( - "All votes in the block commit after block protocol version 10 " + - "must have power.")).ToList()); - - if (!Equals(validatorSetFromCommit)) + if (!Validators.Select(validator => validator.PublicKey) + .SequenceEqual( + blockCommit.Votes.Select(vote => vote.ValidatorPublicKey).ToList())) { throw new InvalidBlockCommitException( $"BlockCommit of BlockHash {blockCommit.BlockHash} " + $"has different validator set with chain state's validator set: \n" + $"in states | \n " + Validators.Aggregate( - string.Empty, (s, v) => s + v + ", \n") + + string.Empty, (s, v) => s + v.PublicKey + ", \n") + $"in blockCommit | \n " + - validatorSetFromCommit.Validators.Aggregate( - string.Empty, (s, v) => s + v + ", \n")); + blockCommit.Votes.Aggregate( + string.Empty, (s, v) => s + v.ValidatorPublicKey + ", \n")); + } + + if (!blockCommit.Votes.All( + v => v.ValidatorPower == GetValidator(v.ValidatorPublicKey).Power)) + { + throw new InvalidBlockCommitException( + $"BlockCommit of BlockHash {blockCommit.BlockHash} " + + $"has different validator power with chain state's validator set: \n" + + $"in states | \n " + + Validators.Aggregate( + string.Empty, (s, v) => s + v.Power + ", \n") + + $"in blockCommit | \n " + + blockCommit.Votes.Aggregate( + string.Empty, (s, v) => s + v.ValidatorPower + ", \n")); } }