Skip to content

Commit a5d2a8b

Browse files
authored
chore: remove obsolete nft_custody_unanchored table (#2190)
1 parent 1724947 commit a5d2a8b

File tree

6 files changed

+170
-124
lines changed

6 files changed

+170
-124
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* eslint-disable camelcase */
2+
3+
exports.shorthands = undefined;
4+
5+
exports.up = pgm => {
6+
pgm.dropTable('nft_custody_unanchored');
7+
};
8+
9+
exports.down = pgm => {
10+
pgm.createTable('nft_custody_unanchored', {
11+
asset_identifier: {
12+
type: 'string',
13+
notNull: true,
14+
},
15+
value: {
16+
type: 'bytea',
17+
notNull: true,
18+
},
19+
recipient: {
20+
type: 'text',
21+
},
22+
block_height: {
23+
type: 'integer',
24+
notNull: true,
25+
},
26+
index_block_hash: {
27+
type: 'bytea',
28+
notNull: true,
29+
},
30+
parent_index_block_hash: {
31+
type: 'bytea',
32+
notNull: true,
33+
},
34+
microblock_hash: {
35+
type: 'bytea',
36+
notNull: true,
37+
},
38+
microblock_sequence: {
39+
type: 'integer',
40+
notNull: true,
41+
},
42+
tx_id: {
43+
type: 'bytea',
44+
notNull: true,
45+
},
46+
tx_index: {
47+
type: 'smallint',
48+
notNull: true,
49+
},
50+
event_index: {
51+
type: 'integer',
52+
notNull: true,
53+
},
54+
});
55+
pgm.createConstraint('nft_custody_unanchored', 'nft_custody_unanchored_unique', 'UNIQUE(asset_identifier, value)');
56+
pgm.createIndex('nft_custody_unanchored', ['recipient', 'asset_identifier']);
57+
pgm.createIndex('nft_custody_unanchored', 'value');
58+
pgm.createIndex('nft_custody_unanchored', [
59+
{ name: 'block_height', sort: 'DESC' },
60+
{ name: 'microblock_sequence', sort: 'DESC' },
61+
{ name: 'tx_index', sort: 'DESC' },
62+
{ name: 'event_index', sort: 'DESC' }
63+
]);
64+
pgm.sql(`
65+
INSERT INTO nft_custody_unanchored (asset_identifier, value, recipient, tx_id, block_height, index_block_hash, parent_index_block_hash, microblock_hash, microblock_sequence, tx_index, event_index) (
66+
SELECT
67+
DISTINCT ON(asset_identifier, value) asset_identifier, value, recipient, tx_id, nft.block_height,
68+
nft.index_block_hash, nft.parent_index_block_hash, nft.microblock_hash, nft.microblock_sequence, nft.tx_index, nft.event_index
69+
FROM
70+
nft_events AS nft
71+
INNER JOIN
72+
txs USING (tx_id)
73+
WHERE
74+
txs.canonical = true
75+
AND txs.microblock_canonical = true
76+
AND nft.canonical = true
77+
AND nft.microblock_canonical = true
78+
ORDER BY
79+
asset_identifier,
80+
value,
81+
txs.block_height DESC,
82+
txs.microblock_sequence DESC,
83+
txs.tx_index DESC,
84+
nft.event_index DESC
85+
)
86+
`);
87+
};

src/api/routes/tokens.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export const TokenRoutes: FastifyPluginAsync<
4949
),
5050
limit: LimitParam(ResourceType.Token, 'Limit', 'max number of tokens to fetch'),
5151
offset: OffsetParam('Offset', 'index of first tokens to fetch'),
52-
unanchored: UnanchoredParamSchema,
5352
tx_metadata: Type.Boolean({
5453
default: false,
5554
description:
@@ -95,15 +94,13 @@ export const TokenRoutes: FastifyPluginAsync<
9594

9695
const limit = getPagingQueryLimit(ResourceType.Token, req.query.limit);
9796
const offset = parsePagingQueryInput(req.query.offset ?? 0);
98-
const includeUnanchored = req.query.unanchored ?? false;
9997
const includeTxMetadata = req.query.tx_metadata ?? false;
10098

10199
const { results, total } = await fastify.db.getNftHoldings({
102100
principal: principal,
103101
assetIdentifiers: assetIdentifiers,
104102
offset: offset,
105103
limit: limit,
106-
includeUnanchored: includeUnanchored,
107104
includeTxMetadata: includeTxMetadata,
108105
});
109106
const parsedResults = results.map(result => {

src/datastore/pg-store.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,16 +3356,12 @@ export class PgStore extends BasePgStore {
33563356
assetIdentifiers?: string[];
33573357
limit: number;
33583358
offset: number;
3359-
includeUnanchored: boolean;
33603359
includeTxMetadata: boolean;
33613360
}): Promise<{ results: NftHoldingInfoWithTxMetadata[]; total: number }> {
33623361
const queryArgs: (string | string[] | number)[] = [args.principal, args.limit, args.offset];
33633362
if (args.assetIdentifiers) {
33643363
queryArgs.push(args.assetIdentifiers);
33653364
}
3366-
const nftCustody = args.includeUnanchored
3367-
? this.sql(`nft_custody_unanchored`)
3368-
: this.sql(`nft_custody`);
33693365
const assetIdFilter =
33703366
args.assetIdentifiers && args.assetIdentifiers.length > 0
33713367
? this.sql`AND nft.asset_identifier IN ${this.sql(args.assetIdentifiers)}`
@@ -3375,7 +3371,7 @@ export class PgStore extends BasePgStore {
33753371
>`
33763372
WITH nft AS (
33773373
SELECT *, (COUNT(*) OVER())::INTEGER AS count
3378-
FROM ${nftCustody} AS nft
3374+
FROM nft_custody AS nft
33793375
WHERE nft.recipient = ${args.principal}
33803376
${assetIdFilter}
33813377
ORDER BY block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC

src/datastore/pg-write-store.ts

Lines changed: 51 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,10 +1416,9 @@ export class PgWriteStore extends PgStore {
14161416
INSERT INTO nft_events ${sql(nftEventInserts)}
14171417
`;
14181418
if (tx.canonical && tx.microblock_canonical) {
1419-
const table = microblock ? sql`nft_custody_unanchored` : sql`nft_custody`;
14201419
await sql`
1421-
INSERT INTO ${table} ${sql(Array.from(custodyInsertsMap.values()))}
1422-
ON CONFLICT ON CONSTRAINT ${table}_unique DO UPDATE SET
1420+
INSERT INTO nft_custody ${sql(Array.from(custodyInsertsMap.values()))}
1421+
ON CONFLICT ON CONSTRAINT nft_custody_unique DO UPDATE SET
14231422
tx_id = EXCLUDED.tx_id,
14241423
index_block_hash = EXCLUDED.index_block_hash,
14251424
parent_index_block_hash = EXCLUDED.parent_index_block_hash,
@@ -1431,22 +1430,22 @@ export class PgWriteStore extends PgStore {
14311430
block_height = EXCLUDED.block_height
14321431
WHERE
14331432
(
1434-
EXCLUDED.block_height > ${table}.block_height
1433+
EXCLUDED.block_height > nft_custody.block_height
14351434
)
14361435
OR (
1437-
EXCLUDED.block_height = ${table}.block_height
1438-
AND EXCLUDED.microblock_sequence > ${table}.microblock_sequence
1436+
EXCLUDED.block_height = nft_custody.block_height
1437+
AND EXCLUDED.microblock_sequence > nft_custody.microblock_sequence
14391438
)
14401439
OR (
1441-
EXCLUDED.block_height = ${table}.block_height
1442-
AND EXCLUDED.microblock_sequence = ${table}.microblock_sequence
1443-
AND EXCLUDED.tx_index > ${table}.tx_index
1440+
EXCLUDED.block_height = nft_custody.block_height
1441+
AND EXCLUDED.microblock_sequence = nft_custody.microblock_sequence
1442+
AND EXCLUDED.tx_index > nft_custody.tx_index
14441443
)
14451444
OR (
1446-
EXCLUDED.block_height = ${table}.block_height
1447-
AND EXCLUDED.microblock_sequence = ${table}.microblock_sequence
1448-
AND EXCLUDED.tx_index = ${table}.tx_index
1449-
AND EXCLUDED.event_index > ${table}.event_index
1445+
EXCLUDED.block_height = nft_custody.block_height
1446+
AND EXCLUDED.microblock_sequence = nft_custody.microblock_sequence
1447+
AND EXCLUDED.tx_index = nft_custody.tx_index
1448+
AND EXCLUDED.event_index > nft_custody.event_index
14501449
)
14511450
`;
14521451
}
@@ -2515,10 +2514,6 @@ export class PgWriteStore extends PgStore {
25152514
AND (index_block_hash = ${args.indexBlockHash} OR index_block_hash = '\\x'::bytea)
25162515
AND tx_id IN ${sql(txIds)}
25172516
`;
2518-
await this.updateNftCustodyFromReOrg(sql, {
2519-
index_block_hash: args.indexBlockHash,
2520-
microblocks: args.microblocks,
2521-
});
25222517
}
25232518

25242519
// Update unanchored tx count in `chain_tip` table
@@ -2539,54 +2534,46 @@ export class PgWriteStore extends PgStore {
25392534
sql: PgSqlClient,
25402535
args: {
25412536
index_block_hash: string;
2542-
microblocks: string[];
25432537
}
25442538
): Promise<void> {
2545-
for (const table of [sql`nft_custody`, sql`nft_custody_unanchored`]) {
2546-
await sql`
2547-
INSERT INTO ${table}
2548-
(asset_identifier, value, tx_id, index_block_hash, parent_index_block_hash, microblock_hash,
2549-
microblock_sequence, recipient, event_index, tx_index, block_height)
2550-
(
2551-
SELECT
2552-
DISTINCT ON(asset_identifier, value) asset_identifier, value, tx_id, txs.index_block_hash,
2553-
txs.parent_index_block_hash, txs.microblock_hash, txs.microblock_sequence, recipient,
2554-
nft.event_index, txs.tx_index, txs.block_height
2555-
FROM
2556-
nft_events AS nft
2557-
INNER JOIN
2558-
txs USING (tx_id)
2559-
WHERE
2560-
txs.canonical = true
2561-
AND txs.microblock_canonical = true
2562-
AND nft.canonical = true
2563-
AND nft.microblock_canonical = true
2564-
AND nft.index_block_hash = ${args.index_block_hash}
2565-
${
2566-
args.microblocks.length > 0
2567-
? sql`AND nft.microblock_hash IN ${sql(args.microblocks)}`
2568-
: sql``
2569-
}
2570-
ORDER BY
2571-
asset_identifier,
2572-
value,
2573-
txs.block_height DESC,
2574-
txs.microblock_sequence DESC,
2575-
txs.tx_index DESC,
2576-
nft.event_index DESC
2577-
)
2578-
ON CONFLICT ON CONSTRAINT ${table}_unique DO UPDATE SET
2579-
tx_id = EXCLUDED.tx_id,
2580-
index_block_hash = EXCLUDED.index_block_hash,
2581-
parent_index_block_hash = EXCLUDED.parent_index_block_hash,
2582-
microblock_hash = EXCLUDED.microblock_hash,
2583-
microblock_sequence = EXCLUDED.microblock_sequence,
2584-
recipient = EXCLUDED.recipient,
2585-
event_index = EXCLUDED.event_index,
2586-
tx_index = EXCLUDED.tx_index,
2587-
block_height = EXCLUDED.block_height
2588-
`;
2589-
}
2539+
await sql`
2540+
INSERT INTO nft_custody
2541+
(asset_identifier, value, tx_id, index_block_hash, parent_index_block_hash, microblock_hash,
2542+
microblock_sequence, recipient, event_index, tx_index, block_height)
2543+
(
2544+
SELECT
2545+
DISTINCT ON(asset_identifier, value) asset_identifier, value, tx_id, txs.index_block_hash,
2546+
txs.parent_index_block_hash, txs.microblock_hash, txs.microblock_sequence, recipient,
2547+
nft.event_index, txs.tx_index, txs.block_height
2548+
FROM
2549+
nft_events AS nft
2550+
INNER JOIN
2551+
txs USING (tx_id)
2552+
WHERE
2553+
txs.canonical = true
2554+
AND txs.microblock_canonical = true
2555+
AND nft.canonical = true
2556+
AND nft.microblock_canonical = true
2557+
AND nft.index_block_hash = ${args.index_block_hash}
2558+
ORDER BY
2559+
asset_identifier,
2560+
value,
2561+
txs.block_height DESC,
2562+
txs.microblock_sequence DESC,
2563+
txs.tx_index DESC,
2564+
nft.event_index DESC
2565+
)
2566+
ON CONFLICT ON CONSTRAINT nft_custody_unique DO UPDATE SET
2567+
tx_id = EXCLUDED.tx_id,
2568+
index_block_hash = EXCLUDED.index_block_hash,
2569+
parent_index_block_hash = EXCLUDED.parent_index_block_hash,
2570+
microblock_hash = EXCLUDED.microblock_hash,
2571+
microblock_sequence = EXCLUDED.microblock_sequence,
2572+
recipient = EXCLUDED.recipient,
2573+
event_index = EXCLUDED.event_index,
2574+
tx_index = EXCLUDED.tx_index,
2575+
block_height = EXCLUDED.block_height
2576+
`;
25902577
}
25912578

25922579
/**
@@ -3050,10 +3037,7 @@ export class PgWriteStore extends PgStore {
30503037
updatedEntities.markedNonCanonical.nftEvents += nftResult.count;
30513038
}
30523039
if (nftResult.count)
3053-
await this.updateNftCustodyFromReOrg(sql, {
3054-
index_block_hash: indexBlockHash,
3055-
microblocks: [],
3056-
});
3040+
await this.updateNftCustodyFromReOrg(sql, { index_block_hash: indexBlockHash });
30573041
});
30583042
q.enqueue(async () => {
30593043
const pox2Result = await sql`

tests/api/datastore.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5884,7 +5884,6 @@ describe('postgres datastore', () => {
58845884
limit: 10,
58855885
offset: 0,
58865886
includeTxMetadata: false,
5887-
includeUnanchored: true,
58885887
})
58895888
).resolves.not.toThrow();
58905889
// Tx list details with empty txIds

0 commit comments

Comments
 (0)