diff --git a/CHANGES.md b/CHANGES.md index 12578d9ba0b..74ab08f6174 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,10 @@ To be released. type `IReadOnlyList?` to `IActionContext`. [[#3713]] - (Libplanet.Action) Removed `TotalFungibleAssets` property from `IWorld`. [[#3714]] + - (Libplanet.Action) Changed `GetBalance()`, `GetTotalSupply()`, and + `GetValidatorSet()` of `IWorldState` to extension methods. [[#3715]] + - (Libplanet.Action) Changed `MintAsset()`, `BurnAsset()`, `TransferAsset()`, + and `SetValidator()` of `IWorld` to extension methods. [[#3715]] ### Backward-incompatible network protocol changes @@ -31,6 +35,7 @@ To be released. [#3713]: https://github.com/planetarium/libplanet/pull/3713 [#3714]: https://github.com/planetarium/libplanet/pull/3714 +[#3715]: https://github.com/planetarium/libplanet/pull/3715 Version 4.2.0 diff --git a/Libplanet.Action/ActionEvaluator.cs b/Libplanet.Action/ActionEvaluator.cs index 4bc94099e60..f5fe07d8e00 100644 --- a/Libplanet.Action/ActionEvaluator.cs +++ b/Libplanet.Action/ActionEvaluator.cs @@ -575,8 +575,7 @@ private static IWorld CommitLegacyWorld(IWorld prevWorld, IStateStore stateStore return new World( new WorldBaseState( stateStore.Commit(prevWorld.GetAccount(ReservedAddresses.LegacyAccount).Trie), - stateStore), - new WorldDelta()); + stateStore)); } private static IWorld CommitWorld(IWorld prevWorld, IStateStore stateStore) @@ -590,8 +589,7 @@ private static IWorld CommitWorld(IWorld prevWorld, IStateStore stateStore) } return new World( - new WorldBaseState(stateStore.Commit(worldTrie), stateStore), - new WorldDelta()); + new WorldBaseState(stateStore.Commit(worldTrie), stateStore)); } [Pure] diff --git a/Libplanet.Action/State/IWorld.cs b/Libplanet.Action/State/IWorld.cs index 5746689f099..96d44413f91 100644 --- a/Libplanet.Action/State/IWorld.cs +++ b/Libplanet.Action/State/IWorld.cs @@ -1,10 +1,7 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; using System.Diagnostics.Contracts; using Libplanet.Crypto; -using Libplanet.Types.Assets; -using Libplanet.Types.Consensus; namespace Libplanet.Action.State { @@ -76,92 +73,5 @@ public interface IWorld : IWorldState /// [Pure] IWorld SetAccount(Address address, IAccount account); - - /// - /// Mints the fungible asset (i.e., in-game monetary), - /// and give it to the . - /// - /// The of the - /// executing this method. - /// The address who receives the minted asset. - /// The asset value to mint. - /// A new instance that the given is added to 's balance. - /// Thrown when the - /// is less than or equal to 0. - /// Thrown when a transaction signer - /// (or a miner in case of block actions) is not a member of the 's . - /// Thrown when the sum of the - /// to be minted and the current total supply amount of the - /// exceeds the - /// . - [Pure] - IWorld MintAsset(IActionContext context, Address recipient, FungibleAssetValue value); - - /// - /// Burns the fungible asset (i.e., in-game monetary) from - /// 's balance. - /// - /// The of the - /// executing this method. - /// The address who owns the fungible asset to burn. - /// The fungible asset to burn. - /// A new instance that the given is subtracted from 's balance. - /// Thrown when the - /// is less than or equal to zero. - /// Thrown when a transaction signer - /// (or a miner in case of block actions) is not a member of the 's . - /// Thrown when the - /// has insufficient balance than to burn. - [Pure] - IWorld BurnAsset(IActionContext context, Address owner, FungibleAssetValue value); - - /// - /// Transfers the fungible asset (i.e., in-game monetary) - /// from the to the . - /// - /// The of the - /// executing this method. - /// The address who sends the fungible asset to - /// the . - /// The address who receives the fungible asset from - /// the . - /// The asset value to transfer. - /// Turn on to allow 's balance - /// less than zero. Turned off by default. - /// A new instance that the given is subtracted from 's balance and added to - /// 's balance. - /// Thrown when the - /// is less than or equal to zero. - /// Thrown when the - /// has insufficient balance than to transfer and - /// the option is turned off. - /// - /// The behavior is different depending on 's - /// . There is a bug for version 0 - /// where this may not act as intended. Such behavior is left intact for backward - /// compatibility. - /// - [Pure] - IWorld TransferAsset( - IActionContext context, - Address sender, - Address recipient, - FungibleAssetValue value, - bool allowNegativeBalance = false); - - /// - /// Sets to the stored . - /// If 0 is given as its power, removes the validator from the . - /// - /// The instance to write. - /// A new instance with - /// set. - [Pure] - IWorld SetValidator(Validator validator); } } diff --git a/Libplanet.Action/State/IWorldExtensions.cs b/Libplanet.Action/State/IWorldExtensions.cs new file mode 100644 index 00000000000..8de5fe62253 --- /dev/null +++ b/Libplanet.Action/State/IWorldExtensions.cs @@ -0,0 +1,367 @@ +using System; +using System.Diagnostics.Contracts; +using System.Numerics; +using Bencodex.Types; +using Libplanet.Crypto; +using Libplanet.Types.Assets; +using Libplanet.Types.Consensus; +using static Libplanet.Action.State.KeyConverters; + +namespace Libplanet.Action.State +{ + public static class IWorldExtensions + { + /// + /// Queries 's balance of the . + /// + /// The to read from. + /// The owner address to query. + /// The currency type to query. + /// + /// The 's balance of the . + /// + [Pure] + public static FungibleAssetValue GetBalance( + this IWorldState worldState, + Address address, + Currency currency) + { + IAccountState account = worldState.GetAccountState(ReservedAddresses.LegacyAccount); + IValue? value = account.Trie.Get(ToFungibleAssetKey(address, currency)); + return value is Integer i + ? FungibleAssetValue.FromRawValue(currency, i) + : currency * 0; + } + + /// + /// Mints the fungible asset (i.e., in-game monetary), + /// and give it to the . + /// + /// The to manipulate. + /// The of the + /// executing this method. + /// The address who receives the minted asset. + /// The asset value to mint. + /// A new instance that the given is added to 's balance. + /// Thrown when the + /// is less than or equal to 0. + /// Thrown when a transaction signer + /// (or a miner in case of block actions) is not a member of the 's . + /// Thrown when the sum of the + /// to be minted and the current total supply amount of the + /// exceeds the + /// . + [Pure] + public static IWorld MintAsset( + this IWorld world, + IActionContext context, + Address recipient, + FungibleAssetValue value) + { + if (value.Sign <= 0) + { + throw new ArgumentOutOfRangeException( + nameof(value), + "The value to mint has to be greater than zero." + ); + } + + Currency currency = value.Currency; + if (!currency.AllowsToMint(context.Signer)) + { + throw new CurrencyPermissionException( + $"The account {context.Signer} has no permission to mint currency {currency}.", + context.Signer, + currency + ); + } + + FungibleAssetValue balance = GetBalance(world, recipient, currency); + BigInteger rawBalance = (balance + value).RawValue; + + if (currency.TotalSupplyTrackable) + { + var currentTotalSupply = GetTotalSupply(world, currency); + if (currency.MaximumSupply < currentTotalSupply + value) + { + var msg = $"The amount {value} attempted to be minted added to the current" + + $" total supply of {currentTotalSupply} exceeds the" + + $" maximum allowed supply of {currency.MaximumSupply}."; + throw new SupplyOverflowException(msg, value); + } + + return UpdateFungibleAssets( + world, + recipient, + currency, + rawBalance, + (currentTotalSupply + value).RawValue); + } + else + { + return UpdateFungibleAssets(world, recipient, currency, rawBalance); + } + } + + /// + /// Burns the fungible asset (i.e., in-game monetary) from + /// 's balance. + /// + /// The to manipulate. + /// The of the + /// executing this method. + /// The address who owns the fungible asset to burn. + /// The fungible asset to burn. + /// A new instance that the given is subtracted from 's balance. + /// Thrown when the + /// is less than or equal to zero. + /// Thrown when a transaction signer + /// (or a miner in case of block actions) is not a member of the 's . + /// Thrown when the + /// has insufficient balance than to burn. + [Pure] + public static IWorld BurnAsset( + this IWorld world, + IActionContext context, + Address owner, + FungibleAssetValue value) + { + string msg; + if (value.Sign <= 0) + { + throw new ArgumentOutOfRangeException( + nameof(value), + "The value to burn has to be greater than zero." + ); + } + + Currency currency = value.Currency; + if (!currency.AllowsToMint(context.Signer)) + { + msg = $"The account {context.Signer} has no permission to burn assets of " + + $"the currency {currency}."; + throw new CurrencyPermissionException(msg, context.Signer, currency); + } + + FungibleAssetValue balance = world.GetBalance(owner, currency); + + if (balance < value) + { + msg = $"The account {owner}'s balance of {currency} is insufficient to burn: " + + $"{balance} < {value}."; + throw new InsufficientBalanceException(msg, owner, balance); + } + + BigInteger rawBalance = (balance - value).RawValue; + if (currency.TotalSupplyTrackable) + { + var currentTotalSupply = world.GetTotalSupply(currency); + return UpdateFungibleAssets( + world, + owner, + currency, + rawBalance, + (currentTotalSupply - value).RawValue); + } + else + { + return UpdateFungibleAssets(world, owner, currency, rawBalance); + } + } + + /// + /// Transfers the fungible asset (i.e., in-game monetary) + /// from the to the . + /// + /// The to manipulate. + /// The of the + /// executing this method. + /// The address who sends the fungible asset to + /// the . + /// The address who receives the fungible asset from + /// the . + /// The asset value to transfer. + /// Turn on to allow 's balance + /// less than zero. Turned off by default. + /// A new instance that the given is subtracted from 's balance and added to + /// 's balance. + /// Thrown when the + /// is less than or equal to zero. + /// Thrown when the + /// has insufficient balance than to transfer and + /// the option is turned off. + /// + /// The behavior is different depending on 's + /// . There is a bug for version 0 + /// where this may not act as intended. Such behavior is left intact for backward + /// compatibility. + /// + [Pure] + public static IWorld TransferAsset( + this IWorld world, + IActionContext context, + Address sender, + Address recipient, + FungibleAssetValue value, + bool allowNegativeBalance = false) => context.BlockProtocolVersion > 0 + ? TransferAssetV1(world, sender, recipient, value, allowNegativeBalance) + : TransferAssetV0(world, sender, recipient, value, allowNegativeBalance); + + /// + /// Returns the total supply of a . + /// + /// The to read from. + /// The currency type to query. + /// The total supply of the . + /// + /// Thrown when the total supply of the + /// given is not trackable. + /// + [Pure] + public static FungibleAssetValue GetTotalSupply( + this IWorldState worldState, + Currency currency) + { + if (!currency.TotalSupplyTrackable) + { + throw TotalSupplyNotTrackableException.WithDefaultMessage(currency); + } + + IAccountState account = worldState.GetAccountState(ReservedAddresses.LegacyAccount); + IValue? value = account.Trie.Get(ToTotalSupplyKey(currency)); + return value is Integer i + ? FungibleAssetValue.FromRawValue(currency, i) + : currency * 0; + } + + /// + /// Returns the validator set. + /// + /// The to read from. + /// The validator set of type . + /// + [Pure] + public static ValidatorSet GetValidatorSet(this IWorldState worldState) + { + IAccountState account = worldState.GetAccountState(ReservedAddresses.LegacyAccount); + IValue? value = account.Trie.Get(ValidatorSetKey); + return value is List list + ? new ValidatorSet(list) + : new ValidatorSet(); + } + + /// + /// Sets to the stored . + /// If 0 is given as its power, removes the validator from the . + /// + /// The to manipulate. + /// The instance to write. + /// A new instance with + /// set. + [Pure] + public static IWorld SetValidator(this IWorld world, Validator validator) => + UpdateValidatorSet(world, world.GetValidatorSet().Update(validator)); + + [Pure] + private static IWorld UpdateFungibleAssets( + IWorld world, + Address address, + Currency currency, + BigInteger amount, + BigInteger? supplyAmount = null) + { + IAccount account = supplyAmount is { } sa + ? new Account(new AccountState( + world.GetAccount(ReservedAddresses.LegacyAccount).Trie + .Set(ToFungibleAssetKey(address, currency), new Integer(amount)) + .Set(ToTotalSupplyKey(currency), new Integer(sa)))) + : new Account(new AccountState( + world.GetAccount(ReservedAddresses.LegacyAccount).Trie + .Set(ToFungibleAssetKey(address, currency), new Integer(amount)))); + + return world.SetAccount(ReservedAddresses.LegacyAccount, account); + } + + [Pure] + private static IWorld TransferAssetV0( + IWorld world, + Address sender, + Address recipient, + FungibleAssetValue value, + bool allowNegativeBalance = false) + { + if (value.Sign <= 0) + { + throw new ArgumentOutOfRangeException( + nameof(value), + "The value to transfer has to be greater than zero." + ); + } + + Currency currency = value.Currency; + FungibleAssetValue senderBalance = world.GetBalance(sender, currency); + FungibleAssetValue recipientBalance = world.GetBalance(recipient, currency); + + if (!allowNegativeBalance && senderBalance < value) + { + var msg = $"The account {sender}'s balance of {currency} is insufficient to " + + $"transfer: {senderBalance} < {value}."; + throw new InsufficientBalanceException(msg, sender, senderBalance); + } + + IWorld intermediate = UpdateFungibleAssets( + world, sender, currency, (senderBalance - value).RawValue); + return UpdateFungibleAssets( + intermediate, recipient, currency, (recipientBalance + value).RawValue); + } + + [Pure] + private static IWorld TransferAssetV1( + IWorld world, + Address sender, + Address recipient, + FungibleAssetValue value, + bool allowNegativeBalance = false) + { + if (value.Sign <= 0) + { + throw new ArgumentOutOfRangeException( + nameof(value), + "The value to transfer has to be greater than zero." + ); + } + + Currency currency = value.Currency; + FungibleAssetValue senderBalance = world.GetBalance(sender, currency); + + if (!allowNegativeBalance && senderBalance < value) + { + var msg = $"The account {sender}'s balance of {currency} is insufficient to " + + $"transfer: {senderBalance} < {value}."; + throw new InsufficientBalanceException(msg, sender, senderBalance); + } + + BigInteger senderRawBalance = (senderBalance - value).RawValue; + IWorld intermediate = UpdateFungibleAssets(world, sender, currency, senderRawBalance); + FungibleAssetValue recipientBalance = intermediate.GetBalance(recipient, currency); + BigInteger recipientRawBalance = (recipientBalance + value).RawValue; + + return UpdateFungibleAssets(intermediate, recipient, currency, recipientRawBalance); + } + + [Pure] + private static IWorld UpdateValidatorSet(IWorld world, ValidatorSet validatorSet) + { + IAccount account = world.GetAccount(ReservedAddresses.LegacyAccount); + return world.SetAccount( + ReservedAddresses.LegacyAccount, + new Account(new AccountState( + account.Trie.Set(ValidatorSetKey, validatorSet.Bencoded)))); + } + } +} diff --git a/Libplanet.Action/State/IWorldState.cs b/Libplanet.Action/State/IWorldState.cs index a0231404f7b..2ed9dbce3a4 100644 --- a/Libplanet.Action/State/IWorldState.cs +++ b/Libplanet.Action/State/IWorldState.cs @@ -2,8 +2,6 @@ using System.Diagnostics.Contracts; using Libplanet.Crypto; using Libplanet.Store.Trie; -using Libplanet.Types.Assets; -using Libplanet.Types.Consensus; namespace Libplanet.Action.State { @@ -51,36 +49,5 @@ public interface IWorldState /// instead. [Pure] IAccountState GetAccountState(Address address); - - /// - /// Queries 's balance of the . - /// - /// The owner address to query. - /// The currency type to query. - /// - /// The 's balance of the . - /// - [Pure] - FungibleAssetValue GetBalance(Address address, Currency currency); - - /// - /// Returns the total supply of a . - /// - /// The currency type to query. - /// The total supply of the . - /// - /// Thrown when the total supply of the - /// given is not trackable. - /// - [Pure] - FungibleAssetValue GetTotalSupply(Currency currency); - - /// - /// Returns the validator set. - /// - /// The validator set of type . - /// - [Pure] - ValidatorSet GetValidatorSet(); } } diff --git a/Libplanet.Action/State/World.cs b/Libplanet.Action/State/World.cs index d87916a3613..c0c9de8b95f 100644 --- a/Libplanet.Action/State/World.cs +++ b/Libplanet.Action/State/World.cs @@ -1,13 +1,7 @@ using System; -using System.Collections.Immutable; using System.Diagnostics.Contracts; -using System.Numerics; -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 { @@ -24,7 +18,7 @@ public World(IWorldState baseState) { } - public World(IWorldState baseState, IWorldDelta delta) + private World(IWorldState baseState, IWorldDelta delta) { _baseState = baseState; Delta = delta; @@ -67,236 +61,5 @@ public IWorld SetAccount(Address address, IAccount account) return new World(_baseState, Delta.SetAccount(address, account)); } - - /// - public FungibleAssetValue GetBalance(Address address, Currency currency) - { - IAccountState account = GetAccountState(ReservedAddresses.LegacyAccount); - IValue? value = account.Trie.Get(ToFungibleAssetKey(address, currency)); - return value is Integer i - ? FungibleAssetValue.FromRawValue(currency, i) - : currency * 0; - } - - /// - public FungibleAssetValue GetTotalSupply(Currency currency) - { - if (!currency.TotalSupplyTrackable) - { - throw TotalSupplyNotTrackableException.WithDefaultMessage(currency); - } - - IAccountState account = GetAccountState(ReservedAddresses.LegacyAccount); - IValue? value = account.Trie.Get(ToTotalSupplyKey(currency)); - return value is Integer i - ? FungibleAssetValue.FromRawValue(currency, i) - : currency * 0; - } - - /// - public ValidatorSet GetValidatorSet() - { - IAccountState account = GetAccountState(ReservedAddresses.LegacyAccount); - IValue? value = account.Trie.Get(ValidatorSetKey); - return value is List list - ? new ValidatorSet(list) - : new ValidatorSet(); - } - - /// - public IWorld MintAsset(IActionContext context, Address recipient, FungibleAssetValue value) - { - if (value.Sign <= 0) - { - throw new ArgumentOutOfRangeException( - nameof(value), - "The value to mint has to be greater than zero." - ); - } - - Currency currency = value.Currency; - if (!currency.AllowsToMint(context.Signer)) - { - throw new CurrencyPermissionException( - $"The account {context.Signer} has no permission to mint currency {currency}.", - context.Signer, - currency - ); - } - - FungibleAssetValue balance = GetBalance(recipient, currency); - BigInteger rawBalance = (balance + value).RawValue; - - if (currency.TotalSupplyTrackable) - { - var currentTotalSupply = GetTotalSupply(currency); - if (currency.MaximumSupply < currentTotalSupply + value) - { - var msg = $"The amount {value} attempted to be minted added to the current" - + $" total supply of {currentTotalSupply} exceeds the" - + $" maximum allowed supply of {currency.MaximumSupply}."; - throw new SupplyOverflowException(msg, value); - } - - return UpdateFungibleAssets( - recipient, - currency, - rawBalance, - (currentTotalSupply + value).RawValue); - } - else - { - return UpdateFungibleAssets(recipient, currency, rawBalance); - } - } - - /// - public IWorld BurnAsset(IActionContext context, Address owner, FungibleAssetValue value) - { - string msg; - if (value.Sign <= 0) - { - throw new ArgumentOutOfRangeException( - nameof(value), - "The value to burn has to be greater than zero." - ); - } - - Currency currency = value.Currency; - if (!currency.AllowsToMint(context.Signer)) - { - msg = $"The account {context.Signer} has no permission to burn assets of " + - $"the currency {currency}."; - throw new CurrencyPermissionException(msg, context.Signer, currency); - } - - FungibleAssetValue balance = GetBalance(owner, currency); - - if (balance < value) - { - msg = $"The account {owner}'s balance of {currency} is insufficient to burn: " + - $"{balance} < {value}."; - throw new InsufficientBalanceException(msg, owner, balance); - } - - BigInteger rawBalance = (balance - value).RawValue; - if (currency.TotalSupplyTrackable) - { - var currentTotalSupply = GetTotalSupply(currency); - return UpdateFungibleAssets( - owner, - currency, - rawBalance, - (currentTotalSupply - value).RawValue); - } - else - { - return UpdateFungibleAssets(owner, currency, rawBalance); - } - } - - /// - public IWorld TransferAsset( - IActionContext context, - Address sender, - Address recipient, - FungibleAssetValue value, - bool allowNegativeBalance = false) => context.BlockProtocolVersion > 0 - ? TransferAssetV1(sender, recipient, value, allowNegativeBalance) - : TransferAssetV0(sender, recipient, value, allowNegativeBalance); - - /// - public IWorld SetValidator(Validator validator) => - UpdateValidatorSet(GetValidatorSet().Update(validator)); - - private IWorld UpdateFungibleAssets( - Address address, - Currency currency, - BigInteger amount, - BigInteger? supplyAmount = null) - { - IAccount account = supplyAmount is { } sa - ? new Account(new AccountState( - GetAccount(ReservedAddresses.LegacyAccount).Trie - .Set(ToFungibleAssetKey(address, currency), new Integer(amount)) - .Set(ToTotalSupplyKey(currency), new Integer(sa)))) - : new Account(new AccountState( - GetAccount(ReservedAddresses.LegacyAccount).Trie - .Set(ToFungibleAssetKey(address, currency), new Integer(amount)))); - - return SetAccount(ReservedAddresses.LegacyAccount, account); - } - - private IWorld UpdateValidatorSet(ValidatorSet validatorSet) - { - IAccount account = GetAccount(ReservedAddresses.LegacyAccount); - return SetAccount( - ReservedAddresses.LegacyAccount, - new Account(new AccountState( - account.Trie.Set(ValidatorSetKey, validatorSet.Bencoded)))); - } - - private IWorld TransferAssetV0( - Address sender, - Address recipient, - FungibleAssetValue value, - bool allowNegativeBalance = false) - { - if (value.Sign <= 0) - { - throw new ArgumentOutOfRangeException( - nameof(value), - "The value to transfer has to be greater than zero." - ); - } - - Currency currency = value.Currency; - FungibleAssetValue senderBalance = GetBalance(sender, currency); - FungibleAssetValue recipientBalance = GetBalance(recipient, currency); - - if (!allowNegativeBalance && senderBalance < value) - { - var msg = $"The account {sender}'s balance of {currency} is insufficient to " + - $"transfer: {senderBalance} < {value}."; - throw new InsufficientBalanceException(msg, sender, senderBalance); - } - - return ((World)UpdateFungibleAssets(sender, currency, (senderBalance - value).RawValue)) - .UpdateFungibleAssets(recipient, currency, (recipientBalance + value).RawValue); - } - - [Pure] - private IWorld TransferAssetV1( - Address sender, - Address recipient, - FungibleAssetValue value, - bool allowNegativeBalance = false) - { - if (value.Sign <= 0) - { - throw new ArgumentOutOfRangeException( - nameof(value), - "The value to transfer has to be greater than zero." - ); - } - - Currency currency = value.Currency; - FungibleAssetValue senderBalance = GetBalance(sender, currency); - - if (!allowNegativeBalance && senderBalance < value) - { - var msg = $"The account {sender}'s balance of {currency} is insufficient to " + - $"transfer: {senderBalance} < {value}."; - throw new InsufficientBalanceException(msg, sender, senderBalance); - } - - BigInteger senderRawBalance = (senderBalance - value).RawValue; - IWorld intermediate = UpdateFungibleAssets(sender, currency, senderRawBalance); - FungibleAssetValue recipientBalance = intermediate.GetBalance(recipient, currency); - BigInteger recipientRawBalance = (recipientBalance + value).RawValue; - - return ((World)intermediate).UpdateFungibleAssets( - recipient, currency, recipientRawBalance); - } } } diff --git a/Libplanet.Action/State/WorldBaseState.cs b/Libplanet.Action/State/WorldBaseState.cs index ace4de4c8f3..997ac86f4d1 100644 --- a/Libplanet.Action/State/WorldBaseState.cs +++ b/Libplanet.Action/State/WorldBaseState.cs @@ -4,8 +4,6 @@ using Libplanet.Crypto; using Libplanet.Store; using Libplanet.Store.Trie; -using Libplanet.Types.Assets; -using Libplanet.Types.Consensus; using static Libplanet.Action.State.KeyConverters; namespace Libplanet.Action.State @@ -48,40 +46,5 @@ public IAccountState GetAccountState(Address address) : new AccountState(_stateStore.GetStateRoot(null)); } } - - /// - public FungibleAssetValue GetBalance(Address address, Currency currency) - { - IAccountState account = GetAccountState(ReservedAddresses.LegacyAccount); - IValue? value = account.Trie.Get(ToFungibleAssetKey(address, currency)); - return value is Integer i - ? FungibleAssetValue.FromRawValue(currency, i) - : currency * 0; - } - - /// - public FungibleAssetValue GetTotalSupply(Currency currency) - { - if (!currency.TotalSupplyTrackable) - { - throw TotalSupplyNotTrackableException.WithDefaultMessage(currency); - } - - IAccountState account = GetAccountState(ReservedAddresses.LegacyAccount); - IValue? value = account.Trie.Get(ToTotalSupplyKey(currency)); - return value is Integer i - ? FungibleAssetValue.FromRawValue(currency, i) - : currency * 0; - } - - /// - public ValidatorSet GetValidatorSet() - { - IAccountState account = GetAccountState(ReservedAddresses.LegacyAccount); - IValue? value = account.Trie.Get(ValidatorSetKey); - return value is List list - ? new ValidatorSet(list) - : new ValidatorSet(); - } } } diff --git a/Libplanet.Mocks/MockWorldState.cs b/Libplanet.Mocks/MockWorldState.cs index 301e281cdef..753bcaba738 100644 --- a/Libplanet.Mocks/MockWorldState.cs +++ b/Libplanet.Mocks/MockWorldState.cs @@ -97,24 +97,6 @@ public IAccountState GetAccountState(Address address) => ? _stateStore.GetStateRoot(new HashDigest(stateRootNotNull)) : _stateStore.GetStateRoot(null)); - public FungibleAssetValue GetBalance(Address address, Currency currency) => - GetAccountState(ReservedAddresses.LegacyAccount).Trie - .Get(ToFungibleAssetKey(address, currency)) is Integer i - ? FungibleAssetValue.FromRawValue(currency, i) - : currency * 0; - - public FungibleAssetValue GetTotalSupply(Currency currency) => - GetAccountState(ReservedAddresses.LegacyAccount).Trie - .Get(ToTotalSupplyKey(currency)) is Integer i - ? FungibleAssetValue.FromRawValue(currency, i) - : currency * 0; - - public ValidatorSet GetValidatorSet() => - GetAccountState(ReservedAddresses.LegacyAccount).Trie - .Get(ValidatorSetKey) is List l - ? new ValidatorSet(l) - : new ValidatorSet(); - /// /// Converts to "modern" if /// is . Otherwise, does nothing.