From 86fde41a655718947e09659348dbde9d9a15c4e4 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Tue, 17 Nov 2026 19:41:03 -0500 Subject: [PATCH 1/2] Apply the stored envelope before initialize, log migration. --- console/executor_store.cpp | 13 +++++++- console/localize.hpp | 2 ++ include/bitcoin/server/parser.hpp | 6 +++- src/parser.cpp | 30 ++++++++++++------- test/parser.cpp | 3 +- .../bitcoind/bitcoind_setup_fixture.hpp | 3 +- .../protocols/native/native_setup_fixture.cpp | 5 +++- .../protocols/native/native_setup_fixture.hpp | 3 +- test/protocols/sparrow/sparrow.cpp | 4 +-- test/test.cpp | 1 + 10 files changed, 49 insertions(+), 21 deletions(-) diff --git a/console/executor_store.cpp b/console/executor_store.cpp index 5ef80a41..e21fc483 100644 --- a/console/executor_store.cpp +++ b/console/executor_store.cpp @@ -156,6 +156,11 @@ code executor::open_store_coded(bool details) span.count())); } + if (metadata_.stored.schema != database::envelope::compiled) + logger(std::format(BS_DATABASE_MIGRATED, + metadata_.stored.schema.to_string(), + database::envelope::compiled.to_string())); + apply_envelope(); logger(BS_DATABASE_STARTED); return error::success; @@ -354,10 +359,16 @@ void executor::apply_envelope() configured.bitcoin, configured.database, configured.node.limited_blocks, + configured.node.require_witness, configured.node.silent_start_height); - + + for (const auto& name: metadata_.overrides) + logger(std::format(BS_DATABASE_ENVELOPE_OVERRIDE, name)); + for (const auto& name: applied) logger(std::format(BS_DATABASE_ENVELOPE_OVERRIDE, name)); + + metadata_.overrides.clear(); } } // namespace server diff --git a/console/localize.hpp b/console/localize.hpp index 3d56f482..04cf594f 100644 --- a/console/localize.hpp +++ b/console/localize.hpp @@ -370,6 +370,8 @@ "Database schema {} does not match compiled schema {}." #define BS_DATABASE_ENVELOPE_OVERRIDE \ "Database envelope [{}] overrides configuration." +#define BS_DATABASE_MIGRATED \ + "Database schema {} migrated to compiled schema {}." #define BS_NETWORK_OVERRIDE \ "Network [{}] overrides configured network [{}]." #define BS_DATABASE_STOPPING \ diff --git a/include/bitcoin/server/parser.hpp b/include/bitcoin/server/parser.hpp index 10f349ff..6d0e9747 100644 --- a/include/bitcoin/server/parser.hpp +++ b/include/bitcoin/server/parser.hpp @@ -75,7 +75,8 @@ class BCS_API parser virtual bool parse(int argc, const char* argv[], std::ostream& error) THROWS; - /// Read the envelope of an existing store and reset defaults to its network. + /// Read the envelope of an existing store, reset defaults to its network, + /// and apply its stored values to configuration. virtual void read_envelope() NOEXCEPT; /// The populated configuration settings values. @@ -85,6 +86,9 @@ class BCS_API parser database::envelope stored{}; code stored_code{}; + /// The names of configured values overridden by the stored envelope. + system::string_list overrides{}; + protected: /// Select the network and reset the defaults to it when it differs. virtual void select_network() NOEXCEPT; diff --git a/src/parser.cpp b/src/parser.cpp index 146f0a73..715758dc 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -173,19 +173,27 @@ void parser::read_envelope() NOEXCEPT return; server_node::store store{ configured.database }; + stored.require_witness = configured.node.require_witness; stored_code = store.read_envelope(stored); - if (stored_code || stored.network == configured.node.network) + if (stored_code) return; - const chain::selection selected = configured.net; - const auto specified = selected != chain::selection::none; + if (stored.network != configured.node.network) + { + const chain::selection selected = configured.net; + const auto specified = selected != chain::selection::none; - reset_network(stored.network); - derive_buckets(); + reset_network(stored.network); + derive_buckets(); - // A specified network is retained as configured, for override reporting. - if (specified) - configured.bitcoin.network = selected; + // A specified network is retained as configured, for override reporting. + if (specified) + configured.bitcoin.network = selected; + } + + overrides = stored.apply(configured.bitcoin, configured.database, + configured.node.limited_blocks, configured.node.require_witness, + configured.node.silent_start_height); } // Network defaults are reset, and then configured settings are reapplied. @@ -2414,7 +2422,7 @@ options_metadata parser::load_settings() THROWS ( "node.provide_filters", setting(&configured.node.provide_filters), - "Serve client filters to network connections, defaults to {}." + "Serve client filters to network connections, indexed only if set at store creation, defaults to {}." ) ( "node.provide_privacy", @@ -2921,7 +2929,7 @@ options_metadata parser::load_settings() THROWS ( "table.filter_bk.buckets", setting(&configured.database.filter_bk.buckets), - "The number of buckets in the filter_bk table head, defaults to {} (0 disables)." + "The number of buckets in the filter_bk table head, defaults to {}." ) ( "table.filter_bk.size", @@ -2938,7 +2946,7 @@ options_metadata parser::load_settings() THROWS ( "table.filter_tx.buckets", setting(&configured.database.filter_tx.buckets), - "The number of buckets in the filter_tx table head, defaults to {} (0 disables)." + "The number of buckets in the filter_tx table head, defaults to {}." ) ( "table.filter_tx.size", diff --git a/test/parser.cpp b/test/parser.cpp index 147a641b..fc78ebd5 100644 --- a/test/parser.cpp +++ b/test/parser.cpp @@ -481,9 +481,10 @@ BC_AUTO_TEST_CASE(parser__read_envelope__specified_network__stored_defaults_spec BC_REQUIRE(instance.stored.network == selection::regtest); const auto& configured = instance.configured; - BC_REQUIRE(configured.bitcoin.network == selection::testnet4); + BC_REQUIRE(configured.bitcoin.network == selection::regtest); BC_REQUIRE_EQUAL(configured.network.identifier, 3669344250u); BC_REQUIRE_EQUAL(configured.database.strong.buckets, 100'000u); + BC_REQUIRE(instance.overrides == system::string_list{ "network" }); } BC_AUTO_TEST_CASE(parser__read_envelope__make__unchanged) diff --git a/test/protocols/bitcoind/bitcoind_setup_fixture.hpp b/test/protocols/bitcoind/bitcoind_setup_fixture.hpp index 311f71a5..154f12c9 100644 --- a/test/protocols/bitcoind/bitcoind_setup_fixture.hpp +++ b/test/protocols/bitcoind/bitcoind_setup_fixture.hpp @@ -432,8 +432,7 @@ struct bitcoind_unfiltered_setup_fixture return test::setup_ten_block_store(query); }, [](configuration& config) { - config.database.filter_bk.buckets = 0; - config.database.filter_tx.buckets = 0; + config.database.envelope.provide_filters = false; }) { } diff --git a/test/protocols/native/native_setup_fixture.cpp b/test/protocols/native/native_setup_fixture.cpp index 34d1aecc..ca5a8e71 100644 --- a/test/protocols/native/native_setup_fixture.cpp +++ b/test/protocols/native/native_setup_fixture.cpp @@ -94,7 +94,10 @@ native_setup_fixture::native_setup_fixture(const initializer& setup, configure(config_); config_.database.initialize(config_.bitcoin, - config_.node.limited_blocks, config_.node.silent_start_height); + config_.node.limited_blocks, + config_.database.envelope.provide_filters, + config_.node.require_witness, + config_.node.silent_start_height); return config_.database; }() }, diff --git a/test/protocols/native/native_setup_fixture.hpp b/test/protocols/native/native_setup_fixture.hpp index 201558a6..664d0774 100644 --- a/test/protocols/native/native_setup_fixture.hpp +++ b/test/protocols/native/native_setup_fixture.hpp @@ -311,8 +311,7 @@ struct native_limited_setup_fixture }, [](server::configuration& config) { config.node.limited_blocks = true; - config.database.filter_bk.buckets = 0; - config.database.filter_tx.buckets = 0; + config.database.envelope.provide_filters = false; }) { } diff --git a/test/protocols/sparrow/sparrow.cpp b/test/protocols/sparrow/sparrow.cpp index 85597ef6..0a575014 100644 --- a/test/protocols/sparrow/sparrow.cpp +++ b/test/protocols/sparrow/sparrow.cpp @@ -182,7 +182,7 @@ struct sparrow_silent_setup_fixture }, true, [](configuration& config) { config.node.silent_start_height = 0; - config.database.initialize(config.bitcoin, false, 0); + config.database.initialize(config.bitcoin, false, true, true, 0); }, service::sparrow) { } @@ -247,7 +247,7 @@ struct sparrow_silent_mapped_setup_fixture }, true, [](configuration& config) { config.node.silent_start_height = 0; - config.database.initialize(config.bitcoin, false, 0); + config.database.initialize(config.bitcoin, false, true, true, 0); }, service::sparrow) { } diff --git a/test/test.cpp b/test/test.cpp index 93edac7e..f92d7899 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -146,6 +146,7 @@ static database::settings database_settings() NOEXCEPT bucket(out.filter_tx); out.envelope.set(out); + out.envelope.provide_filters = true; return out; } From 2c8b1741e0cbc8e6c544c30e9d03b87521cb3c12 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Tue, 17 Nov 2026 21:57:24 -0500 Subject: [PATCH 2/2] Transform bitcoind named args and ignore undeclared rest query params. --- .../protocols/protocol_bitcoind_dispatch.hpp | 2 + src/parsers/bitcoind_query.cpp | 19 +++ .../bitcoind/protocol_bitcoind_dispatch.cpp | 128 +++++++++++++++++- test/parsers/bitcoind_query.cpp | 10 ++ test/protocols/bitcoind/bitcoind_rest.cpp | 7 + test/protocols/bitcoind/bitcoind_rpc.cpp | 59 ++++++++ 6 files changed, 223 insertions(+), 2 deletions(-) diff --git a/include/bitcoin/server/protocols/protocol_bitcoind_dispatch.hpp b/include/bitcoin/server/protocols/protocol_bitcoind_dispatch.hpp index bf185e0a..b515c8e2 100644 --- a/include/bitcoin/server/protocols/protocol_bitcoind_dispatch.hpp +++ b/include/bitcoin/server/protocols/protocol_bitcoind_dispatch.hpp @@ -74,6 +74,8 @@ class protocol_bitcoind_dispatch private: static bool is_ignored(const network::rpc::request_t& message) NOEXCEPT; + static code transform(std::optional& out, + const network::rpc::request_t& in) NOEXCEPT; }; } // namespace server diff --git a/src/parsers/bitcoind_query.cpp b/src/parsers/bitcoind_query.cpp index e09051ee..0b01925b 100644 --- a/src/parsers/bitcoind_query.cpp +++ b/src/parsers/bitcoind_query.cpp @@ -20,6 +20,7 @@ #include #include +#include namespace libbitcoin { namespace server { @@ -40,6 +41,18 @@ static bool to_number(Number& out, const std::string_view& token) NOEXCEPT return result.ec == std::errc{} && result.ptr == end; } +// Query parameters not declared by the target method are ignored. +static bool declares(const std::string& method, + const std::string_view& name) NOEXCEPT +{ + using rest = interface::bitcoind_rest; + const auto names = rpc::parameter_names(method); + return std::ranges::any_of(names, [&](const auto& item) NOEXCEPT + { + return rpc::primary_name(item) == name; + }); +} + bool bitcoind_query(rpc::request_t& out, const std::string& target) NOEXCEPT { wallet::uri uri{}; @@ -57,6 +70,9 @@ bool bitcoind_query(rpc::request_t& out, const std::string& target) NOEXCEPT // Decimal parameters, defaulted or required by the target interface. for (const auto& name: { "count", "offset", "size" }) { + if (!declares(out.method, name)) + continue; + if (const auto it = query.find(name); it != query.cend()) { uint32_t value{}; @@ -70,6 +86,9 @@ bool bitcoind_query(rpc::request_t& out, const std::string& target) NOEXCEPT // Boolean parameters, either "true" or "false". for (const auto& name: { "verbose", "mempool_sequence" }) { + if (!declares(out.method, name)) + continue; + if (const auto it = query.find(name); it != query.cend()) { if (it->second != "true" && it->second != "false") diff --git a/src/protocols/bitcoind/protocol_bitcoind_dispatch.cpp b/src/protocols/bitcoind/protocol_bitcoind_dispatch.cpp index 4908b48d..017e2367 100644 --- a/src/protocols/bitcoind/protocol_bitcoind_dispatch.cpp +++ b/src/protocols/bitcoind/protocol_bitcoind_dispatch.cpp @@ -38,6 +38,114 @@ bool CLASS::is_ignored(const network::rpc::request_t& message) NOEXCEPT !network::rpc::is_command(message.method); } +// bitcoind accepts the members of these options objects as named arguments +// (RPCArg::Type::OBJ_NAMED_PARAMS), collected into the options parameter. +struct named_options +{ + std::string_view method; + std::array members; +}; + +static constexpr std::array options_members +{{ + { "dumptxoutset", { "rollback", "in_memory" } }, + { "gettxspendingprevout", { "mempool_only", "return_spending_tx" } }, + { "importmempool", { "use_current_time", "apply_fee_delta_priority", + "apply_unbroadcast_set" } }, + { "scanblocks", { "filter_false_positives" } } +}}; + +// True if any alias of the parameter names a member. +static bool is_named(const std::string_view& name, + const network::rpc::object_t& object) NOEXCEPT +{ + for (auto aliases = name; !aliases.empty();) + { + const auto bar = aliases.find('|'); + if (object.contains(std::string{ aliases.substr(zero, bar) })) + return true; + + aliases = (bar == std::string_view::npos) ? std::string_view{} : + aliases.substr(add1(bar)); + } + + return false; +} + +// Named arguments are transformed as by bitcoind: options members are +// collected into the options parameter, and the positional arguments of +// "args" are named by the leading parameters. Otherwise out is not set. +TEMPLATE +code CLASS::transform(std::optional& out, + const network::rpc::request_t& in) NOEXCEPT +{ + using namespace network::rpc; + if (!in.params.has_value() || + !std::holds_alternative(in.params.value())) + return error::success; + + const auto& object = std::get(in.params.value()); + const auto entry = std::ranges::find(options_members, in.method, + &named_options::method); + const auto member = [&](const std::string_view& name) NOEXCEPT + { + return !name.empty() && object.contains(std::string{ name }); + }; + + const auto optioned = (entry != options_members.end()) && + std::ranges::any_of(entry->members, member); + const auto positioned = object.contains("args"); + if (!optioned && !positioned) + return error::success; + + out.emplace(in); + auto& params = std::get(out->params.value()); + + if (optioned) + { + if (params.contains("options")) + return network::error::extra_named; + + object_t options{}; + for (const auto& name: entry->members) + { + if (const auto it = params.find(std::string{ name }); + !name.empty() && it != params.end()) + { + options.emplace(it->first, std::move(it->second)); + params.erase(it); + } + } + + params.emplace("options", std::move(options)); + } + + // A non-array "args" remains, refused as an unknown named argument. + const auto it = params.find("args"); + if (it == params.end() || + !std::holds_alternative(it->second.value())) + return error::success; + + auto args = std::get(std::move(it->second.value())); + params.erase(it); + + const auto names = parameter_names(in.method); + if (args.size() > names.size()) + return network::error::extra_positional; + + for (size_t index{}; index < args.size(); ++index) + { + const auto& name = names[index]; + if (is_named(name, params)) + return network::error::extra_named; + + params.emplace(std::string{ primary_name(name) }, + std::move(args[index])); + } + + return error::success; +} + // Subscribe to the post and websocket transports of the interface. TEMPLATE void CLASS::start() NOEXCEPT @@ -126,8 +234,16 @@ void CLASS::handle_receive_post(const code& ec, return; } + std::optional transformed{}; + if (const auto code = transform(transformed, message)) + { + handle_dispatch_fault(code); + return; + } + // Dispatch the request to the interface dispatcher. - if (const auto code = rpc_dispatcher_.notify(message)) + const auto& dispatched = transformed ? *transformed : message; + if (const auto code = rpc_dispatcher_.notify(dispatched)) handle_dispatch_fault(code); } @@ -178,8 +294,16 @@ void CLASS::dispatch_websocket( return; } + std::optional transformed{}; + if (const auto code = transform(transformed, message)) + { + handle_dispatch_fault(code); + return; + } + // Dispatch the request to the interface dispatcher. - if (const auto code = rpc_dispatcher_.notify(message)) + const auto& dispatched = transformed ? *transformed : message; + if (const auto code = rpc_dispatcher_.notify(dispatched)) handle_dispatch_fault(code); } diff --git a/test/parsers/bitcoind_query.cpp b/test/parsers/bitcoind_query.cpp index 5fd34d09..7476d7b0 100644 --- a/test/parsers/bitcoind_query.cpp +++ b/test/parsers/bitcoind_query.cpp @@ -110,4 +110,14 @@ BC_AUTO_TEST_CASE(parsers__bitcoind_query__offset_and_size__overlaid) BC_REQUIRE_EQUAL(std::get(params_of(out).at("size").value()), 1u); } +BC_AUTO_TEST_CASE(parsers__bitcoind_query__undeclared__ignored) +{ + request_t out{}; + const auto target = "/rest/mempool/contents.json?count=1&offset=x"; + BC_REQUIRE(!bitcoind_target(out, target)); + BC_REQUIRE(bitcoind_query(out, target)); + BC_REQUIRE(!params_of(out).contains("count")); + BC_REQUIRE(!params_of(out).contains("offset")); +} + BC_AUTO_TEST_SUITE_END() diff --git a/test/protocols/bitcoind/bitcoind_rest.cpp b/test/protocols/bitcoind/bitcoind_rest.cpp index db8bf04f..1a562f68 100644 --- a/test/protocols/bitcoind/bitcoind_rest.cpp +++ b/test/protocols/bitcoind/bitcoind_rest.cpp @@ -495,6 +495,13 @@ BC_AUTO_TEST_CASE(bitcoind_rest__mempool_contents__invalid_boolean__bad_request) BC_REQUIRE_EQUAL(rest_status("/rest/mempool/contents.json?verbose=1"), status::bad_request); } +BC_AUTO_TEST_CASE(bitcoind_rest__mempool_contents__undeclared_query__ignored) +{ + const auto result = rest_json("/rest/mempool/contents.json?count=1"); + BC_REQUIRE(result.is_object()); + BC_REQUIRE(result.as_object().empty()); +} + BC_AUTO_TEST_CASE(bitcoind_rest__mempool_contents__binary__not_found) { BC_REQUIRE_EQUAL(rest_status("/rest/mempool/contents.bin"), status::not_found); diff --git a/test/protocols/bitcoind/bitcoind_rpc.cpp b/test/protocols/bitcoind/bitcoind_rpc.cpp index 01edb012..b772f8f0 100644 --- a/test/protocols/bitcoind/bitcoind_rpc.cpp +++ b/test/protocols/bitcoind/bitcoind_rpc.cpp @@ -442,6 +442,18 @@ BC_AUTO_TEST_CASE(bitcoind_rpc__getblock__named_verbosity__hex) REQUIRE_NO_THROW_TRUE(response.at("result").is_string()); } +BC_AUTO_TEST_CASE(bitcoind_rpc__getblock__misnamed_optional__invalid_parameter) +{ + const auto response = rpc("getblock", "{\"blockhash\":\"" + block9 + "\",\"verbosty\":0}"); + BC_REQUIRE_MESSAGE(has_code(response, -8), serialize(response)); +} + +BC_AUTO_TEST_CASE(bitcoind_rpc__getblock__both_aliases__invalid_parameter) +{ + const auto response = rpc("getblock", "{\"blockhash\":\"" + block9 + "\",\"verbose\":0,\"verbosity\":0}"); + BC_REQUIRE_MESSAGE(has_code(response, -8), serialize(response)); +} + BC_AUTO_TEST_CASE(bitcoind_rpc__getblock__unknown__invalid_address) { const std::string unknown(64, '1'); @@ -2106,6 +2118,31 @@ BC_AUTO_TEST_CASE(bitcoind_rpc__getblockhash__unknown_named__invalid_parameter) BC_REQUIRE_MESSAGE(has_code(response, -8), serialize(response)); } +BC_AUTO_TEST_CASE(bitcoind_rpc__getblockhash__args__block5) +{ + const auto response = rpc("getblockhash", R"({"args":[5]})"); + BC_REQUIRE_MESSAGE(response.is_object() && response.as_object().contains("result"), serialize(response)); + BC_REQUIRE_EQUAL(as_text(response.at("result")), block5); +} + +BC_AUTO_TEST_CASE(bitcoind_rpc__getblockhash__args_and_named__invalid_parameter) +{ + const auto response = rpc("getblockhash", R"({"args":[5],"height":5})"); + BC_REQUIRE_MESSAGE(has_code(response, -8), serialize(response)); +} + +BC_AUTO_TEST_CASE(bitcoind_rpc__getblockhash__args_extra__misc_error) +{ + const auto response = rpc("getblockhash", R"({"args":[5,6]})"); + BC_REQUIRE_MESSAGE(has_code(response, -1), serialize(response)); +} + +BC_AUTO_TEST_CASE(bitcoind_rpc__getblockhash__args_not_array__invalid_parameter) +{ + const auto response = rpc("getblockhash", R"({"height":5,"args":5})"); + BC_REQUIRE_MESSAGE(has_code(response, -8), serialize(response)); +} + BC_AUTO_TEST_CASE(bitcoind_rpc__getblockhash__type_error__connection_retained) { BC_REQUIRE(has_code(rpc("getblockhash", R"(["x"])"), -3)); @@ -4888,6 +4925,28 @@ BC_AUTO_TEST_CASE(bitcoind_rpc__gettxspendingprevout__broadcast_spender__spendin BC_REQUIRE(!result.contains("blockhash")); } +BC_AUTO_TEST_CASE(bitcoind_rpc__gettxspendingprevout__named_option__spending_tx) +{ + const auto sent = rpc("sendrawtransaction", send_param(test::tx1c)); + BC_REQUIRE_MESSAGE(sent.is_object() && sent.as_object().contains("result"), serialize(sent)); + + const auto funder = test::block1c.transactions_ptr()->front()->hash(false); + const auto outputs = "[{\"txid\":\"" + encode_hash(funder) + "\",\"vout\":0}]"; + const auto response = rpc("gettxspendingprevout", "{\"outputs\":" + outputs + ",\"return_spending_tx\":true}"); + BC_REQUIRE_MESSAGE(response.is_object() && response.as_object().contains("result"), serialize(response)); + + const auto& result = response.at("result").as_array().front().as_object(); + BC_REQUIRE_EQUAL(as_text(result.at("spendingtx")), encode_base16(test::tx1c.to_data(true))); +} + +BC_AUTO_TEST_CASE(bitcoind_rpc__gettxspendingprevout__named_option_and_options__invalid_parameter) +{ + const auto funder = test::block1c.transactions_ptr()->front()->hash(false); + const auto outputs = "[{\"txid\":\"" + encode_hash(funder) + "\",\"vout\":0}]"; + const auto response = rpc("gettxspendingprevout", "{\"outputs\":" + outputs + ",\"options\":{},\"mempool_only\":true}"); + BC_REQUIRE_MESSAGE(has_code(response, -8), serialize(response)); +} + BC_AUTO_TEST_CASE(bitcoind_rpc__gettxspendingprevout__confirmed_spender__blockhash) { const auto funder = test::block1c.transactions_ptr()->front()->hash(false);