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 VERIFIED_SNAPSHOT_BODIES: &str = "verified_snapshot_bodies";
16
17 pub const PIPELINE_UNWIND: &str = "pipeline_unwind";
19
20 pub const SNAP_ATTEMPT: &str = "snap_attempt";
22}
23
24#[auto_impl::auto_impl(&, Arc)]
26pub trait MetadataProvider: Send {
27 fn get_metadata(&self, key: &str) -> ProviderResult<Option<Vec<u8>>>;
29
30 fn pipeline_unwind(&self) -> ProviderResult<Option<PipelineUnwind>> {
33 self.get_metadata(keys::PIPELINE_UNWIND)?
34 .map(|bytes| serde_json::from_slice(&bytes).map_err(ProviderError::other))
35 .transpose()
36 }
37
38 fn storage_settings(&self) -> ProviderResult<Option<StorageSettings>> {
44 Ok(self
45 .get_metadata(keys::STORAGE_SETTINGS)?
46 .and_then(|bytes| serde_json::from_slice(&bytes).ok()))
47 }
48
49 fn snap_attempt(&self) -> ProviderResult<Option<SnapAttempt>> {
54 let Some(bytes) = self.get_metadata(keys::SNAP_ATTEMPT)? else { return Ok(None) };
55
56 let value: serde_json::Value =
59 serde_json::from_slice(&bytes).map_err(ProviderError::other)?;
60 let found = value.get("version").and_then(serde_json::Value::as_u64);
61 if found != Some(SNAP_ATTEMPT_VERSION as u64) {
62 return Err(ProviderError::UnsupportedSnapAttemptVersion {
63 found,
64 supported: SNAP_ATTEMPT_VERSION,
65 })
66 }
67
68 serde_json::from_slice(&bytes).map(Some).map_err(ProviderError::other)
69 }
70}
71
72pub trait MetadataWriter: Send {
74 fn write_metadata(&self, key: &str, value: Vec<u8>) -> ProviderResult<()>;
76
77 fn delete_metadata(&self, _key: &str) -> ProviderResult<()> {
79 Err(ProviderError::UnsupportedProvider)
80 }
81
82 fn write_pipeline_unwind(&self, unwind: Option<&PipelineUnwind>) -> ProviderResult<()> {
84 match unwind {
85 Some(unwind) => self.write_metadata(
86 keys::PIPELINE_UNWIND,
87 serde_json::to_vec(unwind).map_err(ProviderError::other)?,
88 ),
89 None => self.delete_metadata(keys::PIPELINE_UNWIND),
90 }
91 }
92
93 fn write_storage_settings(&self, settings: StorageSettings) -> ProviderResult<()> {
98 self.write_metadata(
99 keys::STORAGE_SETTINGS,
100 serde_json::to_vec(&settings).map_err(ProviderError::other)?,
101 )
102 }
103
104 fn write_snap_attempt(&self, attempt: &SnapAttempt) -> ProviderResult<()> {
106 self.write_metadata(
107 keys::SNAP_ATTEMPT,
108 serde_json::to_vec(attempt).map_err(ProviderError::other)?,
109 )
110 }
111}
112
113pub trait StorageSettingsCache: Send {
115 fn cached_storage_settings(&self) -> StorageSettings;
117
118 fn set_storage_settings_cache(&self, settings: StorageSettings);
123}
124
125#[cfg(feature = "std")]
127pub trait StoragePath: Send {
128 fn storage_path(&self) -> std::path::PathBuf;
130}