Skip to content

Commit 13f3e50

Browse files
committed
feat: add dpos related features
1 parent 1016fbc commit 13f3e50

File tree

14 files changed

+190
-4
lines changed

14 files changed

+190
-4
lines changed
Binary file not shown.
Binary file not shown.

.idea/.idea.Libplanet/.idea/copilot/chatSessions/xd.lck

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Libplanet.Action.Tests/ActionContextTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System.Collections.Immutable;
12
using Libplanet.Action.State;
23
using Libplanet.Action.Tests.Mocks;
34
using Libplanet.Crypto;
45
using Libplanet.Types.Blocks;
6+
using Libplanet.Types.Consensus;
57
using Libplanet.Types.Tx;
68
using Xunit;
79

@@ -12,12 +14,29 @@ public class ActionContextTest
1214
private readonly System.Random _random;
1315
private readonly Address _address;
1416
private readonly TxId _txid;
17+
private readonly BlockCommit _lastCommit;
1518

1619
public ActionContextTest()
1720
{
1821
_random = new System.Random();
1922
_address = _random.NextAddress();
2023
_txid = _random.NextTxId();
24+
var key = new PrivateKey();
25+
var hash = _random.NextBlockHash();
26+
_lastCommit = new BlockCommit(
27+
0,
28+
0,
29+
hash,
30+
new[]
31+
{
32+
new VoteMetadata(
33+
0,
34+
0,
35+
hash,
36+
DateTimeOffset.UtcNow,
37+
key.PublicKey,
38+
VoteFlag.PreCommit).Sign(key),
39+
}.ToImmutableArray());
2140
}
2241

2342
[Fact]
@@ -36,6 +55,7 @@ public void RandomShouldBeDeterministic()
3655
miner: _address,
3756
blockIndex: 1,
3857
blockProtocolVersion: Block.CurrentProtocolVersion,
58+
lastCommit: _lastCommit,
3959
previousState: new World(new MockWorldState()),
4060
randomSeed: seed,
4161
gasLimit: 0
@@ -54,6 +74,7 @@ public void GuidShouldBeDeterministic()
5474
miner: _address,
5575
blockIndex: 1,
5676
blockProtocolVersion: Block.CurrentProtocolVersion,
77+
lastCommit: _lastCommit,
5778
previousState: new World(new MockWorldState()),
5879
randomSeed: 0,
5980
gasLimit: 0
@@ -65,6 +86,7 @@ public void GuidShouldBeDeterministic()
6586
miner: _address,
6687
blockIndex: 1,
6788
blockProtocolVersion: Block.CurrentProtocolVersion,
89+
lastCommit: _lastCommit,
6890
previousState: new World(new MockWorldState()),
6991
randomSeed: 0,
7092
gasLimit: 0
@@ -76,6 +98,7 @@ public void GuidShouldBeDeterministic()
7698
miner: _address,
7799
blockIndex: 1,
78100
blockProtocolVersion: Block.CurrentProtocolVersion,
101+
lastCommit: _lastCommit,
79102
previousState: new World(new MockWorldState()),
80103
randomSeed: 1,
81104
gasLimit: 0
@@ -115,6 +138,7 @@ public void GuidVersionAndVariant()
115138
miner: _address,
116139
blockIndex: 1,
117140
blockProtocolVersion: Block.CurrentProtocolVersion,
141+
lastCommit: _lastCommit,
118142
previousState: new World(new MockWorldState()),
119143
randomSeed: i,
120144
gasLimit: 0

Libplanet.Action.Tests/ActionEvaluationTest.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
using System.Collections.Immutable;
12
using Bencodex.Types;
23
using Libplanet.Action.State;
34
using Libplanet.Action.Tests.Common;
45
using Libplanet.Action.Tests.Mocks;
56
using Libplanet.Crypto;
67
using Libplanet.Types.Blocks;
8+
using Libplanet.Types.Consensus;
79
using Serilog;
810
using Xunit;
911
using Xunit.Abstractions;
@@ -27,8 +29,25 @@ public ActionEvaluationTest(ITestOutputHelper output)
2729
[Fact]
2830
public void Constructor()
2931
{
30-
var txid = new System.Random().NextTxId();
32+
var random = new System.Random();
33+
var txid = random.NextTxId();
3134
Address address = new PrivateKey().Address;
35+
var key = new PrivateKey();
36+
var hash = random.NextBlockHash();
37+
var lastCommit = new BlockCommit(
38+
0,
39+
0,
40+
hash,
41+
new[]
42+
{
43+
new VoteMetadata(
44+
0,
45+
0,
46+
hash,
47+
DateTimeOffset.UtcNow,
48+
key.PublicKey,
49+
VoteFlag.PreCommit).Sign(key),
50+
}.ToImmutableArray());
3251
IWorld world = new World(new MockWorldState());
3352
world = world.SetAccount(
3453
ReservedAddresses.LegacyAccount,
@@ -41,6 +60,7 @@ public void Constructor()
4160
address,
4261
1,
4362
Block.CurrentProtocolVersion,
63+
lastCommit,
4464
new World(new MockWorldState()),
4565
123,
4666
0),
@@ -61,6 +81,7 @@ public void Constructor()
6181
(Text)"item",
6282
evaluation.OutputState.GetAccount(ReservedAddresses.LegacyAccount).GetState(address)
6383
);
84+
Assert.Equal(lastCommit, evaluation.InputContext.LastCommit);
6485
}
6586
}
6687
}

