@totemsdk/omnia-host 0.10.0 → 0.12.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 { SyncSqliteStore } from '@totemsdk/storage/sqlite-sync';
2
+ /**
3
+ * Durable Map facade for host state (factories, virtual channels, splices,
4
+ * VTXOs, pools). Backed by `@totemsdk/storage/sqlite-sync` (same `totemsdk_kv`
5
+ * table + versioned codec as the channel/operation stores).
6
+ *
7
+ * Values are plain objects/arrays/bigints/Uint8Arrays. Runtime-only handles
8
+ * (`localSigner`, signer/lease/swarm objects) are stripped; Map/Set/Date are
9
+ * explicitly tagged so they round-trip. A function anywhere else is dropped
10
+ * rather than persisted (never serialise authority material by accident).
11
+ */
12
+ export declare const HOST_STATE_FORMAT_KEY = "omnia:store:host-state:format";
13
+ export declare const HOST_STATE_FORMAT = 1;
14
+ export declare class PersistentMap<V> extends Map<string, V> {
15
+ private readonly kv;
16
+ private readonly namespace;
17
+ constructor(kv: SyncSqliteStore, namespace: string);
18
+ private key;
19
+ private keysInNamespace;
20
+ get(id: string): V | undefined;
21
+ set(id: string, value: V): this;
22
+ has(id: string): boolean;
23
+ delete(id: string): boolean;
24
+ clear(): void;
25
+ get size(): number;
26
+ keys(): MapIterator<string>;
27
+ values(): MapIterator<V>;
28
+ entries(): MapIterator<[string, V]>;
29
+ [Symbol.iterator](): MapIterator<[string, V]>;
30
+ forEach(callbackfn: (value: V, key: string, map: Map<string, V>) => void): void;
31
+ }
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PersistentMap = exports.HOST_STATE_FORMAT = exports.HOST_STATE_FORMAT_KEY = void 0;
4
+ /**
5
+ * Durable Map facade for host state (factories, virtual channels, splices,
6
+ * VTXOs, pools). Backed by `@totemsdk/storage/sqlite-sync` (same `totemsdk_kv`
7
+ * table + versioned codec as the channel/operation stores).
8
+ *
9
+ * Values are plain objects/arrays/bigints/Uint8Arrays. Runtime-only handles
10
+ * (`localSigner`, signer/lease/swarm objects) are stripped; Map/Set/Date are
11
+ * explicitly tagged so they round-trip. A function anywhere else is dropped
12
+ * rather than persisted (never serialise authority material by accident).
13
+ */
14
+ exports.HOST_STATE_FORMAT_KEY = 'omnia:store:host-state:format';
15
+ exports.HOST_STATE_FORMAT = 1;
16
+ const EPHEMERAL_KEYS = new Set([
17
+ 'localSigner',
18
+ 'signer',
19
+ 'leaseProvider',
20
+ 'signerPort',
21
+ 'swarm',
22
+ 'routing',
23
+ ]);
24
+ function encode(value, seen = new WeakSet()) {
25
+ if (value === null || value === undefined)
26
+ return null;
27
+ if (typeof value === 'bigint')
28
+ return value;
29
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
30
+ return value;
31
+ }
32
+ if (typeof value === 'function')
33
+ return null;
34
+ if (value instanceof Uint8Array)
35
+ return value;
36
+ if (value instanceof Date)
37
+ return { $date: value.toISOString() };
38
+ if (Array.isArray(value)) {
39
+ return value.map((item) => encode(item, seen));
40
+ }
41
+ if (value instanceof Map) {
42
+ const entries = [];
43
+ for (const [key, item] of value.entries())
44
+ entries.push([String(key), encode(item, seen)]);
45
+ return { $map: entries };
46
+ }
47
+ if (value instanceof Set) {
48
+ return { $set: Array.from(value.values(), (item) => encode(item, seen)) };
49
+ }
50
+ if (typeof value === 'object') {
51
+ if (seen.has(value))
52
+ throw new Error('cannot persist cyclic host state');
53
+ seen.add(value);
54
+ const out = {};
55
+ for (const [key, item] of Object.entries(value)) {
56
+ if (EPHEMERAL_KEYS.has(key) || item === undefined || typeof item === 'function')
57
+ continue;
58
+ out[key] = encode(item, seen);
59
+ }
60
+ seen.delete(value);
61
+ return out;
62
+ }
63
+ return null;
64
+ }
65
+ function decode(value) {
66
+ if (value === null)
67
+ return null;
68
+ if (typeof value === 'bigint' ||
69
+ typeof value === 'string' ||
70
+ typeof value === 'number' ||
71
+ typeof value === 'boolean') {
72
+ return value;
73
+ }
74
+ if (value instanceof Uint8Array)
75
+ return value;
76
+ if (Array.isArray(value))
77
+ return value.map(decode);
78
+ if (typeof value === 'object') {
79
+ const record = value;
80
+ if (Array.isArray(record['$map'])) {
81
+ return new Map(record['$map'].map(([key, item]) => [key, decode(item)]));
82
+ }
83
+ if (Array.isArray(record['$set'])) {
84
+ return new Set(record['$set'].map(decode));
85
+ }
86
+ if (typeof record['$date'] === 'string')
87
+ return new Date(record['$date']);
88
+ const out = {};
89
+ for (const [key, item] of Object.entries(record))
90
+ out[key] = decode(item);
91
+ return out;
92
+ }
93
+ return value;
94
+ }
95
+ function iterator(values) {
96
+ let index = 0;
97
+ return {
98
+ next() {
99
+ if (index >= values.length)
100
+ return { done: true, value: undefined };
101
+ return { done: false, value: values[index++] };
102
+ },
103
+ [Symbol.iterator]() {
104
+ return this;
105
+ },
106
+ [Symbol.dispose]() {
107
+ index = values.length;
108
+ },
109
+ };
110
+ }
111
+ class PersistentMap extends Map {
112
+ kv;
113
+ namespace;
114
+ constructor(kv, namespace) {
115
+ super();
116
+ this.kv = kv;
117
+ this.namespace = namespace;
118
+ const format = kv.get(exports.HOST_STATE_FORMAT_KEY);
119
+ if (format !== null && format !== exports.HOST_STATE_FORMAT) {
120
+ throw new Error(`Unsupported omnia-host state format ${format} (this build supports ${exports.HOST_STATE_FORMAT}) — refusing to open`);
121
+ }
122
+ if (format === null)
123
+ kv.set(exports.HOST_STATE_FORMAT_KEY, exports.HOST_STATE_FORMAT);
124
+ }
125
+ key(id) {
126
+ return this.namespace + id;
127
+ }
128
+ keysInNamespace() {
129
+ return this.kv.keys().filter((key) => key.startsWith(this.namespace));
130
+ }
131
+ get(id) {
132
+ const raw = this.kv.get(this.key(id));
133
+ return raw === null ? undefined : decode(raw);
134
+ }
135
+ set(id, value) {
136
+ this.kv.set(this.key(id), encode(value));
137
+ return this;
138
+ }
139
+ has(id) {
140
+ return this.kv.has(this.key(id));
141
+ }
142
+ delete(id) {
143
+ return this.kv.remove(this.key(id));
144
+ }
145
+ clear() {
146
+ for (const key of this.keysInNamespace())
147
+ this.kv.remove(key);
148
+ }
149
+ get size() {
150
+ return this.keysInNamespace().length;
151
+ }
152
+ keys() {
153
+ return iterator(this.keysInNamespace().map((key) => key.slice(this.namespace.length)));
154
+ }
155
+ values() {
156
+ return iterator(Array.from(this.entries(), ([, value]) => value));
157
+ }
158
+ entries() {
159
+ return iterator(this.keysInNamespace().map((key) => [
160
+ key.slice(this.namespace.length),
161
+ decode(this.kv.get(key)),
162
+ ]));
163
+ }
164
+ [Symbol.iterator]() {
165
+ return this.entries();
166
+ }
167
+ forEach(callbackfn) {
168
+ for (const [key, value] of this.entries())
169
+ callbackfn(value, key, this);
170
+ }
171
+ }
172
+ exports.PersistentMap = PersistentMap;
@@ -1,10 +1,11 @@
1
1
  import { type OmniaChannel, type ChannelSigner } from '@totemsdk/omnia';
