diff --git a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs index 463441863..3718a3217 100644 --- a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs +++ b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs @@ -43,13 +43,20 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi { Name = "hash", Description = "Offset block hash for query.", + }, + new QueryArgument + { + Name = "index", + Description = "Offset block index for query." }), resolve: context => { - BlockHash? blockHash = context.GetArgument("hash") switch + BlockHash blockHash = (context.GetArgument("hash"), context.GetArgument("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)) @@ -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 ); } @@ -73,6 +76,7 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi name: "state", arguments: new QueryArguments( new QueryArgument { Name = "hash", Description = "The hash of the block used to fetch state from chain." }, + new QueryArgument { Name = "index", Description = "The index of the block used to fetch state from chain." }, new QueryArgument> { Name = "accountAddress", Description = "The address of account to fetch from the chain." }, new QueryArgument> { Name = "address", Description = "The address of state to fetch from the account." } ), @@ -84,10 +88,14 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi $"{nameof(StandaloneContext)}.{nameof(StandaloneContext.BlockChain)} was not set yet!"); } - var blockHashByteArray = context.GetArgument("hash"); - var blockHash = blockHashByteArray is null - ? blockChain.Tip.Hash - : new BlockHash(blockHashByteArray); + var blockHash = (context.GetArgument("hash"), context.GetArgument("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
("accountAddress"); var address = context.GetArgument
("address");