/ node / router / messages / src / unconfirmed_solution.rs
unconfirmed_solution.rs
  1  // Copyright (c) 2025 ADnet Contributors
  2  // This file is part of the AlphaOS 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  use super::*;
 17  
 18  use alphavm::{
 19      ledger::narwhal::Data,
 20      prelude::{FromBytes, ToBytes},
 21  };
 22  
 23  use std::borrow::Cow;
 24  
 25  #[derive(Clone, Debug, PartialEq, Eq)]
 26  pub struct UnconfirmedSolution<N: Network> {
 27      pub solution_id: SolutionID<N>,
 28      pub solution: Data<Solution<N>>,
 29  }
 30  
 31  impl<N: Network> MessageTrait for UnconfirmedSolution<N> {
 32      /// Returns the message name.
 33      #[inline]
 34      fn name(&self) -> Cow<'static, str> {
 35          "UnconfirmedSolution".into()
 36      }
 37  }
 38  
 39  impl<N: Network> ToBytes for UnconfirmedSolution<N> {
 40      fn write_le<W: io::Write>(&self, mut writer: W) -> io::Result<()> {
 41          self.solution_id.write_le(&mut writer)?;
 42          self.solution.write_le(&mut writer)
 43      }
 44  }
 45  
 46  impl<N: Network> FromBytes for UnconfirmedSolution<N> {
 47      fn read_le<R: io::Read>(mut reader: R) -> io::Result<Self> {
 48          Ok(Self { solution_id: SolutionID::read_le(&mut reader)?, solution: Data::read_le(reader)? })
 49      }
 50  }
 51  
 52  #[cfg(test)]
 53  pub mod prop_tests {
 54      use crate::{Solution, SolutionID, UnconfirmedSolution};
 55      use alphavm::{
 56          ledger::{narwhal::Data, puzzle::PartialSolution},
 57          prelude::{Address, FromBytes, PrivateKey, Rng, TestRng, ToBytes},
 58      };
 59  
 60      use bytes::{Buf, BufMut, BytesMut};
 61      use proptest::prelude::{BoxedStrategy, Strategy, any};
 62      use test_strategy::proptest;
 63  
 64      type CurrentNetwork = alphavm::prelude::MainnetV0;
 65  
 66      pub fn any_solution_id() -> BoxedStrategy<SolutionID<CurrentNetwork>> {
 67          any::<u64>().prop_map(|seed| TestRng::fixed(seed).r#gen::<u64>().into()).boxed()
 68      }
 69  
 70      pub fn any_solution() -> BoxedStrategy<Solution<CurrentNetwork>> {
 71          any::<u64>()
 72              .prop_map(|seed| {
 73                  let mut rng = TestRng::fixed(seed);
 74                  let private_key = PrivateKey::<CurrentNetwork>::new(&mut rng).unwrap();
 75                  let address = Address::try_from(private_key).unwrap();
 76                  let partial_solution = PartialSolution::new(rng.r#gen(), address, rng.r#gen()).unwrap();
 77                  Solution::new(partial_solution, rng.r#gen())
 78              })
 79              .boxed()
 80      }
 81  
 82      pub fn any_unconfirmed_solution() -> BoxedStrategy<UnconfirmedSolution<CurrentNetwork>> {
 83          (any_solution_id(), any_solution())
 84              .prop_map(|(solution_id, ps)| UnconfirmedSolution { solution_id, solution: Data::Object(ps) })
 85              .boxed()
 86      }
 87  
 88      #[proptest]
 89      fn unconfirmed_solution_roundtrip(
 90          #[strategy(any_unconfirmed_solution())] original: UnconfirmedSolution<CurrentNetwork>,
 91      ) {
 92          let mut buf = BytesMut::default().writer();
 93          UnconfirmedSolution::write_le(&original, &mut buf).unwrap();
 94  
 95          let deserialized: UnconfirmedSolution<CurrentNetwork> =
 96              UnconfirmedSolution::read_le(buf.into_inner().reader()).unwrap();
 97          assert_eq!(original.solution_id, deserialized.solution_id);
 98          assert_eq!(
 99              original.solution.deserialize_blocking().unwrap(),
100              deserialized.solution.deserialize_blocking().unwrap(),
101          );
102      }
103  }