Skip to content

Commit

Permalink
Merge pull request #439 from ipdae/issues-371
Browse files Browse the repository at this point in the history
Implement deterministic Guid
  • Loading branch information
dahlia authored Aug 16, 2019
2 parents 52e9107 + e68b2b9 commit f46dfd6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ To be released.
- Removed `IRandom.NextDouble()` method, because [floating-point arithmetics,
which is underspecified, likely introduce
indeterminism][floating-point determinism]. [[#410], [#419]]
- Added `IActionContext.NewGuId()` method. [[#371], [#439]]

### Added interfaces

Expand Down Expand Up @@ -118,6 +119,7 @@ To be released.
[#366]: https://github.com/planetarium/libplanet/pull/366
[#367]: https://github.com/planetarium/libplanet/pull/367
[#369]: https://github.com/planetarium/libplanet/pull/369
[#371]: https://github.com/planetarium/libplanet/issues/371
[#375]: https://github.com/planetarium/libplanet/pull/375
[#377]: https://github.com/planetarium/libplanet/issues/377
[#378]: https://github.com/planetarium/libplanet/pull/378
Expand Down Expand Up @@ -146,6 +148,7 @@ To be released.
[#426]: https://github.com/planetarium/libplanet/pull/426
[#434]: https://github.com/planetarium/libplanet/pull/434
[#438]: https://github.com/planetarium/libplanet/pull/438
[#439]: https://github.com/planetarium/libplanet/pull/439
[LiteDB #1268]: https://github.com/mbdavid/LiteDB/issues/1268
[floating-point determinism]: https://wp.me/p1fTCO-kT

Expand Down
49 changes: 49 additions & 0 deletions Libplanet.Tests/Action/ActionContextTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Immutable;
using Libplanet.Action;
using Xunit;
Expand Down Expand Up @@ -29,6 +30,54 @@ public void RandomShouldBeDeterministic()
}
}

[Fact]
public void GuidShouldBeDeterministic()
{
var address = new Address("21744f4f08db23e044178dafb8273aeb5ebe6644");
var context1 = new ActionContext(
signer: address,
miner: address,
blockIndex: 1,
previousStates: new DumbAccountStateDelta(),
randomSeed: 0
);

var context2 = new ActionContext(
signer: address,
miner: address,
blockIndex: 1,
previousStates: new DumbAccountStateDelta(),
randomSeed: 0
);

var context3 = new ActionContext(
signer: address,
miner: address,
blockIndex: 1,
previousStates: new DumbAccountStateDelta(),
randomSeed: 1
);

(Guid expected, Guid diff)[] testCases =
{
(
new Guid("6f460c1a-755d-d8e4-ad67-65d5f519dbc8"),
new Guid("8286d046-9740-a3e4-95cf-ff46699c73c4")
),
(
new Guid("3b347c2b-f837-0085-ec5e-64005393b30d"),
new Guid("3410cda1-5b13-a34e-6f84-a54adf7a0ea0")
),
};

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

private class DumbAccountStateDelta : IAccountStateDelta
{
public IImmutableSet<Address> UpdatedAddresses =>
Expand Down
10 changes: 10 additions & 0 deletions Libplanet/Action/ActionContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Libplanet.Action
{
internal class ActionContext : IActionContext
Expand Down Expand Up @@ -30,5 +32,13 @@ 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);
}
}
}
9 changes: 9 additions & 0 deletions Libplanet/Action/IActionContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Libplanet.Action
{
/// <summary>
Expand Down Expand Up @@ -53,5 +55,12 @@ 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();
}
}

0 comments on commit f46dfd6

Please sign in to comment.