From c05fa99cbd8f3ba9cb06392fcff0a16109ef1ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Florkiewicz?= Date: Wed, 20 Sep 2023 21:29:12 +0200 Subject: [PATCH] Yiannis review --- node/src/store.rs | 62 ++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/node/src/store.rs b/node/src/store.rs index 82065758..64706f2f 100644 --- a/node/src/store.rs +++ b/node/src/store.rs @@ -20,9 +20,9 @@ pub trait Store: Send + Sync + Debug { async fn has(&self, hash: &Hash) -> bool; async fn has_at(&self, height: u64) -> bool; - async fn append_single(&mut self, header: ExtendedHeader) -> Result<()>; + async fn append_single(&self, header: ExtendedHeader) -> Result<()>; - async fn append>(&mut self, headers: I) -> Result<()> + async fn append>(&self, headers: I) -> Result<()> where I: IntoIterator + Send, ::IntoIter: Send, @@ -39,33 +39,6 @@ pub trait Store: Send + Sync + Debug { } } -#[async_trait] -impl Store for InMemoryStore { - async fn get_by_hash(&self, hash: &Hash) -> Result { - self.get_by_hash(hash) - } - - async fn get_by_height(&self, height: u64) -> Result { - self.get_by_height(height) - } - - async fn height(&self) -> u64 { - self.get_head_height() - } - - async fn has(&self, hash: &Hash) -> bool { - self.exists_by_hash(hash) - } - - async fn has_at(&self, height: u64) -> bool { - self.exists_by_height(height) - } - - async fn append_single(&mut self, header: ExtendedHeader) -> Result<()> { - self.append_continuous(header) - } -} - #[derive(Debug)] pub struct InMemoryStore { headers: DashMap, @@ -75,14 +48,15 @@ pub struct InMemoryStore { #[derive(Error, Debug, PartialEq)] pub enum StoreError { - // TODO: error consolidation again #[error("Hash {0} already exists in store")] HashExists(Hash), + #[error("Height {0} already exists in store")] HeightExists(u64), #[error("Continuous append impossible")] NonContinuousAppend, + #[error("Failed to apply header {0}")] ContinuousAppendFailedAt(usize), @@ -91,6 +65,7 @@ pub enum StoreError { #[error("Store in inconsistent state, lost head")] LostStoreHead, + #[error("Store in inconsistent state; height->hash mapping exists, {0} missing")] LostHash(Hash), } @@ -202,6 +177,33 @@ impl InMemoryStore { } } +#[async_trait] +impl Store for InMemoryStore { + async fn get_by_hash(&self, hash: &Hash) -> Result { + self.get_by_hash(hash) + } + + async fn get_by_height(&self, height: u64) -> Result { + self.get_by_height(height) + } + + async fn height(&self) -> u64 { + self.get_head_height() + } + + async fn has(&self, hash: &Hash) -> bool { + self.exists_by_hash(hash) + } + + async fn has_at(&self, height: u64) -> bool { + self.exists_by_height(height) + } + + async fn append_single(&self, header: ExtendedHeader) -> Result<()> { + self.append_continuous(header) + } +} + impl Default for InMemoryStore { fn default() -> Self { Self::new()