Skip to content

Commit

Permalink
refactor: Refactor ValidateBlockCommitValidators methof of ValidatorSet
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Oct 22, 2024
1 parent 61ffe8c commit 5f5108e
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/Libplanet.Types/Consensus/ValidatorSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,30 +263,39 @@ public Validator GetProposer(long height, int round)
/// <remarks>
/// If <see cref="Vote.ValidatorPower"/> is null, power check is ignored.</remarks>
/// <param name="blockCommit">The <see cref="BlockCommit"/> to check.</param>
/// <exception cref="InvalidBlockCommitException">Thrown when some votes in the
/// <paramref name="blockCommit"/> does not have non-null power.</exception>
/// <exception cref="InvalidBlockCommitException">Thrown when validators from
/// <paramref name="blockCommit"/> is different from validators of this.</exception>
/// <exception cref="InvalidBlockCommitException">Thrown when vote's power in the
/// <paramref name="blockCommit"/> does not equal to validator power.</exception>
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"));
}
}

Expand Down

0 comments on commit 5f5108e

Please sign in to comment.