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
use super::MaybeRelayerAdapter;
use crate::{
    database::Database,
    executor::{
        ExecutionBlockWithSource,
        Executor,
    },
    service::adapters::{
        ExecutorAdapter,
        TransactionsSource,
    },
};
use fuel_core_executor::refs::ContractStorageTrait;
use fuel_core_storage::{
    transactional::StorageTransaction,
    Error as StorageError,
};
use fuel_core_types::{
    blockchain::primitives::DaBlockHeight,
    entities::message::CompressedMessage,
    fuel_tx,
    fuel_tx::{
        Receipt,
        Transaction,
    },
    fuel_types::MessageId,
    services::{
        block_producer::Components,
        executor::{
            Result as ExecutorResult,
            UncommittedResult,
        },
    },
};

impl crate::executor::TransactionsSource for TransactionsSource {
    fn next(&self, gas_limit: u64) -> Vec<Transaction> {
        self.txpool
            .select_transactions(gas_limit)
            .iter()
            .map(|tx| tx.as_ref().into())
            .collect()
    }
}

impl ExecutorAdapter {
    pub(crate) fn _execute_without_commit(
        &self,
        block: ExecutionBlockWithSource<TransactionsSource>,
    ) -> ExecutorResult<UncommittedResult<StorageTransaction<Database>>> {
        let executor = Executor {
            database: self.relayer.database.clone(),
            relayer: self.relayer.clone(),
            config: self.config.clone(),
            utxo_validation: self.config.utxo_validation,
        };
        executor.execute_without_commit(block)
    }

    pub(crate) fn _dry_run(
        &self,
        block: Components<fuel_tx::Transaction>,
        utxo_validation: Option<bool>,
    ) -> ExecutorResult<Vec<Vec<Receipt>>> {
        let executor = Executor {
            database: self.relayer.database.clone(),
            relayer: self.relayer.clone(),
            config: self.config.clone(),
            utxo_validation: self.config.utxo_validation,
        };
        executor.dry_run(block, utxo_validation)
    }
}

/// Implemented to satisfy: `GenesisCommitment for ContractRef<&'a mut Database>`
impl ContractStorageTrait for Database {
    type InnerError = StorageError;
}

impl crate::executor::RelayerPort for MaybeRelayerAdapter {
    fn get_message(
        &self,
        id: &MessageId,
        da_height: &DaBlockHeight,
    ) -> anyhow::Result<Option<CompressedMessage>> {
        #[cfg(feature = "relayer")]
        {
            match self.relayer_synced.as_ref() {
                Some(sync) => sync.get_message(id, da_height),
                None => {
                    if *da_height <= self.da_deploy_height {
                        Ok(fuel_core_storage::StorageAsRef::storage::<
                            fuel_core_storage::tables::Messages,
                        >(&self.database)
                        .get(id)?
                        .map(std::borrow::Cow::into_owned))
                    } else {
                        Ok(None)
                    }
                }
            }
        }
        #[cfg(not(feature = "relayer"))]
        {
            let _ = id;
            let _ = da_height;
            Ok(None)
        }
    }
}