-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3416 from greymistcube/refactor/remove-account-tr…
…ie-get-set Remove trie mutation from Account
- Loading branch information
Showing
15 changed files
with
230 additions
and
86 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
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,83 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Bencodex.Types; | ||
using Libplanet.Crypto; | ||
using Libplanet.Store.Trie; | ||
using Libplanet.Types.Assets; | ||
using Libplanet.Types.Consensus; | ||
using static Libplanet.Action.State.KeyConverters; | ||
|
||
namespace Libplanet.Action.State | ||
{ | ||
/// <summary> | ||
/// A default implementation of <see cref="IAccountState"/> interface. | ||
/// </summary> | ||
public class AccountState : IAccountState | ||
{ | ||
private ITrie _trie; | ||
private AccountStateCache _cache; | ||
|
||
public AccountState(ITrie trie) | ||
{ | ||
_trie = trie; | ||
_cache = new AccountStateCache(); | ||
} | ||
|
||
/// <inheritdoc cref="IAccountState.Trie"/> | ||
public ITrie Trie => _trie; | ||
|
||
/// <inheritdoc cref="IAccountState.GetState"/> | ||
public IValue? GetState(Address address) | ||
{ | ||
if (_cache.TryGetValue(address, out IValue? cachedValue)) | ||
{ | ||
return cachedValue; | ||
} | ||
else | ||
{ | ||
IValue? fetched = Trie.Get(ToStateKey(address)); | ||
_cache.AddOrUpdate(address, fetched); | ||
return fetched; | ||
} | ||
} | ||
|
||
/// <inheritdoc cref="IAccountState.GetStates"/> | ||
public IReadOnlyList<IValue?> GetStates(IReadOnlyList<Address> addresses) => | ||
addresses.Select(address => GetState(address)).ToList(); | ||
|
||
/// <inheritdoc cref="IAccountState.GetBalance"/> | ||
public FungibleAssetValue GetBalance(Address address, Currency currency) | ||
{ | ||
KeyBytes[] keys = new[] { ToFungibleAssetKey(address, currency) }; | ||
IReadOnlyList<IValue?> rawValues = Trie.Get(keys); | ||
return rawValues.Count > 0 && rawValues[0] is Bencodex.Types.Integer i | ||
? FungibleAssetValue.FromRawValue(currency, i) | ||
: currency * 0; | ||
} | ||
|
||
/// <inheritdoc cref="IAccountState.GetTotalSupply"/> | ||
public FungibleAssetValue GetTotalSupply(Currency currency) | ||
{ | ||
if (!currency.TotalSupplyTrackable) | ||
{ | ||
throw TotalSupplyNotTrackableException.WithDefaultMessage(currency); | ||
} | ||
|
||
KeyBytes[] keys = new[] { ToTotalSupplyKey(currency) }; | ||
IReadOnlyList<IValue?> rawValues = Trie.Get(keys); | ||
return rawValues.Count > 0 && rawValues[0] is Bencodex.Types.Integer i | ||
? FungibleAssetValue.FromRawValue(currency, i) | ||
: currency * 0; | ||
} | ||
|
||
/// <inheritdoc cref="IAccountState.GetValidatorSet"/> | ||
public ValidatorSet GetValidatorSet() | ||
{ | ||
KeyBytes[] keys = new[] { ValidatorSetKey }; | ||
IReadOnlyList<IValue?> rawValues = Trie.Get(keys); | ||
return rawValues.Count > 0 && rawValues[0] is List list | ||
? new ValidatorSet(list) | ||
: new ValidatorSet(); | ||
} | ||
} | ||
} |
Oops, something went wrong.