Skip to content

Commit

Permalink
fix: attempt to optimize brc-20 scan (#204)
Browse files Browse the repository at this point in the history
* fix: use cursor query for brc-20 scan

* fix: optimize content retrieval

* fix: ignore transfers that are not brc-20
  • Loading branch information
rafaelcr committed Aug 30, 2023
1 parent bfcc4ef commit f6545cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
19 changes: 8 additions & 11 deletions src/pg/brc20/brc20-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,11 @@ export class Brc20PgStore {
* @param startBlock - Start at block height
* @param endBlock - End at block height
*/
async scanBlocks(startBlock?: number, endBlock?: number): Promise<void> {
const range = await this.parent.sql<{ min: number; max: number }[]>`
SELECT
${startBlock ? this.parent.sql`${startBlock}` : this.parent.sql`MIN(block_height)`} AS min,
${endBlock ? this.parent.sql`${endBlock}` : this.parent.sql`MAX(block_height)`} AS max
FROM locations
`;
for (let blockHeight = range[0].min; blockHeight <= range[0].max; blockHeight++) {
async scanBlocks(startBlock: number, endBlock: number): Promise<void> {
for (let blockHeight = startBlock; blockHeight <= endBlock; blockHeight++) {
await this.parent.sqlWriteTransaction(async sql => {
const block = await sql<DbBrc20ScannedInscription[]>`
SELECT
i.content,
EXISTS(SELECT location_id FROM genesis_locations WHERE location_id = l.id) AS genesis,
${sql(LOCATIONS_COLUMNS.map(c => `l.${c}`))}
FROM locations AS l
Expand All @@ -80,7 +73,11 @@ export class Brc20PgStore {
for (const write of writes) {
if (write.genesis) {
if (write.address === null) continue;
const brc20 = brc20FromInscriptionContent(hexToBuffer(write.content));
// Read contents here to avoid OOM errors when bulk-requesting content from postgres.
const content = await this.parent.sql<{ content: string }[]>`
SELECT content FROM inscriptions WHERE id = ${write.inscription_id}
`;
const brc20 = brc20FromInscriptionContent(hexToBuffer(content[0].content));
if (brc20) {
switch (brc20.op) {
case 'deploy':
Expand Down Expand Up @@ -235,7 +232,7 @@ export class Brc20PgStore {
OR (l.block_height = ${args.block_height} AND l.tx_index < ${args.tx_index}))
LIMIT 3
`;
if (brc20Transfer.count > 2) return;
if (brc20Transfer.count === 0 || brc20Transfer.count > 2) return;
const transfer = brc20Transfer[0];
const amount = new BigNumber(transfer.amount);
const changes: DbBrc20BalanceInsert[] = [
Expand Down
1 change: 0 additions & 1 deletion src/pg/brc20/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { DbLocation } from '../types';

export type DbBrc20ScannedInscription = DbLocation & {
genesis: boolean;
content: string;
};

export type DbBrc20DeployInsert = {
Expand Down

0 comments on commit f6545cd

Please sign in to comment.