Skip to content

Commit

Permalink
feat: Fix BlockChain API from IAccountState
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee authored and limebell committed Oct 30, 2023
1 parent ca1a1d8 commit 2e39bbd
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 158 deletions.
89 changes: 73 additions & 16 deletions Libplanet.Action/State/IBlockChainStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ public interface IBlockChainStates
/// <param name="offset">The <see cref="BlockHash"/> of the <see cref="Block"/> to fetch
/// the state from.</param>
/// <returns>The state associated to specified <paramref name="address"/>.
/// An absent state is represented as <see langword="null"/>. The returned value
/// must be the same as the single element when retrieved via
/// <see cref="GetStates"/>.
/// An absent state is represented as <see langword="null"/>.
/// </returns>
/// <exception cref="ArgumentException">Thrown when <paramref name="offset"/> is not
/// <see langword="null"/> and one of the following is true.
Expand All @@ -119,10 +117,6 @@ public interface IBlockChainStates
/// </description></item>
/// </list>
/// </exception>
/// <remarks>
/// For performance reasons, it is generally recommended to use <see cref="GetStates"/>
/// with a batch of <see cref="Address"/>es instead of iterating over this method.
/// </remarks>
IValue? GetState(Address address, Address accountAddress, BlockHash? offset);

/// <summary>
Expand All @@ -142,52 +136,115 @@ public interface IBlockChainStates
/// </summary>
/// <param name="address">The owner <see cref="Address"/> to query.</param>
/// <param name="currency">The currency type to query.</param>
/// <param name="accountAddress">The account <see cref="Address"/> to query from.</param>
/// <param name="offset">The <see cref="BlockHash"/> of the <see cref="Block"/> to fetch
/// the states from.</param>
/// <returns>The <paramref name="address"/>'s balance for <paramref name="currency"/>
/// at <paramref name="offset"/>. If absent, returns 0 <see cref="FungibleAssetValue"/>
/// at <paramref name="offset"/> and <paramref name="accountAddress"/>.
/// If absent, returns 0 <see cref="FungibleAssetValue"/>
/// for <paramref name="currency"/>.
/// </returns>
/// <exception cref="ArgumentException">Thrown when <see cref="IAccount"/> at
/// <paramref name="offset"/> cannot be created.</exception>
/// <paramref name="offset"/> and <paramref name="accountAddress"/> cannot be created.
/// </exception>
FungibleAssetValue GetBalance(
Address address,
Currency currency,
Address accountAddress,
BlockHash? offset);

/// <summary>
/// Gets <paramref name="address"/>'s balance for given <paramref name="currency"/> in the
/// <see cref="BlockChain"/> at <paramref name="offset"/>.
/// </summary>
/// <param name="address">The owner <see cref="Address"/> to query.</param>
/// <param name="currency">The currency type to query.</param>
/// <param name="stateRootHash">The state root hash of the <see cref="ITrie"/> to fetch
/// the balance from.</param>
/// <returns>The <paramref name="address"/>'s balance for <paramref name="currency"/>
/// at <paramref name="stateRootHash"/>.
/// If absent, returns 0 <see cref="FungibleAssetValue"/> for <paramref name="currency"/>.
/// </returns>
/// <exception cref="ArgumentException">Thrown when <see cref="IAccount"/> at
/// <paramref name="stateRootHash"/> cannot be created.</exception>
FungibleAssetValue GetBalance(
Address address,
Currency currency,
HashDigest<SHA256>? stateRootHash);

/// <summary>
/// Gets the total supply of a <paramref name="currency"/> in the
/// <see cref="BlockChain"/> at <paramref name="offset"/>, and if not found, returns 0.
/// </summary>
/// <param name="currency">The currency type to query.</param>
/// <param name="accountAddress">The account <see cref="Address"/> to query from.</param>
/// <param name="offset">The <see cref="BlockHash"/> of the <see cref="Block"/> to fetch
/// the states from.</param>
/// <returns>The total supply value of <paramref name="currency"/> at
/// <paramref name="offset"/> in <see cref="FungibleAssetValue"/>.
/// <paramref name="offset"/> and <paramref name="accountAddress"/>
/// in <see cref="FungibleAssetValue"/>.
/// If absent, returns 0 <see cref="FungibleAssetValue"/>
/// for <paramref name="currency"/>.</returns>
/// <exception cref="ArgumentException">Thrown when <see cref="IAccount"/> at
/// <paramref name="offset"/> cannot be created.</exception>
/// <paramref name="offset"/> and <paramref name="accountAddress"/> cannot be created.
/// </exception>
/// <exception cref="TotalSupplyNotTrackableException">Thrown when
/// given <paramref name="currency"/>'s <see cref="Currency.TotalSupplyTrackable"/>
/// is <see langword="false"/>.</exception>
/// <seealso cref="GetAccountState"/>
FungibleAssetValue GetTotalSupply(
Currency currency,
Address accountAddress,
BlockHash? offset);

