From f56d7231afd8426937660115d67f869e7091d75e Mon Sep 17 00:00:00 2001 From: Someone Else Date: Mon, 22 May 2023 18:41:55 +0200 Subject: [PATCH] finished read block --- database/src/encoding/mod.rs | 2 +- database/src/interface/default.rs | 57 ++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/database/src/encoding/mod.rs b/database/src/encoding/mod.rs index fe4b306..1cf9f88 100644 --- a/database/src/encoding/mod.rs +++ b/database/src/encoding/mod.rs @@ -60,7 +60,7 @@ pub enum Value { /// variant of Value you should receive. But be careful on your code, or it might crash. impl<'a, T: Table> Value { - pub fn as_type(&'a self) -> &'a ::Value { + pub fn as_type(self) -> ::Value { assert!(matches!(self, Value::Type(_))); // Hint for the compiler to check the boundaries if let Value::Type(value) = self { value diff --git a/database/src/interface/default.rs b/database/src/interface/default.rs index 52381ce..bf45f56 100644 --- a/database/src/interface/default.rs +++ b/database/src/interface/default.rs @@ -3,7 +3,7 @@ use monero::{Hash, BlockHeader, Block}; use monero::blockdata::transaction::KeyImage; -use crate::database::transaction::DupCursor; +use crate::database::transaction::{DupCursor, Cursor}; use crate::{encoding::Value, database::{Interface, Database, transaction::Transaction}, error::DBException, interface::{ReadInterface, WriteInterface}, table, types::{AltBlock, TransactionPruned}}; impl<'thread, D: Database<'thread>> ReadInterface<'thread> for Interface<'thread, D> { @@ -43,7 +43,7 @@ impl<'thread, D: Database<'thread>> ReadInterface<'thread> for Interface<'thread cursor_blockhash .get_dup::(&(), hash)? .ok_or(DBException::NotFound(format!("Failed to find height of block: {}", hash))) - .map(|res| *res.as_type()) + .map(|res| res.as_type()) } fn get_block_weight(&'thread self, height: &u64) -> Result { @@ -132,32 +132,73 @@ impl<'thread, D: Database<'thread>> ReadInterface<'thread> for Interface<'thread fn get_block(&'thread self, hash: &Hash) -> Result { let ro_tx = self.db.tx().map_err(Into::into)?; - todo!() + let mut cursor_blockhash = ro_tx.cursor_dup::()?; + + let blk_height = cursor_blockhash + .get_dup::(&(), hash)? + .ok_or(DBException::NotFound(format!("Can't height of block: {}", hash))) + .map(|res| res.as_type())?; + + ro_tx + .get::(&blk_height)? + .ok_or(DBException::NotFound(format!("Can't find block at height: {}", blk_height))) + .map(|res| res.as_type().0) } fn get_block_from_height(&'thread self, height: &u64) -> Result { let ro_tx = self.db.tx().map_err(Into::into)?; - todo!() + + ro_tx + .get::(height)? + .ok_or(DBException::NotFound(format!("Can't find block at height: {}", height))) + .map(|res| res.as_type().0) } fn get_block_header(&'thread self, hash: &Hash) -> Result { let ro_tx = self.db.tx().map_err(Into::into)?; - todo!() + let mut cursor_blockhash = ro_tx.cursor_dup::()?; + + let blk_height = cursor_blockhash + .get_dup::(&(), hash)? + .ok_or(DBException::NotFound(format!("Can't height of block: {}", hash))) + .map(|res| res.as_type())?; + + ro_tx + .get::(&blk_height)? + .ok_or(DBException::NotFound(format!("Can't find block at height: {}", &blk_height))) + .map(|res| res.as_type().0.header) } fn get_block_header_from_height(&'thread self, height: &u64) -> Result { let ro_tx = self.db.tx().map_err(Into::into)?; - todo!() + + ro_tx + .get::(height)? + .ok_or(DBException::NotFound(format!("Can't find block at height: {}", height))) + .map(|res| res.as_type().0.header) } fn get_top_block(&'thread self) -> Result { let ro_tx = self.db.tx().map_err(Into::into)?; - todo!() + + let height = self.height()?; + + ro_tx + .get::(&height)? + .ok_or(DBException::NotFound(format!("Can't find block at height: {}", height))) + .map(|res| res.as_type().0) } fn get_top_block_hash(&'thread self) -> Result { let ro_tx = self.db.tx().map_err(Into::into)?; - todo!() + let mut cursor_blockmetadata = ro_tx.cursor_dup::()?; + + let height = self.height()?; + let metadata = cursor_blockmetadata + .get_dup::(&(), &height)? + .ok_or(DBException::NotFound(format!("Failed to find block's metadata at height : {}", height)))?; + + Ok(metadata.as_type().block_hash) } // ------------------------------| Transactions |-----------------------------