Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Weight Calculation Functionalities and General Optimizations in Descriptors #32

Merged
merged 6 commits into from
Dec 9, 2023

Conversation

landabaso
Copy link
Member

This Pull Request introduces a series of enhancements and optimizations to the descriptor methods in the @bitcoinerlab/descriptors package. Key updates include the addition of new methods for weight calculation, improvements in memoization, and updates to dependencies and configurations. These changes are aimed at improving the performance and functionality of the module, especially in scenarios involving coinselector algorithms.

Key Changes:

  1. New Descriptor Methods:

    • guessOutput: Identifies the output type (PKH, WPKH, SH) based on a given script.
    • inputWeight: Calculates the Weight Unit contributions of an Output as an input in a transaction.
    • outputWeight: Computes the Weight Unit contributions of an Output as an output in a transaction.
  2. Memoization Implementation:

    • Applied memoization to guessOutput, getSequence, getLockTime, getScriptSatisfaction, inputWeight, and outputWeight for enhanced performance and reduced redundant computations.
  3. Behavioral Changes:

    • The isSegwit() method now assumes addr(SH_TYPE_ADDRESS) descriptors as Segwit SH_WPKH. For non-standard script inputs, the format sh(MINISCRIPT) should be used.
  4. Dependency and Configuration Updates:

    • Updated package version from "2.0.4" to "2.1.0".
    • Modified the build:test script to include --resolveJsonModule.
    • Added lodash.memoize for memoization functionality.
    • Included varuint-bitcoin explicitelly.
    • Enhanced TypeScript configuration to allow importing JSON files, supporting new test fixtures.
  5. Additional Improvements:

    • Fixed formatting issues with Prettier.
    • Utilized explicit varuint-bitcoin functions from the official npm package.
  6. Testing:

    • Added comprehensive tests for the new methods: inputWeight, outputWeight, and guessOutput.

Rationale for Changes:
The relocation of guessOutput, inputWeight, and outputWeight from @bitcoinerlab/coinselect to @bitcoinerlab/descriptors aligns these methods more closely with the module's purpose. The implementation of a caching strategy and memoization significantly boosts performance, particularly for algorithms that frequently access these weight calculation methods.

Documentation:

  • Updated code comments and documentation to reflect the new logic and changes made.

- New Methods Added:
  - `guessOutput`: Determines the output type (PKH, WPKH, SH) for a given script.
  - `inputWeight`: Calculates the Weight Unit contributions of an Output as an input in a transaction.
  - `outputWeight`: Computes the Weight Unit contributions of an Output as an output in a transaction.

- Memoization Applied:
  Memoization has been implemented for the following methods to enhance performance and reduce redundant computations:
  - `guessOutput`
  - `getSequence`
  - `getLockTime`
  - `getScriptSatisfaction`
  - `inputWeight`
  - `outputWeight`

- Changes in Behavior:
  - `isSegwit()` now assumes `addr(SH_TYPE_ADDRESS)` descriptors as Segwit `SH_WPKH`. For non-standard script inputs, the format `sh(MINISCRIPT)` should be used.

- Rationale for Changes:
  guessOutput, inputWeight and outputWeight, previously part of `@bitcoinerlab/coinselect`, have been relocated to better align with the module's purpose. Centralizing these methods and implementing a caching strategy significantly improves performance, especially beneficial for the `coinselector` algorithms that frequently access `inputWeight` and `outputWeight`.

- Dependencies Added:
  - Imported `lodash.memoize` for memoization.
  - Imported `encodingLength` from `varuint-bitcoin` for accurate size calculations.

- Other Improvements:
  - Code comments and documentation have been updated for better clarity and to reflect the new logic.
- Version Bumped:
  - Updated from "2.0.4" to "2.1.0" to signify new features and optimizations.

- TypeScript Configuration:
  - Modified the `build:test` script to include `--resolveJsonModule` for handling of JSON modules, necessary for new vsize tests.

- Dependency Additions:
  - Added `@types/lodash.memoize` (version "^4.1.9") as a development dependency for improved TypeScript support of lodash.memoize.
  - Included `lodash.memoize` (version "^4.1.2") and `varuint-bitcoin` (version "^1.1.2") as runtime dependencies to support the new descriptor methods and optimizations.
@landabaso landabaso merged commit 0511e64 into main Dec 9, 2023
landabaso added a commit to bitcoinerlab/coinselect that referenced this pull request Dec 9, 2023
This update ensure that `@bitcoinerlab/coinselect` takes full advantage of the recent improvements in the `@bitcoinerlab/descriptors` package, particularly the new weight calculation methods. This alignment enhances the performance of coin selection and transaction size estimation since it now relies on smarter caching strategies implemented in `@bitcoinerlab/descriptors`.

It aligns with the recent enhancements and new functionalities in weight calculation methods introduced in `@bitcoinerlab/descriptors` ([PR #32](bitcoinerlab/descriptors#32)).

**Key Changes:**
1. **Package Version and Dependencies:**
   - Upgraded `@bitcoinerlab/coinselect` from version `1.1.0` to `1.2.0`.
   - Updated the dependency on `@bitcoinerlab/descriptors` to version `2.1.0` to leverage the latest enhancements.

2. **Code Refactoring:**
   - Removed local implementations of weight calculations (`inputWeight` and `outputWeight`) and replaced them with the updated methods from `@bitcoinerlab/descriptors`.
   - Simplified and streamlined the code by removing redundant functions and logic, relying on the improved functionalities in the `descriptors` package.

3. **Validation and Dust Calculation Adjustments:**
   - Updated the validation logic to incorporate changes in dust calculation and fee rate validation.
   - Ensured compatibility with the new weight calculation methods in handling UTXOs and transaction outputs.

4. **Test Suite Enhancements:**
   - Revised test cases to align with the updated logic and methods.

Updates and modifications have been appropriately commented and documented within the code.
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