@bsv/wallet-toolbox 2.1.25 → 3.0.0-alpha.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.
Files changed (257) hide show
  1. package/docs/CREATEACTION_BLOCKERS.md +391 -0
  2. package/docs/CUTOVER_RUNBOOK.md +95 -0
  3. package/docs/REQUIREMENTS_COMPLIANCE.md +157 -0
  4. package/docs/ROLLOUT_PLAN.md +273 -0
  5. package/docs/SESSION_HANDOFF.md +298 -0
  6. package/docs/STORAGE_METHOD_WIRING.md +176 -0
  7. package/docs/client.md +3765 -1325
  8. package/docs/monitor.md +255 -33
  9. package/docs/services.md +304 -280
  10. package/docs/setup.md +24 -24
  11. package/docs/storage.md +2783 -251
  12. package/docs/v3-upgrade/index.html +911 -0
  13. package/docs/wallet.md +4956 -9455
  14. package/out/src/Wallet.d.ts.map +1 -1
  15. package/out/src/Wallet.js.map +1 -1
  16. package/out/src/WalletLogger.d.ts.map +1 -1
  17. package/out/src/WalletLogger.js.map +1 -1
  18. package/out/src/__tests/CWIStyleWalletManager.test.js.map +1 -1
  19. package/out/src/__tests/WalletPermissionsManager.fixtures.d.ts.map +1 -1
  20. package/out/src/__tests/WalletPermissionsManager.fixtures.js.map +1 -1
  21. package/out/src/entropy/EntropyCollector.d.ts.map +1 -1
  22. package/out/src/entropy/EntropyCollector.js.map +1 -1
  23. package/out/src/monitor/LeasedMonitorTask.d.ts +43 -0
  24. package/out/src/monitor/LeasedMonitorTask.d.ts.map +1 -0
  25. package/out/src/monitor/LeasedMonitorTask.js +89 -0
  26. package/out/src/monitor/LeasedMonitorTask.js.map +1 -0
  27. package/out/src/monitor/Monitor.d.ts +7 -0
  28. package/out/src/monitor/Monitor.d.ts.map +1 -1
  29. package/out/src/monitor/Monitor.js +7 -0
  30. package/out/src/monitor/Monitor.js.map +1 -1
  31. package/out/src/monitor/MonitorDaemon.d.ts.map +1 -1
  32. package/out/src/monitor/MonitorDaemon.js.map +1 -1
  33. package/out/src/monitor/V7LeasedTask.d.ts +43 -0
  34. package/out/src/monitor/V7LeasedTask.d.ts.map +1 -0
  35. package/out/src/monitor/V7LeasedTask.js +89 -0
  36. package/out/src/monitor/V7LeasedTask.js.map +1 -0
  37. package/out/src/monitor/index.all.d.ts +1 -0
  38. package/out/src/monitor/index.all.d.ts.map +1 -1
  39. package/out/src/monitor/index.all.js +1 -0
  40. package/out/src/monitor/index.all.js.map +1 -1
  41. package/out/src/monitor/tasks/TaskCheckForProofs.d.ts +2 -0
  42. package/out/src/monitor/tasks/TaskCheckForProofs.d.ts.map +1 -1
  43. package/out/src/monitor/tasks/TaskCheckForProofs.js +55 -0
  44. package/out/src/monitor/tasks/TaskCheckForProofs.js.map +1 -1
  45. package/out/src/monitor/tasks/TaskSendWaiting.d.ts.map +1 -1
  46. package/out/src/monitor/tasks/TaskSendWaiting.js.map +1 -1
  47. package/out/src/sdk/WalletStorage.interfaces.d.ts +59 -59
  48. package/out/src/sdk/WalletStorage.interfaces.d.ts.map +1 -1
  49. package/out/src/sdk/types.d.ts +32 -0
  50. package/out/src/sdk/types.d.ts.map +1 -1
  51. package/out/src/sdk/types.js +50 -1
  52. package/out/src/sdk/types.js.map +1 -1
  53. package/out/src/services/chaintracker/chaintracks/Ingest/LiveIngestorWhatsOnChainPoll.d.ts.map +1 -1
  54. package/out/src/services/chaintracker/chaintracks/Ingest/LiveIngestorWhatsOnChainPoll.js.map +1 -1
  55. package/out/src/services/chaintracker/chaintracks/Storage/ChaintracksStorageBase.d.ts.map +1 -1
  56. package/out/src/services/chaintracker/chaintracks/Storage/ChaintracksStorageBase.js.map +1 -1
  57. package/out/src/services/chaintracker/chaintracks/Storage/ChaintracksStorageKnex.d.ts.map +1 -1
  58. package/out/src/services/chaintracker/chaintracks/Storage/ChaintracksStorageKnex.js.map +1 -1
  59. package/out/src/services/chaintracker/chaintracks/createDefaultIdbChaintracksOptions.d.ts.map +1 -1
  60. package/out/src/services/chaintracker/chaintracks/createDefaultIdbChaintracksOptions.js.map +1 -1
  61. package/out/src/services/chaintracker/chaintracks/createDefaultKnexChaintracksOptions.d.ts.map +1 -1
  62. package/out/src/services/chaintracker/chaintracks/createDefaultKnexChaintracksOptions.js.map +1 -1
  63. package/out/src/services/chaintracker/chaintracks/createDefaultNoDbChaintracksOptions.d.ts.map +1 -1
  64. package/out/src/services/chaintracker/chaintracks/createDefaultNoDbChaintracksOptions.js.map +1 -1
  65. package/out/src/services/chaintracker/chaintracks/createIdbChaintracks.d.ts.map +1 -1
  66. package/out/src/services/chaintracker/chaintracks/createIdbChaintracks.js.map +1 -1
  67. package/out/src/services/chaintracker/chaintracks/createKnexChaintracks.d.ts.map +1 -1
  68. package/out/src/services/chaintracker/chaintracks/createKnexChaintracks.js.map +1 -1
  69. package/out/src/services/chaintracker/chaintracks/createNoDbChaintracks.d.ts.map +1 -1
  70. package/out/src/services/chaintracker/chaintracks/createNoDbChaintracks.js.map +1 -1
  71. package/out/src/services/chaintracker/chaintracks/util/BulkFilesReader.d.ts.map +1 -1
  72. package/out/src/services/chaintracker/chaintracks/util/BulkFilesReader.js.map +1 -1
  73. package/out/src/services/chaintracker/chaintracks/util/ChaintracksFs.d.ts.map +1 -1
  74. package/out/src/services/chaintracker/chaintracks/util/ChaintracksFs.js.map +1 -1
  75. package/out/src/services/chaintracker/chaintracks/util/SingleWriterMultiReaderLock.d.ts.map +1 -1
  76. package/out/src/services/chaintracker/chaintracks/util/SingleWriterMultiReaderLock.js.map +1 -1
  77. package/out/src/services/chaintracker/chaintracks/util/__tests/SingleWriterMultiReaderLock.test.js.map +1 -1
  78. package/out/src/storage/StorageIdb.d.ts.map +1 -1
  79. package/out/src/storage/StorageIdb.js +10 -5
  80. package/out/src/storage/StorageIdb.js.map +1 -1
  81. package/out/src/storage/StorageKnex.d.ts +133 -3
  82. package/out/src/storage/StorageKnex.d.ts.map +1 -1
  83. package/out/src/storage/StorageKnex.js +691 -94
  84. package/out/src/storage/StorageKnex.js.map +1 -1
  85. package/out/src/storage/StorageProvider.d.ts +114 -1
  86. package/out/src/storage/StorageProvider.d.ts.map +1 -1
  87. package/out/src/storage/StorageProvider.js +164 -4
  88. package/out/src/storage/StorageProvider.js.map +1 -1
  89. package/out/src/storage/WalletStorageManager.d.ts.map +1 -1
  90. package/out/src/storage/WalletStorageManager.js.map +1 -1
  91. package/out/src/storage/idbHelpers.d.ts +5 -0
  92. package/out/src/storage/idbHelpers.d.ts.map +1 -1
  93. package/out/src/storage/idbHelpers.js +42 -0
  94. package/out/src/storage/idbHelpers.js.map +1 -1
  95. package/out/src/storage/methods/attemptToPostReqsToNetwork.d.ts.map +1 -1
  96. package/out/src/storage/methods/attemptToPostReqsToNetwork.js +116 -4
  97. package/out/src/storage/methods/attemptToPostReqsToNetwork.js.map +1 -1
  98. package/out/src/storage/methods/createAction.d.ts.map +1 -1
  99. package/out/src/storage/methods/createAction.js +22 -5
  100. package/out/src/storage/methods/createAction.js.map +1 -1
  101. package/out/src/storage/methods/internalizeAction.d.ts.map +1 -1
  102. package/out/src/storage/methods/internalizeAction.js +172 -5
  103. package/out/src/storage/methods/internalizeAction.js.map +1 -1
  104. package/out/src/storage/methods/listActionsKnex.d.ts +49 -0
  105. package/out/src/storage/methods/listActionsKnex.d.ts.map +1 -1
  106. package/out/src/storage/methods/listActionsKnex.js +179 -69
  107. package/out/src/storage/methods/listActionsKnex.js.map +1 -1
  108. package/out/src/storage/methods/listOutputsKnex.d.ts.map +1 -1
  109. package/out/src/storage/methods/listOutputsKnex.js +51 -4
  110. package/out/src/storage/methods/listOutputsKnex.js.map +1 -1
  111. package/out/src/storage/methods/processAction.d.ts.map +1 -1
  112. package/out/src/storage/methods/processAction.js +82 -14
  113. package/out/src/storage/methods/processAction.js.map +1 -1
  114. package/out/src/storage/methods/purgeData.d.ts.map +1 -1
  115. package/out/src/storage/methods/purgeData.js +22 -14
  116. package/out/src/storage/methods/purgeData.js.map +1 -1
  117. package/out/src/storage/methods/reviewStatus.d.ts.map +1 -1
  118. package/out/src/storage/methods/reviewStatus.js +18 -9
  119. package/out/src/storage/methods/reviewStatus.js.map +1 -1
  120. package/out/src/storage/schema/KnexMigrations.d.ts.map +1 -1
  121. package/out/src/storage/schema/KnexMigrations.js +134 -0
  122. package/out/src/storage/schema/KnexMigrations.js.map +1 -1
  123. package/out/src/storage/schema/StorageIdbSchema.d.ts +43 -2
  124. package/out/src/storage/schema/StorageIdbSchema.d.ts.map +1 -1
  125. package/out/src/storage/schema/__tests/backfill.runner.test.d.ts +2 -0
  126. package/out/src/storage/schema/__tests/backfill.runner.test.d.ts.map +1 -0
  127. package/out/src/storage/schema/__tests/backfill.runner.test.js +148 -0
  128. package/out/src/storage/schema/__tests/backfill.runner.test.js.map +1 -0
  129. package/out/src/storage/schema/__tests/backfill.test.d.ts +2 -0
  130. package/out/src/storage/schema/__tests/backfill.test.d.ts.map +1 -0
  131. package/out/src/storage/schema/__tests/backfill.test.js +96 -0
  132. package/out/src/storage/schema/__tests/backfill.test.js.map +1 -0
  133. package/out/src/storage/schema/__tests/processingFsm.test.d.ts +2 -0
  134. package/out/src/storage/schema/__tests/processingFsm.test.d.ts.map +1 -0
  135. package/out/src/storage/schema/__tests/processingFsm.test.js +42 -0
  136. package/out/src/storage/schema/__tests/processingFsm.test.js.map +1 -0
  137. package/out/src/storage/schema/__tests/processingFsmLegacyMapping.test.d.ts +2 -0
  138. package/out/src/storage/schema/__tests/processingFsmLegacyMapping.test.d.ts.map +1 -0
  139. package/out/src/storage/schema/__tests/processingFsmLegacyMapping.test.js +82 -0
  140. package/out/src/storage/schema/__tests/processingFsmLegacyMapping.test.js.map +1 -0
  141. package/out/src/storage/schema/__tests/spendabilityRule.test.d.ts +2 -0
  142. package/out/src/storage/schema/__tests/spendabilityRule.test.d.ts.map +1 -0
  143. package/out/src/storage/schema/__tests/spendabilityRule.test.js +29 -0
  144. package/out/src/storage/schema/__tests/spendabilityRule.test.js.map +1 -0
  145. package/out/src/storage/schema/__tests/v7FsmLegacyMapping.test.d.ts +2 -0
  146. package/out/src/storage/schema/__tests/v7FsmLegacyMapping.test.d.ts.map +1 -0
  147. package/out/src/storage/schema/__tests/v7FsmLegacyMapping.test.js +77 -0
  148. package/out/src/storage/schema/__tests/v7FsmLegacyMapping.test.js.map +1 -0
  149. package/out/src/storage/schema/backfill.d.ts +35 -0
  150. package/out/src/storage/schema/backfill.d.ts.map +1 -0
  151. package/out/src/storage/schema/backfill.idb.d.ts +32 -0
  152. package/out/src/storage/schema/backfill.idb.d.ts.map +1 -0
  153. package/out/src/storage/schema/backfill.idb.js +95 -0
  154. package/out/src/storage/schema/backfill.idb.js.map +1 -0
  155. package/out/src/storage/schema/backfill.js +150 -0
  156. package/out/src/storage/schema/backfill.js.map +1 -0
  157. package/out/src/storage/schema/backfill.knex.d.ts +32 -0
  158. package/out/src/storage/schema/backfill.knex.d.ts.map +1 -0
  159. package/out/src/storage/schema/backfill.knex.js +240 -0
  160. package/out/src/storage/schema/backfill.knex.js.map +1 -0
  161. package/out/src/storage/schema/backfill.runner.d.ts +63 -0
  162. package/out/src/storage/schema/backfill.runner.d.ts.map +1 -0
  163. package/out/src/storage/schema/backfill.runner.js +64 -0
  164. package/out/src/storage/schema/backfill.runner.js.map +1 -0
  165. package/out/src/storage/schema/coinbaseMaturityBackfill.d.ts +25 -0
  166. package/out/src/storage/schema/coinbaseMaturityBackfill.d.ts.map +1 -0
  167. package/out/src/storage/schema/coinbaseMaturityBackfill.js +75 -0
  168. package/out/src/storage/schema/coinbaseMaturityBackfill.js.map +1 -0
  169. package/out/src/storage/schema/monitorLease.d.ts +57 -0
  170. package/out/src/storage/schema/monitorLease.d.ts.map +1 -0
  171. package/out/src/storage/schema/monitorLease.js +101 -0
  172. package/out/src/storage/schema/monitorLease.js.map +1 -0
  173. package/out/src/storage/schema/processingFsm.d.ts +27 -0
  174. package/out/src/storage/schema/processingFsm.d.ts.map +1 -0
  175. package/out/src/storage/schema/processingFsm.js +132 -0
  176. package/out/src/storage/schema/processingFsm.js.map +1 -0
  177. package/out/src/storage/schema/schemaCutover.d.ts +34 -0
  178. package/out/src/storage/schema/schemaCutover.d.ts.map +1 -0
  179. package/out/src/storage/schema/schemaCutover.js +230 -0
  180. package/out/src/storage/schema/schemaCutover.js.map +1 -0
  181. package/out/src/storage/schema/schemaCutoverIdb.d.ts +26 -0
  182. package/out/src/storage/schema/schemaCutoverIdb.d.ts.map +1 -0
  183. package/out/src/storage/schema/schemaCutoverIdb.js +90 -0
  184. package/out/src/storage/schema/schemaCutoverIdb.js.map +1 -0
  185. package/out/src/storage/schema/spendabilityRefresh.d.ts +49 -0
  186. package/out/src/storage/schema/spendabilityRefresh.d.ts.map +1 -0
  187. package/out/src/storage/schema/spendabilityRefresh.js +120 -0
  188. package/out/src/storage/schema/spendabilityRefresh.js.map +1 -0
  189. package/out/src/storage/schema/spendabilityRule.d.ts +45 -0
  190. package/out/src/storage/schema/spendabilityRule.d.ts.map +1 -0
  191. package/out/src/storage/schema/spendabilityRule.js +52 -0
  192. package/out/src/storage/schema/spendabilityRule.js.map +1 -0
  193. package/out/src/storage/schema/tables/TableAction.d.ts +3 -3
  194. package/out/src/storage/schema/tables/TableAction.d.ts.map +1 -1
  195. package/out/src/storage/schema/tables/TableChainTip.d.ts +1 -1
  196. package/out/src/storage/schema/tables/TableMonitorLease.d.ts +1 -1
  197. package/out/src/storage/schema/tables/TableOutput.d.ts +7 -0
  198. package/out/src/storage/schema/tables/TableOutput.d.ts.map +1 -1
  199. package/out/src/storage/schema/tables/TableOutput.js.map +1 -1
  200. package/out/src/storage/schema/tables/TableTransactionNew.d.ts +50 -0
  201. package/out/src/storage/schema/tables/TableTransactionNew.d.ts.map +1 -0
  202. package/out/src/storage/schema/tables/TableTransactionNew.js +3 -0
  203. package/out/src/storage/schema/tables/TableTransactionNew.js.map +1 -0
  204. package/out/src/storage/schema/tables/TableTxAudit.d.ts +1 -1
  205. package/out/src/storage/schema/tables/index.d.ts +5 -0
  206. package/out/src/storage/schema/tables/index.d.ts.map +1 -1
  207. package/out/src/storage/schema/tables/index.js +5 -0
  208. package/out/src/storage/schema/tables/index.js.map +1 -1
  209. package/out/src/storage/schema/transactionCrud.d.ts +41 -0
  210. package/out/src/storage/schema/transactionCrud.d.ts.map +1 -0
  211. package/out/src/storage/schema/transactionCrud.js +205 -0
  212. package/out/src/storage/schema/transactionCrud.js.map +1 -0
  213. package/out/src/storage/schema/transactionService.d.ts +315 -0
  214. package/out/src/storage/schema/transactionService.d.ts.map +1 -0
  215. package/out/src/storage/schema/transactionService.js +783 -0
  216. package/out/src/storage/schema/transactionService.js.map +1 -0
  217. package/out/src/storage/schema/txAudit.d.ts +33 -0
  218. package/out/src/storage/schema/txAudit.d.ts.map +1 -0
  219. package/out/src/storage/schema/txAudit.js +64 -0
  220. package/out/src/storage/schema/txAudit.js.map +1 -0
  221. package/out/src/storage/schema/v7Backfill.d.ts.map +1 -1
  222. package/out/src/storage/schema/v7Backfill.js +4 -1
  223. package/out/src/storage/schema/v7Backfill.js.map +1 -1
  224. package/out/src/storage/schema/v7Backfill.runner.d.ts.map +1 -1
  225. package/out/src/storage/schema/v7Backfill.runner.js +3 -1
  226. package/out/src/storage/schema/v7Backfill.runner.js.map +1 -1
  227. package/out/src/storage/schema/v7CoinbaseMaturityBackfill.d.ts +25 -0
  228. package/out/src/storage/schema/v7CoinbaseMaturityBackfill.d.ts.map +1 -0
  229. package/out/src/storage/schema/v7CoinbaseMaturityBackfill.js +75 -0
  230. package/out/src/storage/schema/v7CoinbaseMaturityBackfill.js.map +1 -0
  231. package/out/src/storage/schema/v7Crud.d.ts +5 -3
  232. package/out/src/storage/schema/v7Crud.d.ts.map +1 -1
  233. package/out/src/storage/schema/v7Crud.js +11 -9
  234. package/out/src/storage/schema/v7Crud.js.map +1 -1
  235. package/out/src/storage/schema/v7Cutover.d.ts +34 -0
  236. package/out/src/storage/schema/v7Cutover.d.ts.map +1 -0
  237. package/out/src/storage/schema/v7Cutover.js +223 -0
  238. package/out/src/storage/schema/v7Cutover.js.map +1 -0
  239. package/out/src/storage/schema/v7CutoverIdb.d.ts +26 -0
  240. package/out/src/storage/schema/v7CutoverIdb.d.ts.map +1 -0
  241. package/out/src/storage/schema/v7CutoverIdb.js +90 -0
  242. package/out/src/storage/schema/v7CutoverIdb.js.map +1 -0
  243. package/out/src/storage/schema/v7Fsm.d.ts.map +1 -1
  244. package/out/src/storage/schema/v7Fsm.js +22 -6
  245. package/out/src/storage/schema/v7Fsm.js.map +1 -1
  246. package/out/src/storage/schema/v7Service.d.ts +305 -0
  247. package/out/src/storage/schema/v7Service.d.ts.map +1 -0
  248. package/out/src/storage/schema/v7Service.js +757 -0
  249. package/out/src/storage/schema/v7Service.js.map +1 -0
  250. package/out/src/storage/schema/v7SpendabilityRefresh.d.ts +49 -0
  251. package/out/src/storage/schema/v7SpendabilityRefresh.d.ts.map +1 -0
  252. package/out/src/storage/schema/v7SpendabilityRefresh.js +111 -0
  253. package/out/src/storage/schema/v7SpendabilityRefresh.js.map +1 -0
  254. package/out/src/storage/storageProviderHelpers.js +1 -1
  255. package/out/src/storage/storageProviderHelpers.js.map +1 -1
  256. package/out/src/utility/Format.js.map +1 -1
  257. package/package.json +7 -4
