Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update L1 origin block hash handling and add full L1 origin block hash field #631

Merged
merged 9 commits into from
Dec 2, 2024
5 changes: 5 additions & 0 deletions .changeset/rich-beans-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blobscan/web": patch
---

Added a link to the L1 origin block if the full hash is found using the optimism decoded fields.
51 changes: 31 additions & 20 deletions apps/web/src/pages/tx/[hash].tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FC } from "react";
import { useMemo } from "react";
import type { NextPage } from "next";
import { useRouter } from "next/router";
Expand Down Expand Up @@ -281,30 +282,15 @@ const Tx: NextPage = () => {
},
{
name: "Parent L2 block hash",
value: (
<div className="flex items-center gap-2">
{"0x" + decodedData.parentL2BlockHash + "..."}
</div>
),
value: "0x" + decodedData.parentL2BlockHash + "...",
xFJA marked this conversation as resolved.
Show resolved Hide resolved
},
{
name: "L1 origin block hash",
value: (
<div className="flex items-center gap-2">
<Link
href={
"https://etherscan.io/block/" +
"0x" +
decodedData.l1OriginBlockHash
}
>
{"0x" + decodedData.l1OriginBlockHash}
</Link>
<CopyToClipboard
value={"0x" + decodedData.l1OriginBlockHash}
label="Copy L1 origin block hash"
/>
</div>
<BlockHash
fullHash={decodedData.fullL1OriginBlockHash}
partialHash={decodedData.l1OriginBlockHash}
/>
),
},
{
Expand Down Expand Up @@ -340,4 +326,29 @@ const Tx: NextPage = () => {
);
};

type BlockHashProps = {
partialHash: string;
fullHash: string | undefined;
};

const BlockHash: FC<BlockHashProps> = ({ fullHash, partialHash }) => {
if (fullHash === undefined) {
return "0x" + partialHash + "...";
}

const prefixedFullHash = "0x" + fullHash;

return (
<div className="flex items-center gap-2">
<Link href={`https://blobscan.com/block/${prefixedFullHash}`}>
{prefixedFullHash}
</Link>
<CopyToClipboard
value={prefixedFullHash}
label="Copy L1 origin block hash"
/>
</div>
);
};

export default Tx;
33 changes: 30 additions & 3 deletions packages/api/src/blob-parse/optimism.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { z } from "zod";

import { prisma } from "@blobscan/db";
import { logger } from "@blobscan/logger";

import { autocompleteBlockHash } from "../utils/autocompleteBlockHash";

export const OptimismDecodedDataSchema = z.object({
timestampSinceL2Genesis: z.number(),
lastL1OriginNumber: z.number(),
Expand All @@ -13,6 +12,7 @@
changedByL1Origin: z.number(),
totalTxs: z.number(),
contractCreationTxsNumber: z.number(),
fullL1OriginBlockHash: z.string().optional(),
});

type OptimismDecodedData = z.infer<typeof OptimismDecodedDataSchema>;
Expand All @@ -37,7 +37,7 @@
const hash = await autocompleteBlockHash(decoded.data.l1OriginBlockHash);

if (hash) {
decoded.data.l1OriginBlockHash = hash;
decoded.data.fullL1OriginBlockHash = hash;

Check warning on line 40 in packages/api/src/blob-parse/optimism.ts

View check run for this annotation

Codecov / codecov/patch

packages/api/src/blob-parse/optimism.ts#L40

Added line #L40 was not covered by tests
} else {
logger.error(
`Failed to get full block hash for L1 origin block hash: ${decoded.data.l1OriginBlockHash}`
Expand All @@ -46,3 +46,30 @@

return decoded.data;
}

/* Autocomplete a block hash from a truncated version of it.
@param partialHash - The first bytes of a block hash.
@returns The block hash, if there is a single ocurrence, or null.
*/
async function autocompleteBlockHash(partialHash: string) {
const blocks = await prisma.block.findMany({
where: {
hash: {
startsWith: "0x" + partialHash,
},
},
select: {
hash: true,
},
});

if (blocks[0] === undefined) {
return null;
}

if (blocks.length > 1) {
logger.error(`Multiple blocks found for hash ${partialHash}`);
}

return blocks[0].hash;
}

Check warning on line 75 in packages/api/src/blob-parse/optimism.ts

View check run for this annotation

Codecov / codecov/patch

packages/api/src/blob-parse/optimism.ts#L54-L75

Added lines #L54 - L75 were not covered by tests
29 changes: 0 additions & 29 deletions packages/api/src/utils/autocompleteBlockHash.ts

This file was deleted.

Loading