@optimystic/db-core 0.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.
Files changed (345) hide show
  1. package/README.md +328 -0
  2. package/dist/index.min.js +18 -0
  3. package/dist/index.min.js.map +7 -0
  4. package/dist/src/blocks/block-store.d.ts +12 -0
  5. package/dist/src/blocks/block-store.d.ts.map +1 -0
  6. package/dist/src/blocks/block-store.js +2 -0
  7. package/dist/src/blocks/block-store.js.map +1 -0
  8. package/dist/src/blocks/block-types.d.ts +3 -0
  9. package/dist/src/blocks/block-types.d.ts.map +1 -0
  10. package/dist/src/blocks/block-types.js +9 -0
  11. package/dist/src/blocks/block-types.js.map +1 -0
  12. package/dist/src/blocks/helpers.d.ts +4 -0
  13. package/dist/src/blocks/helpers.d.ts.map +1 -0
  14. package/dist/src/blocks/helpers.js +12 -0
  15. package/dist/src/blocks/helpers.js.map +1 -0
  16. package/dist/src/blocks/index.d.ts +5 -0
  17. package/dist/src/blocks/index.d.ts.map +1 -0
  18. package/dist/src/blocks/index.js +5 -0
  19. package/dist/src/blocks/index.js.map +1 -0
  20. package/dist/src/blocks/structs.d.ts +14 -0
  21. package/dist/src/blocks/structs.d.ts.map +1 -0
  22. package/dist/src/blocks/structs.js +2 -0
  23. package/dist/src/blocks/structs.js.map +1 -0
  24. package/dist/src/btree/btree.d.ts +135 -0
  25. package/dist/src/btree/btree.d.ts.map +1 -0
  26. package/dist/src/btree/btree.js +727 -0
  27. package/dist/src/btree/btree.js.map +1 -0
  28. package/dist/src/btree/independent-trunk.d.ts +17 -0
  29. package/dist/src/btree/independent-trunk.d.ts.map +1 -0
  30. package/dist/src/btree/independent-trunk.js +41 -0
  31. package/dist/src/btree/independent-trunk.js.map +1 -0
  32. package/dist/src/btree/index.d.ts +6 -0
  33. package/dist/src/btree/index.d.ts.map +1 -0
  34. package/dist/src/btree/index.js +6 -0
  35. package/dist/src/btree/index.js.map +1 -0
  36. package/dist/src/btree/key-range.d.ts +13 -0
  37. package/dist/src/btree/key-range.d.ts.map +1 -0
  38. package/dist/src/btree/key-range.js +20 -0
  39. package/dist/src/btree/key-range.js.map +1 -0
  40. package/dist/src/btree/keyset.d.ts +4 -0
  41. package/dist/src/btree/keyset.d.ts.map +1 -0
  42. package/dist/src/btree/keyset.js +4 -0
  43. package/dist/src/btree/keyset.js.map +1 -0
  44. package/dist/src/btree/nodes.d.ts +16 -0
  45. package/dist/src/btree/nodes.d.ts.map +1 -0
  46. package/dist/src/btree/nodes.js +9 -0
  47. package/dist/src/btree/nodes.js.map +1 -0
  48. package/dist/src/btree/path.d.ts +22 -0
  49. package/dist/src/btree/path.d.ts.map +1 -0
  50. package/dist/src/btree/path.js +39 -0
  51. package/dist/src/btree/path.js.map +1 -0
  52. package/dist/src/btree/tree-block.d.ts +7 -0
  53. package/dist/src/btree/tree-block.d.ts.map +1 -0
  54. package/dist/src/btree/tree-block.js +5 -0
  55. package/dist/src/btree/tree-block.js.map +1 -0
  56. package/dist/src/btree/trunk.d.ts +13 -0
  57. package/dist/src/btree/trunk.d.ts.map +1 -0
  58. package/dist/src/btree/trunk.js +2 -0
  59. package/dist/src/btree/trunk.js.map +1 -0
  60. package/dist/src/chain/chain-nodes.d.ts +18 -0
  61. package/dist/src/chain/chain-nodes.d.ts.map +1 -0
  62. package/dist/src/chain/chain-nodes.js +10 -0
  63. package/dist/src/chain/chain-nodes.js.map +1 -0
  64. package/dist/src/chain/chain.d.ts +75 -0
  65. package/dist/src/chain/chain.d.ts.map +1 -0
  66. package/dist/src/chain/chain.js +268 -0
  67. package/dist/src/chain/chain.js.map +1 -0
  68. package/dist/src/chain/index.d.ts +2 -0
  69. package/dist/src/chain/index.d.ts.map +1 -0
  70. package/dist/src/chain/index.js +2 -0
  71. package/dist/src/chain/index.js.map +1 -0
  72. package/dist/src/cluster/i-cluster.d.ts +5 -0
  73. package/dist/src/cluster/i-cluster.d.ts.map +1 -0
  74. package/dist/src/cluster/i-cluster.js +2 -0
  75. package/dist/src/cluster/i-cluster.js.map +1 -0
  76. package/dist/src/cluster/index.d.ts +3 -0
  77. package/dist/src/cluster/index.d.ts.map +1 -0
  78. package/dist/src/cluster/index.js +3 -0
  79. package/dist/src/cluster/index.js.map +1 -0
  80. package/dist/src/cluster/structs.d.ts +47 -0
  81. package/dist/src/cluster/structs.d.ts.map +1 -0
  82. package/dist/src/cluster/structs.js +2 -0
  83. package/dist/src/cluster/structs.js.map +1 -0
  84. package/dist/src/collection/action.d.ts +26 -0
  85. package/dist/src/collection/action.d.ts.map +1 -0
  86. package/dist/src/collection/action.js +2 -0
  87. package/dist/src/collection/action.js.map +1 -0
  88. package/dist/src/collection/collection.d.ts +48 -0
  89. package/dist/src/collection/collection.d.ts.map +1 -0
  90. package/dist/src/collection/collection.js +175 -0
  91. package/dist/src/collection/collection.js.map +1 -0
  92. package/dist/src/collection/index.d.ts +4 -0
  93. package/dist/src/collection/index.d.ts.map +1 -0
  94. package/dist/src/collection/index.js +4 -0
  95. package/dist/src/collection/index.js.map +1 -0
  96. package/dist/src/collection/struct.d.ts +16 -0
  97. package/dist/src/collection/struct.d.ts.map +1 -0
  98. package/dist/src/collection/struct.js +2 -0
  99. package/dist/src/collection/struct.js.map +1 -0
  100. package/dist/src/collections/diary/diary.d.ts +9 -0
  101. package/dist/src/collections/diary/diary.d.ts.map +1 -0
  102. package/dist/src/collections/diary/diary.js +37 -0
  103. package/dist/src/collections/diary/diary.js.map +1 -0
  104. package/dist/src/collections/diary/index.d.ts +3 -0
  105. package/dist/src/collections/diary/index.d.ts.map +1 -0
  106. package/dist/src/collections/diary/index.js +3 -0
  107. package/dist/src/collections/diary/index.js.map +1 -0
  108. package/dist/src/collections/diary/struct.d.ts +2 -0
  109. package/dist/src/collections/diary/struct.d.ts.map +1 -0
  110. package/dist/src/collections/diary/struct.js +3 -0
  111. package/dist/src/collections/diary/struct.js.map +1 -0
  112. package/dist/src/collections/index.d.ts +3 -0
  113. package/dist/src/collections/index.d.ts.map +1 -0
  114. package/dist/src/collections/index.js +3 -0
  115. package/dist/src/collections/index.js.map +1 -0
  116. package/dist/src/collections/tree/collection-trunk.d.ts +11 -0
  117. package/dist/src/collections/tree/collection-trunk.d.ts.map +1 -0
  118. package/dist/src/collections/tree/collection-trunk.js +22 -0
  119. package/dist/src/collections/tree/collection-trunk.js.map +1 -0
  120. package/dist/src/collections/tree/index.d.ts +3 -0
  121. package/dist/src/collections/tree/index.d.ts.map +1 -0
  122. package/dist/src/collections/tree/index.js +3 -0
  123. package/dist/src/collections/tree/index.js.map +1 -0
  124. package/dist/src/collections/tree/struct.d.ts +12 -0
  125. package/dist/src/collections/tree/struct.d.ts.map +1 -0
  126. package/dist/src/collections/tree/struct.js +4 -0
  127. package/dist/src/collections/tree/struct.js.map +1 -0
  128. package/dist/src/collections/tree/tree.d.ts +34 -0
  129. package/dist/src/collections/tree/tree.d.ts.map +1 -0
  130. package/dist/src/collections/tree/tree.js +100 -0
  131. package/dist/src/collections/tree/tree.js.map +1 -0
  132. package/dist/src/index.d.ts +18 -0
  133. package/dist/src/index.d.ts.map +1 -0
  134. package/dist/src/index.js +18 -0
  135. package/dist/src/index.js.map +1 -0
  136. package/dist/src/log/index.d.ts +3 -0
  137. package/dist/src/log/index.d.ts.map +1 -0
  138. package/dist/src/log/index.js +3 -0
  139. package/dist/src/log/index.js.map +1 -0
  140. package/dist/src/log/log.d.ts +57 -0
  141. package/dist/src/log/log.d.ts.map +1 -0
  142. package/dist/src/log/log.js +131 -0
  143. package/dist/src/log/log.js.map +1 -0
  144. package/dist/src/log/struct.d.ts +36 -0
  145. package/dist/src/log/struct.d.ts.map +1 -0
  146. package/dist/src/log/struct.js +3 -0
  147. package/dist/src/log/struct.js.map +1 -0
  148. package/dist/src/network/i-key-network.d.ts +21 -0
  149. package/dist/src/network/i-key-network.d.ts.map +1 -0
  150. package/dist/src/network/i-key-network.js +2 -0
  151. package/dist/src/network/i-key-network.js.map +1 -0
  152. package/dist/src/network/i-peer-network.d.ts +8 -0
  153. package/dist/src/network/i-peer-network.d.ts.map +1 -0
  154. package/dist/src/network/i-peer-network.js +2 -0
  155. package/dist/src/network/i-peer-network.js.map +1 -0
  156. package/dist/src/network/i-repo.d.ts +17 -0
  157. package/dist/src/network/i-repo.d.ts.map +1 -0
  158. package/dist/src/network/i-repo.js +2 -0
  159. package/dist/src/network/i-repo.js.map +1 -0
  160. package/dist/src/network/index.d.ts +6 -0
  161. package/dist/src/network/index.d.ts.map +1 -0
  162. package/dist/src/network/index.js +6 -0
  163. package/dist/src/network/index.js.map +1 -0
  164. package/dist/src/network/repo-protocol.d.ts +19 -0
  165. package/dist/src/network/repo-protocol.d.ts.map +1 -0
  166. package/dist/src/network/repo-protocol.js +2 -0
  167. package/dist/src/network/repo-protocol.js.map +1 -0
  168. package/dist/src/network/struct.d.ts +115 -0
  169. package/dist/src/network/struct.d.ts.map +1 -0
  170. package/dist/src/network/struct.js +2 -0
  171. package/dist/src/network/struct.js.map +1 -0
  172. package/dist/src/transaction/actions-engine.d.ts +37 -0
  173. package/dist/src/transaction/actions-engine.d.ts.map +1 -0
  174. package/dist/src/transaction/actions-engine.js +67 -0
  175. package/dist/src/transaction/actions-engine.js.map +1 -0
  176. package/dist/src/transaction/context.d.ts +60 -0
  177. package/dist/src/transaction/context.d.ts.map +1 -0
  178. package/dist/src/transaction/context.js +91 -0
  179. package/dist/src/transaction/context.js.map +1 -0
  180. package/dist/src/transaction/coordinator.d.ts +118 -0
  181. package/dist/src/transaction/coordinator.d.ts.map +1 -0
  182. package/dist/src/transaction/coordinator.js +417 -0
  183. package/dist/src/transaction/coordinator.js.map +1 -0
  184. package/dist/src/transaction/index.d.ts +10 -0
  185. package/dist/src/transaction/index.d.ts.map +1 -0
  186. package/dist/src/transaction/index.js +7 -0
  187. package/dist/src/transaction/index.js.map +1 -0
  188. package/dist/src/transaction/session.d.ts +80 -0
  189. package/dist/src/transaction/session.d.ts.map +1 -0
  190. package/dist/src/transaction/session.js +161 -0
  191. package/dist/src/transaction/session.js.map +1 -0
  192. package/dist/src/transaction/transaction.d.ts +156 -0
  193. package/dist/src/transaction/transaction.d.ts.map +1 -0
  194. package/dist/src/transaction/transaction.js +31 -0
  195. package/dist/src/transaction/transaction.js.map +1 -0
  196. package/dist/src/transaction/validator.d.ts +46 -0
  197. package/dist/src/transaction/validator.d.ts.map +1 -0
  198. package/dist/src/transaction/validator.js +97 -0
  199. package/dist/src/transaction/validator.js.map +1 -0
  200. package/dist/src/transactor/index.d.ts +4 -0
  201. package/dist/src/transactor/index.d.ts.map +1 -0
  202. package/dist/src/transactor/index.js +4 -0
  203. package/dist/src/transactor/index.js.map +1 -0
  204. package/dist/src/transactor/network-transactor.d.ts +36 -0
  205. package/dist/src/transactor/network-transactor.d.ts.map +1 -0
  206. package/dist/src/transactor/network-transactor.js +297 -0
  207. package/dist/src/transactor/network-transactor.js.map +1 -0
  208. package/dist/src/transactor/transactor-source.d.ts +24 -0
  209. package/dist/src/transactor/transactor-source.d.ts.map +1 -0
  210. package/dist/src/transactor/transactor-source.js +62 -0
  211. package/dist/src/transactor/transactor-source.js.map +1 -0
  212. package/dist/src/transactor/transactor.d.ts +38 -0
  213. package/dist/src/transactor/transactor.d.ts.map +1 -0
  214. package/dist/src/transactor/transactor.js +2 -0
  215. package/dist/src/transactor/transactor.js.map +1 -0
  216. package/dist/src/transform/atomic.d.ts +8 -0
  217. package/dist/src/transform/atomic.d.ts.map +1 -0
  218. package/dist/src/transform/atomic.js +14 -0
  219. package/dist/src/transform/atomic.js.map +1 -0
  220. package/dist/src/transform/cache-source.d.ts +13 -0
  221. package/dist/src/transform/cache-source.d.ts.map +1 -0
  222. package/dist/src/transform/cache-source.js +52 -0
  223. package/dist/src/transform/cache-source.js.map +1 -0
  224. package/dist/src/transform/helpers.d.ts +25 -0
  225. package/dist/src/transform/helpers.d.ts.map +1 -0
  226. package/dist/src/transform/helpers.js +105 -0
  227. package/dist/src/transform/helpers.js.map +1 -0
  228. package/dist/src/transform/index.d.ts +6 -0
  229. package/dist/src/transform/index.d.ts.map +1 -0
  230. package/dist/src/transform/index.js +6 -0
  231. package/dist/src/transform/index.js.map +1 -0
  232. package/dist/src/transform/struct.d.ts +19 -0
  233. package/dist/src/transform/struct.d.ts.map +1 -0
  234. package/dist/src/transform/struct.js +2 -0
  235. package/dist/src/transform/struct.js.map +1 -0
  236. package/dist/src/transform/tracker.d.ts +22 -0
  237. package/dist/src/transform/tracker.d.ts.map +1 -0
  238. package/dist/src/transform/tracker.js +64 -0
  239. package/dist/src/transform/tracker.js.map +1 -0
  240. package/dist/src/utility/actor.d.ts +11 -0
  241. package/dist/src/utility/actor.d.ts.map +1 -0
  242. package/dist/src/utility/actor.js +39 -0
  243. package/dist/src/utility/actor.js.map +1 -0
  244. package/dist/src/utility/batch-coordinator.d.ts +56 -0
  245. package/dist/src/utility/batch-coordinator.d.ts.map +1 -0
  246. package/dist/src/utility/batch-coordinator.js +127 -0
  247. package/dist/src/utility/batch-coordinator.js.map +1 -0
  248. package/dist/src/utility/block-id-to-bytes.d.ts +3 -0
  249. package/dist/src/utility/block-id-to-bytes.d.ts.map +1 -0
  250. package/dist/src/utility/block-id-to-bytes.js +7 -0
  251. package/dist/src/utility/block-id-to-bytes.js.map +1 -0
  252. package/dist/src/utility/ensured.d.ts +3 -0
  253. package/dist/src/utility/ensured.d.ts.map +1 -0
  254. package/dist/src/utility/ensured.js +24 -0
  255. package/dist/src/utility/ensured.js.map +1 -0
  256. package/dist/src/utility/groupby.d.ts +8 -0
  257. package/dist/src/utility/groupby.d.ts.map +1 -0
  258. package/dist/src/utility/groupby.js +15 -0
  259. package/dist/src/utility/groupby.js.map +1 -0
  260. package/dist/src/utility/is-record-empty.d.ts +3 -0
  261. package/dist/src/utility/is-record-empty.d.ts.map +1 -0
  262. package/dist/src/utility/is-record-empty.js +7 -0
  263. package/dist/src/utility/is-record-empty.js.map +1 -0
  264. package/dist/src/utility/latches.d.ts +11 -0
  265. package/dist/src/utility/latches.d.ts.map +1 -0
  266. package/dist/src/utility/latches.js +36 -0
  267. package/dist/src/utility/latches.js.map +1 -0
  268. package/dist/src/utility/nameof.d.ts +3 -0
  269. package/dist/src/utility/nameof.d.ts.map +1 -0
  270. package/dist/src/utility/nameof.js +5 -0
  271. package/dist/src/utility/nameof.js.map +1 -0
  272. package/dist/src/utility/pending.d.ts +13 -0
  273. package/dist/src/utility/pending.d.ts.map +1 -0
  274. package/dist/src/utility/pending.js +37 -0
  275. package/dist/src/utility/pending.js.map +1 -0
  276. package/package.json +56 -0
  277. package/src/blocks/block-store.ts +13 -0
  278. package/src/blocks/block-types.ts +11 -0
  279. package/src/blocks/helpers.ts +13 -0
  280. package/src/blocks/index.ts +5 -0
  281. package/src/blocks/structs.ts +17 -0
  282. package/src/btree/btree.ts +804 -0
  283. package/src/btree/independent-trunk.ts +54 -0
  284. package/src/btree/index.ts +5 -0
  285. package/src/btree/key-range.ts +15 -0
  286. package/src/btree/keyset.ts +6 -0
  287. package/src/btree/nodes.ts +25 -0
  288. package/src/btree/path.ts +37 -0
  289. package/src/btree/tree-block.ts +11 -0
  290. package/src/btree/trunk.ts +14 -0
  291. package/src/chain/chain-nodes.ts +24 -0
  292. package/src/chain/chain.ts +324 -0
  293. package/src/chain/index.ts +2 -0
  294. package/src/cluster/i-cluster.ts +6 -0
  295. package/src/cluster/index.ts +2 -0
  296. package/src/cluster/structs.ts +46 -0
  297. package/src/collection/action.ts +31 -0
  298. package/src/collection/collection.ts +200 -0
  299. package/src/collection/index.ts +3 -0
  300. package/src/collection/struct.ts +20 -0
  301. package/src/collections/diary/diary.ts +43 -0
  302. package/src/collections/diary/index.ts +2 -0
  303. package/src/collections/diary/struct.ts +3 -0
  304. package/src/collections/index.ts +2 -0
  305. package/src/collections/tree/collection-trunk.ts +25 -0
  306. package/src/collections/tree/index.ts +2 -0
  307. package/src/collections/tree/readme.md +19 -0
  308. package/src/collections/tree/struct.ts +18 -0
  309. package/src/collections/tree/tree.ts +124 -0
  310. package/src/index.ts +17 -0
  311. package/src/log/index.ts +2 -0
  312. package/src/log/log.ts +155 -0
  313. package/src/log/struct.ts +40 -0
  314. package/src/network/i-key-network.ts +24 -0
  315. package/src/network/i-peer-network.ts +8 -0
  316. package/src/network/i-repo.ts +19 -0
  317. package/src/network/index.ts +5 -0
  318. package/src/network/repo-protocol.ts +12 -0
  319. package/src/network/struct.ts +137 -0
  320. package/src/transaction/actions-engine.ts +83 -0
  321. package/src/transaction/context.ts +103 -0
  322. package/src/transaction/coordinator.ts +583 -0
  323. package/src/transaction/index.ts +30 -0
  324. package/src/transaction/session.ts +182 -0
  325. package/src/transaction/transaction.ts +205 -0
  326. package/src/transaction/validator.ts +150 -0
  327. package/src/transactor/index.ts +4 -0
  328. package/src/transactor/network-transactor.ts +435 -0
  329. package/src/transactor/transactor-source.ts +65 -0
  330. package/src/transactor/transactor.ts +44 -0
  331. package/src/transform/atomic.ts +16 -0
  332. package/src/transform/cache-source.ts +57 -0
  333. package/src/transform/helpers.ts +117 -0
  334. package/src/transform/index.ts +5 -0
  335. package/src/transform/struct.ts +22 -0
  336. package/src/transform/tracker.ts +70 -0
  337. package/src/utility/actor.ts +62 -0
  338. package/src/utility/batch-coordinator.ts +174 -0
  339. package/src/utility/block-id-to-bytes.ts +8 -0
  340. package/src/utility/ensured.ts +32 -0
  341. package/src/utility/groupby.ts +18 -0
  342. package/src/utility/is-record-empty.ts +5 -0
  343. package/src/utility/latches.ts +42 -0
  344. package/src/utility/nameof.ts +7 -0
  345. package/src/utility/pending.ts +41 -0