2
- /** SQLite-backed Map facade accepted by @totemsdk/omnia's ChannelStore type. */
2
+ /**
3
+ * Synchronous Map facade accepted by @totemsdk/omnia's ChannelStore type,
4
+ * persisted through `@totemsdk/storage` (`SyncSqliteStore`, same codec/envelope
5
+ * as `SqliteStore`). Legacy `omnia_channels` tables are migrated on open.
6
+ */
3
7
  export declare class SqliteChannelStore extends Map<string, OmniaChannel> {
4
- private readonly db;
5
- private readonly readStatement;
6
- private readonly writeStatement;
7
- private readonly deleteStatement;
8
+ private readonly kv;
8
9
  /**
9
10
  * CAN-002: a recovered channel loses `localSigner` (it is not serializable),
10
11
  * so the router's `counterpartPkd()` cannot tell which party is local and
@@ -15,6 +16,8 @@ export declare class SqliteChannelStore extends Map<string, OmniaChannel> {
15
16
  /** Attach the host's signer so recovered channels carry it (CAN-002). */
16
17
  setLocalSigner(signer?: ChannelSigner): void;
17
18
  private rebind;
19
+ private channelKeys;
20
+ private idOf;
18
21
  get(channelId: string): OmniaChannel | undefined;
19
22
  set(channelId: string, channel: OmniaChannel): this;
20
23
  has(channelId: string): boolean;
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.SqliteChannelStore = void 0;
7
- const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
4
+ const sqlite_sync_1 = require("@totemsdk/storage/sqlite-sync");
8
5
  const omnia_1 = require("@totemsdk/omnia");
6
+ const PREFIX = 'omnia:channel:';
7
+ const FORMAT_KEY = 'omnia:store:channel:format';
8
+ const STORE_FORMAT = 1;
9
9
  function iterator(values) {
10
10
  let index = 0;
11
11
  return {
@@ -22,12 +22,13 @@ function iterator(values) {
22
22
  },
23
23
  };
24
24
  }
25
- /** SQLite-backed Map facade accepted by @totemsdk/omnia's ChannelStore type. */
25
+ /**
26
+ * Synchronous Map facade accepted by @totemsdk/omnia's ChannelStore type,
27
+ * persisted through `@totemsdk/storage` (`SyncSqliteStore`, same codec/envelope
28
+ * as `SqliteStore`). Legacy `omnia_channels` tables are migrated on open.
29
+ */
26
30
  class SqliteChannelStore extends Map {
27
- db;
28
- readStatement;
29
- writeStatement;
30
- deleteStatement;
31
+ kv;
31
32
  /**
32
33
  * CAN-002: a recovered channel loses `localSigner` (it is not serializable),
33
34
  * so the router's `counterpartPkd()` cannot tell which party is local and
@@ -36,24 +37,20 @@ class SqliteChannelStore extends Map {
36
37
  localSigner;
37
38
  constructor(dbPath) {
38
39
  super();
39
- this.db = new better_sqlite3_1.default(dbPath);
40
- this.db.pragma('journal_mode = WAL');
41
- this.db.exec(`
42
- CREATE TABLE IF NOT EXISTS omnia_channels (
43
- channel_id TEXT PRIMARY KEY,
44
- channel_json TEXT NOT NULL,
45
- updated_at INTEGER NOT NULL
46
- )
47
- `);
48
- this.readStatement = this.db.prepare('SELECT channel_json FROM omnia_channels WHERE channel_id = ?');
49
- this.writeStatement = this.db.prepare(`
50
- INSERT INTO omnia_channels (channel_id, channel_json, updated_at)
51
- VALUES (?, ?, ?)
52
- ON CONFLICT(channel_id) DO UPDATE SET
53
- channel_json = excluded.channel_json,
54
- updated_at = excluded.updated_at
55
- `);
56
- this.deleteStatement = this.db.prepare('DELETE FROM omnia_channels WHERE channel_id = ?');
40
+ this.kv = new sqlite_sync_1.SyncSqliteStore(dbPath);
41
+ const version = this.kv.get(FORMAT_KEY);
42
+ if (version !== null && version !== STORE_FORMAT) {
43
+ this.kv.close();
44
+ throw new Error(`Unsupported SqliteChannelStore format ${version} (this build supports ${STORE_FORMAT}) — refusing to open`);
45
+ }
46
+ if (version === null) {
47
+ // One-time migration from the pre-storage direct-SQLite table.
48
+ this.kv.migrateLegacyTable('omnia_channels', (row) => ({
49
+ key: PREFIX + String(row['channel_id']),
50
+ value: String(row['channel_json']),
51
+ }));
52
+ this.kv.set(FORMAT_KEY, STORE_FORMAT);
53
+ }
57
54
  }
58
55
  /** Attach the host's signer so recovered channels carry it (CAN-002). */
59
56
  setLocalSigner(signer) {
@@ -62,36 +59,42 @@ class SqliteChannelStore extends Map {
62
59
  rebind(channel) {
63
60
  return this.localSigner ? { ...channel, localSigner: this.localSigner } : channel;
64
61
  }
62
+ channelKeys() {
63
+ return this.kv.keys().filter((key) => key.startsWith(PREFIX));
64
+ }
65
+ idOf(key) {
66
+ return key.slice(PREFIX.length);
67
+ }
65
68
  get(channelId) {
66
- const row = this.readStatement.get(channelId);
67
- return row ? this.rebind((0, omnia_1.recoverChannel)(row.channel_json)) : undefined;
69
+ const snapshot = this.kv.get(PREFIX + channelId);
70
+ return snapshot ? this.rebind((0, omnia_1.recoverChannel)(snapshot)) : undefined;
68
71
  }
69
72
  set(channelId, channel) {
70
- this.writeStatement.run(channelId, (0, omnia_1.serializeChannelSnapshot)(channel), Date.now());
73
+ this.kv.set(PREFIX + channelId, (0, omnia_1.serializeChannelSnapshot)(channel));
71
74
  return this;
72
75
  }
73
76
  has(channelId) {
74
- return this.readStatement.get(channelId) !== undefined;
77
+ return this.kv.has(PREFIX + channelId);
75
78
  }
76
79
  delete(channelId) {
77
- return this.deleteStatement.run(channelId).changes > 0;
80
+ return this.kv.remove(PREFIX + channelId);
78
81
  }
79
82
  clear() {
80
- this.db.exec('DELETE FROM omnia_channels');
83
+ for (const key of this.channelKeys())
84
+ this.kv.remove(key);
81
85
  }
82
86
  get size() {
83
- const row = this.db.prepare('SELECT COUNT(*) AS count FROM omnia_channels').get();
84
- return row.count;
87
+ return this.channelKeys().length;
85
88
  }
86
89
  entries() {
87
- const rows = this.db.prepare('SELECT channel_id, channel_json FROM omnia_channels ORDER BY channel_id').iterate();
88
- return iterator(Array.from(rows, (row) => [
89
- row.channel_id,
90
- this.rebind((0, omnia_1.recoverChannel)(row.channel_json)),
91
- ]));
90
+ const rows = this.channelKeys().map((key) => {
91
+ const snapshot = this.kv.get(key);
92
+ return [this.idOf(key), this.rebind((0, omnia_1.recoverChannel)(snapshot))];
93
+ });
94
+ return iterator(rows);
92
95
  }
93
96
  keys() {
94
- return iterator(Array.from(this.entries(), ([channelId]) => channelId));
97
+ return iterator(this.channelKeys().map((key) => this.idOf(key)));
95
98
  }
96
99
  values() {
97
100
  return iterator(Array.from(this.entries(), ([, channel]) => channel));
@@ -104,7 +107,7 @@ class SqliteChannelStore extends Map {
104
107
  callbackfn(value, key, this);
105
108
  }
106
109
  close() {
107
- this.db.close();
110
+ this.kv.close();
108
111
  }
109
112
  }
110
113
  exports.SqliteChannelStore = SqliteChannelStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@totemsdk/omnia-host",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "Durable Omnia node daemon for channel lifecycle, routing, and control APIs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -17,27 +17,29 @@
17
17
  "files": [
18
18
  "dist",
19
19
  "README.md",
20
- "LICENSE"
20
+ "LICENSE",
21
+ "NOTICE"
21
22
  ],
22
23
  "dependencies": {
23
- "better-sqlite3": "^12.2.0",
24
24
  "ws": "^8.21.1",
25
25
  "@totemsdk/chain-provider": "^0.3.0",
26
26
  "@totemsdk/core": "^1.4.0",
27
27
  "@totemsdk/identity": "^1.0.2",
28
- "@totemsdk/manifest": "^2.0.1",
29
- "@totemsdk/omnia": "^2.3.0",
28
+ "@totemsdk/liquidity-bond": "^0.3.1",
29
+ "@totemsdk/manifest": "^2.0.2",
30
+ "@totemsdk/minima-rpc": "^0.4.0",
31
+ "@totemsdk/omnia": "^2.3.1",
30
32
  "@totemsdk/omnia-factory": "^0.7.0",
31
33
  "@totemsdk/omnia-router": "^0.4.0",
32
34
  "@totemsdk/omnia-splice": "^0.3.0",
33
- "@totemsdk/storage": "^0.1.0",
35
+ "@totemsdk/omnia-pool": "^0.5.3",
36
+ "@totemsdk/p2p-hub": "^0.1.0",
37
+ "@totemsdk/storage": "^0.2.0",
34
38
  "@totemsdk/omnia-vtxo": "^0.6.1",
35
- "@totemsdk/minima-rpc": "^0.4.0",
36
39
  "@totemsdk/wots-lease": "^1.0.1"
37
40
  },
38
41
  "devDependencies": {
39
42
  "@types/jest": "^30.0.0",
40
- "@types/better-sqlite3": "^7.6.13",
41
43
  "@types/node": "^20.19.43",
42
44
  "@types/ws": "^8.5.0",
43
45
  "jest": "^30.4.2",
@@ -51,7 +53,7 @@
51
53
  "publishConfig": {
52
54
  "access": "public"
53
55
  },
54
- "license": "MIT",
56
+ "license": "Apache-2.0",
55
57
  "author": "Totem SDK",
56
58
  "homepage": "https://docs.totem.ing",
57
59
  "repository": {