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: add signer-keys from pox4 events #1857

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docker/docker-compose.dev.stacks-blockchain.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.7'
services:
stacks-blockchain:
image: 'hirosystems/stacks-api-e2e:stacks3.0-1d675fd'
image: 'hirosystems/stacks-api-e2e:stacks3.0-457f270'
restart: on-failure
environment:
STACKS_EVENT_OBSERVER: host.docker.internal:3700
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.dev.stacks-krypton.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.7'
services:
stacks-blockchain:
image: 'hirosystems/stacks-api-e2e:stacks3.0-1d675fd'
image: 'hirosystems/stacks-api-e2e:stacks3.0-457f270'
ports:
- '18443:18443' # bitcoin regtest JSON-RPC interface
- '18444:18444' # bitcoin regtest p2p
Expand Down
132 changes: 132 additions & 0 deletions migrations/1706886038615_update-pox4-signer-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* eslint-disable camelcase */

exports.shorthands = undefined;

/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
exports.up = pgm => {
pgm.dropConstraint('pox4_events', 'valid_event_specific_columns', { ifExists: true });

pgm.addColumn('pox4_events', {
signer_key: {
type: 'bytea',
}
});

pgm.createIndex('pox4_events', 'signer_key');

pgm.addConstraint(
'pox4_events',
'valid_event_specific_columns',
`CHECK (
CASE name
WHEN 'handle-unlock' THEN
first_cycle_locked IS NOT NULL AND
first_unlocked_cycle IS NOT NULL
WHEN 'stack-stx' THEN
lock_period IS NOT NULL AND
lock_amount IS NOT NULL AND
start_burn_height IS NOT NULL AND
unlock_burn_height IS NOT NULL AND
signer_key IS NOT NULL
WHEN 'stack-increase' THEN
increase_by IS NOT NULL AND
total_locked IS NOT NULL
WHEN 'stack-extend' THEN
extend_count IS NOT NULL AND
unlock_burn_height IS NOT NULL AND
signer_key IS NOT NULL
WHEN 'delegate-stx' THEN
amount_ustx IS NOT NULL AND
delegate_to IS NOT NULL
WHEN 'delegate-stack-stx' THEN
lock_period IS NOT NULL AND
lock_amount IS NOT NULL AND
start_burn_height IS NOT NULL AND
unlock_burn_height IS NOT NULL AND
delegator IS NOT NULL
WHEN 'delegate-stack-increase' THEN
increase_by IS NOT NULL AND
total_locked IS NOT NULL AND
delegator IS NOT NULL
WHEN 'delegate-stack-extend' THEN
extend_count IS NOT NULL AND
unlock_burn_height IS NOT NULL AND
delegator IS NOT NULL
WHEN 'stack-aggregation-commit' THEN
reward_cycle IS NOT NULL AND
amount_ustx IS NOT NULL AND
signer_key IS NOT NULL
WHEN 'stack-aggregation-commit-indexed' THEN
reward_cycle IS NOT NULL AND
amount_ustx IS NOT NULL AND
signer_key IS NOT NULL
WHEN 'stack-aggregation-increase' THEN
reward_cycle IS NOT NULL AND
amount_ustx IS NOT NULL
WHEN 'revoke-delegate-stx' THEN
delegate_to IS NOT NULL
ELSE false
END
)`
);
};

