@bsv/wallet-toolbox-client 2.2.0 → 2.3.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.
@@ -1,74 +1,61 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.reviewStatusIdb = reviewStatusIdb;
4
- const provenTxReqStatusesSafeForInputRestore = new Set(['invalid', 'doubleSpend']);
5
4
  /**
5
+ * Looks for unpropagated state:
6
+ *
6
7
  * 1. set transactions to 'failed' if not already failed and provenTxReq with matching txid has status of 'invalid'.
8
+ * 2. sets transactions to 'completed' if provenTx with matching txid exists and current provenTxId is null.
9
+ * 3. sets outputs to spendable true, spentBy undefined if spentBy is a transaction with status 'failed'.
10
+ *
11
+ * @param storage
12
+ * @param args
13
+ * @returns
7
14
  */
8
- async function failTransactionsForInvalidReqs(storage, r) {
15
+ async function reviewStatusIdb(storage, args) {
16
+ const r = { log: '' };
17
+ // 1. set transactions to 'failed' if not already failed and provenTxReq with matching txid has status of 'invalid'.
9
18
  const invalidTxids = [];
10
- await storage.filterProvenTxReqs({ partial: { status: 'invalid' } }, txReq => {
19
+ await storage.filterProvenTxReqs({ partial: { status: 'invalid' }, trx: args.trx }, txReq => {
11
20
  invalidTxids.push(txReq.txid);
12
21
  });
13
22
  for (const txid of invalidTxids) {
14
- const txs = await storage.findTransactions({ partial: { txid } });
23
+ const txs = await storage.findTransactions({ partial: { txid }, trx: args.trx });
15
24
  for (const tx of txs) {
16
25
  if (tx.status !== 'failed') {
17
26
  r.log += `transaction ${tx.transactionId} updated to status of 'failed' was ${tx.status}\n`;
18
- await storage.updateTransactionStatus('failed', tx.transactionId);
27
+ await storage.updateTransactionStatus('failed', tx.transactionId, undefined, undefined, args.trx);
19
28
  }
20
29
  }
21
30
  }
22
- }
23
- /**
24
- * Partitions failed transactions into all failed ids and the subset that is safe for input restore
25
- * (terminal failure: no blocking ProvenTxReq, i.e. all reqs are 'invalid'/'doubleSpend').
26
- */
27
- async function collectFailedTransactionIds(storage) {
28
- const failedTxs = await storage.findTransactions({ partial: { status: 'failed' }, noRawTx: true });
29
- const failedTransactionIds = new Set();
30
- const safeFailedTransactionIds = new Set();
31
+ // 2. sets transactions to 'completed' if provenTx with matching txid exists and current provenTxId is null.
32
+ const provenTxs = {};
33
+ await storage.filterProvenTxs({ partial: {}, trx: args.trx }, provenTx => {
34
+ provenTxs[provenTx.txid] = provenTx.provenTxId;
35
+ });
36
+ for (const [txid, provenTxId] of Object.entries(provenTxs)) {
37
+ const txs = await storage.findTransactions({ partial: { txid }, trx: args.trx });
38
+ for (const tx of txs) {
39
+ if (tx.provenTxId == null) {
40
+ r.log += `transaction ${tx.transactionId} updated to status of 'completed' with provenTxId ${provenTxId}\n`;
41
+ await storage.updateTransaction(tx.transactionId, { status: 'completed', provenTxId }, args.trx);
42
+ }
43
+ }
44
+ }
45
+ // 3. sets outputs to spendable true, spentBy undefined if spentBy is a transaction with status 'failed'.
46
+ const failedTxs = await storage.findTransactions({ partial: { status: 'failed' }, trx: args.trx });
31
47
  for (const tx of failedTxs) {
32
- failedTransactionIds.add(tx.transactionId);
33
- let hasBlockingReq = false;
48
+ let blocked = false;
34
49
  if (tx.txid != null && tx.txid !== '') {
35
- const reqs = await storage.findProvenTxReqs({ partial: { txid: tx.txid } });
36
- hasBlockingReq = reqs.some(req => !provenTxReqStatusesSafeForInputRestore.has(req.status));
50
+ const reqs = await storage.findProvenTxReqs({ partial: { txid: tx.txid }, trx: args.trx });
51
+ blocked = reqs.some(req => !['invalid', 'doubleSpend'].includes(req.status));
37
52
  }
38
- if (!hasBlockingReq)
39
- safeFailedTransactionIds.add(tx.transactionId);
40
- }
41
- return { failedTransactionIds, safeFailedTransactionIds };
42
- }
43
- /**
44
- * Looks for unpropagated state:
45
- *
46
- * 1. set transactions to 'failed' if not already failed and provenTxReq with matching txid has status of 'invalid'.
47
- * 2. sets outputs to spendable true, spentBy undefined if spentBy is a terminal failed transaction.
48
- * 3. sets outputs generated by terminal failed transactions to spendable false, spentBy undefined.
49
- *
50
- * @param storage
51
- * @param args
52
- * @returns
53
- */
54
- async function reviewStatusIdb(storage, args) {
55
- const r = { log: '' };
56
- await failTransactionsForInvalidReqs(storage, r);
57
- const { failedTransactionIds, safeFailedTransactionIds } = await collectFailedTransactionIds(storage);
58
- const outputs = await storage.findOutputs({ partial: {}, trx: args.trx });
59
- for (const output of outputs) {
60
- // An output generated by any failed tx (even one still reconcilable from an active req) is
61
- // not a valid input to restore, matching the SQL `source.status = 'failed'` guard.
62
- const generatedByFailedTx = failedTransactionIds.has(output.transactionId);
63
- const generatedBySafeFailedTx = safeFailedTransactionIds.has(output.transactionId);
64
- const spentBySafeFailedTx = output.spentBy != null && safeFailedTransactionIds.has(output.spentBy);
65
- if (spentBySafeFailedTx && !generatedByFailedTx) {
53
+ if (blocked)
54
+ continue;
55
+ const outputs = await storage.findOutputs({ partial: { userId: tx.userId, spentBy: tx.transactionId }, trx: args.trx });
56
+ for (const output of outputs) {
66
57
  await storage.updateOutput(output.outputId, { spendable: true, spentBy: undefined }, args.trx);
67
- r.log += `output ${output.outputId} updated to spendable because spentBy is failed\n`;
68
- }
69
- if (generatedBySafeFailedTx && (output.spendable || output.spentBy != null)) {
70
- await storage.updateOutput(output.outputId, { spendable: false, spentBy: undefined }, args.trx);
71
- r.log += `output ${output.outputId} updated to not spendable because transaction is failed\n`;
58
+ r.log += `output ${output.outputId} released from failed transaction ${tx.transactionId}\n`;
72
59
  }
73
60
  }
74
61
  return r;
@@ -1 +1 @@
1
- {"version":3,"file":"reviewStatusIdb.js","sourceRoot":"","sources":["../../../../../src/storage/methods/reviewStatusIdb.ts"],"names":[],"mappings":";;AAyDA,0CA8BC;AApFD,MAAM,sCAAsC,GAAG,IAAI,GAAG,CAAwB,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC,CAAA;AAEzG;;GAEG;AACH,KAAK,UAAU,8BAA8B,CAAE,OAAmB,EAAE,CAAkB;IACpF,MA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AM,CAAC,QAAQ,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,CAAA;YAC9F,CAAC,CAAC,GAAG,IAAI,UAAU,MAAM,CAAC,QAAQ,qCAAqC,EAAE,CAAC,aAAa,IAAI,CAAA;QAC7F,CAAC;IACH,CAAC;IAED,OAAO,CAAC,CAAA;AACV,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/wallet-toolbox-client",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
4
4
  "description": "Client only Wallet Storage",
5
5
  "main": "./out/src/index.client.js",
6
6
  "types": "./out/src/index.client.d.ts",
@@ -25,7 +25,7 @@
25
25
  "idb": "^8.0.2"
26
26
  },
27
27
  "peerDependencies": {
28
- "@bsv/sdk": "^2"
28
+ "@bsv/sdk": "^2.1.6"
29
29
  },
30
30
  "peerDependenciesMeta": {
31
31
  "@bsv/sdk": {