Skip to content

Commit adfc252

Browse files
committed
fix: Fix an exception thrown when an evidence query is requested by ID
1 parent 7513c9b commit adfc252

File tree

3 files changed

+35
-79
lines changed

3 files changed

+35
-79
lines changed

src/Libplanet/Blockchain/Policies/BlockPolicy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public BlockPolicy(
160160
if (block.Evidence.Any(evidence => evidence.Height < evidenceExpirationHeight))
161161
{
162162
return new InvalidBlockEvidencePendingDurationException(
163-
$"Block #{block.Index} {block.Hash} includes evidence" +
163+
$"Block #{block.Index} {block.Hash} includes evidence " +
164164
$"that is older than expiration height {evidenceExpirationHeight}");
165165
}
166166

tools/Libplanet.Explorer/Queries/EvidenceQuery.cs

Lines changed: 23 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
21
using GraphQL;
32
using GraphQL.Types;
4-
using Libplanet.Common;
53
using Libplanet.Explorer.GraphTypes;
64
using Libplanet.Types.Blocks;
75
using Libplanet.Types.Evidence;
@@ -20,75 +18,47 @@ public EvidenceQuery()
2018
Field<NonNullGraphType<ListGraphType<NonNullGraphType<EvidenceType>>>>(
2119
"committedEvidence",
2220
arguments: new QueryArguments(
23-
new QueryArgument<BlockHashType>
24-
{
25-
Name = "blockHash",
26-
DefaultValue = null,
27-
},
28-
new QueryArgument<BooleanGraphType>
29-
{
30-
Name = "desc",
31-
DefaultValue = false,
32-
},
33-
new QueryArgument<IntGraphType>
34-
{
35-
Name = "offset",
36-
DefaultValue = 0,
37-
},
38-
new QueryArgument<IntGraphType>
39-
{
40-
Name = "limit",
41-
DefaultValue = MaxLimit,
42-
}
21+
new QueryArgument<IdGraphType> { Name = "hash" },
22+
new QueryArgument<IdGraphType> { Name = "index" }
4323
),
4424
resolve: context =>
4525
{
46-
var blockHash = context.GetArgument<BlockHash?>("blockHash");
47-
bool desc = context.GetArgument<bool>("desc");
48-
int offset = context.GetArgument<int>("offset");
49-
int? limit = context.GetArgument<int?>("limit");
26+
string hash = context.GetArgument<string>("hash");
27+
long? index = context.GetArgument<long?>("index", null);
5028

51-
return ExplorerQuery.ListCommitEvidence(blockHash, desc, offset, limit);
52-
}
53-
);
54-
55-
Field<NonNullGraphType<ListGraphType<NonNullGraphType<EvidenceType>>>>(
56-
"pendingEvidence",
57-
arguments: new QueryArguments(
58-
new QueryArgument<BooleanGraphType>
29+
if (!(hash is null ^ index is null))
5930
{
60-
Name = "desc",
61-
DefaultValue = false,
62-
},
63-
new QueryArgument<IntGraphType>
31+
throw new ExecutionError(
32+
"The parameters hash and index are mutually exclusive; " +
33+
"give only one at a time.");
34+
}
35+
36+
if (hash is { } nonNullHash)
6437
{
65-
Name = "offset",
66-
DefaultValue = 0,
67-
},
68-
new QueryArgument<IntGraphType>
38+
return ExplorerQuery.ListCommitEvidence(BlockHash.FromString(nonNullHash));
39+
}
40+
41+
if (index is { } nonNullIndex)
6942
{
70-
Name = "limit",
71-
DefaultValue = MaxLimit,
43+
return ExplorerQuery.ListCommitEvidence(nonNullIndex);
7244
}
73-
),
74-
resolve: context =>
75-
{
76-
bool desc = context.GetArgument<bool>("desc");
77-
int offset = context.GetArgument<int>("offset");
78-
int? limit = context.GetArgument<int?>("limit", null);
7945

80-
return ExplorerQuery.ListPendingEvidence(desc, offset, limit);
46+
throw new ExecutionError("Unexpected block query");
8147
}
8248
);
8349

50+
Field<NonNullGraphType<ListGraphType<NonNullGraphType<EvidenceType>>>>(
51+
"pendingEvidence",
52+
resolve: context => ExplorerQuery.ListPendingEvidence()
53+
);
54+
8455
Field<EvidenceType>(
8556
"Evidence",
8657
arguments: new QueryArguments(
8758
new QueryArgument<EvidenceIdType> { Name = "id" }
8859
),
8960
resolve: context => ExplorerQuery.GetEvidence(
90-
new EvidenceId(ByteUtil.ParseHex(context.GetArgument<string>("id")
91-
?? throw new ExecutionError("Given id cannot be null."))))
61+
context.GetArgument<EvidenceId>("id"))
9262
);
9363
}
9464
}

tools/Libplanet.Explorer/Queries/ExplorerQuery.cs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -111,38 +111,24 @@ internal static IEnumerable<Transaction> ListStagedTransactions(
111111
return stagedTxs;
112112
}
113113

114-
internal static IEnumerable<EvidenceBase> ListPendingEvidence(
115-
bool desc, int offset, int? limit)
114+
internal static IEnumerable<EvidenceBase> ListPendingEvidence()
116115
{
117-
if (offset < 0)
118-
{
119-
throw new ArgumentOutOfRangeException(
120-
nameof(offset),
121-
$"{nameof(ListPendingEvidence)} doesn't support negative offset.");
122-
}
123-
124116
var blockChain = Chain;
125-
var comparer = desc ? EvidenceIdComparer.Descending : EvidenceIdComparer.Ascending;
126-
var evidence = blockChain.GetPendingEvidence()
127-
.Skip(offset)
128-
.Take(limit ?? int.MaxValue)
129-
.OrderBy(ev => ev.Id, comparer);
117+
return blockChain.GetPendingEvidence();
118+
}
130119

131-
return evidence;
120+
internal static IEnumerable<EvidenceBase> ListCommitEvidence(BlockHash blockHash)
121+
{
122+
var blockChain = Chain;
123+
var block = blockChain[blockHash];
124+
return block.Evidence;
132125
}
133126

134-
internal static IEnumerable<EvidenceBase> ListCommitEvidence(
135-
BlockHash? blockHash, bool desc, int offset, int? limit)
127+
internal static IEnumerable<EvidenceBase> ListCommitEvidence(long index)
136128
{
137129
var blockChain = Chain;
138-
var block = blockHash != null ? blockChain[blockHash.Value] : blockChain.Tip;
139-
var comparer = desc ? EvidenceIdComparer.Descending : EvidenceIdComparer.Ascending;
140-
var evidence = block.Evidence
141-
.Skip(offset)
142-
.Take(limit ?? int.MaxValue)
143-
.OrderBy(ev => ev.Id, comparer);
144-
145-
return evidence;
130+
var block = blockChain[index];
131+
return block.Evidence;
146132
}
147133

148134
internal static Block? GetBlockByHash(BlockHash hash) => Store.GetBlock(hash);

0 commit comments

Comments
 (0)