1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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);
}
}