Repository navigation
Nest TransactionParseError in FFI InputPairError - #2035
Conversation
Coverage Report for CI Build 42824111686Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage remained the same at 87.825%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
|
tACK 1a9fb64
Tested: fmt, clippy ( AI-assisted review; I ran the tests and mutation myself. |
benalleng
left a comment
There was a problem hiding this comment.
Ahh I missed that, perhaps we should change that enum on line 279 and simply use it to replace the original field as a nested enum forInputPairError
caarloshenriq
left a comment
There was a problem hiding this comment.
Concept ACK 1a9fb64
The rename makes sense, and I agree with @benalleng’s suggestion to nest TransactionParseError instead of keeping two names for the same decode failure
| let non_witness_utxo: payjoin::bitcoin::Transaction = | ||
| payjoin::bitcoin::consensus::deserialize(&non_witness_utxo) | ||
| .map_err(|e| InputPairError::InvalidTransaction(e.to_string()))?; | ||
| .map_err(|e| InputPairError::TransactionDecode(e.to_string()))?; |
There was a problem hiding this comment.
+1 on nesting TransactionParseError here. There's already a decode_transaction helper at L195 that could be reused:
decode_transaction(&non_witness_utxo).map_err(InputPairError::TransactionDecode)?One thing though: decode_transaction uses consensus_decode, which accepts trailing bytes, while deserialize here rejects them (the trailing_bytes case in the test passes through the helper). If you reuse it, switching the helper to deserialize keeps this behavior and fixes the other callers too.
1a9fb64 to
0e030b9
Compare
|
Thanks @Xoulomon, @benalleng, @caarloshenriq. Reworked as suggested, now two commits:
I left AI disclosure: drafted with AI assistance (Claude Code). |
|
CI is green on 0e030b9. @benalleng ready for another look when you have time. |
codaMW
left a comment
There was a problem hiding this comment.
tACK 0e030b9
Nice unification routing new_p2pkh and both classify_tx paths through decode_transaction on consensus::deserialize makes trailing-byte rejection consistent, and nesting TransactionParseError reads cleaner than the bare string.
Verified the new test is load-bearing: reverting decode_transaction to Transaction::consensus_decode flips decode_transaction_rejects_trailing_bytes red, since consensus_decode leaves trailing bytes unconsumed. Full payjoin-ffi --lib suite green.
Non-blocking: the switch also tightens classify_tx / classify_receiver_tx to reject trailing bytes the CHANGELOG's classify_tx note covers it, maybe worth a word in the PR body too.
AI-assisted review; ran the build, test, and mutation myself.
benalleng
left a comment
There was a problem hiding this comment.
Very close! just a minor error transparency change to make it even cleaner.
| #[error("Invalid transaction: {0}")] | ||
| InvalidTransaction(String), | ||
| #[error(transparent)] | ||
| TransactionDecode(TransactionParseError), |
There was a problem hiding this comment.
| TransactionDecode(TransactionParseError), | |
| TransactionDecode(#[from] TransactionParseError), |
This will allow you to use decode_transaction(..)? instead of a .map_err()
| payjoin::bitcoin::consensus::deserialize(&non_witness_utxo) | ||
| .map_err(|e| InputPairError::InvalidTransaction(e.to_string()))?; | ||
| let non_witness_utxo = | ||
| decode_transaction(&non_witness_utxo).map_err(InputPairError::TransactionDecode)?; |
There was a problem hiding this comment.
| decode_transaction(&non_witness_utxo).map_err(InputPairError::TransactionDecode)?; | |
| decode_transaction(&non_witness_utxo)?; |
decode_transaction used consensus_decode, which stops after one transaction and ignores any bytes that follow it. InputPair::new_p2pkh decodes with consensus::deserialize instead, which rejects trailing bytes, so the same input could decode in one place and fail in another. Switch the helper to consensus::deserialize so classify_tx and classify_receiver_tx reject trailing bytes too, and so new_p2pkh can share the helper.
InputPairError::InvalidTransaction reported the same failure as TransactionParseError::InvalidTransaction (bytes that do not consensus decode) under a second name, a second message and a String payload. Replace it with InputPairError::TransactionDecode, which wraps TransactionParseError, and have InputPair::new_p2pkh decode through the shared decode_transaction helper. A decode failure now has one type and one message whether it comes from new_p2pkh or classify_tx. This changes the public FFI API, so add a payjoin-ffi changelog fragment.
0e030b9 to
015a204
Compare
|
Applied both suggestions: Also rebased onto master to resolve the CHANGELOG conflict. The entry now lives in Thanks for testing, @codaMW. The PR body already calls out that |
Closes #1974.
InputPairError::InvalidTransaction(String)reported the same failure asTransactionParseError::InvalidTransaction(bytes that don't consensus decode) under a second name and message. This PR makes them one error.Commits
decode_transactionhelper usedconsensus_decode, which ignores bytes after the transaction, whileInputPair::new_p2pkhusedconsensus::deserialize, which rejects them. The helper now usesdeserialize, soclassify_tx/classify_receiver_txreject trailing bytes too (both are still unreleased). Addsdecode_transaction_rejects_trailing_bytes, which fails against the old helper.TransactionParseErrorinInputPairError. Replaces the variant withInputPairError::TransactionDecode(#[from] TransactionParseError)(#[error(transparent)]), andnew_p2pkhnow decodes throughdecode_transaction(..)?. Same pattern as the existing nestedFfiValidation(FfiValidationError).The variant shipped in 0.25.0, so this is a breaking change for bindings that match on it, noted in
payjoin-ffi/changelog.d/input-pair-transaction-decode.md.TransactionParseErroritself is unchanged, so the C#, JS and Kotlin tests that matchTransactionParseError.InvalidTransactionfromclassify_txdon't change.Checked locally on each commit after rebasing onto master:
cargo fmt --all -- --check,cargo clippy -p payjoin-ffi --all-targets --all-features -- -D warnings,cargo test -p payjoin-ffi --all-features,contrib/release/changelog-fragments.sh payjoin-ffi/changelog.d. Also generated the Python bindings to confirm UniFFI emitsInputPairError.TransactionDecodewrappingTransactionParseError. I didn't run the full binding test suites locally.Reviewer: @benalleng
AI disclosure: the change, commit messages and this description were drafted with AI assistance (Claude Code). A local AI review of the branch was run against the AGENTS.md criteria with no findings.
Pull Request Checklist
Please confirm the following before requesting review: