@secondlayer/shared 17.0.0 → 17.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ import { type Kysely, sql } from "kysely";
2
+ import { onChainPlane } from "../src/db/migration-role.ts";
3
+
4
+ // `calculate-rewards` prints its reward cycle as `stx-cycle`, but the decoder
5
+ // left `reward_cycle` null, so `?reward_cycle=` never found those rows. The
6
+ // decoder now promotes it; fill history from the stored `data` (a few dozen
7
+ // rows). Skipped when the table is absent.
8
+ async function tableExists(db: Kysely<unknown>, table: string) {
9
+ const { rows } = await sql<{ exists: boolean }>`
10
+ SELECT to_regclass(${`public.${table}`}) IS NOT NULL AS exists
11
+ `.execute(db);
12
+ return rows[0]?.exists === true;
13
+ }
14
+
15
+ export async function up(db: Kysely<unknown>): Promise<void> {
16
+ await sql`SET lock_timeout = '30s'`.execute(db);
17
+ await onChainPlane(async () => {
18
+ if (!(await tableExists(db, "pox5_events"))) return;
19
+ await sql`
20
+ UPDATE pox5_events
21
+ SET reward_cycle = (data->>'stx-cycle')::integer
22
+ WHERE topic = 'calculate-rewards'
23
+ AND reward_cycle IS NULL
24
+ AND data->>'stx-cycle' ~ '^[0-9]{1,9}$'
25
+ `.execute(db);
26
+ });
27
+ }
28
+
29
+ export async function down(): Promise<void> {
30
+ // Data-only fill of values the decoder now writes; nothing to undo.
31
+ }
@@ -0,0 +1,51 @@
1
+ import { type Kysely, sql } from "kysely";
2
+ import { onChainPlane } from "../src/db/migration-role.ts";
3
+
4
+ // `chain_reorgs.old_index_block_hash` / `new_index_block_hash` held the Stacks
5
+ // block hash (`blocks.hash`), not the index block hash they promise: every row
6
+ // written before this migration did. Rewrite each side to the index hash of the
7
+ // block it names: from `blocks` when that block still holds the fork height,
8
+ // else from a staged `pending_fork_blocks` payload. A side neither holds goes
9
+ // null (unknown) rather than keep a hash of the wrong kind. A side that already
10
+ // names an index hash at its height (written by the fixed indexer) is left
11
+ // alone. Skipped when a table is absent.
12
+ async function tableExists(db: Kysely<unknown>, table: string) {
13
+ const { rows } = await sql<{ exists: boolean }>`
14
+ SELECT to_regclass(${table}) IS NOT NULL AS exists
15
+ `.execute(db);
16
+ return rows[0]?.exists === true;
17
+ }
18
+
19
+ export async function up(db: Kysely<unknown>): Promise<void> {
20
+ await onChainPlane(async () => {
21
+ for (const table of ["chain_reorgs", "blocks", "pending_fork_blocks"]) {
22
+ if (!(await tableExists(db, table))) return;
23
+ }
24
+ await sql`SET lock_timeout = '30s'`.execute(db);
25
+ for (const side of ["old", "new"] as const) {
26
+ const column = sql.ref(`${side}_index_block_hash`);
27
+ const named = sql.ref(`r.${side}_index_block_hash`);
28
+ await sql`
29
+ UPDATE chain_reorgs r
30
+ SET ${column} = COALESCE(
31
+ (SELECT b.index_block_hash FROM blocks b
32
+ WHERE b.height = r.fork_point_height AND b.hash = ${named}),
33
+ (SELECT NULLIF(p.payload->>'index_block_hash', '')
34
+ FROM pending_fork_blocks p
35
+ WHERE p.height = r.fork_point_height AND p.block_hash = ${named}
36
+ LIMIT 1))
37
+ WHERE ${named} IS NOT NULL
38
+ AND NOT EXISTS (SELECT 1 FROM blocks b
39
+ WHERE b.height = r.fork_point_height
40
+ AND b.index_block_hash = ${named})
41
+ AND NOT EXISTS (SELECT 1 FROM pending_fork_blocks p
42
+ WHERE p.height = r.fork_point_height
43
+ AND p.payload->>'index_block_hash' = ${named})
44
+ `.execute(db);
45
+ }
46
+ });
47
+ }
48
+
49
+ export async function down(): Promise<void> {
50
+ // Data repair: the block hashes it replaced were wrong, nothing to restore.
51
+ }
@@ -0,0 +1,71 @@
1
+ import { type Kysely, type RawBuilder, sql } from "kysely";
2
+ import { onChainPlane } from "../src/db/migration-role.ts";
3
+
4
+ // Block ingest now evicts a pending tx once a confirmed tx from its sender
5
+ // spends its nonce. Rows already in `mempool_transactions` predate that and
6
+ // most can never confirm (prod: 173 of 207), so clear them once: a pending tx
7
+ // whose origin nonce is at or below a nonce its sender confirmed is dead.
8
+ //
9
+ // The node admitted each pending tx with a live nonce, so the tx that spent it
10
+ // landed after the oldest receipt: only blocks since then are scanned (an hour
11
+ // of slack, within the last 100k heights), never a sender's whole history.
12
+ // Skipped when a table is absent.
13
+ async function tableExists(db: Kysely<unknown>, table: string) {
14
+ const { rows } = await sql<{ exists: boolean }>`
15
+ SELECT to_regclass(${table}) IS NOT NULL AS exists
16
+ `.execute(db);
17
+ return rows[0]?.exists === true;
18
+ }
19
+
20
+ // SIP-005: the origin nonce is the 8 bytes after version(1) chain_id(4)
21
+ // auth_type(1) hash_mode(1) signer(20), for standard (0x04) and sponsored
22
+ // (0x05) auth alike. Same layout `rawTxNonce` reads.
23
+ function hexOf(column: RawBuilder<unknown>) {
24
+ return sql`regexp_replace(${column}, '^0x', '')`;
25
+ }
26
+ function hasNonce(column: RawBuilder<unknown>) {
27
+ return sql`(substr(${hexOf(column)}, 11, 2) IN ('04', '05')
28
+ AND substr(${hexOf(column)}, 55, 16) ~ '^[0-9a-fA-F]{16}$')`;
29
+ }
30
+ function nonceOf(column: RawBuilder<unknown>) {
31
+ return sql`('x' || substr(${hexOf(column)}, 55, 16))::bit(64)::bigint`;
32
+ }
33
+
34
+ export async function up(db: Kysely<unknown>): Promise<void> {
35
+ await onChainPlane(async () => {
36
+ for (const table of ["mempool_transactions", "transactions", "blocks"]) {
37
+ if (!(await tableExists(db, table))) return;
38
+ }
39
+ const { rows } = await sql<{ floor: string | number | null }>`
40
+ SELECT min(height) AS floor
41
+ FROM blocks
42
+ WHERE height > (SELECT max(height) - 100000 FROM blocks)
43
+ AND timestamp >= (
44
+ SELECT extract(epoch FROM min(received_at) - interval '1 hour')
45
+ FROM mempool_transactions)
46
+ `.execute(db);
47
+ if (rows[0]?.floor == null) return;
48
+ const floor = Number(rows[0].floor);
49
+ const pending = sql.ref("m.raw_tx");
50
+ const confirmed = sql.ref("t.raw_tx");
51
+ await sql`
52
+ DELETE FROM mempool_transactions m
53
+ USING (
54
+ SELECT t.sender, max(${nonceOf(confirmed)}) AS spent
55
+ FROM transactions t
56
+ JOIN blocks b ON b.height = t.block_height AND b.canonical
57
+ WHERE t.block_height >= ${floor}
58
+ AND t.sender IN (SELECT sender FROM mempool_transactions)
59
+ AND ${hasNonce(confirmed)}
60
+ GROUP BY t.sender
61
+ ) c
62
+ WHERE m.sender = c.sender
63
+ AND ${hasNonce(pending)}
64
+ AND ${nonceOf(pending)} <= c.spent
65
+ `.execute(db);
66
+ });
67
+ }
68
+
69
+ export async function down(): Promise<void> {
70
+ // Data cleanup: the evicted txs could never confirm, nothing to restore.
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secondlayer/shared",
3
- "version": "17.0.0",
3
+ "version": "17.1.0",
4
4
  "description": "Internal building blocks shared across Secondlayer packages.",
5
5
  "license": "MIT",
6
6
  "repository": {