Skip to content

Commit 51a0c05

Browse files
committed
Rename tx_block to tx_header, sort/dedup address.
1 parent f478ae1 commit 51a0c05

File tree

4 files changed

+57
-33
lines changed

4 files changed

+57
-33
lines changed

include/bitcoin/node/protocols/protocol_explore.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_EXPLORE_HPP
2020
#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_EXPLORE_HPP
2121

22+
#include <atomic>
2223
#include <memory>
2324
#include <optional>
2425
#include <bitcoin/node/define.hpp>
@@ -95,7 +96,7 @@ class BCN_API protocol_explore
9596
bool handle_get_tx(const code& ec, interface::tx,
9697
uint8_t version, uint8_t media, const system::hash_cptr& hash,
9798
bool witness) NOEXCEPT;
98-
bool handle_get_tx_block(const code& ec, interface::tx_block,
99+
bool handle_get_tx_header(const code& ec, interface::tx_header,
99100
uint8_t version, uint8_t media,
100101
const system::hash_cptr& hash) NOEXCEPT;
101102
bool handle_get_tx_fee(const code& ec, interface::tx_fee,
@@ -152,6 +153,7 @@ class BCN_API protocol_explore
152153
const std::optional<system::hash_cptr>& hash) NOEXCEPT;
153154

154155
dispatcher dispatcher_{};
156+
std::atomic_bool stopping_{};
155157
};
156158

157159
} // namespace node

