Skip to content

Commit

Permalink
fix: inscription count generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Nov 16, 2023
1 parent 0e68c72 commit 13e1f16
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
5 changes: 0 additions & 5 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof schema>;

Expand Down
51 changes: 29 additions & 22 deletions src/pg/counts/counts-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,49 @@ export class CountsPgStore extends BasePgStoreModule {

async applyInscriptions(writes: DbInscriptionInsert[]): Promise<void> {
if (writes.length === 0) return;
const mimeType = new Map<string, any>();
const rarity = new Map<string, any>();
const recursion = new Map<boolean, any>();
const type = new Map<string, any>();
const mimeType = new Map<string, number>();
const rarity = new Map<string, number>();
const recursion = new Map<boolean, number>();
const typeMap = new Map<string, number>();
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
`;
}
Expand Down
18 changes: 8 additions & 10 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 13e1f16

Please sign in to comment.