use crate::database::{ database_description::{ DatabaseDescription, DatabaseMetadata, }, Database, Error as DatabaseError, }; use fuel_core_storage::{ blueprint::plain::Plain, codec::postcard::Postcard, structured_storage::TableWithBlueprint, Error as StorageError, Mappable, Result as StorageResult, StorageAsRef, StorageInspect, }; /// The table that stores all metadata about the database. pub struct MetadataTable(core::marker::PhantomData); impl Mappable for MetadataTable where Description: DatabaseDescription, { type Key = (); type OwnedKey = (); type Value = DatabaseMetadata; type OwnedValue = Self::Value; } impl TableWithBlueprint for MetadataTable where Description: DatabaseDescription, { type Blueprint = Plain; type Column = Description::Column; fn column() -> Self::Column { Description::metadata_column() } } impl Database where Description: DatabaseDescription, Self: StorageInspect, Error = StorageError>, { /// Ensures the version is correct. pub fn check_version(&self) -> StorageResult<()> { let Some(metadata) = self.storage::>().get(&())? else { return Ok(()); }; if metadata.version() != Description::version() { return Err(DatabaseError::InvalidDatabaseVersion { found: metadata.version(), expected: Description::version(), } .into()) } Ok(()) } pub fn latest_height(&self) -> StorageResult> { let metadata = self.storage::>().get(&())?; let metadata = metadata.map(|metadata| *metadata.height()); Ok(metadata) } }