6 #ifndef KAGOME_ADAPTERS_PROTOBUF_BLOCK_RESPONSE 7 #define KAGOME_ADAPTERS_PROTOBUF_BLOCK_RESPONSE 12 #include "scale/scale.hpp" 22 static std::vector<uint8_t>::iterator
write(
24 std::vector<uint8_t> &out,
25 std::vector<uint8_t>::iterator loaded) {
26 ::api::v1::BlockResponse msg;
27 for (
const auto &src_block : t.
blocks) {
28 auto *dst_block = msg.add_blocks();
29 dst_block->set_hash(src_block.hash.toString());
32 dst_block->set_header(
33 vector_to_string(scale::encode(*src_block.header).value()));
36 for (
const auto &ext_body : *src_block.body)
38 vector_to_string(scale::encode(ext_body).value()));
40 if (src_block.receipt)
41 dst_block->set_receipt(src_block.receipt->toString());
43 if (src_block.message_queue)
44 dst_block->set_message_queue(src_block.message_queue->toString());
46 if (src_block.justification) {
47 dst_block->set_justification(
48 src_block.justification->data.toString());
50 dst_block->set_is_empty_justification(
51 src_block.justification->data.empty());
58 static outcome::result<std::vector<uint8_t>::const_iterator>
read(
60 const std::vector<uint8_t> &src,
61 std::vector<uint8_t>::const_iterator from) {
62 const auto remains = src.size() - std::distance(src.begin(), from);
63 assert(remains >= size(out));
65 ::api::v1::BlockResponse msg;
66 if (!msg.ParseFromArray(from.base(), remains))
69 auto &dst_blocks = out.
blocks;
70 dst_blocks.reserve(msg.blocks().size());
71 for (
const auto &src_block_data : msg.blocks()) {
75 OUTCOME_TRY(header, extract_value<primitives::BlockHeader>([&]() {
76 return src_block_data.header();
79 std::optional<primitives::BlockBody> bodies;
80 for (
const auto &b : src_block_data.body()) {
84 body, extract_value<primitives::Extrinsic>([&]() {
return b; }));
85 bodies->emplace_back(std::move(body));
93 std::optional<primitives::Justification> justification;
95 if (not src_block_data.justification().empty()
96 || src_block_data.is_empty_justification()) {
103 dst_blocks.emplace_back(
105 .header = std::move(header),
106 .body = std::move(bodies),
107 .receipt = std::move(receipt),
108 .message_queue = std::move(message_queue),
109 .justification = std::move(justification)});
112 std::advance(from, msg.ByteSizeLong());
117 template <
typename T,
typename F>
119 if (
const auto &buffer = std::forward<F>(f)(); !buffer.empty()) {
122 scale::decode<T>(gsl::span<const uint8_t>(
123 reinterpret_cast<const uint8_t *>(buffer.data()),
125 return std::move(decoded);
131 return std::string(reinterpret_cast<char *>(src.data()),
138 #endif // KAGOME_ADAPTERS_PROTOBUF_BLOCK_RESPONSE static std::vector< uint8_t >::iterator write(const BlocksResponse &t, std::vector< uint8_t > &out, std::vector< uint8_t >::iterator loaded)
std::vector< Extrinsic > BlockBody
std::vector< uint8_t >::iterator appendToVec(const T &msg, std::vector< uint8_t > &out, std::vector< uint8_t >::iterator loaded)
static outcome::result< T > extract_value(F &&f)
static size_t size(const BlocksResponse &t)
static outcome::result< Blob< size_ > > fromString(std::string_view data)
static std::string vector_to_string(std::vector< uint8_t > &&src)
static outcome::result< std::vector< uint8_t >::const_iterator > read(BlocksResponse &out, const std::vector< uint8_t > &src, std::vector< uint8_t >::const_iterator from)
static SLBuffer fromString(const std::string_view &src)
stores content of a string to byte array
Base::iterator emplace(Iter pos, Args &&...args)
primitives::BlockHash hash
common::SLVector< primitives::BlockData, kMaxBlocksInResponse > blocks