Skip to content

Commit

Permalink
fix: same-block inscription location pointer comparison (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Oct 6, 2023
1 parent 12ad68a commit 6fabb40
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ export class PgStore extends BasePgStore {
const distinctPointers = (
cond: (a: DbLocationPointerInsert, b: DbLocationPointerInsert) => boolean
): DbLocationPointerInsert[] => {
const out = new Map<number, DbLocationPointerInsert>();
const out = new Map<string, DbLocationPointerInsert>();
for (const ptr of pointers) {
if (ptr.inscription_id === null) continue;
const current = out.get(ptr.inscription_id);
Expand All @@ -748,12 +748,13 @@ export class PgStore extends BasePgStore {

await this.sqlWriteTransaction(async sql => {
const distinctIds = [
...new Set<number>(pointers.map(i => i.inscription_id).filter(v => v !== null)),
...new Set<string>(pointers.map(i => i.inscription_id).filter(v => v !== null)),
];
const genesisPtrs = distinctPointers(
(a, b) =>
a.block_height < b.block_height ||
(a.block_height === b.block_height && a.tx_index < b.tx_index)
parseInt(a.block_height) < parseInt(b.block_height) ||
(parseInt(a.block_height) === parseInt(b.block_height) &&
parseInt(a.tx_index) < parseInt(b.tx_index))
);
if (genesisPtrs.length) {
const genesis = await sql<{ old_address: string | null; new_address: string | null }[]>`
Expand Down Expand Up @@ -784,8 +785,9 @@ export class PgStore extends BasePgStore {

const currentPtrs = distinctPointers(
(a, b) =>
a.block_height > b.block_height ||
(a.block_height === b.block_height && a.tx_index > b.tx_index)
parseInt(a.block_height) > parseInt(b.block_height) ||
(parseInt(a.block_height) === parseInt(b.block_height) &&
parseInt(a.tx_index) > parseInt(b.tx_index))
);
if (currentPtrs.length) {
const current = await sql<{ old_address: string | null; new_address: string | null }[]>`
Expand Down
8 changes: 4 additions & 4 deletions src/pg/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ export type DbLocationPointer = {
};

export type DbLocationPointerInsert = {
inscription_id: number;
location_id: number;
block_height: number;
tx_index: number;
inscription_id: string;
location_id: string;
block_height: string;
tx_index: string;
address: string | null;
};

Expand Down
55 changes: 55 additions & 0 deletions tests/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,60 @@ describe('EventServer', () => {
});
expect(api4.json().total).toBe(3);
});

test('multiple inscription pointers on the same block are compared correctly', async () => {
const address = 'bc1q92zytmqgczsrg4xuhpc2asz6h4h7ke5hagw8k6';
const address2 = 'bc1qtpm0fsaawxjsthfdrxhmrzunnpjx0g9hncgvp7';
await db.updateInscriptions(
new TestChainhookPayloadBuilder()
.apply()
.block({
height: 808382,
hash: '00000000000000000002b14f0c5dde0b2fc74d022e860696bd64f1f652756674',
})
.transaction({
hash: '6046f17804eb8396285567a20c09598ae1273b6f744b23700ba95593c380ce02',
})
.inscriptionRevealed({
content_bytes: '0x48656C6C6F',
content_type: 'image/png',
content_length: 5,
inscription_number: 33224825,
inscription_fee: 2805,
inscription_id: '6046f17804eb8396285567a20c09598ae1273b6f744b23700ba95593c380ce02i0',
inscription_output_value: 10000,
inscriber_address: address,
ordinal_number: 5,
ordinal_block_height: 0,
ordinal_offset: 0,
satpoint_post_inscription:
'6046f17804eb8396285567a20c09598ae1273b6f744b23700ba95593c380ce02:0:0',
inscription_input_index: 0,
transfers_pre_inscription: 0,
tx_index: 995,
})
.transaction({
hash: '7edaa48337a94da327b6262830505f116775a32db5ad4ad46e87ecea33f21bac',
})
.inscriptionTransferred({
inscription_id: '6046f17804eb8396285567a20c09598ae1273b6f744b23700ba95593c380ce02i0',
updated_address: address2,
satpoint_pre_transfer:
'6046f17804eb8396285567a20c09598ae1273b6f744b23700ba95593c380ce02:0:0',
satpoint_post_transfer:
'7edaa48337a94da327b6262830505f116775a32db5ad4ad46e87ecea33f21bac:0:0',
post_transfer_output_value: null,
tx_index: 1019, // '1019' is less than '995' when compared as a string
})
.build()
);
const response = await fastify.inject({
method: 'GET',
url: `/ordinals/inscriptions/6046f17804eb8396285567a20c09598ae1273b6f744b23700ba95593c380ce02i0`,
});
expect(response.statusCode).toBe(200);
const json = response.json();
expect(json.genesis_address).toBe(address);
});
});
});

0 comments on commit 6fabb40

Please sign in to comment.