From 6b3e790aa15ba169c2427bd51d4b3a1fb1d81cb1 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sun, 29 Mar 2026 21:24:59 -0400 Subject: [PATCH] Integrate get_wire_block into native block data/text queries. --- src/protocols/protocol_native.cpp | 64 +++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/src/protocols/protocol_native.cpp b/src/protocols/protocol_native.cpp index a1aae08f..8e3cddfc 100644 --- a/src/protocols/protocol_native.cpp +++ b/src/protocols/protocol_native.cpp @@ -204,23 +204,65 @@ bool protocol_native::handle_get_block(const code& ec, interface::block, if (stopped(ec)) return false; + const auto& query = archive(); const auto link = to_header(height, hash); - if (const auto block = archive().get_block(link, witness)) + if (link.is_terminal()) { - const auto size = block->serialized_size(witness); - switch (media) + send_not_found(); + return true; + } + + size_t size{}; + if (!query.get_block_size(size, link, witness)) + { + send_internal_server_error(database::error::integrity); + return true; + } + + switch (media) + { + case data: { - case data: - send_chunk(to_bin(*block, size, witness)); + data_chunk out(size); + stream::flip::fast sink{ out }; + flip::bytes::fast writer{ sink }; + if (!query.get_wire_block(writer, link, witness)) + { + send_internal_server_error(database::error::integrity); return true; - case text: - send_text(to_hex(*block, size, witness)); + } + + send_chunk(std::move(out)); + return true; + } + case text: + { + data_chunk out(size); + stream::flip::fast sink{ out }; + flip::bytes::fast writer{ sink }; + if (!query.get_wire_block(writer, link, witness)) + { + send_internal_server_error(database::error::integrity); return true; - case json: - auto model = value_from(block); - inject(model.at("header"), height, link); - send_json(std::move(model), two * size); + } + + // block conversion because there's no hex_flipper. + send_text(encode_base16(out)); + return true; + } + case json: + { + const auto block = query.get_block(link, witness); + if (!block) + { + send_internal_server_error(database::error::integrity); return true; + } + + auto model = value_from(block); + inject(model.at("header"), height, link); + send_json(std::move(model), two * size); + return true; } }