puzzle_response.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 PuzzleResponse<N: Network> { 27 pub epoch_hash: N::BlockHash, 28 pub block_header: Data<Header<N>>, 29 } 30 31 impl<N: Network> MessageTrait for PuzzleResponse<N> { 32 /// Returns the message name. 33 #[inline] 34 fn name(&self) -> Cow<'static, str> { 35 "PuzzleResponse".into() 36 } 37 } 38 39 impl<N: Network> ToBytes for PuzzleResponse<N> { 40 fn write_le<W: io::Write>(&self, mut writer: W) -> io::Result<()> { 41 self.epoch_hash.write_le(&mut writer)?; 42 self.block_header.write_le(&mut writer) 43 } 44 } 45 46 impl<N: Network> FromBytes for PuzzleResponse<N> { 47 fn read_le<R: io::Read>(mut reader: R) -> io::Result<Self> { 48 Ok(Self { epoch_hash: N::BlockHash::read_le(&mut reader)?, block_header: Data::read_le(reader)? }) 49 } 50 } 51 52 #[cfg(test)] 53 pub mod prop_tests { 54 use crate::{PuzzleResponse, challenge_response::prop_tests::any_genesis_header}; 55 use alphavm::{ 56 console::prelude::{FromBytes, ToBytes}, 57 ledger::narwhal::Data, 58 prelude::{Network, Rng, TestRng}, 59 }; 60 61 use bytes::{Buf, BufMut, BytesMut}; 62 use proptest::prelude::{BoxedStrategy, Strategy, any}; 63 use test_strategy::proptest; 64 65 type CurrentNetwork = alphavm::prelude::MainnetV0; 66 67 pub fn any_epoch_hash() -> BoxedStrategy<<CurrentNetwork as Network>::BlockHash> { 68 any::<u64>() 69 .prop_map(|seed| { 70 let mut rng = TestRng::fixed(seed); 71 rng.r#gen() 72 }) 73 .boxed() 74 } 75 76 pub fn any_puzzle_response() -> BoxedStrategy<PuzzleResponse<CurrentNetwork>> { 77 (any_epoch_hash(), any_genesis_header()) 78 .prop_map(|(epoch_hash, bh)| PuzzleResponse { epoch_hash, block_header: Data::Object(bh) }) 79 .boxed() 80 } 81 82 #[proptest] 83 fn puzzle_response_roundtrip(#[strategy(any_puzzle_response())] original: PuzzleResponse<CurrentNetwork>) { 84 let mut buf = BytesMut::default().writer(); 85 PuzzleResponse::write_le(&original, &mut buf).unwrap(); 86 87 let deserialized: PuzzleResponse<CurrentNetwork> = PuzzleResponse::read_le(buf.into_inner().reader()).unwrap(); 88 assert_eq!(original.epoch_hash, deserialized.epoch_hash); 89 assert_eq!( 90 original.block_header.deserialize_blocking().unwrap(), 91 deserialized.block_header.deserialize_blocking().unwrap(), 92 ); 93 } 94 }