Skip to main content

tycho_executor/tx/
ticktock.rs

1use anyhow::Context;
2use tycho_types::models::{AccountStatus, ComputePhase, TickTock, TickTockTxInfo};
3
4use crate::error::{TxError, TxResult};
5use crate::phase::{
6    ActionPhaseContext, ComputePhaseContext, ComputePhaseFull, StoragePhaseContext,
7    TransactionInput,
8};
9use crate::{ExecutorInspector, ExecutorState};
10
11impl ExecutorState<'_> {
12    pub fn run_tick_tock_transaction(
13        &mut self,
14        kind: TickTock,
15        mut inspector: Option<&mut ExecutorInspector<'_>>,
16    ) -> TxResult<TickTockTxInfo> {
17        if self.state.status() != AccountStatus::Active {
18            // Skip ticktock transactions for inactive accounts.
19            return Err(TxError::Skipped);
20        }
21
22        // Run storage phase.
23        let storage_phase = self
24            .storage_phase(StoragePhaseContext {
25                adjust_msg_balance: false,
26                received_message: None,
27            })
28            .context("storage phase failed")?;
29
30        // Run compute phase.
31        let ComputePhaseFull {
32            compute_phase,
33            original_balance,
34            new_state,
35            actions,
36            ..
37        } = self
38            .compute_phase(ComputePhaseContext {
39                input: TransactionInput::TickTock(kind),
40                storage_fee: storage_phase.storage_fees_collected,
41                force_accept: false,
42                inspector: inspector.as_deref_mut(),
43            })
44            .context("compute phase failed")?;
45
46        // Run action phase only if compute phase succeeded.
47        let mut aborted = true;
48        let mut destroyed = false;
49        let mut action_phase = None;
50        if let ComputePhase::Executed(compute_phase) = &compute_phase
51            && compute_phase.success
52        {
53            let res = self
54                .action_phase(ActionPhaseContext {
55                    received_message: None,
56                    original_balance,
57                    new_state,
58                    actions,
59                    compute_phase,
60                    inspector,
61                })
62                .context("action phase failed")?;
63
64            aborted = !res.action_phase.success;
65            destroyed = self.end_status == AccountStatus::NotExists;
66            action_phase = Some(res.action_phase);
67        }
68
69        // Build transaction info.
70        Ok(TickTockTxInfo {
71            kind,
72            storage_phase,
73            compute_phase,
74            action_phase,
75            aborted,
76            destroyed,
77        })
78    }
79}