Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add IAccount.RemoveState() #3577

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ be compatible with this version, specifically, those that ran with
- (Libplanet.Store) Removed `BaseNode`. [[#3574]]
- (Libplanet.Store) Added `ITrie.Remove()` interface method. [[#3576]]
- (Libplanet.Store) Added `FullNode.RemoveChild()` method. [[#3576]]
- (Libplanet.Action) Added `IAccount.RemoveState()` interface method.
[[#3577]]

[#3559]: https://github.com/planetarium/libplanet/pull/3559
[#3560]: https://github.com/planetarium/libplanet/pull/3560
Expand All @@ -51,6 +53,7 @@ be compatible with this version, specifically, those that ran with
[#3573]: https://github.com/planetarium/libplanet/pull/3573
[#3574]: https://github.com/planetarium/libplanet/pull/3574
[#3576]: https://github.com/planetarium/libplanet/pull/3576
[#3577]: https://github.com/planetarium/libplanet/pull/3577


Version 3.9.2
Expand Down
18 changes: 13 additions & 5 deletions Libplanet.Action/State/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public Account(
[Pure]
public IAccount SetState(Address address, IValue state) => UpdateState(address, state);

/// <inheritdoc/>
[Pure]
public IAccount RemoveState(Address address) => UpdateState(address, null);

/// <inheritdoc/>
[Pure]
public FungibleAssetValue GetBalance(Address address, Currency currency) =>
Expand Down Expand Up @@ -182,11 +186,15 @@ public IAccount SetValidator(Validator validator) =>
[Pure]
private Account UpdateState(
Address address,
IValue value) =>
new Account(
new AccountState(
Trie.Set(ToStateKey(address), value)),
TotalUpdatedFungibleAssets);
IValue? value) => value is { } v
? new Account(
new AccountState(
Trie.Set(ToStateKey(address), v)),
TotalUpdatedFungibleAssets)
: new Account(
new AccountState(
Trie.Remove(ToStateKey(address))),
TotalUpdatedFungibleAssets);

[Pure]
private Account UpdateFungibleAssets(
Expand Down
17 changes: 17 additions & 0 deletions Libplanet.Action/State/IAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ public interface IAccount : IAccountState
[Pure]
IAccount SetState(Address address, IValue state);

/// <summary>
/// Gets a new instance that the account state of the given
/// <paramref name="address"/> is removed.
/// </summary>
/// <param name="address">The <see cref="Address"/> referring
/// the account to remove its state.</param>
/// <returns>A new <see cref="IAccount"/> instance that
/// the account state of the given <paramref name="address"/>
/// is removed.</returns>
/// <remarks>
/// This method method does not manipulate the instance,
/// but returns a new <see cref="IAccount"/> instance
/// with updated states instead.
/// </remarks>
[Pure]
IAccount RemoveState(Address address);

/// <summary>
/// Mints the fungible asset <paramref name="value"/> (i.e., in-game monetary),
/// and give it to the <paramref name="recipient"/>.
Expand Down
5 changes: 5 additions & 0 deletions Libplanet.Explorer.Tests/Queries/StateQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ public IAccount SetState(Address address, IValue state)
throw new System.NotImplementedException();
}

public IAccount RemoveState(Address address)
{
throw new System.NotImplementedException();
}

public IAccount MintAsset(IActionContext context, Address recipient, FungibleAssetValue value)
{
throw new System.NotImplementedException();
Expand Down
17 changes: 17 additions & 0 deletions Libplanet.Tests/Action/AccountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ public virtual void States()
Assert.Equal("z", (Text)b.GetState(_addr[0]));
}

[Fact]
public void RemoveState()
{
IAccount a = _initAccount.SetState(_addr[0], (Text)"A");
a = a.SetState(_addr[1], (Text)"B");
Assert.Equal((Text)"A", a.GetState(_addr[0]));
Assert.Equal((Text)"B", a.GetState(_addr[1]));

a = a.RemoveState(_addr[0]);
Assert.Null(a.GetState(_addr[0]));
Assert.Equal((Text)"B", a.GetState(_addr[1]));

a = a.RemoveState(_addr[1]);
Assert.Null(a.GetState(_addr[0]));
Assert.Null(a.GetState(_addr[1]));
}

[Fact]
public virtual void FungibleAssets()
{
Expand Down
Loading