refactor(wallet-toolbox): S3776 long-tail in storage - #101
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
This was referenced Jul 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Reduces Sonar S3776 cognitive complexity violations across the
@bsv/wallet-toolboxstorage 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 allfilterXxxmethodsStorageKnex.ts— 4 violations: extracted serialisation/deserialisation helpers shared between validation methodsStorageProvider.ts— 2 violations:upsertProvenTxReq+handleProvenTxBranchprivate helpersWalletStorageManager.ts— 2 violations:ensureStoreAvailable+evaluateNewMerkleLeafprivate helpersmethods/createAction.ts— 3 violations: decomposedvalidateRequiredInputs(CC 54),createNewInputs(CC 24),createNewOutputs(CC 24)methods/processAction.ts— 2 violations:classifyReqDetails,determineReqTxStatus,buildOutputUpdatesmethods/listActionsKnex.ts+listActionsIdb.ts— 4 violations: per-action enrichment helpers + sharedpartitionActionLabelsmethods/generateChange.ts— 2 violations:removeChurnPairs,distributeExcessFees,removeDustOutputsmethods/attemptToPostReqsToNetwork.ts— 2 violations:tallyTxidResults,gatherCompetingTxidsmethods/ListActionsSpecOp.ts— sharedpartitionActionLabelshelperTest plan
tsc --build— clean, zero type errorslint: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