Skip to content

Commit

Permalink
feat(querydb): get_chain_tip endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicoLeberle committed Mar 25, 2024
1 parent 0bf1bb9 commit 6d00ff3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/querydb/prelude.rs
Original file line number Diff line number Diff line change
@@ -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];
Expand Down Expand Up @@ -56,3 +56,10 @@ pub enum ReDBError {
TableError(redb::TableError),
TransactionError(redb::TransactionError),
}

pub enum ReadError {
ChainTipNotFound,
TransactionError(TransactionError),
TableError(TableError),
StorageError(StorageError),
}
20 changes: 17 additions & 3 deletions src/querydb/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -106,8 +108,20 @@ impl Store {
Ok(())
}

pub fn get_chain_tip(&self) -> &[u8] {
unimplemented!()
pub fn get_chain_tip(&self) -> Result<Vec<u8>, ReadError> {
let read_tx: ReadTransaction = self
.inner_store
.begin_read()
.map_err(ReadError::TransactionError)?;
let chain_tip_table: ReadOnlyTable<ChainTipKeyType, ChainTipValueType> = 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) {
Expand Down

0 comments on commit 6d00ff3

Please sign in to comment.