-
Notifications
You must be signed in to change notification settings - Fork 977
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Compress block candidates in validator-session * Compress blocks in full-node (disabled for now) --------- Co-authored-by: SpyCheese <[email protected]>
- Loading branch information
1 parent
9452c36
commit 0bcebe8
Showing
21 changed files
with
547 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
############################################################################### | ||
# Find LZ4 | ||
# | ||
# This sets the following variables: | ||
# LZ4_FOUND - True if LZ4 was found. | ||
# LZ4_INCLUDE_DIRS - Directories containing the LZ4 include files. | ||
# LZ4_LIBRARIES - Libraries needed to use LZ4. | ||
# LZ4_LIBRARY - Library needed to use LZ4. | ||
# LZ4_LIBRARY_DIRS - Library needed to use LZ4. | ||
|
||
find_package(PkgConfig REQUIRED) | ||
|
||
# If found, LZ$_* variables will be defined | ||
pkg_check_modules(LZ4 REQUIRED liblz4) | ||
|
||
if(NOT LZ4_FOUND) | ||
find_path(LZ4_INCLUDE_DIR lz4.h | ||
HINTS "${LZ4_ROOT}" "$ENV{LZ4_ROOT}" | ||
PATHS "$ENV{PROGRAMFILES}/lz4" "$ENV{PROGRAMW6432}/lz4" | ||
PATH_SUFFIXES include) | ||
|
||
find_library(LZ4_LIBRARY | ||
NAMES lz4 lz4_static | ||
HINTS "${LZ4_ROOT}" "$ENV{LZ4_ROOT}" | ||
PATHS "$ENV{PROGRAMFILES}/lz4" "$ENV{PROGRAMW6432}/lz4" | ||
PATH_SUFFIXES lib) | ||
|
||
if(LZ4_LIBRARY) | ||
set(LZ4_LIBRARIES ${LZ4_LIBRARY}) | ||
get_filename_component(LZ4_LIBRARY_DIRS ${LZ4_LIBRARY} DIRECTORY) | ||
endif() | ||
else() | ||
find_library(LZ4_LIBRARY | ||
NAMES lz4 lz4_static | ||
PATHS ${LZ4_LIBRARY_DIRS} | ||
NO_DEFAULT_PATH) | ||
endif() | ||
|
||
mark_as_advanced(LZ4_LIBRARY LZ4_INCLUDE_DIRS LZ4_LIBRARY_DIRS LZ4_LIBRARIES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
This file is part of TON Blockchain Library. | ||
TON Blockchain Library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 2 of the License, or | ||
(at your option) any later version. | ||
TON Blockchain Library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#include "td/utils/buffer.h" | ||
#include "td/utils/misc.h" | ||
#include <lz4.h> | ||
|
||
namespace td { | ||
|
||
td::BufferSlice lz4_compress(td::Slice data) { | ||
int size = narrow_cast<int>(data.size()); | ||
int buf_size = LZ4_compressBound(size); | ||
td::BufferSlice compressed(buf_size); | ||
int compressed_size = LZ4_compress_default(data.data(), compressed.data(), size, buf_size); | ||
CHECK(compressed_size > 0); | ||
return td::BufferSlice{compressed.as_slice().substr(0, compressed_size)}; | ||
} | ||
|
||
td::Result<td::BufferSlice> lz4_decompress(td::Slice data, int max_decompressed_size) { | ||
TRY_RESULT(size, narrow_cast_safe<int>(data.size())); | ||
if (max_decompressed_size < 0) { | ||
return td::Status::Error("invalid max_decompressed_size"); | ||
} | ||
td::BufferSlice decompressed(max_decompressed_size); | ||
int result = LZ4_decompress_safe(data.data(), decompressed.data(), size, max_decompressed_size); | ||
if (result < 0) { | ||
return td::Status::Error(PSTRING() << "lz4 decompression failed, error code: " << result); | ||
} | ||
if (result == max_decompressed_size) { | ||
return decompressed; | ||
} | ||
return td::BufferSlice{decompressed.as_slice().substr(0, result)}; | ||
} | ||
|
||
} // namespace td |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
This file is part of TON Blockchain Library. | ||
TON Blockchain Library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 2 of the License, or | ||
(at your option) any later version. | ||
TON Blockchain Library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#pragma once | ||
|
||
#include "td/utils/buffer.h" | ||
#include "td/utils/Status.h" | ||
|
||
namespace td { | ||
|
||
td::BufferSlice lz4_compress(td::Slice data); | ||
td::Result<td::BufferSlice> lz4_decompress(td::Slice data, int max_decompressed_size); | ||
|
||
} // namespace td |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
This file is part of TON Blockchain Library. | ||
TON Blockchain Library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 2 of the License, or | ||
(at your option) any later version. | ||
TON Blockchain Library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#pragma once | ||
#include "candidate-serializer.h" | ||
#include "tl-utils/tl-utils.hpp" | ||
#include "vm/boc.h" | ||
#include "td/utils/lz4.h" | ||
#include "validator-session-types.h" | ||
|
||
namespace ton::validatorsession { | ||
|
||
td::Result<td::BufferSlice> serialize_candidate(const tl_object_ptr<ton_api::validatorSession_candidate> &block, | ||
bool compression_enabled) { | ||
if (!compression_enabled) { | ||
return serialize_tl_object(block, true); | ||
} | ||
vm::BagOfCells boc1, boc2; | ||
TRY_STATUS(boc1.deserialize(block->data_)); | ||
if (boc1.get_root_count() != 1) { | ||
return td::Status::Error("block candidate should have exactly one root"); | ||
} | ||
std::vector<td::Ref<vm::Cell>> roots = {boc1.get_root_cell()}; | ||
TRY_STATUS(boc2.deserialize(block->collated_data_)); | ||
for (int i = 0; i < boc2.get_root_count(); ++i) { | ||
roots.push_back(boc2.get_root_cell(i)); | ||
} | ||
TRY_RESULT(data, vm::std_boc_serialize_multi(std::move(roots), 2)); | ||
td::BufferSlice compressed = td::lz4_compress(data); | ||
LOG(VALIDATOR_SESSION_DEBUG) << "Compressing block candidate: " << block->data_.size() + block->collated_data_.size() | ||
<< " -> " << compressed.size(); | ||
return create_serialize_tl_object<ton_api::validatorSession_compressedCandidate>( | ||
0, block->src_, block->round_, block->root_hash_, (int)data.size(), std::move(compressed)); | ||
} | ||
|
||
td::Result<tl_object_ptr<ton_api::validatorSession_candidate>> deserialize_candidate(td::Slice data, | ||
bool compression_enabled, | ||
int max_decompressed_data_size) { | ||
if (!compression_enabled) { | ||
return fetch_tl_object<ton_api::validatorSession_candidate>(data, true); | ||
} | ||
TRY_RESULT(f, fetch_tl_object<ton_api::validatorSession_compressedCandidate>(data, true)); | ||
if (f->decompressed_size_ > max_decompressed_data_size) { | ||
return td::Status::Error("decompressed size is too big"); | ||
} | ||
TRY_RESULT(decompressed, td::lz4_decompress(f->data_, f->decompressed_size_)); | ||
if (decompressed.size() != (size_t)f->decompressed_size_) { | ||
return td::Status::Error("decompressed size mismatch"); | ||
} | ||
TRY_RESULT(roots, vm::std_boc_deserialize_multi(decompressed)); | ||
if (roots.empty()) { | ||
return td::Status::Error("boc is empty"); | ||
} | ||
TRY_RESULT(block_data, vm::std_boc_serialize(roots[0], 31)); | ||
roots.erase(roots.begin()); | ||
TRY_RESULT(collated_data, vm::std_boc_serialize_multi(std::move(roots), 31)); | ||
LOG(VALIDATOR_SESSION_DEBUG) << "Decompressing block candidate: " << f->data_.size() << " -> " | ||
<< block_data.size() + collated_data.size(); | ||
return create_tl_object<ton_api::validatorSession_candidate>(f->src_, f->round_, f->root_hash_, std::move(block_data), | ||
std::move(collated_data)); | ||
} | ||
|
||
} // namespace ton::validatorsession |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
This file is part of TON Blockchain Library. | ||
TON Blockchain Library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 2 of the License, or | ||
(at your option) any later version. | ||
TON Blockchain Library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#pragma once | ||
#include "ton/ton-types.h" | ||
#include "auto/tl/ton_api.h" | ||
|
||
namespace ton::validatorsession { | ||
|
||
td::Result<td::BufferSlice> serialize_candidate(const tl_object_ptr<ton_api::validatorSession_candidate> &block, | ||
bool compression_enabled); | ||
td::Result<tl_object_ptr<ton_api::validatorSession_candidate>> deserialize_candidate(td::Slice data, | ||
bool compression_enabled, | ||
int max_decompressed_data_size); | ||
|
||
} // namespace ton::validatorsession |
Oops, something went wrong.