forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-hello-world.cpp
160 lines (135 loc) · 4.63 KB
/
test-hello-world.cpp
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
/*
This file is part of TON Blockchain source code.
TON Blockchain is free software; you can redistribute it and/or
modify it under the terms of the GNU 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 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 General Public License for more details.
You should have received a copy of the GNU General Public License
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
You must obey the GNU General Public License in all respects for all
of the code used other than OpenSSL. If you modify file(s) with this
exception, you may extend this exception to your version of the file(s),
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version. If you delete this exception statement
from all source files in the program, then also delete it here.
along with TON Blockchain. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include <iostream>
#include "auto/tl/ton_api.h"
#include "auto/tl/ton_api_json.h"
#include "tl/tl_json.h"
#include "td/utils/Random.h"
namespace {
std::string config = R"json(
{
"@type" : "config.local",
"dht": [
{
"@type" : "dht.config.local",
"id" : {
"@type" : "adnl.id.pk.ed25519",
"key" : "VdeZz3BEIE8+tuPSBUNKN0jQXL/0T/SoK4ZpJ9vTCNQ="
},
"addr_list" : {
"@type" : "adnl.addressList",
"version" : 0,
"addrs" : [
{
"@type" : "adnl.address.udp",
"ip" : 2130706433,
"port" : 16000
}
]
}
}
]
}
)json";
std::string config2 = R"json(
{
"@type" : "config.local",
"dht": [
{
"@type" : "dht.config.local",
"id" : {
"@type" : "adnl.id.pk.ed25519",
"key" : "VdeZz3BEIE8+tuPSBUNKN0jQXL/0T/SoK4ZpJ9vTCNQ="
},
"addr_list" : {
"@type" : "adnl.addressList",
"version" : 0,
"addrs" : [
{
"@type" : "adnl.address.udp",
"ip" : 2130706433,
"port" : 16000
}
]
}
}
],
"adnl" : {
"@type" : "adnl.config.local"
}
}
)json";
} // namespace
int main() {
std::cout << "hello world!\n";
auto decode_encode = [](auto obj_json) {
auto as_json_value = td::json_decode(obj_json).move_as_ok();
ton::ton_api::object_ptr<ton::ton_api::Object> obj2;
from_json(obj2, as_json_value).ensure();
CHECK(obj2 != nullptr);
return td::json_encode<std::string>(td::ToJson(obj2));
};
auto test_tl_json = [&decode_encode](auto obj) {
auto obj_json = td::json_encode<std::string>(td::ToJson(obj));
std::cout << obj_json << "\n";
auto obj2_json = decode_encode(obj_json);
CHECK(obj_json == obj2_json);
};
td::Bits256 uint256;
uint256.set_ones();
test_tl_json(ton::ton_api::make_object<ton::ton_api::adnl_id_short>(uint256));
test_tl_json(ton::ton_api::make_object<ton::ton_api::testObject>(
1, ton::ton_api::make_object<ton::ton_api::adnl_id_short>(uint256),
ton::ton_api::make_object<ton::ton_api::getTestObject>()));
std::cout << decode_encode(config) << std::endl;
std::cout << decode_encode(config2) << std::endl;
auto decode_encode_local = [](auto obj_json) {
auto as_json_value = td::json_decode(obj_json).move_as_ok();
ton::ton_api::config_local config_local;
from_json(config_local, as_json_value.get_object()).ensure();
return td::json_encode<std::string>(td::ToJson(config_local));
};
std::cout << decode_encode_local(config) << std::endl;
std::cout << decode_encode_local(config2) << std::endl;
auto create_vector_bytes = [] {
std::vector<td::BufferSlice> res;
res.emplace_back("fdjskld");
res.emplace_back("fdj\0kld");
res.emplace_back("fdj\0\x01\xff\x7fkld");
return res;
};
test_tl_json(ton::ton_api::make_object<ton::ton_api::testVectorBytes>(create_vector_bytes()));
td::Bits256 x;
td::Random::secure_bytes(x.as_slice());
auto s = x.to_hex();
auto v = td::hex_decode(s).move_as_ok();
auto w = td::buffer_to_hex(x.as_slice());
td::Bits256 y;
y.as_slice().copy_from(v);
CHECK(x == y);
auto w2 = td::hex_decode(w).move_as_ok();
td::Bits256 z;
z.as_slice().copy_from(w2);
LOG_CHECK(x == z) << s << " " << w;
return 0;
}