Skip to content

Commit 34d6514

Browse files
committed
misc(ledger): log solution ID when post-ratify fails
1 parent 6a2b98b commit 34d6514

3 files changed

Lines changed: 32 additions & 23 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ledger/puzzle/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ features = [ "account", "network", "types" ]
4545
[dependencies.snarkvm-algorithms]
4646
workspace = true
4747

48+
[dependencies.snarkvm-utilities]
49+
workspace = true
50+
4851
[dependencies.aleo-std]
4952
workspace = true
5053

ledger/puzzle/src/lib.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ use console::{
5050
},
5151
types::U64,
5252
};
53+
use snarkvm_utilities::ensure_equals;
5354

5455
use aleo_std::prelude::*;
56+
use anyhow::Context;
5557
use core::num::NonZeroUsize;
5658
use indexmap::IndexMap;
5759
#[cfg(feature = "locktick")]
@@ -128,10 +130,18 @@ impl<N: Network> Puzzle<N> {
128130

129131
/// Returns the proof target given the solution.
130132
pub fn get_proof_target(&self, solution: &Solution<N>) -> Result<u64> {
133+
let solution_id = solution.id();
134+
131135
// Calculate the proof target.
132-
let proof_target = self.get_proof_target_unchecked(solution)?;
136+
let proof_target = self
137+
.get_proof_target_unchecked(solution)
138+
.with_context(|| format!("Failed to get proof target for solution {solution_id}"))?;
133139
// Ensure the proof target matches the expected proof target.
134-
ensure!(solution.target() == proof_target, "The proof target does not match the expected proof target");
140+
ensure_equals!(
141+
solution.target(),
142+
proof_target,
143+
"The proof target for solution {solution_id} does not match the expected proof target"
144+
);
135145
// Return the proof target.
136146
Ok(proof_target)
137147
}
@@ -176,9 +186,10 @@ impl<N: Network> Puzzle<N> {
176186
// If the proof target is in the cache, then store it.
177187
Some(proof_target) => {
178188
// Ensure that the proof target matches the expected proof target.
179-
ensure!(
180-
solution.target() == *proof_target,
181-
"The proof target does not match the cached proof target"
189+
ensure_equals!(
190+
solution.target(),
191+
*proof_target,
192+
"The proof target for solution {id} does not match the cached proof target"
182193
);
183194
targets[i] = *proof_target
184195
}
@@ -199,9 +210,10 @@ impl<N: Network> Puzzle<N> {
199210
// Get the proof target.
200211
let proof_target = Self::leaves_to_proof_target(leaves)?;
201212
// Ensure that the proof target matches the expected proof target.
202-
ensure!(
203-
solution.target() == proof_target,
204-
"The proof target does not match the computed proof target"
213+
ensure_equals!(
214+
solution.target(),
215+
proof_target,
216+
"The proof target for solution {solution_id} does not match the computed proof target"
205217
);
206218
// Insert the proof target into the cache.
207219
self.proof_target_cache.write().put(*solution_id, proof_target);
@@ -258,12 +270,7 @@ impl<N: Network> Puzzle<N> {
258270
expected_proof_target: u64,
259271
) -> Result<()> {
260272
// Ensure the epoch hash matches.
261-
if solution.epoch_hash() != expected_epoch_hash {
262-
bail!(
263-
"Solution does not match the expected epoch hash (found '{}', expected '{expected_epoch_hash}')",
264-
solution.epoch_hash()
265-
)
266-
}
273+
ensure_equals!(solution.epoch_hash(), expected_epoch_hash, "Solution does not match the expected epoch hash");
267274
// Ensure the solution is greater than or equal to the expected proof target.
268275
let proof_target = self.get_proof_target(solution)?;
269276
if proof_target < expected_proof_target {
@@ -281,12 +288,8 @@ impl<N: Network> Puzzle<N> {
281288
expected_proof_target: u64,
282289
) -> Result<()> {
283290
// Ensure the epoch hash matches.
284-
if solution.epoch_hash() != expected_epoch_hash {
285-
bail!(
286-
"Solution does not match the expected epoch hash (found '{}', expected '{expected_epoch_hash}')",
287-
solution.epoch_hash()
288-
)
289-
}
291+
ensure_equals!(solution.epoch_hash(), expected_epoch_hash, "Solution does not match the expected epoch hash");
292+
290293
// Calculate the proof target of the solution.
291294
let proof_target = self.get_proof_target_unchecked(solution)?;
292295

@@ -323,9 +326,11 @@ impl<N: Network> Puzzle<N> {
323326

324327
// Ensure the epoch hash matches.
325328
cfg_iter!(solutions).try_for_each(|(solution_id, solution)| {
326-
if solution.epoch_hash() != expected_epoch_hash {
327-
bail!("Solution '{solution_id}' did not match the expected epoch hash (found '{}', expected '{expected_epoch_hash}')", solution.epoch_hash())
328-
}
329+
ensure_equals!(
330+
solution.epoch_hash(),
331+
expected_epoch_hash,
332+
"Solution {solution_id} did not match the expected epoch hash"
333+
);
329334
Ok(())
330335
})?;
331336
lap!(timer, "Verify each epoch hash matches");

0 commit comments

Comments
 (0)