Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DelayedRenderer<T> & DelayedActionRenderer<T> for stable app-side state transfer #980

Merged
merged 8 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"struct",
"unrender",
"unrendered",
"unrenderer",
"unrendering",
"unrenders",
]
}
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ To be released.
BoundPeer, TimeSpan?, CancellationToken)` method with
`Swarm<T>.FindSpecificPeerAsync(Address, int, TimeSpan?, CancellationToken)`.
[[#981]]
- Added `IActionContext.GetUnconsumedContext()` method. [[#980]]

### Backward-incompatible network protocol changes

Expand Down Expand Up @@ -179,6 +180,8 @@ To be released.
- Added `IActionRenderer<T>` interface. [[#959], [#967], [#970]]
- Added `AnonymousRenderer<T>` class. [[#959], [#963]]
- Added `AnonymousActionRenderer<T>` interface. [[#959], [#967], [#970]]
- Added `DelayedRenderer<T>` class. [[#980]]
- Added `DelayedActionRenderer<T>` class. [[#980]]
- Added `LoggedRenderer<T>` class. [[#959], [#963]]
- Added `LoggedActionRenderer<T>` interface. [[#959], [#967], [#970]]
- Added `BlockChain<T>.Renderers` property. [[#945], [#959], [#963]]
Expand Down Expand Up @@ -304,6 +307,7 @@ To be released.
[#967]: https://github.com/planetarium/libplanet/issues/967
[#970]: https://github.com/planetarium/libplanet/pull/970
[#972]: https://github.com/planetarium/libplanet/pull/972
[#980]: https://github.com/planetarium/libplanet/pull/980
[#981]: https://github.com/planetarium/libplanet/pull/981
[sleep mode]: https://en.wikipedia.org/wiki/Sleep_mode

Expand Down
27 changes: 27 additions & 0 deletions Libplanet.Tests/Action/ActionContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ public void GuidVersionAndVariant()
}
}

[Fact]
public void GetUnconsumedContext()
{
var random = new System.Random();
var original = new ActionContext(
signer: random.NextAddress(),
miner: random.NextAddress(),
blockIndex: 1,
previousStates: new DumbAccountStateDelta(),
randomSeed: random.Next()
);

// Consume original's random state...
int[] values =
{
original.Random.Next(),
original.Random.Next(),
original.Random.Next(),
};

IActionContext clone = original.GetUnconsumedContext();
Assert.Equal(
values,
new[] { clone.Random.Next(), clone.Random.Next(), clone.Random.Next() }
);
}

private class DumbAccountStateDelta : IAccountStateDelta
{
public IImmutableSet<Address> UpdatedAddresses =>
Expand Down
115 changes: 94 additions & 21 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,30 +704,103 @@ public void UnstageAfterAppendComplete()
Assert.Empty(_blockChain.GetStagedTransactionIds());
}

[Fact]
public async Task ActionRenderersHaveDistinctContexts()
{
var policy = new NullPolicy<DumbAction>();
var store = new DefaultStore(null);
var generatedRandomValueLogs = new List<int>();
IActionRenderer<DumbAction>[] renderers = Enumerable.Range(0, 2).Select(i =>
new LoggedActionRenderer<DumbAction>(
new AnonymousActionRenderer<DumbAction>
{
ActionRenderer = (act, context, nextStates) =>
// Consuming the random state through IRandom.Next() should not
// affect contexts passed to other action renderers.
generatedRandomValueLogs.Add(context.Random.Next()),
},
Log.Logger.ForContext("RendererIndex", i)
)
).ToArray();
BlockChain<DumbAction> blockChain = TestUtils.MakeBlockChain(
policy,
store,
renderers: renderers
);
var privateKey = new PrivateKey();
var action = new DumbAction(default, string.Empty);
var actions = new[] { action };
blockChain.MakeTransaction(privateKey, actions);
Block<DumbAction> block = await blockChain.MineBlock(_fx.Address1, append: false);

generatedRandomValueLogs.Clear();
Assert.Empty(generatedRandomValueLogs);
blockChain.Append(block);
Assert.Equal(2, generatedRandomValueLogs.Count);
Assert.Equal(generatedRandomValueLogs[0], generatedRandomValueLogs[1]);
}

[Fact]
public async Task RenderActionsAfterBlockIsRendered()
{
var policy = new NullPolicy<DumbAction>();
var store = new DefaultStore(null);
int idx = 0;
var blockLogs = new List<(Block<DumbAction> Old, Block<DumbAction> New, int Index)>();
var actionLogs = new List<(ActionEvaluation Evaluation, int Index)>();
IActionRenderer<DumbAction> renderer = new AnonymousActionRenderer<DumbAction>
{
BlockRenderer = (oldTip, newTip) =>
blockLogs.Add((oldTip, newTip, idx++)),
ActionRenderer = (act, context, nextStates) =>
actionLogs.Add((new ActionEvaluation(act, context, nextStates), idx++)),
};
renderer = new LoggedActionRenderer<DumbAction>(renderer, Log.Logger);
BlockChain<DumbAction> blockChain =
TestUtils.MakeBlockChain(policy, store, renderers: new[] { renderer });
var privateKey = new PrivateKey();

var action = new DumbAction(default, string.Empty);
var actions = new[] { action };
blockChain.MakeTransaction(privateKey, actions);
idx = 0;
blockLogs.Clear();
actionLogs.Clear();
Block<DumbAction> prevBlock = blockChain.Tip;
Block<DumbAction> block = await blockChain.MineBlock(_fx.Address1);

Assert.Equal(2, blockChain.Count);
Assert.Single(blockLogs);
Assert.Single(actionLogs);
Assert.Equal((prevBlock, block, 0), blockLogs[0]);
Assert.Equal(1, actionLogs[0].Index);
Assert.Equal(action, actionLogs[0].Evaluation.Action);
}

[Fact]
public async Task RenderActionsAfterAppendComplete()
{
var policy = new NullPolicy<DumbAction>();
var store = new DefaultStore(null);
IActionRenderer<DumbAction> renderer = new AnonymousActionRenderer<DumbAction>
{
ActionRenderer = (_, __, nextStates) =>
throw new SomeException("thrown by renderer"),
};
renderer = new LoggedActionRenderer<DumbAction>(renderer, Log.Logger);
BlockChain<DumbAction> blockChain =
TestUtils.MakeBlockChain(policy, store, renderers: new[] { renderer });
var privateKey = new PrivateKey();

var action = new DumbAction(default, string.Empty);
var actions = new[] { action };
blockChain.MakeTransaction(privateKey, actions);

SomeException e = await Assert.ThrowsAsync<SomeException>(
async () => await blockChain.MineBlock(_fx.Address1)
);
Assert.Equal("thrown by renderer", e.Message);
Assert.Equal(2, blockChain.Count);
var policy = new NullPolicy<DumbAction>();
var store = new DefaultStore(null);
IActionRenderer<DumbAction> renderer = new AnonymousActionRenderer<DumbAction>
{
ActionRenderer = (_, __, nextStates) =>
throw new SomeException("thrown by renderer"),
};
renderer = new LoggedActionRenderer<DumbAction>(renderer, Log.Logger);
Comment on lines +785 to +790
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it overwritten?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the way to use LoggedActionRenderer<T> as it uses decorator pattern. See also the example code in its docs.

BlockChain<DumbAction> blockChain =
TestUtils.MakeBlockChain(policy, store, renderers: new[] { renderer });
var privateKey = new PrivateKey();

var action = new DumbAction(default, string.Empty);
var actions = new[] { action };
blockChain.MakeTransaction(privateKey, actions);

SomeException e = await Assert.ThrowsAsync<SomeException>(
async () => await blockChain.MineBlock(_fx.Address1)
);
Assert.Equal("thrown by renderer", e.Message);
Assert.Equal(2, blockChain.Count);
}

[Fact]
Expand Down
Loading