1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

initial commit

This commit is contained in:
initial commit 2019-09-07 14:03:22 +04:00 committed by vvaltman
commit c2da007f40
1610 changed files with 398047 additions and 0 deletions

23
tl-utils/CMakeLists.txt Normal file
View file

@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
set(TL_UTILS_SOURCE
common-utils.hpp
tl-utils.hpp
tl-utils.cpp
)
add_library(tl-utils STATIC ${TL_UTILS_SOURCE})
target_include_directories(tl-utils PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
target_link_libraries(tl-utils PUBLIC tl_api ton_crypto )
set(TL_LITE_UTILS_SOURCE
common-utils.hpp
lite-utils.hpp
lite-utils.cpp
)
add_library(tl-lite-utils STATIC ${TL_LITE_UTILS_SOURCE})
target_include_directories(tl-lite-utils PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
target_link_libraries(tl-lite-utils PUBLIC tl_lite_api ton_crypto )

208
tl-utils/common-utils.hpp Normal file
View file

@ -0,0 +1,208 @@
/*
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/>.
Copyright 2017-2019 Telegram Systems LLP
*/
#pragma once
#include "tl/tl_object_parse.h"
#include "td/utils/tl_parsers.h"
#include "crypto/common/bitstring.h"
namespace ton {
template <class Tp>
td::BufferSlice serialize_tl_object(const tl_object_ptr<Tp> &T, bool boxed) {
return serialize_tl_object(T.get(), boxed);
}
template <class Tp>
td::BufferSlice serialize_tl_object(const tl_object_ptr<Tp> &T, bool boxed, td::BufferSlice &&suffix) {
return serialize_tl_object(T.get(), boxed, std::move(suffix));
}
template <class Tp>
td::BufferSlice serialize_tl_object(const tl_object_ptr<Tp> &T, bool boxed, td::Slice suffix) {
return serialize_tl_object(T.get(), boxed, std::move(suffix));
}
template <class Tp>
td::UInt256 get_tl_object_sha256(const tl_object_ptr<Tp> &T) {
return get_tl_object_sha256(T.get());
}
template <class Tp>
td::Bits256 get_tl_object_sha_bits256(const tl_object_ptr<Tp> &T) {
return get_tl_object_sha_bits256(T.get());
}
template <typename T>
td::Result<tl_object_ptr<std::enable_if_t<std::is_constructible<T>::value, T>>> fetch_tl_object(
const td::BufferSlice &data, bool boxed) {
td::TlBufferParser p(&data);
tl_object_ptr<T> R;
if (boxed) {
R = TlFetchBoxed<TlFetchObject<T>, T::ID>::parse(p);
} else {
R = move_tl_object_as<T>(T::fetch(p));
}
p.fetch_end();
if (p.get_status().is_ok()) {
return std::move(R);
} else {
return p.get_status();
}
}
template <typename T>
td::Result<tl_object_ptr<std::enable_if_t<!std::is_constructible<T>::value, T>>> fetch_tl_object(
const td::BufferSlice &data, bool boxed) {
CHECK(boxed);
td::TlBufferParser p(&data);
tl_object_ptr<T> R;
R = move_tl_object_as<T>(T::fetch(p));
p.fetch_end();
if (p.get_status().is_ok()) {
return std::move(R);
} else {
return p.get_status();
}
}
template <typename T>
td::Result<tl_object_ptr<std::enable_if_t<std::is_constructible<T>::value, T>>> fetch_tl_object(td::Slice data,
bool boxed) {
td::TlParser p(data);
tl_object_ptr<T> R;
if (boxed) {
R = TlFetchBoxed<TlFetchObject<T>, T::ID>::parse(p);
} else {
R = move_tl_object_as<T>(T::fetch(p));
}
p.fetch_end();
if (p.get_status().is_ok()) {
return std::move(R);
} else {
return p.get_status();
}
}
template <typename T>
td::Result<tl_object_ptr<std::enable_if_t<!std::is_constructible<T>::value, T>>> fetch_tl_object(td::Slice data,
bool boxed) {
CHECK(boxed);
td::TlParser p(data);
tl_object_ptr<T> R;
R = move_tl_object_as<T>(T::fetch(p));
p.fetch_end();
if (p.get_status().is_ok()) {
return std::move(R);
} else {
return p.get_status();
}
}
template <typename T>
td::Result<tl_object_ptr<std::enable_if_t<std::is_constructible<T>::value, T>>> fetch_tl_prefix(td::BufferSlice &data,
bool boxed) {
td::TlBufferParser p(&data);
tl_object_ptr<T> R;
if (boxed) {
R = TlFetchBoxed<TlFetchObject<T>, T::ID>::parse(p);
} else {
R = move_tl_object_as<T>(T::fetch(p));
}
if (p.get_status().is_ok()) {
data.confirm_read(data.size() - p.get_left_len());
return std::move(R);
} else {
return p.get_status();
}
}
template <typename T>
td::Result<tl_object_ptr<std::enable_if_t<!std::is_constructible<T>::value, T>>> fetch_tl_prefix(td::BufferSlice &data,
bool boxed) {
CHECK(boxed);
td::TlBufferParser p(&data);
tl_object_ptr<T> R;
R = move_tl_object_as<T>(T::fetch(p));
if (p.get_status().is_ok()) {
data.confirm_read(data.size() - p.get_left_len());
return std::move(R);
} else {
return p.get_status();
}
}
template <class T>
[[deprecated]] tl_object_ptr<T> clone_tl_object(const tl_object_ptr<T> &obj) {
auto B = serialize_tl_object(obj, true);
auto R = fetch_tl_object<T>(std::move(B), true);
R.ensure();
return R.move_as_ok();
}
template <class T>
td::Result<typename T::ReturnType> fetch_result(td::Slice message, bool check_end = true) {
td::TlParser parser(message);
auto result = T::fetch_result(parser);
if (check_end) {
parser.fetch_end();
}
const char *error = parser.get_error();
if (error != nullptr) {
LOG(ERROR) << "Can't parse: " << td::format::as_hex_dump<4>(message);
return td::Status::Error(500, td::Slice(error));
}
return std::move(result);
}
template <class T>
td::Result<typename T::ReturnType> fetch_result(const td::BufferSlice &message, bool check_end = true) {
td::TlBufferParser parser(&message);
auto result = T::fetch_result(parser);
if (check_end) {
parser.fetch_end();
}
const char *error = parser.get_error();
if (error != nullptr) {
LOG(ERROR) << "Can't parse: " << td::format::as_hex_dump<4>(message.as_slice());
return td::Status::Error(500, td::Slice(error));
}
return std::move(result);
}
template <class Type, class... Args>
td::BufferSlice create_serialize_tl_object(Args &&... args) {
return serialize_tl_object(create_tl_object<Type>(std::forward<Args>(args)...), true);
}
template <class Type, class... Args>
td::BufferSlice create_serialize_tl_object_suffix(td::Slice suffix, Args &&... args) {
return serialize_tl_object(create_tl_object<Type>(std::forward<Args>(args)...), true, suffix);
}
template <class Type, class... Args>
auto create_hash_tl_object(Args &&... args) {
return get_tl_object_sha_bits256(create_tl_object<Type>(std::forward<Args>(args)...));
}
} // namespace ton

132
tl-utils/lite-utils.cpp Normal file
View file

@ -0,0 +1,132 @@
/*
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/>.
Copyright 2017-2019 Telegram Systems LLP
*/
#include "tl-utils.hpp"
#include "tl/tl_object_store.h"
#include "auto/tl/lite_api.hpp"
#include "td/utils/tl_storers.h"
#include "td/utils/crypto.h"
#include "crypto/common/bitstring.h"
namespace ton {
td::BufferSlice serialize_tl_object(const lite_api::Object *T, bool boxed, td::BufferSlice &&suffix) {
td::TlStorerCalcLength X;
T->store(X);
auto l = X.get_length() + (boxed ? 4 : 0);
auto len = l + suffix.size();
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
if (boxed) {
Y.store_binary(T->get_id());
}
T->store(Y);
auto S = B.as_slice();
S.remove_prefix(l);
S.copy_from(suffix.as_slice());
suffix.clear();
return B;
}
td::BufferSlice serialize_tl_object(const lite_api::Object *T, bool boxed) {
td::TlStorerCalcLength X;
T->store(X);
auto l = X.get_length() + (boxed ? 4 : 0);
auto len = l;
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
if (boxed) {
Y.store_binary(T->get_id());
}
T->store(Y);
return B;
}
td::BufferSlice serialize_tl_object(const lite_api::Function *T, bool boxed) {
CHECK(boxed);
td::TlStorerCalcLength X;
T->store(X);
auto l = X.get_length();
auto len = l;
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
T->store(Y);
return B;
}
td::BufferSlice serialize_tl_object(const lite_api::Function *T, bool boxed, td::BufferSlice &&suffix) {
CHECK(boxed);
td::TlStorerCalcLength X;
T->store(X);
auto l = X.get_length();
auto len = l + suffix.size();
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
T->store(Y);
auto S = B.as_slice();
S.remove_prefix(l);
S.copy_from(suffix.as_slice());
suffix.clear();
return B;
}
td::UInt256 get_tl_object_sha256(const lite_api::Object *T) {
td::TlStorerCalcLength X;
T->store(X);
auto len = X.get_length() + 4;
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
Y.store_binary(T->get_id());
T->store(Y);
td::UInt256 id256;
td::sha256(B.as_slice(), id256.as_slice());
return id256;
}
td::Bits256 get_tl_object_sha_bits256(const lite_api::Object *T) {
td::TlStorerCalcLength X;
T->store(X);
auto len = X.get_length() + 4;
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
Y.store_binary(T->get_id());
T->store(Y);
td::Bits256 id256;
td::sha256(B.as_slice(), id256.as_slice());
return id256;
}
} // namespace ton

49
tl-utils/lite-utils.hpp Normal file
View file

@ -0,0 +1,49 @@
/*
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/>.
Copyright 2017-2019 Telegram Systems LLP
*/
#pragma once
#include "auto/tl/lite_api.h"
#include "tl/tl_object_parse.h"
#include "td/utils/tl_parsers.h"
#include "crypto/common/bitstring.h"
#include "common-utils.hpp"
namespace ton {
td::BufferSlice serialize_tl_object(const lite_api::Object *T, bool boxed);
td::BufferSlice serialize_tl_object(const lite_api::Function *T, bool boxed);
td::BufferSlice serialize_tl_object(const lite_api::Object *T, bool boxed, td::BufferSlice &&suffix);
td::BufferSlice serialize_tl_object(const lite_api::Function *T, bool boxed, td::BufferSlice &&suffix);
td::UInt256 get_tl_object_sha256(const lite_api::Object *T);
template <class Tp, std::enable_if_t<std::is_base_of<lite_api::Object, Tp>::value>>
td::UInt256 get_tl_object_sha256(const Tp &T) {
return get_tl_object_sha256(static_cast<const lite_api::Object *>(&T));
}
td::Bits256 get_tl_object_sha_bits256(const lite_api::Object *T);
template <class Tp, std::enable_if_t<std::is_base_of<lite_api::Object, Tp>::value>>
td::Bits256 get_tl_object_sha_bits256(const Tp &T) {
return get_tl_object_sha_bits256(static_cast<const lite_api::Object *>(&T));
}
} // namespace ton

138
tl-utils/tl-utils.cpp Normal file
View file

@ -0,0 +1,138 @@
/*
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/>.
Copyright 2017-2019 Telegram Systems LLP
*/
#include "tl-utils.hpp"
#include "tl/tl_object_store.h"
#include "auto/tl/ton_api.hpp"
#include "td/utils/tl_storers.h"
#include "td/utils/crypto.h"
#include "crypto/common/bitstring.h"
namespace ton {
td::BufferSlice serialize_tl_object(const ton_api::Object *T, bool boxed, td::BufferSlice &&suffix) {
return serialize_tl_object(T, boxed, suffix.as_slice());
}
td::BufferSlice serialize_tl_object(const ton_api::Object *T, bool boxed, td::Slice suffix) {
td::TlStorerCalcLength X;
T->store(X);
auto l = X.get_length() + (boxed ? 4 : 0);
auto len = l + suffix.size();
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
if (boxed) {
Y.store_binary(T->get_id());
}
T->store(Y);
auto S = B.as_slice();
S.remove_prefix(l);
S.copy_from(suffix);
return B;
}
td::BufferSlice serialize_tl_object(const ton_api::Object *T, bool boxed) {
td::TlStorerCalcLength X;
T->store(X);
auto l = X.get_length() + (boxed ? 4 : 0);
auto len = l;
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
if (boxed) {
Y.store_binary(T->get_id());
}
T->store(Y);
return B;
}
td::BufferSlice serialize_tl_object(const ton_api::Function *T, bool boxed) {
CHECK(boxed);
td::TlStorerCalcLength X;
T->store(X);
auto l = X.get_length();
auto len = l;
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
T->store(Y);
return B;
}
td::BufferSlice serialize_tl_object(const ton_api::Function *T, bool boxed, td::BufferSlice &&suffix) {
return serialize_tl_object(T, boxed, suffix.as_slice());
}
td::BufferSlice serialize_tl_object(const ton_api::Function *T, bool boxed, td::Slice suffix) {
CHECK(boxed);
td::TlStorerCalcLength X;
T->store(X);
auto l = X.get_length();
auto len = l + suffix.size();
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
T->store(Y);
auto S = B.as_slice();
S.remove_prefix(l);
S.copy_from(suffix);
return B;
}
td::UInt256 get_tl_object_sha256(const ton_api::Object *T) {
td::TlStorerCalcLength X;
T->store(X);
auto len = X.get_length() + 4;
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
Y.store_binary(T->get_id());
T->store(Y);
td::UInt256 id256;
td::sha256(B.as_slice(), id256.as_slice());
return id256;
}
td::Bits256 get_tl_object_sha_bits256(const ton_api::Object *T) {
td::TlStorerCalcLength X;
T->store(X);
auto len = X.get_length() + 4;
td::BufferSlice B(len);
td::TlStorerUnsafe Y(B.as_slice().ubegin());
Y.store_binary(T->get_id());
T->store(Y);
td::Bits256 id256;
td::sha256(B.as_slice(), id256.as_slice());
return id256;
}
} // namespace ton

51
tl-utils/tl-utils.hpp Normal file
View file

@ -0,0 +1,51 @@
/*
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/>.
Copyright 2017-2019 Telegram Systems LLP
*/
#pragma once
#include "auto/tl/ton_api.h"
#include "tl/tl_object_parse.h"
#include "td/utils/tl_parsers.h"
#include "crypto/common/bitstring.h"
#include "common-utils.hpp"
namespace ton {
td::BufferSlice serialize_tl_object(const ton_api::Object *T, bool boxed);
td::BufferSlice serialize_tl_object(const ton_api::Function *T, bool boxed);
td::BufferSlice serialize_tl_object(const ton_api::Object *T, bool boxed, td::BufferSlice &&suffix);
td::BufferSlice serialize_tl_object(const ton_api::Function *T, bool boxed, td::BufferSlice &&suffix);
td::BufferSlice serialize_tl_object(const ton_api::Object *T, bool boxed, td::Slice suffix);
td::BufferSlice serialize_tl_object(const ton_api::Function *T, bool boxed, td::Slice suffix);
td::UInt256 get_tl_object_sha256(const ton_api::Object *T);
template <class Tp, std::enable_if_t<std::is_base_of<ton_api::Object, Tp>::value>>
td::UInt256 get_tl_object_sha256(const Tp &T) {
return get_tl_object_sha256(static_cast<const ton_api::Object *>(&T));
}
td::Bits256 get_tl_object_sha_bits256(const ton_api::Object *T);
template <class Tp, std::enable_if_t<std::is_base_of<ton_api::Object, Tp>::value>>
td::Bits256 get_tl_object_sha_bits256(const Tp &T) {
return get_tl_object_sha_bits256(static_cast<const ton_api::Object *>(&T));
}
} // namespace ton