Skip to content

Commit

Permalink
Merge pull request planetarium#3813 from limebell/feature/nullable-vo…
Browse files Browse the repository at this point in the history
…tepower

Make `IVoteMetadata.ValidatorPower` nullable
  • Loading branch information
limebell committed Jun 10, 2024
2 parents ce8f7ec + 9e25244 commit c9ace8f
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 15 deletions.
6 changes: 4 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ Version DPoS
and its implementations. [[#3730]]
- (Libplanet.Action) Added `MaxGasPrice` property to `IActionContext`
interface and its implementations. [[#3762]]
- (Libplanet.Explorer) Added `ValidatorPower` field to `VoteType`. [[#3737]]
- (Libplanet.Explorer) Added `ValidatorPower` field to `VoteType`.
[[#3737], [#3813]]
- (Libplanet.Types) Added `ValidatorPower` property to `IVoteMetadata`
interface and its implementations. [[#3737]]
interface and its implementations. [[#3737], [#3813]]

### Backward-incompatible network protocol changes

Expand All @@ -55,6 +56,7 @@ Version DPoS
[#3748]: https://github.com/planetarium/libplanet/pull/3748
[#3762]: https://github.com/planetarium/libplanet/pull/3762
[#3764]: https://github.com/planetarium/libplanet/pull/3764
[#3813]: https://github.com/planetarium/libplanet/pull/3813


Version Sloth
Expand Down
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
7 changes: 4 additions & 3 deletions Libplanet.Net/Consensus/HeightVoteSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ public void AddVote(Vote vote)
vote);
}

if (_validatorSet.GetValidator(validatorKey).Power != vote.ValidatorPower)
if (vote.ValidatorPower is { } power &&
_validatorSet.GetValidator(validatorKey).Power != power)
{
const string msg = "ValidatorPower of the vote is not the same " +
"with the one in the validator set";
const string msg = "ValidatorPower of the vote is given and the value is " +
"not the same with the one in the validator set";
throw new InvalidVoteException(msg, vote);
}

Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Tests/Blocks/BlockCommitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public void ToHash()
{
var randomHash = new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size));
var keys = Enumerable.Range(0, 4).Select(_ => new PrivateKey()).ToList();
var votes = keys.Select(key =>
var votes = keys.Select((key, index) =>
new VoteMetadata(
1,
0,
randomHash,
DateTimeOffset.UtcNow,
key.PublicKey,
BigInteger.One,
index == 0 ? (BigInteger?)null : BigInteger.One,
VoteFlag.PreCommit).Sign(key))
.ToImmutableArray();
var blockCommit = new BlockCommit(1, 0, randomHash, votes);
Expand Down
11 changes: 11 additions & 0 deletions Libplanet.Tests/Consensus/VoteMetadataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public void Bencoded()
VoteFlag.PreCommit);
var decoded = new VoteMetadata(expected.Bencoded);
Assert.Equal(expected, decoded);

expected = new VoteMetadata(
1,
2,
hash,
DateTimeOffset.UtcNow,
key.PublicKey,
null,
VoteFlag.PreCommit);
decoded = new VoteMetadata(expected.Bencoded);
Assert.Equal(expected, decoded);
}
}
}
14 changes: 14 additions & 0 deletions Libplanet.Tests/Consensus/VoteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ public void Sign()
Vote vote = voteMetadata.Sign(privateKey);
Assert.True(
privateKey.PublicKey.Verify(_codec.Encode(voteMetadata.Bencoded), vote.Signature));

var nullPowerVoteMetadata = new VoteMetadata(
1,
2,
hash,
DateTimeOffset.UtcNow,
privateKey.PublicKey,
null,
VoteFlag.PreCommit);
Vote nullPowerVote = nullPowerVoteMetadata.Sign(privateKey);
Assert.True(
privateKey.PublicKey.Verify(
_codec.Encode(nullPowerVoteMetadata.Bencoded),
nullPowerVote.Signature));
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Types/Consensus/IVoteMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface IVoteMetadata
/// <summary>
/// The voting power of the validator that voted.
/// </summary>
BigInteger ValidatorPower { get; }
BigInteger? ValidatorPower { get; }

/// <summary>
/// The <see cref="VoteFlag"/> indicating the type of a <see cref="Vote"/>.
Expand Down
3 changes: 2 additions & 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 Expand Up @@ -166,6 +166,7 @@ public override string ToString()
var dict = new Dictionary<string, object>
{
{ "validator_public_key", ValidatorPublicKey.ToString() },
{ "validator_power", ValidatorPower.ToString() },

Check warning on line 169 in Libplanet.Types/Consensus/Vote.cs

View workflow job for this annotation

GitHub Actions / docs

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.

Check warning on line 169 in Libplanet.Types/Consensus/Vote.cs

View workflow job for this annotation

GitHub Actions / docs

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.

Check warning on line 169 in Libplanet.Types/Consensus/Vote.cs

View workflow job for this annotation

GitHub Actions / docs

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.

Check warning on line 169 in Libplanet.Types/Consensus/Vote.cs

View workflow job for this annotation

GitHub Actions / docs

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.

Check warning on line 169 in Libplanet.Types/Consensus/Vote.cs

View workflow job for this annotation

GitHub Actions / check-build

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.

Check warning on line 169 in Libplanet.Types/Consensus/Vote.cs

View workflow job for this annotation

GitHub Actions / check-build

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.

Check warning on line 169 in Libplanet.Types/Consensus/Vote.cs

View workflow job for this annotation

GitHub Actions / check-build

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.

Check warning on line 169 in Libplanet.Types/Consensus/Vote.cs

View workflow job for this annotation

GitHub Actions / check-build

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.
{ "vote_flag", Flag.ToString() },
{ "block_hash", BlockHash.ToString() },
{ "height", Height },
Expand Down
16 changes: 11 additions & 5 deletions Libplanet.Types/Consensus/VoteMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class VoteMetadata : IVoteMetadata, IEquatable<VoteMetadata>, IBencodable
BlockHash blockHash,
DateTimeOffset timestamp,
PublicKey validatorPublicKey,
BigInteger validatorPower,
BigInteger? validatorPower,
VoteFlag flag)
{
if (height < 0)
Expand All @@ -82,7 +82,7 @@ public class VoteMetadata : IVoteMetadata, IEquatable<VoteMetadata>, IBencodable
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 +127,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?)null,
flag: (VoteFlag)(int)(Integer)bencoded[FlagKey])
{
}
Expand All @@ -149,7 +151,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 +169,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 c9ace8f

Please sign in to comment.