Skip to main content

fuel_core_compression/
ports.rs

1//! Ports this service requires to function.
2
3use fuel_core_types::{
4    fuel_compression::RegistryKey,
5    fuel_tx::{
6        Address,
7        AssetId,
8        CompressedUtxoId,
9        UtxoId,
10        Word,
11    },
12    fuel_types::Nonce,
13    tai64::Tai64,
14};
15
16/// Rolling cache for compression.
17/// Holds the latest state which can be event sourced from the compressed blocks.
18/// The changes done using this trait in a single call to `compress` or `decompress`
19/// must be committed atomically, after which block height must be incremented.
20pub trait TemporalRegistry<T> {
21    /// Reads a value from the registry at its current height.
22    fn read_registry(&self, key: &RegistryKey) -> anyhow::Result<T>;
23
24    /// Reads timestamp of the value from the registry.
25    fn read_timestamp(&self, key: &RegistryKey) -> anyhow::Result<Tai64>;
26
27    /// Writes a value from to the registry. The timestamp is the time of the block,
28    /// and it is used for key retention.
29    fn write_registry(
30        &mut self,
31        key: &RegistryKey,
32        value: &T,
33        timestamp: Tai64,
34    ) -> anyhow::Result<()>;
35
36    /// Lookup registry key by the value.
37    fn registry_index_lookup(&self, value: &T) -> anyhow::Result<Option<RegistryKey>>;
38}
39
40impl<D, T> TemporalRegistry<T> for &mut D
41where
42    D: TemporalRegistry<T>,
43{
44    fn read_registry(&self, key: &RegistryKey) -> anyhow::Result<T> {
45        <D as TemporalRegistry<T>>::read_registry(self, key)
46    }
47
48    fn read_timestamp(&self, key: &RegistryKey) -> anyhow::Result<Tai64> {
49        <D as TemporalRegistry<T>>::read_timestamp(self, key)
50    }
51
52    fn write_registry(
53        &mut self,
54        key: &RegistryKey,
55        value: &T,
56        timestamp: Tai64,
57    ) -> anyhow::Result<()> {
58        <D as TemporalRegistry<T>>::write_registry(self, key, value, timestamp)
59    }
60
61    fn registry_index_lookup(&self, value: &T) -> anyhow::Result<Option<RegistryKey>> {
62        <D as TemporalRegistry<T>>::registry_index_lookup(self, value)
63    }
64}
65
66/// Lookup for UTXO pointers used for compression.
67pub trait UtxoIdToPointer {
68    fn lookup(&self, utxo_id: UtxoId) -> anyhow::Result<CompressedUtxoId>;
69}
70
71impl<D> UtxoIdToPointer for &mut D
72where
73    D: UtxoIdToPointer,
74{
75    fn lookup(&self, utxo_id: UtxoId) -> anyhow::Result<CompressedUtxoId> {
76        <D as UtxoIdToPointer>::lookup(self, utxo_id)
77    }
78}
79
80/// Lookup for history of UTXOs and messages, used for decompression.
81pub trait HistoryLookup {
82    fn utxo_id(&self, c: CompressedUtxoId) -> anyhow::Result<UtxoId>;
83    fn coin(&self, utxo_id: UtxoId) -> anyhow::Result<CoinInfo>;
84    fn message(&self, nonce: Nonce) -> anyhow::Result<MessageInfo>;
85}
86
87/// Information about a coin.
88#[derive(Debug, Clone)]
89pub struct CoinInfo {
90    pub owner: Address,
91    pub amount: u64,
92    pub asset_id: AssetId,
93}
94
95/// Information about a message.
96#[derive(Debug, Clone)]
97pub struct MessageInfo {
98    pub sender: Address,
99    pub recipient: Address,
100    pub amount: Word,
101    pub data: Vec<u8>,
102}
103
104/// Evictor registry to keep track of the latest used key for the type `T`.
105pub trait EvictorDb<T> {
106    fn get_latest_assigned_key(&self) -> anyhow::Result<Option<RegistryKey>>;
107    fn set_latest_assigned_key(&mut self, key: RegistryKey) -> anyhow::Result<()>;
108}
109
110impl<D, T> EvictorDb<T> for &mut D
111where
112    D: EvictorDb<T>,
113{
114    fn get_latest_assigned_key(&self) -> anyhow::Result<Option<RegistryKey>> {
115        <D as EvictorDb<T>>::get_latest_assigned_key(self)
116    }
117
118    fn set_latest_assigned_key(&mut self, key: RegistryKey) -> anyhow::Result<()> {
119        <D as EvictorDb<T>>::set_latest_assigned_key(self, key)
120    }
121}
122
123#[cfg(feature = "fault-proving")]
124pub mod fault_proving {
125    pub trait GetRegistryRoot {
126        type Error: core::fmt::Display;
127        fn registry_root(
128            &self,
129        ) -> Result<crate::compressed_block_payload::v1::RegistryRoot, Self::Error>;
130    }
131
132    impl<D> GetRegistryRoot for &mut D
133    where
134        D: GetRegistryRoot,
135    {
136        type Error = D::Error;
137        fn registry_root(
138            &self,
139        ) -> Result<crate::compressed_block_payload::v1::RegistryRoot, Self::Error>
140        {
141            D::registry_root(self)
142        }
143    }
144}
145
146#[cfg(feature = "fault-proving")]
147pub use fault_proving::*;