Skip to content

Commit 9eeabc9

Browse files
authored
Merge pull request #670 from evoskuil/master
Integrate get_wire_block into native block data/text queries.
2 parents 8cf8a86 + 6b3e790 commit 9eeabc9

1 file changed

Lines changed: 53 additions & 11 deletions

File tree

src/protocols/protocol_native.cpp

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,65 @@ bool protocol_native::handle_get_block(const code& ec, interface::block,
204204
if (stopped(ec))
205205
return false;
206206

207+
const auto& query = archive();
207208
const auto link = to_header(height, hash);
208-
if (const auto block = archive().get_block(link, witness))
209+
if (link.is_terminal())
209210
{
210-
const auto size = block->serialized_size(witness);
211-
switch (media)
211+
send_not_found();
212+
return true;
213+
}
214+
215+
size_t size{};
216+
if (!query.get_block_size(size, link, witness))
217+
{
218+
send_internal_server_error(database::error::integrity);
219+
return true;
220+
}
221+
222+
switch (media)
223+
{
224+
case data:
212225
{
213-
case data:
214-
send_chunk(to_bin(*block, size, witness));
226+
data_chunk out(size);
227+
stream::flip::fast sink{ out };
228+
flip::bytes::fast writer{ sink };
229+
if (!query.get_wire_block(writer, link, witness))
230+
{
231+
send_internal_server_error(database::error::integrity);
215232
return true;
216-
case text:
217-
send_text(to_hex(*block, size, witness));
233+
}
234+
235+
send_chunk(std::move(out));
236+
return true;
237+
}
238+
case text:
239+
{
240+
data_chunk out(size);
241+
stream::flip::fast sink{ out };
242+
flip::bytes::fast writer{ sink };
243+
if (!query.get_wire_block(writer, link, witness))
244+
{
245+
send_internal_server_error(database::error::integrity);
218246
return true;
219-
case json:
220-
auto model = value_from(block);
221-
inject(model.at("header"), height, link);
222-
send_json(std::move(model), two * size);
247+
}
248+
249+
// block conversion because there's no hex_flipper.
250+
send_text(encode_base16(out));
251+
return true;
252+
}
253+
case json:
254+
{
255+
const auto block = query.get_block(link, witness);
256+
if (!block)
257+
{
258+
send_internal_server_error(database::error::integrity);
223259
return true;
260+
}
261+
262+
auto model = value_from(block);
263+
inject(model.at("header"), height, link);
264+
send_json(std::move(model), two * size);
265+
return true;
224266
}
225267
}
226268

0 commit comments

Comments
 (0)