Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/sv2/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
node::Sv2NewTemplateMsg::Sv2NewTemplateMsg(const CBlockHeader& header, const node::CoinbaseTx coinbase, std::vector<uint256> coinbase_merkle_path, uint64_t template_id, bool future_template)
: m_template_id{template_id}, m_future_template{future_template}
{
Assert(coinbase_merkle_path.size() <= 255);

m_version = header.nVersion;

m_coinbase_tx_version = coinbase.version;
Expand Down
58 changes: 46 additions & 12 deletions src/sv2/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,43 @@ inline constexpr uint16_t TEMPLATE_DISTRIBUTION_MAX_VERSION{2};
*/
using u24_t = uint8_t[3];

/**
* Formatter for the STR0_255 and B0_255 types: a one-byte length followed by
* that many bytes. Bitcoin's default string serialization uses a CompactSize
* length instead, which differs from 253 bytes up.
*/
struct Sv2Bytes255Formatter
{
template <typename Stream, typename V>
void Ser(Stream& s, const V& v)
{
if (v.size() > 255) throw std::ios_base::failure("B0_255 length limit exceeded");
s << static_cast<uint8_t>(v.size());
if (!v.empty()) s.write(MakeByteSpan(v));
}

template <typename Stream, typename V>
void Unser(Stream& s, V& v)
{
uint8_t size;
s >> size;
v.resize(size);
if (size != 0) s.read(MakeWritableByteSpan(v));
}
};

/** Formatter for the SEQ0_255 type: a one-byte element count followed by the elements. */
struct Sv2Seq255Formatter
{
template <typename Stream, typename V>
void Ser(Stream& s, const V& v)
{
if (v.size() > 255) throw std::ios_base::failure("SEQ0_255 length limit exceeded");
s << static_cast<uint8_t>(v.size());
for (const auto& elem : v) s << elem;
}
};

/**
* All the stratum v2 message types handled by the template provider.
*/
Expand Down Expand Up @@ -130,12 +167,12 @@ struct Sv2SetupConnectionMsg
>> m_min_version
>> m_max_version
>> m_required_flags
>> m_endpoint_host
>> Using<Sv2Bytes255Formatter>(m_endpoint_host)
>> m_endpoint_port
>> m_vendor
>> m_hardware_version
>> m_firmware
>> m_device_id;
>> Using<Sv2Bytes255Formatter>(m_vendor)
>> Using<Sv2Bytes255Formatter>(m_hardware_version)
>> Using<Sv2Bytes255Formatter>(m_firmware)
>> Using<Sv2Bytes255Formatter>(m_device_id);
}
};

Expand Down Expand Up @@ -239,7 +276,7 @@ struct Sv2SetupConnectionErrorMsg
void Serialize(Stream& s) const
{
s << m_flags
<< m_error_code;
<< Using<Sv2Bytes255Formatter>(m_error_code);
}
};

Expand Down Expand Up @@ -328,7 +365,7 @@ struct Sv2NewTemplateMsg
<< m_future_template
<< m_version
<< m_coinbase_tx_version
<< m_coinbase_prefix
<< Using<Sv2Bytes255Formatter>(m_coinbase_prefix)
<< m_coinbase_tx_input_sequence
<< m_coinbase_tx_value_remaining
<< m_coinbase_tx_outputs_count;
Expand All @@ -352,10 +389,7 @@ struct Sv2NewTemplateMsg
}

s << m_coinbase_tx_locktime
// Technically using VARINT is a problem for a merkle path longer
// than 127 elements, but a block can't have enough transactions to
Comment on lines -355 to -356

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.

// run into that.
<< m_merkle_path;
<< Using<Sv2Seq255Formatter>(m_merkle_path);
Comment thread
xyephy marked this conversation as resolved.
Resolved
}
};

Expand Down Expand Up @@ -530,7 +564,7 @@ struct Sv2RequestTransactionDataErrorMsg
void Serialize(Stream& s) const
{
s << m_template_id
<< m_error_code;
<< Using<Sv2Bytes255Formatter>(m_error_code);
}
};

Expand Down
11 changes: 7 additions & 4 deletions src/test/fuzz/sv2_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ FUZZ_TARGET(sv2_setup_connection, .init = Sv2FuzzInitialize)
std::string device_id = FuzzedString(provider);

ss << protocol << min_version << max_version << flags
<< endpoint_host << endpoint_port
<< vendor << hardware_version << firmware << device_id;
<< Using<node::Sv2Bytes255Formatter>(endpoint_host) << endpoint_port
<< Using<node::Sv2Bytes255Formatter>(vendor)
<< Using<node::Sv2Bytes255Formatter>(hardware_version)
<< Using<node::Sv2Bytes255Formatter>(firmware)
<< Using<node::Sv2Bytes255Formatter>(device_id);

