Ton

Форк
0
/
test-hello-world.cpp 
160 строк · 4.6 Кб
1
/* 
2
    This file is part of TON Blockchain source code.
3

4
    TON Blockchain is free software; you can redistribute it and/or
5
    modify it under the terms of the GNU General Public License
6
    as published by the Free Software Foundation; either version 2
7
    of the License, or (at your option) any later version.
8

9
    TON Blockchain 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 General Public License for more details.
13

14
    You should have received a copy of the GNU General Public License
15

16
    In addition, as a special exception, the copyright holders give permission 
17
    to link the code of portions of this program with the OpenSSL library. 
18
    You must obey the GNU General Public License in all respects for all 
19
    of the code used other than OpenSSL. If you modify file(s) with this 
20
    exception, you may extend this exception to your version of the file(s), 
21
    but you are not obligated to do so. If you do not wish to do so, delete this 
22
    exception statement from your version. If you delete this exception statement 
23
    from all source files in the program, then also delete it here.
24
    along with TON Blockchain.  If not, see <http://www.gnu.org/licenses/>.
25

26
    Copyright 2017-2020 Telegram Systems LLP
27
*/
28
#include <iostream>
29

30
#include "auto/tl/ton_api.h"
31
#include "auto/tl/ton_api_json.h"
32

33
#include "tl/tl_json.h"
34
#include "td/utils/Random.h"
35

36
namespace {
37
std::string config = R"json(
38
{
39
  "@type" : "config.local",
40
  "dht": [
41
    {
42
      "@type" : "dht.config.local",
43
      "id" : {
44
        "@type" : "adnl.id.pk.ed25519",
45
        "key" : "VdeZz3BEIE8+tuPSBUNKN0jQXL/0T/SoK4ZpJ9vTCNQ="
46
      },
47
      "addr_list" : {
48
        "@type" : "adnl.addressList",
49
        "version" : 0,
50
        "addrs" : [
51
          {
52
            "@type" : "adnl.address.udp",
53
            "ip" : 2130706433,
54
            "port" : 16000
55
          }
56
        ]
57
      }
58
    }
59
  ]
60
}
61
)json";
62
std::string config2 = R"json(
63
{
64
  "@type" : "config.local",
65
  "dht": [
66
    {
67
      "@type" : "dht.config.local",
68
      "id" : {
69
        "@type" : "adnl.id.pk.ed25519",
70
        "key" : "VdeZz3BEIE8+tuPSBUNKN0jQXL/0T/SoK4ZpJ9vTCNQ="
71
      },
72
      "addr_list" : {
73
        "@type" : "adnl.addressList",
74
        "version" : 0,
75
        "addrs" : [
76
          {
77
            "@type" : "adnl.address.udp",
78
            "ip" : 2130706433,
79
            "port" : 16000
80
          }
81
        ]
82
      }
83
    }
84
  ],
85
  "adnl" : {
86
    "@type" : "adnl.config.local"
87
  }
88
}
89
)json";
90
}  // namespace
91

92
int main() {
93
  std::cout << "hello world!\n";
94

95
  auto decode_encode = [](auto obj_json) {
96
    auto as_json_value = td::json_decode(obj_json).move_as_ok();
97
    ton::ton_api::object_ptr<ton::ton_api::Object> obj2;
98
    from_json(obj2, std::move(as_json_value)).ensure();
99
    CHECK(obj2 != nullptr);
100
    return td::json_encode<std::string>(td::ToJson(obj2));
101
  };
102

103
  auto test_tl_json = [&decode_encode](auto obj) {
104
    auto obj_json = td::json_encode<std::string>(td::ToJson(obj));
105
    std::cout << obj_json << "\n";
106

107
    auto obj2_json = decode_encode(obj_json);
108
    CHECK(obj_json == obj2_json);
109
  };
110

111
  td::Bits256 uint256;
112
  uint256.set_ones();
113
  test_tl_json(ton::ton_api::make_object<ton::ton_api::adnl_id_short>(uint256));
114

115
  test_tl_json(ton::ton_api::make_object<ton::ton_api::testObject>(
116
      1, ton::ton_api::make_object<ton::ton_api::adnl_id_short>(uint256),
117
      ton::ton_api::make_object<ton::ton_api::getTestObject>()));
118

119
  std::cout << decode_encode(config) << std::endl;
120
  std::cout << decode_encode(config2) << std::endl;
121

122
  auto decode_encode_local = [](auto obj_json) {
123
    auto as_json_value = td::json_decode(obj_json).move_as_ok();
124
    ton::ton_api::config_local config_local;
125
    from_json(config_local, as_json_value.get_object()).ensure();
126
    return td::json_encode<std::string>(td::ToJson(config_local));
127
  };
128
  std::cout << decode_encode_local(config) << std::endl;
129
  std::cout << decode_encode_local(config2) << std::endl;
130

131
  auto create_vector_bytes = [] {
132
    std::vector<td::BufferSlice> res;
133
    res.emplace_back("fdjskld");
134
    res.emplace_back("fdj\0kld");
135
    res.emplace_back("fdj\0\x01\xff\x7fkld");
136
    return res;
137
  };
138
  test_tl_json(ton::ton_api::make_object<ton::ton_api::testVectorBytes>(create_vector_bytes()));
139

140
  td::Bits256 x;
141
  td::Random::secure_bytes(x.as_slice());
142

143
  auto s = x.to_hex();
144

145
  auto v = td::hex_decode(s).move_as_ok();
146

147
  auto w = td::buffer_to_hex(x.as_slice());
148

149
  td::Bits256 y;
150
  y.as_slice().copy_from(v);
151

152
  CHECK(x == y);
153

154
  auto w2 = td::hex_decode(w).move_as_ok();
155
  td::Bits256 z;
156
  z.as_slice().copy_from(w2);
157

158
  LOG_CHECK(x == z) << s << " " << w;
159
  return 0;
160
}
161

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.