2
This file is part of TON Blockchain source code.
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.
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.
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/>.
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.
26
Copyright 2017-2019 Telegram Systems LLP
28
#include "block/block.h"
32
#include "block-auto.h"
33
#include "block-parse.h"
43
IntError(std::string _msg) : err_msg(_msg) {
45
IntError(const char* _msg) : err_msg(_msg) {
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()};
56
auto res = boc.deserialize(bytes_res.move_as_ok());
58
throw IntError{PSTRING() << "cannot deserialize bag-of-cells " << res.move_as_error()};
60
if (res.move_as_ok() <= 0 || boc.get_root_cell().is_null()) {
61
throw IntError{"cannot deserialize bag-of-cells "};
63
return boc.get_root_cell();
67
block::ShardId id{ton::masterchainId}, id2{ton::basechainId, 0x11efULL << 48};
68
std::cout << '[' << id << "][" << id2 << ']' << std::endl;
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;
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;
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;
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;
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;
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;
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;
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;
137
ok = tlb::csr_unpack_inexact(cs, data2);
138
std::cout << "block::gen::Grams.unpack(cs, data2) = " << ok << std::endl;
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;
145
std::cout << " len = " << data3.len << "; value = " << data3.value << std::endl;
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;
155
td::BitArray<256> hash;
156
std::memset(hash.data(), 0x69, 32);
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;
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;
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;
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;
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)
189
std::cout << "block::gen::HashmapE(32,UInt16).validate() = "
190
<< block::gen::HashmapE{32, block::gen::t_uint16}.validate(cs) << std::endl;
194
std::cout << "usage: test-block [<boc-file>]\n\tor test-block -h\n";
198
int main(int argc, char* const argv[]) {
200
int new_verbosity_level = VERBOSITY_NAME(INFO);
201
auto zerostate = std::make_unique<block::ZerostateInfo>();
202
while ((i = getopt(argc, argv, "hv:")) != -1) {
205
new_verbosity_level = VERBOSITY_NAME(FATAL) + (verbosity = td::to_integer<int>(td::Slice(optarg)));
215
SET_VERBOSITY_LEVEL(new_verbosity_level);
218
while (optind < argc) {
219
auto boc = load_boc(argv[optind++]);
221
std::cerr << "(invalid boc)" << std::endl;
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;
233
std::cout << "(valid Block)" << std::endl;
240
} catch (IntError& err) {
241
std::cerr << "caught internal error " << err.err_msg << std::endl;
243
} catch (vm::VmError& err) {
244
std::cerr << "caught vm error " << err.get_msg() << std::endl;