Skip to content

Commit

Permalink
fix: remove gap detection (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr authored Mar 9, 2024
1 parent 578294d commit 040ee04
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 127 deletions.
2 changes: 0 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ const schema = Type.Object({

/** Enables BRC-20 processing in write mode APIs */
BRC20_BLOCK_SCAN_ENABLED: Type.Boolean({ default: true }),
/** Enables inscription gap detection to prevent ingesting unordered blocks */
INSCRIPTION_GAP_DETECTION_ENABLED: Type.Boolean({ default: true }),
});
type Env = Static<typeof schema>;

Expand Down
32 changes: 0 additions & 32 deletions src/pg/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { PgBytea, logger, toEnumValue } from '@hirosystems/api-toolkit';
import { hexToBuffer, normalizedHexString, parseSatPoint } from '../api/util/helpers';
import {
BadPayloadRequestError,
BitcoinEvent,
BitcoinInscriptionRevealed,
BitcoinInscriptionTransferred,
} from '@hirosystems/chainhook-client';
import { ENV } from '../env';
import {
DbLocationTransferType,
InscriptionEventData,
Expand All @@ -15,36 +13,6 @@ import {
} from './types';
import { OrdinalSatoshi } from '../api/util/ordinal-satoshi';

/**
* Check if writing the next block would create an inscription number gap
* @param currentNumber - Current max blessed number
* @param writes - Incoming inscription event data
* @param currentBlockHeight - Current height
* @param nextBlockHeight - Height to be inserted
*/
export function assertNoBlockInscriptionGap(args: {
currentNumber: number;
writes: InscriptionEventData[];
currentBlockHeight: number;
nextBlockHeight: number;
}) {
if (!ENV.INSCRIPTION_GAP_DETECTION_ENABLED) return;
const nextReveal = args.writes.find(
w =>
'inscription' in w &&
w.inscription.number >= 0 &&
// Spent as fee come first in the block
w.location.address != null &&
w.location.address != ''
);
if (!nextReveal) return;
const next = (nextReveal as InscriptionRevealData).inscription.number;
if (next !== args.currentNumber + 1)
throw new BadPayloadRequestError(
`Block inscription gap detected: Attempting to insert #${next} (${args.nextBlockHeight}) but current max is #${args.currentNumber}. Chain tip is at ${args.currentBlockHeight}.`
);
}

/**
* Returns a list of referenced inscription ids from inscription content.
* @param content - Inscription content
Expand Down
11 changes: 1 addition & 10 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ENV } from '../env';
import { Brc20PgStore } from './brc20/brc20-pg-store';
import { CountsPgStore } from './counts/counts-pg-store';
import { getIndexResultCountType } from './counts/helpers';
import { assertNoBlockInscriptionGap, revealInsertsFromOrdhookEvent } from './helpers';
import { revealInsertsFromOrdhookEvent } from './helpers';
import {
DbFullyLocatedInscriptionResult,
DbInscriptionContent,
Expand All @@ -33,7 +33,6 @@ import {
DbPaginatedResult,
InscriptionEventData,
LOCATIONS_COLUMNS,
InscriptionRevealData,
InscriptionInsert,
LocationInsert,
LocationData,
Expand Down Expand Up @@ -109,11 +108,9 @@ export class PgStore extends BasePgStore {
// Check where we're at in terms of ingestion, e.g. block height and max blessed inscription
// number. This will let us determine if we should skip ingesting this block or throw an
// error if a gap is detected.
const currentNumber = (await this.getMaxInscriptionNumber()) ?? -1;
const currentBlockHeight = await this.getChainTipBlockHeight();
const event = applyEvent as BitcoinEvent;
if (
ENV.INSCRIPTION_GAP_DETECTION_ENABLED &&
event.block_identifier.index <= currentBlockHeight &&
event.block_identifier.index !== ORDINALS_GENESIS_BLOCK
) {
Expand All @@ -125,12 +122,6 @@ export class PgStore extends BasePgStore {
logger.info(`PgStore ingesting block ${event.block_identifier.index}`);
const time = stopwatch();
const writes = revealInsertsFromOrdhookEvent(event);
assertNoBlockInscriptionGap({
currentNumber,
writes,
currentBlockHeight,
nextBlockHeight: event.block_identifier.index,
});
for (const writeChunk of batchIterate(writes, INSERT_BATCH_SIZE))
await this.insertInscriptions(writeChunk, payload.chainhook.is_streaming_blocks);
updatedBlockHeightMin = Math.min(updatedBlockHeightMin, event.block_identifier.index);
Expand Down
83 changes: 0 additions & 83 deletions tests/ordhook/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,89 +546,6 @@ describe('EventServer', () => {
});

describe('gap detection', () => {
test('server rejects payload with first inscription gap', async () => {
await db.updateInscriptions(
new TestChainhookPayloadBuilder()
.apply()
.block({
height: 778575,
hash: '0x00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d',
timestamp: 1676913207,
})
.transaction({
hash: '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201',
})
.inscriptionRevealed({
content_bytes: '0x48656C6C6F',
content_type: 'text/plain;charset=utf-8',
content_length: 5,
inscription_number: { classic: 0, jubilee: 0 },
inscription_fee: 705,
inscription_id: '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201i0',
inscription_output_value: 10000,
inscriber_address: 'bc1pscktlmn99gyzlvymvrezh6vwd0l4kg06tg5rvssw0czg8873gz5sdkteqj',
ordinal_number: 257418248345364,
ordinal_block_height: 650000,
ordinal_offset: 0,
satpoint_post_inscription:
'9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201:0:0',
inscription_input_index: 0,
transfers_pre_inscription: 0,
tx_index: 0,
curse_type: null,
inscription_pointer: null,
delegate: null,
metaprotocol: null,
metadata: null,
parent: null,
})
.build()
);
const errorPayload = new TestChainhookPayloadBuilder()
.apply()
.block({
height: 778576,
hash: '00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d',
timestamp: 1676913207,
})
.transaction({
hash: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc',
})
.inscriptionRevealed({
content_bytes: '0x48656C6C6F',
content_type: 'text/plain;charset=utf-8',
content_length: 5,
inscription_number: { classic: 5, jubilee: 5 }, // Gap at 5
inscription_fee: 705,
inscription_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0',
inscription_output_value: 10000,
inscriber_address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td',
ordinal_number: 1050000000000000,
ordinal_block_height: 650000,
ordinal_offset: 0,
satpoint_post_inscription:
'38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0:0',
inscription_input_index: 0,
transfers_pre_inscription: 0,
tx_index: 0,
curse_type: null,
inscription_pointer: null,
delegate: null,
metaprotocol: null,
metadata: null,
parent: null,
})
.build();
await expect(db.updateInscriptions(errorPayload)).rejects.toThrow(BadPayloadRequestError);
const response = await server['fastify'].inject({
method: 'POST',
url: `/payload`,
headers: { authorization: `Bearer ${ENV.ORDHOOK_NODE_AUTH_TOKEN}` },
payload: errorPayload,
});
expect(response.statusCode).toBe(400);
});

test('server ignores past blocks', async () => {
const payload = new TestChainhookPayloadBuilder()
.apply()
Expand Down

0 comments on commit 040ee04

Please sign in to comment.