Sv2SetupConnectionMsg msg;
ss >> msg;
Expand Down Expand Up @@ -268,7 +271,7 @@ FUZZ_TARGET(sv2_setup_connection_error, .init = Sv2FuzzInitialize)
// Roundtrip: read back serialized fields and verify
uint32_t rt_flags;
std::string rt_error;
ss >> rt_flags >> rt_error;
ss >> rt_flags >> Using<node::Sv2Bytes255Formatter>(rt_error);
assert(rt_flags == flags);
assert(rt_error == error_code_expect);
}
Expand Down Expand Up @@ -362,7 +365,7 @@ FUZZ_TARGET(sv2_request_transaction_data_error, .init = Sv2FuzzInitialize)
// Roundtrip: read back serialized fields and verify
uint64_t rt_template_id;
std::string rt_error;
ss >> rt_template_id >> rt_error;
ss >> rt_template_id >> Using<node::Sv2Bytes255Formatter>(rt_error);
assert(rt_template_id == template_id);
assert(rt_error == error_code_expect);
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/sv2_connman_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,26 @@ BOOST_AUTO_TEST_CASE(send_error_removes_client_with_queued_messages)
BOOST_CHECK(tester.WaitForClientCount(0));
}

// STR0_255 fields have a one-byte length, so a 255-byte vendor string is valid.
BOOST_AUTO_TEST_CASE(setup_connection_with_255_byte_string)
{
ConnTester tester{};
tester.handshake();

DataStream ss{};
ss << node::TEMPLATE_DISTRIBUTION_PROTOCOL << uint16_t{2} << uint16_t{2} << uint32_t{0};
ss << uint8_t{0} << uint16_t{0};
ss << uint8_t{255};
ss.write(MakeByteSpan(std::string(255, 'v')));
ss << uint8_t{0} << uint8_t{0} << uint8_t{0};
std::vector<uint8_t> bytes(ss.size());
ss >> MakeWritableByteSpan(bytes);

node::Sv2NetMsg setup{node::Sv2MsgType::SETUP_CONNECTION, std::move(bytes)};
tester.RemoteToLocalMsg(setup);
auto [response, response_bytes]{tester.LocalToRemoteMsg()};
BOOST_CHECK(response.m_msg_type == node::Sv2MsgType::SETUP_CONNECTION_SUCCESS);
BOOST_CHECK(tester.IsFullyConnected());
}

BOOST_AUTO_TEST_SUITE_END()
54 changes: 54 additions & 0 deletions src/test/sv2_messages_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,58 @@ BOOST_AUTO_TEST_CASE(Sv2SubmitSolution_test)
BOOST_CHECK_EQUAL(submit_solution.m_ntime, 1661877399);
BOOST_CHECK_EQUAL(submit_solution.m_nonce, 58720255);
}

BOOST_AUTO_TEST_CASE(Sv2SetupConnection_long_strings_test)
{
for (const size_t len : {252, 253, 254, 255}) {
DataStream ss{};
ss << uint8_t{0} << uint16_t{2} << uint16_t{2} << uint32_t{0};
ss << uint8_t{0}; // endpoint_host
ss << uint16_t{0}; // endpoint_port
ss << uint8_t(len); // vendor
ss.write(MakeByteSpan(std::string(len, 'v')));
ss << uint8_t{0} << uint8_t{0}; // hardware_version, firmware
ss << uint8_t{3}; // device_id
ss.write(MakeByteSpan(std::string{"dev"}));

node::Sv2SetupConnectionMsg setup_conn;
BOOST_CHECK_NO_THROW(ss >> setup_conn);
BOOST_CHECK_EQUAL(setup_conn.m_vendor.size(), len);
BOOST_CHECK_EQUAL(setup_conn.m_device_id, "dev");
BOOST_CHECK(ss.empty());
}

// A length byte announcing more data than the stream holds.
DataStream short_ss{};
short_ss << uint8_t{255};
short_ss.write(MakeByteSpan(std::string(254, 'x')));
std::string str;
BOOST_CHECK_THROW(short_ss >> Using<node::Sv2Bytes255Formatter>(str), std::ios_base::failure);
}

BOOST_AUTO_TEST_CASE(Sv2SetupConnectionError_long_error_code_test)
{
for (const size_t len : {252, 253, 255}) {
node::Sv2SetupConnectionErrorMsg msg{0, std::string(len, 'e')};
DataStream ss{};
ss << msg;
BOOST_CHECK_EQUAL(ss.size(), 4 + 1 + len);
BOOST_CHECK_EQUAL(uint8_t(ss[4]), len);
}
node::Sv2SetupConnectionErrorMsg msg{0, std::string(256, 'e')};
DataStream ss{};
BOOST_CHECK_THROW(ss << msg, std::ios_base::failure);
}

BOOST_AUTO_TEST_CASE(Sv2_length_limit_test)
{
DataStream ss{};
const std::vector<uint8_t> bytes(256);
BOOST_CHECK_THROW(ss << Using<node::Sv2Bytes255Formatter>(CScript(bytes.begin(), bytes.end())), std::ios_base::failure);
BOOST_CHECK_THROW(ss << Using<node::Sv2Seq255Formatter>(std::vector<uint256>(256)), std::ios_base::failure);

ss << Using<node::Sv2Seq255Formatter>(std::vector<uint256>(255));
BOOST_CHECK_EQUAL(ss.size(), 1 + 255 * 32);
BOOST_CHECK_EQUAL(uint8_t(ss[0]), 255);
}
BOOST_AUTO_TEST_SUITE_END()
Loading