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                stop_on_accept: false,
43                inspector: inspector.as_deref_mut(),
44            })
45            .context("compute phase failed")?;
46
47        // Run action phase only if compute phase succeeded.
48        let mut aborted = true;
49        let mut destroyed = false;
50        let mut action_phase = None;
51        if let ComputePhase::Executed(compute_phase) = &compute_phase
52            && compute_phase.success
53        {
54            let res = self
55                .action_phase(ActionPhaseContext {
56                    received_message: None,
57                    original_balance,
58                    new_state,
59                    actions,
60                    compute_phase,
61                    inspector,
62                })
63                .context("action phase failed")?;
64
65            aborted = !res.action_phase.success;
66            destroyed = self.end_status == AccountStatus::NotExists;
67            action_phase = Some(res.action_phase);
68        }
69
70        // Build transaction info.
71        Ok(TickTockTxInfo {
72            kind,
73            storage_phase,
74            compute_phase,
75            action_phase,
76            aborted,
77            destroyed,
78        })
79    }
80}