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
4 changes: 2 additions & 2 deletions src/protocols/native/protocol_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ bool protocol_native::handle_log(const code& ec, uint8_t , time_t ,
if (!websocket())
return true;

// TODO: json only.
// TODO: map subscribed flags to event filter, emit single json event.
return false;
}

Expand All @@ -261,7 +261,7 @@ bool protocol_native::handle_events(const code& ec, uint8_t ,
if (!websocket())
return true;

// TODO: json only.
// TODO: map subscribed flags to event filter, emit single json event.
return false;
}

Expand Down
2 changes: 0 additions & 2 deletions src/protocols/native/protocol_native_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,7 @@ void protocol_native::do_block(node::header_t link, media_type media) NOEXCEPT
notify_text(encode_base16(hash));
return;
case json:
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
notify_json(value_from(encode_base16(hash)), two * hash_size);
BC_POP_WARNING()
return;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/protocols/native/protocol_native_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ void protocol_native::do_transaction(node::transaction_t link,
notify_text(encode_base16(hash));
return;
case json:
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
notify_json(value_from(encode_base16(hash)), two * hash_size);
BC_POP_WARNING()
return;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/protocols/protocol_html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void protocol_html::send_empty(const request& request) NOEXCEPT
void protocol_html::notify_json(boost::json::value&& model, size_t size_hint,
const request& request) NOEXCEPT
{
BC_ASSERT(stranded());
BC_ASSERT(stranded() && websocket());
response response{ status::ok, request.version() };
response.body() = json_value
{
Expand All @@ -288,7 +288,7 @@ void protocol_html::notify_json(boost::json::value&& model, size_t size_hint,
void protocol_html::notify_text(std::string&& hexidecimal,
const request& request) NOEXCEPT
{
BC_ASSERT(stranded());
BC_ASSERT(stranded() && websocket());
response response{ status::ok, request.version() };
response.body() = std::move(hexidecimal);
NOTIFY(std::move(response), handle_complete, _1, error::success);
Expand All @@ -297,15 +297,15 @@ void protocol_html::notify_text(std::string&& hexidecimal,
void protocol_html::notify_chunk(system::data_chunk&& bytes,
const request& request) NOEXCEPT
{
BC_ASSERT(stranded());
BC_ASSERT(stranded() && websocket());
response response{ status::ok, request.version() };
response.body() = std::move(bytes);
NOTIFY(std::move(response), handle_complete, _1, error::success);
}

void protocol_html::notify_empty(const request& request) NOEXCEPT
{
BC_ASSERT(stranded());
BC_ASSERT(stranded() && websocket());
response response{ status::ok, request.version() };
response.body() = empty_value{};
NOTIFY(std::move(response), handle_complete, _1, error::success);
Expand Down
1 change: 1 addition & 0 deletions test/protocols/electrum/electrum_setup_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ electrum_setup_fixture::electrum_setup_fixture(const initializer& setup,
electrum.maximum_history = 5;
electrum.maximum_headers = 5;
electrum.connections = 1;
electrum.inactivity_minutes = 1;
database_settings.interval_depth = 2;
node_settings.delay_inbound = false;
node_settings.minimum_fee_rate = 99.0;
Expand Down
4 changes: 2 additions & 2 deletions test/protocols/electrum/electrum_setup_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ struct electrum_setup_fixture
boost::json::value receive();
int64_t get_error(const std::string& request);
boost::json::value get(const std::string& request);
void notify(node::chase event_, node::event_value value=0_u32);
void notify(node::chase event_, node::event_value value={});
bool handshake(electrum::version version,
const std::string& name="test", network::rpc::code_t id=0);
const std::string& name="test", network::rpc::code_t id={});

protected:
configuration config_;
Expand Down
54 changes: 45 additions & 9 deletions test/protocols/native/native_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ BOOST_AUTO_TEST_CASE(native__top_subscribe__json__expected)
// top (websockets)
// ----------------------------------------------------------------------------

BOOST_AUTO_TEST_CASE(native__ws_upgrade__always__expected)
BOOST_AUTO_TEST_CASE(native__ws_upgrade__always__success)
{
const auto ec = ws_upgrade();
BOOST_REQUIRE_MESSAGE(!ec, ec.message());
Expand Down Expand Up @@ -118,13 +118,49 @@ BOOST_AUTO_TEST_CASE(native__ws_top_subscribe__json__expected)
BOOST_REQUIRE_EQUAL(response.as_int64(), 9);
}

////BOOST_AUTO_TEST_CASE(native__ws_top_subscribe__stop__expected)
////{
//// BOOST_REQUIRE(!ws_upgrade());
////
//// const auto response = ws_get_json("/v1/top/subscribe?stop=true");
//// BOOST_REQUIRE(response.is_int64());
//// BOOST_REQUIRE_EQUAL(response.as_int64(), 9);
////}
BOOST_AUTO_TEST_CASE(native__ws_top_subscribe__stop__empty)
{
BOOST_REQUIRE(!ws_upgrade());

const auto response = ws_get_text("/v1/top/subscribe?stop=true");
BOOST_REQUIRE(response.empty());
}

BOOST_AUTO_TEST_CASE(native__ws_top_subscribe__repeat__idempotent)
{
BOOST_REQUIRE(!ws_upgrade());

const auto response1 = ws_get_json("/v1/top/subscribe?format=json");
BOOST_REQUIRE(response1.is_int64());
BOOST_REQUIRE_EQUAL(response1.as_int64(), 9);

const auto response2 = ws_get_json("/v1/top/subscribe?format=json");
BOOST_REQUIRE(response2.is_int64());
BOOST_REQUIRE_EQUAL(response2.as_int64(), 9);
}

BOOST_AUTO_TEST_CASE(native__ws_top_subscribe__progressive_notify__expected)
{
BOOST_REQUIRE(!ws_upgrade());

BOOST_REQUIRE(query_.set(test::bogus_block10, database::context{ 0, 10, 0 }, false, false));
BOOST_REQUIRE(query_.set(test::bogus_block11, database::context{ 0, 11, 0 }, false, false));
BOOST_REQUIRE(query_.set(test::bogus_block12, database::context{ 0, 12, 0 }, false, false));
const auto hash10 = test::bogus_block10.transactions_ptr()->at(1)->hash(false);
const auto hash11 = test::bogus_block11.transactions_ptr()->at(0)->hash(false);
const auto hash12 = test::bogus_block12.transactions_ptr()->at(0)->hash(false);

const auto response = ws_get_json("/v1/top/subscribe?format=json");
BOOST_REQUIRE(response.is_int64());
BOOST_REQUIRE_EQUAL(response.as_int64(), 9);

BOOST_REQUIRE(query_.push_confirmed(query_.to_header(test::bogus_block10.hash()), true));
BOOST_REQUIRE_EQUAL(ws_get_text("/v1/top/subscribe?format=text"), "0a");

BOOST_REQUIRE(query_.push_confirmed(query_.to_header(test::bogus_block11.hash()), true));
notify(node::chase::organized, node::header_t{ 11 });

BOOST_REQUIRE_EQUAL(to_string(ws_receive()), "0b");
}

BOOST_AUTO_TEST_SUITE_END()
23 changes: 21 additions & 2 deletions test/protocols/native/native_setup_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ native_setup_fixture::native_setup_fixture(const initializer& setup)
native.binds = { { NATIVE_ENDPOINT } };
native.connections = 1;
native.path = "unused";
native.inactivity_minutes = 1;
database_settings.interval_depth = 2;
node_settings.delay_inbound = false;
node_settings.minimum_fee_rate = 99.0;
Expand Down Expand Up @@ -108,6 +109,19 @@ native_setup_fixture::~native_setup_fixture()

BC_POP_WARNING()

// utility
boost::json::value parse_json(std::string_view value)
{
try
{
return boost::json::parse(value);
}
catch (...)
{
return {};
}
}

native_setup_fixture::string_request
native_setup_fixture::create_request(std::string_view target)
{
Expand Down Expand Up @@ -185,7 +199,7 @@ boost::json::value native_setup_fixture::get_json(std::string_view target)
BOOST_CHECK_MESSAGE(!ec, ec.message());
BOOST_CHECK_EQUAL(response.result(), http::status::ok);

return boost::json::parse(response.body());
return parse_json(response.body());
}

network::boost_code native_setup_fixture::ws_upgrade()
Expand Down Expand Up @@ -239,7 +253,7 @@ std::string native_setup_fixture::ws_get_text(std::string_view message)
boost::json::value native_setup_fixture::ws_get_json(
std::string_view message)
{
return boost::json::parse(ws_get_text(message));
return parse_json(ws_get_text(message));
}

data_chunk native_setup_fixture::ws_get_data(std::string_view message)
Expand All @@ -252,3 +266,8 @@ data_chunk native_setup_fixture::ws_get_data(std::string_view message)

return ws_receive();
}

void native_setup_fixture::notify(node::chase event_, node::event_value value)
{
server_.notify(node::error::success, event_, value);
}
2 changes: 2 additions & 0 deletions test/protocols/native/native_setup_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct native_setup_fixture
boost::json::value ws_get_json(std::string_view message);
system::data_chunk ws_get_data(std::string_view message);

void notify(node::chase event_, node::event_value value={});

protected:
server::configuration config_;
test::store_t store_;
Expand Down
Loading