@secondlayer/shared 17.1.0 → 18.0.1
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.
- package/dist/src/archive/range-digest.d.ts +2 -65
- package/dist/src/archive/reference.d.ts +11 -2
- package/dist/src/archive/reference.js +48 -6
- package/dist/src/archive/reference.js.map +3 -3
- package/dist/src/archive/semantic-digest-builder.d.ts +2 -65
- package/dist/src/coverage/index.d.ts +12 -76
- package/dist/src/coverage/index.js +19 -60
- package/dist/src/coverage/index.js.map +3 -3
- package/dist/src/db/index.d.ts +3 -111
- package/dist/src/db/index.js +1 -34
- package/dist/src/db/index.js.map +5 -5
- package/dist/src/db/queries/chain-reorgs.d.ts +2 -65
- package/dist/src/db/queries/chain-reorgs.js +1 -34
- package/dist/src/db/queries/chain-reorgs.js.map +5 -5
- package/dist/src/db/queries/contracts.d.ts +2 -65
- package/dist/src/db/queries/contracts.js.map +2 -2
- package/dist/src/db/queries/instance.d.ts +2 -65
- package/dist/src/db/queries/integrity.d.ts +2 -65
- package/dist/src/db/queries/subgraph-gaps.d.ts +2 -65
- package/dist/src/db/queries/subgraph-operations.d.ts +2 -65
- package/dist/src/db/queries/subgraphs.d.ts +2 -65
- package/dist/src/db/queries/sync-scope.d.ts +2 -65
- package/dist/src/db/queries/webhook-replays.d.ts +2 -65
- package/dist/src/db/queries/webhooks.d.ts +2 -65
- package/dist/src/db/schema.d.ts +3 -64
- package/dist/src/db/schema.js.map +2 -2
- package/dist/src/index.d.ts +3 -111
- package/dist/src/index.js +1 -34
- package/dist/src/index.js.map +5 -5
- package/dist/src/insights/index.d.ts +2 -65
- package/dist/src/leader.js +1 -34
- package/dist/src/leader.js.map +5 -5
- package/dist/src/node/local-client.d.ts +2 -65
- package/dist/src/queue/listener.js +1 -34
- package/dist/src/queue/listener.js.map +5 -5
- package/migrations/0211_decoded_events_slim.ts +114 -0
- package/migrations/0212_events_transactions_redundant_indexes.ts +71 -0
- package/migrations/0213_drop_sbtc_token_events.ts +32 -0
- package/migrations/0214_drop_coverage_receipts.ts +24 -0
- package/migrations/0215_decoder_checkpoints_last_output_at.ts +33 -0
- package/package.json +2 -2
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Slim `decoded_events` (chain plane / SOURCE).
|
|
6
|
+
*
|
|
7
|
+
* Drops what no query uses: the `(tx_id, event_index)` and bare `event_type`
|
|
8
|
+
* indexes (every event_type read is served by a composite), and the
|
|
9
|
+
* `source_cursor` (always equal to `cursor`) and `microblock_hash` (never
|
|
10
|
+
* written) columns. Both are metadata-only on any table size; the deploy
|
|
11
|
+
* stops lock holders before migrating.
|
|
12
|
+
*
|
|
13
|
+
* The contract/sender/recipient composites become partial on their own
|
|
14
|
+
* column being NOT NULL: equality and IN filters imply it, so every reader
|
|
15
|
+
* still uses them, and rows without the column (prints have no sender) stop
|
|
16
|
+
* costing index space. Rebuilding them is a full index build, so it only
|
|
17
|
+
* runs inline while the table is small (fresh installs, and the v2 database
|
|
18
|
+
* before its decoder rebuild). On a large table it skips with a NOTICE and
|
|
19
|
+
* the old full indexes stay until the table is rebuilt.
|
|
20
|
+
*
|
|
21
|
+
* `payload` new values compress with lz4 (metadata-only; existing rows keep
|
|
22
|
+
* pglz until rewritten).
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export const INLINE_BUILD_MAX_BYTES = 1024 ** 3; // 1 GiB
|
|
26
|
+
// column -> index name (the contract index predates the `<column>_` naming)
|
|
27
|
+
const FILTER_INDEXES = [
|
|
28
|
+
["contract_id", "decoded_events_contract_height_event_idx"],
|
|
29
|
+
["sender", "decoded_events_sender_height_event_idx"],
|
|
30
|
+
["recipient", "decoded_events_recipient_height_event_idx"],
|
|
31
|
+
] as const;
|
|
32
|
+
|
|
33
|
+
export function shouldRebuildFilterIndexes(bytes: number): boolean {
|
|
34
|
+
return bytes <= INLINE_BUILD_MAX_BYTES;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
38
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
39
|
+
SELECT to_regclass(${table}) IS NOT NULL AS exists
|
|
40
|
+
`.execute(db);
|
|
41
|
+
return rows[0]?.exists === true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
45
|
+
await onChainPlane(async () => {
|
|
46
|
+
if (!(await tableExists(db, "decoded_events"))) return;
|
|
47
|
+
|
|
48
|
+
await sql`DROP INDEX IF EXISTS decoded_events_tx_id_event_index_idx`.execute(
|
|
49
|
+
db,
|
|
50
|
+
);
|
|
51
|
+
await sql`DROP INDEX IF EXISTS decoded_events_event_type_idx`.execute(db);
|
|
52
|
+
await sql`
|
|
53
|
+
ALTER TABLE decoded_events
|
|
54
|
+
DROP COLUMN IF EXISTS source_cursor,
|
|
55
|
+
DROP COLUMN IF EXISTS microblock_hash
|
|
56
|
+
`.execute(db);
|
|
57
|
+
await sql`ALTER TABLE decoded_events ALTER COLUMN payload SET COMPRESSION lz4`.execute(
|
|
58
|
+
db,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const size = await sql<{ bytes: string }>`
|
|
62
|
+
SELECT pg_total_relation_size('decoded_events')::text AS bytes
|
|
63
|
+
`.execute(db);
|
|
64
|
+
const bytes = Number(size.rows[0]?.bytes ?? 0);
|
|
65
|
+
if (!shouldRebuildFilterIndexes(bytes)) {
|
|
66
|
+
await sql`DO $$ BEGIN RAISE NOTICE ${sql.lit(
|
|
67
|
+
`decoded_events partial filter indexes SKIPPED: table is ${bytes} bytes — they become partial when the table is rebuilt (see this migration's header).`,
|
|
68
|
+
)}; END $$`.execute(db);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
for (const [column, name] of FILTER_INDEXES) {
|
|
72
|
+
const index = sql.ref(name);
|
|
73
|
+
const col = sql.ref(column);
|
|
74
|
+
await sql`DROP INDEX IF EXISTS ${index}`.execute(db);
|
|
75
|
+
await sql`
|
|
76
|
+
CREATE INDEX ${index} ON decoded_events (${col}, block_height, event_index)
|
|
77
|
+
WHERE ${col} IS NOT NULL
|
|
78
|
+
`.execute(db);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
84
|
+
await onChainPlane(async () => {
|
|
85
|
+
if (!(await tableExists(db, "decoded_events"))) return;
|
|
86
|
+
await sql`
|
|
87
|
+
ALTER TABLE decoded_events
|
|
88
|
+
ADD COLUMN IF NOT EXISTS source_cursor TEXT,
|
|
89
|
+
ADD COLUMN IF NOT EXISTS microblock_hash TEXT
|
|
90
|
+
`.execute(db);
|
|
91
|
+
await sql`UPDATE decoded_events SET source_cursor = cursor WHERE source_cursor IS NULL`.execute(
|
|
92
|
+
db,
|
|
93
|
+
);
|
|
94
|
+
await sql`ALTER TABLE decoded_events ALTER COLUMN source_cursor SET NOT NULL`.execute(
|
|
95
|
+
db,
|
|
96
|
+
);
|
|
97
|
+
await sql`ALTER TABLE decoded_events ALTER COLUMN payload SET COMPRESSION pglz`.execute(
|
|
98
|
+
db,
|
|
99
|
+
);
|
|
100
|
+
await sql`CREATE INDEX IF NOT EXISTS decoded_events_event_type_idx ON decoded_events (event_type)`.execute(
|
|
101
|
+
db,
|
|
102
|
+
);
|
|
103
|
+
await sql`CREATE INDEX IF NOT EXISTS decoded_events_tx_id_event_index_idx ON decoded_events (tx_id, event_index)`.execute(
|
|
104
|
+
db,
|
|
105
|
+
);
|
|
106
|
+
for (const [column, name] of FILTER_INDEXES) {
|
|
107
|
+
const index = sql.ref(name);
|
|
108
|
+
await sql`DROP INDEX IF EXISTS ${index}`.execute(db);
|
|
109
|
+
await sql`CREATE INDEX ${index} ON decoded_events (${sql.ref(column)}, block_height, event_index)`.execute(
|
|
110
|
+
db,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Drop raw-table indexes another index already covers (chain plane / SOURCE).
|
|
6
|
+
*
|
|
7
|
+
* - `events_type_idx`: no query filters `events` by type alone; every type
|
|
8
|
+
* predicate rides a block_height range (`events_height_type_idx`).
|
|
9
|
+
* - `events_block_height_idx`: `events_height_type_idx` and
|
|
10
|
+
* `events_logical_id_uniq` both lead with block_height.
|
|
11
|
+
* - `transactions_block_height_idx`: `transactions_height_tx_index_uniq`
|
|
12
|
+
* leads with block_height. That one is built by hand on large tables (0199
|
|
13
|
+
* skips them), so this drop only runs where it exists and is valid.
|
|
14
|
+
*
|
|
15
|
+
* Each drop is guarded on its covering index being present and valid; a
|
|
16
|
+
* skipped drop raises a NOTICE. Plain DROP INDEX is metadata-only; the deploy
|
|
17
|
+
* stops lock holders before migrating.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
async function validIndex(db: Kysely<unknown>, name: string): Promise<boolean> {
|
|
21
|
+
const { rows } = await sql<{ ok: boolean }>`
|
|
22
|
+
SELECT i.indisvalid AND i.indisready AS ok
|
|
23
|
+
FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid
|
|
24
|
+
WHERE c.relname = ${name}
|
|
25
|
+
`.execute(db);
|
|
26
|
+
return rows[0]?.ok === true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function dropIfCovered(
|
|
30
|
+
db: Kysely<unknown>,
|
|
31
|
+
index: string,
|
|
32
|
+
coveredBy: readonly string[],
|
|
33
|
+
): Promise<void> {
|
|
34
|
+
for (const cover of coveredBy) {
|
|
35
|
+
if (await validIndex(db, cover)) {
|
|
36
|
+
await sql`DROP INDEX IF EXISTS ${sql.ref(index)}`.execute(db);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
await sql`DO $$ BEGIN RAISE NOTICE ${sql.lit(
|
|
41
|
+
`${index} kept: none of ${coveredBy.join(", ")} exists and is valid — rerun this drop by hand once it does.`,
|
|
42
|
+
)}; END $$`.execute(db);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
46
|
+
await onChainPlane(async () => {
|
|
47
|
+
await sql`DROP INDEX IF EXISTS events_type_idx`.execute(db);
|
|
48
|
+
await dropIfCovered(db, "events_block_height_idx", [
|
|
49
|
+
"events_height_type_idx",
|
|
50
|
+
"events_logical_id_uniq",
|
|
51
|
+
]);
|
|
52
|
+
await dropIfCovered(db, "transactions_block_height_idx", [
|
|
53
|
+
"transactions_height_tx_index_uniq",
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
59
|
+
await onChainPlane(async () => {
|
|
60
|
+
// Inline builds: fine on dev/test; on a large table build them CONCURRENTLY by hand first.
|
|
61
|
+
await sql`CREATE INDEX IF NOT EXISTS events_type_idx ON events (type)`.execute(
|
|
62
|
+
db,
|
|
63
|
+
);
|
|
64
|
+
await sql`CREATE INDEX IF NOT EXISTS events_block_height_idx ON events (block_height)`.execute(
|
|
65
|
+
db,
|
|
66
|
+
);
|
|
67
|
+
await sql`CREATE INDEX IF NOT EXISTS transactions_block_height_idx ON transactions (block_height)`.execute(
|
|
68
|
+
db,
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
// `sbtc_token_events` has no reader: its API went away with the datasets
|
|
5
|
+
// product surface. Every row duplicates a `decoded_events` ft_transfer /
|
|
6
|
+
// ft_mint / ft_burn row for the sBTC asset (the classic decoders write those
|
|
7
|
+
// for every contract), and the one extra column, `memo`, is the next print
|
|
8
|
+
// event, which `decoded_events` also holds. Chain-plane only; each step is
|
|
9
|
+
// skipped when its table is absent.
|
|
10
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
11
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
12
|
+
SELECT to_regclass(${`public.${table}`}) IS NOT NULL AS exists
|
|
13
|
+
`.execute(db);
|
|
14
|
+
return rows[0]?.exists === true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
18
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
19
|
+
await onChainPlane(async () => {
|
|
20
|
+
if (await tableExists(db, "sbtc_token_events")) {
|
|
21
|
+
await sql`DROP TABLE IF EXISTS sbtc_token_events`.execute(db);
|
|
22
|
+
}
|
|
23
|
+
if (await tableExists(db, "decoder_checkpoints")) {
|
|
24
|
+
await sql`DELETE FROM decoder_checkpoints WHERE decoder_name IN ('decode.sbtc_token.v1', 'backfill.sbtc_token')`.execute(
|
|
25
|
+
db,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The rows can be rebuilt from `decoded_events` or Streams if ever needed.
|
|
32
|
+
export async function down(_db: Kysely<unknown>): Promise<void> {}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
// Per-block coverage receipts were written on every decoder commit but never
|
|
5
|
+
// read, and nothing ever wrote `coverage_segments` or `stage_runs`.
|
|
6
|
+
// `stage_failures` and `stage_registry` stay: they are the durable fault
|
|
7
|
+
// record. Chain-plane only; every step is a no-op when its table is absent.
|
|
8
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
9
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
10
|
+
await onChainPlane(async () => {
|
|
11
|
+
// Dropping run_id also drops its foreign key to stage_runs.
|
|
12
|
+
await sql`ALTER TABLE IF EXISTS stage_failures DROP COLUMN IF EXISTS run_id`.execute(
|
|
13
|
+
db,
|
|
14
|
+
);
|
|
15
|
+
// Receipts reference stage_runs, so they go before it.
|
|
16
|
+
await sql`DROP TABLE IF EXISTS stage_block_receipts`.execute(db);
|
|
17
|
+
await sql`DROP TABLE IF EXISTS coverage_segments`.execute(db);
|
|
18
|
+
await sql`DROP TABLE IF EXISTS stage_runs`.execute(db);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// No-op: the receipts were never read. 0116_coverage_schema documents the
|
|
23
|
+
// original DDL.
|
|
24
|
+
export async function down(_db: Kysely<unknown>): Promise<void> {}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
// Decoder health now reads `decoder_checkpoints.last_output_at`, stamped in the
|
|
5
|
+
// commit transaction when a commit wrote rows. The composite index only served
|
|
6
|
+
// the old per-table "latest created_at" health query (see 0080), and cost a
|
|
7
|
+
// write on every decoded row. Chain-plane only; no-ops when a table is absent.
|
|
8
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
9
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
10
|
+
await onChainPlane(async () => {
|
|
11
|
+
// Nullable, no default: metadata-only change.
|
|
12
|
+
await sql`ALTER TABLE IF EXISTS decoder_checkpoints ADD COLUMN IF NOT EXISTS last_output_at TIMESTAMPTZ`.execute(
|
|
13
|
+
db,
|
|
14
|
+
);
|
|
15
|
+
await sql`DROP INDEX IF EXISTS decoded_events_evt_canon_created_idx`.execute(
|
|
16
|
+
db,
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
22
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
23
|
+
await onChainPlane(async () => {
|
|
24
|
+
await sql`ALTER TABLE IF EXISTS decoder_checkpoints DROP COLUMN IF EXISTS last_output_at`.execute(
|
|
25
|
+
db,
|
|
26
|
+
);
|
|
27
|
+
await sql`DO $$ BEGIN
|
|
28
|
+
IF to_regclass('public.decoded_events') IS NOT NULL THEN
|
|
29
|
+
CREATE INDEX IF NOT EXISTS decoded_events_evt_canon_created_idx ON decoded_events (event_type, canonical, created_at DESC);
|
|
30
|
+
END IF;
|
|
31
|
+
END $$`.execute(db);
|
|
32
|
+
});
|
|
33
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@secondlayer/shared",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "18.0.1",
|
|
4
4
|
"description": "Internal building blocks shared across Secondlayer packages.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -264,7 +264,7 @@
|
|
|
264
264
|
},
|
|
265
265
|
"files": ["dist", "migrations"],
|
|
266
266
|
"scripts": {
|
|
267
|
-
"build": "bunup",
|
|
267
|
+
"build": "rm -rf dist && bunup",
|
|
268
268
|
"dev": "bunup --watch",
|
|
269
269
|
"test": "bun test",
|
|
270
270
|
"typecheck": "bun check",
|