Ton

Форк
0
/
test-block.cpp 
248 строк · 10.7 Кб
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
    along with TON Blockchain.  If not, see <http://www.gnu.org/licenses/>.
16

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

26
    Copyright 2017-2019 Telegram Systems LLP
27
*/
28
#include "block/block.h"
29
#include "vm/boc.h"
30
#include <iostream>
31
#include "block-db.h"
32
#include "block-auto.h"
33
#include "block-parse.h"
34
#include "vm/cp0.h"
35
#include <getopt.h>
36

37
using td::Ref;
38

39
int verbosity;
40

41
struct IntError {
42
  std::string err_msg;
43
  IntError(std::string _msg) : err_msg(_msg) {
44
  }
45
  IntError(const char* _msg) : err_msg(_msg) {
46
  }
47
};
48

49
td::Ref<vm::Cell> load_boc(std::string filename) {
50
  std::cerr << "loading bag-of-cell file " << filename << std::endl;
51
  auto bytes_res = block::load_binary_file(filename);
52
  if (bytes_res.is_error()) {
53
    throw IntError{PSTRING() << "cannot load file `" << filename << "` : " << bytes_res.move_as_error()};
54
  }
55
  vm::BagOfCells boc;
56
  auto res = boc.deserialize(bytes_res.move_as_ok());
57
  if (res.is_error()) {
58
    throw IntError{PSTRING() << "cannot deserialize bag-of-cells " << res.move_as_error()};
59
  }
60
  if (res.move_as_ok() <= 0 || boc.get_root_cell().is_null()) {
61
    throw IntError{"cannot deserialize bag-of-cells "};
62
  }
63
  return boc.get_root_cell();
64
}
65

