-
Notifications
You must be signed in to change notification settings - Fork 0
/
tonlib.patch
355 lines (335 loc) · 15.8 KB
/
tonlib.patch
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
diff --git a/crypto/smc-envelope/SmartContract.cpp b/crypto/smc-envelope/SmartContract.cpp
index f3485be..30030aa 100644
--- a/crypto/smc-envelope/SmartContract.cpp
+++ b/crypto/smc-envelope/SmartContract.cpp
@@ -52,30 +52,51 @@ td::Ref<vm::Stack> prepare_vm_stack(td::RefInt256 amount, td::Ref<vm::CellSlice>
return stack_ref;
}
-td::Ref<vm::Tuple> prepare_vm_c7(td::uint32 now, td::uint64 balance) {
- // TODO: fix initialization of c7
+td::Ref<vm::Tuple> prepare_vm_c7(SmartContract::Args args) {
td::BitArray<256> rand_seed;
rand_seed.as_slice().fill(0);
td::RefInt256 rand_seed_int{true};
rand_seed_int.unique_write().import_bits(rand_seed.cbits(), 256, false);
+
+ td::uint32 now = 0;
+ if (args.now) {
+ now = args.now.unwrap();
+ }
+
+ vm::CellBuilder cb;
+ if (args.address) {
+ td::BigInt256 dest_addr;
+ dest_addr.import_bits((*args.address).addr.as_bitslice());
+ cb.store_ones(1)
+ .store_zeroes(2)
+ .store_long((*args.address).workchain, 8)
+ .store_int256(dest_addr, 256);
+ }
+ auto address = cb.finalize();
+ auto config = td::Ref<vm::Cell>();
+
+ if (args.config) {
+ config = (*args.config)->get_root_cell();
+ }
+
auto tuple = vm::make_tuple_ref(
td::make_refint(0x076ef1ea), // [ magic:0x076ef1ea
td::make_refint(0), // actions:Integer
td::make_refint(0), // msgs_sent:Integer
td::make_refint(now), // unixtime:Integer
- td::make_refint(0), // block_lt:Integer
- td::make_refint(0), // trans_lt:Integer
+ td::make_refint(0), //TODO: // block_lt:Integer
+ td::make_refint(0), //TODO: // trans_lt:Integer
std::move(rand_seed_int), // rand_seed:Integer
- block::CurrencyCollection(balance).as_vm_tuple(), // balance_remaining:[Integer (Maybe Cell)]
- vm::load_cell_slice_ref(vm::CellBuilder().finalize()) // myself:MsgAddressInt
- //vm::StackEntry::maybe(td::Ref<vm::Cell>())
+ block::CurrencyCollection(args.balance).as_vm_tuple(), // balance_remaining:[Integer (Maybe Cell)]
+ vm::load_cell_slice_ref(address), // myself:MsgAddressInt
+ vm::StackEntry::maybe(config) //vm::StackEntry::maybe(td::Ref<vm::Cell>())
); // global_config:(Maybe Cell) ] = SmartContractInfo;
//LOG(DEBUG) << "SmartContractInfo initialized with " << vm::StackEntry(tuple).to_string();
return vm::make_tuple_ref(std::move(tuple));
}
SmartContract::Answer run_smartcont(SmartContract::State state, td::Ref<vm::Stack> stack, td::Ref<vm::Tuple> c7,
- vm::GasLimits gas, bool ignore_chksig) {
+ vm::GasLimits gas, bool ignore_chksig, td::Ref<vm::Cell> libraries) {
auto gas_credit = gas.gas_credit;
vm::init_op_cp0();
vm::DictionaryBase::get_empty_dictionary();
@@ -108,11 +129,14 @@ SmartContract::Answer run_smartcont(SmartContract::State state, td::Ref<vm::Stac
vm::VmState vm{state.code, std::move(stack), gas, 1, state.data, log};
vm.set_c7(std::move(c7));
vm.set_chksig_always_succeed(ignore_chksig);
+ if (!libraries.is_null())
+ vm.register_library_collection(libraries);
try {
res.code = ~vm.run();
} catch (...) {
LOG(FATAL) << "catch unhandled exception";
}
+ td::ConstBitPtr mlib = vm.get_missing_library();
res.new_state = std::move(state);
res.stack = vm.get_stack_ref();
gas = vm.get_gas_limits();
@@ -128,13 +152,17 @@ SmartContract::Answer run_smartcont(SmartContract::State state, td::Ref<vm::Stac
LOG(DEBUG) << "VM accepted: " << res.accepted;
LOG(DEBUG) << "VM success: " << res.success;
}
+ if (!mlib.is_null()) {
+ LOG(DEBUG) << "Missing library: " << mlib.to_hex(256);
+ res.missing_library = mlib;
+ }
if (res.success) {
res.new_state.data = vm.get_c4();
res.actions = vm.get_d(5);
LOG(DEBUG) << "output actions:\n"
<< block::gen::OutList{res.output_actions_count(res.actions)}.as_string_ref(res.actions);
}
- LOG_IF(ERROR, gas_credit != 0 && (res.accepted && !res.success))
+ LOG_IF(ERROR, gas_credit != 0 && (res.accepted && !res.success) && mlib.is_null())
<< "Accepted but failed with code " << res.code << "\n"
<< res.gas_used << "\n";
return res;
@@ -176,12 +204,8 @@ td::Ref<vm::Cell> SmartContract::get_init_state() const {
}
SmartContract::Answer SmartContract::run_method(Args args) {
- td::uint32 now = 0;
- if (args.now) {
- now = args.now.unwrap();
- }
if (!args.c7) {
- args.c7 = prepare_vm_c7(now, args.balance);
+ args.c7 = prepare_vm_c7(args);
}
if (!args.limits) {
bool is_internal = args.get_method_id().ok() == 0;
@@ -193,18 +217,15 @@ SmartContract::Answer SmartContract::run_method(Args args) {
CHECK(args.method_id);
args.stack.value().write().push_smallint(args.method_id.unwrap());
auto res =
- run_smartcont(get_state(), args.stack.unwrap(), args.c7.unwrap(), args.limits.unwrap(), args.ignore_chksig);
+ run_smartcont(get_state(), args.stack.unwrap(), args.c7.unwrap(), args.limits.unwrap(), args.ignore_chksig,
+ args.libraries ? args.libraries.unwrap().get_root_cell() : td::Ref<vm::Cell>{});
state_ = res.new_state;
return res;
}
SmartContract::Answer SmartContract::run_get_method(Args args) const {
- td::uint32 now = 0;
- if (args.now) {
- now = args.now.unwrap();
- }
if (!args.c7) {
- args.c7 = prepare_vm_c7(now, args.balance);
+ args.c7 = prepare_vm_c7(args);
}
if (!args.limits) {
args.limits = vm::GasLimits{1000000};
@@ -214,7 +235,8 @@ SmartContract::Answer SmartContract::run_get_method(Args args) const {
}
CHECK(args.method_id);
args.stack.value().write().push_smallint(args.method_id.unwrap());
- return run_smartcont(get_state(), args.stack.unwrap(), args.c7.unwrap(), args.limits.unwrap(), args.ignore_chksig);
+ return run_smartcont(get_state(), args.stack.unwrap(), args.c7.unwrap(), args.limits.unwrap(), args.ignore_chksig,
+ args.libraries ? args.libraries.unwrap().get_root_cell() : td::Ref<vm::Cell>{});
}
SmartContract::Answer SmartContract::run_get_method(td::Slice method, Args args) const {
diff --git a/crypto/smc-envelope/SmartContract.h b/crypto/smc-envelope/SmartContract.h
index b26f488..95dd2e8 100644
--- a/crypto/smc-envelope/SmartContract.h
+++ b/crypto/smc-envelope/SmartContract.h
@@ -26,6 +26,7 @@
#include "td/utils/crypto.h"
#include "block/block.h"
+#include "block/mc-config.h"
namespace ton {
class SmartContract : public td::CntObject {
@@ -48,6 +49,7 @@ class SmartContract : public td::CntObject {
td::Ref<vm::Cell> actions;
td::int32 code;
td::int64 gas_used;
+ td::ConstBitPtr missing_library{0};
static int output_actions_count(td::Ref<vm::Cell> list);
};
@@ -61,6 +63,10 @@ class SmartContract : public td::CntObject {
td::uint64 amount{0};
td::uint64 balance{0};
+ td::optional<block::StdAddress> address;
+ td::optional<std::shared_ptr<const block::Config>> config;
+ td::optional<vm::Dictionary> libraries;
+
Args() {
}
Args(std::initializer_list<vm::StackEntry> stack)
@@ -106,6 +112,18 @@ class SmartContract : public td::CntObject {
this->balance = balance;
return std::move(*this);
}
+ Args&& set_address(block::StdAddress address) {
+ this->address = address;
+ return std::move(*this);
+ }
+ Args&& set_config(std::shared_ptr<const block::Config>& config) {
+ this->config = config;
+ return std::move(*this);
+ }
+ Args&& set_libraries(vm::Dictionary libraries) {
+ this->libraries = libraries;
+ return std::move(*this);
+ }
td::Result<td::int32> get_method_id() const {
if (!method_id) {
diff --git a/crypto/vm/vm.cpp b/crypto/vm/vm.cpp
index 038640a..fb2f4d1 100644
--- a/crypto/vm/vm.cpp
+++ b/crypto/vm/vm.cpp
@@ -604,6 +604,7 @@ Ref<Cell> VmState::load_library(td::ConstBitPtr hash) {
return lib;
}
}
+ missing_library = hash;
return {};
}
diff --git a/crypto/vm/vm.h b/crypto/vm/vm.h
index 0fef964..44aa7f2 100644
--- a/crypto/vm/vm.h
+++ b/crypto/vm/vm.h
@@ -96,6 +96,7 @@ class VmState final : public VmStateInterface {
td::int64 loaded_cells_count{0};
int stack_trace{0}, debug_off{0};
bool chksig_always_succeed{false};
+ td::ConstBitPtr missing_library{0};
public:
enum {
@@ -321,6 +322,9 @@ class VmState final : public VmStateInterface {
Ref<OrdCont> ref_to_cont(Ref<Cell> cell) const {
return td::make_ref<OrdCont>(load_cell_slice_ref(std::move(cell)), get_cp());
}
+ td::ConstBitPtr get_missing_library() const {
+ return missing_library;
+ }
private:
void init_cregs(bool same_c3 = false, bool push_0 = true);
diff --git a/tonlib/tonlib/LastConfig.cpp b/tonlib/tonlib/LastConfig.cpp
index d07482c..0111095 100644
--- a/tonlib/tonlib/LastConfig.cpp
+++ b/tonlib/tonlib/LastConfig.cpp
@@ -62,9 +62,7 @@ void LastConfig::with_last_block(td::Result<LastBlockState> r_last_block) {
}
auto last_block = r_last_block.move_as_ok();
- auto params = params_;
- client_.send_query(ton::lite_api::liteServer_getConfigParams(0, create_tl_lite_block_id(last_block.last_block_id),
- std::move(params)),
+ client_.send_query(ton::lite_api::liteServer_getConfigAll(0, create_tl_lite_block_id(last_block.last_block_id)),
[this](auto r_config) { this->on_config(std::move(r_config)); });
}
diff --git a/tonlib/tonlib/TonlibClient.cpp b/tonlib/tonlib/TonlibClient.cpp
index 1ac3a0d..eefc581 100644
--- a/tonlib/tonlib/TonlibClient.cpp
+++ b/tonlib/tonlib/TonlibClient.cpp
@@ -861,13 +861,13 @@ class Query {
}
return res;
}
- td::Result<std::pair<Fee, std::vector<Fee>>> estimate_fees(bool ignore_chksig, const block::Config& cfg) {
+ td::Result<std::pair<Fee, std::vector<Fee>>> estimate_fees(bool ignore_chksig, std::shared_ptr<const block::Config>& cfg, vm::Dictionary& libraries) {
// gas fees
bool is_masterchain = raw_.source->get_address().workchain == ton::masterchainId;
- TRY_RESULT(gas_limits_prices, cfg.get_gas_limits_prices(is_masterchain));
- TRY_RESULT(storage_prices, cfg.get_storage_prices());
- TRY_RESULT(masterchain_msg_prices, cfg.get_msg_prices(true));
- TRY_RESULT(basechain_msg_prices, cfg.get_msg_prices(false));
+ TRY_RESULT(gas_limits_prices, cfg->get_gas_limits_prices(is_masterchain));
+ TRY_RESULT(storage_prices, cfg->get_storage_prices());
+ TRY_RESULT(masterchain_msg_prices, cfg->get_msg_prices(true));
+ TRY_RESULT(basechain_msg_prices, cfg->get_msg_prices(false));
block::MsgPrices* msg_prices[2] = {&basechain_msg_prices, &masterchain_msg_prices};
auto storage_fee_256 = block::StoragePrices::compute_storage_fees(
raw_.source->get_sync_time(), storage_prices, raw_.source->raw().storage_stat,
@@ -888,7 +888,9 @@ class Query {
.set_limits(gas_limits)
.set_balance(raw_.source->get_balance())
.set_now(raw_.source->get_sync_time())
- .set_ignore_chksig(ignore_chksig));
+ .set_ignore_chksig(ignore_chksig)
+ .set_address(raw_.source->get_address())
+ .set_config(cfg).set_libraries(libraries));
td::int64 fwd_fee = 0;
if (res.success) {
LOG(DEBUG) << "output actions:\n"
@@ -910,7 +912,7 @@ class Query {
for (auto& destination : raw_.destinations) {
bool dest_is_masterchain = destination && destination->get_address().workchain == ton::masterchainId;
- TRY_RESULT(dest_gas_limits_prices, cfg.get_gas_limits_prices(dest_is_masterchain));
+ TRY_RESULT(dest_gas_limits_prices, cfg->get_gas_limits_prices(dest_is_masterchain));
auto dest_storage_fee_256 =
destination ? block::StoragePrices::compute_storage_fees(
destination->get_sync_time(), storage_prices, destination->raw().storage_stat,
@@ -2306,9 +2308,10 @@ struct ToRawTransactions {
return td::Status::Error("Failed to unpack CommonMsgInfo::ext_out_msg_info");
}
TRY_RESULT(src, to_std_address(msg_info.src));
+ auto created_lt = static_cast<td::int64>(msg_info.created_lt);
return tonlib_api::make_object<tonlib_api::raw_message>(
tonlib_api::make_object<tonlib_api::accountAddress>(src),
- tonlib_api::make_object<tonlib_api::accountAddress>(), 0, 0, 0, 0, std::move(body_hash), get_data(src));
+ tonlib_api::make_object<tonlib_api::accountAddress>(), 0, 0, 0, created_lt, std::move(body_hash), get_data(src));
}
}
@@ -3266,7 +3269,7 @@ void TonlibClient::query_estimate_fees(td::int64 id, bool ignore_chksig, td::Res
return;
}
TRY_RESULT_PROMISE(promise, state, std::move(r_state));
- TRY_RESULT_PROMISE_PREFIX(promise, fees, TRY_VM(it->second->estimate_fees(ignore_chksig, *state.config)),
+ TRY_RESULT_PROMISE_PREFIX(promise, fees, TRY_VM(it->second->estimate_fees(ignore_chksig, state.config, libraries)),
TonlibError::Internal());
promise.set_value(tonlib_api::make_object<tonlib_api::query_fees>(
fees.first.to_tonlib_api(), td::transform(fees.second, [](auto& x) { return x.to_tonlib_api(); })));
@@ -3493,14 +3496,29 @@ td::Status TonlibClient::do_request(const tonlib_api::smc_runGetMethod& request,
args.set_stack(std::move(stack));
args.set_balance(it->second->get_balance());
args.set_now(it->second->get_sync_time());
- auto res = smc->run_get_method(std::move(args));
+ args.set_address(it->second->get_address());
- // smc.runResult gas_used:int53 stack:vector<tvm.StackEntry> exit_code:int32 = smc.RunResult;
- std::vector<object_ptr<tonlib_api::tvm_StackEntry>> res_stack;
- for (auto& entry : res.stack->as_span()) {
- res_stack.push_back(to_tonlib_api(entry));
+ auto code = smc->get_state().code;
+ if (code.not_null()) {
+ std::vector<td::Bits256> libraryList;
}
- promise.set_value(tonlib_api::make_object<tonlib_api::smc_runResult>(res.gas_used, std::move(res_stack), res.code));
+
+ args.set_libraries(libraries);
+
+ client_.with_last_config([smc=std::move(smc), args=std::move(args),
+ promise = std::move(promise)](td::Result<LastConfigState> r_state) mutable {
+ TRY_RESULT_PROMISE(promise, state, std::move(r_state));
+ args.set_config(state.config);
+
+ auto res = smc->run_get_method(std::move(args));
+
+ // smc.runResult gas_used:int53 stack:vector<tvm.StackEntry> exit_code:int32 = smc.RunResult;
+ std::vector<object_ptr<tonlib_api::tvm_StackEntry>> res_stack;
+ for (auto& entry : res.stack->as_span()) {
+ res_stack.push_back(to_tonlib_api(entry));
+ }
+ promise.set_value(tonlib_api::make_object<tonlib_api::smc_runResult>(res.gas_used, std::move(res_stack), res.code));
+ });
return td::Status::OK();
}
diff --git a/tonlib/tonlib/TonlibClient.h b/tonlib/tonlib/TonlibClient.h
index bedc1d4..4dfbe8c 100644
--- a/tonlib/tonlib/TonlibClient.h
+++ b/tonlib/tonlib/TonlibClient.h
@@ -107,6 +107,7 @@ class TonlibClient : public td::actor::Actor {
td::optional<ton::BlockIdExt> block_id;
};
QueryContext query_context_;
+ vm::Dictionary libraries{256};
// network
td::actor::ActorOwn<ton::adnl::AdnlExtClient> raw_client_;