Skip to content

Commit

Permalink
Disallow zero confirmations to prevent mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Sep 4, 2020
1 parent 16faa43 commit 251ede7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Libplanet.Tests/Blockchain/Renderers/DelayedActionRendererTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public DelayedActionRendererTest(ITestOutputHelper output)
{
}

[Fact]
public override void Constructor()
{
ArgumentOutOfRangeException e = Assert.Throws<ArgumentOutOfRangeException>(() =>
new DelayedActionRenderer<DumbAction>(
new AnonymousActionRenderer<DumbAction>(),
_store,
confirmations: 0
)
);
Assert.Equal("confirmations", e.ParamName);
}

[Fact]
public override void BlocksBeingAppended()
{
Expand Down
14 changes: 14 additions & 0 deletions Libplanet.Tests/Blockchain/Renderers/DelayedRendererTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Libplanet.Blockchain.Renderers;
Expand Down Expand Up @@ -65,6 +66,19 @@ public DelayedRendererTest(ITestOutputHelper output)
}
}

[Fact]
public virtual void Constructor()
{
ArgumentOutOfRangeException e = Assert.Throws<ArgumentOutOfRangeException>(() =>
new DelayedRenderer<DumbAction>(
new AnonymousRenderer<DumbAction>(),
_store,
confirmations: 0
)
);
Assert.Equal("confirmations", e.ParamName);
}

[Fact]
public virtual void BlocksBeingAppended()
{
Expand Down
12 changes: 12 additions & 0 deletions Libplanet/Blockchain/Renderers/DelayedRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,21 @@ public class DelayedRenderer<T> : IRenderer<T>
/// implementations and receives delayed events.</param>
/// <param name="store">The same store to what <see cref="BlockChain{T}"/> uses.</param>
/// <param name="confirmations">The required number of confirmations to recognize a block.
/// It must be greater than zero (note that zero <paramref name="confirmations"/> mean
/// nothing is delayed so that it is equivalent to the bare <paramref name="renderer"/>).
/// See also the <see cref="Confirmations"/> property.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the argument
/// <paramref name="confirmations"/> is not greater than zero.</exception>
public DelayedRenderer(IRenderer<T> renderer, IStore store, uint confirmations)
{
if (confirmations < 1)
{
string msg =
"Zero confirmations mean nothing is delayed so that it is equivalent to the " +
$"bare {nameof(renderer)}; configure it to more than zero.";
throw new ArgumentOutOfRangeException(nameof(confirmations), msg);
}

Logger = Log.ForContext(GetType());
Renderer = renderer;
Store = store;
Expand Down

0 comments on commit 251ede7

Please sign in to comment.