snarkvm_ledger_debug/
find.rs1use super::*;
16
17impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
18 pub fn find_block_height_from_state_root(&self, state_root: N::StateRoot) -> Result<Option<u32>> {
20 self.vm.block_store().find_block_height_from_state_root(state_root)
21 }
22
23 pub fn find_block_hash(&self, transaction_id: &N::TransactionID) -> Result<Option<N::BlockHash>> {
25 self.vm.block_store().find_block_hash(transaction_id)
26 }
27
28 pub fn find_block_height_from_puzzle_commitment(
30 &self,
31 puzzle_commitment: &PuzzleCommitment<N>,
32 ) -> Result<Option<u32>> {
33 self.vm.block_store().find_block_height_from_puzzle_commitment(puzzle_commitment)
34 }
35
36 pub fn find_transaction_id_from_program_id(&self, program_id: &ProgramID<N>) -> Result<Option<N::TransactionID>> {
38 self.vm.transaction_store().find_transaction_id_from_program_id(program_id)
39 }
40
41 pub fn find_transaction_id_from_transition_id(
43 &self,
44 transition_id: &N::TransitionID,
45 ) -> Result<Option<N::TransactionID>> {
46 self.vm.transaction_store().find_transaction_id_from_transition_id(transition_id)
47 }
48
49 pub fn find_transition_id(&self, id: &Field<N>) -> Result<N::TransitionID> {
51 self.vm.transition_store().find_transition_id(id)
52 }
53
54 pub fn find_record_ciphertexts<'a>(
56 &'a self,
57 view_key: &'a ViewKey<N>,
58 filter: RecordsFilter<N>,
59 ) -> Result<impl '_ + Iterator<Item = (Field<N>, Cow<'_, Record<N, Ciphertext<N>>>)>> {
60 let address_x_coordinate = view_key.to_address().to_x_coordinate();
62 let sk_tag = match GraphKey::try_from(view_key) {
64 Ok(graph_key) => graph_key.sk_tag(),
65 Err(e) => bail!("Failed to derive the graph key from the view key: {e}"),
66 };
67
68 Ok(self.records().flat_map(move |cow| {
69 let (commitment, record) = match cow {
71 (Cow::Borrowed(commitment), record) => (*commitment, record),
72 (Cow::Owned(commitment), record) => (commitment, record),
73 };
74
75 let commitment = match filter {
77 RecordsFilter::All => Ok(Some(commitment)),
78 RecordsFilter::Spent => Record::<N, Plaintext<N>>::tag(sk_tag, commitment).and_then(|tag| {
79 self.contains_tag(&tag).map(|is_spent| match is_spent {
81 true => Some(commitment),
82 false => None,
83 })
84 }),
85 RecordsFilter::Unspent => Record::<N, Plaintext<N>>::tag(sk_tag, commitment).and_then(|tag| {
86 self.contains_tag(&tag).map(|is_spent| match is_spent {
88 true => None,
89 false => Some(commitment),
90 })
91 }),
92 RecordsFilter::SlowSpent(private_key) => {
93 Record::<N, Plaintext<N>>::serial_number(private_key, commitment).and_then(|serial_number| {
94 self.contains_serial_number(&serial_number).map(|is_spent| match is_spent {
96 true => Some(commitment),
97 false => None,
98 })
99 })
100 }
101 RecordsFilter::SlowUnspent(private_key) => {
102 Record::<N, Plaintext<N>>::serial_number(private_key, commitment).and_then(|serial_number| {
103 self.contains_serial_number(&serial_number).map(|is_spent| match is_spent {
105 true => None,
106 false => Some(commitment),
107 })
108 })
109 }
110 };
111
112 match commitment {
113 Ok(Some(commitment)) => {
114 match record.is_owner_with_address_x_coordinate(view_key, &address_x_coordinate) {
115 true => Some((commitment, record)),
116 false => None,
117 }
118 }
119 Ok(None) => None,
120 Err(e) => {
121 warn!("Failed to process 'find_record_ciphertexts({:?})': {e}", filter);
122 None
123 }
124 }
125 }))
126 }
127
128 pub fn find_records<'a>(
130 &'a self,
131 view_key: &'a ViewKey<N>,
132 filter: RecordsFilter<N>,
133 ) -> Result<impl '_ + Iterator<Item = (Field<N>, Record<N, Plaintext<N>>)>> {
134 self.find_record_ciphertexts(view_key, filter).map(|iter| {
135 iter.flat_map(|(commitment, record)| match record.decrypt(view_key) {
136 Ok(record) => Some((commitment, record)),
137 Err(e) => {
138 warn!("Failed to decrypt the record: {e}");
139 None
140 }
141 })
142 })
143 }
144}