Skip to content

Commit 557557b

Browse files
authored
Merge pull request #931 from evoskuil/master
Update explore queries and integrated read test.
2 parents 93b9794 + 1ad1e40 commit 557557b

File tree

6 files changed

+244
-90
lines changed

6 files changed

+244
-90
lines changed

console/executor_test_reader.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ void executor::read_test(const hash_digest&) const
3636
{
3737
logger("Wire size computation.");
3838
const auto start = fine_clock::now();
39-
const auto last = metadata_.configured.node.maximum_height_();
39+
const auto top = query_.get_top_associated();
4040
const auto concurrency = metadata_.configured.node.maximum_concurrency_();
4141

4242
size_t size{};
43-
for (auto height = zero; !cancel_ && height <= last; ++height)
43+
for (auto height = zero; !cancel_ && height <= top; ++height)
4444
{
4545
const auto link = query_.to_candidate(height);
4646
if (link.is_terminal())
@@ -64,6 +64,10 @@ void executor::read_test(const hash_digest&) const
6464
size % height % span.count());
6565
}
6666
}
67+
68+
const auto span = duration_cast<milliseconds>(fine_clock::now() - start);
69+
logger(format("Wire size (%1%) at (%2%) in (%3%) ms.") %
70+
size % top % span.count());
6771
}
6872

6973
#if defined(UNDEFINED)

include/bitcoin/node/interfaces/explore.hpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@ struct explore_methods
3333
method<"top", uint8_t, uint8_t>{ "version", "media" },
3434
method<"block", uint8_t, uint8_t, nullable<system::hash_cptr>, nullable<uint32_t>, optional<true>>{ "version", "media", "hash", "height", "witness" },
3535
method<"block_header", uint8_t, uint8_t, nullable<system::hash_cptr>, nullable<uint32_t>>{ "version", "media", "hash", "height" },
36+
method<"block_header_context", uint8_t, uint8_t, nullable<system::hash_cptr>, nullable<uint32_t>>{ "version", "media", "hash", "height" },
37+
method<"block_details", uint8_t, uint8_t, nullable<system::hash_cptr>, nullable<uint32_t>>{ "version", "media", "hash", "height" },
3638
method<"block_txs", uint8_t, uint8_t, nullable<system::hash_cptr>, nullable<uint32_t>>{ "version", "media", "hash", "height" },
37-
method<"block_fees", uint8_t, uint8_t, nullable<system::hash_cptr>, nullable<uint32_t>>{ "version", "media", "hash", "height" },
3839
method<"block_filter", uint8_t, uint8_t, uint8_t, nullable<system::hash_cptr>, nullable<uint32_t>>{ "version", "media", "type", "hash", "height" },
3940
method<"block_filter_hash", uint8_t, uint8_t, uint8_t, nullable<system::hash_cptr>, nullable<uint32_t>>{ "version", "media", "type", "hash", "height" },
4041
method<"block_filter_header", uint8_t, uint8_t, uint8_t, nullable<system::hash_cptr>, nullable<uint32_t>>{ "version", "media", "type", "hash", "height" },
4142
method<"block_tx", uint8_t, uint8_t, uint32_t, nullable<system::hash_cptr>, nullable<uint32_t>, optional<true>>{ "version", "media", "position", "hash", "height", "witness" },
4243

4344
method<"tx", uint8_t, uint8_t, system::hash_cptr, optional<true>>{ "version", "media", "hash", "witness" },
4445
method<"tx_header", uint8_t, uint8_t, system::hash_cptr>{ "version", "media", "hash" },
45-
method<"tx_fee", uint8_t, uint8_t, system::hash_cptr>{ "version", "media", "hash" },
46+
method<"tx_details", uint8_t, uint8_t, system::hash_cptr>{ "version", "media", "hash" },
4647

4748
method<"inputs", uint8_t, uint8_t, system::hash_cptr, optional<true>>{ "version", "media", "hash", "witness" },
4849
method<"input", uint8_t, uint8_t, system::hash_cptr, uint32_t, optional<true>>{ "version", "media", "hash", "index", "witness" },
@@ -73,32 +74,33 @@ struct explore_methods
7374

