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
use crate::{
    database::{
        database_description::off_chain::OffChain,
        Database,
    },
    fuel_core_graphql_api::storage::transactions::{
        OwnedTransactionIndexCursor,
        OwnedTransactionIndexKey,
        OwnedTransactions,
        TransactionStatuses,
    },
};

use fuel_core_storage::{
    iter::{
        IterDirection,
        IteratorOverTable,
    },
    tables::Transactions,
    Result as StorageResult,
};
use fuel_core_types::{
    self,
    fuel_tx::{
        Bytes32,
        Transaction,
        TxPointer,
    },
    fuel_types::Address,
    services::txpool::TransactionStatus,
};

impl Database {
    pub fn all_transactions(
        &self,
        start: Option<&Bytes32>,
        direction: Option<IterDirection>,
    ) -> impl Iterator<Item = StorageResult<Transaction>> + '_ {
        self.iter_all_by_start::<Transactions>(start, direction)
            .map(|res| res.map(|(_, tx)| tx))
    }
}

impl Database<OffChain> {
    /// Iterates over a KV mapping of `[address + block height + tx idx] => transaction id`. This
    /// allows for efficient lookup of transaction ids associated with an address, sorted by
    /// block age and ordering within a block. The cursor tracks the `[block height + tx idx]` for
    /// pagination purposes.
    pub fn owned_transactions(
        &self,
        owner: Address,
        start: Option<OwnedTransactionIndexCursor>,
        direction: Option<IterDirection>,
    ) -> impl Iterator<Item = StorageResult<(TxPointer, Bytes32)>> + '_ {
        let start = start.map(|cursor| {
            OwnedTransactionIndexKey::new(&owner, cursor.block_height, cursor.tx_idx)
        });
        self.iter_all_filtered::<OwnedTransactions, _>(
            Some(owner),
            start.as_ref(),
            direction,
        )
        .map(|res| {
            res.map(|(key, tx_id)| (TxPointer::new(key.block_height, key.tx_idx), tx_id))
        })
    }

    pub fn get_tx_status(
        &self,
        id: &Bytes32,
    ) -> StorageResult<Option<TransactionStatus>> {
        use fuel_core_storage::StorageAsRef;
        self.storage::<TransactionStatuses>()
            .get(id)
            .map(|v| v.map(|v| v.into_owned()))
    }
}