Working version Reviewed-on: #21
This commit was merged in pull request #21.
This commit is contained in:
2026-11-01 22:07:51 +00:00
parent 1edf2e6200
commit 636f5b9c77
80 changed files with 4065 additions and 549 deletions
+250 -100
View File
@@ -1,88 +1,191 @@
import { mkdir, open, rename, unlink } from 'node:fs/promises';
import { chmod, mkdir, readFile, stat } from 'node:fs/promises';
import { join } from 'node:path';
import { randomUUID } from 'node:crypto';
import { setTimeout as delay } from 'node:timers/promises';
import { DatabaseSync } from 'node:sqlite';
import { z } from 'zod';
import { NETWORK, CONSENSUS, type TestnetBlock, type NftTransfer, type NftState,
type MiningTemplate, type TestnetSnapshot } from '../../shared/testnet.ts';
import { CHAIN_ID, GENESIS_HASH, applyTransfer, blockSchema, makeTemplate,
transferId, transferSchema, validateBlock, workFor } from './consensus.ts';
import { NETWORK, CONSENSUS, type TestnetBlock,
type NftState, type NftStateView, type TestnetTransaction, type MiningTemplate,
type TestnetDeck, type TestnetSnapshot, type TestnetSummary, type CanonicalPage } from '../../shared/testnet.ts';
import { CHAIN_ID, GENESIS_HASH, applyTransactionOverlay, blockSchema, makeTemplate,
applyTransferOverlay, materializeState, transactionId, transactionSchema, transferSchema,
validateBlock, workFor } from './consensus.ts';
import type { PowEngine } from './randomx.ts';
import type { Article } from '../../shared/protocol.ts';
import type { MintStatsSnapshot } from '../../shared/enrichment.ts';
import { PROFILE } from '../../shared/network.ts';
import { RpcError } from '../resources.ts';
import { CanonicalIndex } from './canonical-index.ts';
const MAX_BLOCKS = 10_000;
const MAX_PENDING = 256;
const MAX_FILE = 128 * 1024 * 1024;
const MAX_DECK_PAGE = 24;
const MAX_LEGACY_FILE = 128 * 1024 * 1024;
const diskSchema = z.object({ version: z.literal(1), network: z.literal(NETWORK),
chainId: z.literal(CHAIN_ID), blocks: z.array(blockSchema).max(MAX_BLOCKS),
pendingTransfers: z.array(transferSchema).max(MAX_PENDING) }).strict();
type Node = { block: TestnetBlock; state: NftState; work: bigint };
chainId: z.literal(CHAIN_ID), blocks: z.array(blockSchema).max(10_000),
pendingTransfers: z.array(transactionSchema).max(MAX_PENDING) }).strict();
type Node = { block: TestnetBlock; state: NftStateView; work: bigint };
const DATABASE_SCHEMA_VERSION = '1';
/** Experimental, bounded DAG. The supplied engine is always used, including on restart.
* One process must own a data directory; there is no cross-process writer locking.
/** SQLite-backed DAG. Blocks append transactionally; pending rows are replaced transactionally.
* Each block state retains only its NFT delta, sharing the parent's state overlay.
*/
export class Ledger {
private readonly nodes = new Map<string, Node>();
private tip = GENESIS_HASH;
private pending: NftTransfer[] = [];
private pending: TestnetTransaction[] = [];
private pendingState: NftStateView = {};
private queue: Promise<unknown> = Promise.resolve();
private queued = 0;
/** Application-owned connectivity metadata; never part of consensus or persistence. */
peers = 0;
private readonly file: string;
private readonly db: DatabaseSync;
private readonly pow: PowEngine;
private constructor(file: string, pow: PowEngine) { this.file = file; this.pow = pow; }
private readonly index: CanonicalIndex;
private closed = false;
private revision = 0;
private reorgs = 0;
private readonly nowSeconds: () => number;
private canonicalCache: { tip: string; blocks: TestnetBlock[] } | undefined;
private constructor(file: string, db: DatabaseSync, pow: PowEngine, nowSeconds: () => number) {
this.file = file; this.db = db; this.pow = pow; this.nowSeconds = nowSeconds; this.index = new CanonicalIndex(db);
}
static async open(dir: string, pow: PowEngine): Promise<Ledger> {
const directory = join(dir, 'testnet');
static async open(dir: string, pow: PowEngine, nowSeconds: () => number = () => Math.floor(Date.now() / 1000)): Promise<Ledger> {
const directory = join(dir, PROFILE);
await mkdir(directory, { recursive: true, mode: 0o700 });
const ledger = new Ledger(join(directory, 'ledger.json'), pow);
let handle;
try { handle = await open(ledger.file, 'r'); }
catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
await ledger.persist([], []);
return ledger;
}
let text: string;
const file = join(directory, 'ledger.sqlite');
let databaseExists = false;
try { databaseExists = (await stat(file)).size > 0; }
catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }
const db = new DatabaseSync(file);
try {
if ((await handle.stat()).size > MAX_FILE) throw new Error('Testnet ledger file exceeds 128 MiB');
// Bounded reads also protect against a file growing after stat().
const chunks: Buffer[] = [];
let size = 0;
for (;;) {
const buffer = Buffer.alloc(Math.min(64 * 1024, MAX_FILE + 1 - size));
const { bytesRead } = await handle.read(buffer, 0, buffer.length, null);
if (!bytesRead) break;
size += bytesRead;
if (size > MAX_FILE) throw new Error('Testnet ledger file exceeds 128 MiB');
chunks.push(buffer.subarray(0, bytesRead));
const tableExists = (name: string) => Boolean(db.prepare(
"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(name));
if (databaseExists && !tableExists('metadata')) {
throw new Error('SQLite ledger metadata missing; refusing to initialize over unknown data');
}
text = Buffer.concat(chunks).toString('utf8');
} finally { await handle.close(); }
const disk = diskSchema.parse(JSON.parse(text)); // Mismatches/corruption fail closed, never reset.
for (const raw of disk.blocks) {
if (ledger.nodes.has(raw.hash)) throw new Error('Duplicate persisted block');
const node = await ledger.validate(raw);
ledger.nodes.set(node.block.hash, node);
if (node.work > ledger.tipWork()) ledger.tip = node.block.hash;
const readMeta = databaseExists ? db.prepare('SELECT value FROM metadata WHERE key = ?') : undefined;
if (!databaseExists) {
db.exec('BEGIN IMMEDIATE');
try {
db.exec(`CREATE TABLE metadata (key TEXT PRIMARY KEY, value TEXT NOT NULL) WITHOUT ROWID;
CREATE TABLE blocks (
sequence INTEGER PRIMARY KEY AUTOINCREMENT,
hash TEXT NOT NULL UNIQUE,
previous_hash TEXT NOT NULL,
block_json TEXT NOT NULL
);
CREATE INDEX blocks_by_parent ON blocks(previous_hash);
CREATE TABLE pending_transfers (
position INTEGER PRIMARY KEY,
id TEXT NOT NULL UNIQUE,
transaction_json TEXT NOT NULL
);`);
const insertMeta = db.prepare('INSERT INTO metadata(key, value) VALUES(?, ?)');
insertMeta.run('schema_version', DATABASE_SCHEMA_VERSION);
insertMeta.run('network', NETWORK);
insertMeta.run('chain_id', CHAIN_ID);
db.exec('COMMIT');
} catch (error) { db.exec('ROLLBACK'); throw error; }
} else {
if (!tableExists('blocks') || !tableExists('pending_transfers')) {
throw new Error('SQLite ledger schema is incomplete; refusing to repair stored data automatically');
}
const schemaVersion = readMeta!.get('schema_version') as { value: string } | undefined;
const storedNetwork = readMeta!.get('network') as { value: string } | undefined;
const storedChain = readMeta!.get('chain_id') as { value: string } | undefined;
if (schemaVersion?.value !== DATABASE_SCHEMA_VERSION || storedNetwork?.value !== NETWORK || storedChain?.value !== CHAIN_ID) {
throw new Error('SQLite ledger schema or chain mismatch; refusing to reset stored data');
}
}
if (process.platform !== 'win32') await chmod(file, 0o600);
db.exec('PRAGMA journal_mode = WAL; PRAGMA synchronous = FULL; PRAGMA foreign_keys = ON; PRAGMA busy_timeout = 5000;');
const migrated = db.prepare('SELECT value FROM metadata WHERE key = ?').get('legacy_json_migrated') as { value: string } | undefined;
if (!migrated) {
const legacyFile = join(directory, 'ledger.json');
let legacy: unknown;
try {
const info = await stat(legacyFile);
if (info.size > MAX_LEGACY_FILE) throw new Error('Legacy testnet ledger exceeds 128 MiB; preserve it and migrate offline');
legacy = JSON.parse(await readFile(legacyFile, 'utf8'));
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
}
const insertMeta = db.prepare('INSERT OR REPLACE INTO metadata(key, value) VALUES(?, ?)');
db.exec('BEGIN IMMEDIATE');
try {
if (legacy !== undefined) {
const oldDisk = diskSchema.parse(legacy);
const insertBlock = db.prepare('INSERT INTO blocks(hash, previous_hash, block_json) VALUES(?, ?, ?)');
for (const block of oldDisk.blocks) insertBlock.run(block.hash, block.previousHash, JSON.stringify(block));
const insertPending = db.prepare('INSERT INTO pending_transfers(position, id, transaction_json) VALUES(?, ?, ?)');
oldDisk.pendingTransfers.forEach((tx, index) => insertPending.run(index, transactionId(tx), JSON.stringify(tx)));
insertMeta.run('legacy_json_imported_at', String(nowSeconds()));
insertMeta.run('legacy_json_block_count', String(oldDisk.blocks.length));
}
insertMeta.run('legacy_json_migrated', '1');
db.exec('COMMIT');
} catch (error) { db.exec('ROLLBACK'); throw error; }
}
const ledger = new Ledger(file, db, pow, nowSeconds);
const importedAtText = (db.prepare('SELECT value FROM metadata WHERE key=?').get('legacy_json_imported_at') as { value: string } | undefined)?.value;
const importedCountText = (db.prepare('SELECT value FROM metadata WHERE key=?').get('legacy_json_block_count') as { value: string } | undefined)?.value;
const legacyImportedAt = importedAtText !== undefined && /^(0|[1-9]\d{0,10})$/.test(importedAtText)
? Number(importedAtText) : undefined;
const legacyImportedCount = importedCountText !== undefined && /^(0|[1-9]\d{0,9})$/.test(importedCountText)
? Number(importedCountText) : undefined;
if ((importedAtText !== undefined && (legacyImportedAt === undefined || !Number.isSafeInteger(legacyImportedAt))) ||
(importedCountText !== undefined && (legacyImportedCount === undefined || !Number.isSafeInteger(legacyImportedCount))) ||
(legacyImportedAt === undefined) !== (legacyImportedCount === undefined)) {
throw new Error('Legacy ledger import timestamp metadata is invalid');
}
for (const row of db.prepare('SELECT sequence, block_json FROM blocks ORDER BY sequence').iterate() as Iterable<{ sequence: number; block_json: string }>) {
const raw = blockSchema.parse(JSON.parse(row.block_json));
if (ledger.nodes.has(raw.hash)) throw new Error('Duplicate persisted block');
const importedLegacyBlock = legacyImportedCount !== undefined && row.sequence <= legacyImportedCount;
// Historical SQLite blocks already passed admission; wall-clock rollback must not
// strand them. Imported JSON blocks retain their original admission clock so an
// untrusted legacy file cannot inject arbitrarily future-dated history.
const node = importedLegacyBlock
? await ledger.validate(raw, false, legacyImportedAt!)
: await ledger.validate(raw, true);
ledger.nodes.set(node.block.hash, node);
if (node.work > ledger.tipWork()) ledger.tip = node.block.hash;
}
let state = ledger.tipState();
const seen = new Set(ledger.canonical().flatMap(b => b.transactions.map(transactionId)));
for (const row of db.prepare('SELECT id, transaction_json FROM pending_transfers ORDER BY position').iterate() as Iterable<{ id: string; transaction_json: string }>) {
const tx = transactionSchema.parse(JSON.parse(row.transaction_json));
const id = transactionId(tx);
if (row.id !== id || seen.has(id)) throw new Error('Duplicate or mismatched persisted transfer');
state = applyTransactionOverlay(state, tx, ledger.height() + 1);
seen.add(id);
ledger.pending.push(tx);
}
ledger.pendingState = state;
ledger.index.initialize(ledger.canonical(), materializeState(ledger.tipState()));
return ledger;
} catch (error) {
db.close();
throw error;
}
const state = structuredClone(ledger.tipState());
const seen = new Set(ledger.canonical().flatMap(b => b.transactions.map(transferId)));
for (const tx of disk.pendingTransfers) {
const id = transferId(tx);
if (seen.has(id)) throw new Error('Duplicate persisted transfer');
applyTransfer(state, tx, ledger.height() + 1);
seen.add(id);
}
ledger.pending = disk.pendingTransfers;
return ledger;
}
get size(): number { return this.nodes.size; }
get allBlocks(): TestnetBlock[] { return this.inventory(0, MAX_BLOCKS); }
diagnostics() { const createdAt=this.nodes.get(this.tip)?.block.createdAt??null;
return { height:this.height(), dagBlocks:this.size, pending:this.pending.length, mutationQueue:this.queued,
reorgsSinceStartup:this.reorgs,tipCreatedAt:createdAt,tipAgeSeconds:createdAt===null?null:Math.max(0,Date.now()/1000-createdAt) }; }
get allBlocks(): TestnetBlock[] {
const rows = this.db.prepare('SELECT block_json FROM blocks ORDER BY sequence').all() as { block_json: string }[];
return rows.map(row => blockSchema.parse(JSON.parse(row.block_json)));
}
close(): void {
if (this.closed) return;
this.closed = true;
this.db.close();
}
block(hash: string): TestnetBlock | undefined {
const block = this.nodes.get(hash)?.block;
return block ? structuredClone(block) : undefined;
@@ -91,10 +194,12 @@ export class Ledger {
* HTTP callers should request limit=32 and return next=offset+length or null.
*/
inventory(offset: number, limit: number): TestnetBlock[] {
if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(limit) || limit < 1 || limit > MAX_BLOCKS) {
if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(limit) || limit < 1 || limit > 1000) {
throw new Error('Invalid inventory range');
}
return structuredClone(Array.from(this.nodes.values()).slice(offset, offset + limit).map(n => n.block));
const rows = this.db.prepare('SELECT block_json FROM blocks ORDER BY sequence LIMIT ? OFFSET ?')
.all(limit, offset) as { block_json: string }[];
return rows.map(row => blockSchema.parse(JSON.parse(row.block_json)));
}
async submit(raw: unknown): Promise<{ accepted: boolean; reorg: boolean }> {
@@ -102,37 +207,47 @@ export class Ledger {
return this.serialize(async () => {
const parsed = blockSchema.parse(input);
if (this.nodes.has(parsed.hash)) return { accepted: false, reorg: false };
if (this.size >= MAX_BLOCKS) throw new Error('Experimental testnet limit: 10000 stored blocks');
const node = await this.validate(parsed);
const wins = node.work > this.tipWork();
const oldChain = this.canonical();
const chain = wins ? [...this.branch(node.block.previousHash), node.block] : oldChain;
const hashes = new Set(chain.map(b => b.hash));
const disconnected = wins ? oldChain.filter(b => !hashes.has(b.hash)) : [];
const pending = wins ? this.filterPending(node.state, chain,
[...disconnected.flatMap(b => b.transactions), ...this.pending]) : this.pending;
const filtered = wins ? this.filterPending(node.state, chain,
[...disconnected.flatMap(b => b.transactions), ...this.pending]) : undefined;
const pending = filtered?.pending ?? this.pending;
// Nothing visible changes until the atomic replacement succeeds.
await this.persist([...Array.from(this.nodes.values(), n => n.block), node.block], pending);
const oldHashes = new Set(oldChain.map(b => b.hash));
const connected = wins ? chain.filter(b => !oldHashes.has(b.hash)) : [];
this.persist(node.block, pending, disconnected, connected);
this.nodes.set(node.block.hash, node);
if (wins) this.tip = node.block.hash;
if (disconnected.length) this.reorgs++;
this.pending = pending;
if (filtered) this.pendingState = filtered.state;
this.revision++;
return { accepted: true, reorg: disconnected.length > 0 };
});
}
async submitTransfer(raw: unknown): Promise<string> {
const tx = transferSchema.parse(structuredClone(raw));
return this.submitTransaction(tx);
}
async submitTransaction(raw: unknown): Promise<string> {
const input = structuredClone(raw);
return this.serialize(async () => {
const tx = transferSchema.parse(input);
const id = transferId(tx);
if (this.pending.some(t => transferId(t) === id)) return id;
const tx = transactionSchema.parse(input);
const id = transactionId(tx);
if (this.pending.some(t => transactionId(t) === id)) return id;
if (this.pending.length >= MAX_PENDING) throw new Error('Testnet mempool limit: 256 transfers');
const state = structuredClone(this.tipState());
for (const pending of this.pending) applyTransfer(state, pending, this.height() + 1);
applyTransfer(state, tx, this.height() + 1); // No speculative issuance.
const nextPendingState = applyTransactionOverlay(this.pendingState, tx, this.height() + 1);
const pending = [...this.pending, tx];
await this.persist(Array.from(this.nodes.values(), n => n.block), pending);
this.persist(undefined, pending);
this.pending = pending;
this.pendingState = nextPendingState;
this.revision++;
return id;
});
}
@@ -150,9 +265,43 @@ export class Ledger {
peers: this.peers, algorithm: 'RandomX',
maturity: CONSENSUS.coinbaseMaturity });
}
/** Small metadata/mempool response. No full-chain traversal or clone. */
summary(): TestnetSummary {
return { network: NETWORK, chainId: CHAIN_ID, genesis: GENESIS_HASH, tip: this.tip,
height: this.height(), revision: this.revision, cumulativeWork: this.tipWork().toString(),
pendingTransfers: structuredClone(this.pending), peers: this.peers, algorithm: 'RandomX', maturity: CONSENSUS.coinbaseMaturity };
}
peerStatus() { return { network: NETWORK, chainId: CHAIN_ID, size: this.size, tip: this.tip }; }
pendingTransactions(): TestnetTransaction[] { return structuredClone(this.pending); }
canonicalEvents(after: number, limit = 64) { return this.index.events(after, limit); }
canonicalHead(): number { return this.index.head(); }
nft(token: string) { return this.index.nft(token, this.height()); }
canonicalTransaction(id: string) { return this.index.transaction(id); }
legacySnapshot(maxBlocks: number): TestnetSnapshot {
// Reject before any full-chain copy/serialization. Never truncate ownership history silently.
if (this.height() > maxBlocks) throw new RpcError(413, 'Full snapshot disabled at this height; use /api/summary and /api/canonical pages.');
return this.snapshot();
}
canonicalPage(tip: string, offset: number, limit = 16): CanonicalPage {
if (tip !== this.tip) throw new RpcError(409, 'Canonical tip changed; restart paged loading.');
if (!Number.isSafeInteger(offset) || offset < 0 || offset > this.height() ||
!Number.isSafeInteger(limit) || limit < 1 || limit > 16) throw new RpcError(400, 'Invalid canonical page');
const blocks = structuredClone(this.canonical().slice(offset, offset + limit));
return { network: NETWORK, chainId: CHAIN_ID, tip: this.tip, height: this.height(), offset, blocks,
next: offset + blocks.length < this.height() ? offset + blocks.length : null };
}
deck(address: string, offset = 0, limit = MAX_DECK_PAGE): TestnetDeck {
if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(limit) || limit < 1 || limit > MAX_DECK_PAGE) {
throw new Error('Invalid deck page');
}
const { total, nfts } = this.index.deck(address, offset, limit, this.height());
const next = offset + nfts.length < total ? offset + nfts.length : null;
return structuredClone({ network: NETWORK, chainId: CHAIN_ID, address, tip: this.tip,
offset, limit, total, next, nfts });
}
private height(): number { return this.nodes.get(this.tip)?.block.height ?? 0; }
private tipState(): NftState { return this.nodes.get(this.tip)?.state ?? {}; }
private tipState(): NftStateView { return this.nodes.get(this.tip)?.state ?? {}; }
private tipWork(): bigint { return this.nodes.get(this.tip)?.work ?? 0n; }
private branch(hash: string): TestnetBlock[] {
const result: TestnetBlock[] = [];
@@ -164,26 +313,31 @@ export class Ledger {
}
return result.reverse();
}
private canonical(): TestnetBlock[] { return this.branch(this.tip); }
private async validate(raw: TestnetBlock): Promise<Node> {
private canonical(): TestnetBlock[] {
if (this.canonicalCache?.tip !== this.tip) this.canonicalCache = { tip: this.tip, blocks: this.branch(this.tip) };
return this.canonicalCache.blocks; // Internal immutable block references only; public APIs clone their slice.
}
private async validate(raw: TestnetBlock, historicalReplay = false, now = this.nowSeconds()): Promise<Node> {
const ancestors = this.branch(raw.previousHash);
const parent = this.nodes.get(raw.previousHash);
const { block, state } = await validateBlock(raw, ancestors, parent?.state ?? {}, this.pow);
const { block, state } = await validateBlock(raw, ancestors, parent?.state ?? {}, this.pow,
now, historicalReplay);
return { block, state, work: (parent?.work ?? 0n) + workFor(block.target) };
}
private filterPending(parent: NftState, chain: TestnetBlock[], candidates: NftTransfer[]): NftTransfer[] {
const state = structuredClone(parent);
const seen = new Set(chain.flatMap(b => b.transactions.map(transferId)));
const pending: NftTransfer[] = [];
private filterPending(parent: NftStateView, chain: TestnetBlock[], candidates: TestnetTransaction[]):
{ pending: TestnetTransaction[]; state: NftStateView } {
let state = parent;
const seen = new Set(chain.flatMap(b => b.transactions.map(transactionId)));
const pending: TestnetTransaction[] = [];
for (const tx of candidates) {
if (pending.length === MAX_PENDING) break;
const id = transferId(tx);
const id = transactionId(tx);
if (seen.has(id)) continue;
try { applyTransfer(state, tx, chain.length + 1); }
try { state = applyTransactionOverlay(state, tx, chain.length + 1); }
catch { continue; } // Disconnected issuance, immature coins and conflicts are not replayable.
seen.add(id); pending.push(tx);
}
return pending;
return { pending, state };
}
private serialize<T>(operation: () => Promise<T>): Promise<T> {
if (this.queued >= 256) return Promise.reject(new Error('Testnet mutation queue full'));
@@ -192,25 +346,21 @@ export class Ledger {
this.queue = result.catch(() => undefined).finally(() => { this.queued--; });
return result;
}
private async persist(blocks: TestnetBlock[], pendingTransfers: NftTransfer[]): Promise<void> {
const data = JSON.stringify({ version: 1, network: NETWORK, chainId: CHAIN_ID, blocks, pendingTransfers });
if (Buffer.byteLength(data) > MAX_FILE) throw new Error('Testnet ledger file exceeds 128 MiB');
const temp = `${this.file}.${randomUUID()}.tmp`;
private persist(block: TestnetBlock | undefined, pendingTransfers: TestnetTransaction[],
disconnected: TestnetBlock[] = [], connected: TestnetBlock[] = []): void {
this.db.exec('BEGIN IMMEDIATE');
try {
const handle = await open(temp, 'wx', 0o600);
try { await handle.writeFile(data); await handle.sync(); }
finally { await handle.close(); }
// Windows scanners can briefly hold the destination open without delete sharing.
// Retry only transient sharing failures; never unlink the old durable ledger.
for (let attempt = 0; ; attempt++) {
try { await rename(temp, this.file); break; }
catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (process.platform !== 'win32' || attempt >= 5 ||
!['EPERM', 'EACCES', 'EBUSY'].includes(code ?? '')) throw error;
await delay(10 * 2 ** attempt);
}
}
} finally { await unlink(temp).catch(() => undefined); }
if (block) this.db.prepare('INSERT INTO blocks(hash, previous_hash, block_json) VALUES(?, ?, ?)')
.run(block.hash, block.previousHash, JSON.stringify(block));
this.db.exec('DELETE FROM pending_transfers');
const insertPending = this.db.prepare('INSERT INTO pending_transfers(position, id, transaction_json) VALUES(?, ?, ?)');
pendingTransfers.forEach((tx, index) => insertPending.run(index, transactionId(tx), JSON.stringify(tx)));
for (const old of [...disconnected].reverse()) this.index.disconnect(old);
for (const next of connected) this.index.connect(next);
this.db.exec('COMMIT');
} catch (error) {
this.db.exec('ROLLBACK');
throw error;
}
}
}