/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
exports.down = pgm => {
pgm.dropConstraint('pox4_events', 'valid_event_specific_columns', { ifExists: true });

pgm.dropIndex('pox4_events', 'signer_key');
pgm.dropColumn('pox4_events', 'signer_key');

pgm.addConstraint(
'pox4_events',
'valid_event_specific_columns',
`CHECK (
CASE name
WHEN 'handle-unlock' THEN
first_cycle_locked IS NOT NULL AND
first_unlocked_cycle IS NOT NULL
WHEN 'stack-stx' THEN
lock_period IS NOT NULL AND
lock_amount IS NOT NULL AND
start_burn_height IS NOT NULL AND
unlock_burn_height IS NOT NULL
WHEN 'stack-increase' THEN
increase_by IS NOT NULL AND
total_locked IS NOT NULL
WHEN 'stack-extend' THEN
extend_count IS NOT NULL AND
unlock_burn_height IS NOT NULL
WHEN 'delegate-stx' THEN
amount_ustx IS NOT NULL AND
delegate_to IS NOT NULL
WHEN 'delegate-stack-stx' THEN
lock_period IS NOT NULL AND
lock_amount IS NOT NULL AND
start_burn_height IS NOT NULL AND
unlock_burn_height IS NOT NULL AND
delegator IS NOT NULL
WHEN 'delegate-stack-increase' THEN
increase_by IS NOT NULL AND
total_locked IS NOT NULL AND
delegator IS NOT NULL
WHEN 'delegate-stack-extend' THEN
extend_count IS NOT NULL AND
unlock_burn_height IS NOT NULL AND
delegator IS NOT NULL
WHEN 'stack-aggregation-commit' THEN
reward_cycle IS NOT NULL AND
amount_ustx IS NOT NULL
WHEN 'stack-aggregation-commit-indexed' THEN
reward_cycle IS NOT NULL AND
amount_ustx IS NOT NULL
WHEN 'stack-aggregation-increase' THEN
reward_cycle IS NOT NULL AND
amount_ustx IS NOT NULL
WHEN 'revoke-delegate-stx' THEN
delegate_to IS NOT NULL
ELSE false
END
)`
);
};
4 changes: 4 additions & 0 deletions src/api/controllers/db-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export function parsePoxSyntheticEvent(poxEvent: DbPoxSyntheticEvent) {
lock_period: poxEvent.data.lock_period.toString(),
start_burn_height: poxEvent.data.start_burn_height.toString(),
unlock_burn_height: poxEvent.data.unlock_burn_height.toString(),
signer_key: poxEvent.data.signer_key,
},
};
}
Expand All @@ -249,6 +250,7 @@ export function parsePoxSyntheticEvent(poxEvent: DbPoxSyntheticEvent) {
data: {
extend_count: poxEvent.data.extend_count.toString(),
unlock_burn_height: poxEvent.data.unlock_burn_height.toString(),
signer_key: poxEvent.data.signer_key,
},
};
}
Expand Down Expand Up @@ -300,6 +302,7 @@ export function parsePoxSyntheticEvent(poxEvent: DbPoxSyntheticEvent) {
data: {
reward_cycle: poxEvent.data.reward_cycle.toString(),
amount_ustx: poxEvent.data.amount_ustx.toString(),
signer_key: poxEvent.data.signer_key,
},
};
}
Expand All @@ -309,6 +312,7 @@ export function parsePoxSyntheticEvent(poxEvent: DbPoxSyntheticEvent) {
data: {
reward_cycle: poxEvent.data.reward_cycle.toString(),
amount_ustx: poxEvent.data.amount_ustx.toString(),
signer_key: poxEvent.data.signer_key,
},
};
}
Expand Down
10 changes: 10 additions & 0 deletions src/datastore/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ export interface DbPoxSyntheticStackStxEvent extends DbPoxSyntheticBaseEventData
lock_period: bigint;
start_burn_height: bigint;
unlock_burn_height: bigint;
signer_key: string | null;
};
}

Expand All @@ -383,6 +384,7 @@ export interface DbPoxSyntheticStackExtendEvent extends DbPoxSyntheticBaseEventD
data: {
extend_count: bigint;
unlock_burn_height: bigint;
signer_key: string | null;
};
}

Expand Down Expand Up @@ -429,6 +431,7 @@ export interface DbPoxSyntheticStackAggregationCommitEvent extends DbPoxSyntheti
data: {
reward_cycle: bigint;
amount_ustx: bigint;
signer_key: string | null;
};
}

Expand All @@ -438,6 +441,7 @@ export interface DbPoxSyntheticStackAggregationCommitIndexedEvent
data: {
reward_cycle: bigint;
amount_ustx: bigint;
signer_key: string | null;
};
}

Expand Down Expand Up @@ -1276,6 +1280,9 @@ export interface PoxSyntheticEventQueryResult {

// unique to stack-aggregation-commit, delegate-stx
amount_ustx: string | null;

// [pox4] unique to stacks-stx, stack-extend, stack-aggregation-commit, stack-aggregation-commit-indexed
signer_key?: string | null;
}

