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
38 changes: 36 additions & 2 deletions include/bitcoin/network/impl/messages/rpc/dispatcher.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ inline external_t<Argument> CLASS::get_named(
return CLASS::get_valued<Argument>(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
{
Expand Down Expand Up @@ -185,8 +204,23 @@ inline externals_t<Arguments> CLASS::extract_named(
const auto object = get_object(params);
constexpr auto count = std::tuple_size_v<Arguments>;

// 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{};
[&]<size_t... Index>(std::index_sequence<Index...>) THROWS
{
((named += counted(names.at(Index))), ...);
}(std::make_index_sequence<count>{});

if (named != object.size())
throw std::system_error{ error::extra_named };

return [&]<size_t... Index>(std::index_sequence<Index...>) THROWS
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/network/messages/rpc/dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class dispatcher
template <typename Argument>
static inline external_t<Argument> 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;
Expand Down
23 changes: 23 additions & 0 deletions include/bitcoin/network/messages/rpc/publish.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <auto& Methods>
constexpr std::span<const std::string_view> parameter_names(
const std::string_view& name) NOEXCEPT
{
std::span<const std::string_view> 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
Expand Down
13 changes: 11 additions & 2 deletions test/messages/rpc/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" } } } }
});
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
9 changes: 9 additions & 0 deletions test/messages/rpc/publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ static_assert(!is_command<served_methods::methods>("alpha"));
// Commands are implemented (published).
static_assert(to_view(method_names<commanded_methods::methods>()) == "alpha beta");

// parameter_names
// -----------------------------------------------------------------------------

static_assert(parameter_names<mixed_methods::methods>("alpha").empty());
static_assert(parameter_names<mixed_methods::methods>("gamma").size() == 1u);
static_assert(parameter_names<mixed_methods::methods>("gamma").front() == "flag");
static_assert(parameter_names<mixed_methods::methods>("beta").front() == "flag");
static_assert(parameter_names<mixed_methods::methods>("omega").empty());

// publish
// -----------------------------------------------------------------------------

Expand Down
Loading