fuel_core/database/
transactions.rs

1use crate::{
2    database::OffChainIterableKeyValueView,
3    fuel_core_graphql_api::storage::transactions::{
4        OwnedTransactionIndexCursor,
5        OwnedTransactionIndexKey,
6        OwnedTransactions,
7        TransactionStatuses,
8    },
9};
10use fuel_core_storage::{
11    Result as StorageResult,
12    iter::{
13        IterDirection,
14        IteratorOverTable,
15    },
16};
17use fuel_core_types::{
18    self,
19    fuel_tx::{
20        Bytes32,
21        TxPointer,
22    },
23    fuel_types::Address,
24    services::transaction_status::TransactionExecutionStatus,
25};
26
27#[cfg(feature = "test-helpers")]
28impl crate::database::Database {
29    pub fn all_transactions(
30        &self,
31        start: Option<&Bytes32>,
32        direction: Option<IterDirection>,
33    ) -> impl Iterator<Item = StorageResult<fuel_core_types::fuel_tx::Transaction>>
34    + '_
35    + use<'_> {
36        use fuel_core_storage::tables::Transactions;
37        self.iter_all_by_start::<Transactions>(start, direction)
38            .map(|res| res.map(|(_, tx)| tx))
39    }
40}
41
42impl OffChainIterableKeyValueView {
43    /// Iterates over a KV mapping of `[address + block height + tx idx] => transaction id`. This
44    /// allows for efficient lookup of transaction ids associated with an address, sorted by
45    /// block age and ordering within a block. The cursor tracks the `[block height + tx idx]` for
46    /// pagination purposes.
47    pub fn owned_transactions(
48        &self,
49        owner: Address,
50        start: Option<OwnedTransactionIndexCursor>,
51        direction: Option<IterDirection>,
52    ) -> impl Iterator<Item = StorageResult<(TxPointer, Bytes32)>> + '_ {
53        let start = start.map(|cursor| {
54            OwnedTransactionIndexKey::new(&owner, cursor.block_height, cursor.tx_idx)
55        });
56        self.iter_all_filtered::<OwnedTransactions, _>(
57            Some(owner),
58            start.as_ref(),
59            direction,
60        )
61        .map(|res| {
62            res.map(|(key, tx_id)| (TxPointer::new(key.block_height, key.tx_idx), tx_id))
63        })
64    }
65
66    pub fn get_tx_status(
67        &self,
68        id: &Bytes32,
69    ) -> StorageResult<Option<TransactionExecutionStatus>> {
70        use fuel_core_storage::StorageAsRef;
71        self.storage::<TransactionStatuses>()
72            .get(id)
73            .map(|v| v.map(|v| v.into_owned()))
74    }
75}