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
13 changes: 12 additions & 1 deletion console/executor_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions console/localize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
6 changes: 5 additions & 1 deletion include/bitcoin/server/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<network::rpc::request_t>& out,
const network::rpc::request_t& in) NOEXCEPT;
};

} // namespace server
Expand Down
30 changes: 19 additions & 11 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -2414,7 +2422,7 @@ options_metadata parser::load_settings() THROWS
(
"node.provide_filters",
setting<bool>(&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",
Expand Down Expand Up @@ -2921,7 +2929,7 @@ options_metadata parser::load_settings() THROWS
(
"table.filter_bk.buckets",
setting<uint32_t>(&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",
Expand All @@ -2938,7 +2946,7 @@ options_metadata parser::load_settings() THROWS
(
"table.filter_tx.buckets",
setting<uint32_t>(&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",
Expand Down
19 changes: 19 additions & 0 deletions src/parsers/bitcoind_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <charconv>
#include <bitcoin/server/define.hpp>
#include <bitcoin/server/interfaces/interfaces.hpp>

namespace libbitcoin {
namespace server {
Expand All @@ -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<rest::methods>(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{};
Expand All @@ -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{};
Expand All @@ -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")
Expand Down
128 changes: 126 additions & 2 deletions src/protocols/bitcoind/protocol_bitcoind_dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,114 @@ bool CLASS::is_ignored(const network::rpc::request_t& message) NOEXCEPT
!network::rpc::is_command<Interface::methods>(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<std::string_view, 3> members;
};

static constexpr std::array<named_options, 4> 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<network::rpc::request_t>& out,
const network::rpc::request_t& in) NOEXCEPT
{
using namespace network::rpc;
if (!in.params.has_value() ||
!std::holds_alternative<object_t>(in.params.value()))
return error::success;

const auto& object = std::get<object_t>(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<object_t>(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<array_t>(it->second.value()))
return error::success;

auto args = std::get<array_t>(std::move(it->second.value()));
params.erase(it);

const auto names = parameter_names<Interface::methods>(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
Expand Down Expand Up @@ -126,8 +234,16 @@ void CLASS::handle_receive_post(const code& ec,
return;
}

std::optional<network::rpc::request_t> 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);
}

Expand Down Expand Up @@ -178,8 +294,16 @@ void CLASS::dispatch_websocket(
return;
}

std::optional<network::rpc::request_t> 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);
}

Expand Down
3 changes: 2 additions & 1 deletion test/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/parsers/bitcoind_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,14 @@ BC_AUTO_TEST_CASE(parsers__bitcoind_query__offset_and_size__overlaid)
BC_REQUIRE_EQUAL(std::get<uint32_t>(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()
7 changes: 7 additions & 0 deletions test/protocols/bitcoind/bitcoind_rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading