|
| 1 | +/* |
| 2 | + This file is part of TON Blockchain Library. |
| 3 | +
|
| 4 | + TON Blockchain Library is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU Lesser General Public License as published by |
| 6 | + the Free Software Foundation, either version 2 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + TON Blockchain Library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public License |
| 15 | + along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | +#pragma once |
| 18 | +#include "candidate-serializer.h" |
| 19 | +#include "tl-utils/tl-utils.hpp" |
| 20 | +#include "vm/boc.h" |
| 21 | +#include "td/utils/lz4.h" |
| 22 | +#include "validator-session-types.h" |
| 23 | + |
| 24 | +namespace ton::validatorsession { |
| 25 | + |
| 26 | +td::Result<td::BufferSlice> serialize_candidate(const tl_object_ptr<ton_api::validatorSession_candidate> &block, |
| 27 | + bool compression_enabled) { |
| 28 | + if (!compression_enabled) { |
| 29 | + return serialize_tl_object(block, true); |
| 30 | + } |
| 31 | + vm::BagOfCells boc1, boc2; |
| 32 | + TRY_STATUS(boc1.deserialize(block->data_)); |
| 33 | + if (boc1.get_root_count() != 1) { |
| 34 | + return td::Status::Error("block candidate should have exactly one root"); |
| 35 | + } |
| 36 | + std::vector<td::Ref<vm::Cell>> roots = {boc1.get_root_cell()}; |
| 37 | + TRY_STATUS(boc2.deserialize(block->collated_data_)); |
| 38 | + for (int i = 0; i < boc2.get_root_count(); ++i) { |
| 39 | + roots.push_back(boc2.get_root_cell(i)); |
| 40 | + } |
| 41 | + TRY_RESULT(data, vm::std_boc_serialize_multi(std::move(roots), 2)); |
| 42 | + td::BufferSlice compressed = td::lz4_compress(data); |
| 43 | + LOG(VALIDATOR_SESSION_DEBUG) << "Compressing block candidate: " << block->data_.size() + block->collated_data_.size() |
| 44 | + << " -> " << compressed.size(); |
| 45 | + return create_serialize_tl_object<ton_api::validatorSession_compressedCandidate>( |
| 46 | + 0, block->src_, block->round_, block->root_hash_, (int)data.size(), std::move(compressed)); |
| 47 | +} |
| 48 | + |
| 49 | +td::Result<tl_object_ptr<ton_api::validatorSession_candidate>> deserialize_candidate(td::Slice data, |
| 50 | + bool compression_enabled, |
| 51 | + int max_decompressed_data_size) { |
| 52 | + if (!compression_enabled) { |
| 53 | + return fetch_tl_object<ton_api::validatorSession_candidate>(data, true); |
| 54 | + } |
| 55 | + TRY_RESULT(f, fetch_tl_object<ton_api::validatorSession_compressedCandidate>(data, true)); |
| 56 | + if (f->decompressed_size_ > max_decompressed_data_size) { |
| 57 | + return td::Status::Error("decompressed size is too big"); |
| 58 | + } |
| 59 | + TRY_RESULT(decompressed, td::lz4_decompress(f->data_, f->decompressed_size_)); |
| 60 | + if (decompressed.size() != (size_t)f->decompressed_size_) { |
| 61 | + return td::Status::Error("decompressed size mismatch"); |
| 62 | + } |
| 63 | + TRY_RESULT(roots, vm::std_boc_deserialize_multi(decompressed)); |
| 64 | + if (roots.empty()) { |
| 65 | + return td::Status::Error("boc is empty"); |
| 66 | + } |
| 67 | + TRY_RESULT(block_data, vm::std_boc_serialize(roots[0], 31)); |
| 68 | + roots.erase(roots.begin()); |
| 69 | + TRY_RESULT(collated_data, vm::std_boc_serialize_multi(std::move(roots), 31)); |
| 70 | + LOG(VALIDATOR_SESSION_DEBUG) << "Decompressing block candidate: " << f->data_.size() << " -> " |
| 71 | + << block_data.size() + collated_data.size(); |
| 72 | + return create_tl_object<ton_api::validatorSession_candidate>(f->src_, f->round_, f->root_hash_, std::move(block_data), |
| 73 | + std::move(collated_data)); |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace ton::validatorsession |
0 commit comments