Skip to content

sv2: use a one-byte length for STR0_255, B0_255 and SEQ0_255 - #166

Merged
Sjors merged 2 commits into
stratum-mining:masterfrom
xyephy:2026/11/sv2-short-lengths
Nov 12, 2026
Merged

Sjors merged 2 commits into
stratum-mining:masterfrom
xyephy:2026/11/sv2-short-lengths

Conversation

@xyephy

@xyephy xyephy commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

The spec encodes STR0_255 and B0_255 as a one-byte length followed by the bytes, and SEQ0_255 as a one-byte count. sv2-tp used Bitcoin's default serialization, which writes a CompactSize length. The two agree up to 252 and differ from 253 up.

The first commit fixes the receive side. A SetupConnection with a valid 253-255 byte string (vendor, firmware, etc.) failed to parse, and sv2-tp disconnected the client with "Received invalid SetupConnection message". It adds a message test for 252-255 byte strings and a connman test that sends a 255-byte vendor and expects SetupConnection.Success. The sv2_setup_connection fuzz target wrote its input with CompactSize too, so it couldn't catch this; it now uses the spec encoding.

The second commit does the same for the fields sv2-tp sends: the SetupConnection.Error and RequestTransactionData.Error error codes, the coinbase prefix, and the merkle path. The output bytes don't change for any value sv2-tp sends today. Values over 255 now throw instead of producing an invalid message. It also drops the merkle path comment, which wrongly described the encoding as VARINT.

The spec encodes STR0_255 with a one-byte length, but SetupConnection
was read with Bitcoin's CompactSize length. The two agree up to 252
bytes, so a valid 253-255 byte vendor, firmware or other string made
sv2-tp reject the message and disconnect the client.
@xyephy
xyephy marked this pull request as draft November 12, 2026 11:25
@Sjors

Sjors commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Nice catch! Why is it draft?

@xyephy
xyephy marked this pull request as ready for review November 12, 2026 12:20
@xyephy

xyephy commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Nice catch! Why is it draft?

was waiting on some tests, now its good to go.

Comment thread src/sv2/messages.h
Comment on lines -380 to -381
// Technically using VARINT is a problem for a merkle path longer
// than 127 elements, but a block can't have enough transactions to

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

My original comment here was wrong, as I was confusing VARINT with CompactSize.

@Sjors Sjors left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ACK b5ed334

Just one suggestion.

Comment thread src/sv2/messages.h Resolved
Error codes, the coinbase prefix and the merkle path were also written
with a CompactSize length. The bytes are the same for every value
sv2-tp sends today, so this only aligns the code with the spec. Values
over 255 now throw instead of producing an invalid message.
@xyephy
xyephy force-pushed the 2026/11/sv2-short-lengths branch from b5ed334 to 6512db0 Compare November 12, 2026 13:27
@Sjors

Sjors commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

ACK 6512db0

@Sjors
Sjors merged commit f88a306 into stratum-mining:master Nov 12, 2026
25 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.

2 participants