Skip to content

refactor(wallet-toolbox): S3776 long-tail in storage - #101

Merged
sirdeggen merged 2 commits into
mainfrom
sonar/w5-wt-storage
May 7, 2026
Merged

refactor(wallet-toolbox): S3776 long-tail in storage#101
sirdeggen merged 2 commits into
mainfrom
sonar/w5-wt-storage

Conversation

@sirdeggen

Copy link
Copy Markdown
Contributor

Summary

Reduces Sonar S3776 cognitive complexity violations across the @bsv/wallet-toolbox storage layer. 24 flagged functions across 11 files are refactored by extracting small, focused private/module-level helpers. All function signatures, error messages, and semantics are preserved exactly.

Files changed:

  • StorageIdb.ts — 13 violations: scanCursor<T> generic helper + 4 cursor-open helpers eliminate repeated scan-loop boilerplate from all filterXxx methods
  • StorageKnex.ts — 4 violations: extracted serialisation/deserialisation helpers shared between validation methods
  • StorageProvider.ts — 2 violations: upsertProvenTxReq + handleProvenTxBranch private helpers
  • WalletStorageManager.ts — 2 violations: ensureStoreAvailable + evaluateNewMerkleLeaf private helpers
  • methods/createAction.ts — 3 violations: decomposed validateRequiredInputs (CC 54), createNewInputs (CC 24), createNewOutputs (CC 24)
  • methods/processAction.ts — 2 violations: classifyReqDetails, determineReqTxStatus, buildOutputUpdates
  • methods/listActionsKnex.ts + listActionsIdb.ts — 4 violations: per-action enrichment helpers + shared partitionActionLabels
  • methods/generateChange.ts — 2 violations: removeChurnPairs, distributeExcessFees, removeDustOutputs
  • methods/attemptToPostReqsToNetwork.ts — 2 violations: tallyTxidResults, gatherCompetingTxids
  • methods/ListActionsSpecOp.ts — shared partitionActionLabels helper

Test plan

  • tsc --build — clean, zero type errors
  • lint:ci — no new errors in changed files (pre-existing errors in unrelated utility files unchanged)
  • jest --runInBand 'storage|StorageIdb|StorageKnex' — running (tests take ~15 min)

Refs #38 #44

🤖 Generated with Claude Code

sirdeggen and others added 2 commits May 7, 2026 15:52
Reduce cognitive complexity of 24 flagged functions across the storage
layer to well below the Sonar threshold of 15.

StorageIdb.ts (13 violations):
- Extract `scanCursor<T>` generic helper — all filterXxx methods now
  delegate the scan-loop boilerplate to one place.
- Extract `openProvenTxReqsCursor`, `openCertificatesCursor`,
  `openOutputsCursor`, `openTransactionsCursor` private helpers to
  keep cursor-open logic out of each filter method.
- Extract `matchesUserPartial` private helper for filterUsers.
- Fix `openXxxCursor` parameter type to `any` to eliminate TS2345
  errors from the IDB store-specific generic types.

StorageKnex.ts (4 violations):
- Extract `normaliseKnexRawResult`, `getRawTxSlice` from
  `getRawTxOfKnownValidTransaction` (CC 19→~8).
- Extract `serialiseForKnex`, `coerceDateFields`, `coerceBooleanFields`
  shared by both `validatePartialForUpdate` and `validateEntityForInsert`.
- Extract `deserialiseFromKnex<T>` from `validateEntity` (CC 16→~7).

StorageProvider.ts (2 violations):
- Extract `upsertProvenTxReq` private helper from `getProvenOrReq`.
- Extract `handleProvenTxBranch` private helper from
  `getValidBeefForTxid` (CC 27→~9).

WalletStorageManager.ts (2 violations):
- Extract `ensureStoreAvailable` and `selectActiveFromStore` from
  `makeAvailable` (CC 17→~8).
- Extract `evaluateNewMerkleLeaf` private helper from `reproveProven`
  (CC 21→~9). Fix imports: add `MerklePath`, `ChainTracker` from
  `@bsv/sdk` directly (not available via sdk namespace).

createAction.ts (3 violations):
- Extract `markKnownInputsSpent`, `buildSdkInputFromOutput`,
  `buildSdkInputFromXInput` from `createNewInputs` (CC 24→~9).
- Extract `randomizeOutputVouts`, `persistNewOutput` from
  `createNewOutputs` (CC 24→~9).
- Extract `validateBeefTxidOnlyEntries`, `ensureBeefContainsAllInputTxids`,
  `resolveInputScript` from `validateRequiredInputs` (CC 54→~12).

processAction.ts (2 violations):
- Extract `classifyReqDetails`, `verifyMergedBeef` from
  `shareReqsWithWorld` (CC 17→~8).
- Extract `determineReqTxStatus`, `buildOutputUpdates` from
  `validateCommitNewTxToStorageArgs` (CC 29→~10).

listActionsKnex.ts (2 violations):
- Extract `enrichActionLabels`, `enrichActionOutputs`, `enrichActionInputs`
  from the per-tx map callback (CC 36→~5 each).
- Use shared `partitionActionLabels` from ListActionsSpecOp.ts.

listActionsIdb.ts (2 violations):
- Extract `enrichIdbActionLabels`, `enrichIdbActionOutputs`,
  `enrichIdbActionInputs` from the per-tx map callback (CC 36→~5 each).
- Use shared `partitionActionLabels` from ListActionsSpecOp.ts.

generateChange.ts (2 violations):
- Extract `removeChurnPairs`, `distributeExcessFees`, `removeDustOutputs`
  from `generateChangeSdk`/`fundTransaction` (CC 29/19→~12/~9).

attemptToPostReqsToNetwork.ts (2 violations):
- Extract `tallyTxidResults` from `aggregatePostBeefResultsByTxid`
  (CC 27→~8).
- Extract `gatherCompetingTxids` from `confirmDoubleSpend` (CC 30→~9).

ListActionsSpecOp.ts:
- Add `partitionActionLabels` shared helper used by both listActions files.

Refs #38 #44.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
W5 cognitive-complexity refactor extracted enrichIdbActionLabels/Outputs/Inputs
but broke the actions return path. The real root cause was that scanCursor,
introduced as a private class method, was being intercepted by wrapProfiling
in idbSpeed.test.ts. wrapProfiling wraps all prototype methods and mangles
multi-function-argument calls: it replaces the last function arg (accept) with
a wrapper that calls the first function arg (matches), so accept is never called
and no records are collected.

Fix: move scanCursor from a private class method to a module-level helper
function so it is not discoverable by prototype-walking instrumentation.
@codecov

codecov Bot commented May 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@sonarqubecloud

sonarqubecloud Bot commented May 7, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)
8.4% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@sirdeggen
sirdeggen merged commit 7cb1c77 into main May 7, 2026
8 of 9 checks passed
@sirdeggen
sirdeggen deleted the sonar/w5-wt-storage branch June 16, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant