Skip to content

Commit

Permalink
Removed contextual interpretation of ValidatorSet
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Jan 2, 2024
1 parent 931f530 commit 4ab9e78
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
66 changes: 61 additions & 5 deletions Libplanet.Explorer.Tests/Queries/StateQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,40 @@ public async Task AccountByBlockHashThenTotalSupply()
Assert.IsAssignableFrom<string>(totalSupply["hex"]));
}

[Fact]
public async Task AccountByBlockHashThenValidatorSet()
{
IBlockChainStates source = new MockChainStates();
ExecutionResult result = await ExecuteQueryAsync<StateQuery>(@"
{
account (blockHash: ""01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"") {
validatorSet {
hex
}
}
}
", source: source);

Assert.Null(result.Errors);
ExecutionNode resultData = Assert.IsAssignableFrom<ExecutionNode>(result.Data);
IDictionary<string, object> resultDict =
Assert.IsAssignableFrom<IDictionary<string, object>>(resultData!.ToValue());
IDictionary<string, object> account =
Assert.IsAssignableFrom<IDictionary<string, object>>(resultDict["account"]);

IDictionary<string, object> totalSupply =
Assert.IsAssignableFrom<IDictionary<string, object>>(account["validatorSet"]);
Assert.Equal(
ByteUtil.Hex(_codec.Encode(new ValidatorSet(new List<Validator>
{
new(
PublicKey.FromHex(
"032038e153d344773986c039ba5dbff12ae70cfdf6ea8beb7c5ea9b361a72a9233"),
new BigInteger(1)),
}).Bencoded)),
Assert.IsAssignableFrom<string>(totalSupply["hex"]));
}

[Fact]
public async Task AccountByStateRootHashThenStateAndStates()
{
Expand Down Expand Up @@ -720,11 +754,33 @@ public MockTrie(HashDigest<SHA256>? stateRootHash)

public ITrie Set(in KeyBytes key, IValue value) => throw new NotSupportedException();

public IValue Get(KeyBytes key) => _stateRootHash is { }
? key.Length == (HashDigest<SHA1>.Size * 2 + 2) // Length for total supply key
? new Integer(10000)
: new Integer(123)
: null;
public IValue Get(KeyBytes key)
{
if (_stateRootHash is { })
{
if (key.Length == 3) // Length for validator set key
{
return new ValidatorSet(new List<Validator>
{
new(
PublicKey.FromHex(
"032038e153d344773986c039ba5dbff12ae70cfdf6ea8beb7c5ea9b361a72a9233"),
new BigInteger(1)),
}).Bencoded;
}

if (key.Length == (HashDigest<SHA1>.Size * 2 + 2)) // Length for total supply key
{
return new Integer(10000);
}

return new Integer(123); // Assume we are looking for balance.
}
else
{
return null;
}
}

public IReadOnlyList<IValue> Get(IReadOnlyList<KeyBytes> keys) =>
throw new NotSupportedException();
Expand Down
13 changes: 9 additions & 4 deletions Libplanet.Explorer/GraphTypes/AccountStateType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace Libplanet.Explorer.GraphTypes
{
public class AccountStateType : ObjectGraphType<IAccountState>
{
internal static readonly KeyBytes ValidatorSetKey =
new KeyBytes(new byte[] { _underScore, _underScore, _underScore });

private const byte _underScore = 95; // '_'

private static readonly byte[] _conversionTable =
Expand All @@ -37,10 +40,12 @@ public AccountStateType()
{
Name = "AccountState";
Description =
"Represents raw account state. This is meant to represent a raw storage state " +
"Represents a raw account state. This is meant to represent a raw storage state " +
"void of any application layer context and/or logic. In particular, " +
"this does not deal with currency directly, which requires additional " +
"information on currency such as its ticker and possible minters, etc.";
"this does not deal with currency or fungible asset value directly, " +
"which requires additional information on currency such as its ticker " +
"and possible minters, etc. while interpreting the data retrieved " +
"with the provided contextual information. The same is true for validator sets.";

Field<NonNullGraphType<HashDigestType<SHA256>>>(
name: "stateRootHash",
Expand Down Expand Up @@ -141,7 +146,7 @@ public AccountStateType()
Field<BencodexValueType>(
name: "validatorSet",
description: "The validator set.",
resolve: context => context.Source.GetValidatorSet().Bencoded
resolve: context => context.Source.Trie.Get(ValidatorSetKey)
);
}

Expand Down

0 comments on commit 4ab9e78

Please sign in to comment.