Skip to content

Conversation

@Velnbur
Copy link
Contributor

@Velnbur Velnbur commented Nov 1, 2025

Overview

Add hex-conservative as a dependency and refactor hex parsing code to use it instead of the custom implementation. Update FromHex trait to use associated error types and standardize hex decoding across the codebase.

Closes: #258

@Velnbur Velnbur marked this pull request as draft November 1, 2025 20:21
Comment on lines +58 to +61
/// Hex fixed parsing errors
HexFixedError(DecodeFixedLengthBytesError),
/// Hex variable parsing errors
HexVariableError(DecodeVariableLengthBytesError),
Copy link
Contributor Author

@Velnbur Velnbur Nov 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really sure if it's okay to keep both this way. We can alternatively keep only one variant as before HexError(), add hex::Error which is merge of DecodeFixedLengthBytesError and DecodeVariableLengthBytesError and add From implementations

@Velnbur Velnbur marked this pull request as ready for review November 2, 2025 09:17
actual-serde = { package = "serde", version = "1.0.103", features = [
"derive",
], optional = true }
hex-conservative = "1.0.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Velnbur thanks for the PR, could you please regenerate the Cargo.lock file and include it as Cargo-latest.lock ?

@delta1
Copy link
Member

delta1 commented Nov 3, 2025

Please run cargo test, I'm seeing 2 test failures

failures:

---- pset::tests::single_blinded_output_pset stdout ----

thread 'pset::tests::single_blinded_output_pset' panicked at src/pset/mod.rs:863:54:
called `Result::unwrap()` on an `Err` value: SurjectionProofVerificationError(0)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- blind::tests::test_blind_tx stdout ----

thread 'blind::tests::test_blind_tx' panicked at src/blind.rs:1474:55:
called `Result::unwrap()` on an `Err` value: SurjectionProofVerificationError(0)

@apoelstra
Copy link
Member

This is clearly a LLM and it's not even running the tests. Let's not waste our time on this.

@apoelstra apoelstra closed this Nov 3, 2025
@Velnbur
Copy link
Contributor Author

Velnbur commented Nov 3, 2025

Fixed tests, missed a part that blinding factors have reverse order of bytes. And yeah, I forgot to run tests as in corner of my mind I relied on CI which is the real source of truth that checks different versions of Rust, and was expecting to receive a notification from it where I broke something (also the fact that you store binaries of Elements and Bitcoin nodes in repo is really confusing). So, totally my fault

This is clearly a LLM

What made you think so? I made this changes without any AI tools. Except commit messages, I use automation tools to write them, shouldn't be a sin nowadays . May be some words like "for more robust hexadecimal encoding/decoding" sound AI-ish, kind of understand that

Also that's really mean that you just closed this PR without any time for me to response. I'm in different timezone, doing this in my free time and have relatively often electricity (and so network) shortages.

@apoelstra
Copy link
Member

What made you think so? I made this changes without any AI tools. Except commit messages, I use automation tools to write them, shouldn't be a sin nowadays . May be some words like "for more robust hexadecimal encoding/decoding" sound AI-ish, kind of understand that

I apologize -- and for what it's worth, I don't mind the use of AI tools or even majority-LLM-authored PRs. What's a problem is that Github is now full of unsupervised agents which open broken PRs and then respond to any comments or requests by closing their PR and moving on. It's extremely irksome to be scammed by a machine into doing pointless work. (There are some technical limitations keeping us on Github but I hope to abandon it entirely within the next year or two because of this.)

For future reference -- what triggered me was mostly the "Overview" heading in your PR comment, which looks like LLM output (humans don't usually write PR descriptions in structured markdown), combined with the unit tests not passing (though humans do this all the time :P).

@Velnbur
Copy link
Contributor Author

Velnbur commented Nov 4, 2025

It's fine, I understand the issue now. Thanks for feedback.

"Overview" heading in your PR comment

Seems that I've learned that from LLMs :) . Originally I planned to describe there alternative approaches how to handle different errors from hex_conservative crate but from rust-bitcoin/bitcoin_hashes#124 it seems that to add associated Error type was actually the goal.

Also, what we would do with this PR? Do I need to recreate it or you can do that?

@delta1 delta1 reopened this Nov 4, 2025
@delta1
Copy link
Member

delta1 commented Nov 4, 2025

Hi @Velnbur reopened, please address feedback

@Velnbur
Copy link
Contributor Author

Velnbur commented Nov 4, 2025

@delta1 Check please latest commits, I added Cargo-latest.lock and tests are passing (at least those that start with cargo test) as you asked. Am I missing something? My apologies if so

@Velnbur
Copy link
Contributor Author

Velnbur commented Nov 4, 2025

Okay, I see tests failing, working on it

Comment on lines +248 to +249
where
<B as FromHex>::Err: std::fmt::Display,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added bounds here for associated Err type, as compiler suggested

@Velnbur
Copy link
Contributor Author

Velnbur commented Nov 4, 2025

@delta1 I finally regenerated it correctly, sorry for inconveniences :)

@apoelstra
Copy link
Member

Can you please squash/rebase these commits so that they compile by themselves? You need to update the lockfile in the same commit as you change Cargo.toml. Also, please don't make changes in one commit which are reverted in another commit.

Also, please don't update unrelated dependencies. You can do that in a separate PR if you want but it will require separate review.

Add `hex-conservative` crate as a dependency for more robust
hexadecimal encoding/decoding. Replace custom hex parsing logic with
the library's implementation across the codebase. Update error types
to use `hex-conservative`'s error variants instead of custom `hex::Error`.
Implement `TweakHexDecodeError` to handle hex and tweak validation
errors for blinding factors.

Add Display bound to FromHex error type in serde_util.
@Velnbur
Copy link
Contributor Author

Velnbur commented Nov 4, 2025

Got it, done! Squashed commits

Also, please don't update unrelated dependencies

My bad, created Cargo.lock from scratch, not from from existing Cargo-latest.lock

}

#[doc(hidden)]
impl From<hex_conservative::DecodeFixedLengthBytesError> for TweakHexDecodeError {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 775d7cd:

We don't have to fix this here (it's a problem throughout the crate) but I would like to get rid of these From impls since they increase the API surface and encourage unthinking error promotion.

impl std::error::Error for TweakHexDecodeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
TweakHexDecodeError::InvalidHex(err) => Some(err),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 775d7cd:

Also don't need to fix it here but we should use Self:: instead of writing TweakHexDecodeError:: all over the place.

}
}

impl hex::FromHex for AssetBlindingFactor {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 775d7cd:

I don't think we should keep the FromHex trait. Where is it used? Can we replace it with FromStr or with a special-purpose utility trait?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, it's used for the serde hex_bytes module.

It seems to me that we can replace it with FromStr, introduce a sealed trait if we really want to limit its scope (but do we? I don't think so), and then we can drop hex-conservative 0.2 entirely.

Copy link
Contributor Author

@Velnbur Velnbur Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't than all fmt::Display implementations use hex as well? Asking cause it isn't so for Script at least:

fmt::Debug::fmt(self, f)

@apoelstra
Copy link
Member

Shouldn't than all fmt::Display implementations use hex as well? Asking cause it isn't so for Script at least:

Ohh, right, hex-conservative 1.0 doesn't have encoding yet.

Ok, I guess we're stuck with 0.x for now. This PR is good as-is, given the circumstance.

Thanks for your patience and continued iterating!

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 775d7cd; successfully ran local tests

@apoelstra apoelstra merged commit 646f317 into ElementsProject:master Nov 4, 2025
8 checks passed
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.

std::error::Error implementation for hex::Error

3 participants