Skip to content

Commit

Permalink
Merge pull request #3737 from limebell/feature/vote-power
Browse files Browse the repository at this point in the history
Implement Vote Power
  • Loading branch information
limebell authored Apr 16, 2024
2 parents 1398e6b + 10e7781 commit 1620267
Show file tree
Hide file tree
Showing 49 changed files with 699 additions and 168 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Version DPoS
parameters instead of the `policyBlockActionGetter` parameter. [[#3701]]
- (Libplanet.Action) Added `SetValidatorSet` method to `IAccount` interface
and its implementations. [[#3730]]
- (Libplanet.Explorer) Added `ValidatorPower` field to `VoteType`. [[#3737]]
- (Libplanet.Types) Added `ValidatorPower` property to `IVoteMetadata`
interface and its implementations. [[#3737]]

### Backward-incompatible network protocol changes

Expand All @@ -39,6 +42,7 @@ Version DPoS

[#3701]: https://github.com/planetarium/libplanet/pull/3701
[#3730]: https://github.com/planetarium/libplanet/pull/3730
[#3737]: https://github.com/planetarium/libplanet/pull/3737


Version 4.1.0
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Action.Tests/ActionContextTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System.Numerics;
using Libplanet.Action.State;
using Libplanet.Action.Tests.Mocks;
using Libplanet.Crypto;
Expand Down Expand Up @@ -35,6 +36,7 @@ public ActionContextTest()
hash,
DateTimeOffset.UtcNow,
key.PublicKey,
BigInteger.One,
VoteFlag.PreCommit).Sign(key),
}.ToImmutableArray());
}
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Action.Tests/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System.Numerics;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Action.Tests.Common;
Expand Down Expand Up @@ -46,6 +47,7 @@ public void Constructor()
hash,
DateTimeOffset.UtcNow,
key.PublicKey,
BigInteger.One,
VoteFlag.PreCommit).Sign(key),
}.ToImmutableArray());
IWorld world = new World(new MockWorldState());
Expand Down
1 change: 1 addition & 0 deletions Libplanet.Action.Tests/Sys/InitializeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void ExecuteInNonGenesis()
hash,
DateTimeOffset.UtcNow,
key.PublicKey,
BigInteger.One,
VoteFlag.PreCommit).Sign(key),
}.ToImmutableArray());
var context = new ActionContext(
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Benchmarks/Commit.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using BenchmarkDotNet.Attributes;
using Libplanet.Crypto;
using Libplanet.Types.Blocks;
Expand Down Expand Up @@ -65,6 +66,7 @@ private void SetupVotes()
_blockHash,
DateTimeOffset.UtcNow,
_privateKeys[x].PublicKey,
BigInteger.One,
VoteFlag.PreCommit).Sign(_privateKeys[x]))
.ToArray();
}
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Explorer.Tests/GeneratedBlockChainFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.Loader;
Expand Down Expand Up @@ -196,6 +197,7 @@ private void AddBlock(ImmutableArray<Transaction> transactions)
block.Hash,
DateTimeOffset.UtcNow,
pk.PublicKey,
BigInteger.One,
VoteFlag.PreCommit).Sign(pk)).ToImmutableArray()));
MinedBlocks = MinedBlocks
.SetItem(
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Explorer.Tests/GraphTypes/BlockCommitTypeTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Numerics;
using GraphQL;
using GraphQL.Types;
using GraphQL.Execution;
Expand Down Expand Up @@ -28,6 +29,7 @@ public async void Query()
blockHash,
DateTimeOffset.Now,
privateKey.PublicKey,
BigInteger.One,
VoteFlag.PreCommit).Sign(privateKey);
var blockCommit = new BlockCommit(1, 0, blockHash, ImmutableArray.Create(vote));

Expand Down
4 changes: 4 additions & 0 deletions Libplanet.Explorer.Tests/GraphTypes/BlockTypeTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Numerics;
using System.Security.Cryptography;
using GraphQL;
using GraphQL.Execution;
Expand Down Expand Up @@ -31,6 +32,7 @@ public async void Query()
lastBlockHash,
DateTimeOffset.Now,
privateKey.PublicKey,
BigInteger.One,
VoteFlag.PreCommit).Sign(privateKey));
var lastBlockCommit = new BlockCommit(1, 0, lastBlockHash, lastVotes);
var preEval = new BlockContent(
Expand Down Expand Up @@ -70,6 +72,7 @@ public async void Query()
blockHash
timestamp
validatorPublicKey
validatorPower
flag
signature
}
Expand Down Expand Up @@ -117,6 +120,7 @@ public async void Query()
{ "blockHash", lastVotes[0].BlockHash.ToString() },
{ "timestamp", new DateTimeOffsetGraphType().Serialize(lastVotes[0].Timestamp) },
{ "validatorPublicKey", lastVotes[0].ValidatorPublicKey.ToString() },
{ "validatorPower", lastVotes[0].ValidatorPower },
{ "flag", lastVotes[0].Flag.ToString() },
{ "signature", ByteUtil.Hex(lastVotes[0].Signature) },
}
Expand Down
4 changes: 4 additions & 0 deletions Libplanet.Explorer.Tests/GraphTypes/VoteTypeTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using GraphQL;
using GraphQL.Types;
using GraphQL.Execution;
Expand Down Expand Up @@ -27,6 +28,7 @@ public async void Query()
blockHash,
DateTimeOffset.Now,
privateKey.PublicKey,
123,
VoteFlag.PreCommit).Sign(privateKey);

var query =
Expand All @@ -36,6 +38,7 @@ public async void Query()
blockHash
timestamp
validatorPublicKey
validatorPower
flag
signature
}";
Expand All @@ -54,6 +57,7 @@ public async void Query()
Assert.Equal(vote.BlockHash.ToString(), resultData["blockHash"]);
Assert.Equal(new DateTimeOffsetGraphType().Serialize(vote.Timestamp), resultData["timestamp"]);
Assert.Equal(vote.ValidatorPublicKey.ToString(), resultData["validatorPublicKey"]);
Assert.Equal(vote.ValidatorPower, resultData["validatorPower"]);
Assert.Equal(vote.Flag.ToString(), resultData["flag"]);
Assert.Equal(ByteUtil.Hex(vote.Signature), resultData["signature"]);
}
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Explorer.Tests/Indexing/BlockChainIndexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
using Libplanet.Crypto;
Expand Down Expand Up @@ -57,6 +58,7 @@ await index.SynchronizeAsync(
divergentBlock.Hash,
DateTimeOffset.UtcNow,
pk.PublicKey,
BigInteger.One,
VoteFlag.PreCommit)
.Sign(pk))
.ToImmutableArray()));
Expand Down
4 changes: 4 additions & 0 deletions Libplanet.Explorer/GraphTypes/VoteType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public VoteType()
"ValidatorPublicKey",
description: "Public key of the validator which is subject of the vote.",
resolve: ctx => ctx.Source.ValidatorPublicKey);
Field<NonNullGraphType<BigIntGraphType>>(
"ValidatorPower",
description: "Power of the validator which is subject of the vote.",
resolve: ctx => ctx.Source.ValidatorPower);
Field<NonNullGraphType<VoteFlagType>>(
"Flag",
description: "Flag of the vote",
Expand Down
13 changes: 9 additions & 4 deletions Libplanet.Net.Tests/Consensus/ConsensusContextNonProposerTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using Bencodex;
using Bencodex.Types;
using Libplanet.Crypto;
Expand Down Expand Up @@ -72,6 +73,7 @@ public async void NewHeightWithLastCommit()
block1.Hash,
DateTimeOffset.UtcNow,
TestUtils.ValidatorSet[i].PublicKey,
TestUtils.ValidatorSet[i].Power,
VoteFlag.PreVote).Sign(TestUtils.PrivateKeys[i]);
consensusContext.HandleMessage(new ConsensusPreVoteMsg(expectedVotes[i]));
}
Expand All @@ -86,6 +88,7 @@ public async void NewHeightWithLastCommit()
block1.Hash,
DateTimeOffset.UtcNow,
TestUtils.ValidatorSet[i].PublicKey,
TestUtils.ValidatorSet[i].Power,
VoteFlag.PreCommit).Sign(TestUtils.PrivateKeys[i]);
consensusContext.HandleMessage(new ConsensusPreCommitMsg(expectedVotes[i]));
}
Expand Down Expand Up @@ -174,9 +177,9 @@ public async void HandleMessageFromHigherHeight()
throw new Exception("Proposal is null.");
}

foreach ((PrivateKey privateKey, BoundPeer peer)
foreach ((PrivateKey privateKey, BigInteger power)
in TestUtils.PrivateKeys.Zip(
TestUtils.Peers,
TestUtils.ValidatorSet.Validators.Select(v => v.Power),
(first, second) => (first, second)))
{
if (privateKey == TestUtils.PrivateKeys[2])
Expand All @@ -193,12 +196,13 @@ in TestUtils.PrivateKeys.Zip(
proposal!.BlockHash,
DateTimeOffset.UtcNow,
privateKey.PublicKey,
power,
VoteFlag.PreVote).Sign(privateKey)));
}

foreach ((PrivateKey privateKey, BoundPeer peer)
foreach ((PrivateKey privateKey, BigInteger power)
in TestUtils.PrivateKeys.Zip(
TestUtils.Peers,
TestUtils.ValidatorSet.Validators.Select(v => v.Power),
(first, second) => (first, second)))
{
if (privateKey == TestUtils.PrivateKeys[2])
Expand All @@ -215,6 +219,7 @@ in TestUtils.PrivateKeys.Zip(
proposal!.BlockHash,
DateTimeOffset.UtcNow,
privateKey.PublicKey,
power,
VoteFlag.PreCommit).Sign(privateKey)));
}

Expand Down
8 changes: 8 additions & 0 deletions Libplanet.Net.Tests/Consensus/ConsensusContextProposerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,19 @@ public async void IncreaseRoundWhenTimeout()
new ConsensusPreVoteMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[2],
TestUtils.ValidatorSet[2].Power,
1,
0,
hash: default,
flag: VoteFlag.PreVote)));

consensusContext.HandleMessage(
new ConsensusPreVoteMsg(
vote: TestUtils.CreateVote(
TestUtils.PrivateKeys[3],
TestUtils.ValidatorSet[3].Power,
1,
0,
hash: default,
flag: VoteFlag.PreVote)));

Expand All @@ -74,15 +78,19 @@ public async void IncreaseRoundWhenTimeout()
new ConsensusPreCommitMsg(
TestUtils.CreateVote(
TestUtils.PrivateKeys[2],
TestUtils.ValidatorSet[2].Power,
1,
0,
hash: default,
flag: VoteFlag.PreCommit)));

consensusContext.HandleMessage(
new ConsensusPreCommitMsg(
vote: TestUtils.CreateVote(
TestUtils.PrivateKeys[3],
TestUtils.ValidatorSet[3].Power,
1,
0,
hash: default,
flag: VoteFlag.PreCommit)));

Expand Down
Loading

0 comments on commit 1620267

Please sign in to comment.