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

feat: improve insert nft custody #1880

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
101 changes: 52 additions & 49 deletions src/datastore/pg-write-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,61 +1163,64 @@ export class PgWriteStore extends PgStore {
events: DbNftEvent[],
microblock: boolean = false
) {
for (const batch of batchIterate(events, INSERT_BATCH_SIZE)) {
const custodyInsertsMap = new Map<string, NftCustodyInsertValues>();
const nftEventInserts: NftEventInsertValues[] = [];
for (const event of batch) {
const custodyItem: NftCustodyInsertValues = {
asset_identifier: event.asset_identifier,
value: event.value,
tx_id: event.tx_id,
index_block_hash: tx.index_block_hash,
parent_index_block_hash: tx.parent_index_block_hash,
microblock_hash: tx.microblock_hash,
microblock_sequence: tx.microblock_sequence,
recipient: event.recipient ?? null,
event_index: event.event_index,
tx_index: event.tx_index,
block_height: event.block_height,
};
// Avoid duplicates on NFT custody inserts, because we could run into an `ON CONFLICT DO
// UPDATE command cannot affect row a second time` error otherwise.
const custodyKey = `${event.asset_identifier}_${event.value}`;
const currCustody = custodyInsertsMap.get(custodyKey);
if (currCustody) {
if (
custodyItem.block_height > currCustody.block_height ||
(custodyItem.block_height == currCustody.block_height &&
custodyItem.microblock_sequence > currCustody.microblock_sequence) ||
(custodyItem.block_height == currCustody.block_height &&
custodyItem.microblock_sequence == currCustody.microblock_sequence &&
custodyItem.tx_index > currCustody.tx_index) ||
(custodyItem.block_height == currCustody.block_height &&
custodyItem.microblock_sequence == currCustody.microblock_sequence &&
custodyItem.tx_index == currCustody.tx_index &&
custodyItem.event_index > currCustody.event_index)
) {
custodyInsertsMap.set(custodyKey, custodyItem);
}
} else {
const custodyInsertsMap = new Map<string, NftCustodyInsertValues>();
const nftEventInserts: NftEventInsertValues[] = [];
for (const event of events) {
const custodyItem: NftCustodyInsertValues = {
asset_identifier: event.asset_identifier,
value: event.value,
tx_id: event.tx_id,
index_block_hash: tx.index_block_hash,
parent_index_block_hash: tx.parent_index_block_hash,
microblock_hash: tx.microblock_hash,
microblock_sequence: tx.microblock_sequence,
recipient: event.recipient ?? null,
event_index: event.event_index,
tx_index: event.tx_index,
block_height: event.block_height,
};
// Avoid duplicates on NFT custody inserts, because we could run into an `ON CONFLICT DO
// UPDATE command cannot affect row a second time` error otherwise.
const custodyKey = `${event.asset_identifier}_${event.value}`;
const currCustody = custodyInsertsMap.get(custodyKey);
if (currCustody) {
if (
custodyItem.block_height > currCustody.block_height ||
(custodyItem.block_height == currCustody.block_height &&
custodyItem.microblock_sequence > currCustody.microblock_sequence) ||
(custodyItem.block_height == currCustody.block_height &&
custodyItem.microblock_sequence == currCustody.microblock_sequence &&
custodyItem.tx_index > currCustody.tx_index) ||
(custodyItem.block_height == currCustody.block_height &&
custodyItem.microblock_sequence == currCustody.microblock_sequence &&
custodyItem.tx_index == currCustody.tx_index &&
custodyItem.event_index > currCustody.event_index)
) {
custodyInsertsMap.set(custodyKey, custodyItem);
}
const valuesItem: NftEventInsertValues = {
...custodyItem,
microblock_canonical: tx.microblock_canonical,
canonical: event.canonical,
sender: event.sender ?? null,
asset_event_type_id: event.asset_event_type_id,
};
nftEventInserts.push(valuesItem);
} else {
custodyInsertsMap.set(custodyKey, custodyItem);
}
const valuesItem: NftEventInsertValues = {
...custodyItem,
microblock_canonical: tx.microblock_canonical,
canonical: event.canonical,
sender: event.sender ?? null,
asset_event_type_id: event.asset_event_type_id,
};
nftEventInserts.push(valuesItem);
}
for (const batch of batchIterate(nftEventInserts, INSERT_BATCH_SIZE)) {
await sql`
INSERT INTO nft_events ${sql(nftEventInserts)}
INSERT INTO nft_events ${sql(batch)}
`;
if (tx.canonical && tx.microblock_canonical) {
const table = microblock ? sql`nft_custody_unanchored` : sql`nft_custody`;
}

if (tx.canonical && tx.microblock_canonical) {
const table = microblock ? sql`nft_custody_unanchored` : sql`nft_custody`;
for (const batch of batchIterate(Array.from(custodyInsertsMap.values()), INSERT_BATCH_SIZE)) {
await sql`
INSERT INTO ${table} ${sql(Array.from(custodyInsertsMap.values()))}
INSERT INTO ${table} ${sql(batch)}
ON CONFLICT ON CONSTRAINT ${table}_unique DO UPDATE SET
tx_id = EXCLUDED.tx_id,
index_block_hash = EXCLUDED.index_block_hash,
Expand Down
Loading