Skip to main content

snarkvm_ledger_block/solutions/
mod.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16mod bytes;
17mod merkle;
18mod serialize;
19mod string;
20
21use console::{
22    network::{error, prelude::*},
23    types::Field,
24};
25use snarkvm_ledger_committee::Committee;
26use snarkvm_ledger_narwhal_batch_header::BatchHeader;
27use snarkvm_ledger_puzzle::{PuzzleSolutions, SolutionID};
28
29#[derive(Clone, Eq, PartialEq)]
30pub struct Solutions<N: Network> {
31    /// The prover solutions for the puzzle.
32    solutions: Option<PuzzleSolutions<N>>,
33}
34
35impl<N: Network> Solutions<N> {
36    /// The maximum number of aborted solutions allowed in a block.
37    pub fn max_aborted_solutions() -> Result<usize> {
38        Ok(BatchHeader::<N>::MAX_TRANSMISSIONS_PER_BATCH
39            * BatchHeader::<N>::MAX_GC_ROUNDS
40            * Committee::<N>::max_committee_size()? as usize)
41    }
42}
43
44impl<N: Network> From<Option<PuzzleSolutions<N>>> for Solutions<N> {
45    /// Initializes a new instance of the solutions.
46    fn from(solutions: Option<PuzzleSolutions<N>>) -> Self {
47        // Return the solutions.
48        Self { solutions }
49    }
50}
51
52impl<N: Network> Solutions<N> {
53    /// Initializes a new instance of the solutions.
54    pub fn new(solutions: PuzzleSolutions<N>) -> Result<Self> {
55        // Return the solutions.
56        Ok(Self { solutions: Some(solutions) })
57    }
58
59    /// Returns `true` if the solutions are empty.
60    pub fn is_empty(&self) -> bool {
61        self.solutions.is_none()
62    }
63
64    /// Returns the number of solutions.
65    pub fn len(&self) -> usize {
66        match &self.solutions {
67            Some(solutions) => solutions.len(),
68            None => 0,
69        }
70    }
71}
72
73impl<N: Network> Solutions<N> {
74    /// Returns an iterator over the solution IDs.
75    pub fn solution_ids<'a>(&'a self) -> Box<dyn Iterator<Item = &'a SolutionID<N>> + 'a> {
76        match &self.solutions {
77            Some(solutions) => Box::new(solutions.keys()),
78            None => Box::new(std::iter::empty::<&SolutionID<N>>()),
79        }
80    }
81}
82
83impl<N: Network> Deref for Solutions<N> {
84    type Target = Option<PuzzleSolutions<N>>;
85
86    /// Returns the solutions.
87    fn deref(&self) -> &Self::Target {
88        &self.solutions
89    }
90}