@@ -0,0 +1,240 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KnexBackfillDriver = void 0;
4
+ exports.runKnexBackfill = runKnexBackfill;
5
+ const backfill_runner_1 = require("./backfill.runner");
6
+ const STREAM_BATCH = 500;
7
+ /**
8
+ * Knex driver for the the backfill.
9
+ *
10
+ * Reads from the legacy `proven_tx_reqs`, `proven_txs`, and `transactions`
11
+ * tables, writes into the new `transactions_new` and `actions` tables, and
12
+ * re-points `tx_labels_map` rows to the new `actionId`.
13
+ *
14
+ * All work is performed against the `knex` (or transaction) handle passed in.
15
+ * The caller is responsible for opening a transaction around the orchestrator
16
+ * call when atomicity is desired.
17
+ */
18
+ class KnexBackfillDriver {
19
+ constructor(knex) {
20
+ this.knex = knex;
21
+ }
22
+ async *streamLegacyReqs() {
23
+ var _a, _b, _c;
24
+ let lastId = 0;
25
+ /* eslint-disable-next-line no-constant-condition */
26
+ while (true) {
27
+ const rows = await this.knex('proven_tx_reqs as r')
28
+ .leftJoin('proven_txs as p', 'r.provenTxId', 'p.provenTxId')
29
+ .select('r.*', this.knex.ref('p.height').as('p_height'), this.knex.ref('p.index').as('p_index'), this.knex.ref('p.merklePath').as('p_merklePath'), this.knex.ref('p.blockHash').as('p_blockHash'), this.knex.ref('p.merkleRoot').as('p_merkleRoot'), this.knex.ref('p.rawTx').as('p_rawTx'), this.knex.ref('p.created_at').as('p_created_at'), this.knex.ref('p.updated_at').as('p_updated_at'), this.knex.ref('p.txid').as('p_txid'))
30
+ .where('r.provenTxReqId', '>', lastId)
31
+ .orderBy('r.provenTxReqId')
32
+ .limit(STREAM_BATCH);
33
+ if (rows.length === 0)
34
+ return;
35
+ for (const row of rows) {
36
+ lastId = row.provenTxReqId;
37
+ const req = {
38
+ created_at: row.created_at,
39
+ updated_at: row.updated_at,
40
+ provenTxReqId: row.provenTxReqId,
41
+ provenTxId: row.provenTxId,
42
+ status: row.status,
43
+ attempts: row.attempts,
44
+ notified: row.notified,
45
+ txid: row.txid,
46
+ batch: row.batch,
47
+ history: row.history,
48
+ notify: row.notify,
49
+ rawTx: (_a = toBytes(row.rawTx)) !== null && _a !== void 0 ? _a : [],
50
+ inputBEEF: toBytes(row.inputBEEF),
51
+ wasBroadcast: row.wasBroadcast,
52
+ rebroadcastAttempts: row.rebroadcastAttempts
53
+ };
54
+ let proven;
55
+ if (row.p_txid != null) {
56
+ proven = {
57
+ created_at: row.p_created_at,
58
+ updated_at: row.p_updated_at,
59
+ provenTxId: row.provenTxId,
60
+ txid: row.p_txid,
61
+ height: row.p_height,
62
+ index: row.p_index,
63
+ merklePath: (_b = toBytes(row.p_merklePath)) !== null && _b !== void 0 ? _b : [],
64
+ rawTx: (_c = toBytes(row.p_rawTx)) !== null && _c !== void 0 ? _c : [],
65
+ blockHash: row.p_blockHash,
66
+ merkleRoot: row.p_merkleRoot
67
+ };
68
+ }
69
+ yield { req, proven };
70
+ }
71
+ }
72
+ }
73
+ async *streamLegacyTransactions() {
74
+ let lastId = 0;
75
+ /* eslint-disable-next-line no-constant-condition */
76
+ while (true) {
77
+ const rows = await this.knex('transactions')
78
+ .where('transactionId', '>', lastId)
79
+ .orderBy('transactionId')
80
+ .limit(STREAM_BATCH)
81
+ .select('*');
82
+ if (rows.length === 0)
83
+ return;
84
+ for (const row of rows) {
85
+ lastId = row.transactionId;
86
+ yield {
87
+ created_at: row.created_at,
88
+ updated_at: row.updated_at,
89
+ transactionId: row.transactionId,
90
+ userId: row.userId,
91
+ provenTxId: row.provenTxId,
92
+ status: row.status,
93
+ reference: row.reference,
94
+ isOutgoing: !!row.isOutgoing,
95
+ satoshis: row.satoshis,
96
+ description: row.description,
97
+ version: row.version,
98
+ lockTime: row.lockTime,
99
+ txid: row.txid,
100
+ inputBEEF: toBytes(row.inputBEEF),
101
+ rawTx: toBytes(row.rawTx)
102
+ };
103
+ }
104
+ }
105
+ }
106
+ async upsertTransactionNew(row) {
107
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
108
+ const existing = await this.knex('transactions_new').where({ txid: row.txid }).first('transactionId');
109
+ if (existing != null) {
110
+ await this.knex('transactions_new')
111
+ .where({ transactionId: existing.transactionId })
112
+ .update({
113
+ processing: row.processing,
114
+ processing_changed_at: row.processingChangedAt,
115
+ next_action_at: (_a = row.nextActionAt) !== null && _a !== void 0 ? _a : null,
116
+ attempts: row.attempts,
117
+ rebroadcast_cycles: row.rebroadcastCycles,
118
+ was_broadcast: row.wasBroadcast,
119
+ batch: (_b = row.batch) !== null && _b !== void 0 ? _b : null,
120
+ raw_tx: toBuf(row.rawTx),
121
+ input_beef: toBuf(row.inputBeef),
122
+ height: (_c = row.height) !== null && _c !== void 0 ? _c : null,
123
+ merkle_index: (_d = row.merkleIndex) !== null && _d !== void 0 ? _d : null,
124
+ merkle_path: toBuf(row.merklePath),
125
+ merkle_root: (_e = row.merkleRoot) !== null && _e !== void 0 ? _e : null,
126
+ block_hash: (_f = row.blockHash) !== null && _f !== void 0 ? _f : null,
127
+ is_coinbase: row.isCoinbase,
128
+ updated_at: row.updated_at
129
+ });
130
+ return existing.transactionId;
131
+ }
132
+ const [insertedId] = await this.knex('transactions_new').insert({
133
+ txid: row.txid,
134
+ processing: row.processing,
135
+ processing_changed_at: row.processingChangedAt,
136
+ next_action_at: (_g = row.nextActionAt) !== null && _g !== void 0 ? _g : null,
137
+ attempts: row.attempts,
138
+ rebroadcast_cycles: row.rebroadcastCycles,
139
+ was_broadcast: row.wasBroadcast,
140
+ idempotency_key: (_h = row.idempotencyKey) !== null && _h !== void 0 ? _h : null,
141
+ batch: (_j = row.batch) !== null && _j !== void 0 ? _j : null,
142
+ raw_tx: toBuf(row.rawTx),
143
+ input_beef: toBuf(row.inputBeef),
144
+ height: (_k = row.height) !== null && _k !== void 0 ? _k : null,
145
+ merkle_index: (_l = row.merkleIndex) !== null && _l !== void 0 ? _l : null,
146
+ merkle_path: toBuf(row.merklePath),
147
+ merkle_root: (_m = row.merkleRoot) !== null && _m !== void 0 ? _m : null,
148
+ block_hash: (_o = row.blockHash) !== null && _o !== void 0 ? _o : null,
149
+ is_coinbase: row.isCoinbase,
150
+ last_provider: (_p = row.lastProvider) !== null && _p !== void 0 ? _p : null,
151
+ last_provider_status: (_q = row.lastProviderStatus) !== null && _q !== void 0 ? _q : null,
152
+ frozen_reason: (_r = row.frozenReason) !== null && _r !== void 0 ? _r : null,
153
+ row_version: row.rowVersion,
154
+ created_at: row.created_at,
155
+ updated_at: row.updated_at
156
+ });
157
+ if (typeof insertedId === 'number' && insertedId > 0)
158
+ return insertedId;
159
+ const row2 = await this.knex('transactions_new').where({ txid: row.txid }).first('transactionId');
160
+ return row2.transactionId;
161
+ }
162
+ async upsertAction(row) {
163
+ var _a, _b;
164
+ const existing = await this.knex('actions')
165
+ .where({ userId: row.userId, transactionId: row.transactionId })
166
+ .first('actionId');
167
+ if (existing != null) {
168
+ await this.knex('actions')
169
+ .where({ actionId: existing.actionId })
170
+ .update({
171
+ reference: row.reference,
172
+ description: row.description,
173
+ isOutgoing: row.isOutgoing,
174
+ satoshis_delta: row.satoshisDelta,
175
+ user_nosend: row.userNosend,
176
+ hidden: row.hidden,
177
+ user_aborted: row.userAborted,
178
+ notify_json: (_a = row.notifyJson) !== null && _a !== void 0 ? _a : null,
179
+ updated_at: row.updated_at
180
+ });
181
+ return existing.actionId;
182
+ }
183
+ const [insertedId] = await this.knex('actions').insert({
184
+ userId: row.userId,
185
+ transactionId: row.transactionId,
186
+ reference: row.reference,
187
+ description: row.description,
188
+ isOutgoing: row.isOutgoing,
189
+ satoshis_delta: row.satoshisDelta,
190
+ user_nosend: row.userNosend,
191
+ hidden: row.hidden,
192
+ user_aborted: row.userAborted,
193
+ notify_json: (_b = row.notifyJson) !== null && _b !== void 0 ? _b : null,
194
+ row_version: row.rowVersion,
195
+ created_at: row.created_at,
196
+ updated_at: row.updated_at
197
+ });
198
+ if (typeof insertedId === 'number' && insertedId > 0)
199
+ return insertedId;
200
+ const row2 = await this.knex('actions')
201
+ .where({ userId: row.userId, transactionId: row.transactionId })
202
+ .first('actionId');
203
+ return row2.actionId;
204
+ }
205
+ async repointTxLabelMap(legacyTransactionId, actionId) {
206
+ if (legacyTransactionId === actionId)
207
+ return;
208
+ await this.knex('tx_labels_map')
209
+ .where({ transactionId: legacyTransactionId })
210
+ .update({ transactionId: actionId });
211
+ }
212
+ }
213
+ exports.KnexBackfillDriver = KnexBackfillDriver;
214
+ /**
215
+ * Convenience entry point. Opens a Knex transaction and runs the orchestrator
216
+ * inside it so that the entire backfill either succeeds or rolls back.
217
+ */
218
+ async function runKnexBackfill(knex, now = new Date()) {
219
+ return await knex.transaction(async (trx) => {
220
+ const driver = new KnexBackfillDriver(trx);
221
+ return await (0, backfill_runner_1.runBackfill)(driver, now);
222
+ });
223
+ }
224
+ function toBytes(v) {
225
+ if (v == null)
226
+ return undefined;
227
+ if (Array.isArray(v))
228
+ return v;
229
+ if (Buffer.isBuffer(v))
230
+ return Array.from(v.values());
231
+ if (v instanceof Uint8Array)
232
+ return Array.from(v.values());
233
+ return undefined;
234
+ }
235
+ function toBuf(v) {
236
+ if (v == null)
237
+ return null;
238
+ return Buffer.from(v);
239
+ }
240
+ //# sourceMappingURL=backfill.knex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backfill.knex.js","sourceRoot":"","sources":["../../../../src/storage/schema/backfill.knex.ts"],"names":[],"mappings":";;;AAqOA,0CAKC;AAlOD,uDAA8E;AAE9E,MAAM,YAAY,GAAG,GAAG,CAAA;AAExB;;;;;;;;;;GAUG;AACH,MAAa,kBAAkB;IAC7B,YAA8B,IAAU;QAAV,SAAI,GAAJ,IAAI,CAAM;IAAG,CAAC;IAE5C,KAAK,CAAC,CAAE,gBAAgB;;QACtB,IAAI,MAAM,GAAG,CAAC,CAAA;QACd,oDAAoD;QACpD,OAAO,IAAI,EAAE,CAAC;YACZ,MAAM,IAAI,GAAU,MAAM,IAAI,CAAC,IAAI,CAAC,qBAAqB,CAAC;iBACvD,QAAQ,CAAC,iBAAiB,EAAE,cAAc,EAAE,cAAc,CAAC;iBAC3D,MAAM,CACL,KAAK,EACL,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,EACxC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,EACtC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC,cAAc,CAAC,EAChD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC,aAAa,CAAC,EAC9C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC,cAAc,CAAC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E,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAA;IAC1D,OAAO,SAAS,CAAA;AAClB,CAAC;AAED,SAAS,KAAK,CAAE,CAAuB;IACrC,IAAI,CAAC,IAAI,IAAI;QAAE,OAAO,IAAI,CAAA;IAC1B,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;AACvB,CAAC"}
@@ -0,0 +1,63 @@
1
+ import { TableAction, TableProvenTx, TableProvenTxReq, TableTransaction, TableTransactionNew } from './tables';
2
+ /**
3
+ * Storage-agnostic driver used by the the backfill orchestrator.
4
+ *
5
+ * Implementations must be idempotent — calling each method twice with the same
6
+ * input must not produce duplicate rows. The orchestrator is responsible for
7
+ * iteration order and chunking; the driver only performs writes.
8
+ */
9
+ export interface BackfillDriver {
10
+ /** Stream legacy `proven_tx_reqs` rows joined with their `proven_txs` row when available. */
11
+ streamLegacyReqs: () => AsyncIterable<{
12
+ req: TableProvenTxReq;
13
+ proven?: TableProvenTx;
14
+ }>;
15
+ /** Stream legacy `transactions` rows ordered by transactionId ascending. */
16
+ streamLegacyTransactions: () => AsyncIterable<TableTransaction>;
17
+ /**
18
+ * Upsert a new `transactions` row by `txid`. Returns the resolved
19
+ * `transactionId` (PK of `transactions_new`).
20
+ */
21
+ upsertTransactionNew: (row: Omit<TableTransactionNew, 'transactionId'>) => Promise<number>;
22
+ /**
23
+ * Upsert a new `actions` row by `(userId, transactionId)`. Returns the
24
+ * resolved `actionId`.
25
+ */
26
+ upsertAction: (row: Omit<TableAction, 'actionId'>) => Promise<number>;
27
+ /**
28
+ * Re-point a single legacy `tx_labels_map` row whose `transactionId` refers
29
+ * to the old `transactions` PK so that it points to the new `actions.actionId`.
30
+ *
31
+ * Implementations may choose to rewrite the entire mapping in bulk and treat
32
+ * this method as a no-op; the orchestrator will still call it once per pair.
33
+ */
34
+ repointTxLabelMap: (legacyTransactionId: number, actionId: number) => Promise<void>;
35
+ }
36
+ export interface BackfillStats {
37
+ reqsBackfilled: number;
38
+ legacyTxOnlyBackfilled: number;
39
+ actionsBackfilled: number;
40
+ labelMapsRepointed: number;
41
+ }
42
+ /**
43
+ * Orchestrates §3 Steps 2-5 of the spec (PROD_REQ_V7_TS.md).
44
+ *
45
+ * Strategy:
46
+ * 1. Walk every `proven_tx_reqs` row (joined with its proof) and upsert a row
47
+ * in `transactions_new`. Build a `txid -> transactionId` map.
48
+ * 2. Walk every legacy `transactions` row.
49
+ * - If the row has a txid AND it is not in the map, upsert a tx-only new-schema
50
+ * row (locally-created actions that never reached the broadcast queue).
51
+ * - Compute the new-schema `transactionId` for this legacy row. Rows without a
52
+ * txid (unsigned, unprocessed) are skipped — they have no canonical new-schema
53
+ * representation yet and are surfaced as gaps in the stats.
54
+ * 3. Upsert an `actions` row per legacy `transactions` row that has a new-schema
55
+ * transactionId.
56
+ * 4. Re-point `tx_labels_map.transactionId` from legacy PK to new `actionId`.
57
+ *
58
+ * The driver is invoked one row at a time so that the entire pass can run as
59
+ * a single Knex transaction or a single IDB readwrite transaction without
60
+ * holding the full row set in memory.
61
+ */
62
+ export declare function runBackfill(driver: BackfillDriver, now?: Date): Promise<BackfillStats>;
63
+ //# sourceMappingURL=backfill.runner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backfill.runner.d.ts","sourceRoot":"","sources":["../../../../src/storage/schema/backfill.runner.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,EACpB,MAAM,UAAU,CAAA;AAEjB;;;;;;GAMG;AACH,MAAM,WAAW,cAAc;IAC7B,6FAA6F;IAC7F,gBAAgB,EAAE,MAAM,aAAa,CAAC;QAAE,GAAG,EAAE,gBAAgB,CAAC;QAAC,MAAM,CAAC,EAAE,aAAa,CAAA;KAAE,CAAC,CAAA;IACxF,4EAA4E;IAC5E,wBAAwB,EAAE,MAAM,aAAa,CAAC,gBAAgB,CAAC,CAAA;IAC/D;;;OAGG;IACH,oBAAoB,EAAE,CAAC,GAAG,EAAE,IAAI,CAAC,mBAAmB,EAAE,eAAe,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IAC1F;;;OAGG;IACH,YAAY,EAAE,CAAC,GAAG,EAAE,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IACrE;;;;;;OAMG;IACH,iBAAiB,EAAE,CAAC,mBAAmB,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CACpF;AAED,MAAM,WAAW,aAAa;IAC5B,cAAc,EAAE,MAAM,CAAA;IACtB,sBAAsB,EAAE,MAAM,CAAA;IAC9B,iBAAiB,EAAE,MAAM,CAAA;IACzB,kBAAkB,EAAE,MAAM,CAAA;CAC3B;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,WAAW,CAAE,MAAM,EAAE,cAAc,EAAE,GAAG,GAAE,IAAiB,GAAG,OAAO,CAAC,aAAa,CAAC,CA2CzG"}
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runBackfill = runBackfill;
4
+ const backfill_1 = require("./backfill");
5
+ /**
6
+ * Orchestrates §3 Steps 2-5 of the spec (PROD_REQ_V7_TS.md).
7
+ *
8
+ * Strategy:
9
+ * 1. Walk every `proven_tx_reqs` row (joined with its proof) and upsert a row
10
+ * in `transactions_new`. Build a `txid -> transactionId` map.
11
+ * 2. Walk every legacy `transactions` row.
12
+ * - If the row has a txid AND it is not in the map, upsert a tx-only new-schema
13
+ * row (locally-created actions that never reached the broadcast queue).
14
+ * - Compute the new-schema `transactionId` for this legacy row. Rows without a
15
+ * txid (unsigned, unprocessed) are skipped — they have no canonical new-schema
16
+ * representation yet and are surfaced as gaps in the stats.
17
+ * 3. Upsert an `actions` row per legacy `transactions` row that has a new-schema
18
+ * transactionId.
19
+ * 4. Re-point `tx_labels_map.transactionId` from legacy PK to new `actionId`.
20
+ *
21
+ * The driver is invoked one row at a time so that the entire pass can run as
22
+ * a single Knex transaction or a single IDB readwrite transaction without
23
+ * holding the full row set in memory.
24
+ */
25
+ async function runBackfill(driver, now = new Date()) {
26
+ const stats = {
27
+ reqsBackfilled: 0,
28
+ legacyTxOnlyBackfilled: 0,
29
+ actionsBackfilled: 0,
30
+ labelMapsRepointed: 0
31
+ };
32
+ const txidToNewId = new Map();
33
+ for await (const { req, proven } of driver.streamLegacyReqs()) {
34
+ const newRow = (0, backfill_1.buildTransactionNewRow)(req, proven, now);
35
+ const id = await driver.upsertTransactionNew(newRow);
36
+ txidToNewId.set(newRow.txid, id);
37
+ stats.reqsBackfilled += 1;
38
+ }
39
+ for await (const legacy of driver.streamLegacyTransactions()) {
40
+ let newId;
41
+ if (legacy.txid != null) {
42
+ newId = txidToNewId.get(legacy.txid);
43
+ if (newId === undefined) {
44
+ const newRow = (0, backfill_1.buildTransactionNewRowFromLegacyTx)(legacy, now);
45
+ if (newRow !== undefined) {
46
+ newId = await driver.upsertTransactionNew(newRow);
47
+ // Use newRow.txid (not legacy.txid) as the map key so that empty-string
48
+ // legacy txids each get a unique placeholder key and don't collide.
49
+ txidToNewId.set(newRow.txid, newId);
50
+ stats.legacyTxOnlyBackfilled += 1;
51
+ }
52
+ }
53
+ }
54
+ if (newId === undefined)
55
+ continue;
56
+ const actionRow = (0, backfill_1.buildActionRow)(legacy, newId, now);
57
+ const actionId = await driver.upsertAction(actionRow);
58
+ stats.actionsBackfilled += 1;
59
+ await driver.repointTxLabelMap(legacy.transactionId, actionId);
60
+ stats.labelMapsRepointed += 1;
61
+ }
62
+ return stats;
63
+ }
64
+ //# sourceMappingURL=backfill.runner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backfill.runner.js","sourceRoot":"","sources":["../../../../src/storage/schema/backfill.runner.ts"],"names":[],"mappings":";;AAoEA,kCA2CC;AA/GD,yCAAuG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IAAI,EAAE,KAAK,CAAC,CAAA;oBACnC,KAAK,CAAC,sBAAsB,IAAI,CAAC,CAAA;gBACnC,CAAC;YACH,CAAC;QACH,CAAC;QACD,IAAI,KAAK,KAAK,SAAS;YAAE,SAAQ;QAEjC,MAAM,SAAS,GAAG,IAAA,yBAAc,EAAC,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC,CAAA;QACpD,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,YAAY,CAAC,SAAS,CAAC,CAAA;QACrD,KAAK,CAAC,iBAAiB,IAAI,CAAC,CAAA;QAE5B,MAAM,MAAM,CAAC,iBAAiB,CAAC,MAAM,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAA;QAC9D,KAAK,CAAC,kBAAkB,IAAI,CAAC,CAAA;IAC/B,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { Knex } from 'knex';
2
+ export interface BackfillCoinbaseMaturityArgs {
3
+ /** Maximum number of rows examined per batch. Default 500. */
4
+ batchSize?: number;
5
+ /** Override clock for `updated_at` writes. Default `new Date()`. */
6
+ now?: Date;
7
+ }
8
+ export interface BackfillCoinbaseMaturityStats {
9
+ /** Rows visited by the scan, including any that could not be updated. */
10
+ examined: number;
11
+ /** Rows whose `matures_at_height` was written by this run. */
12
+ updated: number;
13
+ }
14
+ /**
15
+ * Stream every output joined with `transactions` where the owning transaction
16
+ * is a coinbase, `outputs.matures_at_height` is NULL, and the transaction has
17
+ * a non-null block height. For each candidate compute
18
+ * `maturesAtHeight = height + 100` and write it back in a batched UPDATE.
19
+ *
20
+ * Returns `{ examined, updated }` for telemetry. The two counts diverge only
21
+ * when concurrent writers update the same rows mid-scan; in steady state on
22
+ * a quiescent DB they are equal.
23
+ */
24
+ export declare function backfillCoinbaseMaturity(knex: Knex, args?: BackfillCoinbaseMaturityArgs): Promise<BackfillCoinbaseMaturityStats>;
25
+ //# sourceMappingURL=coinbaseMaturityBackfill.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coinbaseMaturityBackfill.d.ts","sourceRoot":"","sources":["../../../../src/storage/schema/coinbaseMaturityBackfill.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAiC3B,MAAM,WAAW,4BAA4B;IAC3C,8DAA8D;IAC9D,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,oEAAoE;IACpE,GAAG,CAAC,EAAE,IAAI,CAAA;CACX;AAED,MAAM,WAAW,6BAA6B;IAC5C,yEAAyE;IACzE,QAAQ,EAAE,MAAM,CAAA;IAChB,8DAA8D;IAC9D,OAAO,EAAE,MAAM,CAAA;CAChB;AAED;;;;;;;;;GASG;AACH,wBAAsB,wBAAwB,CAC5C,IAAI,EAAE,IAAI,EACV,IAAI,GAAE,4BAAiC,GACtC,OAAO,CAAC,6BAA6B,CAAC,CAkCxC"}
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.backfillCoinbaseMaturity = backfillCoinbaseMaturity;
4
+ /**
5
+ * §4 spendability backfill — populate `outputs.matures_at_height` for
6
+ * legacy coinbase outputs that were inserted before the new-schema column existed
7
+ * (migration `2026-05-13-001`).
8
+ *
9
+ * The §4 rule treats coinbase outputs whose `matures_at_height` is NULL as
10
+ * not-yet-mature, so any coinbase output inserted prior to the migration is
11
+ * permanently un-spendable until backfilled. This helper computes the BSV
12
+ * coinbase maturity height as `transactions.height + 100` and writes it via
13
+ * batched UPDATEs.
14
+ *
15
+ * Only rows where:
16
+ * - `transactions.is_coinbase = true`
17
+ * - `outputs.matures_at_height IS NULL`
18
+ * - `transactions.height IS NOT NULL`
19
+ * are touched. Non-coinbase outputs and outputs whose owning transaction has
20
+ * no recorded block height are left alone.
21
+ *
22
+ * Idempotent: re-running after a successful backfill examines the same set
23
+ * of rows from a fresh scan and finds none with a NULL maturity, so the
24
+ * second invocation reports `updated: 0`.
25
+ *
26
+ * Note on column naming: the database column is snake_case
27
+ * (`outputs.matures_at_height`) but is exposed in API surfaces as
28
+ * `maturesAtHeight`. This module references the DB column name directly
29
+ * because Knex passes column literals straight through to SQL.
30
+ */
31
+ const DEFAULT_BATCH_SIZE = 500;
32
+ const BSV_COINBASE_MATURITY = 100;
33
+ /**
34
+ * Stream every output joined with `transactions` where the owning transaction
35
+ * is a coinbase, `outputs.matures_at_height` is NULL, and the transaction has
36
+ * a non-null block height. For each candidate compute
37
+ * `maturesAtHeight = height + 100` and write it back in a batched UPDATE.
38
+ *
39
+ * Returns `{ examined, updated }` for telemetry. The two counts diverge only
40
+ * when concurrent writers update the same rows mid-scan; in steady state on
41
+ * a quiescent DB they are equal.
42
+ */
43
+ async function backfillCoinbaseMaturity(knex, args = {}) {
44
+ var _a, _b;
45
+ const batchSize = (_a = args.batchSize) !== null && _a !== void 0 ? _a : DEFAULT_BATCH_SIZE;
46
+ const now = (_b = args.now) !== null && _b !== void 0 ? _b : new Date();
47
+ const stats = { examined: 0, updated: 0 };
48
+ let lastOutputId = 0;
49
+ /* eslint-disable-next-line no-constant-condition */
50
+ while (true) {
51
+ const rows = await knex('outputs as o')
52
+ .join('transactions as t', 't.transactionId', 'o.transactionId')
53
+ .where('t.is_coinbase', true)
54
+ .whereNull('o.matures_at_height')
55
+ .whereNotNull('t.height')
56
+ .andWhere('o.outputId', '>', lastOutputId)
57
+ .orderBy('o.outputId')
58
+ .limit(batchSize)
59
+ .select(knex.ref('o.outputId').as('outputId'), knex.ref('t.height').as('height'));
60
+ if (rows.length === 0)
61
+ return stats;
62
+ for (const row of rows) {
63
+ lastOutputId = row.outputId;
64
+ stats.examined += 1;
65
+ const maturesAtHeight = row.height + BSV_COINBASE_MATURITY;
66
+ const written = await knex('outputs')
67
+ .where({ outputId: row.outputId })
68
+ .whereNull('matures_at_height')
69
+ .update({ matures_at_height: maturesAtHeight, updated_at: now });
70
+ if (written > 0)
71
+ stats.updated += 1;
72
+ }
73
+ }
74
+ }
75
+ //# sourceMappingURL=coinbaseMaturityBackfill.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coinbaseMaturityBackfill.js","sourceRoot":"","sources":["../../../../src/storage/schema/coinbaseMaturityBackfill.ts"],"names":[],"mappings":";;AAyDA,4DAqCC;AA5FD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,MAAM,kBAAkB,GAAG,GAAG,CAAA;AAC9B,MAAM,qBAAqB,GAAG,GAAG,CAAA;AAgBjC;;;;;;;;;GASG;AACI,KAAK,UAAU,wBAAwB,CAC5C,IAAU,EACV,OAAqC,EAAE;;IAEvC,MAAM,SAAS,GAAG,MAAA,IAAI,CAAC,SAAS,mCAAI,kBAAkB,CAAA;IACtD,MAAM,GAAG,GAAG,MAAA,IAAI,CAAC,GAAG,mCAAI,IAAI,IAAI,EAAE,CAAA;IAClC,MAAM,KAAK,GAAkC,EAAE,QAAQ,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAA;IAExE,IAAI,YAAY,GAAG,CAAC,CAAA;IACpB,oDAAoD;IACpD,OAAO,IAAI,EAAE,CAAC;QACZ,MAAM,IAAI,GAAgD,MAAM,IAAI,CAAC,cAAc,CAAC;aACjF,IAAI,CAAC,mBAAmB,EAAE,iBAAiB,EAAE,iBAAiB,CAAC;aAC/D,KAAK,CAAC,eAAe,EAAE,IAAI,CAAC;aAC5B,SAAS,CAAC,qBAAqB,CAAC;aAChC,YAAY,CAAC,UAAU,CAAC;aACxB,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE,YAAY,CAAC;aACzC,OAAO,CAAC,YAAY,CAAC;aACrB,KAAK,CAAC,SAAS,CAAC;aAChB,MAAM,CACL,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,EACrC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAC,CAClC,CAAA;QAEH,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,KAAK,CAAA;QAEnC,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,YAAY,GAAG,GAAG,CAAC,QAAQ,CAAA;YAC3B,KAAK,CAAC,QAAQ,IAAI,CAAC,CAAA;YACnB,MAAM,eAAe,GAAG,GAAG,CAAC,MAAM,GAAG,qBAAqB,CAAA;YAC1D,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC;iBAClC,KAAK,CAAC,EAAE,QAAQ,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC;iBACjC,SAAS,CAAC,mBAAmB,CAAC;iBAC9B,MAAM,CAAC,EAAE,iBAAiB,EAAE,eAAe,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC,CAAA;YAClE,IAAI,OAAO,GAAG,CAAC;gBAAE,KAAK,CAAC,OAAO,IAAI,CAAC,CAAA;QACrC,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,57 @@
1
+ import { Knex } from 'knex';
2
+ import { TableMonitorLease } from './tables';
3
+ /**
4
+ * monitor lease primitive.
5
+ *
6
+ * A `monitor_lease` row records the owner that may currently execute a named
7
+ * task. Acquisition is opportunistic: a Monitor calls `tryClaimLease()` to
8
+ * insert or take over a stale row; if it succeeds it owns the task until
9
+ * `expiresAt`. While running it calls `renewLease()` to extend the deadline.
10
+ * On clean shutdown it calls `releaseLease()`.
11
+ *
12
+ * The Knex driver expresses claim + takeover as a single conditional UPDATE
13
+ * so that two contending Monitors cannot both believe they own the task — at
14
+ * most one row write will succeed per (task, expiry) pair.
15
+ */
16
+ export interface MonitorLeaseClaim {
17
+ taskName: string;
18
+ ownerId: string;
19
+ ttlMs: number;
20
+ note?: string;
21
+ }
22
+ export interface MonitorLeaseRenew {
23
+ taskName: string;
24
+ ownerId: string;
25
+ ttlMs: number;
26
+ note?: string;
27
+ }
28
+ export interface MonitorLeaseRelease {
29
+ taskName: string;
30
+ ownerId: string;
31
+ }
32
+ export interface MonitorLeaseResult {
33
+ acquired: boolean;
34
+ lease?: TableMonitorLease;
35
+ }
36
+ /**
37
+ * Try to claim a lease on `taskName`. Succeeds iff:
38
+ * - no row exists for the task, OR
39
+ * - the existing row has `expiresAt <= now`, OR
40
+ * - the existing row's `ownerId` already equals the requested owner.
41
+ *
42
+ * On success the row is upserted with `expiresAt = now + ttlMs` and
43
+ * `renewCount = renewCount + 1` (0 for fresh rows).
44
+ */
45
+ export declare function tryClaimLease(knex: Knex, claim: MonitorLeaseClaim, now?: Date): Promise<MonitorLeaseResult>;
46
+ /**
47
+ * Extend the current owner's lease. Fails (returns `acquired: false`) when the
48
+ * row does not exist, is owned by someone else, or has already expired.
49
+ */
50
+ export declare function renewLease(knex: Knex, renew: MonitorLeaseRenew, now?: Date): Promise<MonitorLeaseResult>;
51
+ /**
52
+ * Release the lease (no-op when not owned by the caller).
53
+ * The row is deleted so subsequent claimants do not see an inherited
54
+ * `renew_count`.
55
+ */
56
+ export declare function releaseLease(knex: Knex, release: MonitorLeaseRelease): Promise<boolean>;
57
+ //# sourceMappingURL=monitorLease.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"monitorLease.d.ts","sourceRoot":"","sources":["../../../../src/storage/schema/monitorLease.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAC3B,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAA;AAE5C;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,OAAO,CAAA;IACjB,KAAK,CAAC,EAAE,iBAAiB,CAAA;CAC1B;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,iBAAiB,EACxB,GAAG,GAAE,IAAiB,GACrB,OAAO,CAAC,kBAAkB,CAAC,CAmC7B;AAED;;;GAGG;AACH,wBAAsB,UAAU,CAC9B,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,iBAAiB,EACxB,GAAG,GAAE,IAAiB,GACrB,OAAO,CAAC,kBAAkB,CAAC,CAkB7B;AAED;;;;GAIG;AACH,wBAAsB,YAAY,CAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC,CAK9F"}