Skip to content

Commit

Permalink
[explorer] minimize checkpoint fetching (MystenLabs#10250)
Browse files Browse the repository at this point in the history
## Description

Minimize the data fetching for checkpoints by checking if the digest is
valid first. This also reorganizes conditions so that we prefer early
return instead.

## Test Plan

Tested locally.

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
Jordan-Mysten authored Mar 31, 2023
1 parent 9bdbd7a commit 456f1b3
Showing 1 changed file with 39 additions and 43 deletions.
82 changes: 39 additions & 43 deletions apps/explorer/src/hooks/useSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const getResultsForTransaction = async (
query: string
) => {
if (!isValidTransactionDigest(query)) return null;

const txdata = await rpc.getTransactionBlock({ digest: query });
return {
label: 'transaction',
Expand All @@ -46,38 +45,37 @@ const getResultsForObject = async (rpc: JsonRpcProvider, query: string) => {
if (!isValidSuiObjectId(normalized)) return null;

const { data, error } = await rpc.getObject({ id: normalized });
if (is(data, SuiObjectData) && !error) {
return {
label: 'object',
results: [
{
id: data.objectId,
label: data.objectId,
type: 'object',
},
],
};
}

return null;
if (!is(data, SuiObjectData) || error) return null;

return {
label: 'object',
results: [
{
id: data.objectId,
label: data.objectId,
type: 'object',
},
],
};
};

const getResultsForCheckpoint = async (rpc: JsonRpcProvider, query: string) => {
// Checkpoint digests have the same format as transaction digests:
if (!isValidTransactionDigest(query)) return null;

const { digest } = await rpc.getCheckpoint({ id: query });
if (digest) {
return {
label: 'checkpoint',
results: [
{
id: digest,
label: digest,
type: 'checkpoint',
},
],
};
}

return null;
if (!digest) return null;

return {
label: 'checkpoint',
results: [
{
id: digest,
label: digest,
type: 'checkpoint',
},
],
};
};

const getResultsForAddress = async (rpc: JsonRpcProvider, query: string) => {
Expand All @@ -96,20 +94,18 @@ const getResultsForAddress = async (rpc: JsonRpcProvider, query: string) => {
}),
]);

if (from.data?.length || to.data?.length) {
return {
label: 'address',
results: [
{
id: normalized,
label: normalized,
type: 'address',
},
],
};
}

return null;
if (!from.data?.length && !to.data?.length) return null;

return {
label: 'address',
results: [
{
id: normalized,
label: normalized,
type: 'address',
},
],
};
};

export function useSearch(query: string) {
Expand Down

0 comments on commit 456f1b3

Please sign in to comment.