From 23db67603c815875ffc2d60339b177173b45f633 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 16 Dec 2024 12:11:08 +0700 Subject: [PATCH 1/3] Rename CoinbaseOutputDataSize --- src/sv2/connman.cpp | 8 ++++---- src/sv2/connman.h | 2 +- src/sv2/messages.h | 8 ++++---- src/test/sv2_connman_tests.cpp | 2 +- src/test/sv2_template_provider_tests.cpp | 4 ++-- src/test/sv2_transport_tests.cpp | 8 ++++---- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/sv2/connman.cpp b/src/sv2/connman.cpp index 4b65634803726..f8246f31b5898 100644 --- a/src/sv2/connman.cpp +++ b/src/sv2/connman.cpp @@ -309,7 +309,7 @@ void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& clie break; } - case Sv2MsgType::COINBASE_OUTPUT_DATA_SIZE: + case Sv2MsgType::COINBASE_OUTPUT_CONSTRAINTS: { if (!client.m_setup_connection_confirmed) { CloseConnection(client.m_id); @@ -317,12 +317,12 @@ void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& clie return; } - node::Sv2CoinbaseOutputDataSizeMsg coinbase_output_data_size; + node::Sv2CoinbaseOutputConstraintsMsg coinbase_output_data_size; try { ss >> coinbase_output_data_size; client.m_coinbase_output_data_size_recv = true; } catch (const std::exception& e) { - LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Received invalid CoinbaseOutputDataSize message from client id=%zu: %s\n", + LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Received invalid CoinbaseOutputConstraints message from client id=%zu: %s\n", client.m_id, e.what()); CloseConnection(client.m_id); client.m_disconnect_flag = true; @@ -333,7 +333,7 @@ void Sv2Connman::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Client& clie LogPrintLevel(BCLog::SV2, BCLog::Level::Debug, "coinbase_output_max_additional_size=%d bytes\n", max_additional_size); if (max_additional_size > MAX_BLOCK_WEIGHT) { - LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Received impossible CoinbaseOutputDataSize from client id=%zu: %d\n", + LogPrintLevel(BCLog::SV2, BCLog::Level::Error, "Received impossible CoinbaseOutputConstraints from client id=%zu: %d\n", client.m_id, max_additional_size); CloseConnection(client.m_id); client.m_disconnect_flag = true; diff --git a/src/sv2/connman.h b/src/sv2/connman.h index 287af427d2ba1..4148d83bedc6e 100644 --- a/src/sv2/connman.h +++ b/src/sv2/connman.h @@ -45,7 +45,7 @@ struct Sv2Client std::deque m_send_messages; /** - * Whether the client has received CoinbaseOutputDataSize message. + * Whether the client has received CoinbaseOutputConstraints message. */ bool m_coinbase_output_data_size_recv = false; diff --git a/src/sv2/messages.h b/src/sv2/messages.h index 17a9d8ca023e2..21a3442287e97 100644 --- a/src/sv2/messages.h +++ b/src/sv2/messages.h @@ -41,7 +41,7 @@ enum class Sv2MsgType : uint8_t { REQUEST_TRANSACTION_DATA_SUCCESS = 0x74, REQUEST_TRANSACTION_DATA_ERROR = 0x75, SUBMIT_SOLUTION = 0x76, - COINBASE_OUTPUT_DATA_SIZE = 0x70, + COINBASE_OUTPUT_CONSTRAINTS = 0x70, }; static const std::map SV2_MSG_NAMES{ @@ -54,7 +54,7 @@ static const std::map SV2_MSG_NAMES{ {Sv2MsgType::REQUEST_TRANSACTION_DATA_SUCCESS, "RequestTransactionData.Success"}, {Sv2MsgType::REQUEST_TRANSACTION_DATA_ERROR, "RequestTransactionData.Error"}, {Sv2MsgType::SUBMIT_SOLUTION, "SubmitSolution"}, - {Sv2MsgType::COINBASE_OUTPUT_DATA_SIZE, "CoinbaseOutputDataSize"}, + {Sv2MsgType::COINBASE_OUTPUT_CONSTRAINTS, "CoinbaseOutputConstraints"}, }; struct Sv2SetupConnectionMsg @@ -137,12 +137,12 @@ struct Sv2SetupConnectionMsg * The template provider MUST NOT provide NewWork messages which would represent consensus-invalid blocks once this * additional size — along with a maximally-sized (100 byte) coinbase field — is added. */ -struct Sv2CoinbaseOutputDataSizeMsg +struct Sv2CoinbaseOutputConstraintsMsg { /** * The default message type value for this Stratum V2 message. */ - static constexpr auto m_msg_type = Sv2MsgType::COINBASE_OUTPUT_DATA_SIZE; + static constexpr auto m_msg_type = Sv2MsgType::COINBASE_OUTPUT_CONSTRAINTS; /** * The maximum additional serialized bytes which the pool will add in coinbase transaction outputs. diff --git a/src/test/sv2_connman_tests.cpp b/src/test/sv2_connman_tests.cpp index 8ed51a26ba03c..c70ed1dc52c2c 100644 --- a/src/test/sv2_connman_tests.cpp +++ b/src/test/sv2_connman_tests.cpp @@ -198,7 +198,7 @@ BOOST_AUTO_TEST_CASE(client_tests) std::vector coinbase_output_max_additional_size_bytes{ 0x01, 0x00, 0x00, 0x00 }; - node::Sv2NetMsg msg{node::Sv2MsgType::COINBASE_OUTPUT_DATA_SIZE, std::move(coinbase_output_max_additional_size_bytes)}; + node::Sv2NetMsg msg{node::Sv2MsgType::COINBASE_OUTPUT_CONSTRAINTS, std::move(coinbase_output_max_additional_size_bytes)}; // No reply expected, not yet implemented tester.RemoteToLocalMsg(msg); } diff --git a/src/test/sv2_template_provider_tests.cpp b/src/test/sv2_template_provider_tests.cpp index d7badfeda2412..13ab01dc52b06 100644 --- a/src/test/sv2_template_provider_tests.cpp +++ b/src/test/sv2_template_provider_tests.cpp @@ -157,10 +157,10 @@ BOOST_AUTO_TEST_CASE(client_tests) // output data size: BOOST_REQUIRE(tester.GetBlockTemplateCount() == 0); - std::vector coinbase_output_max_additional_size_bytes{ + std::vector coinbase_output_constraint_bytes{ 0x01, 0x00, 0x00, 0x00 }; - node::Sv2NetMsg msg{node::Sv2MsgType::COINBASE_OUTPUT_DATA_SIZE, std::move(coinbase_output_max_additional_size_bytes)}; + node::Sv2NetMsg msg{node::Sv2MsgType::COINBASE_OUTPUT_CONSTRAINTS, std::move(coinbase_output_constraint_bytes)}; tester.receiveMessage(msg); BOOST_TEST_MESSAGE("The reply should be NewTemplate and SetNewPrevHash"); BOOST_REQUIRE_EQUAL(tester.PeerReceiveBytes(), 2 * SV2_HEADER_ENCRYPTED_SIZE + 91 + 80 + 2 * Poly1305::TAGLEN); diff --git a/src/test/sv2_transport_tests.cpp b/src/test/sv2_transport_tests.cpp index 9a353d231ad3e..cf3fd59a64e32 100644 --- a/src/test/sv2_transport_tests.cpp +++ b/src/test/sv2_transport_tests.cpp @@ -25,7 +25,7 @@ using namespace std::literals; using node::Sv2NetMsg; -using node::Sv2CoinbaseOutputDataSizeMsg; +using node::Sv2CoinbaseOutputConstraintsMsg; using node::Sv2MsgType; BOOST_FIXTURE_TEST_SUITE(sv2_transport_tests, RegTestingSetup) @@ -363,15 +363,15 @@ BOOST_AUTO_TEST_CASE(sv2_transport_responder_test) tester.CompareHash(); // Handshake complete, have the initiator send us a message: - Sv2CoinbaseOutputDataSizeMsg body{4000}; + Sv2CoinbaseOutputConstraintsMsg body{4000}; Sv2NetMsg msg{body}; - BOOST_REQUIRE(msg.m_msg_type == Sv2MsgType::COINBASE_OUTPUT_DATA_SIZE); + BOOST_REQUIRE(msg.m_msg_type == Sv2MsgType::COINBASE_OUTPUT_CONSTRAINTS); tester.SendPacket(msg); ret = tester.Interact(); BOOST_REQUIRE(ret && ret->size() == 1); BOOST_CHECK((*ret)[0] && - (*ret)[0]->m_msg_type == Sv2MsgType::COINBASE_OUTPUT_DATA_SIZE); + (*ret)[0]->m_msg_type == Sv2MsgType::COINBASE_OUTPUT_CONSTRAINTS); tester.CompareHash(); From 47705fd8b6a6aed23fbae65f1277318de72e607a Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 16 Dec 2024 12:08:32 +0700 Subject: [PATCH 2/3] Add coinbase_output_max_sigops --- src/sv2/messages.h | 7 +++++++ src/test/sv2_template_provider_tests.cpp | 3 ++- src/test/sv2_transport_tests.cpp | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/sv2/messages.h b/src/sv2/messages.h index 21a3442287e97..d51517722a052 100644 --- a/src/sv2/messages.h +++ b/src/sv2/messages.h @@ -149,10 +149,16 @@ struct Sv2CoinbaseOutputConstraintsMsg */ uint32_t m_coinbase_output_max_additional_size; + /** + * The maximum additional sigops which the pool will add in coinbase transaction outputs. + */ + uint16_t m_coinbase_output_max_sigops; + template void Serialize(Stream& s) const { s << m_coinbase_output_max_additional_size; + s << m_coinbase_output_max_sigops; }; @@ -160,6 +166,7 @@ struct Sv2CoinbaseOutputConstraintsMsg void Unserialize(Stream& s) { s >> m_coinbase_output_max_additional_size; + s >> m_coinbase_output_max_sigops; } }; diff --git a/src/test/sv2_template_provider_tests.cpp b/src/test/sv2_template_provider_tests.cpp index 13ab01dc52b06..20642d81f0bba 100644 --- a/src/test/sv2_template_provider_tests.cpp +++ b/src/test/sv2_template_provider_tests.cpp @@ -158,7 +158,8 @@ BOOST_AUTO_TEST_CASE(client_tests) BOOST_REQUIRE(tester.GetBlockTemplateCount() == 0); std::vector coinbase_output_constraint_bytes{ - 0x01, 0x00, 0x00, 0x00 + 0x01, 0x00, 0x00, 0x00, // coinbase_output_max_additional_size + 0x00, 0x00 // coinbase_output_max_sigops }; node::Sv2NetMsg msg{node::Sv2MsgType::COINBASE_OUTPUT_CONSTRAINTS, std::move(coinbase_output_constraint_bytes)}; tester.receiveMessage(msg); diff --git a/src/test/sv2_transport_tests.cpp b/src/test/sv2_transport_tests.cpp index cf3fd59a64e32..c4c7da2cfd358 100644 --- a/src/test/sv2_transport_tests.cpp +++ b/src/test/sv2_transport_tests.cpp @@ -363,7 +363,7 @@ BOOST_AUTO_TEST_CASE(sv2_transport_responder_test) tester.CompareHash(); // Handshake complete, have the initiator send us a message: - Sv2CoinbaseOutputConstraintsMsg body{4000}; + Sv2CoinbaseOutputConstraintsMsg body{4000, 400}; Sv2NetMsg msg{body}; BOOST_REQUIRE(msg.m_msg_type == Sv2MsgType::COINBASE_OUTPUT_CONSTRAINTS); From 0ede234112ec1dc2c3e807efcf204bb44d6c57d7 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 16 Dec 2024 13:14:45 +0700 Subject: [PATCH 3/3] Fall back to 400 sigops if field is missing This makes the Template Provider backward compatible if the spec is updated. --- src/sv2/messages.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/sv2/messages.h b/src/sv2/messages.h index d51517722a052..8a3af49baf062 100644 --- a/src/sv2/messages.h +++ b/src/sv2/messages.h @@ -166,7 +166,14 @@ struct Sv2CoinbaseOutputConstraintsMsg void Unserialize(Stream& s) { s >> m_coinbase_output_max_additional_size; - s >> m_coinbase_output_max_sigops; + try { + // This field was added to the spec on ..., + // SRI roles before ... do not provide it. + s >> m_coinbase_output_max_sigops; + } catch (const std::ios_base::failure& e) { + // Just use the default if it's missing + m_coinbase_output_max_sigops = 400; + } } };