Skip to content

Commit eda79d0

Browse files
committed
fix: do not store duplicate MockProposals, MockSignatures, or MockBlocks
1 parent d0e79ba commit eda79d0

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

migrations/1729684505758_mock_proposals.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,8 @@ export function up(pgm: MigrationBuilder): void {
5858
pgm.createIndex('mock_proposals', ['stacks_tip']);
5959
pgm.createIndex('mock_proposals', ['index_block_hash']);
6060
pgm.createIndex('mock_proposals', ['burn_block_height']);
61+
62+
pgm.createConstraint('mock_proposals', 'mock_proposals_idb_unique', {
63+
unique: ['index_block_hash'],
64+
});
6165
}

migrations/1729684505759_mock_signature.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,8 @@ export function up(pgm: MigrationBuilder): void {
7171
pgm.createIndex('mock_signatures', ['stacks_tip']);
7272
pgm.createIndex('mock_signatures', ['index_block_hash']);
7373
pgm.createIndex('mock_signatures', ['burn_block_height']);
74+
75+
pgm.createConstraint('mock_signatures', 'mock_signatures_signer_key_idb_unique', {
76+
unique: ['signer_key', 'index_block_hash'],
77+
});
7478
}

migrations/1729684505760_mock_blocks.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export function up(pgm: MigrationBuilder): void {
6565
pgm.createIndex('mock_blocks', ['index_block_hash']);
6666
pgm.createIndex('mock_blocks', ['burn_block_height']);
6767

68+
pgm.createConstraint('mock_blocks', 'mock_blocks_idb_unique', {
69+
unique: ['index_block_hash'],
70+
});
71+
6872
// Mock block signer signatures
6973
pgm.createTable('mock_block_signer_signatures', {
7074
id: {
@@ -99,4 +103,8 @@ export function up(pgm: MigrationBuilder): void {
99103
pgm.createIndex('mock_block_signer_signatures', ['stacks_tip']);
100104
pgm.createIndex('mock_block_signer_signatures', ['stacks_tip_height']);
101105
pgm.createIndex('mock_block_signer_signatures', ['index_block_hash']);
106+
107+
pgm.createConstraint('mock_block_signer_signatures', 'mock_block_signers_idb_unique', {
108+
unique: ['index_block_hash', 'signer_key'],
109+
});
102110
}

src/pg/chainhook/chainhook-pg-store.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,18 @@ export class ChainhookPgStore extends BasePgStoreModule {
188188
network_id: messageData.mock_proposal.peer_info.network_id,
189189
index_block_hash: normalizeHexString(messageData.mock_proposal.peer_info.index_block_hash),
190190
};
191-
await sql`
191+
const result = await sql`
192192
INSERT INTO mock_blocks ${sql(dbMockBlock)}
193+
ON CONFLICT ON CONSTRAINT mock_blocks_idb_unique DO NOTHING
193194
`;
194195

196+
if (result.count === 0) {
197+
logger.info(
198+
`Skipped inserting duplicate mock block height=${dbMockBlock.stacks_tip_height}, hash=${dbMockBlock.stacks_tip}`
199+
);
200+
return;
201+
}
202+
195203
for (const batch of batchIterate(messageData.mock_signatures, 500)) {
196204
const sigs = batch.map(sig => {
197205
const dbSig: DbMockBlockSignerSignature = {
@@ -235,9 +243,15 @@ export class ChainhookPgStore extends BasePgStoreModule {
235243
// Metadata fields
236244
metadata_server_version: messageData.metadata.server_version,
237245
};
238-
await sql`
246+
const result = await sql`
239247
INSERT INTO mock_signatures ${sql(dbMockSignature)}
248+
ON CONFLICT ON CONSTRAINT mock_signatures_signer_key_idb_unique DO NOTHING
240249
`;
250+
if (result.count === 0) {
251+
logger.info(
252+
`Skipped inserting duplicate mock signature height=${dbMockSignature.stacks_tip_height}, hash=${dbMockSignature.stacks_tip}, signer=${dbMockSignature.signer_key}`
253+
);
254+
}
241255
}
242256

243257
private async applyMockProposal(
@@ -258,9 +272,15 @@ export class ChainhookPgStore extends BasePgStoreModule {
258272
network_id: messageData.network_id,
259273
index_block_hash: normalizeHexString(messageData.index_block_hash),
260274
};
261-
await sql`
275+
const result = await sql`
262276
INSERT INTO mock_proposals ${sql(dbMockProposal)}
277+
ON CONFLICT ON CONSTRAINT mock_proposals_idb_unique DO NOTHING
263278
`;
279+
if (result.count === 0) {
280+
logger.info(
281+
`Skipped inserting duplicate mock proposal height=${dbMockProposal.stacks_tip_height}, hash=${dbMockProposal.stacks_tip}`
282+
);
283+
}
264284
}
265285

266286
private async applyBlockProposal(
@@ -332,7 +352,7 @@ export class ChainhookPgStore extends BasePgStoreModule {
332352

333353
if (result.count === 0) {
334354
logger.info(
335-
`Skipped inserting duplicate block response signer_key=${dbBlockResponse.signer_key}, hash=${dbBlockResponse.signer_sighash}`
355+
`Skipped inserting duplicate block response signer=${dbBlockResponse.signer_key}, hash=${dbBlockResponse.signer_sighash}`
336356
);
337357
}
338358
}

0 commit comments

Comments
 (0)