Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit 7461667

Browse files
committed
fix: batch inscription transfers
1 parent c5d0d90 commit 7461667

File tree

4 files changed

+28
-33
lines changed

4 files changed

+28
-33
lines changed

.vscode/launch.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@
9191
"--testTimeout=3600000",
9292
"--runInBand",
9393
"--no-cache",
94-
"-t",
95-
"index filtered by address"
9694
],
9795
"outputCapture": "std",
9896
"console": "integratedTerminal",

src/pg/pg-store.ts

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -185,39 +185,36 @@ export class PgStore extends BasePgStore {
185185
...l,
186186
timestamp: sql`TO_TIMESTAMP(${l.timestamp})`,
187187
}));
188+
// Insert locations, figure out moved inscriptions, insert inscription transfers.
188189
for await (const batch of batchIterate(entries, INSERT_BATCH_SIZE))
189190
await sql`
190-
INSERT INTO locations ${sql(batch)}
191-
ON CONFLICT (ordinal_number, block_height, tx_index) DO NOTHING
192-
`;
193-
// Insert block transfers.
194-
let block_transfer_index = 0;
195-
const transferEntries = [];
196-
for (const transfer of cache.locations) {
197-
const transferred = await sql<{ genesis_id: string; number: string }[]>`
198-
SELECT genesis_id, number FROM inscriptions
199-
WHERE ordinal_number = ${transfer.ordinal_number} AND (
200-
block_height < ${transfer.block_height}
201-
OR (block_height = ${transfer.block_height} AND tx_index < ${transfer.tx_index})
191+
WITH location_inserts AS (
192+
INSERT INTO locations ${sql(batch)}
193+
ON CONFLICT (ordinal_number, block_height, tx_index) DO NOTHING
194+
RETURNING ordinal_number, block_height, block_hash, tx_index
195+
),
196+
prev_transfer_index AS (
197+
SELECT MAX(block_transfer_index) AS max
198+
FROM inscription_transfers
199+
WHERE block_height = (SELECT block_height FROM location_inserts LIMIT 1)
200+
),
201+
moved_inscriptions AS (
202+
SELECT
203+
i.genesis_id, i.number, i.ordinal_number, li.block_height, li.block_hash, li.tx_index,
204+
(
205+
ROW_NUMBER() OVER (ORDER BY li.tx_index ASC) + (SELECT COALESCE(max, -1) FROM prev_transfer_index)
206+
) AS block_transfer_index
207+
FROM inscriptions AS i
208+
INNER JOIN location_inserts AS li ON li.ordinal_number = i.ordinal_number
209+
WHERE
210+
i.block_height < li.block_height
211+
OR (i.block_height = li.block_height AND i.tx_index < li.tx_index)
202212
)
213+
INSERT INTO inscription_transfers
214+
(genesis_id, number, ordinal_number, block_height, block_hash, tx_index, block_transfer_index)
215+
(SELECT * FROM moved_inscriptions)
216+
ON CONFLICT (block_height, block_transfer_index) DO NOTHING
203217
`;
204-
for (const inscription of transferred)
205-
transferEntries.push({
206-
genesis_id: inscription.genesis_id,
207-
number: inscription.number,
208-
ordinal_number: transfer.ordinal_number,
209-
block_height: transfer.block_height,
210-
block_hash: transfer.block_hash,
211-
tx_index: transfer.tx_index,
212-
block_transfer_index: block_transfer_index++,
213-
});
214-
}
215-
if (transferEntries.length)
216-
for await (const batch of batchIterate(transferEntries, INSERT_BATCH_SIZE))
217-
await sql`
218-
INSERT INTO inscription_transfers ${sql(batch)}
219-
ON CONFLICT (block_height, block_transfer_index) DO NOTHING
220-
`;
221218
}
222219
if (cache.recursiveRefs.size)
223220
for (const [genesis_id, refs] of cache.recursiveRefs) {

tests/api/inscriptions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3031,7 +3031,7 @@ describe('/inscriptions', () => {
30313031
new TestChainhookPayloadBuilder()
30323032
.rollback()
30333033
.block({
3034-
height: 775618,
3034+
height: 778575,
30353035
hash: '000000000000000000032ef6c45a69c0496456b3cae84ee9f2899f636d03c5ac',
30363036
timestamp: 1675312161,
30373037
})

tests/ordhook/server.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('EventServer', () => {
126126
const payload2 = new TestChainhookPayloadBuilder()
127127
.rollback()
128128
.block({
129-
height: 107,
129+
height: 767430,
130130
hash: '0x163de66dc9c0949905bfe8e148bde04600223cf88d19f26fdbeba1d6e6fa0f88',
131131
timestamp: 1676913207,
132132
})

0 commit comments

Comments
 (0)