Skip to content
Closed
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
18 changes: 16 additions & 2 deletions src/protocols/bitcoind/protocol_bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,22 @@ void protocol_bitcoind::send_rpc(response_t&& model,
const auto statused = options().error_status && !batched;
auto message = to_response(statused ? to_status(model) : status::ok);
message.set(field::content_type, json);
message.body() = rpc::response{ {}, std::move(model) };
message.prepare_payload();

// Batch parts are streamed (chunked). A singleton is sized, as bitcoind
// does and as some of its clients (e.g. fulcrum) require.
if (batched)
{
message.body() = rpc::response{ {}, std::move(model) };
message.prepare_payload();
}
else
{
auto text = serialize(value_from(model));
const auto size = text.size();
message.body() = std::move(text);
message.content_length(size);
}

SEND(std::move(message), handle_complete, _1, close_reason);
}

Expand Down
2 changes: 1 addition & 1 deletion src/protocols/protocol_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void protocol_http::send_stream(size_t size, wire_writer&& writer,
.write = std::move(writer),
.size = size
};
message.prepare_payload();
message.content_length(size);
SEND(std::move(message), handle_stream, _1);
}

Expand Down
29 changes: 29 additions & 0 deletions test/protocols/bitcoind/bitcoind_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ BC_FIXTURE_TEST_SUITE(bitcoind_tests, bitcoind_ten_block_setup_fixture)
// blockchain
// ----------------------------------------------------------------------------

BC_AUTO_TEST_CASE(bitcoind_rpc__getblockcount__http_response__content_length)
{
const auto response = rpc_response("getblockcount");
BC_REQUIRE(response.result() == status::ok);
BC_REQUIRE(!response.chunked());
BC_REQUIRE(response.content_length().has_value());
BC_REQUIRE_EQUAL(response.content_length().value(), response.body().size());
BC_REQUIRE_EQUAL(test::parse_json(response.body()).at("result").as_int64(), 9);
}

BC_AUTO_TEST_CASE(bitcoind_rpc__getblock__hex_http_response__content_length)
{
const auto response = rpc_response("getblock", hash_param(test::block9_hash, "0"));
BC_REQUIRE(response.result() == status::ok);
BC_REQUIRE(!response.chunked());
BC_REQUIRE(response.content_length().has_value());
BC_REQUIRE_EQUAL(response.content_length().value(), response.body().size());
REQUIRE_NO_THROW_TRUE(test::parse_json(response.body()).at("result").is_string());
}

BC_AUTO_TEST_CASE(bitcoind_rpc__unknown_method__http_response__content_length)
{
const auto response = rpc_response("nonsense");
BC_REQUIRE(!response.chunked());
BC_REQUIRE(response.content_length().has_value());
BC_REQUIRE_EQUAL(response.content_length().value(), response.body().size());
BC_REQUIRE(has_code(test::parse_json(response.body()), -32601));
}

BC_AUTO_TEST_CASE(bitcoind_rpc__getblockcount__ten_block_store__nine)
{
const auto response = rpc("getblockcount");
Expand Down
7 changes: 7 additions & 0 deletions test/protocols/bitcoind/bitcoind_setup_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ system::json::value bitcoind_setup_fixture::rpc(std::string_view method,
return client_.post(body_of(method, params), "/", true);
}

network::http::message<false, network::http::string_body>
bitcoind_setup_fixture::rpc_response(std::string_view method,
std::string_view params)
{
return client_.post_response(body_of(method, params));
}

system::json::value bitcoind_setup_fixture::tcp_rpc(std::string_view method,
std::string_view params)
{
Expand Down
4 changes: 4 additions & 0 deletions test/protocols/bitcoind/bitcoind_setup_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ struct bitcoind_setup_fixture
// object). Returns the parsed json-rpc response object (with result/error).
system::json::value rpc(std::string_view method, std::string_view params="[]");

// As rpc(), returning the http response (for framing).
network::http::message<false, network::http::string_body> rpc_response(
std::string_view method, std::string_view params="[]");

// JSON-RPC over HTTP POST to "/" with a raw body (e.g. a batch). Returns
// the parsed json response, or {"dropped":true} if the channel dropped.
system::json::value rpc_body(std::string_view body);
Expand Down
Loading