From 5a276fee25f7385ca1cbcf36129aac6ce94bd347 Mon Sep 17 00:00:00 2001 From: moreal Date: Fri, 17 May 2024 05:13:02 +0900 Subject: [PATCH 1/3] Provide `long`-typed `index` parameter for `stateQuery` field --- .../GraphTypes/StandaloneQuery.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs index 463441863..cc80c7b5e 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)) From b2d36472a09508ca59c7d05f264798a9fbd59f5e Mon Sep 17 00:00:00 2001 From: moreal Date: Fri, 17 May 2024 05:19:36 +0900 Subject: [PATCH 2/3] Provide `long`-typed `index` parameter for `state` field --- .../GraphTypes/StandaloneQuery.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs index cc80c7b5e..5298b2d9f 100644 --- a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs +++ b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs @@ -80,6 +80,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." } ), @@ -91,10 +92,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"); From 48e75508b5251ba7a28da123feaf11a6ac1c073a Mon Sep 17 00:00:00 2001 From: moreal Date: Fri, 17 May 2024 05:20:46 +0900 Subject: [PATCH 3/3] Refactor --- NineChronicles.Headless/GraphTypes/StandaloneQuery.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs index 5298b2d9f..3718a3217 100644 --- a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs +++ b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs @@ -51,12 +51,12 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi }), resolve: context => { - BlockHash? blockHash = (context.GetArgument("hash"), context.GetArgument("index")) switch + BlockHash blockHash = (context.GetArgument("hash"), context.GetArgument("index")) switch { ({ } 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, + (null, null) => standaloneContext.BlockChain.Tip.Hash, }; if (!(standaloneContext.BlockChain is { } chain)) @@ -66,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 ); }