diff --git a/include/bitcoin/network/impl/messages/rpc/dispatcher.ipp b/include/bitcoin/network/impl/messages/rpc/dispatcher.ipp index 989156de3..17541ce39 100644 --- a/include/bitcoin/network/impl/messages/rpc/dispatcher.ipp +++ b/include/bitcoin/network/impl/messages/rpc/dispatcher.ipp @@ -128,6 +128,25 @@ inline external_t CLASS::get_named( return CLASS::get_valued(internal); } +// The number of members that name the parameter, one for each alias present. +TEMPLATE +inline size_t CLASS::count_named(const std::string_view& name, + const object_t& object) NOEXCEPT +{ + size_t count{}; + for (auto aliases = name; !aliases.empty();) + { + const auto bar = aliases.find('|'); + if (object.contains(std::string{ aliases.substr(zero, bar) })) + ++count; + + aliases = (bar == std::string_view::npos) ? std::string_view{} : + aliases.substr(add1(bar)); + } + + return count; +} + TEMPLATE inline array_t CLASS::get_array(const parameters_t& params) THROWS { @@ -185,8 +204,23 @@ inline externals_t CLASS::extract_named( const auto object = get_object(params); constexpr auto count = std::tuple_size_v; - // This doesn't catch duplicate names (allowed by json-rpc). - if (object.size() > count) + // Each member names one parameter, by one of its aliases. + const auto counted = [&](const std::string_view& name) THROWS + { + const auto found = CLASS::count_named(name, object); + if (found > one) + throw std::system_error{ error::extra_named }; + + return found; + }; + + size_t named{}; + [&](std::index_sequence) THROWS + { + ((named += counted(names.at(Index))), ...); + }(std::make_index_sequence{}); + + if (named != object.size()) throw std::system_error{ error::extra_named }; return [&](std::index_sequence) THROWS diff --git a/include/bitcoin/network/messages/rpc/dispatcher.hpp b/include/bitcoin/network/messages/rpc/dispatcher.hpp index b8475adf6..f5255977f 100644 --- a/include/bitcoin/network/messages/rpc/dispatcher.hpp +++ b/include/bitcoin/network/messages/rpc/dispatcher.hpp @@ -110,6 +110,8 @@ class dispatcher template static inline external_t get_named(const std::string_view& name, const object_t& object) THROWS; + static inline size_t count_named(const std::string_view& name, + const object_t& object) NOEXCEPT; static inline array_t get_array(const parameters_t& params) THROWS; static inline object_t get_object(const parameters_t& params) THROWS; diff --git a/include/bitcoin/network/messages/rpc/publish.hpp b/include/bitcoin/network/messages/rpc/publish.hpp index f76702bfd..01751d95d 100644 --- a/include/bitcoin/network/messages/rpc/publish.hpp +++ b/include/bitcoin/network/messages/rpc/publish.hpp @@ -99,6 +99,29 @@ constexpr bool is_command(const std::string_view& name) NOEXCEPT }, Methods); } +/// The parameter names of the named method, empty if not declared. +template +constexpr std::span parameter_names( + const std::string_view& name) NOEXCEPT +{ + std::span out{}; + const auto find = [&](const auto& item) NOEXCEPT + { + if (item.name != name) + return false; + + out = item.parameter_names(); + return true; + }; + + std::apply([&](const auto&... items) NOEXCEPT + { + (find(items) || ...); + }, Methods); + + return out; +} + } // namespace rpc } // namespace network } // namespace libbitcoin diff --git a/test/messages/rpc/dispatcher.cpp b/test/messages/rpc/dispatcher.cpp index ddd92dfa5..0c820b12d 100644 --- a/test/messages/rpc/dispatcher.cpp +++ b/test/messages/rpc/dispatcher.cpp @@ -336,7 +336,7 @@ BC_AUTO_TEST_CASE(dispatcher__notify__all_required_named_params__expected) const auto ec2 = instance.notify( { - // missing_parameter (misnamed/absent) + // extra_named (misnamed) .method = "all_required", .params = { object_t{ { "fu", boolean_type{ true } }, { "ga", number_t{ 24.0 } }, { "zi", string_t{ "42" } } } } }); @@ -380,7 +380,7 @@ BC_AUTO_TEST_CASE(dispatcher__notify__all_required_named_params__expected) }); BC_REQUIRE_EQUAL(ec1, error::missing_parameter); - BC_REQUIRE_EQUAL(ec2, error::missing_parameter); + BC_REQUIRE_EQUAL(ec2, error::extra_named); BC_REQUIRE_EQUAL(ec3, error::unexpected_type); BC_REQUIRE_EQUAL(ec4, error::extra_named); BC_REQUIRE(!ec5); @@ -488,10 +488,18 @@ BC_AUTO_TEST_CASE(dispatcher__notify__with_options_named_params__expected) .params = { object_t{ { "a", string_t{ "42" } }, { "b", number_t{ 42.0 } }, { "c", boolean_type{ false } } } } }); + const auto ec5 = instance.notify( + { + // extra_named (misnamed optional, within parameter count) + .method = "with_options", + .params = { object_t{ { "a", string_t{ "42" } }, { "bb", number_t{ 42.0 } } } } + }); + BC_REQUIRE_EQUAL(ec1, error::missing_parameter); BC_REQUIRE(!ec2); BC_REQUIRE(!ec3); BC_REQUIRE(!ec4); + BC_REQUIRE_EQUAL(ec5, error::extra_named); BC_REQUIRE_EQUAL(result_a, "42"); BC_REQUIRE_EQUAL(result_b, 4.2); BC_REQUIRE_EQUAL(result_c, true); @@ -1516,6 +1524,7 @@ BC_AUTO_TEST_CASE(dispatcher__notify__named_alias__either_name) BC_REQUIRE_EQUAL(result_b, 7.0); BC_REQUIRE_EQUAL(instance.notify({ .method = "casted", .params = { object_t{ { "bee", number_t{ 5.0 } } } } }), error::missing_parameter); + BC_REQUIRE_EQUAL(instance.notify({ .method = "casted", .params = { object_t{ { "a", boolean_type{ true } }, { "b", string_t{ "5" } }, { "bee", string_t{ "5" } } } } }), error::extra_named); instance.stop(error::service_stopped); } diff --git a/test/messages/rpc/publish.cpp b/test/messages/rpc/publish.cpp index 070e7b888..614f73509 100644 --- a/test/messages/rpc/publish.cpp +++ b/test/messages/rpc/publish.cpp @@ -95,6 +95,15 @@ static_assert(!is_command("alpha")); // Commands are implemented (published). static_assert(to_view(method_names()) == "alpha beta"); +// parameter_names +// ----------------------------------------------------------------------------- + +static_assert(parameter_names("alpha").empty()); +static_assert(parameter_names("gamma").size() == 1u); +static_assert(parameter_names("gamma").front() == "flag"); +static_assert(parameter_names("beta").front() == "flag"); +static_assert(parameter_names("omega").empty()); + // publish // -----------------------------------------------------------------------------