Skip to content

Commit

Permalink
feat: make IVoteMetadata.ValidatorPower nullable (skip changelog)
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Jun 10, 2024
1 parent ce8f7ec commit d251ea8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Libplanet.Explorer/GraphTypes/VoteType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public VoteType()
"ValidatorPublicKey",
description: "Public key of the validator which is subject of the vote.",
resolve: ctx => ctx.Source.ValidatorPublicKey);
Field<NonNullGraphType<BigIntGraphType>>(
Field<BigIntGraphType>(
"ValidatorPower",
description: "Power of the validator which is subject of the vote.",
resolve: ctx => ctx.Source.ValidatorPower);
Expand Down
4 changes: 3 additions & 1 deletion Libplanet.Types/Consensus/IVoteMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public interface IVoteMetadata

/// <summary>
/// The voting power of the validator that voted.
/// If null, the vote is legacy thus <see cref="ValidatorPower"/>
/// is not included in signature.
/// </summary>
BigInteger ValidatorPower { get; }
BigInteger? ValidatorPower { get; }

/// <summary>
/// The <see cref="VoteFlag"/> indicating the type of a <see cref="Vote"/>.
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Types/Consensus/Vote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private Vote(Bencodex.Types.Dictionary encoded)
public PublicKey ValidatorPublicKey => _metadata.ValidatorPublicKey;

/// <inheritdoc/>
public BigInteger ValidatorPower => _metadata.ValidatorPower;
public BigInteger? ValidatorPower => _metadata.ValidatorPower;

/// <inheritdoc/>
public VoteFlag Flag => _metadata.Flag;
Expand Down
20 changes: 14 additions & 6 deletions Libplanet.Types/Consensus/VoteMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public class VoteMetadata : IVoteMetadata, IEquatable<VoteMetadata>, IBencodable
/// <param name="validatorPublicKey">
/// <see cref="PublicKey"/> of the validator made the vote.
/// </param>
/// <param name="validatorPower">The voting power of the validator.</param>
/// <param name="validatorPower">The voting power of the validator.
/// If null is given, the vote is legacy thus <see cref="IVoteMetadata.ValidatorPower"/>
/// is not included in signature.</param>
/// <param name="flag"><see cref="VoteFlag"/> for the vote's status.</param>
/// <exception cref="ArgumentException">Thrown for any of the following reasons:
/// <list type="bullet">
Expand All @@ -69,7 +71,7 @@ public VoteMetadata(
BlockHash blockHash,
DateTimeOffset timestamp,
PublicKey validatorPublicKey,
BigInteger validatorPower,
BigInteger? validatorPower,
VoteFlag flag)
{
if (height < 0)
Expand All @@ -82,7 +84,7 @@ public VoteMetadata(
throw new ArgumentException(
$"Given {nameof(round)} cannot be negative: {round}");
}
else if (validatorPower <= 0)
else if (validatorPower is { } power && power <= 0)
{
var msg = $"Given {nameof(validatorPower)} cannot be negative " +
$"or equal to zero: {validatorPower}";
Expand Down Expand Up @@ -127,7 +129,9 @@ private VoteMetadata(Bencodex.Types.Dictionary bencoded)
CultureInfo.InvariantCulture),
validatorPublicKey: new PublicKey(
((Binary)bencoded[ValidatorPublicKeyKey]).ByteArray),
validatorPower: (Integer)bencoded[ValidatorPowerKey],
validatorPower: bencoded.ContainsKey(ValidatorPowerKey)
? (Integer)bencoded[ValidatorPowerKey]
: (Integer)(-1),
flag: (VoteFlag)(int)(Integer)bencoded[FlagKey])
{
}
Expand All @@ -149,7 +153,7 @@ private VoteMetadata(Bencodex.Types.Dictionary bencoded)
public PublicKey ValidatorPublicKey { get; }

/// <inheritdoc/>
public BigInteger ValidatorPower { get; }
public BigInteger? ValidatorPower { get; }

/// <inheritdoc/>
public VoteFlag Flag { get; }
Expand All @@ -167,14 +171,18 @@ public Bencodex.Types.IValue Bencoded
TimestampKey,
Timestamp.ToString(TimestampFormat, CultureInfo.InvariantCulture))
.Add(ValidatorPublicKeyKey, ValidatorPublicKey.Format(compress: true))
.Add(ValidatorPowerKey, ValidatorPower)
.Add(FlagKey, (long)Flag);

if (BlockHash is { } blockHash)
{
encoded = encoded.Add(BlockHashKey, blockHash.ByteArray);
}

if (ValidatorPower is { } power)
{
encoded = encoded.Add(ValidatorPowerKey, power);
}

return encoded;
}
}
Expand Down

0 comments on commit d251ea8

Please sign in to comment.