Skip to content

Commit

Permalink
feat(db): add tx index to BlobsOnTransactions model (#584)
Browse files Browse the repository at this point in the history
* chore: add changeset

* chore: merge changesets

* chore(db): refactor database generator

* feat(db): add tx index to  `BlobsOnTransactions` model

* feat: make tx index mandatory

* perf(api): add block timestamp and transaction index as ordering clauses when fetching blobs

* chore: add changeset

* fix(api): return tx index when fetching a single blob

* perf(db): improve tx index backfilling migration
  • Loading branch information
PJColombo authored and luis-herasme committed Oct 13, 2024
1 parent f1c9406 commit 3c17ee0
Show file tree
Hide file tree
Showing 24 changed files with 909 additions and 399 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-pillows-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blobscan/db": minor
---

Added tx index to `BlobsOnTransactions` model
5 changes: 5 additions & 0 deletions .changeset/yellow-peas-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blobscan/api": patch
---

Enhanced blob fetching performance by adding sorting based on block timestamps and transaction index
2 changes: 2 additions & 0 deletions packages/api/src/routers/blob/common/selects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function createBlobSelect(expands: Expands) {
blockTimestamp: true,
index: true,
txHash: true,
txIndex: true,
transaction: {
select: {
rollup: true,
Expand All @@ -48,6 +49,7 @@ export function createBlobsOnTransactionsSelect(expands: Expands) {
blockNumber: true,
blockTimestamp: true,
txHash: true,
txIndex: true,
blob: {
select: baseBlobSelect,
},
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/routers/blob/common/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const serializedBlobOnTransactionSchema = serializedBaseBlobSchema.merge(
z.object({
index: blobIndexSchema,
txHash: z.string(),
txIndex: z.number().nonnegative(),
blockHash: z.string(),
blockNumber: blockNumberSchema,
blockTimestamp: z.string(),
Expand All @@ -86,6 +87,7 @@ export const serializedBlobSchema = serializedBaseBlobSchema.merge(
z
.object({
hash: z.string(),
txIndex: z.number().nonnegative(),
index: blobIndexSchema,
blockHash: z.string(),
blockNumber: z.number().nonnegative(),
Expand Down Expand Up @@ -127,6 +129,7 @@ export function serializeBlobOnTransaction(
blockTimestamp,
index,
txHash,
txIndex,
block,
transaction,
} = blobOnTransaction;
Expand All @@ -136,6 +139,7 @@ export function serializeBlobOnTransaction(
blockNumber,
blockTimestamp: serializeDate(blockTimestamp),
txHash,
txIndex,
index,
};

Expand Down Expand Up @@ -167,13 +171,15 @@ export function serializeBlob(blob: QueriedBlob): SerializedBlob {
blockHash,
blockNumber,
blockTimestamp,
txIndex,
index,
txHash,
block,
transaction,
}) => {
return {
index,
txIndex,
hash: txHash,
blockHash,
blockNumber,
Expand Down
8 changes: 2 additions & 6 deletions packages/api/src/routers/blob/getAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ export const getAll = publicProcedure
: undefined,
},
orderBy: [
{ blockNumber: filters.sort },
{
transaction: {
index: filters.sort,
},
},
{ blockTimestamp: filters.sort },
{ txIndex: filters.sort },
{
index: filters.sort,
},
Expand Down
26 changes: 18 additions & 8 deletions packages/api/src/routers/indexer/indexData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,24 @@ export function createDBBlobs({

export function createDBBlobsOnTransactions({
block,
transactions,
blobs,
}: IndexDataFormattedInput): BlobsOnTransactions[] {
return blobs.map(({ versionedHash, txHash, index }) => ({
blobHash: versionedHash,
blockHash: block.hash,
blockNumber: block.number,
blockTimestamp: timestampToDate(block.timestamp),
txHash: txHash,
index,
}));
return blobs.map(({ versionedHash, txHash, index }) => {
const tx = transactions.find((t) => t.hash === txHash);

if (!tx) {
throw new Error(`Transaction ${txHash} not found`);
}

return {
blobHash: versionedHash,
blockHash: block.hash,
blockNumber: block.number,
blockTimestamp: timestampToDate(block.timestamp),
txHash: txHash,
txIndex: tx.index,
index,
};
});
}
Loading

0 comments on commit 3c17ee0

Please sign in to comment.