-
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.
- Loading branch information
Showing
7 changed files
with
125 additions
and
44 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Collections.Immutable; | ||
using System.Diagnostics.Contracts; | ||
using Libplanet.Crypto; | ||
|
||
namespace Libplanet.Action.State | ||
{ | ||
public interface IWorldDelta | ||
{ | ||
/// <summary> | ||
/// A dictionary representing changed account states for each <see cref="Address"/>. | ||
/// This lasts till new empty delta instance has been made. | ||
/// </summary> | ||
[Pure] | ||
IImmutableDictionary<Address, IAccount> Accounts { get; } | ||
|
||
/// <summary> | ||
/// A dictionary representing changed account states for each <see cref="Address"/>. | ||
/// Elements of this collection are removed when <see cref="CommitAccount"/> called | ||
/// by corresponding <see cref="Address"/>. | ||
/// </summary> | ||
[Pure] | ||
IImmutableDictionary<Address, IAccount> Uncommitted { get; } | ||
|
||
/// <summary> | ||
/// Set account on both of <see cref="Accounts"/> and <see cref="Uncommitted"/> | ||
/// dictionaries. If <see cref="IAccount"/> already exists on | ||
/// <paramref name="address"/>, update with new <paramref name="account"/>. | ||
/// </summary> | ||
/// <param name="address"><see cref="Address"/> to set on.</param> | ||
/// <param name="account"><see cref="IAccount"/> to set.</param> | ||
/// <returns>New <see cref="IWorldDelta"/> that account is properly set.</returns> | ||
[Pure] | ||
IWorldDelta SetAccount(Address address, IAccount account); | ||
|
||
/// <summary> | ||
/// Remove item from <see cref="Uncommitted"/> dictionary where its key is | ||
/// <paramref name="address"/>. | ||
/// </summary> | ||
/// <param name="address"><see cref="Address"/> of <see cref="IAccount"/> to | ||
/// remove from <see cref="Uncommitted"/>.</param> | ||
/// <returns> | ||
/// New <see cref="IWorldDelta"/> item of <paramref name="address"/> removed from | ||
/// <see cref="Uncommitted"/>. | ||
/// If corresponding uncommitted does not exist, returns identical one. | ||
/// </returns> | ||
[Pure] | ||
IWorldDelta CommitAccount(Address address); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Libplanet.Crypto; | ||
|
||
namespace Libplanet.Action.State | ||
{ | ||
public class WorldDelta : IWorldDelta | ||
{ | ||
private IImmutableDictionary<Address, AccountItem> _accounts; | ||
|
||
public WorldDelta() | ||
{ | ||
_accounts = ImmutableDictionary<Address, AccountItem>.Empty; | ||
} | ||
|
||
private WorldDelta(IImmutableDictionary<Address, AccountItem> accounts) | ||
{ | ||
_accounts = accounts; | ||
} | ||
|
||
/// <inheritdoc cref="IWorldDelta.Accounts"/> | ||
public IImmutableDictionary<Address, IAccount> Accounts | ||
=> _accounts | ||
.ToImmutableDictionary(item => item.Key, item => item.Value.Account); | ||
|
||
/// <inheritdoc cref="IWorldDelta.Uncommitted"/> | ||
public IImmutableDictionary<Address, IAccount> Uncommitted | ||
=> _accounts | ||
.Where(item => !item.Value.Committed) | ||
.ToImmutableDictionary(item => item.Key, item => item.Value.Account); | ||
|
||
/// <inheritdoc cref="IWorldDelta.SetAccount"/> | ||
public IWorldDelta SetAccount(Address address, IAccount account) | ||
=> new WorldDelta(_accounts.SetItem(address, new AccountItem(account, false))); | ||
|
||
/// <inheritdoc cref="IWorldDelta.CommitAccount"/> | ||
public IWorldDelta CommitAccount(Address address) | ||
=> _accounts.TryGetValue(address, out AccountItem accountItem) | ||
? new WorldDelta( | ||
_accounts.SetItem(address, new AccountItem(accountItem.Account, true))) | ||
: this; | ||
|
||
internal struct AccountItem | ||
{ | ||
public AccountItem(IAccount account, bool committed) | ||
{ | ||
Account = account; | ||
Committed = committed; | ||
} | ||
|
||
public IAccount Account { get; } | ||
|
||
public bool Committed { get; set; } | ||
} | ||
} | ||
} |
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