Libplanet.Action.Tests/Sys/InitializeTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public void Execute()
5151
miner: random.NextAddress(),
5252
blockIndex: 0,
5353
blockProtocolVersion: Block.CurrentProtocolVersion,
54+
lastCommit: null,
5455
previousState: prevState,
5556
randomSeed: 123,
5657
gasLimit: 0);
@@ -76,12 +77,29 @@ public void ExecuteInNonGenesis()
7677
Address signer = random.NextAddress();
7778
var prevState = new World(new MockWorldState());
7879
BlockHash genesisHash = random.NextBlockHash();
80+
var key = new PrivateKey();
81+
var hash = random.NextBlockHash();
82+
var lastCommit = new BlockCommit(
83+
0,
84+
0,
85+
hash,
86+
new[]
87+
{
88+
new VoteMetadata(
89+
0,
90+
0,
91+
hash,
92+
DateTimeOffset.UtcNow,
93+
key.PublicKey,
94+
VoteFlag.PreCommit).Sign(key),
95+
}.ToImmutableArray());
7996
var context = new ActionContext(
8097
signer: signer,
8198
txid: random.NextTxId(),
8299
miner: random.NextAddress(),
83100
blockIndex: 10,
84101
blockProtocolVersion: Block.CurrentProtocolVersion,
102+
lastCommit: lastCommit,
85103
previousState: prevState,
86104
randomSeed: 123,
87105
gasLimit: long.MaxValue);

Libplanet.Action/ActionContext.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
2-
using System.Diagnostics.Contracts;
32
using System.Threading;
43
using Libplanet.Action.State;
54
using Libplanet.Crypto;
5+
using Libplanet.Types.Blocks;
66
using Libplanet.Types.Tx;
77

88
namespace Libplanet.Action
@@ -19,6 +19,7 @@ public ActionContext(
1919
Address miner,
2020
long blockIndex,
2121
int blockProtocolVersion,
22+
BlockCommit? lastCommit,
2223
IWorld previousState,
2324
int randomSeed,
2425
long gasLimit)
@@ -28,6 +29,7 @@ public ActionContext(
2829
Miner = miner;
2930
BlockIndex = blockIndex;
3031
BlockProtocolVersion = blockProtocolVersion;
32+
LastCommit = lastCommit;
3133
PreviousState = previousState;
3234
RandomSeed = randomSeed;
3335
_gasLimit = gasLimit;
@@ -50,6 +52,9 @@ public ActionContext(
5052
/// <inheritdoc cref="IActionContext.BlockProtocolVersion"/>
5153
public int BlockProtocolVersion { get; }
5254

55+
/// <inheritdoc cref="IActionContext.LastCommit"/>
56+
public BlockCommit? LastCommit { get; }
57+
5358
/// <inheritdoc cref="IActionContext.PreviousState"/>
5459
public IWorld PreviousState { get; }
5560

Libplanet.Action/ActionEvaluator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ IActionContext CreateActionContext(
203203
miner: blockHeader.Miner,
204204
blockIndex: blockHeader.Index,
205205
blockProtocolVersion: blockHeader.ProtocolVersion,
206+
lastCommit: blockHeader.LastCommit,
206207
previousState: prevState,
207208
randomSeed: randomSeed,
208209
gasLimit: actionGasLimit);
@@ -266,6 +267,7 @@ IActionContext CreateActionContext(IWorld newPrevState)
266267
miner: inputContext.Miner,
267268
blockIndex: inputContext.BlockIndex,
268269
blockProtocolVersion: inputContext.BlockProtocolVersion,
270+
lastCommit: inputContext.LastCommit,
269271
previousState: newPrevState,
270272
randomSeed: inputContext.RandomSeed,
271273
gasLimit: inputContext.GasLimit());

Libplanet.Action/IActionContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public interface IActionContext
5151
[Pure]
5252
int BlockProtocolVersion { get; }
5353

54+
/// <summary>
55+
/// The <see cref="BlockCommit"/> about previous <see cref="Block"/>'s vote information.
56+
/// <see langword="null"/> if the block is the genesis block.
57+
/// </summary>
58+
[Pure]
59+
BlockCommit? LastCommit { get; }
60+
5461
/// <summary>
5562
/// A null delta of states, which means it represents the states
5663
/// before <see cref="IAction"/> executes.

Libplanet.Tests/Action/AccountDiffTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public IActionContext CreateActionContext(Address signer, ITrie trie) =>
148148
signer,
149149
0,
150150
Block.CurrentProtocolVersion,
151+
null,
151152
new World(
152153
new WorldBaseState(
153154
trie,

0 commit comments

Comments
 (0)