use super::*;
impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
pub fn get_state_root(&self, block_height: u32) -> Result<Option<N::StateRoot>> {
self.vm.block_store().get_state_root(block_height)
}
pub fn get_state_path_for_commitment(&self, commitment: &Field<N>) -> Result<StatePath<N>> {
self.vm.block_store().get_state_path_for_commitment(commitment)
}
pub fn get_epoch_challenge(&self, block_height: u32) -> Result<EpochChallenge<N>> {
let epoch_number = block_height / N::NUM_BLOCKS_PER_EPOCH;
let epoch_starting_height = epoch_number * N::NUM_BLOCKS_PER_EPOCH;
let epoch_block_hash = self.get_previous_hash(epoch_starting_height)?;
EpochChallenge::new(epoch_number, epoch_block_hash, N::COINBASE_PUZZLE_DEGREE)
}
pub fn get_block(&self, height: u32) -> Result<Block<N>> {
if height == 0 {
return Ok(self.genesis.clone());
}
let block_hash = match self.vm.block_store().get_block_hash(height)? {
Some(block_hash) => block_hash,
None => bail!("Block {height} does not exist in storage"),
};
match self.vm.block_store().get_block(&block_hash)? {
Some(block) => Ok(block),
None => bail!("Block {height} ('{block_hash}') does not exist in storage"),
}
}
pub fn get_blocks(&self, heights: Range<u32>) -> Result<Vec<Block<N>>> {
cfg_into_iter!(heights).map(|height| self.get_block(height)).collect()
}
pub fn get_block_by_hash(&self, block_hash: &N::BlockHash) -> Result<Block<N>> {
match self.vm.block_store().get_block(block_hash)? {
Some(block) => Ok(block),
None => bail!("Block '{block_hash}' does not exist in storage"),
}
}
pub fn get_height(&self, block_hash: &N::BlockHash) -> Result<u32> {
match self.vm.block_store().get_block_height(block_hash)? {
Some(height) => Ok(height),
None => bail!("Missing block height for block '{block_hash}'"),
}
}
pub fn get_hash(&self, height: u32) -> Result<N::BlockHash> {
if height == 0 {
return Ok(self.genesis.hash());
}
match self.vm.block_store().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> {
if height == 0 {
return Ok(N::BlockHash::default());
}
match self.vm.block_store().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>> {
if height == 0 {
return Ok(*self.genesis.header());
}
let block_hash = match self.vm.block_store().get_block_hash(height)? {
Some(block_hash) => block_hash,
None => bail!("Block {height} does not exist in storage"),
};
match self.vm.block_store().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>> {
if height == 0 {
return Ok(self.genesis.transactions().clone());
}
let block_hash = match self.vm.block_store().get_block_hash(height)? {
Some(block_hash) => block_hash,
None => bail!("Block {height} does not exist in storage"),
};
match self.vm.block_store().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.vm.transaction_store().get_transaction(&transaction_id)? {
Some(transaction) => Ok(transaction),
None => bail!("Missing transaction for ID {transaction_id}"),
}
}
pub fn get_confirmed_transaction(&self, transaction_id: N::TransactionID) -> Result<ConfirmedTransaction<N>> {
match self.vm.block_store().get_confirmed_transaction(&transaction_id)? {
Some(confirmed_transaction) => Ok(confirmed_transaction),
None => bail!("Missing confirmed transaction for ID {transaction_id}"),
}
}
pub fn get_program(&self, program_id: ProgramID<N>) -> Result<Program<N>> {
match self.vm.transaction_store().get_program(&program_id)? {
Some(program) => Ok(program),
None => bail!("Missing program for ID {program_id}"),
}
}
pub fn get_coinbase(&self, height: u32) -> Result<Option<CoinbaseSolution<N>>> {
if height == 0 {
return Ok(self.genesis.coinbase().cloned());
}
let block_hash = match self.vm.block_store().get_block_hash(height)? {
Some(block_hash) => block_hash,
None => bail!("Block {height} does not exist in storage"),
};
self.vm.block_store().get_block_coinbase(&block_hash)
}
pub fn get_signature(&self, height: u32) -> Result<Signature<N>> {
if height == 0 {
return Ok(*self.genesis.signature());
}
let block_hash = match self.vm.block_store().get_block_hash(height)? {
Some(block_hash) => block_hash,
None => bail!("Block {height} does not exist in storage"),
};
match self.vm.block_store().get_block_signature(&block_hash)? {
Some(signature) => Ok(signature),
None => bail!("Missing signature for block {height}"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::test_helpers::CurrentLedger;
use console::network::Testnet3;
type CurrentNetwork = Testnet3;
#[test]
fn test_get_block() {
let genesis = Block::from_bytes_le(CurrentNetwork::genesis_bytes()).unwrap();
let ledger = CurrentLedger::load(genesis.clone(), None).unwrap();
let candidate = ledger.get_block(0).unwrap();
assert_eq!(genesis, candidate);
}
}