66
void test1() {
67
  block::ShardId id{ton::masterchainId}, id2{ton::basechainId, 0x11efULL << 48};
68
  std::cout << '[' << id << "][" << id2 << ']' << std::endl;
69
  vm::CellBuilder cb;
70
  cb << id << id2;
71
  std::cout << "ShardIdent.pack() = " << block::tlb::t_ShardIdent.pack(cb, {12, 3, 0x3aeULL << 52}) << std::endl;
72
  std::cout << cb << std::endl;
73
  auto cref = cb.finalize();
74
  td::Ref<vm::CellSlice> cs{true, cref}, cs2;
75
  block::ShardId id3{cs.write()}, id4, id5;
76
  cs >> id4 >> id5;
77
  std::cout << '[' << id3 << "][" << id4 << "][" << id5 << ']' << std::endl;
78
  vm::CellSlice csl{std::move(cref)};
79
  std::cout << "ShardIdent.get_size() = " << block::tlb::t_ShardIdent.get_size(csl) << std::endl;
80
  std::cout << "MsgAddress.get_size() = " << block::tlb::t_MsgAddress.get_size(csl) << std::endl;
81
  std::cout << "Grams.get_size() = " << block::tlb::t_Grams.get_size(csl) << std::endl;
82
  std::cout << "Grams.as_integer() = " << block::tlb::t_Grams.as_integer(csl) << std::endl;
83
  (csl + 8).print_rec(std::cout);
84
  std::cout << "Grams.get_size() = " << block::tlb::t_Grams.get_size(csl + 8) << std::endl;
85
  std::cout << "Grams.as_integer() = " << block::tlb::t_Grams.as_integer(csl + 8) << std::endl;
86

87
  vm::CellSlice csl2{csl};
88
  block::gen::ShardIdent::Record sh_id;
89
  for (int i = 0; i < 3; i++) {
90
    std::cout << csl2 << std::endl;
91
    bool ok = tlb::unpack(csl2, sh_id);
92
    std::cout << "block::gen::ShardIdent.unpack() = " << ok << std::endl;
93
    if (ok) {
94
      std::cout << "  (shard_ident shard_pfx_bits:" << sh_id.shard_pfx_bits << " workchain_id:" << sh_id.workchain_id
95
                << " shard_prefix:" << std::hex << sh_id.shard_prefix << std::dec << ")" << std::endl;
96
    }
97
  }
98

99
  block::tlb::ShardIdent::Record shard_id;
100
  for (int i = 0; i < 3; i++) {
101
    std::cout << "ShardIdent.validate() = " << block::tlb::t_ShardIdent.validate(csl) << std::endl;
102
    csl.print_rec(std::cerr);
103
    csl.dump(std::cerr, 7);
104
    std::cout << "ShardIdent.unpack() = " << block::tlb::t_ShardIdent.unpack(csl, shard_id) << std::endl;
105
    if (shard_id.is_valid()) {
106
      std::cout << "shard_pfx_bits:" << shard_id.shard_pfx_bits << " workchain_id:" << shard_id.workchain_id
107
                << " shard_prefix:" << shard_id.shard_prefix << std::endl;
108
    }
109
  }
110
  std::cout << "ShardIdent.skip_validate() = " << block::tlb::t_ShardIdent.validate_skip(csl) << std::endl;
111
  std::cout << "ShardIdent.skip_validate() = " << block::tlb::t_ShardIdent.validate_skip(csl) << std::endl;
112
  std::cout << "ShardIdent.skip_validate() = " << block::tlb::t_ShardIdent.validate_skip(csl) << std::endl;
113
  using namespace td::literals;
114
  std::cout << "Grams.store_intval(239) = " << block::tlb::t_Grams.store_integer_value(cb, "239"_i256) << std::endl;
115
  std::cout << "Grams.store_intval(17239) = " << block::tlb::t_Grams.store_integer_value(cb, "17239"_i256) << std::endl;
116
  std::cout << "Grams.store_intval(-17) = " << block::tlb::t_Grams.store_integer_value(cb, "-17"_i256) << std::endl;
117
  std::cout << "Grams.store_intval(0) = " << block::tlb::t_Grams.store_integer_value(cb, "0"_i256) << std::endl;
118
  std::cout << cb << std::endl;
119
  cs = td::Ref<vm::CellSlice>{true, cb.finalize()};
120
  std::cout << "Grams.store_intval(666) = " << block::tlb::t_Grams.store_integer_value(cb, "666"_i256) << std::endl;
121
  std::cout << cb << std::endl;
122
  cs2 = td::Ref<vm::CellSlice>{true, cb.finalize()};
123
  std::cout << "Grams.validate(cs) = " << block::tlb::t_Grams.validate(*cs) << std::endl;
124
  std::cout << "Grams.validate(cs2) = " << block::tlb::t_Grams.validate(*cs2) << std::endl;
125
  //
126
  block::gen::SplitMergeInfo::Record data;
127
  block::gen::Grams::Record data2;
128
  std::cout << "block::gen::Grams.validate(cs) = " << block::gen::t_Grams.validate(*cs) << std::endl;
129
  std::cout << "block::gen::Grams.validate(cs2) = " << block::gen::t_Grams.validate(*cs2) << std::endl;
130
  std::cout << "[cs = " << cs << "]" << std::endl;
131
  bool ok = tlb::csr_unpack_inexact(cs, data);
132
  std::cout << "block::gen::SplitMergeInfo.unpack(cs, data) = " << ok << std::endl;
133
  if (ok) {
134
    std::cout << "  cur_shard_pfx_len = " << data.cur_shard_pfx_len << "; acc_split_depth = " << data.acc_split_depth
135
              << "; this_addr = " << data.this_addr << "; sibling_addr = " << data.sibling_addr << std::endl;
136
  }
137
  ok = tlb::csr_unpack_inexact(cs, data2);
138
  std::cout << "block::gen::Grams.unpack(cs, data2) = " << ok << std::endl;
139
  if (ok) {
140
    std::cout << "  amount = " << data2.amount << std::endl;
141
    block::gen::VarUInteger::Record data3;
142
    ok = tlb::csr_type_unpack(data2.amount, block::gen::t_VarUInteger_16, data3);
143
    std::cout << "  block::gen::VarUInteger16.unpack(amount, data3) = " << ok << std::endl;
144
    if (ok) {
145
      std::cout << "    len = " << data3.len << "; value = " << data3.value << std::endl;
146
      vm::CellBuilder cb;
147
      std::cout << "    block::gen::VarUInteger16.pack(cb, data3) = "
148
                << tlb::type_pack(cb, block::gen::t_VarUInteger_16, data3) << std::endl;
149
      std::cout << "    cb = " << cb.finalize() << std::endl;
150
    }
151
  }
152
  /* 
153
  {
154
    vm::CellBuilder cb;
155
    td::BitArray<256> hash;
156
    std::memset(hash.data(), 0x69, 32);
157
    bool ok = tlb::pack(
158
        cb, block::gen::Test::Record{1000000000000, {170239, -888, {239017, "1000000000000000000"_ri256}, hash}, 17});
159
    std::cout << "  block::gen::Test::pack(cb, {1000000000000, ...}) = " << ok << std::endl;
160
    std::cout << "  cb = " << cb << std::endl;
161
    auto cell = cb.finalize();
162
    vm::CellSlice cs{cell};
163
    cs.print_rec(std::cout);
164
    block::gen::Test::Record data;
165
    std::cout << "  block::gen::Test::validate_ref(cell) = " << block::gen::t_Test.validate_ref(cell) << std::endl;
166
    ok = tlb::unpack(cs, data);
167
    std::cout << "  block::gen::Test::unpack(cs, data) = " << ok << std::endl;
168
    if (ok) {
169
      std::cout << "a:" << data.a << " b:" << data.r1.b << " c:" << data.r1.c << " d:" << data.r1.r1.d
170
                << " e:" << data.r1.r1.e << " f:" << data.r1.f << " g:" << data.g << std::endl;
171
    }
172
    std::cout << "  block::gen::Test::print_ref(cell) = ";
173
    block::gen::t_Test.print_ref(std::cout, cell, 2);
174
    block::gen::t_CurrencyCollection.print_ref(std::cout, cell, 2);
175
    std::cout << std::endl;
176
  }
177
  */
178
  std::cout << "Grams.add_values() = " << block::tlb::t_Grams.add_values(cb, cs.write(), cs2.write()) << std::endl;
179
  std::cout << cb << std::endl;
180
  std::cout << "block::gen::t_HashmapAug_64_...print_type() = "
181
            << block::gen::t_HashmapAug_64_Ref_Transaction_CurrencyCollection << std::endl;
182
}
183

