Skip to content

Commit

Permalink
Merge pull request #508 from Unengine/add-randomextension
Browse files Browse the repository at this point in the history
Remove `IActionContext.NewGuid()` & Add `RandomExtension.GenerateRandomGuid()`
  • Loading branch information
dahlia authored Sep 10, 2019
2 parents 16cf867 + 4c9575a commit 738bb57
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ To be released.
`Swarm<T>.BootstrapAsync()` method instead. [[#353]]
- `Peer` with endpoints should be typed as `BoundPeer` which is inherited from
`Peer`. [[#353]]
- Removed `IActionContext.NewGuid()` method. To get a randomly generated [Guid][Guid],
use `RandomExtension.GenerateRandomGuid()` instead. [[#508]]

### Added interfaces

Expand All @@ -71,6 +73,7 @@ To be released.
both a `Block<T>.Hash` and a `Transaction<T>.Id`, so that signers cannot
predict the order of transactions in a block before it's mined.
[[#244], [#355]]
- Added `RandomExtension` static class. [[#508]]

### Behavioral changes

Expand Down Expand Up @@ -111,7 +114,9 @@ To be released.
[#486]: https://github.com/planetarium/libplanet/pull/486
[#498]: https://github.com/planetarium/libplanet/pull/498
[#496]: https://github.com/planetarium/libplanet/pull/496
[#508]: https://github.com/planetarium/libplanet/pull/508
[Kademlia]: https://en.wikipedia.org/wiki/Kademlia
[Guid]: https://docs.microsoft.com/ko-kr/dotnet/api/system.guid?view=netframework-4.8


Version 0.5.3
Expand Down
6 changes: 3 additions & 3 deletions Libplanet.Tests/Action/ActionContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public void GuidShouldBeDeterministic()

foreach (var (expected, diff) in testCases)
{
Assert.Equal(expected, context1.NewGuid());
Assert.Equal(expected, context2.NewGuid());
Assert.Equal(diff, context3.NewGuid());
Assert.Equal(expected, context1.Random.GenerateRandomGuid());
Assert.Equal(expected, context2.Random.GenerateRandomGuid());
Assert.Equal(diff, context3.Random.GenerateRandomGuid());
}
}

Expand Down
8 changes: 0 additions & 8 deletions Libplanet/Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,5 @@ public ActionContext(
public IAccountStateDelta PreviousStates { get; }

public IRandom Random { get; }

public Guid NewGuid()
{
// FIXME implement rfc4122 https://www.ietf.org/rfc/rfc4122.txt
var b = new byte[16];
Random.NextBytes(b);
return new Guid(b);
}
}
}
7 changes: 0 additions & 7 deletions Libplanet/Action/IActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,5 @@ public interface IActionContext
/// <returns>A random object that shares interface mostly equivalent
/// to <see cref="System.Random"/>.</returns>
IRandom Random { get; }

/// <summary>
/// Creates new <see cref="Guid"/>. Its seed (state) is determined by a block and
/// a transaction, which is deterministic as like <see cref="Random"/>.
/// </summary>
/// <returns>A deterministic guid.</returns>
Guid NewGuid();
}
}
27 changes: 27 additions & 0 deletions Libplanet/Action/RandomExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Libplanet.Action
{
/// <summary>
/// This extension class provides some convenient methods
/// to deal with <see cref="IRandom"/>.
/// </summary>
public static class RandomExtension
{
/// <summary>
/// Generates a random <see cref="Guid"/>.
/// </summary>
/// <param name="random"> <see cref="IRandom"/> to generate
/// a random <see cref="Guid"/>.</param>
/// <returns> Generated random <see cref="Guid"/>.
/// </returns>
/// <seealso cref="IRandom"/>
public static Guid GenerateRandomGuid(this IRandom random)
{
// FIXME implement rfc4122 https://www.ietf.org/rfc/rfc4122.txt
var bytes = new byte[16];
random.NextBytes(bytes);
return new Guid(bytes);
}
}
}

0 comments on commit 738bb57

Please sign in to comment.