export interface PoxSyntheticEventInsertValues {
Expand Down Expand Up @@ -1334,6 +1341,9 @@ export interface PoxSyntheticEventInsertValues {

// unique to stack-aggregation-commit, delegate-stx
amount_ustx: PgNumeric | null;

// [pox4] unique to stacks-stx, stack-extend, stack-aggregation-commit, stack-aggregation-commit-indexed
signer_key?: PgBytea | null;
}

export interface NftEventInsertValues {
Expand Down
4 changes: 4 additions & 0 deletions src/datastore/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ export function parseDbPoxSyntheticEvent(row: PoxSyntheticEventQueryResult): DbP
lock_period: BigInt(unwrapOptionalProp(row, 'lock_period')),
start_burn_height: BigInt(unwrapOptionalProp(row, 'start_burn_height')),
unlock_burn_height: BigInt(unwrapOptionalProp(row, 'unlock_burn_height')),
signer_key: row.signer_key ?? null,
},
};
return {
Expand Down Expand Up @@ -684,6 +685,7 @@ export function parseDbPoxSyntheticEvent(row: PoxSyntheticEventQueryResult): DbP
data: {
extend_count: BigInt(unwrapOptionalProp(row, 'extend_count')),
unlock_burn_height: BigInt(unwrapOptionalProp(row, 'unlock_burn_height')),
signer_key: row.signer_key ?? null,
},
};
return {
Expand Down Expand Up @@ -762,6 +764,7 @@ export function parseDbPoxSyntheticEvent(row: PoxSyntheticEventQueryResult): DbP
data: {
reward_cycle: BigInt(unwrapOptionalProp(row, 'reward_cycle')),
amount_ustx: BigInt(unwrapOptionalProp(row, 'amount_ustx')),
signer_key: row.signer_key ?? null,
},
};
return {
Expand All @@ -776,6 +779,7 @@ export function parseDbPoxSyntheticEvent(row: PoxSyntheticEventQueryResult): DbP
data: {
reward_cycle: BigInt(unwrapOptionalProp(row, 'reward_cycle')),
amount_ustx: BigInt(unwrapOptionalProp(row, 'amount_ustx')),
signer_key: row.signer_key ?? null,
},
};
return {
Expand Down
6 changes: 5 additions & 1 deletion src/datastore/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1910,8 +1910,12 @@ export class PgStore extends BasePgStore {
poxTable: PoxSyntheticEventTable;
}): Promise<DbPoxSyntheticEvent[]> {
return await this.sqlTransaction(async sql => {
const cols =
poxTable === 'pox4_events'
? [...POX_SYNTHETIC_EVENT_COLUMNS, 'signer_key']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not needed, but this could be an array.push, depending on how often this code path runs, to avoid a spread

: POX_SYNTHETIC_EVENT_COLUMNS;
const queryResults = await sql<PoxSyntheticEventQueryResult[]>`
SELECT ${sql(POX_SYNTHETIC_EVENT_COLUMNS)}
SELECT ${sql(cols)}
FROM ${sql(poxTable)}
WHERE canonical = true AND microblock_canonical = true
ORDER BY block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
Expand Down
15 changes: 15 additions & 0 deletions src/datastore/pg-write-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ export class PgWriteStore extends PgStore {
reward_cycle: null,
amount_ustx: null,
};
if (poxTable === 'pox4_events') {
value.signer_key = null;
}

// Set event-specific columns
switch (event.name) {
Expand All @@ -826,6 +829,9 @@ export class PgWriteStore extends PgStore {
value.lock_amount = event.data.lock_amount.toString();
value.start_burn_height = event.data.start_burn_height.toString();
value.unlock_burn_height = event.data.unlock_burn_height.toString();
if (poxTable === 'pox4_events') {
value.signer_key = event.data.signer_key;
}
break;
}
case SyntheticPoxEventName.StackIncrease: {
Expand All @@ -836,6 +842,9 @@ export class PgWriteStore extends PgStore {
case SyntheticPoxEventName.StackExtend: {
value.extend_count = event.data.extend_count.toString();
value.unlock_burn_height = event.data.unlock_burn_height.toString();
if (poxTable === 'pox4_events') {
value.signer_key = event.data.signer_key;
}
break;
}
case SyntheticPoxEventName.DelegateStx: {
Expand Down Expand Up @@ -867,11 +876,17 @@ export class PgWriteStore extends PgStore {
case SyntheticPoxEventName.StackAggregationCommit: {
value.reward_cycle = event.data.reward_cycle.toString();
value.amount_ustx = event.data.amount_ustx.toString();
if (poxTable === 'pox4_events') {
value.signer_key = event.data.signer_key;
}
break;
}
case SyntheticPoxEventName.StackAggregationCommitIndexed: {
value.reward_cycle = event.data.reward_cycle.toString();
value.amount_ustx = event.data.amount_ustx.toString();
if (poxTable === 'pox4_events') {
value.signer_key = event.data.signer_key;
}
break;
}
case SyntheticPoxEventName.StackAggregationIncrease: {
Expand Down
Loading
Loading