/ ledger / block / src / solutions / mod.rs
mod.rs
 1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
 2  // This file is part of the alphavm 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  
16  mod bytes;
17  mod merkle;
18  mod serialize;
19  mod string;
20  
21  use alphavm_ledger_committee::Committee;
22  use alphavm_ledger_narwhal_batch_header::BatchHeader;
23  use alphavm_ledger_puzzle::{PuzzleSolutions, SolutionID};
24  use console::{
25      network::{error, prelude::*},
26      types::Field,
27  };
28  
29  #[derive(Clone, Eq, PartialEq)]
30  pub struct Solutions<N: Network> {
31      /// The prover solutions for the puzzle.
32      solutions: Option<PuzzleSolutions<N>>,
33  }
34  
35  impl<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  
44  impl<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  
52  impl<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  
73  impl<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  
83  impl<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  }