explorer: Hide compute units if log parsing fails (#24848)

This commit is contained in:
Justin Starry 2022-04-30 04:11:59 +08:00 committed by GitHub
parent 2d7e29477a
commit 45314a89b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 28 deletions

View File

@ -45,7 +45,7 @@ type TransactionWithInvocations = {
signature?: TransactionSignature; signature?: TransactionSignature;
meta: ConfirmedTransactionMeta | null; meta: ConfirmedTransactionMeta | null;
invocations: Map<string, number>; invocations: Map<string, number>;
computeUnits: number; computeUnits?: number;
logTruncated: boolean; logTruncated: boolean;
}; };
@ -92,16 +92,22 @@ export function BlockHistoryCard({ block }: { block: BlockResponse }) {
invokedPrograms.set(programId, programTransactionCount + 1); invokedPrograms.set(programId, programTransactionCount + 1);
} }
const parsedLogs = parseProgramLogs( let logTruncated = false;
tx.meta?.logMessages ?? [], let computeUnits: number | undefined = undefined;
tx.meta?.err ?? null, try {
cluster const parsedLogs = parseProgramLogs(
); tx.meta?.logMessages ?? [],
tx.meta?.err ?? null,
cluster
);
const logTruncated = parsedLogs[parsedLogs.length - 1].truncated; logTruncated = parsedLogs[parsedLogs.length - 1].truncated;
const computeUnits = parsedLogs computeUnits = parsedLogs
.map(({ computeUnits }) => computeUnits) .map(({ computeUnits }) => computeUnits)
.reduce((sum, next) => sum + next); .reduce((sum, next) => sum + next);
} catch (err) {
// ignore parsing errors because some old logs aren't parsable
}
return { return {
index, index,
@ -116,9 +122,12 @@ export function BlockHistoryCard({ block }: { block: BlockResponse }) {
return { transactions, invokedPrograms }; return { transactions, invokedPrograms };
}, [block, cluster]); }, [block, cluster]);
const filteredTransactions = React.useMemo(() => { const [filteredTransactions, showComputeUnits] = React.useMemo((): [
TransactionWithInvocations[],
boolean
] => {
const voteFilter = VOTE_PROGRAM_ID.toBase58(); const voteFilter = VOTE_PROGRAM_ID.toBase58();
const filteredTxs = transactions const filteredTxs: TransactionWithInvocations[] = transactions
.filter(({ invocations }) => { .filter(({ invocations }) => {
if (programFilter === ALL_TRANSACTIONS) { if (programFilter === ALL_TRANSACTIONS) {
return true; return true;
@ -136,11 +145,15 @@ export function BlockHistoryCard({ block }: { block: BlockResponse }) {
return tx.message.accountKeys.find((key) => key.equals(accountFilter)); return tx.message.accountKeys.find((key) => key.equals(accountFilter));
}); });
if (sortMode === "compute") { const showComputeUnits = filteredTxs.every(
filteredTxs.sort((a, b) => b.computeUnits - a.computeUnits); (tx) => tx.computeUnits !== undefined
);
if (sortMode === "compute" && showComputeUnits) {
filteredTxs.sort((a, b) => b.computeUnits! - a.computeUnits!);
} }
return filteredTxs; return [filteredTxs, showComputeUnits];
}, [ }, [
block.transactions, block.transactions,
transactions, transactions,
@ -201,15 +214,17 @@ export function BlockHistoryCard({ block }: { block: BlockResponse }) {
</th> </th>
<th className="text-muted">Result</th> <th className="text-muted">Result</th>
<th className="text-muted">Transaction Signature</th> <th className="text-muted">Transaction Signature</th>
<th {showComputeUnits && (
className="text-muted c-pointer" <th
onClick={() => { className="text-muted c-pointer"
query.set("sort", "compute"); onClick={() => {
history.push(pickClusterParams(location, query)); query.set("sort", "compute");
}} history.push(pickClusterParams(location, query));
> }}
Compute >
</th> Compute
</th>
)}
<th className="text-muted">Invoked Programs</th> <th className="text-muted">Invoked Programs</th>
</tr> </tr>
</thead> </thead>
@ -245,10 +260,14 @@ export function BlockHistoryCard({ block }: { block: BlockResponse }) {
</td> </td>
<td>{signature}</td> <td>{signature}</td>
<td className="text-end"> {showComputeUnits && (
{tx.logTruncated && ">"} <td className="text-end">
{new Intl.NumberFormat("en-US").format(tx.computeUnits)} {tx.logTruncated && ">"}
</td> {tx.computeUnits !== undefined
? new Intl.NumberFormat("en-US").format(tx.computeUnits)
: "Unknown"}
</td>
)}
<td> <td>
{tx.invocations.size === 0 {tx.invocations.size === 0
? "NA" ? "NA"