diff --git a/Libplanet.Explorer.Tests/Queries/StateQueryTest.cs b/Libplanet.Explorer.Tests/Queries/StateQueryTest.cs index fc731709368..935e3e4e7eb 100644 --- a/Libplanet.Explorer.Tests/Queries/StateQueryTest.cs +++ b/Libplanet.Explorer.Tests/Queries/StateQueryTest.cs @@ -33,9 +33,7 @@ public async Task States() addresses: [""0x5003712B63baAB98094aD678EA2B24BcE445D076"", ""0x0000000000000000000000000000000000000000""], offsetBlockHash: ""01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"" - ) { - hex - } + ) } ", source: source); Assert.Null(result.Errors); @@ -45,14 +43,7 @@ public async Task States() object[] states = Assert.IsAssignableFrom(resultDict["states"]); Assert.Equal(2, states.Length); - Assert.Equal( - new Dictionary() - { - { "hex", "6e" }, - }, - Assert.IsAssignableFrom>(states[0]) - ); - Assert.Null(states[1]); + Assert.Equal(new[] { new byte[] { 110, }, null }, states); } [Fact] @@ -186,9 +177,7 @@ public async Task ThrowExecutionErrorIfViolateMutualExclusive() ""01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"", offsetStateRootHash: ""c33b27773104f75ac9df5b0533854108bd498fab31e5236b6f1e1f6404d5ef64"" - ) { - hex - } + ) } ", source: source); Assert.IsType(result.Errors); @@ -205,9 +194,7 @@ public async Task StatesBySrh() addresses: [""0x5003712B63baAB98094aD678EA2B24BcE445D076"", ""0x0000000000000000000000000000000000000000""], offsetStateRootHash: ""c33b27773104f75ac9df5b0533854108bd498fab31e5236b6f1e1f6404d5ef64"" - ) { - hex - } + ) } ", source: source); Assert.Null(result.Errors); @@ -216,15 +203,7 @@ public async Task StatesBySrh() Assert.IsAssignableFrom>(resultData!.ToValue()); object[] states = Assert.IsAssignableFrom(resultDict["states"]); - Assert.Equal(2, states.Length); - Assert.Equal( - new Dictionary() - { - { "hex", "6e" }, - }, - Assert.IsAssignableFrom>(states[0]) - ); - Assert.Null(states[1]); + Assert.Equal(new[] { new byte[] { 110, }, null }, states); } [Fact] diff --git a/Libplanet.Explorer/GraphTypes/LegacyBencodexValueType.cs b/Libplanet.Explorer/GraphTypes/LegacyBencodexValueType.cs new file mode 100644 index 00000000000..9bade27fd03 --- /dev/null +++ b/Libplanet.Explorer/GraphTypes/LegacyBencodexValueType.cs @@ -0,0 +1,51 @@ +using System; +using Bencodex; +using GraphQL.Language.AST; +using GraphQL.Types; +using Libplanet.Common; + +namespace Libplanet.Explorer.GraphTypes +{ + public class LegacyBencodexValueType : StringGraphType + { + private static readonly Codec _codec = new(); + + public LegacyBencodexValueType() + { + Name = "BencodexValue"; + } + + public override object? Serialize(object? value) + { + if (value is Bencodex.Types.IValue iv) + { + return _codec.Encode(iv); + } + + return value; + } + + public override object? ParseValue(object? value) + { + return value switch + { + null => null, + string hex => _codec.Decode(ByteUtil.ParseHex(hex)), + _ => throw new ArgumentException( + $"Expected a hexadecimal string but {value}", + nameof(value) + ), + }; + } + + public override object? ParseLiteral(IValue? value) + { + if (value is StringValue) + { + return ParseValue(value.Value); + } + + return null; + } + } +} diff --git a/Libplanet.Explorer/Queries/StateQuery.cs b/Libplanet.Explorer/Queries/StateQuery.cs index 353a29d1f0a..962145f6261 100644 --- a/Libplanet.Explorer/Queries/StateQuery.cs +++ b/Libplanet.Explorer/Queries/StateQuery.cs @@ -15,7 +15,7 @@ public class StateQuery : ObjectGraphType public StateQuery() { Name = "StateQuery"; - Field>>( + Field>>( "states", arguments: new QueryArguments( new QueryArgument>>>