reth_storage_api/
metadata.rs1use alloc::vec::Vec;
4pub use reth_db_api::models::PipelineUnwind;
5use reth_db_api::models::{SnapAttempt, StorageSettings, SNAP_ATTEMPT_VERSION};
6use reth_storage_errors::provider::{ProviderError, ProviderResult};
7
8pub mod keys {
10 pub const STORAGE_SETTINGS: &str = "storage_settings";
12
13 pub const PIPELINE_UNWIND: &str = "pipeline_unwind";
15
16 pub const SNAP_ATTEMPT: &str = "snap_attempt";
18}
19
20#[auto_impl::auto_impl(&, Arc)]
22pub trait MetadataProvider: Send {
23 fn get_metadata(&self, key: &str) -> ProviderResult<Option<Vec<u8>>>;
25
26 fn pipeline_unwind(&self) -> ProviderResult<Option<PipelineUnwind>> {
29 self.get_metadata(keys::PIPELINE_UNWIND)?
30 .map(|bytes| serde_json::from_slice(&bytes).map_err(ProviderError::other))
31 .transpose()
32 }
33
34 fn storage_settings(&self) -> ProviderResult<Option<StorageSettings>> {
40 Ok(self
41 .get_metadata(keys::STORAGE_SETTINGS)?
42 .and_then(|bytes| serde_json::from_slice(&bytes).ok()))
43 }
44
45 fn snap_attempt(&self) -> ProviderResult<Option<SnapAttempt>> {
50 let Some(bytes) = self.get_metadata(keys::SNAP_ATTEMPT)? else { return Ok(None) };
51
52 let value: serde_json::Value =
55 serde_json::from_slice(&bytes).map_err(ProviderError::other)?;
56 let found = value.get("version").and_then(serde_json::Value::as_u64);
57 if found != Some(SNAP_ATTEMPT_VERSION as u64) {
58 return Err(ProviderError::UnsupportedSnapAttemptVersion {
59 found,
60 supported: SNAP_ATTEMPT_VERSION,
61 })
62 }
63
64 serde_json::from_slice(&bytes).map(Some).map_err(ProviderError::other)
65 }
66}
67
68pub trait MetadataWriter: Send {
70 fn write_metadata(&self, key: &str, value: Vec<u8>) -> ProviderResult<()>;
72
73 fn delete_metadata(&self, _key: &str) -> ProviderResult<()> {
75 Err(ProviderError::UnsupportedProvider)
76 }
77
78 fn write_pipeline_unwind(&self, unwind: Option<&PipelineUnwind>) -> ProviderResult<()> {
80 match unwind {
81 Some(unwind) => self.write_metadata(
82 keys::PIPELINE_UNWIND,
83 serde_json::to_vec(unwind).map_err(ProviderError::other)?,
84 ),
85 None => self.delete_metadata(keys::PIPELINE_UNWIND),
86 }
87 }
88
89 fn write_storage_settings(&self, settings: StorageSettings) -> ProviderResult<()> {
94 self.write_metadata(
95 keys::STORAGE_SETTINGS,
96 serde_json::to_vec(&settings).map_err(ProviderError::other)?,
97 )
98 }
99
100 fn write_snap_attempt(&self, attempt: &SnapAttempt) -> ProviderResult<()> {
102 self.write_metadata(
103 keys::SNAP_ATTEMPT,
104 serde_json::to_vec(attempt).map_err(ProviderError::other)?,
105 )
106 }
107}
108
109pub trait StorageSettingsCache: Send {
111 fn cached_storage_settings(&self) -> StorageSettings;
113
114 fn set_storage_settings_cache(&self, settings: StorageSettings);
119}
120
121#[cfg(feature = "std")]
123pub trait StoragePath: Send {
124 fn storage_path(&self) -> std::path::PathBuf;
126}