forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon-utils.hpp
211 lines (183 loc) · 6.35 KB
/
common-utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
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-2020 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) {
Type object(std::forward<Args>(args)...);
return serialize_tl_object(&object, true);
}
template <class Type, class... Args>
td::BufferSlice create_serialize_tl_object_suffix(td::Slice suffix, Args &&... args) {
Type object(std::forward<Args>(args)...);
return serialize_tl_object(&object, true, suffix);
}
template <class Type, class... Args>
auto create_hash_tl_object(Args &&... args) {
Type object(std::forward<Args>(args)...);
return get_tl_object_sha_bits256(&object);
}
} // namespace ton