-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #970 from dahlia/action-renderer
Short-circuit evaluation of stale actions on reorg
- Loading branch information
Showing
20 changed files
with
1,218 additions
and
679 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,6 @@ | |
"secp256k1", | ||
"struct", | ||
"unrender", | ||
"unrendered", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
Libplanet.Tests/Blockchain/Renderers/AnonymousActionRendererTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
using System; | ||
using Libplanet.Action; | ||
using Libplanet.Blockchain.Renderers; | ||
using Libplanet.Blocks; | ||
using Libplanet.Tests.Common.Action; | ||
using Xunit; | ||
|
||
namespace Libplanet.Tests.Blockchain.Renderers | ||
{ | ||
public class AnonymousActionRendererTest | ||
{ | ||
private static IAction _action = new DumbAction(); | ||
|
||
private static IAccountStateDelta _stateDelta = | ||
new AccountStateDeltaImpl(_ => null, (_, __) => default, default); | ||
|
||
private static IActionContext _actionContext = | ||
new ActionContext(default, default, default, _stateDelta, default); | ||
|
||
private static Exception _exception = new Exception(); | ||
|
||
private static Block<DumbAction> _genesis = | ||
TestUtils.MineGenesis<DumbAction>(default(Address)); | ||
|
||
private static Block<DumbAction> _blockA = TestUtils.MineNext(_genesis); | ||
|
||
private static Block<DumbAction> _blockB = TestUtils.MineNext(_genesis); | ||
|
||
[Fact] | ||
public void ActionRenderer() | ||
{ | ||
(IAction, IActionContext, IAccountStateDelta)? record = null; | ||
var renderer = new AnonymousActionRenderer<DumbAction> | ||
{ | ||
ActionRenderer = (action, context, nextStates) => | ||
record = (action, context, nextStates), | ||
}; | ||
|
||
renderer.UnrenderAction(_action, _actionContext, _stateDelta); | ||
Assert.Null(record); | ||
renderer.RenderActionError(_action, _actionContext, _exception); | ||
Assert.Null(record); | ||
renderer.UnrenderActionError(_action, _actionContext, _exception); | ||
Assert.Null(record); | ||
renderer.RenderBlock(_genesis, _blockA); | ||
Assert.Null(record); | ||
renderer.RenderReorg(_blockA, _blockB, _genesis); | ||
Assert.Null(record); | ||
|
||
renderer.RenderAction(_action, _actionContext, _stateDelta); | ||
Assert.NotNull(record); | ||
Assert.Same(_action, record?.Item1); | ||
Assert.Same(_actionContext, record?.Item2); | ||
Assert.Same(_stateDelta, record?.Item3); | ||
} | ||
|
||
[Fact] | ||
public void ActionUnrenderer() | ||
{ | ||
(IAction, IActionContext, IAccountStateDelta)? record = null; | ||
var renderer = new AnonymousActionRenderer<DumbAction> | ||
{ | ||
ActionUnrenderer = (action, context, nextStates) => | ||
record = (action, context, nextStates), | ||
}; | ||
|
||
renderer.RenderAction(_action, _actionContext, _stateDelta); | ||
Assert.Null(record); | ||
renderer.RenderActionError(_action, _actionContext, _exception); | ||
Assert.Null(record); | ||
renderer.UnrenderActionError(_action, _actionContext, _exception); | ||
Assert.Null(record); | ||
renderer.RenderBlock(_genesis, _blockA); | ||
Assert.Null(record); | ||
renderer.RenderReorg(_blockA, _blockB, _genesis); | ||
Assert.Null(record); | ||
|
||
renderer.UnrenderAction(_action, _actionContext, _stateDelta); | ||
Assert.NotNull(record); | ||
Assert.Same(_action, record?.Item1); | ||
Assert.Same(_actionContext, record?.Item2); | ||
Assert.Same(_stateDelta, record?.Item3); | ||
} | ||
|
||
[Fact] | ||
public void ActionErrorRenderer() | ||
{ | ||
(IAction, IActionContext, Exception)? record = null; | ||
var renderer = new AnonymousActionRenderer<DumbAction> | ||
{ | ||
ActionErrorRenderer = (action, context, exception) => | ||
record = (action, context, exception), | ||
}; | ||
|
||
renderer.RenderAction(_action, _actionContext, _stateDelta); | ||
Assert.Null(record); | ||
renderer.UnrenderAction(_action, _actionContext, _stateDelta); | ||
Assert.Null(record); | ||
renderer.UnrenderActionError(_action, _actionContext, _exception); | ||
Assert.Null(record); | ||
renderer.RenderBlock(_genesis, _blockA); | ||
Assert.Null(record); | ||
renderer.RenderReorg(_blockA, _blockB, _genesis); | ||
Assert.Null(record); | ||
|
||
renderer.RenderActionError(_action, _actionContext, _exception); | ||
Assert.NotNull(record); | ||
Assert.Same(_action, record?.Item1); | ||
Assert.Same(_actionContext, record?.Item2); | ||
Assert.Same(_exception, record?.Item3); | ||
} | ||
|
||
[Fact] | ||
public void ActionErrorUnrenderer() | ||
{ | ||
(IAction, IActionContext, Exception)? record = null; | ||
var renderer = new AnonymousActionRenderer<DumbAction> | ||
{ | ||
ActionErrorUnrenderer = (action, context, exception) => | ||
record = (action, context, exception), | ||
}; | ||
|
||
renderer.RenderAction(_action, _actionContext, _stateDelta); | ||
Assert.Null(record); | ||
renderer.UnrenderAction(_action, _actionContext, _stateDelta); | ||
Assert.Null(record); | ||
renderer.RenderActionError(_action, _actionContext, _exception); | ||
Assert.Null(record); | ||
renderer.RenderBlock(_genesis, _blockA); | ||
Assert.Null(record); | ||
renderer.RenderReorg(_blockA, _blockB, _genesis); | ||
Assert.Null(record); | ||
|
||
renderer.UnrenderActionError(_action, _actionContext, _exception); | ||
Assert.NotNull(record); | ||
Assert.Same(_action, record?.Item1); | ||
Assert.Same(_actionContext, record?.Item2); | ||
Assert.Same(_exception, record?.Item3); | ||
} | ||
|
||
[Fact] | ||
public void BlockRenderer() | ||
{ | ||
(Block<DumbAction> Old, Block<DumbAction> New)? record = null; | ||
var renderer = new AnonymousActionRenderer<DumbAction> | ||
{ | ||
BlockRenderer = (oldTip, newTip) => record = (oldTip, newTip), | ||
}; | ||
|
||
renderer.RenderAction(_action, _actionContext, _stateDelta); | ||
Assert.Null(record); | ||
renderer.UnrenderAction(_action, _actionContext, _stateDelta); | ||
Assert.Null(record); | ||
renderer.RenderActionError(_action, _actionContext, _exception); | ||
Assert.Null(record); | ||
renderer.UnrenderActionError(_action, _actionContext, _exception); | ||
Assert.Null(record); | ||
renderer.RenderReorg(_blockA, _blockB, _genesis); | ||
Assert.Null(record); | ||
|
||
renderer.RenderBlock(_genesis, _blockA); | ||
Assert.NotNull(record); | ||
Assert.Same(_genesis, record?.Old); | ||
Assert.Same(_blockA, record?.New); | ||
} | ||
|
||
[Fact] | ||
public void BlockReorg() | ||
{ | ||
(Block<DumbAction> Old, Block<DumbAction> New, Block<DumbAction> Bp)? record = null; | ||
var renderer = new AnonymousActionRenderer<DumbAction> | ||
{ | ||
ReorgRenderer = (oldTip, newTip, bp) => record = (oldTip, newTip, bp), | ||
}; | ||
|
||
renderer.RenderAction(_action, _actionContext, _stateDelta); | ||
Assert.Null(record); | ||
renderer.UnrenderAction(_action, _actionContext, _stateDelta); | ||
Assert.Null(record); | ||
renderer.RenderActionError(_action, _actionContext, _exception); | ||
Assert.Null(record); | ||
renderer.UnrenderActionError(_action, _actionContext, _exception); | ||
Assert.Null(record); | ||
renderer.RenderBlock(_genesis, _blockA); | ||
Assert.Null(record); | ||
|
||
renderer.RenderReorg(_blockA, _blockB, _genesis); | ||
Assert.NotNull(record); | ||
Assert.Same(_blockA, record?.Old); | ||
Assert.Same(_blockB, record?.New); | ||
Assert.Same(_genesis, record?.Bp); | ||
} | ||
} | ||
} |
Oops, something went wrong.