/// <summary>
/// Gets the total supply of a <paramref name="currency"/> in the
/// <see cref="BlockChain"/> at <paramref name="offset"/>, and if not found, returns 0.
/// </summary>
/// <param name="currency">The currency type to query.</param>
/// <param name="stateRootHash">The state root hash of the <see cref="ITrie"/> to fetch
/// the total supply from.</param>
/// <returns>The total supply value of <paramref name="currency"/> at
/// <paramref name="stateRootHash"/> in <see cref="FungibleAssetValue"/>.
/// If absent, returns 0 <see cref="FungibleAssetValue"/>
/// for <paramref name="currency"/>.</returns>
/// <exception cref="ArgumentException">Thrown when <see cref="IAccount"/> at
/// <paramref name="stateRootHash"/> cannot be created.</exception>
/// <exception cref="TotalSupplyNotTrackableException">Thrown when
/// given <paramref name="currency"/>'s <see cref="Currency.TotalSupplyTrackable"/>
/// is <see langword="false"/>.</exception>
FungibleAssetValue GetTotalSupply(
Currency currency,
HashDigest<SHA256>? stateRootHash);

/// <summary>
/// Returns the validator set in the
/// <see cref="BlockChain"/> at <paramref name="offset"/>.
/// </summary>
/// <param name="accountAddress">The account <see cref="Address"/> to query from.</param>
/// <param name="offset">The <see cref="BlockHash"/> of the <see cref="Block"/> to fetch
/// the states from.</param>
/// <returns>The validator set of type <see cref="ValidatorSet"/> at
/// <paramref name="offset"/>.
/// <paramref name="offset"/> and <paramref name="accountAddress"/>.
/// </returns>
/// <exception cref="ArgumentException">Thrown when <see cref="IAccount"/> at
/// <paramref name="offset"/> and <paramref name="accountAddress"/> cannot be created.
/// </exception>
ValidatorSet GetValidatorSet(
Address accountAddress,
BlockHash? offset);

/// <summary>
/// Returns the validator set in the
/// <see cref="BlockChain"/> at <paramref name="offset"/>.
/// </summary>
/// <param name="stateRootHash">The state root hash of the <see cref="ITrie"/> to fetch
/// the validator set from.</param>
/// <returns>The validator set of type <see cref="ValidatorSet"/> at
/// <paramref name="stateRootHash"/>.
/// </returns>
/// <exception cref="ArgumentException">Thrown when <see cref="IAccount"/> at
/// <paramref name="offset"/> cannot be created.</exception>
/// <seealso cref="GetAccountState"/>
ValidatorSet GetValidatorSet(BlockHash? offset);
/// <paramref name="stateRootHash"/> cannot be created.</exception>
ValidatorSet GetValidatorSet(
HashDigest<SHA256>? stateRootHash);
}
}
4 changes: 2 additions & 2 deletions Libplanet.Explorer.Tests/GeneratedBlockChainFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.Loader;
using Libplanet.Action.State;
using Libplanet.Action.Sys;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
Expand All @@ -12,7 +13,6 @@
using Libplanet.Types.Blocks;
using Libplanet.Types.Consensus;
using Libplanet.Types.Tx;
using Libplanet.Consensus;
using Libplanet.Store;
using Libplanet.Store.Trie;

