snarkvm_ledger_debug/
get.rs1use super::*;
16
17impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
18 pub fn get_committee(&self, block_height: u32) -> Result<Option<Committee<N>>> {
20 self.vm.finalize_store().committee_store().get_committee(block_height)
21 }
22
23 pub fn get_committee_for_round(&self, round: u64) -> Result<Option<Committee<N>>> {
25 self.vm.finalize_store().committee_store().get_committee_for_round(round)
26 }
27
28 pub fn get_state_root(&self, block_height: u32) -> Result<Option<N::StateRoot>> {
30 self.vm.block_store().get_state_root(block_height)
31 }
32
33 pub fn get_state_path_for_commitment(&self, commitment: &Field<N>) -> Result<StatePath<N>> {
35 self.vm.block_store().get_state_path_for_commitment(commitment)
36 }
37
38 pub fn get_epoch_challenge(&self, block_height: u32) -> Result<EpochChallenge<N>> {
40 let epoch_number = block_height / N::NUM_BLOCKS_PER_EPOCH;
42 let epoch_starting_height = epoch_number * N::NUM_BLOCKS_PER_EPOCH;
44 let epoch_block_hash = self.get_previous_hash(epoch_starting_height)?;
46 EpochChallenge::new(epoch_number, epoch_block_hash, N::COINBASE_PUZZLE_DEGREE)
48 }
49
50 pub fn get_block(&self, height: u32) -> Result<Block<N>> {
52 if height == 0 {
54 return Ok(self.genesis_block.clone());
55 }
56 let block_hash = match self.vm.block_store().get_block_hash(height)? {
58 Some(block_hash) => block_hash,
59 None => bail!("Block {height} does not exist in storage"),
60 };
61 match self.vm.block_store().get_block(&block_hash)? {
63 Some(block) => Ok(block),
64 None => bail!("Block {height} ('{block_hash}') does not exist in storage"),
65 }
66 }
67
68 pub fn get_blocks(&self, heights: Range<u32>) -> Result<Vec<Block<N>>> {
71 cfg_into_iter!(heights).map(|height| self.get_block(height)).collect()
72 }
73
74 pub fn get_block_by_hash(&self, block_hash: &N::BlockHash) -> Result<Block<N>> {
76 match self.vm.block_store().get_block(block_hash)? {
78 Some(block) => Ok(block),
79 None => bail!("Block '{block_hash}' does not exist in storage"),
80 }
81 }
82
83 pub fn get_height(&self, block_hash: &N::BlockHash) -> Result<u32> {
85 match self.vm.block_store().get_block_height(block_hash)? {
86 Some(height) => Ok(height),
87 None => bail!("Missing block height for block '{block_hash}'"),
88 }
89 }
90
91 pub fn get_hash(&self, height: u32) -> Result<N::BlockHash> {
93 if height == 0 {
95 return Ok(self.genesis_block.hash());
96 }
97 match self.vm.block_store().get_block_hash(height)? {
98 Some(block_hash) => Ok(block_hash),
99 None => bail!("Missing block hash for block {height}"),
100 }
101 }
102
103 pub fn get_previous_hash(&self, height: u32) -> Result<N::BlockHash> {
105 if height == 0 {
107 return Ok(N::BlockHash::default());
108 }
109 match self.vm.block_store().get_previous_block_hash(height)? {
110 Some(previous_hash) => Ok(previous_hash),
111 None => bail!("Missing previous block hash for block {height}"),
112 }
113 }
114
115 pub fn get_header(&self, height: u32) -> Result<Header<N>> {
117 if height == 0 {
119 return Ok(*self.genesis_block.header());
120 }
121 let block_hash = match self.vm.block_store().get_block_hash(height)? {
123 Some(block_hash) => block_hash,
124 None => bail!("Block {height} does not exist in storage"),
125 };
126 match self.vm.block_store().get_block_header(&block_hash)? {
128 Some(header) => Ok(header),
129 None => bail!("Missing block header for block {height}"),
130 }
131 }
132
133 pub fn get_transactions(&self, height: u32) -> Result<Transactions<N>> {
135 if height == 0 {
137 return Ok(self.genesis_block.transactions().clone());
138 }
139 let Some(block_hash) = self.vm.block_store().get_block_hash(height)? else {
141 bail!("Block {height} does not exist in storage");
142 };
143 match self.vm.block_store().get_block_transactions(&block_hash)? {
145 Some(transactions) => Ok(transactions),
146 None => bail!("Missing block transactions for block {height}"),
147 }
148 }
149
150 pub fn get_aborted_transaction_ids(&self, height: u32) -> Result<Vec<N::TransactionID>> {
152 if height == 0 {
154 return Ok(self.genesis_block.aborted_transaction_ids().clone());
155 }
156 let Some(block_hash) = self.vm.block_store().get_block_hash(height)? else {
158 bail!("Block {height} does not exist in storage");
159 };
160 match self.vm.block_store().get_block_aborted_transaction_ids(&block_hash)? {
162 Some(aborted_transaction_ids) => Ok(aborted_transaction_ids),
163 None => bail!("Missing aborted transaction IDs for block {height}"),
164 }
165 }
166
167 pub fn get_transaction(&self, transaction_id: N::TransactionID) -> Result<Transaction<N>> {
169 match self.vm.block_store().get_transaction(&transaction_id)? {
171 Some(transaction) => Ok(transaction),
172 None => bail!("Missing transaction for ID {transaction_id}"),
173 }
174 }
175
176 pub fn get_confirmed_transaction(&self, transaction_id: N::TransactionID) -> Result<ConfirmedTransaction<N>> {
178 match self.vm.block_store().get_confirmed_transaction(&transaction_id)? {
180 Some(confirmed_transaction) => Ok(confirmed_transaction),
181 None => bail!("Missing confirmed transaction for ID {transaction_id}"),
182 }
183 }
184
185 pub fn get_unconfirmed_transaction(&self, transaction_id: &N::TransactionID) -> Result<Transaction<N>> {
187 match self.vm.block_store().get_unconfirmed_transaction(transaction_id)? {
189 Some(unconfirmed_transaction) => Ok(unconfirmed_transaction),
190 None => bail!("Missing unconfirmed transaction for ID {transaction_id}"),
191 }
192 }
193
194 pub fn get_program(&self, program_id: ProgramID<N>) -> Result<Program<N>> {
196 match self.vm.block_store().get_program(&program_id)? {
197 Some(program) => Ok(program),
198 None => bail!("Missing program for ID {program_id}"),
199 }
200 }
201
202 pub fn get_solutions(&self, height: u32) -> Result<Option<CoinbaseSolution<N>>> {
204 if height == 0 {
206 return Ok(self.genesis_block.solutions().cloned());
207 }
208 let block_hash = match self.vm.block_store().get_block_hash(height)? {
210 Some(block_hash) => block_hash,
211 None => bail!("Block {height} does not exist in storage"),
212 };
213 self.vm.block_store().get_block_solutions(&block_hash)
215 }
216
217 pub fn get_solution(&self, solution_id: &PuzzleCommitment<N>) -> Result<ProverSolution<N>> {
219 self.vm.block_store().get_solution(solution_id)
220 }
221
222 pub fn get_authority(&self, height: u32) -> Result<Authority<N>> {
224 if height == 0 {
226 return Ok(self.genesis_block.authority().clone());
227 }
228 let block_hash = match self.vm.block_store().get_block_hash(height)? {
230 Some(block_hash) => block_hash,
231 None => bail!("Block {height} does not exist in storage"),
232 };
233 match self.vm.block_store().get_block_authority(&block_hash)? {
235 Some(authority) => Ok(authority),
236 None => bail!("Missing authority for block {height}"),
237 }
238 }
239
240 pub fn get_batch_certificate(&self, certificate_id: &Field<N>) -> Result<Option<BatchCertificate<N>>> {
242 self.vm.block_store().get_batch_certificate(certificate_id)
243 }
244}
245
246#[cfg(test)]
247mod tests {
248 use super::*;
249 use crate::test_helpers::CurrentLedger;
250 use console::network::Testnet3;
251
252 type CurrentNetwork = Testnet3;
253
254 #[test]
255 fn test_get_block() {
256 let genesis = Block::from_bytes_le(CurrentNetwork::genesis_bytes()).unwrap();
258
259 let ledger = CurrentLedger::load(genesis.clone(), None).unwrap();
261 let candidate = ledger.get_block(0).unwrap();
263 assert_eq!(genesis, candidate);
265 }
266}