diff --git a/.Lib9c.Tests/Action/Common/MockWorldState.cs b/.Lib9c.Tests/Action/Common/MockWorldState.cs index a19f62ffe1..6771fb20ee 100644 --- a/.Lib9c.Tests/Action/Common/MockWorldState.cs +++ b/.Lib9c.Tests/Action/Common/MockWorldState.cs @@ -61,6 +61,11 @@ public MockWorldState SetAccountState(Address address, IAccountState accountStat public MockWorldState SetState(Address accountAddress, Address address, IValue state) => SetAccountState(accountAddress, GetMockAccountState(accountAddress).SetState(address, state)); + public FungibleAssetValue GetBalance(Address address, Currency currency) + => GetAccountState(ReservedAddresses.LegacyAccount).Trie.Get(MockKeyConverters.ToFungibleAssetKey(address, currency)) is Integer value + ? FungibleAssetValue.FromRawValue(currency, value) + : currency * 0; + public MockWorldState SetBalance( Address address, FungibleAssetValue amount) @@ -128,6 +133,11 @@ public MockWorldState TransferBalance( => SubtractBalance(sender, currency, rawAmount) .AddBalance(recipient, currency, rawAmount); + public FungibleAssetValue GetTotalSupply(Currency currency) + => GetAccountState(ReservedAddresses.LegacyAccount).Trie.Get(MockKeyConverters.ToTotalSupplyKey(currency)) is Integer value + ? FungibleAssetValue.FromRawValue(currency, value) + : currency * 0; + public MockWorldState SetTotalSupply(FungibleAssetValue amount) => SetTotalSupply(amount.Currency, amount.RawValue); @@ -155,6 +165,11 @@ public MockWorldState SubtractTotalSupply(Currency currency, BigInteger rawAmoun GetMockAccountState(ReservedAddresses.LegacyAccount) .SubtractTotalSupply(currency, rawAmount)); + public ValidatorSet GetValidatorSet() + => GetMockAccountState(ReservedAddresses.LegacyAccount).Trie.Get(MockKeyConverters.ValidatorSetKey) is { } value + ? new ValidatorSet(value) + : new ValidatorSet(); + public MockWorldState SetValidator(Validator validator) => SetAccountState( ReservedAddresses.LegacyAccount, diff --git a/.Lib9c.Tests/Policy/BlockPolicyTest.cs b/.Lib9c.Tests/Policy/BlockPolicyTest.cs index f5591b66cb..f420bec26e 100644 --- a/.Lib9c.Tests/Policy/BlockPolicyTest.cs +++ b/.Lib9c.Tests/Policy/BlockPolicyTest.cs @@ -396,7 +396,6 @@ public void EarnMiningGoldWhenSuccessMining() blockChain.Append(block, GenerateBlockCommit(block, adminPrivateKey)); FungibleAssetValue actualBalance = blockChain .GetWorldState() - .GetAccountState(ReservedAddresses.LegacyAccount) .GetBalance(adminAddress, _currency); FungibleAssetValue expectedBalance = new FungibleAssetValue(_currency, 10, 0); Assert.True(expectedBalance.Equals(actualBalance)); diff --git a/.Lib9c.Tools/SubCommand/Account.cs b/.Lib9c.Tools/SubCommand/Account.cs index 7eee98c734..ca4ec8408c 100644 --- a/.Lib9c.Tools/SubCommand/Account.cs +++ b/.Lib9c.Tools/SubCommand/Account.cs @@ -62,7 +62,6 @@ public void Balance( FungibleAssetValue balance = chain .GetWorldState(offset.Hash) - .GetAccountState(ReservedAddresses.LegacyAccount) .GetBalance(addr, gold); Console.WriteLine("{0}\t{1}", addr, balance); return; @@ -94,7 +93,6 @@ i.GoldDistributions is Bencodex.Types.List l FungibleAssetValue balance = chain .GetWorldState(offset.Hash) - .GetAccountState(ReservedAddresses.LegacyAccount) .GetBalance(addr, gold); Console.WriteLine("{0}\t{1}", addr, balance); printed.Add(addr); diff --git a/.Libplanet b/.Libplanet index 7d95f209e8..81ddb0a52b 160000 --- a/.Libplanet +++ b/.Libplanet @@ -1 +1 @@ -Subproject commit 7d95f209e8358f2f90568cfc4de9ff819c8eea40 +Subproject commit 81ddb0a52b2ea66034848270f3d8a6630432585b diff --git a/.Libplanet.Extensions.RemoteBlockChainStates/RemoteAccount.cs b/.Libplanet.Extensions.RemoteBlockChainStates/RemoteAccount.cs index 69e7860b71..c9471db66b 100644 --- a/.Libplanet.Extensions.RemoteBlockChainStates/RemoteAccount.cs +++ b/.Libplanet.Extensions.RemoteBlockChainStates/RemoteAccount.cs @@ -22,21 +22,12 @@ public RemoteAccount(IAccountState baseState) public ITrie Trie => _baseState.Trie; - public FungibleAssetValue GetBalance(Address address, Currency currency) - => _baseState.GetBalance(address, currency); - public IValue? GetState(Address address) => _baseState.GetState(address); public IReadOnlyList GetStates(IReadOnlyList
addresses) => _baseState.GetStates(addresses); - public FungibleAssetValue GetTotalSupply(Currency currency) - => _baseState.GetTotalSupply(currency); - - public ValidatorSet GetValidatorSet() - => _baseState.GetValidatorSet(); - public IAccount MintAsset(IActionContext context, Address recipient, FungibleAssetValue value) { throw new NotSupportedException(); diff --git a/.Libplanet.Extensions.RemoteBlockChainStates/RemoteAccountState.cs b/.Libplanet.Extensions.RemoteBlockChainStates/RemoteAccountState.cs index c9ac2bb574..dd4aef008f 100644 --- a/.Libplanet.Extensions.RemoteBlockChainStates/RemoteAccountState.cs +++ b/.Libplanet.Extensions.RemoteBlockChainStates/RemoteAccountState.cs @@ -8,9 +8,7 @@ using Libplanet.Common; using Libplanet.Crypto; using Libplanet.Store.Trie; -using Libplanet.Types.Assets; using Libplanet.Types.Blocks; -using Libplanet.Types.Consensus; namespace Libplanet.Extensions.RemoteBlockChainStates; @@ -121,131 +119,6 @@ public RemoteAccountState( return response.Data.StateQuery.States is { } state ? codec.Decode(state) : null; } - public FungibleAssetValue GetBalance(Address address, Currency currency) - { - object? currencyInput = currency.TotalSupplyTrackable ? new - { - ticker = currency.Ticker, - decimalPlaces = currency.DecimalPlaces, - minters = currency.Minters?.Select(addr => addr.ToString()).ToArray(), - totalSupplyTrackable = currency.TotalSupplyTrackable, - maximumSupplyMajorUnit = currency.MaximumSupply.Value.MajorUnit, - maximumSupplyMinorUnit = currency.MaximumSupply.Value.MinorUnit, - } : new - { - ticker = currency.Ticker, - decimalPlaces = currency.DecimalPlaces, - minters = currency.Minters?.Select(addr => addr.ToString()).ToArray(), - totalSupplyTrackable = currency.TotalSupplyTrackable, - }; - var response = _graphQlHttpClient.SendQueryAsync( - new GraphQLRequest( - @"query GetBalance( - $owner: Address!, - $currency: CurrencyInput!, - $accountStateRootHash: HashDigest_SHA256!) - { - stateQuery - { - balance( - owner: $owner, - currency: $currency, - accountStateRootHash: $accountStateRootHash) - { - string - } - } - }", - operationName: "GetBalance", - variables: new - { - owner = address.ToString(), - currency = currencyInput, - accountStateRootHash = Trie.Hash is { } accountSrh - ? ByteUtil.Hex(accountSrh.ByteArray) - : null, - })).Result; - - return FungibleAssetValue.Parse(currency, response.Data.StateQuery.Balance.String.Split()[0]); - } - - public FungibleAssetValue GetTotalSupply(Currency currency) - { - object? currencyInput = currency.TotalSupplyTrackable ? new - { - ticker = currency.Ticker, - decimalPlaces = currency.DecimalPlaces, - minters = currency.Minters.Select(addr => addr.ToString()).ToArray(), - totalSupplyTrackable = currency.TotalSupplyTrackable, - maximumSupplyMajorUnit = currency.MaximumSupply.Value.MajorUnit, - maximumSupplyMinorUnit = currency.MaximumSupply.Value.MinorUnit, - } : new - { - ticker = currency.Ticker, - decimalPlaces = currency.DecimalPlaces, - minters = currency.Minters.Select(addr => addr.ToString()).ToArray(), - totalSupplyTrackable = currency.TotalSupplyTrackable, - }; - var response = _graphQlHttpClient.SendQueryAsync( - new GraphQLRequest( - @"query GetTotalSupply( - $currency: CurrencyInput!, - $accountStateRootHash: HashDigest_SHA256!) - { - stateQuery - { - totalSupply( - currency: $currency, - offsetBlockHash: $offsetBlockHash - accountStateRootHash: $accountStateRootHash) - { - string - } - } - }", - operationName: "GetTotalSupply", - variables: new - { - currency = currencyInput, - accountStateRootHash = Trie.Hash is { } accountSrh - ? ByteUtil.Hex(accountSrh.ByteArray) - : null, - })).Result; - - return FungibleAssetValue.Parse(currency, response.Data.StateQuery.TotalSupply.String.Split()[0]); - } - - public ValidatorSet GetValidatorSet() - { - var response = _graphQlHttpClient.SendQueryAsync( - new GraphQLRequest( - @"query GetValidators( - $accountStateRootHash: HashDigest_SHA256!) - { - stateQuery - { - validators( - accountStateRootHash: $accountStateRootHash) - { - publicKey - power - } - } - }", - operationName: "GetValidators", - variables: new - { - accountStateRootHash = Trie.Hash is { } accountSrh - ? ByteUtil.Hex(accountSrh.ByteArray) - : null, - })).Result; - - return new ValidatorSet(response.Data.StateQuery.Validators - .Select(x => - new Validator(new PublicKey(ByteUtil.ParseHex(x.PublicKey)), x.Power)) - .ToList()); - } - private class GetAccountStateResponseType { public StateQueryWithAccountStateType StateQuery { get; set; } @@ -270,46 +143,4 @@ private class StateQueryWithStatesType { public byte[] States { get; set; } } - - private class GetBalanceResponseType - { - public StateQueryWithBalanceType StateQuery { get; set; } - } - - private class StateQueryWithBalanceType - { - public FungibleAssetValueWithStringType Balance { get; set; } - } - - private class FungibleAssetValueWithStringType - { - public string String { get; set; } - } - - private class GetTotalSupplyResponseType - { - public StateQueryWithTotalSupplyType StateQuery { get; set; } - } - - private class StateQueryWithTotalSupplyType - { - public FungibleAssetValueWithStringType TotalSupply { get; set; } - } - - private class GetValidatorsResponseType - { - public StateQueryWithValidatorsType StateQuery { get; set; } - } - - private class StateQueryWithValidatorsType - { - public ValidatorType[] Validators { get; set; } - } - - private class ValidatorType - { - public string PublicKey { get; set; } - - public long Power { get; set; } - } } diff --git a/.Libplanet.Extensions.RemoteBlockChainStates/RemoteWorldState.cs b/.Libplanet.Extensions.RemoteBlockChainStates/RemoteWorldState.cs index 4ccffd1619..6d2977b219 100644 --- a/.Libplanet.Extensions.RemoteBlockChainStates/RemoteWorldState.cs +++ b/.Libplanet.Extensions.RemoteBlockChainStates/RemoteWorldState.cs @@ -6,7 +6,9 @@ using Libplanet.Common; using Libplanet.Crypto; using Libplanet.Store.Trie; +using Libplanet.Types.Assets; using Libplanet.Types.Blocks; +using Libplanet.Types.Consensus; namespace Libplanet.Extensions.RemoteBlockChainStates; @@ -77,6 +79,131 @@ public RemoteWorldState(Uri explorerEndpoint, HashDigest? offsetStateRoo public IAccountState GetAccountState(Address address) => new RemoteAccountState(_explorerEndpoint, address, Trie.Hash); + public FungibleAssetValue GetBalance(Address address, Currency currency) + { + object? currencyInput = currency.TotalSupplyTrackable ? new + { + ticker = currency.Ticker, + decimalPlaces = currency.DecimalPlaces, + minters = currency.Minters?.Select(addr => addr.ToString()).ToArray(), + totalSupplyTrackable = currency.TotalSupplyTrackable, + maximumSupplyMajorUnit = currency.MaximumSupply.Value.MajorUnit, + maximumSupplyMinorUnit = currency.MaximumSupply.Value.MinorUnit, + } : new + { + ticker = currency.Ticker, + decimalPlaces = currency.DecimalPlaces, + minters = currency.Minters?.Select(addr => addr.ToString()).ToArray(), + totalSupplyTrackable = currency.TotalSupplyTrackable, + }; + var response = _graphQlHttpClient.SendQueryAsync( + new GraphQLRequest( + @"query GetBalance( + $owner: Address!, + $currency: CurrencyInput!, + $accountStateRootHash: HashDigest_SHA256!) + { + stateQuery + { + balance( + owner: $owner, + currency: $currency, + accountStateRootHash: $accountStateRootHash) + { + string + } + } + }", + operationName: "GetBalance", + variables: new + { + owner = address.ToString(), + currency = currencyInput, + accountStateRootHash = Trie.Hash is { } accountSrh + ? ByteUtil.Hex(accountSrh.ByteArray) + : null, + })).Result; + + return FungibleAssetValue.Parse(currency, response.Data.StateQuery.Balance.String.Split()[0]); + } + + public FungibleAssetValue GetTotalSupply(Currency currency) + { + object? currencyInput = currency.TotalSupplyTrackable ? new + { + ticker = currency.Ticker, + decimalPlaces = currency.DecimalPlaces, + minters = currency.Minters.Select(addr => addr.ToString()).ToArray(), + totalSupplyTrackable = currency.TotalSupplyTrackable, + maximumSupplyMajorUnit = currency.MaximumSupply.Value.MajorUnit, + maximumSupplyMinorUnit = currency.MaximumSupply.Value.MinorUnit, + } : new + { + ticker = currency.Ticker, + decimalPlaces = currency.DecimalPlaces, + minters = currency.Minters.Select(addr => addr.ToString()).ToArray(), + totalSupplyTrackable = currency.TotalSupplyTrackable, + }; + var response = _graphQlHttpClient.SendQueryAsync( + new GraphQLRequest( + @"query GetTotalSupply( + $currency: CurrencyInput!, + $accountStateRootHash: HashDigest_SHA256!) + { + stateQuery + { + totalSupply( + currency: $currency, + offsetBlockHash: $offsetBlockHash + accountStateRootHash: $accountStateRootHash) + { + string + } + } + }", + operationName: "GetTotalSupply", + variables: new + { + currency = currencyInput, + accountStateRootHash = Trie.Hash is { } accountSrh + ? ByteUtil.Hex(accountSrh.ByteArray) + : null, + })).Result; + + return FungibleAssetValue.Parse(currency, response.Data.StateQuery.TotalSupply.String.Split()[0]); + } + + public ValidatorSet GetValidatorSet() + { + var response = _graphQlHttpClient.SendQueryAsync( + new GraphQLRequest( + @"query GetValidators( + $accountStateRootHash: HashDigest_SHA256!) + { + stateQuery + { + validators( + accountStateRootHash: $accountStateRootHash) + { + publicKey + power + } + } + }", + operationName: "GetValidators", + variables: new + { + accountStateRootHash = Trie.Hash is { } accountSrh + ? ByteUtil.Hex(accountSrh.ByteArray) + : null, + })).Result; + + return new ValidatorSet(response.Data.StateQuery.Validators + .Select(x => + new Validator(new PublicKey(ByteUtil.ParseHex(x.PublicKey)), x.Power)) + .ToList()); + } + public class GetWorldStateResponseType { public StateQueryWithWorldStateType StateQuery { get; set; } @@ -93,4 +220,46 @@ public class WorldStateType public bool Legacy { get; set; } } + + private class GetBalanceResponseType + { + public StateQueryWithBalanceType StateQuery { get; set; } + } + + private class StateQueryWithBalanceType + { + public FungibleAssetValueWithStringType Balance { get; set; } + } + + private class FungibleAssetValueWithStringType + { + public string String { get; set; } + } + + private class GetTotalSupplyResponseType + { + public StateQueryWithTotalSupplyType StateQuery { get; set; } + } + + private class StateQueryWithTotalSupplyType + { + public FungibleAssetValueWithStringType TotalSupply { get; set; } + } + + private class GetValidatorsResponseType + { + public StateQueryWithValidatorsType StateQuery { get; set; } + } + + private class StateQueryWithValidatorsType + { + public ValidatorType[] Validators { get; set; } + } + + private class ValidatorType + { + public string PublicKey { get; set; } + + public long Power { get; set; } + } } diff --git a/Lib9c.Policy/Policy/BlockPolicySource.cs b/Lib9c.Policy/Policy/BlockPolicySource.cs index edd16ab940..c18d245bf0 100644 --- a/Lib9c.Policy/Policy/BlockPolicySource.cs +++ b/Lib9c.Policy/Policy/BlockPolicySource.cs @@ -167,7 +167,6 @@ internal IBlockPolicy GetPolicy( { if (blockChain .GetWorldState() - .GetAccountState(ReservedAddresses.LegacyAccount) .GetBalance(MeadConfig.PatronAddress, Currencies.Mead) < 1 * Currencies.Mead) { // Check Activation diff --git a/Lib9c/Action/RewardGold.cs b/Lib9c/Action/RewardGold.cs index 13a2e21265..a4fcebbb71 100644 --- a/Lib9c/Action/RewardGold.cs +++ b/Lib9c/Action/RewardGold.cs @@ -308,8 +308,7 @@ public IWorld MinerReward(IActionContext ctx, IWorld states) public static IWorld TransferMead(IActionContext context, IWorld states) { #pragma warning disable LAA1002 - var targetAddresses = states.GetAccount(ReservedAddresses.LegacyAccount) - .TotalUpdatedFungibleAssets + var targetAddresses = states.TotalUpdatedFungibleAssets #pragma warning restore LAA1002 .Where(pair => pair.Item2.Equals(Currencies.Mead)) .Select(pair => pair.Item1) diff --git a/Lib9c/Module/LegacyModule.cs b/Lib9c/Module/LegacyModule.cs index d4313b3577..044d356fe0 100644 --- a/Lib9c/Module/LegacyModule.cs +++ b/Lib9c/Module/LegacyModule.cs @@ -13,7 +13,6 @@ using Libplanet.Common; using Libplanet.Crypto; using Libplanet.Types.Assets; -using Libplanet.Types.Consensus; using LruCacheNet; using Nekoyume.Action; using Nekoyume.Helper; @@ -33,11 +32,6 @@ public static class LegacyModule private static readonly LruCache SheetsCache = new LruCache(SheetsCacheSize); - // Basic implementations from IAccount and IAccountState - public static IImmutableSet<(Address, Currency)> TotalUpdatedFungibleAssets( - this IWorld worldState) => - worldState.GetAccount(ReservedAddresses.LegacyAccount).TotalUpdatedFungibleAssets; - public static IValue GetLegacyState(this IWorldState worldState, Address address) => worldState.GetAccountState(ReservedAddresses.LegacyAccount).GetState(address); @@ -51,58 +45,6 @@ public static IWorld SetLegacyState(this IWorld world, Address address, IValue s ReservedAddresses.LegacyAccount, world.GetAccount(ReservedAddresses.LegacyAccount).SetState(address, state)); - public static FungibleAssetValue GetBalance( - this IWorldState worldState, - Address address, - Currency currency) => - worldState.GetAccountState(ReservedAddresses.LegacyAccount).GetBalance(address, currency); - - public static FungibleAssetValue GetTotalSupply(this IWorldState worldState, Currency currency) => - worldState.GetAccountState(ReservedAddresses.LegacyAccount).GetTotalSupply(currency); - - public static IWorld MintAsset( - this IWorld world, - IActionContext context, - Address recipient, - FungibleAssetValue value) => - world.SetAccount( - ReservedAddresses.LegacyAccount, - world.GetAccount(ReservedAddresses.LegacyAccount) - .MintAsset(context, recipient, value)); - - public static ValidatorSet GetValidatorSet(this IWorldState worldState) => - worldState.GetAccountState(ReservedAddresses.LegacyAccount).GetValidatorSet(); - - public static IWorld TransferAsset( - this IWorld world, - IActionContext context, - Address sender, - Address recipient, - FungibleAssetValue value, - bool allowNegativeBalance = false) => - world.SetAccount( - ReservedAddresses.LegacyAccount, - world.GetAccount(ReservedAddresses.LegacyAccount) - .TransferAsset(context, sender, recipient, value, allowNegativeBalance)); - - public static IWorld BurnAsset( - this IWorld world, - IActionContext context, - Address owner, - FungibleAssetValue value) => - world.SetAccount( - ReservedAddresses.LegacyAccount, - world.GetAccount(ReservedAddresses.LegacyAccount) - .BurnAsset(context, owner, value)); - - public static IWorld SetValidator( - this IWorld world, - Libplanet.Types.Consensus.Validator validator) => - world.SetAccount( - ReservedAddresses.LegacyAccount, - world.GetAccount(ReservedAddresses.LegacyAccount) - .SetValidator(validator)); - // Methods from AccountExtensions public static IWorld MarkBalanceChanged( this IWorld world, @@ -113,7 +55,7 @@ params Address[] accounts { if (accounts.Length == 1) { - return MintAsset(world, context, accounts[0], currency * 1); + return world.MintAsset(context, accounts[0], currency * 1); } else if (accounts.Length < 1) { @@ -122,8 +64,7 @@ params Address[] accounts for (int i = 1; i < accounts.Length; i++) { - world = TransferAsset( - world, + world = world.TransferAsset( context, accounts[i - 1], accounts[i], @@ -173,11 +114,11 @@ public static IWorld SetWorldBossKillReward( { if (reward.Currency.Equals(CrystalCalculator.CRYSTAL)) { - world = MintAsset(world, context, agentAddress, reward); + world = world.MintAsset(context, agentAddress, reward); } else { - world = MintAsset(world, context, avatarAddress, reward); + world = world.MintAsset(context, avatarAddress, reward); } } } @@ -205,7 +146,7 @@ public static IWorld Mead( while (true) { var price = rawValue * Currencies.Mead; - var balance = GetBalance(world, signer, Currencies.Mead); + var balance = world.GetBalance(signer, Currencies.Mead); if (balance < price) { var requiredMead = price - balance; @@ -215,7 +156,7 @@ public static IWorld Mead( var patron = contract[0].ToAddress(); try { - world = TransferAsset(world, context, patron, signer, requiredMead); + world = world.TransferAsset(context, patron, signer, requiredMead); } catch (InsufficientBalanceException) { @@ -270,7 +211,7 @@ public static bool TryGetGoldBalance( { try { - balance = GetBalance(worldState, address, currency); + balance = worldState.GetBalance(address, currency); return true; } catch (BalanceDoesNotExistsException) @@ -284,7 +225,7 @@ public static GoldBalanceState GetGoldBalanceState( this IWorldState worldState, Address address, Currency currency - ) => new GoldBalanceState(address, GetBalance(worldState, address, currency)); + ) => new GoldBalanceState(address, worldState.GetBalance(address, currency)); public static Currency GetGoldCurrency(this IWorldState worldState) { @@ -932,7 +873,7 @@ public static FungibleAssetValue GetStakedAmount( Address agentAddr) { var goldCurrency = GetGoldCurrency(worldState); - return GetBalance(worldState, StakeState.DeriveAddress(agentAddr), goldCurrency); + return worldState.GetBalance(StakeState.DeriveAddress(agentAddr), goldCurrency); } public static bool TryGetStakeStateV2(