Skip to content

Commit

Permalink
fix: Fix an exception thrown when an evidence query is requested by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Dec 26, 2024
1 parent 7513c9b commit adfc252
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/Libplanet/Blockchain/Policies/BlockPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public BlockPolicy(
if (block.Evidence.Any(evidence => evidence.Height < evidenceExpirationHeight))
{
return new InvalidBlockEvidencePendingDurationException(
$"Block #{block.Index} {block.Hash} includes evidence" +
$"Block #{block.Index} {block.Hash} includes evidence " +
$"that is older than expiration height {evidenceExpirationHeight}");
}

Expand Down
76 changes: 23 additions & 53 deletions tools/Libplanet.Explorer/Queries/EvidenceQuery.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using GraphQL;
using GraphQL.Types;
using Libplanet.Common;
using Libplanet.Explorer.GraphTypes;
using Libplanet.Types.Blocks;
using Libplanet.Types.Evidence;
Expand All @@ -20,75 +18,47 @@ public EvidenceQuery()
Field<NonNullGraphType<ListGraphType<NonNullGraphType<EvidenceType>>>>(
"committedEvidence",
arguments: new QueryArguments(
new QueryArgument<BlockHashType>
{
Name = "blockHash",
DefaultValue = null,
},
new QueryArgument<BooleanGraphType>
{
Name = "desc",
DefaultValue = false,
},
new QueryArgument<IntGraphType>
{
Name = "offset",
DefaultValue = 0,
},
new QueryArgument<IntGraphType>
{
Name = "limit",
DefaultValue = MaxLimit,
}
new QueryArgument<IdGraphType> { Name = "hash" },
new QueryArgument<IdGraphType> { Name = "index" }
),
resolve: context =>
{
var blockHash = context.GetArgument<BlockHash?>("blockHash");
bool desc = context.GetArgument<bool>("desc");
int offset = context.GetArgument<int>("offset");
int? limit = context.GetArgument<int?>("limit");
string hash = context.GetArgument<string>("hash");
long? index = context.GetArgument<long?>("index", null);

return ExplorerQuery.ListCommitEvidence(blockHash, desc, offset, limit);
}
);

Field<NonNullGraphType<ListGraphType<NonNullGraphType<EvidenceType>>>>(
"pendingEvidence",
arguments: new QueryArguments(
new QueryArgument<BooleanGraphType>
if (!(hash is null ^ index is null))
{
Name = "desc",
DefaultValue = false,
},
new QueryArgument<IntGraphType>
throw new ExecutionError(
"The parameters hash and index are mutually exclusive; " +
"give only one at a time.");
}

if (hash is { } nonNullHash)
{
Name = "offset",
DefaultValue = 0,
},
new QueryArgument<IntGraphType>
return ExplorerQuery.ListCommitEvidence(BlockHash.FromString(nonNullHash));
}

if (index is { } nonNullIndex)
{
Name = "limit",
DefaultValue = MaxLimit,
return ExplorerQuery.ListCommitEvidence(nonNullIndex);
}
),
resolve: context =>
{
bool desc = context.GetArgument<bool>("desc");
int offset = context.GetArgument<int>("offset");
int? limit = context.GetArgument<int?>("limit", null);

return ExplorerQuery.ListPendingEvidence(desc, offset, limit);
throw new ExecutionError("Unexpected block query");
}
);

Field<NonNullGraphType<ListGraphType<NonNullGraphType<EvidenceType>>>>(
"pendingEvidence",
resolve: context => ExplorerQuery.ListPendingEvidence()
);

Field<EvidenceType>(
"Evidence",
arguments: new QueryArguments(
new QueryArgument<EvidenceIdType> { Name = "id" }
),
resolve: context => ExplorerQuery.GetEvidence(
new EvidenceId(ByteUtil.ParseHex(context.GetArgument<string>("id")
?? throw new ExecutionError("Given id cannot be null."))))
context.GetArgument<EvidenceId>("id"))
);
}
}
Expand Down
36 changes: 11 additions & 25 deletions tools/Libplanet.Explorer/Queries/ExplorerQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,38 +111,24 @@ internal static IEnumerable<Transaction> ListStagedTransactions(
return stagedTxs;
}

internal static IEnumerable<EvidenceBase> ListPendingEvidence(
bool desc, int offset, int? limit)
internal static IEnumerable<EvidenceBase> ListPendingEvidence()
{
if (offset < 0)
{
throw new ArgumentOutOfRangeException(
nameof(offset),
$"{nameof(ListPendingEvidence)} doesn't support negative offset.");
}

var blockChain = Chain;
var comparer = desc ? EvidenceIdComparer.Descending : EvidenceIdComparer.Ascending;
var evidence = blockChain.GetPendingEvidence()
.Skip(offset)
.Take(limit ?? int.MaxValue)
.OrderBy(ev => ev.Id, comparer);
return blockChain.GetPendingEvidence();
}

return evidence;
internal static IEnumerable<EvidenceBase> ListCommitEvidence(BlockHash blockHash)
{
var blockChain = Chain;
var block = blockChain[blockHash];
return block.Evidence;
}

internal static IEnumerable<EvidenceBase> ListCommitEvidence(
BlockHash? blockHash, bool desc, int offset, int? limit)
internal static IEnumerable<EvidenceBase> ListCommitEvidence(long index)
{
var blockChain = Chain;
var block = blockHash != null ? blockChain[blockHash.Value] : blockChain.Tip;
var comparer = desc ? EvidenceIdComparer.Descending : EvidenceIdComparer.Ascending;
var evidence = block.Evidence
.Skip(offset)
.Take(limit ?? int.MaxValue)
.OrderBy(ev => ev.Id, comparer);

return evidence;
var block = blockChain[index];
return block.Evidence;
}

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

0 comments on commit adfc252

Please sign in to comment.