use super::*;
impl<N: Network, B: BlockStorage<N>, P: ProgramStorage<N>> Ledger<N, B, P> {
pub fn get_block(&self, height: u32) -> Result<Block<N>> {
let block_hash = match self.blocks.get_block_hash(height)? {
Some(block_hash) => block_hash,
None => bail!("Block {height} does not exist in storage"),
};
match self.blocks.get_block(&block_hash)? {
Some(block) => Ok(block),
None => bail!("Block {height} ('{block_hash}') does not exist in storage"),
}
}
pub fn get_hash(&self, height: u32) -> Result<N::BlockHash> {
match self.blocks.get_block_hash(height)? {
Some(block_hash) => Ok(block_hash),
None => bail!("Missing block hash for block {height}"),
}
}
pub fn get_previous_hash(&self, height: u32) -> Result<N::BlockHash> {
match self.blocks.get_previous_block_hash(height)? {
Some(previous_hash) => Ok(previous_hash),
None => bail!("Missing previous block hash for block {height}"),
}
}
pub fn get_header(&self, height: u32) -> Result<Header<N>> {
let block_hash = match self.blocks.get_block_hash(height)? {
Some(block_hash) => block_hash,
None => bail!("Block {height} does not exist in storage"),
};
match self.blocks.get_block_header(&block_hash)? {
Some(header) => Ok(header),
None => bail!("Missing block header for block {height}"),
}
}
pub fn get_transactions(&self, height: u32) -> Result<Transactions<N>> {
let block_hash = match self.blocks.get_block_hash(height)? {
Some(block_hash) => block_hash,
None => bail!("Block {height} does not exist in storage"),
};
match self.blocks.get_block_transactions(&block_hash)? {
Some(transactions) => Ok(transactions),
None => bail!("Missing block transactions for block {height}"),
}
}
pub fn get_transaction(&self, transaction_id: N::TransactionID) -> Result<Transaction<N>> {
match self.transactions.get_transaction(&transaction_id)? {
Some(transaction) => Ok(transaction),
None => bail!("Missing transaction for id {transaction_id}"),
}
}
pub fn get_signature(&self, height: u32) -> Result<Signature<N>> {
let block_hash = match self.blocks.get_block_hash(height)? {
Some(block_hash) => block_hash,
None => bail!("Block {height} does not exist in storage"),
};
match self.blocks.get_block_signature(&block_hash)? {
Some(signature) => Ok(signature),
None => bail!("Missing signature for block {height}"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ledger::test_helpers::CurrentLedger;
#[test]
fn test_get_block() {
let genesis = Block::from_bytes_le(GenesisBytes::load_bytes()).unwrap();
let ledger = CurrentLedger::new().unwrap();
let candidate = ledger.get_block(0).unwrap();
assert_eq!(genesis, candidate);
}
}