184
void test2(vm::CellSlice& cs) {
185
  std::cout << "Bool.validate() = " << block::tlb::t_Bool.validate(cs) << std::endl;
186
  std::cout << "UInt16.validate() = " << block::tlb::t_uint16.validate(cs) << std::endl;
187
  std::cout << "HashmapE(32,UInt16).validate() = " << block::tlb::HashmapE(32, block::tlb::t_uint16).validate(cs)
188
            << std::endl;
189
  std::cout << "block::gen::HashmapE(32,UInt16).validate() = "
190
            << block::gen::HashmapE{32, block::gen::t_uint16}.validate(cs) << std::endl;
191
}
192

193
void usage() {
194
  std::cout << "usage: test-block [<boc-file>]\n\tor test-block -h\n";
195
  std::exit(2);
196
}
197

198
int main(int argc, char* const argv[]) {
199
  int i;
200
  int new_verbosity_level = VERBOSITY_NAME(INFO);
201
  auto zerostate = std::make_unique<block::ZerostateInfo>();
202
  while ((i = getopt(argc, argv, "hv:")) != -1) {
203
    switch (i) {
204
      case 'v':
205
        new_verbosity_level = VERBOSITY_NAME(FATAL) + (verbosity = td::to_integer<int>(td::Slice(optarg)));
206
        break;
207
      case 'h':
208
        usage();
209
        std::exit(2);
210
      default:
211
        usage();
212
        std::exit(2);
213
    }
214
  }
215
  SET_VERBOSITY_LEVEL(new_verbosity_level);
216
  try {
217
    bool done = false;
218
    while (optind < argc) {
219
      auto boc = load_boc(argv[optind++]);
220
      if (boc.is_null()) {
221
        std::cerr << "(invalid boc)" << std::endl;
222
        std::exit(2);
223
      } else {
224
        done = true;
225
        vm::CellSlice cs{vm::NoVm(), boc};
226
        cs.print_rec(std::cout);
227
        std::cout << std::endl;
228
        block::gen::t_Block.print_ref(std::cout, boc);
229
        std::cout << std::endl;
230
        if (!block::gen::t_Block.validate_ref(boc)) {
231
          std::cout << "(invalid Block)" << std::endl;
232
        } else {
233
          std::cout << "(valid Block)" << std::endl;
234
        }
235
      }
236
    }
237
    if (!done) {
238
      test1();
239
    }
240
  } catch (IntError& err) {
241
    std::cerr << "caught internal error " << err.err_msg << std::endl;
242
    return 1;
243
  } catch (vm::VmError& err) {
244
    std::cerr << "caught vm error " << err.get_msg() << std::endl;
245
    return 1;
246
  }
247
  return 0;
248
}
249

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

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

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

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