Skip to content

Commit

Permalink
Merge pull request #2477 from moreal/state-by-index
Browse files Browse the repository at this point in the history
Provide `index` parameter when to query state
  • Loading branch information
moreal authored May 17, 2024
2 parents 84b91fc + 48e7550 commit 9c08c57
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions NineChronicles.Headless/GraphTypes/StandaloneQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi
{
Name = "hash",
Description = "Offset block hash for query.",
},
new QueryArgument<LongGraphType>
{
Name = "index",
Description = "Offset block index for query."
}),
resolve: context =>
{
BlockHash? blockHash = context.GetArgument<byte[]>("hash") switch
BlockHash blockHash = (context.GetArgument<byte[]?>("hash"), context.GetArgument<long?>("index")) switch
{
byte[] bytes => new BlockHash(bytes),
null => standaloneContext.BlockChain?.Tip?.Hash,
({ } bytes, null) => new BlockHash(bytes),
(null, { } index) => standaloneContext.BlockChain[index].Hash,
(not null, not null) => throw new ArgumentException("Only one of 'hash' and 'index' must be given."),
(null, null) => standaloneContext.BlockChain.Tip.Hash,
};

if (!(standaloneContext.BlockChain is { } chain))
Expand All @@ -59,11 +66,7 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi

return new StateContext(
chain.GetWorldState(blockHash),
blockHash switch
{
BlockHash bh => chain[bh].Index,
null => chain.Tip!.Index,
},
chain[blockHash].Index,
stateMemoryCache
);
}
Expand All @@ -73,6 +76,7 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi
name: "state",
arguments: new QueryArguments(
new QueryArgument<ByteStringType> { Name = "hash", Description = "The hash of the block used to fetch state from chain." },
new QueryArgument<LongGraphType> { Name = "index", Description = "The index of the block used to fetch state from chain." },
new QueryArgument<NonNullGraphType<AddressType>> { Name = "accountAddress", Description = "The address of account to fetch from the chain." },
new QueryArgument<NonNullGraphType<AddressType>> { Name = "address", Description = "The address of state to fetch from the account." }
),
Expand All @@ -84,10 +88,14 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi
$"{nameof(StandaloneContext)}.{nameof(StandaloneContext.BlockChain)} was not set yet!");
}

var blockHashByteArray = context.GetArgument<byte[]>("hash");
var blockHash = blockHashByteArray is null
? blockChain.Tip.Hash
: new BlockHash(blockHashByteArray);
var blockHash = (context.GetArgument<byte[]?>("hash"), context.GetArgument<long?>("index")) switch
{
(not null, not null) => throw new ArgumentException(
"Only one of 'hash' and 'index' must be given."),
(null, { } index) => blockChain[index].Hash,
({ } bytes, null) => new BlockHash(bytes),
(null, null) => blockChain.Tip.Hash,
};
var accountAddress = context.GetArgument<Address>("accountAddress");
var address = context.GetArgument<Address>("address");

Expand Down

0 comments on commit 9c08c57

Please sign in to comment.