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: same-block inscription location pointer comparison #248

Merged
merged 5 commits into from
Oct 6, 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
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);
});
});
});
Loading