Skip to content

Commit

Permalink
fix: re-org issue with cycle-signer-set data (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x authored Nov 8, 2024
1 parent 8ef5ebf commit 4f592e7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/pg/chainhook/chainhook-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,25 @@ export class ChainhookPgStore extends BasePgStoreModule {
}
}

async insertRewardSetSigners(sql: PgSqlClient, rewardSetSigners: DbRewardSetSigner[]) {
async insertRewardSetSigners(
sql: PgSqlClient,
rewardSetSigners: DbRewardSetSigner[]
): Promise<{ rowsDeleted: number; rowsInserted: number }> {
if (rewardSetSigners.length === 0) {
return { rowsDeleted: 0, rowsInserted: 0 };
}

const cycleNumber = rewardSetSigners[0].cycle_number;
const deleteRows = await sql`
DELETE FROM reward_set_signers
WHERE cycle_number = ${cycleNumber}
`;
if (deleteRows.count > 0) {
this.logger.warn(
`Deleted existing reward set signers for cycle ${cycleNumber} before inserting new rows, deleted ${deleteRows.count} rows`
);
}

let insertCount = 0;
for await (const batch of batchIterate(rewardSetSigners, 500)) {
const result = await sql`
Expand All @@ -547,13 +565,13 @@ export class ChainhookPgStore extends BasePgStoreModule {
`ChainhookPgStore apply reward_set_signers, cycle=${rewardSetSigners[0].cycle_number}, count=${insertCount}`
);
}
return { rowsDeleted: deleteRows.count, rowsInserted: insertCount };
}

private async rollBackTransactions(sql: PgSqlClient, block: StacksEvent) {
const blockHeight = block.block_identifier.index;
await this.rollBackBlock(sql, blockHeight);
await this.rollBackBlockSignerSignatures(sql, blockHeight);
await this.rollBackRewardSetSigners(sql, blockHeight);
}

private async rollBackBlock(sql: PgSqlClient, blockHeight: number) {
Expand All @@ -576,13 +594,4 @@ export class ChainhookPgStore extends BasePgStoreModule {
`ChainhookPgStore rollback block signer signatures for block ${blockHeight}, deleted ${res.count} rows`
);
}

private async rollBackRewardSetSigners(sql: PgSqlClient, blockHeight: number) {
const res = await sql`
DELETE FROM reward_set_signers WHERE block_height = ${blockHeight}
`;
this.logger.info(
`ChainhookPgStore rollback reward set signers for block ${blockHeight}, deleted ${res.count} rows`
);
}
}
44 changes: 44 additions & 0 deletions tests/db/duplicate-signer-set-insert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as fs from 'node:fs';
import { PgStore } from '../../src/pg/pg-store';
import { RpcStackerSetResponse } from '../../src/stacks-core-rpc/stacks-core-rpc-client';
import { rpcStackerSetToDbRewardSetSigners } from '../../src/stacks-core-rpc/stacker-set-updater';

describe('Duplicate signer set insert', () => {
let db: PgStore;

beforeAll(async () => {
db = await PgStore.connect();
});

afterAll(async () => {
await db.close();
});

test('Insert initial signer set', async () => {
const stackerSetDump = JSON.parse(
fs.readFileSync('./tests/dumps/dump-stacker-set-cycle-72-2024-11-02.json', 'utf8')
) as RpcStackerSetResponse;
const insertResult = await db.chainhook.insertRewardSetSigners(
db.sql,
rpcStackerSetToDbRewardSetSigners(stackerSetDump, 72)
);
expect(insertResult).toEqual({
rowsDeleted: 0,
rowsInserted: 11,
});
});

test('Overwrite signer set', async () => {
const stackerSetDump = JSON.parse(
fs.readFileSync('./tests/dumps/dump-stacker-set-cycle-72-2024-11-02.json', 'utf8')
) as RpcStackerSetResponse;
const insertResult = await db.chainhook.insertRewardSetSigners(
db.sql,
rpcStackerSetToDbRewardSetSigners(stackerSetDump, 72)
);
expect(insertResult).toEqual({
rowsDeleted: 11,
rowsInserted: 11,
});
});
});

0 comments on commit 4f592e7

Please sign in to comment.