Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: attempt to optimize brc-20 scan #204

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading