From 1ee920b868183bf8e0e6adcfaafcef59ac559bb1 Mon Sep 17 00:00:00 2001 From: Stan Bondi Date: Wed, 6 Jul 2022 12:50:42 +0200 Subject: [PATCH] Add metadata impl for chaindb mock --- .../core/src/storage/chain/metadata_key.rs | 2 +- .../src/storage/metadata_backend_adapter.rs | 2 +- dan_layer/core/src/storage/mocks/chain_db.rs | 27 +++++++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/dan_layer/core/src/storage/chain/metadata_key.rs b/dan_layer/core/src/storage/chain/metadata_key.rs index 20c6f2b7b9..2bc40fba25 100644 --- a/dan_layer/core/src/storage/chain/metadata_key.rs +++ b/dan_layer/core/src/storage/chain/metadata_key.rs @@ -24,7 +24,7 @@ use std::fmt::{Display, Formatter}; use crate::storage::metadata_backend_adapter::AsKeyBytes; -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ChainDbMetadataKey { CheckpointNumber, } diff --git a/dan_layer/core/src/storage/metadata_backend_adapter.rs b/dan_layer/core/src/storage/metadata_backend_adapter.rs index 2aa744bf17..6d3999c93c 100644 --- a/dan_layer/core/src/storage/metadata_backend_adapter.rs +++ b/dan_layer/core/src/storage/metadata_backend_adapter.rs @@ -23,7 +23,7 @@ // TODO: probably want to use something like bors or consensus encoding use tari_utilities::message_format::MessageFormat; -use crate::storage::StorageError; +use crate::storage::AtomicDb; pub trait MetadataBackendAdapter: AtomicDb + Send + Sync + Clone { fn get_metadata( diff --git a/dan_layer/core/src/storage/mocks/chain_db.rs b/dan_layer/core/src/storage/mocks/chain_db.rs index a95f1f6102..b6d648afe7 100644 --- a/dan_layer/core/src/storage/mocks/chain_db.rs +++ b/dan_layer/core/src/storage/mocks/chain_db.rs @@ -208,26 +208,43 @@ impl MetadataBackendAdapter for MockChainDbBackupAdapter { fn get_metadata( &self, key: &ChainDbMetadataKey, - transaction: &Self::DbTransaction, + _transaction: &Self::DbTransaction, ) -> Result, Self::Error> { let lock = self.db.read()?; - Ok(lock.metadata.rows().find(|(k, _)| k == key)) + let v = lock + .metadata + .rows() + .find(|(k, _)| k == key) + .map(|(_, v)| v) + .map(|v| T::from_binary(v).unwrap()); + Ok(v) } fn set_metadata( &self, key: ChainDbMetadataKey, value: T, - transaction: &Self::DbTransaction, + _transaction: &Self::DbTransaction, ) -> Result<(), Self::Error> { let mut lock = self.db.write()?; - Ok(lock.metadata.rows().find(|(k, _)| k == key)) + let value = value.to_binary().unwrap(); + let id = lock.metadata.records().find(|(_, (k, _))| *k == key).map(|(id, _)| id); + match id { + Some(id) => { + lock.metadata.update(id, (key, value)); + }, + None => { + lock.metadata.insert((key, value)); + }, + } + + Ok(()) } fn metadata_key_exists( &self, _key: &ChainDbMetadataKey, - transaction: &Self::DbTransaction, + _transaction: &Self::DbTransaction, ) -> Result { todo!() }