7475
using block = at<1>;
7576
using block_header = at<2>;
76-
using block_txs = at<3>;
77-
using block_fees = at<4>;
78-
using block_filter = at<5>;
79-
using block_filter_hash = at<6>;
80-
using block_filter_header = at<7>;
81-
using block_tx = at<8>;
82-
83-
using tx = at<9>;
84-
using tx_header = at<10>;
85-
using tx_fee = at<11>;
86-
87-
using inputs = at<12>;
88-
using input = at<13>;
89-
using input_script = at<14>;
90-
using input_witness = at<15>;
91-
92-
using outputs = at<16>;
93-
using output = at<17>;
94-
using output_script = at<18>;
95-
using output_spender = at<19>;
96-
using output_spenders = at<20>;
97-
98-
using address = at<21>;
99-
using address_confirmed = at<22>;
100-
using address_unconfirmed = at<23>;
101-
using address_balance = at<24>;
77+
using block_header_context = at<3>;
78+
using block_details = at<4>;
79+
using block_txs = at<5>;
80+
using block_filter = at<6>;
81+
using block_filter_hash = at<7>;
82+
using block_filter_header = at<8>;
83+
using block_tx = at<9>;
84+
85+
using tx = at<10>;
86+
using tx_header = at<11>;
87+
using tx_details = at<12>;
88+
89+
using inputs = at<13>;
90+
using input = at<14>;
91+
using input_script = at<15>;
92+
using input_witness = at<16>;
93+
94+
using outputs = at<17>;
95+
using output = at<18>;
96+
using output_script = at<19>;
97+
using output_spender = at<20>;
98+
using output_spenders = at<21>;
99+
100+
using address = at<22>;
101+
using address_confirmed = at<23>;
102+
using address_unconfirmed = at<24>;
103+
using address_balance = at<25>;
102104
};
103105

104106
/// ?format=data|text|json (via query string).

