diff --git a/src/querydb/prelude.rs b/src/querydb/prelude.rs index 21d31c2d..d8466f65 100644 --- a/src/querydb/prelude.rs +++ b/src/querydb/prelude.rs @@ -1,5 +1,5 @@ use pallas; -use redb::{MultimapTableDefinition, TableDefinition}; +use redb::{MultimapTableDefinition, StorageError, TableDefinition, TableError, TransactionError}; // Given a block, table "block" maps its hash to its CBOR representation pub type BlockKeyType<'a> = &'a [u8; 32]; @@ -56,3 +56,10 @@ pub enum ReDBError { TableError(redb::TableError), TransactionError(redb::TransactionError), } + +pub enum ReadError { + ChainTipNotFound, + TransactionError(TransactionError), + TableError(TableError), + StorageError(StorageError), +} diff --git a/src/querydb/store.rs b/src/querydb/store.rs index 1e7b1508..9548bf19 100644 --- a/src/querydb/store.rs +++ b/src/querydb/store.rs @@ -3,7 +3,9 @@ use pallas::{ crypto::hash::Hash, ledger::traverse::{MultiEraBlock, MultiEraOutput, MultiEraPolicyAssets, MultiEraTx}, }; -use redb::{Database, MultimapTable, ReadableTable, Table, WriteTransaction}; +use redb::{ + Database, MultimapTable, ReadOnlyTable, ReadTransaction, ReadableTable, Table, WriteTransaction, +}; use std::{ops::Deref, path::Path}; pub struct Store { @@ -106,8 +108,20 @@ impl Store { Ok(()) } - pub fn get_chain_tip(&self) -> &[u8] { - unimplemented!() + pub fn get_chain_tip(&self) -> Result, ReadError> { + let read_tx: ReadTransaction = self + .inner_store + .begin_read() + .map_err(ReadError::TransactionError)?; + let chain_tip_table: ReadOnlyTable = read_tx + .open_table(CHAIN_TIP_TABLE) + .map_err(ReadError::TableError)?; + let res = chain_tip_table + .last() + .map_err(ReadError::StorageError)? + .ok_or(ReadError::ChainTipNotFound) + .map(|entry| Vec::from(entry.1.value())); + res } pub fn get_chain_parameters(&self) {