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

Refactor - Transaction Input Types #58

Open
wants to merge 41 commits into
base: develop
Choose a base branch
from

Conversation

DaMatrix
Copy link

@DaMatrix DaMatrix commented Aug 2, 2024

Description

This PR depends on #57 (tx-constructor-types), #44 (disable-p2sh), and #49 (addresses).

This replaces TxIn with an enum, where each variant contains the necessary information for a transaction input of a specific type. Each variant corresponds exactly to one of the original input script formats. Doing this brings the following advantages:

  • Substantially more compact binary serialization, as each input contains only the minimal amount of information required to reconstruct the original unlock script instead of the full script
  • Prevents accidental creation of a script with an invalid format from being created at all, which avoids accidental errors without breaking anything (because we reject every script whose format we don't recognize anyway)
  • Enforces consistent type checks across the entire codebase, instead of re-declaring the types of each field in each kind of input script separately in every method
  • Makes gross, hacky code which creates transaction inputs with empty scripts impossible
    • These have made their way into actual transaction objects in a number of tests
  • Provides a clean mechanism to support alternative transaction formats in the future, including properly separating the locking and unlocking parts of a script between transaction inputs and outputs

Unfortunately, adding any kind of additional strict input validation when constructing these values is difficult/impossible, as it's impossible to check a single input validity's without knowing the context of the entire enclosing transaction and its input transactions. However, we can at least ensure that the individual fields have legal values thanks to strict typing, and we can ensure that we can't create weird inputs with impossible structures (such as a Coinbase input containing an OutPoint).

Changelog

  • Added structs representing each type of transaction input, and an enum containing one variant for each
  • Replaced the existing TxIn struct with the new enum
  • Changed transaction_utils::construct_tx_in_out_signable_hash to accept an OutPoint instead of a TxIn
    • This function only ever used the OutPoint part of the TxIn, but because it needs to be called before a transaction input is created, there was a ton of code which had to create invalid TxIn instances with empty scripts

Type of Change

Please mark the appropriate option by putting an "x" inside the brackets:

  • Bug fix
  • New feature
  • Enhancement or optimization
  • Documentation update
  • Other (please specify)

Checklist

Put an "x" in the boxes that apply. If you're unsure about any of these, don't hesitate to ask. We're here to help!

  • I have tested the changes locally and they work as expected.
  • I have added necessary documentation or updated existing documentation.
  • My code follows the project's coding standards and style guidelines.
  • I have added/updated relevant tests to ensure the changes are properly covered.
  • I have checked for and resolved any merge conflicts.
  • My commits have clear and descriptive messages.

Screenshots (if applicable)

If the changes affect the UI or have visual effects, please provide screenshots or GIFs showcasing the changes.

Additional Context (if applicable)

Add any additional context or information about the changes that may be helpful in understanding the pull request.

Related Issues (if applicable)

If this pull request is related to any existing issues, please list them here.

Requested Reviewers

Mention any specific individuals or teams you would like to request a review from.

DaMatrix added 30 commits May 29, 2024 13:51
This will be useful when I go through and add descriptive errors to everything everywhere
This allows other structs which wrap a fixed-length byte array to simply use it as a field, and not have to deal with any of the details of formatting or JSON hex (de)serialization.
this makes it clear which serialization format is being used where, as we are going to be changing this up soon
This will enable us to keep JSON serialization separate from the binary encoding, and also be much more explicit about the exact binary representation of objects.
Also implemented PlaceholderIndexed for all crypto structs
P2SH doesn't work yet, I don't want it to exist at all for the time being
DaMatrix added 11 commits July 18, 2024 13:21
…or-types

# Conflicts:
#	src/primitives/transaction.rs
#	src/utils/script_utils.rs
#	src/utils/transaction_utils.rs
# Conflicts:
#	src/utils/transaction_utils.rs
…Point instead of a whole TxIn

The script part was never used anyway, this just resulted in a bunch of unnecessary temporary TxIns being created without a script.
# Conflicts:
#	src/primitives/transaction.rs
#	src/script/interface_ops.rs
#	src/script/lang.rs
#	src/utils/script_utils.rs
#	src/utils/transaction_utils.rs
@DaMatrix DaMatrix requested a review from a team as a code owner August 2, 2024 13:56
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