Skip to main content

tycho_executor/
lib.rs

1use anyhow::{Context, Result};
2use tycho_types::cell::Lazy;
3use tycho_types::dict;
4use tycho_types::error::Error;
5use tycho_types::models::{
6    Account, AccountState, AccountStatus, BlockId, CurrencyCollection, HashUpdate, IntAddr,
7    LibDescr, Message, OwnedMessage, ShardAccount, SimpleLib, StdAddr, StorageInfo, StorageUsed,
8    TickTock, Transaction, TxInfo,
9};
10use tycho_types::num::{Tokens, Uint15};
11use tycho_types::prelude::*;
12
13pub use self::config::ParsedConfig;
14pub use self::error::{TxError, TxResult};
15use self::util::new_varuint56_truncate;
16pub use self::util::{ExtStorageStat, OwnedExtStorageStat, StorageStatLimits};
17
18mod config;
19mod error;
20mod util;
21
22pub mod phase {
23    pub use self::action::{ActionPhaseContext, ActionPhaseFull};
24    pub use self::bounce::BouncePhaseContext;
25    pub use self::compute::{
26        ComputePhaseContext, ComputePhaseFull, ComputePhaseSmcInfo, TransactionInput,
27    };
28    pub use self::receive::{MsgStateInit, ReceivedMessage};
29    pub use self::storage::StoragePhaseContext;
30
31    mod action;
32    mod bounce;
33    mod compute;
34    mod credit;
35    mod receive;
36    mod storage;
37}
38
39mod tx {
40    mod ordinary;
41    mod ticktock;
42}
43
44/// Transaction executor.
45#[derive(Debug)]
46pub struct Executor<'a> {
47    params: &'a ExecutorParams,
48    config: &'a ParsedConfig,
49    min_lt: u64,
50    override_special: Option<bool>,
51}
52
53impl<'a> Executor<'a> {
54    pub fn new(params: &'a ExecutorParams, config: &'a ParsedConfig) -> Self {
55        Self {
56            params,
57            config,
58            min_lt: 0,
59            override_special: None,
60        }
61    }
62
63    pub fn with_min_lt(mut self, min_lt: u64) -> Self {
64        self.set_min_lt(min_lt);
65        self
66    }
67
68    pub fn set_min_lt(&mut self, min_lt: u64) {
69        self.min_lt = min_lt;
70    }
71
72    pub fn override_special(mut self, is_special: bool) -> Self {
73        self.override_special = Some(is_special);
74        self
75    }
76
77    #[inline]
78    pub fn begin_ordinary<'s, M>(
79        &self,
80        address: &StdAddr,
81        is_external: bool,
82        msg: M,
83        state: &'s ShardAccount,
84    ) -> TxResult<UncommittedTransaction<'a, 's>>
85    where
86        M: LoadMessage,
87    {
88        self.begin_ordinary_ext(address, is_external, msg, state, None)
89    }
90
91    pub fn begin_ordinary_ext<'s, M>(
92        &self,
93        address: &StdAddr,
94        is_external: bool,
95        msg: M,
96        state: &'s ShardAccount,
97        inspector: Option<&mut ExecutorInspector<'_>>,
98    ) -> TxResult<UncommittedTransaction<'a, 's>>
99    where
100        M: LoadMessage,
101    {
102        let msg_root = msg.load_message_root()?;
103
104        let account = state.load_account()?;
105        let mut exec = self.begin(address, account)?;
106        let info = exec.run_ordinary_transaction(is_external, msg_root.clone(), inspector)?;
107
108        UncommittedTransaction::with_info(exec, state, Some(msg_root), info).map_err(TxError::Fatal)
109    }
110
111    #[inline]
112    pub fn check_ordinary<M>(&self, address: &StdAddr, msg: M, state: &ShardAccount) -> TxResult<()>
113    where
114        M: LoadMessage,
115    {
116        self.check_ordinary_ext(address, msg, state, None)
117    }
118
119    pub fn check_ordinary_ext<M>(
120        &self,
121        address: &StdAddr,
122        msg: M,
123        state: &ShardAccount,
124        inspector: Option<&mut ExecutorInspector<'_>>,
125    ) -> TxResult<()>
126    where
127        M: LoadMessage,
128    {
129        let msg_root = msg.load_message_root()?;
130        let account = state.load_account()?;
131        let mut exec = self.begin(address, account)?;
132        exec.check_ordinary_transaction(msg_root, inspector)
133    }
134
135    #[inline]
136    pub fn begin_tick_tock<'s>(
137        &self,
138        address: &StdAddr,
139        kind: TickTock,
140        state: &'s ShardAccount,
141    ) -> TxResult<UncommittedTransaction<'a, 's>> {
142        self.begin_tick_tock_ext(address, kind, state, None)
143    }
144
145    pub fn begin_tick_tock_ext<'s>(
146        &self,
147        address: &StdAddr,
148        kind: TickTock,
149        state: &'s ShardAccount,
150        inspector: Option<&mut ExecutorInspector<'_>>,
151    ) -> TxResult<UncommittedTransaction<'a, 's>> {
152        let account = state.load_account()?;
153        let mut exec = self.begin(address, account)?;
154        let info = exec.run_tick_tock_transaction(kind, inspector)?;
155
156        UncommittedTransaction::with_info(exec, state, None, info).map_err(TxError::Fatal)
157    }
158
159    pub fn begin(&self, address: &StdAddr, account: Option<Account>) -> Result<ExecutorState<'a>> {
160        let is_special = self
161            .override_special
162            .unwrap_or_else(|| self.config.is_special(address));
163
164        let acc_address;
165        let acc_storage_stat;
166        let acc_balance;
167        let acc_state;
168        let orig_status;
169        let end_status;
170        let start_lt;
171        match account {
172            Some(acc) => {
173                acc_address = 'addr: {
174                    if let IntAddr::Std(acc_addr) = acc.address
175                        && acc_addr == *address
176                    {
177                        break 'addr acc_addr;
178                    }
179                    anyhow::bail!("account address mismatch");
180                };
181                acc_storage_stat = acc.storage_stat;
182                acc_balance = acc.balance;
183                acc_state = acc.state;
184                orig_status = acc_state.status();
185                end_status = orig_status;
186                start_lt = std::cmp::max(self.min_lt, acc.last_trans_lt);
187            }
188            None => {
189                acc_address = address.clone();
190                acc_storage_stat = StorageInfo {
191                    used: StorageUsed::ZERO,
192                    storage_extra: Default::default(),
193                    last_paid: 0,
194                    due_payment: None,
195                };
196                acc_balance = CurrencyCollection::ZERO;
197                acc_state = AccountState::Uninit;
198                orig_status = AccountStatus::NotExists;
199                end_status = AccountStatus::Uninit;
200                start_lt = self.min_lt;
201            }
202        };
203
204        let mut is_marks_authority = false;
205        let mut is_suspended_by_marks = false;
206        if self.params.authority_marks_enabled
207            && let Some(marks) = &self.config.authority_marks
208        {
209            is_marks_authority = marks.is_authority(address);
210            is_suspended_by_marks =
211                !is_special && !is_marks_authority && marks.is_suspended(&acc_balance)?;
212        }
213
214        Ok(ExecutorState {
215            params: self.params,
216            config: self.config,
217            is_special,
218            is_marks_authority,
219            is_suspended_by_marks,
220            address: acc_address,
221            storage_stat: acc_storage_stat,
222            balance: acc_balance,
223            state: acc_state,
224            orig_status,
225            end_status,
226            start_lt,
227            end_lt: start_lt + 1,
228            out_msgs: Vec::new(),
229            total_fees: Tokens::ZERO,
230            burned: Tokens::ZERO,
231            cached_storage_stat: None,
232        })
233    }
234}
235
236/// Executor internals inspector.
237#[derive(Default)]
238pub struct ExecutorInspector<'e> {
239    /// Actions list from compute phase.
240    pub actions: Option<Cell>,
241    /// A set of changes of the public libraries dict.
242    ///
243    /// NOTE: The order is the same as the actions order so
244    /// it can contain duplicates and must be folded before use.
245    pub public_libs_diff: Vec<PublicLibraryChange>,
246    /// Compute phase exit code.
247    pub exit_code: Option<i32>,
248    /// Hash of the library in case it was missing during execution.
249    pub missing_library: Option<HashBytes>,
250    /// Total gas consumed (including the remaining "free" gas
251    /// and everything that exceeds the limit).
252    pub total_gas_used: u64,
253    /// Debug output target.
254    pub debug: Option<&'e mut dyn std::fmt::Write>,
255    /// Hook for a compute phase to modify C7 register data.
256    pub modify_smc_info: Option<&'e mut ModifySmcInfoFn>,
257}
258
259/// Public library diff operation.
260#[derive(Debug, Clone, PartialEq, Eq)]
261pub enum PublicLibraryChange {
262    Add(Cell),
263    Remove(HashBytes),
264}
265
266impl PublicLibraryChange {
267    /// Returns a hash of the changed library.
268    pub fn lib_hash(&self) -> &HashBytes {
269        match self {
270            Self::Add(cell) => cell.repr_hash(),
271            Self::Remove(hash) => hash,
272        }
273    }
274}
275
276/// Hook for a compute phase to modify C7 register data.
277pub type ModifySmcInfoFn = dyn FnMut(&mut phase::ComputePhaseSmcInfo) -> Result<()>;
278
279/// Shared state for executor phases.
280#[derive(Debug)]
281pub struct ExecutorState<'a> {
282    pub params: &'a ExecutorParams,
283    pub config: &'a ParsedConfig,
284
285    pub is_special: bool,
286    pub is_marks_authority: bool,
287    pub is_suspended_by_marks: bool,
288
289    pub address: StdAddr,
290    pub storage_stat: StorageInfo,
291    pub balance: CurrencyCollection,
292    pub state: AccountState,
293
294    pub orig_status: AccountStatus,
295    pub end_status: AccountStatus,
296    pub start_lt: u64,
297    pub end_lt: u64,
298
299    pub out_msgs: Vec<Lazy<OwnedMessage>>,
300    pub total_fees: Tokens,
301
302    pub burned: Tokens,
303
304    pub cached_storage_stat: Option<OwnedExtStorageStat>,
305}
306
307#[cfg(test)]
308impl<'a> ExecutorState<'a> {
309    pub(crate) fn new_non_existent(
310        params: &'a ExecutorParams,
311        config: &'a impl AsRef<ParsedConfig>,
312        address: &StdAddr,
313    ) -> Self {
314        Self {
315            params,
316            config: config.as_ref(),
317            is_special: false,
318            is_marks_authority: false,
319            is_suspended_by_marks: false,
320            address: address.clone(),
321            storage_stat: Default::default(),
322            balance: CurrencyCollection::ZERO,
323            state: AccountState::Uninit,
324            orig_status: AccountStatus::NotExists,
325            end_status: AccountStatus::Uninit,
326            start_lt: 0,
327            end_lt: 1,
328            out_msgs: Vec::new(),
329            total_fees: Tokens::ZERO,
330            burned: Tokens::ZERO,
331            cached_storage_stat: None,
332        }
333    }
334
335    pub(crate) fn new_uninit(
336        params: &'a ExecutorParams,
337        config: &'a impl AsRef<ParsedConfig>,
338        address: &StdAddr,
339        balance: impl Into<CurrencyCollection>,
340    ) -> Self {
341        let mut res = Self::new_non_existent(params, config, address);
342        res.balance = balance.into();
343        res.orig_status = AccountStatus::Uninit;
344
345        if params.authority_marks_enabled
346            && let Some(marks) = &config.as_ref().authority_marks
347        {
348            res.is_marks_authority = marks.is_authority(address);
349            res.is_suspended_by_marks = !res.is_special
350                && !res.is_marks_authority
351                && marks.is_suspended(&res.balance).unwrap();
352        }
353
354        res
355    }
356
357    pub(crate) fn new_frozen(
358        params: &'a ExecutorParams,
359        config: &'a impl AsRef<ParsedConfig>,
360        address: &StdAddr,
361        balance: impl Into<CurrencyCollection>,
362        state_hash: HashBytes,
363    ) -> Self {
364        let mut res = Self::new_uninit(params, config, address, balance);
365        res.state = AccountState::Frozen(state_hash);
366        res.orig_status = AccountStatus::Frozen;
367        res.end_status = AccountStatus::Frozen;
368        res
369    }
370
371    pub(crate) fn new_active(
372        params: &'a ExecutorParams,
373        config: &'a impl AsRef<ParsedConfig>,
374        address: &StdAddr,
375        balance: impl Into<CurrencyCollection>,
376        data: Cell,
377        code_boc: impl AsRef<[u8]>,
378    ) -> Self {
379        use tycho_types::models::StateInit;
380
381        let mut res = Self::new_uninit(params, config, address, balance);
382        res.state = AccountState::Active(StateInit {
383            split_depth: None,
384            special: None,
385            code: Some(Boc::decode(code_boc).unwrap()),
386            data: Some(data),
387            libraries: Dict::new(),
388        });
389        res.orig_status = AccountStatus::Active;
390        res.end_status = AccountStatus::Active;
391        res
392    }
393}
394
395/// Executor configuration parameters.
396#[derive(Debug, Default, Clone)]
397pub struct ExecutorParams {
398    /// Public libraries from the referenced masterchain state.
399    pub libraries: Dict<HashBytes, LibDescr>,
400    /// Rand seed of the block.
401    pub rand_seed: HashBytes,
402    /// Unix timestamp in seconds of the block.
403    pub block_unixtime: u32,
404    /// Logical time of the block.
405    pub block_lt: u64,
406    /// Last known previous masterchain block before current.
407    pub prev_mc_block_id: Option<BlockId>,
408    /// VM behaviour modifiers.
409    pub vm_modifiers: tycho_vm::BehaviourModifiers,
410    /// Prevent [`Frozen`] accounts from being deleted
411    /// when their storage due is too high.
412    ///
413    /// [`Frozen`]: tycho_types::models::AccountState::Frozen
414    pub disable_delete_frozen_accounts: bool,
415    /// Charge account balance for additional `total_action_fees`
416    /// when action phase fails.
417    pub charge_action_fees_on_fail: bool,
418    /// Attaches an original message body as an additional cell
419    /// to a bounced message body.
420    pub full_body_in_bounced: bool,
421    /// More gas-predictable extra currency behaviour.
422    pub strict_extra_currency: bool,
423    /// Whether accounts can be suspended by authority marks.
424    pub authority_marks_enabled: bool,
425}
426
427/// Executed transaction.
428pub struct UncommittedTransaction<'a, 's> {
429    original: &'s ShardAccount,
430    exec: ExecutorState<'a>,
431    in_msg: Option<Cell>,
432    info: Lazy<TxInfo>,
433}
434
435impl<'a, 's> UncommittedTransaction<'a, 's> {
436    #[inline]
437    pub fn with_info(
438        exec: ExecutorState<'a>,
439        original: &'s ShardAccount,
440        in_msg: Option<Cell>,
441        info: impl Into<TxInfo>,
442    ) -> Result<Self> {
443        let info = info.into();
444        let info = Lazy::new(&info)?;
445        Ok(Self {
446            original,
447            exec,
448            in_msg,
449            info,
450        })
451    }
452
453    /// Creates a partially finalized transaction.
454    ///
455    /// It differs from the normal transaction by having a stub `state_update`
456    /// and possibly denormalized `end_status`.
457    pub fn build_uncommitted(&self) -> Result<Transaction, Error> {
458        thread_local! {
459            static EMPTY_STATE_UPDATE: Lazy<HashUpdate> = Lazy::new(&HashUpdate {
460                old: HashBytes::ZERO,
461                new: HashBytes::ZERO,
462            })
463            .unwrap();
464        }
465
466        self.build_transaction(self.exec.end_status, EMPTY_STATE_UPDATE.with(Clone::clone))
467    }
468
469    /// Creates a final transaction and a new contract state.
470    pub fn commit(mut self) -> Result<ExecutorOutput> {
471        // Collect brief account state info and build new account state.
472        let account_state;
473        let new_state_meta;
474        let end_status = match self.build_account_state()? {
475            None => {
476                // TODO: Replace with a constant?
477                account_state = CellBuilder::build_from(false)?;
478
479                // Brief meta.
480                new_state_meta = AccountMeta {
481                    balance: CurrencyCollection::ZERO,
482                    libraries: Dict::new(),
483                    exists: false,
484                };
485
486                // Done
487                AccountStatus::NotExists
488            }
489            Some(state) => {
490                // Load previous account storage info.
491                let prev_account_storage = 'prev: {
492                    let mut cs = self.original.account.as_slice_allow_exotic();
493                    if !cs.load_bit()? {
494                        // account_none$0
495                        break 'prev None;
496                    }
497                    // account$1
498                    // addr:MsgAddressInt
499                    IntAddr::load_from(&mut cs)?;
500                    // storage_stat:StorageInfo
501                    let storage_info = StorageInfo::load_from(&mut cs)?;
502                    // storage:AccountStorage
503                    Some((storage_info.used, cs))
504                };
505
506                // Serialize part of the new account state to compute new storage stats.
507                let mut account_storage = CellBuilder::new();
508                // last_trans_lt:uint64
509                account_storage.store_u64(self.exec.end_lt)?;
510                // balance:CurrencyCollection
511                self.exec
512                    .balance
513                    .store_into(&mut account_storage, Cell::empty_context())?;
514                // state:AccountState
515                state.store_into(&mut account_storage, Cell::empty_context())?;
516
517                // Update storage info.
518                self.exec.storage_stat.used = compute_storage_used(
519                    prev_account_storage,
520                    account_storage.as_full_slice(),
521                    &mut self.exec.cached_storage_stat,
522                    self.exec.params.strict_extra_currency,
523                )?;
524
525                // Build new account state.
526                account_state = CellBuilder::build_from((
527                    true,                            // account$1
528                    &self.exec.address,              // addr:MsgAddressInt
529                    &self.exec.storage_stat,         // storage_stat:StorageInfo
530                    account_storage.as_full_slice(), // storage:AccountStorage
531                ))?;
532
533                // Brief meta.
534                let libraries = match &state {
535                    AccountState::Active(state) => state.libraries.clone(),
536                    AccountState::Frozen(..) | AccountState::Uninit => Dict::new(),
537                };
538                new_state_meta = AccountMeta {
539                    balance: self.exec.balance.clone(),
540                    libraries,
541                    exists: true,
542                };
543
544                // Done
545                state.status()
546            }
547        };
548
549        // Serialize transaction.
550        let state_update = Lazy::new(&HashUpdate {
551            old: *self.original.account.repr_hash(),
552            new: *account_state.repr_hash(),
553        })?;
554        let transaction = self
555            .build_transaction(end_status, state_update)
556            .and_then(|tx| Lazy::new(&tx))?;
557
558        // Collect brief transaction info.
559        let transaction_meta = TransactionMeta {
560            total_fees: self.exec.total_fees,
561            next_lt: self.exec.end_lt,
562            out_msgs: self.exec.out_msgs,
563        };
564
565        // New shard account state.
566        let new_state = ShardAccount {
567            // SAFETY: `account_state` is an ordinary cell.
568            account: unsafe { Lazy::from_raw_unchecked(account_state) },
569            last_trans_hash: *transaction.repr_hash(),
570            last_trans_lt: self.exec.start_lt,
571        };
572
573        // Done
574        Ok(ExecutorOutput {
575            new_state,
576            new_state_meta,
577            transaction,
578            transaction_meta,
579            burned: self.exec.burned,
580        })
581    }
582
583    fn build_account_state(&self) -> Result<Option<AccountState>> {
584        Ok(match self.exec.end_status {
585            // Account was deleted.
586            AccountStatus::NotExists => None,
587            // Uninit account with zero balance is also treated as deleted.
588            AccountStatus::Uninit if self.exec.balance.is_zero() => None,
589            // Uninit account stays the same.
590            AccountStatus::Uninit => Some(AccountState::Uninit),
591            // Active account must have a known active state.
592            AccountStatus::Active => {
593                debug_assert!(matches!(self.exec.state, AccountState::Active(_)));
594                Some(self.exec.state.clone())
595            }
596            // Normalize frozen state.
597            AccountStatus::Frozen => {
598                let cell;
599                let frozen_hash = match &self.exec.state {
600                    // Uninit accounts can't be frozen, but if they accidentialy can
601                    // just use the account address as frozen state hash to produce the
602                    // same uninit state.
603                    AccountState::Uninit => &self.exec.address.address,
604                    // To freeze an active account we must compute a hash of its state.
605                    AccountState::Active(state_init) => {
606                        cell = CellBuilder::build_from(state_init)?;
607                        cell.repr_hash()
608                    }
609                    // Account is already frozen.
610                    AccountState::Frozen(hash_bytes) => hash_bytes,
611                };
612
613                // Normalize account state.
614                Some(if frozen_hash == &self.exec.address.address {
615                    AccountState::Uninit
616                } else {
617                    AccountState::Frozen(*frozen_hash)
618                })
619            }
620        })
621    }
622
623    fn build_transaction(
624        &self,
625        end_status: AccountStatus,
626        state_update: Lazy<HashUpdate>,
627    ) -> Result<Transaction, Error> {
628        Ok(Transaction {
629            account: self.exec.address.address,
630            lt: self.exec.start_lt,
631            prev_trans_hash: self.original.last_trans_hash,
632            prev_trans_lt: self.original.last_trans_lt,
633            now: self.exec.params.block_unixtime,
634            out_msg_count: Uint15::new(self.exec.out_msgs.len() as _),
635            orig_status: self.exec.orig_status,
636            end_status,
637            in_msg: self.in_msg.clone(),
638            out_msgs: build_out_msgs(&self.exec.out_msgs)?,
639            total_fees: self.exec.total_fees.into(),
640            state_update,
641            info: self.info.clone(),
642        })
643    }
644}
645
646fn compute_storage_used(
647    mut prev: Option<(StorageUsed, CellSlice<'_>)>,
648    mut new_storage: CellSlice<'_>,
649    cache: &mut Option<OwnedExtStorageStat>,
650    without_extra_currencies: bool,
651) -> Result<StorageUsed> {
652    fn skip_extra(slice: &mut CellSlice<'_>) -> Result<bool, Error> {
653        let mut cs = *slice;
654        cs.skip_first(64, 0)?; // skip lt
655        let balance = CurrencyCollection::load_from(&mut cs)?;
656        Ok(if balance.other.is_empty() {
657            false
658        } else {
659            slice.skip_first(0, 1)?;
660            true
661        })
662    }
663
664    if without_extra_currencies {
665        if let Some((_, prev)) = &mut prev {
666            skip_extra(prev)?;
667        }
668        skip_extra(&mut new_storage)?;
669    }
670
671    // Try to reuse previous storage stats if no cells were changed.
672    if let Some((prev_used, prev_storage)) = prev {
673        'reuse: {
674            // Always recompute when previous stats are invalid.
675            if prev_used.cells.is_zero()
676                || prev_used.bits.into_inner() < prev_storage.size_bits() as u64
677            {
678                break 'reuse;
679            }
680
681            // Simple check that some cells were changed.
682            if prev_storage.size_refs() != new_storage.size_refs() {
683                break 'reuse;
684            }
685
686            // Compare each cell.
687            for (prev, new) in prev_storage.references().zip(new_storage.references()) {
688                if prev != new {
689                    break 'reuse;
690                }
691            }
692
693            // Adjust bit size of the root slice.
694            return Ok(StorageUsed {
695                // Compute the truncated difference of the previous storage root and a new one.
696                bits: new_varuint56_truncate(
697                    (prev_used.bits.into_inner() - prev_storage.size_bits() as u64)
698                        .saturating_add(new_storage.size_bits() as u64),
699                ),
700                // All other stats are unchanged.
701                cells: prev_used.cells,
702            });
703        }
704    }
705
706    // Init cache.
707    let cache = cache.get_or_insert_with(OwnedExtStorageStat::unlimited);
708    cache.set_unlimited();
709
710    // Compute stats for childern.
711    for cell in new_storage.references().cloned() {
712        cache.add_cell(cell);
713    }
714    let stats = cache.stats();
715
716    // Done.
717    Ok(StorageUsed {
718        cells: new_varuint56_truncate(stats.cell_count.saturating_add(1)),
719        bits: new_varuint56_truncate(stats.bit_count.saturating_add(new_storage.size_bits() as _)),
720    })
721}
722
723/// Committed transaction output.
724#[derive(Clone, Debug)]
725pub struct ExecutorOutput {
726    pub new_state: ShardAccount,
727    pub new_state_meta: AccountMeta,
728    pub transaction: Lazy<Transaction>,
729    pub transaction_meta: TransactionMeta,
730    pub burned: Tokens,
731}
732
733/// Short account description.
734#[derive(Clone, Debug)]
735pub struct AccountMeta {
736    pub balance: CurrencyCollection,
737    pub libraries: Dict<HashBytes, SimpleLib>,
738    pub exists: bool,
739}
740
741/// Short transaction description.
742#[derive(Clone, Debug)]
743pub struct TransactionMeta {
744    /// A sum of all collected fees from all phases.
745    pub total_fees: Tokens,
746    /// List of outbound messages.
747    pub out_msgs: Vec<Lazy<OwnedMessage>>,
748    /// Minimal logical time for the next transaction on this account.
749    pub next_lt: u64,
750}
751
752/// Message cell source.
753pub trait LoadMessage {
754    fn load_message_root(self) -> Result<Cell>;
755}
756
757impl<T: LoadMessage + Clone> LoadMessage for &T {
758    #[inline]
759    fn load_message_root(self) -> Result<Cell> {
760        T::load_message_root(T::clone(self))
761    }
762}
763
764impl LoadMessage for Cell {
765    #[inline]
766    fn load_message_root(self) -> Result<Cell> {
767        Ok(self)
768    }
769}
770
771impl<T: EquivalentRepr<OwnedMessage>> LoadMessage for Lazy<T> {
772    #[inline]
773    fn load_message_root(self) -> Result<Cell> {
774        Ok(self.into_inner())
775    }
776}
777
778impl LoadMessage for OwnedMessage {
779    #[inline]
780    fn load_message_root(self) -> Result<Cell> {
781        CellBuilder::build_from(self).context("failed to serialize inbound message")
782    }
783}
784
785impl LoadMessage for Message<'_> {
786    #[inline]
787    fn load_message_root(self) -> Result<Cell> {
788        CellBuilder::build_from(self).context("failed to serialize inbound message")
789    }
790}
791
792fn build_out_msgs(out_msgs: &[Lazy<OwnedMessage>]) -> Result<Dict<Uint15, Cell>, Error> {
793    dict::build_dict_from_sorted_iter(
794        out_msgs
795            .iter()
796            .enumerate()
797            .map(|(i, msg)| (Uint15::new(i as _), msg.inner().clone())),
798        Cell::empty_context(),
799    )
800    .map(Dict::from_raw)
801}
802
803#[cfg(test)]
804mod tests {
805    use std::rc::Rc;
806
807    use tycho_types::boc::BocRepr;
808    use tycho_types::models::{BlockchainConfig, MsgInfo, StateInit};
809
810    use super::*;
811
812    pub fn make_default_config() -> Rc<ParsedConfig> {
813        thread_local! {
814            pub static PARSED_CONFIG: Rc<ParsedConfig> = make_custom_config(|_| Ok(()));
815        }
816
817        PARSED_CONFIG.with(Clone::clone)
818    }
819
820    pub fn make_custom_config<F>(f: F) -> Rc<ParsedConfig>
821    where
822        F: FnOnce(&mut BlockchainConfig) -> anyhow::Result<()>,
823    {
824        let mut config: BlockchainConfig =
825            BocRepr::decode(include_bytes!("../res/config.boc")).unwrap();
826
827        config.params.set_global_id(100).unwrap();
828
829        // TODO: Update config BOC
830        config
831            .params
832            .set_size_limits(&ParsedConfig::DEFAULT_SIZE_LIMITS_CONFIG)
833            .unwrap();
834
835        f(&mut config).unwrap();
836
837        Rc::new(ParsedConfig::parse(config, u32::MAX).unwrap())
838    }
839
840    pub fn make_default_params() -> ExecutorParams {
841        ExecutorParams {
842            block_unixtime: 1738799198,
843            full_body_in_bounced: false,
844            strict_extra_currency: true,
845            vm_modifiers: tycho_vm::BehaviourModifiers {
846                chksig_always_succeed: true,
847                ..Default::default()
848            },
849            ..Default::default()
850        }
851    }
852
853    pub fn make_message(
854        info: impl Into<MsgInfo>,
855        init: Option<StateInit>,
856        body: Option<CellBuilder>,
857    ) -> Cell {
858        let body = match &body {
859            None => Cell::empty_cell_ref().as_slice_allow_exotic(),
860            Some(cell) => cell.as_full_slice(),
861        };
862        CellBuilder::build_from(Message {
863            info: info.into(),
864            init,
865            body,
866            layout: None,
867        })
868        .unwrap()
869    }
870
871    pub fn make_big_tree(depth: u8, count: &mut u16, target: u16) -> Cell {
872        *count += 1;
873
874        if depth == 0 {
875            CellBuilder::build_from(*count).unwrap()
876        } else {
877            let mut b = CellBuilder::new();
878            for _ in 0..4 {
879                if *count < target {
880                    b.store_reference(make_big_tree(depth - 1, count, target))
881                        .unwrap();
882                }
883            }
884            b.build().unwrap()
885        }
886    }
887}