include/bitcoin/node/protocols/protocol_explore.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ class BCN_API protocol_explore
7272
bool handle_get_block_header(const code& ec, interface::block_header,
7373
uint8_t version, uint8_t media, std::optional<system::hash_cptr> hash,
7474
std::optional<uint32_t> height) NOEXCEPT;
75-
bool handle_get_block_txs(const code& ec, interface::block_txs,
75+
bool handle_get_block_header_context(const code& ec, interface::block_header_context,
76+
uint8_t version, uint8_t media, std::optional<system::hash_cptr> hash,
77+
std::optional<uint32_t> height) NOEXCEPT;
78+
bool handle_get_block_details(const code& ec, interface::block_details,
7679
uint8_t version, uint8_t media, std::optional<system::hash_cptr> hash,
7780
std::optional<uint32_t> height) NOEXCEPT;
78-
bool handle_get_block_fees(const code& ec, interface::block_fees,
81+
bool handle_get_block_txs(const code& ec, interface::block_txs,
7982
uint8_t version, uint8_t media, std::optional<system::hash_cptr> hash,
8083
std::optional<uint32_t> height) NOEXCEPT;
8184
bool handle_get_block_filter(const code& ec, interface::block_filter,
@@ -101,7 +104,7 @@ class BCN_API protocol_explore
101104
bool handle_get_tx_header(const code& ec, interface::tx_header,
102105
uint8_t version, uint8_t media,
103106
const system::hash_cptr& hash) NOEXCEPT;
104-
bool handle_get_tx_fee(const code& ec, interface::tx_fee,
107+
bool handle_get_tx_details(const code& ec, interface::tx_details,
105108
uint8_t version, uint8_t media,
106109
const system::hash_cptr& hash) NOEXCEPT;
107110

src/parsers/explore_target.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ code explore_target(request_t& out, const std::string_view& path) NOEXCEPT
209209
const auto component = segments[segment++];
210210
if (component == "header")
211211
method = "tx_header";
212-
else if (component == "fee")
213-
method = "tx_fee";
212+
else if (component == "details")
213+
method = "tx_details";
214214
else
215215
return error::invalid_component;
216216
}
@@ -267,11 +267,24 @@ code explore_target(request_t& out, const std::string_view& path) NOEXCEPT
267267
method = "block_tx";
268268
}
269269
else if (component == "header")
270-
method = "block_header";
270+
{
271+
if (segment == segments.size())
272+
{
273+
method = "block_header";
274+
}
275+
else
276+
{
277+
const auto subcomponent = segments[segment++];
278+
if (subcomponent == "context")
279+
method = "block_header_context";
280+
else
281+
return error::invalid_subcomponent;
282+
}
283+
}
271284
else if (component == "txs")
272285
method = "block_txs";
273-
else if (component == "fees")
274-
method = "block_fees";
286+
else if (component == "details")
287+
method = "block_details";
275288
else if (component == "filter")
276289
{
277290
if (segment == segments.size())

src/protocols/protocol_explore.cpp

Lines changed: 96 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,17 @@ void protocol_explore::start() NOEXCEPT
8484

8585
SUBSCRIBE_EXPLORE(handle_get_block, _1, _2, _3, _4, _5, _6, _7);
8686
SUBSCRIBE_EXPLORE(handle_get_block_header, _1, _2, _3, _4, _5, _6);
87+
SUBSCRIBE_EXPLORE(handle_get_block_header_context, _1, _2, _3, _4, _5, _6);
88+
SUBSCRIBE_EXPLORE(handle_get_block_details, _1, _2, _3, _4, _5, _6);
8789
SUBSCRIBE_EXPLORE(handle_get_block_txs, _1, _2, _3, _4, _5, _6);
88-
SUBSCRIBE_EXPLORE(handle_get_block_fees, _1, _2, _3, _4, _5, _6);
8990
SUBSCRIBE_EXPLORE(handle_get_block_filter, _1, _2, _3, _4, _5, _6, _7);
9091
SUBSCRIBE_EXPLORE(handle_get_block_filter_hash, _1, _2, _3, _4, _5, _6, _7);
9192
SUBSCRIBE_EXPLORE(handle_get_block_filter_header, _1, _2, _3, _4, _5, _6, _7);
9293
SUBSCRIBE_EXPLORE(handle_get_block_tx, _1, _2, _3, _4, _5, _6, _7, _8);
9394

9495
SUBSCRIBE_EXPLORE(handle_get_tx, _1, _2, _3, _4, _5, _6);
9596
SUBSCRIBE_EXPLORE(handle_get_tx_header, _1, _2, _3, _4, _5);
96-
SUBSCRIBE_EXPLORE(handle_get_tx_fee, _1, _2, _3, _4, _5);
97+
SUBSCRIBE_EXPLORE(handle_get_tx_details, _1, _2, _3, _4, _5);
9798

9899
SUBSCRIBE_EXPLORE(handle_get_inputs, _1, _2, _3, _4, _5, _6);
99100
SUBSCRIBE_EXPLORE(handle_get_input, _1, _2, _3, _4, _5, _6, _7);
@@ -318,56 +319,81 @@ bool protocol_explore::handle_get_block_header(const code& ec,
318319
return true;
319320
}
320321

321-
bool protocol_explore::handle_get_block_txs(const code& ec,
322-
interface::block_txs, uint8_t, uint8_t media,
322+
bool protocol_explore::handle_get_block_header_context(const code& ec,
323+
interface::block_header_context, uint8_t, uint8_t media,
323324
std::optional<hash_cptr> hash, std::optional<uint32_t> height) NOEXCEPT
324325
{
325326
if (stopped(ec))
326327
return false;
327328

329+
// states:
330+
// block_valid
331+
// block_confirmable
332+
// block_unconfirmable
333+
// get_header_state->unvalidated can be no header or no txs.
334+
////const auto state = query.get_header_state(link);
335+
////if (state == database::error::unvalidated)
336+
////{
337+
//// send_not_found();
338+
//// return true;
339+
////}
340+
328341
const auto& query = archive();
329-
if (const auto hashes = query.get_tx_keys(to_header(height, hash));
330-
!hashes.empty())
342+
const auto link = to_header(height, hash);
343+
database::context context{};
344+
if (query.get_context(context, link))
331345
{
332-
const auto size = hashes.size() * hash_size;
333346
switch (media)
334347
{
335348
case data:
336-
{
337-
const auto data = pointer_cast<const uint8_t>(hashes.data());
338-
send_chunk(to_chunk({ data, std::next(data, size) }));
349+
send_chunk(to_little_endian_size(context.flags));
339350
return true;
340-
}
341351
case text:
342-
{
343-
const auto data = pointer_cast<const uint8_t>(hashes.data());
344-
send_text(encode_base16({ data, std::next(data, size) }));
352+
send_text(encode_base16(to_little_endian_size(context.flags)));
345353
return true;
346-
}
347354
case json:
348-
{
349-
array out(hashes.size());
350-
std::ranges::transform(hashes, out.begin(),
351-
[](const auto& hash) { return encode_hash(hash); });
352-
send_json(out, two * size);
355+
send_json(context.flags, two * sizeof(context.flags));
353356
return true;
354-
}
355357
}
356358
}
357359

358360
send_not_found();
359361
return true;
360362
}
361363

362-
bool protocol_explore::handle_get_block_fees(const code& ec,
363-
interface::block_fees, uint8_t, uint8_t media,
364+
bool protocol_explore::handle_get_block_details(const code& ec,
365+
interface::block_details, uint8_t, uint8_t media,
364366
std::optional<hash_cptr> hash, std::optional<uint32_t> height) NOEXCEPT
365367
{
366368
if (stopped(ec))
367369
return false;
368370

369-
if (const auto fees = archive().get_block_fees(to_header(height, hash));
370-
fees != max_uint64)
371+
const auto& query = archive();
372+
const auto link = to_header(height, hash);
373+
const auto state = query.get_block_state(link);
374+
375+
// get_block_state->unassociated can be no header or no txs.
376+
if (state == database::error::unassociated)
377+
{
378+
send_not_found();
379+
return true;
380+
}
381+
382+
// states:
383+
// unvalidated
384+
// block_valid
385+
// block_confirmable
386+
// block_unconfirmable
387+
388+
// both txs table (can get from details)
389+
//const auto size = query.get_block_size(link);
390+
//const auto count = query.get_tx_count(link);
391+
392+
// TODO:
393+
// query (whole block and all prevouts, same as get_block_fees)
394+
// fees, claim, reward, subsidy, weight, size, count.
395+
396+
if (const auto fees = query.get_block_fees(link); fees != max_uint64)
371397
{
372398
switch (media)
373399
{
@@ -387,6 +413,47 @@ bool protocol_explore::handle_get_block_fees(const code& ec,
387413
return true;
388414
}
389415

416+
bool protocol_explore::handle_get_block_txs(const code& ec,
417+
interface::block_txs, uint8_t, uint8_t media,
418+
std::optional<hash_cptr> hash, std::optional<uint32_t> height) NOEXCEPT
419+
{
420+
if (stopped(ec))
421+
return false;
422+
423+
const auto& query = archive();
424+
if (const auto hashes = query.get_tx_keys(to_header(height, hash));
425+
!hashes.empty())
426+
{
427+
const auto size = hashes.size() * hash_size;
428+
switch (media)
429+
{
430+
case data:
431+
{
432+
const auto data = pointer_cast<const uint8_t>(hashes.data());
433+
send_chunk(to_chunk({ data, std::next(data, size) }));
434+
return true;
435+
}
436+
case text:
437+
{
438+
const auto data = pointer_cast<const uint8_t>(hashes.data());
439+
send_text(encode_base16({ data, std::next(data, size) }));
440+
return true;
441+
}
442+
case json:
443+
{
444+
array out(hashes.size());
445+
std::ranges::transform(hashes, out.begin(),
446+
[](const auto& hash) { return encode_hash(hash); });
447+
send_json(out, two * size);
448+
return true;
449+
}
450+
}
451+
}
452+
453+
send_not_found();
454+
return true;
455+
}
456+
390457
bool protocol_explore::handle_get_block_filter(const code& ec,
391458
interface::block_filter, uint8_t, uint8_t media, uint8_t type,
392459
std::optional<hash_cptr> hash, std::optional<uint32_t> height) NOEXCEPT
@@ -595,12 +662,14 @@ bool protocol_explore::handle_get_tx_header(const code& ec,
595662
return true;
596663
}
597664

598-
bool protocol_explore::handle_get_tx_fee(const code& ec, interface::tx_fee,
599-
uint8_t, uint8_t media, const hash_cptr& hash) NOEXCEPT
665+
bool protocol_explore::handle_get_tx_details(const code& ec,
666+
interface::tx_details, uint8_t, uint8_t media,
667+
const hash_cptr& hash) NOEXCEPT
600668
{
601669
if (stopped(ec))
602670
return false;
603671

672+
// TODO: expand details to include tx.size and tx.weight.
604673
const auto& query = archive();
605674
if (const auto fee = query.get_tx_fee(query.to_tx(*hash));
606675
fee != max_uint64)

0 commit comments

Comments
 (0)