Skip to content

Commit

Permalink
chore: remove obsolete nft_custody_unanchored table
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Dec 20, 2024
1 parent 1724947 commit a1ce302
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 77 deletions.
87 changes: 87 additions & 0 deletions migrations/1734712921681_drop-nft-custody-unanchored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* eslint-disable camelcase */

exports.shorthands = undefined;

exports.up = pgm => {
pgm.dropTable('nft_custody_unanchored');
};

exports.down = pgm => {
pgm.createTable('nft_custody_unanchored', {
asset_identifier: {
type: 'string',
notNull: true,
},
value: {
type: 'bytea',
notNull: true,
},
recipient: {
type: 'text',
},
block_height: {
type: 'integer',
notNull: true,
},
index_block_hash: {
type: 'bytea',
notNull: true,
},
parent_index_block_hash: {
type: 'bytea',
notNull: true,
},
microblock_hash: {
type: 'bytea',
notNull: true,
},
microblock_sequence: {
type: 'integer',
notNull: true,
},
tx_id: {
type: 'bytea',
notNull: true,
},
tx_index: {
type: 'smallint',
notNull: true,
},
event_index: {
type: 'integer',
notNull: true,
},
});
pgm.createConstraint('nft_custody_unanchored', 'nft_custody_unanchored_unique', 'UNIQUE(asset_identifier, value)');
pgm.createIndex('nft_custody_unanchored', ['recipient', 'asset_identifier']);
pgm.createIndex('nft_custody_unanchored', 'value');
pgm.createIndex('nft_custody_unanchored', [
{ name: 'block_height', sort: 'DESC' },
{ name: 'microblock_sequence', sort: 'DESC' },
{ name: 'tx_index', sort: 'DESC' },
{ name: 'event_index', sort: 'DESC' }
]);
pgm.sql(`
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) (
SELECT
DISTINCT ON(asset_identifier, value) asset_identifier, value, recipient, tx_id, nft.block_height,
nft.index_block_hash, nft.parent_index_block_hash, nft.microblock_hash, nft.microblock_sequence, nft.tx_index, nft.event_index
FROM
nft_events AS nft
INNER JOIN
txs USING (tx_id)
WHERE
txs.canonical = true
AND txs.microblock_canonical = true
AND nft.canonical = true
AND nft.microblock_canonical = true
ORDER BY
asset_identifier,
value,
txs.block_height DESC,
txs.microblock_sequence DESC,
txs.tx_index DESC,
nft.event_index DESC
)
`);
};
3 changes: 0 additions & 3 deletions src/api/routes/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const TokenRoutes: FastifyPluginAsync<
),
limit: LimitParam(ResourceType.Token, 'Limit', 'max number of tokens to fetch'),
offset: OffsetParam('Offset', 'index of first tokens to fetch'),
unanchored: UnanchoredParamSchema,
tx_metadata: Type.Boolean({
default: false,
description:
Expand Down Expand Up @@ -95,15 +94,13 @@ export const TokenRoutes: FastifyPluginAsync<

const limit = getPagingQueryLimit(ResourceType.Token, req.query.limit);
const offset = parsePagingQueryInput(req.query.offset ?? 0);
const includeUnanchored = req.query.unanchored ?? false;
const includeTxMetadata = req.query.tx_metadata ?? false;

const { results, total } = await fastify.db.getNftHoldings({
principal: principal,
assetIdentifiers: assetIdentifiers,
offset: offset,
limit: limit,
includeUnanchored: includeUnanchored,
includeTxMetadata: includeTxMetadata,
});
const parsedResults = results.map(result => {
Expand Down
6 changes: 1 addition & 5 deletions src/datastore/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3356,16 +3356,12 @@ export class PgStore extends BasePgStore {
assetIdentifiers?: string[];
limit: number;
offset: number;
includeUnanchored: boolean;
includeTxMetadata: boolean;
}): Promise<{ results: NftHoldingInfoWithTxMetadata[]; total: number }> {
const queryArgs: (string | string[] | number)[] = [args.principal, args.limit, args.offset];
if (args.assetIdentifiers) {
queryArgs.push(args.assetIdentifiers);
}
const nftCustody = args.includeUnanchored
? this.sql(`nft_custody_unanchored`)
: this.sql(`nft_custody`);
const assetIdFilter =
args.assetIdentifiers && args.assetIdentifiers.length > 0
? this.sql`AND nft.asset_identifier IN ${this.sql(args.assetIdentifiers)}`
Expand All @@ -3375,7 +3371,7 @@ export class PgStore extends BasePgStore {
>`
WITH nft AS (
SELECT *, (COUNT(*) OVER())::INTEGER AS count
FROM ${nftCustody} AS nft
FROM nft_custody AS nft
WHERE nft.recipient = ${args.principal}
${assetIdFilter}
ORDER BY block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
Expand Down
120 changes: 52 additions & 68 deletions src/datastore/pg-write-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1415,11 +1415,10 @@ export class PgWriteStore extends PgStore {
await sql`
INSERT INTO nft_events ${sql(nftEventInserts)}
`;
if (tx.canonical && tx.microblock_canonical) {
const table = microblock ? sql`nft_custody_unanchored` : sql`nft_custody`;
if (tx.canonical && tx.microblock_canonical && !microblock) {
await sql`
INSERT INTO ${table} ${sql(Array.from(custodyInsertsMap.values()))}
ON CONFLICT ON CONSTRAINT ${table}_unique DO UPDATE SET
INSERT INTO nft_custody ${sql(Array.from(custodyInsertsMap.values()))}
ON CONFLICT ON CONSTRAINT nft_custody_unique DO UPDATE SET
tx_id = EXCLUDED.tx_id,
index_block_hash = EXCLUDED.index_block_hash,
parent_index_block_hash = EXCLUDED.parent_index_block_hash,
Expand All @@ -1431,22 +1430,22 @@ export class PgWriteStore extends PgStore {
block_height = EXCLUDED.block_height
WHERE
(
EXCLUDED.block_height > ${table}.block_height
EXCLUDED.block_height > nft_custody.block_height
)
OR (
EXCLUDED.block_height = ${table}.block_height
AND EXCLUDED.microblock_sequence > ${table}.microblock_sequence
EXCLUDED.block_height = nft_custody.block_height
AND EXCLUDED.microblock_sequence > nft_custody.microblock_sequence
)
OR (
EXCLUDED.block_height = ${table}.block_height
AND EXCLUDED.microblock_sequence = ${table}.microblock_sequence
AND EXCLUDED.tx_index > ${table}.tx_index
EXCLUDED.block_height = nft_custody.block_height
AND EXCLUDED.microblock_sequence = nft_custody.microblock_sequence
AND EXCLUDED.tx_index > nft_custody.tx_index
)
OR (
EXCLUDED.block_height = ${table}.block_height
AND EXCLUDED.microblock_sequence = ${table}.microblock_sequence
AND EXCLUDED.tx_index = ${table}.tx_index
AND EXCLUDED.event_index > ${table}.event_index
EXCLUDED.block_height = nft_custody.block_height
AND EXCLUDED.microblock_sequence = nft_custody.microblock_sequence
AND EXCLUDED.tx_index = nft_custody.tx_index
AND EXCLUDED.event_index > nft_custody.event_index
)
`;
}
Expand Down Expand Up @@ -2515,10 +2514,6 @@ export class PgWriteStore extends PgStore {
AND (index_block_hash = ${args.indexBlockHash} OR index_block_hash = '\\x'::bytea)
AND tx_id IN ${sql(txIds)}
`;
await this.updateNftCustodyFromReOrg(sql, {
index_block_hash: args.indexBlockHash,
microblocks: args.microblocks,
});
}

// Update unanchored tx count in `chain_tip` table
Expand All @@ -2539,54 +2534,46 @@ export class PgWriteStore extends PgStore {
sql: PgSqlClient,
args: {
index_block_hash: string;
microblocks: string[];
}
): Promise<void> {
for (const table of [sql`nft_custody`, sql`nft_custody_unanchored`]) {
await sql`
INSERT INTO ${table}
(asset_identifier, value, tx_id, index_block_hash, parent_index_block_hash, microblock_hash,
microblock_sequence, recipient, event_index, tx_index, block_height)
(
SELECT
DISTINCT ON(asset_identifier, value) asset_identifier, value, tx_id, txs.index_block_hash,
txs.parent_index_block_hash, txs.microblock_hash, txs.microblock_sequence, recipient,
nft.event_index, txs.tx_index, txs.block_height
FROM
nft_events AS nft
INNER JOIN
txs USING (tx_id)
WHERE
txs.canonical = true
AND txs.microblock_canonical = true
AND nft.canonical = true
AND nft.microblock_canonical = true
AND nft.index_block_hash = ${args.index_block_hash}
${
args.microblocks.length > 0
? sql`AND nft.microblock_hash IN ${sql(args.microblocks)}`
: sql``
}
ORDER BY
asset_identifier,
value,
txs.block_height DESC,
txs.microblock_sequence DESC,
txs.tx_index DESC,
nft.event_index DESC
)
ON CONFLICT ON CONSTRAINT ${table}_unique DO UPDATE SET
tx_id = EXCLUDED.tx_id,
index_block_hash = EXCLUDED.index_block_hash,
parent_index_block_hash = EXCLUDED.parent_index_block_hash,
microblock_hash = EXCLUDED.microblock_hash,
microblock_sequence = EXCLUDED.microblock_sequence,
recipient = EXCLUDED.recipient,
event_index = EXCLUDED.event_index,
tx_index = EXCLUDED.tx_index,
block_height = EXCLUDED.block_height
`;
}
await sql`
INSERT INTO nft_custody
(asset_identifier, value, tx_id, index_block_hash, parent_index_block_hash, microblock_hash,
microblock_sequence, recipient, event_index, tx_index, block_height)
(
SELECT
DISTINCT ON(asset_identifier, value) asset_identifier, value, tx_id, txs.index_block_hash,
txs.parent_index_block_hash, txs.microblock_hash, txs.microblock_sequence, recipient,
nft.event_index, txs.tx_index, txs.block_height
FROM
nft_events AS nft
INNER JOIN
txs USING (tx_id)
WHERE
txs.canonical = true
AND txs.microblock_canonical = true
AND nft.canonical = true
AND nft.microblock_canonical = true
AND nft.index_block_hash = ${args.index_block_hash}
ORDER BY
asset_identifier,
value,
txs.block_height DESC,
txs.microblock_sequence DESC,
txs.tx_index DESC,
nft.event_index DESC
)
ON CONFLICT ON CONSTRAINT nft_custody_unique DO UPDATE SET
tx_id = EXCLUDED.tx_id,
index_block_hash = EXCLUDED.index_block_hash,
parent_index_block_hash = EXCLUDED.parent_index_block_hash,
microblock_hash = EXCLUDED.microblock_hash,
microblock_sequence = EXCLUDED.microblock_sequence,
recipient = EXCLUDED.recipient,
event_index = EXCLUDED.event_index,
tx_index = EXCLUDED.tx_index,
block_height = EXCLUDED.block_height
`;
}

/**
Expand Down Expand Up @@ -3050,10 +3037,7 @@ export class PgWriteStore extends PgStore {
updatedEntities.markedNonCanonical.nftEvents += nftResult.count;
}
if (nftResult.count)
await this.updateNftCustodyFromReOrg(sql, {
index_block_hash: indexBlockHash,
microblocks: [],
});
await this.updateNftCustodyFromReOrg(sql, { index_block_hash: indexBlockHash });
});
q.enqueue(async () => {
const pox2Result = await sql`
Expand Down
1 change: 0 additions & 1 deletion tests/api/datastore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5884,7 +5884,6 @@ describe('postgres datastore', () => {
limit: 10,
offset: 0,
includeTxMetadata: false,
includeUnanchored: true,
})
).resolves.not.toThrow();
// Tx list details with empty txIds
Expand Down

0 comments on commit a1ce302

Please sign in to comment.