From 13e1f16f6980b6743abe8d95644703329f07fe3f Mon Sep 17 00:00:00 2001 From: Rafael Cardenas Date: Thu, 16 Nov 2023 16:17:09 -0600 Subject: [PATCH] fix: inscription count generation --- src/env.ts | 5 ---- src/pg/counts/counts-pg-store.ts | 51 ++++++++++++++++++-------------- src/pg/pg-store.ts | 18 +++++------ 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/env.ts b/src/env.ts index 72106e43..9f83eed3 100644 --- a/src/env.ts +++ b/src/env.ts @@ -55,11 +55,6 @@ const schema = Type.Object({ /** Enables BRC-20 processing in write mode APIs */ BRC20_BLOCK_SCAN_ENABLED: Type.Boolean({ default: true }), - /** - * Disables inscription genesis/current location calculation, count aggregation, etc. so blocks - * can be ingested faster during a full replay. - */ - FAST_INGESTION_MODE: Type.Boolean({ default: false }), }); type Env = Static; diff --git a/src/pg/counts/counts-pg-store.ts b/src/pg/counts/counts-pg-store.ts index d63251fa..61a15ac4 100644 --- a/src/pg/counts/counts-pg-store.ts +++ b/src/pg/counts/counts-pg-store.ts @@ -56,42 +56,49 @@ export class CountsPgStore extends BasePgStoreModule { async applyInscriptions(writes: DbInscriptionInsert[]): Promise { if (writes.length === 0) return; - const mimeType = new Map(); - const rarity = new Map(); - const recursion = new Map(); - const type = new Map(); + const mimeType = new Map(); + const rarity = new Map(); + const recursion = new Map(); + const typeMap = new Map(); for (const i of writes) { - const t = i.number < 0 ? 'cursed' : 'blessed'; - mimeType.set(i.mime_type, { - mime_type: i.mime_type, - count: mimeType.get(i.mime_type)?.count ?? 0 + 1, - }); - rarity.set(i.sat_rarity, { - sat_rarity: i.sat_rarity, - count: rarity.get(i.sat_rarity)?.count ?? 0 + 1, - }); - recursion.set(i.recursive, { - recursive: i.recursive, - count: recursion.get(i.recursive)?.count ?? 0 + 1, - }); - type.set(t, { type: t, count: type.get(t)?.count ?? 0 + 1 }); + mimeType.set(i.mime_type, (mimeType.get(i.mime_type) ?? 0) + 1); + rarity.set(i.sat_rarity, (rarity.get(i.sat_rarity) ?? 0) + 1); + recursion.set(i.recursive, (recursion.get(i.recursive) ?? 0) + 1); + const inscrType = i.number < 0 ? 'cursed' : 'blessed'; + typeMap.set(inscrType, (typeMap.get(inscrType) ?? 0) + 1); } + const mimeTypeInsert = Array.from(mimeType.entries()).map(k => ({ + mime_type: k[0], + count: k[1], + })); + const rarityInsert = Array.from(rarity.entries()).map(k => ({ + sat_rarity: k[0], + count: k[1], + })); + const recursionInsert = Array.from(recursion.entries()).map(k => ({ + recursive: k[0], + count: k[1], + })); + const typeInsert = Array.from(typeMap.entries()).map(k => ({ + type: k[0], + count: k[1], + })); // `counts_by_address` and `counts_by_genesis_address` count increases are handled in // `applyLocations`. await this.sql` WITH increase_mime_type AS ( - INSERT INTO counts_by_mime_type ${this.sql([...mimeType.values()])} + INSERT INTO counts_by_mime_type ${this.sql(mimeTypeInsert)} ON CONFLICT (mime_type) DO UPDATE SET count = counts_by_mime_type.count + EXCLUDED.count ), increase_rarity AS ( - INSERT INTO counts_by_sat_rarity ${this.sql([...rarity.values()])} + INSERT INTO counts_by_sat_rarity ${this.sql(rarityInsert)} ON CONFLICT (sat_rarity) DO UPDATE SET count = counts_by_sat_rarity.count + EXCLUDED.count ), increase_recursive AS ( - INSERT INTO counts_by_recursive ${this.sql([...recursion.values()])} + INSERT INTO counts_by_recursive ${this.sql(recursionInsert)} ON CONFLICT (recursive) DO UPDATE SET count = counts_by_recursive.count + EXCLUDED.count ) - INSERT INTO counts_by_type ${this.sql([...type.values()])} + INSERT INTO counts_by_type ${this.sql(typeInsert)} ON CONFLICT (type) DO UPDATE SET count = counts_by_type.count + EXCLUDED.count `; } diff --git a/src/pg/pg-store.ts b/src/pg/pg-store.ts index 484cbb90..93ae558b 100644 --- a/src/pg/pg-store.ts +++ b/src/pg/pg-store.ts @@ -669,16 +669,14 @@ export class PgStore extends BasePgStore { RETURNING inscription_id, id AS location_id, block_height, tx_index, address `; await this.updateInscriptionRecursions(writes); - if (!ENV.FAST_INGESTION_MODE) { - if (transferGenesisIds.size) - await sql` - UPDATE inscriptions - SET updated_at = NOW() - WHERE genesis_id IN ${sql([...transferGenesisIds])} - `; - await this.updateInscriptionLocationPointers(locations); - await this.counts.applyInscriptions(inscriptions); - } + if (transferGenesisIds.size) + await sql` + UPDATE inscriptions + SET updated_at = NOW() + WHERE genesis_id IN ${sql([...transferGenesisIds])} + `; + await this.updateInscriptionLocationPointers(locations); + await this.counts.applyInscriptions(inscriptions); for (const reveal of writes) { const action = reveal.inscription ? `reveal #${reveal.inscription.number}` : `transfer`; logger.info(