Skip to content

Commit

Permalink
Merge pull request #3537 from greymistcube/refactor/total-updated-fun…
Browse files Browse the repository at this point in the history
…gible-assets

🧹 Remove internal tracking of amounts for `TotalUpdatedFungibleAssets`
  • Loading branch information
greymistcube authored Dec 5, 2023
2 parents 95f57e6 + e5bf020 commit 480b708
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ be compatible with this version, specifically, those that ran with
null or both should not be null at the same time. [[#3529]]
- (Libplanet.Action) Removed `IAccountDelta` interface. [[#3535]]
- (Libplanet.Action) Removed `IAccount.Delta` interface property. [[#3535]]
- (Libplanet.Action) Changed constructor `IAccount(IAccountState,
IImmutableDictionary<(Address, Currency), BigInteger>)` to
`IAccount(IAccountState, IImmutableHashSet<(Address, Currency)>)`.
[[#3537]]

### Backward-incompatible network protocol changes

Expand All @@ -52,6 +56,7 @@ be compatible with this version, specifically, those that ran with
[#3523]: https://github.com/planetarium/libplanet/pull/3523
[#3529]: https://github.com/planetarium/libplanet/pull/3529
[#3535]: https://github.com/planetarium/libplanet/pull/3535
[#3537]: https://github.com/planetarium/libplanet/pull/3537
[Libplanet 2.0.0]: https://www.nuget.org/packages/Libplanet/2.0.0


Expand Down
9 changes: 3 additions & 6 deletions Libplanet.Action/ActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,9 @@ IActionContext CreateActionContext(IAccount newPrevState)

state = feeCollector.Refund(state);
state = feeCollector.Reward(state);
state = state is Account a
? new Account(
new AccountState(stateStore.Commit(a.Trie)),
a.TotalUpdatedFungibles)
: throw new InvalidOperationException(
$"Internal {nameof(IAccount)} is not of valid type: {state.GetType()}");
state = new Account(
new AccountState(stateStore.Commit(state.Trie)),
state.TotalUpdatedFungibleAssets);

if (!state.Trie.Recorded)
{
Expand Down
20 changes: 8 additions & 12 deletions Libplanet.Action/State/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,23 @@ public class Account : IAccount
private readonly IAccountState _baseState;

public Account(IAccountState baseState)
: this(baseState, ImmutableDictionary<(Address, Currency), BigInteger>.Empty)
: this(baseState, ImmutableHashSet<(Address, Currency)>.Empty)
{
}

public Account(
IAccountState baseState,
IImmutableDictionary<(Address, Currency), BigInteger> totalUpdatedFungibles)
IImmutableSet<(Address, Currency)> totalUpdatedFungibleAssets)
{
_baseState = baseState;
TotalUpdatedFungibles = totalUpdatedFungibles;
TotalUpdatedFungibleAssets = totalUpdatedFungibleAssets;
}

/// <inheritdoc cref="IAccountState.Trie"/>
public ITrie Trie => _baseState.Trie;

/// <inheritdoc/>
public IImmutableSet<(Address, Currency)> TotalUpdatedFungibleAssets =>
TotalUpdatedFungibles.Keys.ToImmutableHashSet();

public IImmutableDictionary<(Address, Currency), BigInteger> TotalUpdatedFungibles
{ get; }
public IImmutableSet<(Address, Currency)> TotalUpdatedFungibleAssets { get; }

/// <inheritdoc/>
[Pure]
Expand Down Expand Up @@ -190,7 +186,7 @@ private Account UpdateState(
new Account(
new AccountState(
Trie.Set(ToStateKey(address), value)),
TotalUpdatedFungibles);
TotalUpdatedFungibleAssets);

[Pure]
private Account UpdateFungibleAssets(
Expand All @@ -203,18 +199,18 @@ private Account UpdateFungibleAssets(
Trie
.Set(ToFungibleAssetKey(address, currency), new Integer(amount))
.Set(ToTotalSupplyKey(currency), new Integer(sa))),
TotalUpdatedFungibles.SetItem((address, currency), amount))
TotalUpdatedFungibleAssets.Add((address, currency)))
: new Account(
new AccountState(
Trie.Set(ToFungibleAssetKey(address, currency), new Integer(amount))),
TotalUpdatedFungibles.SetItem((address, currency), amount));
TotalUpdatedFungibleAssets.Add((address, currency)));

[Pure]
private Account UpdateValidatorSet(ValidatorSet validatorSet) =>
new Account(
new AccountState(
Trie.Set(ValidatorSetKey, validatorSet.Bencoded)),
TotalUpdatedFungibles);
TotalUpdatedFungibleAssets);

[Pure]
private IAccount TransferAssetV0(
Expand Down

0 comments on commit 480b708

Please sign in to comment.