Skip to content

Conversation

@tcharding
Copy link
Member

@tcharding tcharding commented Oct 7, 2025

First remove usage of BytesEncoder::with_length_prefix when encoding a script. Then remove the constructor and the length prefix all together from the BytesEncoder.

This is based on discussion in #5097, as suggested originally by jrakibi, and is a cleaner alternative to my first effort in #5098 (which tried to remove the other constructor).

The idea here is to have only one constructor per encoder so an encoders usage is unambiguous.

Instead of having the length prefix handled by the `BytesEncoder` we
can instead use an `Encoder2` to make the presences of the prefix
explicit.

With this applied there are no more usages of the
`BytesEncoder::with_length_prefix` constructor. It will be removed in
a separate patch.
Users can use an `Encoder2` to achive this at the cost of a little
more code with the benefit of being more explicit and hence clearer.
@apoelstra
Copy link
Member

Nice. Like to see all this red.

The idea here is to have only one constructor per encoder so an encoders usage is unambiguous.

I would say "the idea is to remove unnecessary/redundant constructors to give us maximum API flexibility for 1.0". But ok, we agree on the code.

// Should have length prefix chunk, then data chunk, then exhausted.
let obj = [1u8, 2, 3];
let test_bytes = TestBytes(&obj, true);
let mut encoder = test_bytes.encoder();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can still keep this test and use composition

let mut encoder =
    Encoder2::new(CompactSizeEncoder::new(obj.len() as u64), BytesEncoder::without_length_prefix(&obj));

Comment on lines -450 to -451
let enc1 = TestBytes(&[0xAA, 0xBB], false).encoder();
let enc2 = TestBytes(&[0xCC], true).encoder();
Copy link
Contributor

Choose a reason for hiding this comment

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

can be replaced with

    let enc1 = TestBytes(&[0xAA, 0xBB]).encoder();
    let enc2 = Encoder2::new(CompactSizeEncoder::new(1u64), BytesEncoder::without_length_prefix(&[0xCC]));

Comment on lines -368 to -369
let test_bytes = TestBytes(&obj, true);
let mut encoder = test_bytes.encoder();
Copy link
Contributor

Choose a reason for hiding this comment

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

same here we can use BytesEncoder composition with CompactSizeEncoder

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 c9c41f7; successfully ran local tests

@tcharding
Copy link
Member Author

hmm, I'm inclined to just clobber the tests even though you are correct @jrakibi. @apoelstra do you have an opinion? Or maybe I should just close this and get out of you way @jrakibi, you seem to have this under control and it was your idea first anyways.

@tcharding
Copy link
Member Author

After reading #5090 (comment) I'm going to update this with 'please merge now' with my usual 'we can re-visit the test code during the RC cycle'.

@jrakibi
Copy link
Contributor

jrakibi commented Oct 9, 2025

ACK c9c41f7

@apoelstra apoelstra merged commit aa0b02a into rust-bitcoin:master Oct 9, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants