-
Notifications
You must be signed in to change notification settings - Fork 17
/
transaction_executor.rs
1229 lines (1149 loc) · 50.7 KB
/
transaction_executor.rs
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2018-2020 TON DEV SOLUTIONS LTD.
*
* Licensed under the SOFTWARE EVALUATION License (the "License"); you may not use
* this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific TON DEV software governing permissions and
* limitations under the License.
*/
#![allow(clippy::too_many_arguments)]
use crate::{
blockchain_config::{BlockchainConfig, CalcMsgFwdFees},
error::ExecutorError, vmsetup::VMSetup,
};
use std::sync::{atomic::{AtomicU64, Ordering}, Arc};
use ton_block::{
AccStatusChange, Account, AccountStatus, AddSub, AnycastInfo, ComputeSkipReason,
CurrencyCollection, Deserializable, GasLimitsPrices, GetRepresentationHash, GlobalCapabilities,
Grams, HashUpdate, Message, MsgAddressInt, OutAction,
OutActions, Serializable, StorageUsedShort, TrActionPhase, TrBouncePhase, TrComputePhase,
TrComputePhaseVm, TrCreditPhase, TrStoragePhase, Transaction, WorkchainFormat,
BASE_WORKCHAIN_ID, MASTERCHAIN_ID, RESERVE_ALL_BUT, RESERVE_IGNORE_ERROR, RESERVE_PLUS_ORIG,
RESERVE_REVERSE, RESERVE_VALID_MODES, SENDMSG_ALL_BALANCE, SENDMSG_DELETE_IF_EMPTY,
SENDMSG_IGNORE_ERROR, SENDMSG_PAY_FEE_SEPARATELY, SENDMSG_REMAINING_MSG_BALANCE,
SENDMSG_VALID_FLAGS,
};
use ton_types::{error, fail, ExceptionCode, Cell, Result, HashmapE, HashmapType, IBitstring, UInt256};
use ton_vm::{
error::tvm_exception, executor::gas::gas_state::Gas,
smart_contract_info::SmartContractInfo,
stack::{Stack, StackItem},
};
use ton_block::MsgAddressInt::AddrStd;
use ton_block::MsgAddressInt::AddrVar;
const RESULT_CODE_ACTIONLIST_INVALID: i32 = 32;
const RESULT_CODE_TOO_MANY_ACTIONS: i32 = 33;
const RESULT_CODE_UNKNOWN_OR_INVALID_ACTION: i32 = 34;
const RESULT_CODE_INCORRECT_SRC_ADDRESS: i32 = 35;
const RESULT_CODE_INCORRECT_DST_ADDRESS: i32 = 36;
const RESULT_CODE_NOT_ENOUGH_GRAMS: i32 = 37;
const RESULT_CODE_NOT_ENOUGH_EXTRA: i32 = 38;
const RESULT_CODE_INVALID_BALANCE: i32 = 40;
const RESULT_CODE_BAD_ACCOUNT_STATE: i32 = 41;
const RESULT_CODE_UNSUPPORTED: i32 = -1;
const MAX_ACTIONS: usize = 255;
const MAX_MSG_BITS: usize = 1 << 21;
const MAX_MSG_CELLS: usize = 1 << 13;
pub struct ExecuteParams {
pub state_libs: HashmapE,
pub block_unixtime: u32,
pub block_lt: u64,
pub last_tr_lt: Arc<AtomicU64>,
pub seed_block: UInt256,
pub debug: bool
}
impl ExecuteParams {
pub fn default() -> Self {
Self {
state_libs: HashmapE::default(),
block_unixtime: 0,
block_lt: 0,
last_tr_lt: Arc::new(AtomicU64::new(0)),
seed_block: UInt256::default(),
debug: false
}
}
}
pub trait TransactionExecutor {
fn execute_with_params(
&self,
in_msg: Option<&Message>,
account: &mut Account,
params: ExecuteParams,
) -> Result<Transaction>;
#[deprecated]
fn execute_for_account(
&self,
in_msg: Option<&Message>,
account: &mut Account,
state_libs: HashmapE, // masterchain libraries
block_unixtime: u32,
block_lt: u64,
last_tr_lt: Arc<AtomicU64>,
debug: bool
) -> Result<Transaction> {
let params = ExecuteParams {
state_libs,
block_unixtime,
block_lt,
last_tr_lt,
seed_block: UInt256::default(),
debug,
};
let mut account_root = account.serialize()?;
let transaction = self.execute_with_libs_and_params(in_msg, &mut account_root, params)?;
*account = Account::construct_from_cell(account_root)?;
Ok(transaction)
}
fn execute_with_libs_and_params(
&self,
in_msg: Option<&Message>,
account_root: &mut Cell,
params: ExecuteParams,
) -> Result<Transaction> {
let old_hash = account_root.repr_hash();
let mut account = Account::construct_from_cell(account_root.clone())?;
let mut transaction = self.execute_with_params(
in_msg,
&mut account,
params,
)?;
if self.config().raw_config().has_capability(GlobalCapabilities::CapFastStorageStat) {
account.update_storage_stat_fast()?;
} else {
account.update_storage_stat()?;
}
*account_root = account.serialize()?;
let new_hash = account_root.repr_hash();
transaction.write_state_update(&HashUpdate::with_hashes(old_hash, new_hash))?;
Ok(transaction)
}
#[deprecated]
fn execute_with_libs(
&self,
in_msg: Option<&Message>,
account_root: &mut Cell,
state_libs: HashmapE, // masterchain libraries
block_unixtime: u32,
block_lt: u64,
last_tr_lt: Arc<AtomicU64>,
debug: bool
) -> Result<Transaction> {
let params = ExecuteParams {
state_libs,
block_unixtime,
block_lt,
last_tr_lt,
seed_block: UInt256::default(),
debug,
};
self.execute_with_libs_and_params(in_msg, account_root, params)
}
#[deprecated]
fn execute(
&self,
in_msg: Option<&Message>,
account_root: &mut Cell,
block_unixtime: u32,
block_lt: u64,
last_tr_lt: Arc<AtomicU64>,
debug: bool
) -> Result<Transaction> {
let params = ExecuteParams {
state_libs: HashmapE::default(),
block_unixtime,
block_lt,
last_tr_lt,
seed_block: UInt256::default(),
debug,
};
self.execute_with_libs_and_params(in_msg, account_root, params)
}
fn build_contract_info(&self, acc_balance: &CurrencyCollection, acc_address: &MsgAddressInt, block_unixtime: u32, block_lt: u64, tr_lt: u64, seed_block: UInt256) -> SmartContractInfo {
let mut info = SmartContractInfo::with_myself(acc_address.serialize().unwrap_or_default().into());
*info.block_lt_mut() = block_lt;
*info.trans_lt_mut() = tr_lt;
*info.unix_time_mut() = block_unixtime;
*info.balance_remaining_grams_mut() = acc_balance.grams.0;
*info.balance_remaining_other_mut() = acc_balance.other_as_hashmap();
if let Some(data) = self.config().raw_config().config_params.data() {
info.set_config_params(data.clone());
}
info.calc_rand_seed(seed_block, &acc_address.address().get_bytestring(0));
info
}
fn ordinary_transaction(&self) -> bool;
fn config(&self) -> &BlockchainConfig;
fn build_stack(&self, in_msg: Option<&Message>, account: &Account) -> Stack;
/// Implementation of transaction's storage phase.
/// If account does not exist - phase skipped.
/// Calculates storage fees and substracts them from account balance.
/// If account balance becomes negative after that, then account is frozen.
fn storage_phase(
&self,
acc: &mut Account,
acc_balance: &mut CurrencyCollection,
tr: &mut Transaction,
is_masterchain: bool,
is_special: bool
) -> Option<TrStoragePhase> {
log::debug!(target: "executor", "storage_phase");
if is_special {
log::debug!(target: "executor", "Special account: AccStatusChange::Unchanged");
return Some(TrStoragePhase::default())
}
if acc.is_none() {
log::debug!(target: "executor", "Account::None");
return Some(TrStoragePhase::default())
}
let mut fee = Grams::from(self.config().calc_storage_fee(
acc.storage_info()?,
is_masterchain,
tr.now(),
));
if let Some(due_payment) = acc.due_payment() {
fee.add(due_payment).ok()?;
acc.set_due_payment(None);
}
if acc_balance.grams >= fee {
log::debug!(target: "executor", "acc_balance: {}, storage fee: {}", acc_balance.grams, fee);
acc_balance.grams.sub(&fee).ok()?;
tr.add_fee_grams(&fee).ok()?;
Some(TrStoragePhase::with_params(fee, None, AccStatusChange::Unchanged))
} else {
log::debug!(target: "executor", "acc_balance: {} is storage fee from total: {}", acc_balance.grams, fee);
let storage_fees_collected = std::mem::replace(&mut acc_balance.grams, Grams::default());
tr.add_fee_grams(&storage_fees_collected).ok()?;
fee.sub(&storage_fees_collected).ok()?;
let need_freeze = fee.0 > self.config().get_gas_config(is_masterchain).freeze_due_limit.into();
let need_delete =
(acc.status() == AccountStatus::AccStateUninit || acc.status() == AccountStatus::AccStateFrozen) &&
fee.0 > self.config().get_gas_config(is_masterchain).delete_due_limit.into();
if need_delete {
let mut total_fees = tr.total_fees.clone();
total_fees.add(&CurrencyCollection { grams: Default::default(), other: acc_balance.other.clone() }).ok()?;
tr.set_total_fees(total_fees);
*acc = Account::new();
Some(TrStoragePhase::with_params(storage_fees_collected, Some(fee), AccStatusChange::Deleted))
} else if need_freeze {
acc.set_due_payment(Some(fee.clone()));
if acc.status() == AccountStatus::AccStateActive {
acc.try_freeze().unwrap();
Some(TrStoragePhase::with_params(storage_fees_collected, Some(fee), AccStatusChange::Frozen))
} else {
Some(TrStoragePhase::with_params(storage_fees_collected, Some(fee), AccStatusChange::Unchanged))
}
} else {
acc.set_due_payment(Some(fee.clone()));
Some(TrStoragePhase::with_params(storage_fees_collected, Some(fee), AccStatusChange::Unchanged))
}
}
}
/// Implementation of transaction's credit phase.
/// Increases account balance by the amount that appears in the internal message header.
/// If account does not exist - phase skipped.
/// If message is not internal - phase skipped.
fn credit_phase(
&self,
acc: &mut Account,
tr: &mut Transaction,
msg_balance: &mut CurrencyCollection,
acc_balance: &mut CurrencyCollection,
) -> Option<TrCreditPhase> {
let due_payment = acc.due_payment().cloned().unwrap_or(Grams::default());
let collected = std::cmp::min(&due_payment, &msg_balance.grams).clone();
msg_balance.grams.0 = msg_balance.grams.0 - collected.0;
let due_payment_remaining = Grams(due_payment.0 - collected.0);
acc.set_due_payment(if due_payment_remaining.0 != 0 {Some(due_payment_remaining)} else {None});
tr.total_fees.grams.0 = tr.total_fees.grams.0 + collected.0;
log::debug!(target: "executor", "credit_phase: add funds {} to {}", msg_balance.grams, acc_balance.grams);
acc_balance.add(&msg_balance).ok()?;
Some(TrCreditPhase::with_params(if collected.0 != 0 {Some(collected.clone())} else {None}, msg_balance.clone()))
//TODO: Is it need to credit with ihr_fee value in internal messages?
}
/// Implementation of transaction's computing phase.
/// Evaluates new accout state and invokes TVM if account has contract code.
fn compute_phase(
&self,
msg: Option<&Message>,
acc: &mut Account,
acc_balance: &mut CurrencyCollection,
msg_balance: &CurrencyCollection,
state_libs: HashmapE, // masterchain libraries
smc_info: SmartContractInfo,
stack: Stack,
is_masterchain: bool,
is_special: bool,
debug: bool,
) -> Result<(TrComputePhase, Option<Cell>, Option<Cell>)> {
let mut result_acc = acc.clone();
let mut vm_phase = TrComputePhaseVm::default();
let init_code_hash = self.config().raw_config().has_capability(GlobalCapabilities::CapInitCodeHash);
let is_external = if let Some(msg) = msg {
if let Some(header) = msg.int_header() {
log::debug!(target: "executor", "msg internal, bounce: {}", header.bounce);
if result_acc.is_none() {
if let Some(new_acc) = account_from_message(msg, msg_balance, true, init_code_hash) {
result_acc = new_acc;
result_acc.set_last_paid(smc_info.unix_time());
// if there was a balance in message (not bounce), then account state at least become uninit
*acc = result_acc.clone();
acc.uninit_account();
}
}
false
} else {
log::debug!(target: "executor", "msg external");
true
}
} else {
debug_assert!(!result_acc.is_none());
false
};
log::debug!(target: "executor", "acc balance: {}", acc_balance.grams);
log::debug!(target: "executor", "msg balance: {}", msg_balance.grams);
let is_ordinary = self.ordinary_transaction();
let gas_config = self.config().get_gas_config(is_masterchain);
let gas = init_gas(acc_balance.grams.0, msg_balance.grams.0, is_external, is_special, is_ordinary, gas_config);
if gas.get_gas_limit() == 0 && gas.get_gas_credit() == 0 {
log::debug!(target: "executor", "skip computing phase no gas");
return Ok((TrComputePhase::skipped(ComputeSkipReason::NoGas), None, None))
}
let mut libs = vec![];
if let Some(msg) = msg {
if let Some(state_init) = msg.state_init() {
libs.push(state_init.libraries().inner());
}
if let Some(reason) = compute_new_state(&mut result_acc, &acc_balance, msg, init_code_hash) {
if !init_code_hash {
*acc = result_acc;
}
return Ok((TrComputePhase::skipped(reason), None, None))
}
};
//code must present but can be empty (i.g. for uninitialized account)
let code = result_acc.get_code().unwrap_or_default();
let data = result_acc.get_data().unwrap_or_default();
libs.push(result_acc.libraries().inner());
libs.push(state_libs);
vm_phase.gas_credit = match gas.get_gas_credit() as u32 {
0 => None,
value => Some(value.into())
};
vm_phase.gas_limit = (gas.get_gas_limit() as u64).into();
let mut vm = VMSetup::new(code.into())
.set_contract_info(smc_info)
.set_stack(stack)
.set_data(data)
.set_libraries(libs)
.set_gas(gas)
.set_debug(debug)
.create();
//TODO: set vm_init_state_hash
let result = vm.execute();
log::trace!(target: "executor", "execute result: {:?}", result);
let mut raw_exit_arg = None;
match result {
Err(err) => {
log::debug!(target: "executor", "VM terminated with exception: {}", err);
let exception = tvm_exception(err)?;
vm_phase.exit_code = if let Some(code) = exception.custom_code() {
code
} else {
!(exception.exception_code().unwrap_or(ExceptionCode::UnknownError) as i32)
};
vm_phase.exit_arg = match exception.value.as_integer().and_then(|value| value.into(std::i32::MIN..=std::i32::MAX)) {
Err(_) | Ok(0) => None,
Ok(exit_arg) => Some(exit_arg)
};
raw_exit_arg = Some(exception.value);
}
Ok(exit_code) => vm_phase.exit_code = exit_code
};
vm_phase.success = vm.get_committed_state().is_committed();
log::debug!(target: "executor", "VM terminated with exit code {}", vm_phase.exit_code);
// calc gas fees
let gas = vm.get_gas();
let credit = gas.get_gas_credit() as u32;
//for external messages gas will not be exacted if VM throws the exception and gas_credit != 0
let used = gas.get_gas_used() as u64;
vm_phase.gas_used = used.into();
if credit != 0 {
if is_external {
fail!(ExecutorError::NoAcceptError(vm_phase.exit_code, raw_exit_arg))
}
vm_phase.gas_fees = Grams::zero();
} else { // credit == 0 means contract accepted
let gas_fees = if is_special { 0 } else { gas_config.calc_gas_fee(used) };
vm_phase.gas_fees = gas_fees.into();
};
log::debug!(
target: "executor",
"gas after: gl: {}, gc: {}, gu: {}, fees: {}",
gas.get_gas_limit() as u64, credit, used, vm_phase.gas_fees
);
//set mode
vm_phase.mode = 0;
vm_phase.vm_steps = vm.steps();
//TODO: vm_final_state_hash
log::debug!(target: "executor", "acc_balance: {}, gas fees: {}", acc_balance.grams, vm_phase.gas_fees);
if !acc_balance.grams.sub(&vm_phase.gas_fees)? {
log::debug!(target: "executor", "can't sub funds: {} from acc_balance: {}", vm_phase.gas_fees, acc_balance.grams);
fail!(ExecutorError::TrExecutorError("can't sub funds: from acc_balance".to_string()))
}
let new_data = if let StackItem::Cell(cell) = vm.get_committed_state().get_root() {
Some(cell)
} else {
log::debug!(target: "executor", "invalid contract, it must be cell in c4 register");
vm_phase.success = false;
None
};
let out_actions = if let StackItem::Cell(root_cell) = vm.get_committed_state().get_actions() {
Some(root_cell)
} else {
log::debug!(target: "executor", "invalid contract, it must be cell in c5 register");
vm_phase.success = false;
None
};
*acc = result_acc;
Ok((TrComputePhase::Vm(vm_phase), out_actions, new_data))
}
/// Implementation of transaction's action phase.
/// If computing phase is successful then action phase is started.
/// If TVM invoked in computing phase returned some output actions,
/// then they will be added to transaction's output message list.
/// Total value from all outbound internal messages will be collected and
/// substracted from account balance. If account has enough funds this
/// will be succeded, otherwise action phase is failed, transaction will be
/// marked as aborted, account changes will be rollbacked.
fn action_phase(
&self,
tr: &mut Transaction,
acc: &mut Account,
original_acc_balance: &CurrencyCollection,
acc_balance: &mut CurrencyCollection,
msg_remaining_balance: &mut CurrencyCollection,
compute_phase_fees: &Grams,
actions_cell: Cell,
new_data: Option<Cell>,
is_special: bool,
) -> Option<(TrActionPhase, Vec<Message>)> {
let mut acc_copy = acc.clone();
let mut acc_remaining_balance = acc_balance.clone();
let mut phase = TrActionPhase::default();
let mut total_reserved_value = CurrencyCollection::default();
let mut actions = match OutActions::construct_from_cell(actions_cell) {
Err(err) => {
log::debug!(
target: "executor",
"cannot parse action list: format is invalid, err: {}",
err
);
// Here you can select only one of 2 error codes: RESULT_CODE_UNKNOWN_OR_INVALID_ACTION or RESULT_CODE_ACTIONLIST_INVALID
phase.result_code = RESULT_CODE_UNKNOWN_OR_INVALID_ACTION;
return Some((phase, vec![]))
}
Ok(actions) => actions
};
if actions.len() > MAX_ACTIONS {
log::debug!(target: "executor", "too many actions: {}", actions.len());
phase.result_code = RESULT_CODE_TOO_MANY_ACTIONS;
return Some((phase, vec![]))
}
phase.action_list_hash = actions.hash().ok()?;
phase.tot_actions = actions.len() as i16;
let process_err_code = |err_code: i32, i: usize, phase: &mut TrActionPhase| {
if err_code != 0 {
log::debug!(target: "executor", "action failed: error_code={}", err_code);
phase.valid = true;
phase.result_code = err_code;
if i != 0 {
phase.result_arg = Some(i as i32);
}
if err_code == RESULT_CODE_NOT_ENOUGH_GRAMS || err_code == RESULT_CODE_NOT_ENOUGH_EXTRA {
phase.no_funds = true;
}
true
} else {
false
}
};
let mut out_msgs0 = vec![];
let my_addr = acc_copy.get_addr()?.clone();
let workchains = match self.config().raw_config().workchains() {
Ok(workchains) => workchains,
#[cfg(not(test))]
Err(e) => {
log::error!(target: "executor", "get workchains error {}", e);
return None
}
};
for (i, action) in actions.iter_mut().enumerate() {
let err_code = match std::mem::replace(action, OutAction::None) {
OutAction::SendMsg{ mode, mut out_msg } => {
if let Some(header) = out_msg.int_header() {
// let src_workchain_id = my_addr.workchain_id();
match header.dst.workchain_id() {
// allow to send only to -1 from 0 or -1
MASTERCHAIN_ID => {
if my_addr.workchain_id() != MASTERCHAIN_ID && my_addr.workchain_id() != BASE_WORKCHAIN_ID {
log::error!(target: "executor", "masterchain cannot accept from {} workchain", my_addr.workchain_id());
return None
}
}
// allow to send to self or from master to any which is possible
workchain_id => {
if my_addr.workchain_id() == workchain_id || my_addr.workchain_id() == MASTERCHAIN_ID {
match workchains.get(&workchain_id) {
Ok(None) => {
log::error!(target: "executor", "workchain {} is not deployed", workchain_id);
return None
}
Err(e) => {
log::error!(target: "executor", "workchain {} cannot be get {}", workchain_id, e);
return None
}
Ok(Some(descr)) => {
if !descr.accept_msgs {
log::error!(target: "executor", "cannot send message from {} to {} it doesn't accept", header.src, header.dst);
return None
}
}
}
} else {
log::error!(target: "executor", "cannot send message from {} to {} it doesn't allow yet", header.src, header.dst);
return None
}
}
}
}
if (mode & SENDMSG_ALL_BALANCE) != 0 {
out_msgs0.push((mode, out_msg));
continue
}
let result = outmsg_action_handler(
&mut phase,
mode,
&mut out_msg,
&mut acc_remaining_balance,
msg_remaining_balance,
compute_phase_fees,
self.config(),
is_special,
&my_addr
);
match result {
Ok(_) => {
phase.msgs_created += 1;
out_msgs0.push((mode, out_msg));
0
}
Err(code) => code
}
}
OutAction::ReserveCurrency{ mode, value } => {
match reserve_action_handler(mode, &value, original_acc_balance, &mut acc_remaining_balance) {
Ok(reserved_value) => {
phase.spec_actions += 1;
match total_reserved_value.add(&reserved_value) {
Ok(_) => 0,
Err(_) => RESULT_CODE_INVALID_BALANCE
}
}
Err(code) => code
}
}
OutAction::SetCode{ new_code: code } => {
match setcode_action_handler(&mut acc_copy, code) {
None => {
phase.spec_actions += 1;
0
}
Some(code) => code
}
}
OutAction::ChangeLibrary{ mode, code, hash} => {
match change_library_action_handler(&mut acc_copy, mode, code, hash) {
None => {
phase.spec_actions += 1;
0
}
Some(code) => code
}
}
OutAction::None => RESULT_CODE_UNKNOWN_OR_INVALID_ACTION
};
if process_err_code(err_code, i, &mut phase) {
return Some((phase, vec![]))
}
}
let mut out_msgs = vec![];
for (i, (mode, mut out_msg)) in out_msgs0.into_iter().enumerate() {
if (mode & SENDMSG_ALL_BALANCE) == 0 {
out_msgs.push(out_msg);
continue
}
let result = outmsg_action_handler(
&mut phase,
mode,
&mut out_msg,
&mut acc_remaining_balance,
msg_remaining_balance,
compute_phase_fees,
self.config(),
is_special,
&my_addr
);
let err_code = match result {
Ok(_) => {
phase.msgs_created += 1;
out_msgs.push(out_msg);
0
}
Err(code) => code
};
if process_err_code(err_code, i, &mut phase) {
return Some((phase, vec![]));
}
}
//calc new account balance
if let Err(err) = acc_remaining_balance.add(&total_reserved_value) {
log::debug!(target: "executor", "failed to add account balance with reserved value {}", err);
return None
}
if let Some(fee) = phase.total_action_fees.as_ref() {
log::debug!(target: "executor", "action fees: {}", fee);
tr.add_fee_grams(fee).ok()?;
}
phase.valid = true;
phase.success = true;
*acc_balance = acc_remaining_balance;
*acc = acc_copy;
if let Some(new_data) = new_data {
acc.set_data(new_data);
}
Some((phase, out_msgs))
}
/// Implementation of transaction's bounce phase.
/// Bounce phase occurs only if transaction 'aborted' flag is set and
/// if inbound message is internal message with field 'bounce=true'.
/// Generates outbound internal message for original message sender, with value equal
/// to value of original message minus gas payments and forwarding fees
/// and empty body. Generated message is added to transaction's output message list.
fn bounce_phase(
&self,
mut remaining_msg_balance: CurrencyCollection,
compute_phase_fees: &Grams,
msg: &Message,
tr: &mut Transaction,
my_addr: &MsgAddressInt,
) -> Option<(TrBouncePhase, Option<Message>)> {
let header = msg.int_header()?;
if !header.bounce { // TODO convert to panic
return None
}
// create bounced message and swap src and dst addresses
let mut header = header.clone();
let msg_src = header.src_ref()?.clone();
let msg_dst = std::mem::replace(&mut header.dst, msg_src);
header.set_src(msg_dst);
if let Some(new_dst) = check_rewrite_dest_addr(my_addr, &header.dst, self.config()) {
header.dst = new_dst;
} else {
log::warn!(target: "executor", "Incorrect destination address in a bounced message {}", header.dst);
return None
}
// calculated storage for bounced message is empty
let storage = StorageUsedShort::default();
let fwd_prices = self.config().get_fwd_prices(msg.is_masterchain());
let fwd_full_fees = fwd_prices.fwd_fee(&Cell::default());
let fwd_mine_fees = fwd_prices.mine_fee(&fwd_full_fees);
let fwd_fees = Grams::from(fwd_full_fees.0 - fwd_mine_fees.0);
if remaining_msg_balance.grams.0 < fwd_full_fees.0 + compute_phase_fees.0 {
log::debug!(
target: "executor", "bounce phase - not enough grams {} to get fwd fee {}",
remaining_msg_balance, fwd_full_fees
);
return Some((TrBouncePhase::no_funds(storage, fwd_full_fees), None))
}
// create header for new bounced message and swap src and dst addresses
log::debug!(target: "executor", "get fee {} from bounce msg {}", fwd_full_fees, remaining_msg_balance);
remaining_msg_balance.grams.sub(&fwd_full_fees).ok()?;
remaining_msg_balance.grams.sub(&compute_phase_fees).ok()?;
header.ihr_disabled = true;
header.bounce = false;
header.bounced = true;
header.ihr_fee = Grams::zero();
header.fwd_fee = fwd_fees.clone();
header.value = remaining_msg_balance.clone();
let mut bounce_msg = Message::with_int_header(header);
if self.config().has_capability(GlobalCapabilities::CapBounceMsgBody) {
let mut builder = (-1i32).write_to_new_cell().ok()?;
if let Some(mut body) = msg.body() {
body.shrink_data(0..256);
builder.append_bytestring(&body).ok()?;
}
bounce_msg.set_body(builder.into_cell().ok()?.into());
}
log::debug!(target: "executor", "bounce fees: {} bounce value: {}", fwd_mine_fees, bounce_msg.get_value().unwrap().grams);
tr.add_fee_grams(&fwd_mine_fees).ok()?;
Some((TrBouncePhase::ok(storage, fwd_mine_fees, fwd_fees), Some(bounce_msg)))
}
fn add_messages(&self, tr: &mut Transaction, out_msgs: Vec<Message>, lt: Arc<AtomicU64>) -> Result<u64> {
let mut lt = lt.fetch_add(1 + out_msgs.len() as u64, Ordering::Relaxed);
lt += 1;
for mut msg in out_msgs {
msg.set_at_and_lt(tr.now(), lt);
tr.add_out_message(&msg)?;
lt += 1;
}
Ok(lt)
}
}
/// Calculate new account state according to inbound message and current account state.
/// If account does not exist - it can be created with uninitialized state.
/// If account is uninitialized - it can be created with active state.
/// If account exists - it can be frozen.
/// Returns computed initial phase.
fn compute_new_state(acc: &mut Account, acc_balance: &CurrencyCollection, in_msg: &Message, init_code_hash: bool) -> Option<ComputeSkipReason> {
log::debug!(target: "executor", "compute_account_state");
match acc.status() {
AccountStatus::AccStateNonexist => {
log::error!(target: "executor", "account must exist");
Some(ComputeSkipReason::BadState)
}
//Account exists, but can be in different states.
AccountStatus::AccStateActive => {
//account is active, just return it
log::debug!(target: "executor", "account state: AccountActive");
None
}
AccountStatus::AccStateUninit => {
log::debug!(target: "executor", "AccountUninit");
if let Some(state_init) = in_msg.state_init() {
// if msg is a constructor message then
// borrow code and data from it and switch account state to 'active'.
log::debug!(target: "executor", "message for uninitialized: activated");
match acc.try_activate_by_init_code_hash(state_init, init_code_hash) {
Err(err) => {
log::debug!(target: "executor", "reason: {}", err);
Some(ComputeSkipReason::NoState)
}
Ok(_) => None
}
} else {
log::debug!(target: "executor", "message for uninitialized: skip computing phase");
Some(ComputeSkipReason::NoState)
}
}
AccountStatus::AccStateFrozen => {
log::debug!(target: "executor", "AccountFrozen");
//account balance was credited and if it positive after that
//and inbound message bear code and data then make some check and unfreeze account
if !acc_balance.grams.is_zero() {
if let Some(state_init) = in_msg.state_init() {
log::debug!(target: "executor", "message for frozen: activated");
return match acc.try_activate_by_init_code_hash(state_init, init_code_hash) {
Err(err) => {
log::debug!(target: "executor", "reason: {}", err);
Some(ComputeSkipReason::NoState)
}
Ok(_) => None
}
}
}
//skip computing phase, because account is frozen (bad state)
log::debug!(target: "executor", "account is frozen (bad state): skip computing phase");
Some(ComputeSkipReason::NoState)
}
}
}
fn check_replace_src_addr<'a>(src: &'a Option<MsgAddressInt>, acc_addr: &'a MsgAddressInt) -> Option<&'a MsgAddressInt> {
match src {
None => Some(acc_addr),
Some(src) => match src {
AddrStd(_) => {
if src != acc_addr {
None
} else {
Some(src)
}
}
AddrVar(_) => None
}
}
}
fn is_valid_addr_len(addr_len: u16, min_addr_len: u16, max_addr_len: u16, addr_len_step: u16) -> bool {
(addr_len >= min_addr_len) && (addr_len <= max_addr_len) &&
((addr_len == min_addr_len) || (addr_len == max_addr_len) ||
((addr_len_step != 0) && ((addr_len - min_addr_len) % addr_len_step == 0)))
}
fn check_rewrite_dest_addr(src: &MsgAddressInt, dst: &MsgAddressInt, config: &BlockchainConfig) -> Option<MsgAddressInt> {
let (mut anycast_opt, addr_len, workchain_id, address, mut repack);
match dst {
MsgAddressInt::AddrVar(dst) => {
repack = dst.addr_len.0 == 256 && dst.workchain_id >= -128 && dst.workchain_id < 128;
anycast_opt = dst.anycast.clone();
addr_len = dst.addr_len.0 as u16;
workchain_id = dst.workchain_id;
address = dst.address.clone();
}
MsgAddressInt::AddrStd(dst) => {
repack = false;
anycast_opt = dst.anycast.clone();
addr_len = 256;
workchain_id = dst.workchain_id as i32;
address = dst.address.clone();
}
}
let is_masterchain = workchain_id == MASTERCHAIN_ID;
if !is_masterchain {
let workchains = config.raw_config().workchains().unwrap_or_default();
if let Ok(Some(wc)) = workchains.get(&workchain_id) {
if !wc.accept_msgs {
log::debug!(target: "executor", "destination address belongs to workchain {} not accepting new messages", workchain_id);
return None;
}
let (min_addr_len, max_addr_len, addr_len_step) = match wc.format {
WorkchainFormat::Extended(wf) => (wf.min_addr_len(), wf.max_addr_len(), wf.addr_len_step()),
WorkchainFormat::Basic(_) => (256, 256, 0)
};
if !is_valid_addr_len(addr_len, min_addr_len, max_addr_len, addr_len_step) {
log::debug!(target: "executor", "destination address has length {} invalid for destination workchain {}", addr_len, workchain_id);
return None
}
} else {
log::debug!(target: "executor", "destination address contains unknown workchain_id {}", workchain_id);
return None
}
}
if let Some(anycast) = &anycast_opt {
if is_masterchain {
log::debug!(target: "executor", "masterchain address cannot be anycast");
return None
}
match src.address().get_slice(0, anycast.depth.0 as usize) {
Ok(pfx) => {
if pfx != anycast.rewrite_pfx {
match AnycastInfo::with_rewrite_pfx(pfx) {
Ok(anycast) => {
repack = true;
anycast_opt = Some(anycast)
}
Err(err) => {
log::debug!(target: "executor", "Incorrect anycast prefix {}", err);
return None
}
}
}
}
Err(err) => {
log::debug!(target: "executor", "Incorrect src address {}", err);
return None
}
}
}
if !repack {
Some(dst.clone())
} else if addr_len == 256 && workchain_id >= -128 && workchain_id < 128 {
// repack as an addr_std
MsgAddressInt::with_standart(anycast_opt, workchain_id as i8, address).ok()
} else {
// repack as an addr_var
MsgAddressInt::with_variant(anycast_opt, workchain_id, address).ok()
}
}
fn outmsg_action_handler(
phase: &mut TrActionPhase,
mut mode: u8,
msg: &mut Message,
acc_balance: &mut CurrencyCollection,
msg_balance: &mut CurrencyCollection,
compute_phase_fees: &Grams,
config: &BlockchainConfig,
is_special: bool,
my_addr: &MsgAddressInt,
) -> std::result::Result<CurrencyCollection, i32> {
// we cannot send all balance from account and from message simultaneously ?
let invalid_flags = SENDMSG_REMAINING_MSG_BALANCE | SENDMSG_ALL_BALANCE;
if (mode & !SENDMSG_VALID_FLAGS) != 0 ||
(mode & invalid_flags) == invalid_flags ||
((mode & SENDMSG_DELETE_IF_EMPTY) != 0 && (mode & SENDMSG_ALL_BALANCE) == 0)
{
log::error!(target: "executor", "outmsg mode has unsupported flags");
return Err(RESULT_CODE_UNSUPPORTED);
}
let skip = if (mode & SENDMSG_IGNORE_ERROR) != 0 {
None
} else {
Some(())
};
let (fwd_mine_fee, total_fwd_fees);
let mut result_value; // to sub from acc_balance
if let Some(new_src) = check_replace_src_addr(&msg.src(), my_addr) {
msg.set_src_address(new_src.clone());
} else {
log::warn!(target: "executor", "Incorrect source address {:?}", msg.src());
return Err(RESULT_CODE_INCORRECT_SRC_ADDRESS);
}
let fwd_prices = config.get_fwd_prices(msg.is_masterchain());
let compute_fwd_fee = if is_special {
Grams::default()
} else {
msg.serialize()
.map(|cell| fwd_prices.fwd_fee(&cell))
.map_err(|err| {
log::error!(target: "executor", "cannot serialize message in action phase : {}", err);
RESULT_CODE_ACTIONLIST_INVALID
})?
};
if let Some(int_header) = msg.int_header_mut() {
if let Some(new_dst) = check_rewrite_dest_addr(my_addr, &int_header.dst, config) {
int_header.dst = new_dst;
} else {
log::warn!(target: "executor", "Incorrect destination address {}", int_header.dst);
return Err(skip.map(|_| RESULT_CODE_INCORRECT_DST_ADDRESS).unwrap_or(0))
}
int_header.bounced = false;
result_value = int_header.value.clone();
int_header.ihr_disabled = true; // ihr is disabled because it does not work
if int_header.ihr_disabled {
int_header.ihr_fee = Grams::default();
} else {
let compute_ihr_fee = fwd_prices.ihr_fee(&compute_fwd_fee);
if int_header.ihr_fee < compute_ihr_fee {
int_header.ihr_fee = compute_ihr_fee
}
}
let fwd_fee = std::cmp::max(&int_header.fwd_fee, &compute_fwd_fee).clone();
fwd_mine_fee = fwd_prices.mine_fee(&fwd_fee);
total_fwd_fees = Grams::from(fwd_fee.0 + int_header.ihr_fee.0);
let fwd_remain_fee = fwd_fee.0 - fwd_mine_fee.0;
if (mode & SENDMSG_ALL_BALANCE) != 0 {
//send all remaining account balance
result_value = acc_balance.clone();
int_header.value = acc_balance.clone();
mode &= !SENDMSG_PAY_FEE_SEPARATELY;
}
if (mode & SENDMSG_REMAINING_MSG_BALANCE) != 0 {
//send all remainig balance of inbound message
result_value.add(msg_balance).ok();
if (mode & SENDMSG_PAY_FEE_SEPARATELY) == 0 {
if result_value.grams.0 < compute_phase_fees.0 {
return Err(skip.map(|_| RESULT_CODE_NOT_ENOUGH_GRAMS).unwrap_or(0))