Skip to content

Commit

Permalink
Parametrize the staging strategy & make it volatile by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Dec 17, 2020
1 parent edb84b9 commit 9d87645
Show file tree
Hide file tree
Showing 21 changed files with 248 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"Libplanet",
"Libplanet's",
"PRNG",
"Unstage",
"blockchain",
"branchpoint",
"broadcasted",
Expand All @@ -22,6 +23,7 @@
"runtimes",
"secp256k1",
"struct",
"unstage",
"unrender",
"unrendered",
"unrenderer",
Expand Down
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ To be released.

### Backward-incompatible API changes

- Added `stagePolicy` as the second parameter to `BlockChain<T>()`
constructor. [[#1130], [#1131]]
- Removed `IBlockStatesStore` interface. [[#1117]]
- Removed `BaseBlockStatesStore` abstract class. [[#1117]]
- Removed `Swarm<T>.GetTrustedStateCompleterAsync()` method. [[#1117]]
Expand All @@ -28,6 +30,8 @@ To be released.

### Added APIs

- Added `IStagePolicy` interface. [[#1130], [#1131]]
- Added `VolatileStagePolicy` class. [[#1130], [#1131]]
- Added `ITransport` interface. [[#1052]]
- Added `NetMQTransport` class which implements `ITransport`. [[#1052]]
- Added `Message` abstract class. [[#1052]]
Expand Down Expand Up @@ -77,6 +81,10 @@ To be released.
- Upgraded *Bencodex* package (which is a dependency) so that Libplanet gets
benefits from its recent optimizations.
[[#1081], [#1084], [#1086], [#1101]]
- When a `BlockChain<T>` follows `VolatileStagePolicy<T>`, which is
Libplanet's the only built-in `IStagePolicy<T>` implementation at
the moment, as its `StagePolicy`, its staged transactions are no longer
persistent but volatile instead. [[#1130], [#1131]]
- `Swarm<T>` became not to receive states from trusted peers.
[[#1061], [#1102]]
- `Swarm<T>` became not to retry when block downloading. [[#1062], [#1102]]
Expand Down Expand Up @@ -114,6 +122,8 @@ To be released.
[#1120]: https://github.com/planetarium/libplanet/pull/1120
[#1124]: https://github.com/planetarium/libplanet/pull/1124
[#1125]: https://github.com/planetarium/libplanet/pull/1125
[#1130]: https://github.com/planetarium/libplanet/issues/1130
[#1131]: https://github.com/planetarium/libplanet/pull/1131
[#1132]: https://github.com/planetarium/libplanet/pull/1132


Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Benchmarks/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Crypto;
using Libplanet.Tests.Blockchain;
using Libplanet.Tests.Common.Action;
Expand Down Expand Up @@ -31,6 +32,7 @@ public void SetupChain()
_fx = new DefaultStoreFixture();
_blockChain = new BlockChain<DumbAction>(
new NullPolicy<DumbAction>(),
new VolatileStagePolicy<DumbAction>(),
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock);
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Benchmarks/MineBlock.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Blocks;
using Libplanet.Crypto;
using Libplanet.Tests.Blockchain;
Expand All @@ -19,6 +20,7 @@ public MineBlock()
_fx = new DefaultStoreFixture();
_blockChain = new BlockChain<DumbAction>(
new NullPolicy<DumbAction>(),
new VolatileStagePolicy<DumbAction>(),
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock
Expand Down
5 changes: 4 additions & 1 deletion Libplanet.Benchmarks/SwarmBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using BenchmarkDotNet.Attributes;
using Libplanet.Action;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Blocks;
using Libplanet.Crypto;
using Libplanet.Net;
Expand All @@ -23,6 +24,7 @@ public class SwarmBenchmark
private const int SwarmNumber = 10;
private const int WaitTimeout = 5 * 1000;
private readonly NullPolicy<DumbAction> _policy;
private readonly IStagePolicy<DumbAction> _stagePolicy;
private readonly List<Block<DumbAction>> _blocks;
private readonly AppProtocolVersion _appProtocolVersion;
private PrivateKey[] _keys;
Expand All @@ -33,6 +35,7 @@ public class SwarmBenchmark
public SwarmBenchmark()
{
_policy = new NullPolicy<DumbAction>();
_stagePolicy = new VolatileStagePolicy<DumbAction>();
_blocks = new List<Block<DumbAction>>
{
TestUtils.MineGenesis<DumbAction>(),
Expand All @@ -59,7 +62,7 @@ public void InitializeSwarms()
_keys[i] = _keys[i] ?? new PrivateKey();
_fxs[i] = new DefaultStoreFixture(memory: true);
_blockChains[i] = new BlockChain<DumbAction>(
_policy, _fxs[i].Store, _fxs[i].StateStore, _blocks[0]);
_policy, _stagePolicy, _fxs[i].Store, _fxs[i].StateStore, _blocks[0]);
_swarms[i] = new Swarm<DumbAction>(
_blockChains[i],
_keys[i],
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.RocksDBStore.Tests/RocksDBStoreTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Store;
using Libplanet.Tests.Blockchain;
using Libplanet.Tests.Common.Action;
Expand Down Expand Up @@ -44,6 +45,7 @@ public void ReopenStoreAfterDispose()
new TrieStateStore(new MemoryKeyValueStore(), new MemoryKeyValueStore());
var blocks = new BlockChain<DumbAction>(
new NullPolicy<DumbAction>(),
new VolatileStagePolicy<DumbAction>(),
store,
stateStore,
Fx.GenesisBlock
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Tests/Blockchain/BlockChainTest.Append.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ bool IsSignerValid(Transaction<DumbAction> tx, BlockChain<DumbAction> chain)
{
var blockChain = new BlockChain<DumbAction>(
policy,
new VolatileStagePolicy<DumbAction>(),
fx.Store,
fx.StateStore,
fx.GenesisBlock);
Expand Down Expand Up @@ -363,6 +364,7 @@ public void AppendValidatesBlock()
var blockChain = new BlockChain<DumbAction>(
new NullPolicy<DumbAction>(
new InvalidBlockDifficultyException(string.Empty)),
new VolatileStagePolicy<DumbAction>(),
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private void ValidateNextBlockInvalidStateRootHash()
var store = new DefaultStore(null);
var chain = new BlockChain<DumbAction>(
policy,
new VolatileStagePolicy<DumbAction>(),
store,
_fx.StateStore,
genesisBlock);
Expand Down
49 changes: 43 additions & 6 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public partial class BlockChainTest : IDisposable
private ValidatingActionRenderer<DumbAction> _renderer;
private Block<DumbAction> _validNext;
private List<Transaction<DumbAction>> _emptyTransaction;
private IStagePolicy<DumbAction> _stagePolicy;

public BlockChainTest(ITestOutputHelper output)
{
Expand All @@ -47,10 +48,12 @@ public BlockChainTest(ITestOutputHelper output)
.ForContext<BlockChainTest>();

_policy = new BlockPolicy<DumbAction>(new MinerReward(1), maxBlockBytes: 50 * 1024);
_stagePolicy = new VolatileStagePolicy<DumbAction>();
_fx = new DefaultStoreFixture(memory: true, blockAction: _policy.BlockAction);
_renderer = new ValidatingActionRenderer<DumbAction>();
_blockChain = new BlockChain<DumbAction>(
_policy,
_stagePolicy,
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock,
Expand Down Expand Up @@ -247,6 +250,7 @@ bool IsSignerValid(Transaction<DumbAction> tx, BlockChain<DumbAction> chain)
{
var blockChain = new BlockChain<DumbAction>(
policy,
new VolatileStagePolicy<DumbAction>(),
fx.Store,
fx.StateStore,
fx.GenesisBlock);
Expand Down Expand Up @@ -325,6 +329,7 @@ public async void CanonicalId()

var z = new BlockChain<DumbAction>(
new BlockPolicy<DumbAction>(new MinerReward(1)),
new VolatileStagePolicy<DumbAction>(),
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock
Expand Down Expand Up @@ -429,6 +434,7 @@ public async void ProcessActions()
new TrieStateStore(new MemoryKeyValueStore(), new MemoryKeyValueStore());
var chain = new BlockChain<PolymorphicAction<BaseAction>>(
new BlockPolicy<PolymorphicAction<BaseAction>>(),
new VolatileStagePolicy<PolymorphicAction<BaseAction>>(),
store,
stateStore,
genesisBlock
Expand Down Expand Up @@ -533,6 +539,7 @@ IEnumerable<ExecuteRecord> NonRehearsalExecutions() =>
var emptyRenderer = new AnonymousRenderer<DumbAction>();
var chain = new BlockChain<DumbAction>(
policy,
new VolatileStagePolicy<DumbAction>(),
fx.Store,
fx.StateStore,
fx.GenesisBlock,
Expand All @@ -559,6 +566,7 @@ IEnumerable<ExecuteRecord> NonRehearsalExecutions() =>
var renderer = new RecordingActionRenderer<DumbAction>();
var newChain = new BlockChain<DumbAction>(
policy,
new VolatileStagePolicy<DumbAction>(),
fx.Store,
fx.StateStore,
Guid.NewGuid(),
Expand Down Expand Up @@ -872,6 +880,7 @@ public async Task ForkShouldSkipExecuteAndRenderGenesis()
var renderer = new RecordingActionRenderer<DumbAction>();
var blockChain = new BlockChain<DumbAction>(
_blockChain.Policy,
new VolatileStagePolicy<DumbAction>(),
store,
stateStore,
genesis,
Expand Down Expand Up @@ -1285,7 +1294,12 @@ public async Task ReorgIsUnableToHeterogenousChain(bool render)
TestUtils.MineGenesis<DumbAction>(timestamp: DateTimeOffset.UtcNow)
.AttachStateRootHash(fx2.StateStore, _policy.BlockAction);
var chain2 = new BlockChain<DumbAction>(
_blockChain.Policy, fx2.Store, fx2.StateStore, genesis2);
_blockChain.Policy,
_blockChain.StagePolicy,
fx2.Store,
fx2.StateStore,
genesis2
);
for (int i = 0; i < 5; i++)
{
await _blockChain.MineBlock(default);
Expand Down Expand Up @@ -1315,6 +1329,7 @@ public void GetStateOnlyDrillsDownUntilRequestedAddressesAreFound()
var tracker = new StoreTracker(_fx.Store);
var chain = new BlockChain<DumbAction>(
policy,
new VolatileStagePolicy<DumbAction>(),
tracker,
_fx.StateStore,
_fx.GenesisBlock
Expand Down Expand Up @@ -1366,6 +1381,7 @@ public void GetStateReturnsEarlyForNonexistentAccount()
var tracker = new StoreTracker(_fx.Store);
var chain = new BlockChain<DumbAction>(
blockPolicy,
new VolatileStagePolicy<DumbAction>(),
tracker,
_fx.StateStore,
_fx.GenesisBlock
Expand Down Expand Up @@ -1408,6 +1424,7 @@ public async void GetStateReturnsValidStateAfterFork()
var chain =
new BlockChain<DumbAction>(
new NullPolicy<DumbAction>(),
new VolatileStagePolicy<DumbAction>(),
store,
stateStore,
genesisBlock);
Expand Down Expand Up @@ -1453,6 +1470,7 @@ public async void GetStateReturnsLatestStatesWhenMultipleAddresses()
var addresses = privateKeys.Select(AddressExtensions.ToAddress).ToList();
var chain = new BlockChain<DumbAction>(
new NullPolicy<DumbAction>(),
new VolatileStagePolicy<DumbAction>(),
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock);
Expand Down Expand Up @@ -1501,11 +1519,13 @@ public async void FindBranchPoint()
var genesisBlock = BlockChain<DumbAction>.MakeGenesisBlock();
var emptyChain = new BlockChain<DumbAction>(
_blockChain.Policy,
new VolatileStagePolicy<DumbAction>(),
emptyFx.Store,
emptyFx.StateStore,
emptyFx.GenesisBlock);
var fork = new BlockChain<DumbAction>(
_blockChain.Policy,
new VolatileStagePolicy<DumbAction>(),
forkFx.Store,
forkFx.StateStore,
forkFx.GenesisBlock);
Expand Down Expand Up @@ -1716,8 +1736,8 @@ public void MakeTransaction()
_blockChain.MakeTransaction(privateKey, actions);
_blockChain.MakeTransaction(privateKey, actions);

List<Transaction<DumbAction>> txs = _blockChain.Store
.IterateStagedTransactionIds()
List<Transaction<DumbAction>> txs = _stagePolicy
.Iterate(_blockChain)
.Select(_blockChain.Store.GetTransaction<DumbAction>)
.OrderBy(tx => tx.Nonce)
.ToList();
Expand Down Expand Up @@ -1774,6 +1794,7 @@ public async void MineBlockWithBlockAction()

var blockChain = new BlockChain<DumbAction>(
policy,
new VolatileStagePolicy<DumbAction>(),
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock);
Expand Down Expand Up @@ -1919,6 +1940,7 @@ internal static (Address, Address[] Addresses, BlockChain<DumbAction> Chain)
.AttachStateRootHash(stateStore, blockPolicy.BlockAction);
var chain = new BlockChain<DumbAction>(
blockPolicy,
new VolatileStagePolicy<DumbAction>(),
store,
stateStore,
chainId,
Expand Down Expand Up @@ -2101,12 +2123,14 @@ private async void AbortMining()
StoreFixture fx2 = new DefaultStoreFixture(blockAction: policy2.BlockAction);
var chain1 = new BlockChain<DumbAction>(
policy1,
new VolatileStagePolicy<DumbAction>(),
fx1.Store,
fx1.StateStore,
fx1.GenesisBlock);
var renderer2 = new RecordingActionRenderer<DumbAction>();
var chain2 = new BlockChain<DumbAction>(
policy2,
new VolatileStagePolicy<DumbAction>(),
fx2.Store,
fx1.StateStore,
fx2.GenesisBlock,
Expand Down Expand Up @@ -2157,6 +2181,7 @@ private void ConstructWithGenesisBlock()
BlockChain<DumbAction> blockChain =
new BlockChain<DumbAction>(
policy,
new VolatileStagePolicy<DumbAction>(),
storeFixture.Store,
storeFixture.StateStore,
BlockChain<DumbAction>.MakeGenesisBlock(actions));
Expand All @@ -2175,18 +2200,30 @@ private void ConstructWithGenesisBlock()
private void ConstructWithUnexpectedGenesisBlock()
{
var policy = new NullPolicy<DumbAction>();
var stagePolicy = new VolatileStagePolicy<DumbAction>();
var store = new DefaultStore(null);
var stateStore =
new TrieStateStore(new MemoryKeyValueStore(), new MemoryKeyValueStore());
var genesisBlockA = BlockChain<DumbAction>.MakeGenesisBlock();
var genesisBlockB = BlockChain<DumbAction>.MakeGenesisBlock();

var blockChain = new BlockChain<DumbAction>(policy, store, stateStore, genesisBlockA);
var blockChain = new BlockChain<DumbAction>(
policy,
stagePolicy,
store,
stateStore,
genesisBlockA
);

Assert.Throws<InvalidGenesisBlockException>(() =>
{
var blockchain =
new BlockChain<DumbAction>(policy, store, stateStore, genesisBlockB);
var blockchain = new BlockChain<DumbAction>(
policy,
stagePolicy,
store,
stateStore,
genesisBlockB
);
});
}

Expand Down
3 changes: 3 additions & 0 deletions Libplanet.Tests/Blockchain/Policies/BlockPolicyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class BlockPolicyTest : IDisposable
private StoreFixture _fx;
private BlockChain<DumbAction> _chain;
private IBlockPolicy<DumbAction> _policy;
private IStagePolicy<DumbAction> _stagePolicy;

public BlockPolicyTest(ITestOutputHelper output)
{
Expand All @@ -32,8 +33,10 @@ public BlockPolicyTest(ITestOutputHelper output)
blockAction: null,
blockIntervalMilliseconds: 3 * 60 * 60 * 1000
);
_stagePolicy = new VolatileStagePolicy<DumbAction>();
_chain = new BlockChain<DumbAction>(
_policy,
_stagePolicy,
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock);
Expand Down
Loading

0 comments on commit 9d87645

Please sign in to comment.