Expand Down Expand Up @@ -189,7 +189,7 @@ private Transaction
{
var random = new System.Random(seed);
var addr = pk.ToAddress();
var bal = (int)(Chain.GetBalance(addr, TestCurrency).MajorUnit & int.MaxValue);
var bal = (int)(Chain.GetBalance(addr, TestCurrency, ReservedAddresses.LegacyAccount).MajorUnit & int.MaxValue);
return Transaction.Create(
nonce,
pk,
Expand Down
63 changes: 31 additions & 32 deletions Libplanet.Explorer.Tests/Queries/StateQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public async Task Balance()
balance(
owner: ""0x5003712B63baAB98094aD678EA2B24BcE445D076"",
currency: { ticker: ""ABC"", decimalPlaces: 2, totalSupplyTrackable: true },
accountAddress: ""0x1000000000000000000000000000000000000000"",
offsetBlockHash:
""01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b""
) {
Expand Down Expand Up @@ -159,6 +160,7 @@ public async Task TotalSupply()
{
totalSupply(
currency: { ticker: ""ABC"", decimalPlaces: 2, totalSupplyTrackable: true },
accountAddress: ""0x1000000000000000000000000000000000000000"",
offsetBlockHash:
""01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b""
) {
Expand Down Expand Up @@ -212,6 +214,7 @@ public async Task Validators()
ExecutionResult result = await ExecuteQueryAsync<StateQuery>(@"
{
validators(
accountAddress: ""0x1000000000000000000000000000000000000000"",
offsetBlockHash:
""01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b""
) {
Expand Down Expand Up @@ -289,6 +292,7 @@ public async Task BalanceBySrh()
balance(
owner: ""0x5003712B63baAB98094aD678EA2B24BcE445D076"",
currency: { ticker: ""ABC"", decimalPlaces: 2, totalSupplyTrackable: true },
accountAddress: ""0x1000000000000000000000000000000000000000"",
offsetStateRootHash:
""c33b27773104f75ac9df5b0533854108bd498fab31e5236b6f1e1f6404d5ef64""
) {
Expand Down Expand Up @@ -331,6 +335,7 @@ public async Task TotalSupplyBySrh()
{
totalSupply(
currency: { ticker: ""ABC"", decimalPlaces: 2, totalSupplyTrackable: true },
accountAddress: ""0x1000000000000000000000000000000000000000"",
offsetStateRootHash:
""c33b27773104f75ac9df5b0533854108bd498fab31e5236b6f1e1f6404d5ef64""
) {
Expand Down Expand Up @@ -363,6 +368,7 @@ public async Task TotalSupplyBySrh()
{
totalSupply(
currency: { ticker: ""LEG"", decimalPlaces: 0, totalSupplyTrackable: false },
accountAddress: ""0x1000000000000000000000000000000000000000"",
offsetBlockHash:
""01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b""
) {
Expand All @@ -384,6 +390,7 @@ public async Task ValidatorsBySrh()
ExecutionResult result = await ExecuteQueryAsync<StateQuery>(@"
{
validators(
accountAddress: ""0x1000000000000000000000000000000000000000"",
offsetStateRootHash:
""c33b27773104f75ac9df5b0533854108bd498fab31e5236b6f1e1f6404d5ef64""
) {
Expand All @@ -406,50 +413,42 @@ public async Task ValidatorsBySrh()

private class MockChainStates : IBlockChainStates
{
public IReadOnlyList<IValue> GetStates(
IReadOnlyList<Address> addresses,
HashDigest<SHA256>? stateRootHash) =>
addresses.Select(addr => GetAccountState(stateRootHash).GetState(addr)).ToList().AsReadOnly();

public FungibleAssetValue GetBalance(
Address address, Currency currency, BlockHash? offset) =>
new MockAccount().GetBalance(address, currency);

public FungibleAssetValue GetTotalSupply(Currency currency, BlockHash? offset) =>
new MockAccount().GetTotalSupply(currency);

public ValidatorSet GetValidatorSet(BlockHash? offset) =>
new MockAccount().GetValidatorSet();


public IValue? GetState(Address address, Address accountAddress, BlockHash? offset)
=> new MockAccount().GetState(address);
public IWorldState GetWorldState(HashDigest<SHA256>? hash)
=> new MockWorld();

public IWorldState GetWorldState(BlockHash? offset)
=> new MockWorld();

public IAccountState GetAccountState(HashDigest<SHA256>? hash)
=> new MockAccount();

public IAccountState GetAccountState(Address address, BlockHash? blockHash)
=> new MockAccount();

public ITrie GetBlockTrie(BlockHash? offset)
{
throw new System.NotImplementedException();
}
public IValue? GetState(Address address, HashDigest<SHA256>? hash)
=> new MockAccount().GetState(address);

public ITrie GetTrie(HashDigest<SHA256>? hash)
{
throw new System.NotImplementedException();
}
public IValue? GetState(Address address, Address accountAddress, BlockHash? offset)
=> new MockAccount().GetState(address);

public IWorldState GetWorldState(HashDigest<SHA256>? hash)
=> new MockWorld();
public FungibleAssetValue GetBalance(
Address address, Currency currency, HashDigest<SHA256>? hash) =>
new MockAccount().GetBalance(address, currency);

public IAccountState GetAccountState(HashDigest<SHA256>? hash)
=> new MockAccount();
public FungibleAssetValue GetBalance(
Address address, Currency currency, Address accountAddress, BlockHash? offset) =>
new MockAccount().GetBalance(address, currency);

public IValue? GetState(Address address, HashDigest<SHA256>? hash)
=> new MockAccount().GetState(address);
public FungibleAssetValue GetTotalSupply(Currency currency, HashDigest<SHA256>? hash) =>
new MockAccount().GetTotalSupply(currency);

public FungibleAssetValue GetTotalSupply(Currency currency, Address accountAddress, BlockHash? offset) =>
new MockAccount().GetTotalSupply(currency);
public ValidatorSet GetValidatorSet(HashDigest<SHA256>? hash) =>
new MockAccount().GetValidatorSet();

public ValidatorSet GetValidatorSet(Address accountAddress, BlockHash? offset) =>
new MockAccount().GetValidatorSet();
}

private class MockWorld : IWorld
Expand Down
Loading

0 comments on commit 2e39bbd

Please sign in to comment.