src/parse/target.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ code parse_target(request_t& out, const std::string_view& path) NOEXCEPT
199199
else
200200
{
201201
const auto component = segments[segment++];
202-
if (component == "block")
203-
method = "tx_block";
202+
if (component == "header")
203+
method = "tx_header";
204204
else if (component == "fee")
205205
method = "tx_fee";
206206
else

src/protocols/protocol_explore.cpp

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <algorithm>
2222
#include <optional>
2323
#include <ranges>
24+
#include <set>
2425
#include <utility>
2526
#include <bitcoin/node/define.hpp>
2627
#include <bitcoin/node/parse/parse.hpp>
@@ -40,6 +41,27 @@ using namespace network::messages::peer;
4041
using namespace std::placeholders;
4142
using namespace boost::json;
4243

44+
using point_set = std::set<chain::point>;
45+
using outpoint_set = std::set<chain::outpoint>;
46+
47+
DEFINE_JSON_TO_TAG(point_set)
48+
{
49+
point_set out{};
50+
for (const auto& point : value.as_array())
51+
out.insert(value_to<chain::point>(point));
52+
53+
return out;
54+
}
55+
56+
DEFINE_JSON_TO_TAG(outpoint_set)
57+
{
58+
outpoint_set out{};
59+
for (const auto& outpoint : value.as_array())
60+
out.insert(value_to<chain::outpoint>(outpoint));
61+
62+
return out;
63+
}
64+
4365
// Avoiding namespace conflict.
4466
using object_type = network::rpc::object_t;
4567

@@ -70,7 +92,7 @@ void protocol_explore::start() NOEXCEPT
7092
SUBSCRIBE_EXPLORE(handle_get_block_tx, _1, _2, _3, _4, _5, _6, _7, _8);
7193

7294
SUBSCRIBE_EXPLORE(handle_get_tx, _1, _2, _3, _4, _5, _6);
73-
SUBSCRIBE_EXPLORE(handle_get_tx_block, _1, _2, _3, _4, _5);
95+
SUBSCRIBE_EXPLORE(handle_get_tx_header, _1, _2, _3, _4, _5);
7496
SUBSCRIBE_EXPLORE(handle_get_tx_fee, _1, _2, _3, _4, _5);
7597

7698
SUBSCRIBE_EXPLORE(handle_get_inputs, _1, _2, _3, _4, _5, _6);
@@ -94,6 +116,7 @@ void protocol_explore::start() NOEXCEPT
94116
void protocol_explore::stopping(const code& ec) NOEXCEPT
95117
{
96118
BC_ASSERT(stranded());
119+
stopping_.store(true);
97120
dispatcher_.stop(ec);
98121
protocol_html::stopping(ec);
99122
}
@@ -532,37 +555,40 @@ bool protocol_explore::handle_get_tx(const code& ec, interface::tx, uint8_t,
532555
return true;
533556
}
534557

535-
bool protocol_explore::handle_get_tx_block(const code& ec, interface::tx_block,
558+
bool protocol_explore::handle_get_tx_header(const code& ec, interface::tx_header,
536559
uint8_t, uint8_t media, const hash_cptr& hash) NOEXCEPT
537560
{
538561
if (stopped(ec))
539562
return false;
540563

541564
const auto& query = archive();
542-
const auto block = query.to_confirmed_block(*hash);
543-
if (block.is_terminal())
565+
const auto link = query.to_confirmed_block(*hash);
566+
if (link.is_terminal())
544567
{
545568
send_not_found();
546569
return true;
547570
}
548571

549-
const auto key = query.get_header_key(block);
550-
if (key == null_hash)
572+
const auto header = query.get_header(link);
573+
if (!header)
551574
{
552575
send_internal_server_error(database::error::integrity);
553576
return true;
554577
}
555578

579+
constexpr auto size = chain::header::serialized_size();
556580
switch (media)
557581
{
558582
case data:
559-
send_chunk(to_chunk(key));
583+
send_chunk(to_bin(*header, size));
560584
return true;
561585
case text:
562-
send_text(encode_base16(key));
586+
send_text(to_hex(*header, size));
563587
return true;
564588
case json:
565-
send_json(value_from(encode_hash(key)), two * hash_size);
589+
auto model = value_from(header);
590+
inject(model, {}, link);
591+
send_json(std::move(model), two * size);
566592
return true;
567593
}
568594

@@ -893,12 +919,9 @@ bool protocol_explore::handle_get_output_spenders(const code& ec,
893919
return true;
894920
}
895921

896-
// TODO: dedup and lexical sort.
897-
chain::points out(points.size());
898-
std::ranges::transform(points, out.begin(), [&](const auto& link) NOEXCEPT
899-
{
900-
return query.get_spender(link);
901-
});
922+
point_set out{};
923+
for (const auto& point: points)
924+
out.insert(query.get_spender(point));
902925

903926
const auto size = out.size() * chain::point::serialized_size();
904927
switch (media)
@@ -931,8 +954,10 @@ bool protocol_explore::handle_get_address(const code& ec, interface::address,
931954
return true;
932955
}
933956

957+
// TODO: post queries to thread (both stopping() and this are stranded).
958+
934959
database::output_links outputs{};
935-
if (!query.to_address_outputs(outputs, *hash))
960+
if (!query.to_address_outputs(stopping_, outputs, *hash))
936961
{
937962
send_internal_server_error(database::error::integrity);
938963
return true;
@@ -944,14 +969,11 @@ bool protocol_explore::handle_get_address(const code& ec, interface::address,
944969
return true;
945970
}
946971

947-
// TODO: dedup and lexical sort.
948-
chain::points out(outputs.size());
949-
std::ranges::transform(outputs, out.begin(), [&](const auto& link) NOEXCEPT
950-
{
951-
return query.get_spent(link);
952-
});
972+
outpoint_set out{};
973+
for (const auto& output: outputs)
974+
out.insert(query.get_spent(output));
953975

954-
const auto size = out.size() * chain::point::serialized_size();
976+
const auto size = out.size() * chain::outpoint::serialized_size();
955977
switch (media)
956978
{
957979
case data:

test/parse/target.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,15 +424,15 @@ BOOST_AUTO_TEST_CASE(parse__parse_target__tx_invalid_component__invalid_componen
424424
BOOST_REQUIRE_EQUAL(parse_target(out, path), node::error::invalid_component);
425425
}
426426

427-
// tx_block
427+
// tx_header
428428

429-
BOOST_AUTO_TEST_CASE(parse__parse_target__tx_block_valid__expected)
429+
BOOST_AUTO_TEST_CASE(parse__parse_target__tx_header_valid__expected)
430430
{
431-
const std::string path = "/v42/tx/0000000000000000000000000000000000000000000000000000000000000042/block";
431+
const std::string path = "/v42/tx/0000000000000000000000000000000000000000000000000000000000000042/header";
432432

433433
request_t request{};
434434
BOOST_REQUIRE(!parse_target(request, path));
435-
BOOST_REQUIRE_EQUAL(request.method, "tx_block");
435+
BOOST_REQUIRE_EQUAL(request.method, "tx_header");
436436
BOOST_REQUIRE(request.params.has_value());
437437

438438
const auto& params = request.params.value();
@@ -452,16 +452,16 @@ BOOST_AUTO_TEST_CASE(parse__parse_target__tx_block_valid__expected)
452452
BOOST_REQUIRE_EQUAL(to_uintx(*hash_cptr), uint256_t{ 0x42 });
453453
}
454454

455-
BOOST_AUTO_TEST_CASE(parse__parse_target__tx_block_invalid_component__invalid_component)
455+
BOOST_AUTO_TEST_CASE(parse__parse_target__tx_header_invalid_component__invalid_component)
456456
{
457457
const std::string path = "/v3/tx/0000000000000000000000000000000000000000000000000000000000000000/invalid";
458458
request_t out{};
459459
BOOST_REQUIRE_EQUAL(parse_target(out, path), node::error::invalid_component);
460460
}
461461

462-
BOOST_AUTO_TEST_CASE(parse__parse_target__tx_block_extra_segment__extra_segment)
462+
BOOST_AUTO_TEST_CASE(parse__parse_target__tx_header_extra_segment__extra_segment)
463463
{
464-
const std::string path = "/v3/tx/0000000000000000000000000000000000000000000000000000000000000000/block/extra";
464+
const std::string path = "/v3/tx/0000000000000000000000000000000000000000000000000000000000000000/header/extra";
465465
request_t out{};
466466
BOOST_REQUIRE_EQUAL(parse_target(out, path), node::error::extra_segment);
467467
}

0 commit comments

Comments
 (0)