@@ -0,0 +1,161 @@
1
+ import { createTransactionStamp, createTransactionId } from "./transaction.js";
2
+ /**
3
+ * TransactionSession manages incremental transaction building.
4
+ *
5
+ * This is the high-level API for building transactions incrementally:
6
+ * - Stamp is created at BEGIN (stable throughout transaction)
7
+ * - Execute statements one at a time
8
+ * - Engine translates statements to actions (if not already provided)
9
+ * - Actions are immediately applied to collections via coordinator.applyActions()
10
+ * - On commit, all statements are compiled into a complete Transaction
11
+ * - The Transaction is then committed through coordinator.commit() for PEND/COMMIT orchestration
12
+ *
13
+ * Usage:
14
+ * const session = new TransactionSession(coordinator, engine);
15
+ * await session.execute('INSERT INTO users (id, name) VALUES (?, ?)', [1, 'Alice']);
16
+ * await session.execute('SELECT * FROM orders WHERE user_id = ?', [1]);
17
+ * const result = await session.commit();
18
+ *
19
+ * For validation/replay, use engine.execute() directly with a complete Transaction.
20
+ */
21
+ export class TransactionSession {
22
+ coordinator;
23
+ engine;
24
+ statements = [];
25
+ stamp;
26
+ committed = false;
27
+ rolledBack = false;
28
+ constructor(coordinator, engine, peerId = 'local', // TODO: Get from coordinator or config
29
+ schemaHash = '' // TODO: Get from engine
30
+ ) {
31
+ this.coordinator = coordinator;
32
+ this.engine = engine;
33
+ // Create stamp at BEGIN (stable throughout transaction)
34
+ this.stamp = createTransactionStamp(peerId, Date.now(), schemaHash, 'unknown' // TODO: Get engine ID from engine
35
+ );
36
+ }
37
+ /**
38
+ * Execute a statement.
39
+ *
40
+ * If actions are provided, they are applied directly.
41
+ * Otherwise, the engine translates the statement to actions.
42
+ *
43
+ * @param statement - The statement to execute (engine-specific, e.g., SQL statement)
44
+ * @param actions - Optional pre-computed actions (for Quereus module case)
45
+ * @returns Execution result with any returned values
46
+ */
47
+ async execute(statement, actions) {
48
+ if (this.committed) {
49
+ return { success: false, error: 'Transaction already committed' };
50
+ }
51
+ if (this.rolledBack) {
52
+ return { success: false, error: 'Transaction already rolled back' };
53
+ }
54
+ try {
55
+ // If actions not provided, enlist engine to translate statement
56
+ let actionsToApply;
57
+ if (actions) {
58
+ actionsToApply = actions;
59
+ }
60
+ else {
61
+ // Create a temporary transaction with just this statement for translation
62
+ const tempTransaction = {
63
+ stamp: this.stamp,
64
+ statements: [statement],
65
+ reads: [],
66
+ id: 'temp' // Temporary ID for translation only
67
+ };
68
+ const result = await this.engine.execute(tempTransaction);
69
+ if (!result.success || !result.actions) {
70
+ return { success: false, error: result.error || 'Failed to translate statement' };
71
+ }
72
+ actionsToApply = result.actions;
73
+ }
74
+ // Apply actions through coordinator
75
+ await this.coordinator.applyActions(actionsToApply, this.stamp.id);
76
+ // Accumulate the statement for later compilation
77
+ this.statements.push(statement);
78
+ return { success: true };
79
+ }
80
+ catch (error) {
81
+ return {
82
+ success: false,
83
+ error: `Failed to execute statement: ${error instanceof Error ? error.message : String(error)}`
84
+ };
85
+ }
86
+ }
87
+ /**
88
+ * Commit the transaction.
89
+ *
90
+ * Compiles all statements into a complete Transaction and commits through coordinator.
91
+ */
92
+ async commit() {
93
+ if (this.committed) {
94
+ return { success: false, error: 'Transaction already committed' };
95
+ }
96
+ if (this.rolledBack) {
97
+ return { success: false, error: 'Transaction already rolled back' };
98
+ }
99
+ // Create the complete transaction
100
+ const transaction = {
101
+ stamp: this.stamp,
102
+ statements: this.statements,
103
+ reads: [], // TODO: Track reads during statement execution
104
+ id: createTransactionId(this.stamp.id, this.statements, [])
105
+ };
106
+ // Commit through coordinator (which will orchestrate PEND/COMMIT)
107
+ await this.coordinator.commit(transaction);
108
+ this.committed = true;
109
+ return { success: true };
110
+ }
111
+ /**
112
+ * Rollback the transaction (discard local state).
113
+ *
114
+ * Note: Actions have already been applied to collections' trackers.
115
+ * Rollback just prevents commit and clears session state.
116
+ * Collections will discard tracker state when they sync or update.
117
+ */
118
+ async rollback() {
119
+ if (this.committed) {
120
+ throw new Error('Cannot rollback: transaction already committed');
121
+ }
122
+ if (this.rolledBack) {
123
+ throw new Error('Transaction already rolled back');
124
+ }
125
+ // Rollback through coordinator
126
+ await this.coordinator.rollback(this.stamp.id);
127
+ this.rolledBack = true;
128
+ this.statements.length = 0;
129
+ }
130
+ /**
131
+ * Get the transaction stamp ID (stable throughout transaction).
132
+ */
133
+ getStampId() {
134
+ return this.stamp.id;
135
+ }
136
+ /**
137
+ * Get the transaction stamp (full metadata).
138
+ */
139
+ getStamp() {
140
+ return this.stamp;
141
+ }
142
+ /**
143
+ * Get the list of accumulated statements.
144
+ */
145
+ getStatements() {
146
+ return this.statements;
147
+ }
148
+ /**
149
+ * Check if the transaction has been committed.
150
+ */
151
+ isCommitted() {
152
+ return this.committed;
153
+ }
154
+ /**
155
+ * Check if the transaction has been rolled back.
156
+ */
157
+ isRolledBack() {
158
+ return this.rolledBack;
159
+ }
160
+ }
161
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../../../src/transaction/session.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAE/E;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,kBAAkB;IAOZ;IACA;IAPD,UAAU,GAAa,EAAE,CAAC;IAC1B,KAAK,CAAmB;IACjC,SAAS,GAAG,KAAK,CAAC;IAClB,UAAU,GAAG,KAAK,CAAC;IAE3B,YACkB,WAAmC,EACnC,MAA0B,EAC3C,SAAiB,OAAO,EAAE,uCAAuC;IACjE,aAAqB,EAAE,CAAC,wBAAwB;;QAH/B,gBAAW,GAAX,WAAW,CAAwB;QACnC,WAAM,GAAN,MAAM,CAAoB;QAI3C,wDAAwD;QACxD,IAAI,CAAC,KAAK,GAAG,sBAAsB,CAClC,MAAM,EACN,IAAI,CAAC,GAAG,EAAE,EACV,UAAU,EACV,SAAS,CAAC,kCAAkC;SAC5C,CAAC;IACH,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,OAAO,CAAC,SAAiB,EAAE,OAA6B;QAC7D,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,+BAA+B,EAAE,CAAC;QACnE,CAAC;QACD,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACrB,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,iCAAiC,EAAE,CAAC;QACrE,CAAC;QAED,IAAI,CAAC;YACJ,gEAAgE;YAChE,IAAI,cAAmC,CAAC;YACxC,IAA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IACH,aAAa;QACZ,OAAO,IAAI,CAAC,UAAU,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,WAAW;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,YAAY;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACxB,CAAC;CACD"}
@@ -0,0 +1,156 @@
1
+ import type { BlockId } from "../blocks/index.js";
2
+ import type { CollectionId } from "../collection/index.js";
3
+ /**
4
+ * Transaction Stamp: Created at BEGIN, stable throughout transaction lifecycle.
5
+ *
6
+ * The stamp contains metadata about the transaction's origin and context.
7
+ * The id is computed as a hash of these fields.
8
+ */
9
+ export type TransactionStamp = {
10
+ /** Peer that initiated the transaction */
11
+ peerId: string;
12
+ /** When transaction started (milliseconds since epoch) */
13
+ timestamp: number;
14
+ /** Hash of schema version(s) for validation */
15
+ schemaHash: string;
16
+ /** Which engine (e.g., 'quereus@0.5.3', 'actions@1.0.0') */
17
+ engineId: string;
18
+ /** Hash of the stamp fields (computed) - stable identifier throughout transaction */
19
+ id: string;
20
+ };
21
+ /**
22
+ * Transaction: Finalized at COMMIT with complete statement history.
23
+ *
24
+ * Transactions span multiple collections and use pluggable engines for interpreting the statements.
25
+ * The engine re-executes the statements to verify the resulting operations match what was proposed.
26
+ */
27
+ export type Transaction = {
28
+ /** The transaction stamp (includes stable id) */
29
+ stamp: TransactionStamp;
30
+ /** Engine-specific statements (for replay/validation)
31
+ * Array of statements executed during the transaction.
32
+ * - For Quereus: SQL statements
33
+ * - For ActionsEngine: JSON-encoded actions
34
+ */
35
+ statements: string[];
36
+ /** Read dependencies for optimistic concurrency control */
37
+ reads: ReadDependency[];
38
+ /** Transaction identifier (hash of stamp.id + statements + reads)
39
+ * Final transaction identity, used in logs
40
+ */
41
+ id: string;
42
+ };
43
+ /**
44
+ * Read dependency for optimistic concurrency control.
45
+ * Tracks which block revisions were read during transaction execution.
46
+ */
47
+ export type ReadDependency = {
48
+ blockId: BlockId;
49
+ /** Expected revision number at time of read */
50
+ revision: number;
51
+ };
52
+ /**
53
+ * Transaction reference embedded in actions.
54
+ * Just the transaction ID - full transaction can be looked up separately if needed.
55
+ */
56
+ export type TransactionRef = string;
57
+ /**
58
+ * Create a transaction stamp with computed id.
59
+ * The id is a hash of the stamp fields.
60
+ */
61
+ export declare function createTransactionStamp(peerId: string, timestamp: number, schemaHash: string, engineId: string): TransactionStamp;
62
+ /**
63
+ * Create a transaction id from stamp id, statements, and reads.
64
+ * This is the final transaction identity used in logs.
65
+ */
66
+ export declare function createTransactionId(stampId: string, statements: string[], reads: ReadDependency[]): string;
67
+ /**
68
+ * Transaction engine interface.
69
+ * Pluggable engines implement this to process transaction statements.
70
+ *
71
+ * Engines are responsible for:
72
+ * 1. Parsing the engine-specific statements
73
+ * 2. Executing/re-executing to produce actions
74
+ * 3. Returning the resulting actions per collection
75
+ */
76
+ export interface ITransactionEngine {
77
+ /**
78
+ * Process a transaction statements to produce actions.
79
+ *
80
+ * Used both for:
81
+ * - Initial execution (client creating transaction)
82
+ * - Re-execution (validators verifying transaction)
83
+ *
84
+ * @param transaction - The transaction to process
85
+ * @returns The resulting actions from execution
86
+ */
87
+ execute(transaction: Transaction): Promise<ExecutionResult>;
88
+ }
89
+ /**
90
+ * Result of transaction execution.
91
+ */
92
+ export type ExecutionResult = {
93
+ /** Whether execution succeeded */
94
+ success: boolean;
95
+ /** Actions produced by executing the transaction */
96
+ actions?: CollectionActions[];
97
+ /** Results from executing actions (e.g., return values from reads) */
98
+ results?: Map<CollectionId, any[]>;
99
+ /** Error message if execution failed */
100
+ error?: string;
101
+ };
102
+ /**
103
+ * Actions for a specific collection resulting from transaction execution.
104
+ */
105
+ export type CollectionActions = {
106
+ /** Collection identifier */
107
+ collectionId: string;
108
+ /** Actions to apply to this collection */
109
+ actions: unknown[];
110
+ };
111
+ /**
112
+ * Result of transaction validation.
113
+ */
114
+ export type ValidationResult = {
115
+ /** Whether validation succeeded */
116
+ valid: boolean;
117
+ /** Reason for validation failure (if valid=false) */
118
+ reason?: string;
119
+ /** The operations hash computed during validation (for debugging) */
120
+ computedHash?: string;
121
+ };
122
+ /**
123
+ * Transaction validator interface.
124
+ * Pluggable validators implement this to verify transaction integrity.
125
+ *
126
+ * Validators are invoked when a node receives a PendRequest with a transaction.
127
+ * They re-execute the transaction and verify the operations match.
128
+ */
129
+ export interface ITransactionValidator {
130
+ /**
131
+ * Validate a transaction by re-executing and comparing operations hash.
132
+ *
133
+ * Validation steps:
134
+ * 1. Verify stamp.engineId matches a known engine
135
+ * 2. Verify stamp.schemaHash matches local schema
136
+ * 3. Verify read dependencies (no stale reads)
137
+ * 4. Re-execute transaction.statements through engine (isolated state)
138
+ * 5. Collect operations from re-execution
139
+ * 6. Compute hash of operations
140
+ * 7. Compare with sender's operationsHash
141
+ *
142
+ * @param transaction - The transaction to validate
143
+ * @param operationsHash - The hash to compare against
144
+ * @returns Validation result
145
+ */
146
+ validate(transaction: Transaction, operationsHash: string): Promise<ValidationResult>;
147
+ /**
148
+ * Get the schema hash for a given engine.
149
+ * Used to verify the sender's schema matches local schema.
150
+ *
151
+ * @param engineId - The engine identifier
152
+ * @returns The schema hash, or undefined if engine not found
153
+ */
154
+ getSchemaHash(engineId: string): Promise<string | undefined>;
155
+ }
156
+ //# sourceMappingURL=transaction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../../../src/transaction/transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC9B,0CAA0C;IAC1C,MAAM,EAAE,MAAM,CAAC;IAEf,0DAA0D;IAC1D,SAAS,EAAE,MAAM,CAAC;IAElB,+CAA+C;IAC/C,UAAU,EAAE,MAAM,CAAC;IAEnB,4DAA4D;IAC5D,Q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AAC;CACf,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC/B,4BAA4B;IAC5B,YAAY,EAAE,MAAM,CAAC;IACrB,0CAA0C;IAC1C,OAAO,EAAE,OAAO,EAAE,CAAC;CACnB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC9B,mCAAmC;IACnC,KAAK,EAAE,OAAO,CAAC;IACf,qDAAqD;IACrD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qEAAqE;IACrE,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,WAAW,qBAAqB;IACrC;;;;;;;;;;;;;;;OAeG;IACH,QAAQ,CAAC,WAAW,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAEtF;;;;;;OAMG;IACH,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;CAC7D"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Create a transaction stamp with computed id.
3
+ * The id is a hash of the stamp fields.
4
+ */
5
+ export function createTransactionStamp(peerId, timestamp, schemaHash, engineId) {
6
+ const stampData = JSON.stringify({ peerId, timestamp, schemaHash, engineId });
7
+ const id = `stamp:${hashString(stampData)}`;
8
+ return { peerId, timestamp, schemaHash, engineId, id };
9
+ }
10
+ /**
11
+ * Create a transaction id from stamp id, statements, and reads.
12
+ * This is the final transaction identity used in logs.
13
+ */
14
+ export function createTransactionId(stampId, statements, reads) {
15
+ const txData = JSON.stringify({ stampId, statements, reads });
16
+ return `tx:${hashString(txData)}`;
17
+ }
18
+ /**
19
+ * Simple hash function for creating IDs.
20
+ * Uses a basic hash for now - can be replaced with proper cryptographic hash later.
21
+ */
22
+ function hashString(str) {
23
+ let hash = 0;
24
+ for (let i = 0; i < str.length; i++) {
25
+ const char = str.charCodeAt(i);
26
+ hash = ((hash << 5) - hash) + char;
27
+ hash = hash & hash; // Convert to 32-bit integer
28
+ }
29
+ return Math.abs(hash).toString(36);
30
+ }
31
+ //# sourceMappingURL=transaction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../../src/transaction/transaction.ts"],"names":[],"mappings":"AAoEA;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CACrC,MAAc,EACd,SAAiB,EACjB,UAAkB,EAClB,QAAgB;IAEhB,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC9E,MAAM,EAAE,GAAG,SAAS,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;IAC5C,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;AACxD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAClC,OAAe,EACf,UAAoB,EACpB,KAAuB;IAEvB,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;IAC9D,OAAO,MAAM,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;AACnC,CAAC;AAED;;;GAGG;AACH,SAAS,UAAU,CAAC,GAAW;IAC9B,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAC/B,IAAI,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;QACnC,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,4BAA4B;IACjD,CAAC;IACD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,46 @@
1
+ import type { CollectionId, Transforms } from '../index.js';
2
+ import type { Transaction, ITransactionEngine, ITransactionValidator, ValidationResult, CollectionActions } from './transaction.js';
3
+ /**
4
+ * Engine registration for validation.
5
+ */
6
+ export type EngineRegistration = {
7
+ /** The transaction engine instance */
8
+ engine: ITransactionEngine;
9
+ /** Get the current schema hash for this engine */
10
+ getSchemaHash: () => Promise<string>;
11
+ };
12
+ /**
13
+ * Factory function to create a validation coordinator.
14
+ * This allows isolated execution of transactions for validation.
15
+ */
16
+ export type ValidationCoordinatorFactory = () => {
17
+ /** Apply actions to collections in isolated state */
18
+ applyActions(actions: CollectionActions[], stampId: string): Promise<void>;
19
+ /** Get all transforms from the validation state */
20
+ getTransforms(): Map<CollectionId, Transforms>;
21
+ /** Dispose of the validation coordinator */
22
+ dispose(): void;
23
+ };
24
+ /**
25
+ * Transaction validator implementation.
26
+ *
27
+ * Validates transactions by re-executing them and comparing operations hash.
28
+ * Used by cluster participants when receiving PendRequests.
29
+ */
30
+ export declare class TransactionValidator implements ITransactionValidator {
31
+ private readonly engines;
32
+ private readonly createValidationCoordinator;
33
+ constructor(engines: Map<string, EngineRegistration>, createValidationCoordinator: ValidationCoordinatorFactory);
34
+ validate(transaction: Transaction, operationsHash: string): Promise<ValidationResult>;
35
+ getSchemaHash(engineId: string): Promise<string | undefined>;
36
+ /**
37
+ * Collect all operations from transforms.
38
+ */
39
+ private collectOperations;
40
+ /**
41
+ * Compute hash of all operations.
42
+ * Must match TransactionCoordinator.hashOperations for consistent validation.
43
+ */
44
+ private hashOperations;
45
+ }
46
+ //# sourceMappingURL=validator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../../src/transaction/validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAW,YAAY,EAA2B,UAAU,EAAe,MAAM,aAAa,CAAC;AAC3G,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAapI;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAChC,sCAAsC;IACtC,MAAM,EAAE,kBAAkB,CAAC;IAC3B,kDAAkD;IAClD,aAAa,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;CACrC,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,4BAA4B,GAAG,MAAM;IAChD,qDAAqD;IACrD,YAAY,CAAC,OAAO,EAAE,iBAAiB,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3E,mDAAmD;IACnD,aAAa,IAAI,GAAG,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;IAC/C,4CAA4C;IAC5C,OAAO,IAAI,IAAI,CAAC;CAChB,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,oBAAqB,YAAW,qBAAqB;IAEhE,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,2BAA2B;gBAD3B,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,EACxC,2BAA2B,EAAE,4BAA4B;IAGrE,QAAQ,CAAC,WAAW,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAiErF,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAKlE;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAczB;;;OAGG;IACH,OAAO,CAAC,cAAc;CAQtB"}
@@ -0,0 +1,97 @@
1
+ import { Tracker } from '../transform/tracker.js';
2
+ /**
3
+ * Transaction validator implementation.
4
+ *
5
+ * Validates transactions by re-executing them and comparing operations hash.
6
+ * Used by cluster participants when receiving PendRequests.
7
+ */
8
+ export class TransactionValidator {
9
+ engines;
10
+ createValidationCoordinator;
11
+ constructor(engines, createValidationCoordinator) {
12
+ this.engines = engines;
13
+ this.createValidationCoordinator = createValidationCoordinator;
14
+ }
15
+ async validate(transaction, operationsHash) {
16
+ const { stamp, statements } = transaction;
17
+ // 1. Verify engine exists
18
+ const registration = this.engines.get(stamp.engineId);
19
+ if (!registration) {
20
+ return {
21
+ valid: false,
22
+ reason: `Unknown engine: ${stamp.engineId}`
23
+ };
24
+ }
25
+ // 2. Verify schema hash matches
26
+ const localSchemaHash = await registration.getSchemaHash();
27
+ if (localSchemaHash !== stamp.schemaHash) {
28
+ return {
29
+ valid: false,
30
+ reason: `Schema mismatch: local=${localSchemaHash}, sender=${stamp.schemaHash}`
31
+ };
32
+ }
33
+ // 3. Verify read dependencies (optimistic concurrency)
34
+ // TODO: Implement read dependency validation
35
+ // For now, we skip this check - will be implemented with proper block versioning
36
+ // 4. Create isolated validation coordinator
37
+ const validationCoordinator = this.createValidationCoordinator();
38
+ try {
39
+ // 5. Re-execute transaction through engine
40
+ const result = await registration.engine.execute(transaction);
41
+ if (!result.success) {
42
+ return {
43
+ valid: false,
44
+ reason: `Re-execution failed: ${result.error}`
45
+ };
46
+ }
47
+ // 6. Apply actions to validation coordinator (builds transforms)
48
+ if (result.actions && result.actions.length > 0) {
49
+ await validationCoordinator.applyActions(result.actions, stamp.id);
50
+ }
51
+ // 7. Collect operations from validation coordinator
52
+ const transforms = validationCoordinator.getTransforms();
53
+ const allOperations = this.collectOperations(transforms);
54
+ // 8. Compute hash
55
+ const computedHash = this.hashOperations(allOperations);
56
+ // 9. Compare with sender's hash
57
+ if (computedHash !== operationsHash) {
58
+ return {
59
+ valid: false,
60
+ reason: `Operations hash mismatch`,
61
+ computedHash
62
+ };
63
+ }
64
+ return { valid: true, computedHash };
65
+ }
66
+ finally {
67
+ validationCoordinator.dispose();
68
+ }
69
+ }
70
+ async getSchemaHash(engineId) {
71
+ const registration = this.engines.get(engineId);
72
+ return registration ? await registration.getSchemaHash() : undefined;
73
+ }
74
+ /**
75
+ * Collect all operations from transforms.
76
+ */
77
+ collectOperations(transforms) {
78
+ return Array.from(transforms.entries()).flatMap(([collectionId, t]) => [
79
+ ...Object.entries(t.inserts).map(([blockId, block]) => ({ type: 'insert', collectionId, blockId, block })),
80
+ ...Object.entries(t.updates).map(([blockId, operations]) => ({ type: 'update', collectionId, blockId, operations })),
81
+ ...t.deletes.map(blockId => ({ type: 'delete', collectionId, blockId }))
82
+ ]);
83
+ }
84
+ /**
85
+ * Compute hash of all operations.
86
+ * Must match TransactionCoordinator.hashOperations for consistent validation.
87
+ */
88
+ hashOperations(operations) {
89
+ const operationsData = JSON.stringify(operations);
90
+ const hash = Array.from(operationsData).reduce((acc, char) => {
91
+ const charCode = char.charCodeAt(0);
92
+ return ((acc << 5) - acc + charCode) & acc;
93
+ }, 0);
94
+ return `ops:${Math.abs(hash).toString(36)}`;
95
+ }
96
+ }
97
+ //# sourceMappingURL=validator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validator.js","sourceRoot":"","sources":["../../../src/transaction/validator.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAkClD;;;;;GAKG;AACH,MAAM,OAAO,oBAAoB;IAEd;IACA;IAFlB,YACkB,OAAwC,EACxC,2BAAyD;QADzD,YAAO,GAAP,OAAO,CAAiC;QACxC,gCAA2B,GAA3B,2BAA2B,CAA8B;IACxE,CAAC;IAEJ,KAAK,CAAC,QAAQ,CAAC,WAAwB,EAAE,cAAsB;QAC9D,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,WAAW,CAAC;QAE1C,0BAA0B;QAC1B,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QACtD,IAAI,CAAC,YAAY,EAAE,CAAC;YACnB,O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,CAAC,CACpD;SACD,CAAC,CAAC;IACJ,CAAC;IAED;;;OAGG;IACK,cAAc,CAAC,UAAgC;QACtD,MAAM,cAAc,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QAClD,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;YAC5D,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YACpC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,GAAG,GAAG,QAAQ,CAAC,GAAG,GAAG,CAAC;QAC5C,CAAC,EAAE,CAAC,CAAC,CAAC;QACN,OAAO,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;IAC7C,CAAC;CACD"}
@@ -0,0 +1,4 @@
1
+ export * from "./network-transactor.js";
2
+ export * from "./transactor-source.js";
3
+ export * from "./transactor.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/transactor/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from "./network-transactor.js";
2
+ export * from "./transactor-source.js";
3
+ export * from "./transactor.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/transactor/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { type PeerId } from "@libp2p/interface";
2
+ import type { ActionTransforms, ActionBlocks, BlockActionStatus, ITransactor, IKeyNetwork, BlockId, GetBlockResults, PendResult, CommitResult, PendRequest, IRepo, BlockGets, CommitRequest, ClusterNomineesResult } from "../index.js";
3
+ type NetworkTransactorInit = {
4
+ timeoutMs: number;
5
+ abortOrCancelTimeoutMs: number;
6
+ keyNetwork: IKeyNetwork;
7
+ getRepo: (peerId: PeerId) => IRepo;
8
+ };
9
+ export declare class NetworkTransactor implements ITransactor {
10
+ private readonly keyNetwork;
11
+ private readonly timeoutMs;
12
+ private readonly abortOrCancelTimeoutMs;
13
+ private readonly getRepo;
14
+ constructor(init: NetworkTransactorInit);
15
+ get(blockGets: BlockGets): Promise<GetBlockResults>;
16
+ getStatus(blockActions: ActionBlocks[]): Promise<BlockActionStatus[]>;
17
+ private consolidateCoordinators;
18
+ pend(blockAction: PendRequest): Promise<PendResult>;
19
+ cancel(actionRef: ActionBlocks): Promise<void>;
20
+ queryClusterNominees(blockId: BlockId): Promise<ClusterNomineesResult>;
21
+ commit(request: CommitRequest): Promise<CommitResult>;
22
+ private commitBlock;
23
+ /** Attempts to commit a set of blocks, and handles failures and errors */
24
+ private commitBlocks;
25
+ /** Creates batches for a given payload, grouped by the coordinating peer for each block id */
26
+ private batchesForPayload;
27
+ /** Cancels a pending transaction by canceling all blocks associated with the transaction, including failed peers */
28
+ private cancelBatch;
29
+ private formatBatchStatuses;
30
+ }
31
+ /**
32
+ * Returns the block actions grouped by action id and concatenated transforms
33
+ */
34
+ export declare function distinctBlockActionTransforms(blockActions: ActionTransforms[]): ActionTransforms[];
35
+ export {};
36
+ //# sourceMappingURL=network-transactor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"network-transactor.d.ts","sourceRoot":"","sources":["../../../src/transactor/network-transactor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,iBAAiB,EAAE,WAAW,EAA6B,WAAW,EAAE,OAAO,EAAE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