ergo_chain_types/
preheader.rs

1//! Block header with fields that can be predicted by miner
2use crate::{BlockId, EcPoint, Header, Votes};
3
4/// Block header with the current `spendingTransaction`, that can be predicted
5/// by a miner before it's formation
6#[derive(PartialEq, Eq, Debug, Clone)]
7pub struct PreHeader {
8    /// Block version, to be increased on every soft and hardfork
9    pub version: u8,
10    /// Hash of parent block
11    pub parent_id: BlockId,
12    /// Timestamp of a block in ms from UNIX epoch
13    pub timestamp: u64,
14    /// Current difficulty in a compressed view.
15    pub n_bits: u64,
16    /// Block height
17    pub height: u32,
18    /// Public key of miner
19    pub miner_pk: Box<EcPoint>,
20    /// Votes
21    pub votes: Votes,
22}
23
24impl From<Header> for PreHeader {
25    fn from(bh: Header) -> Self {
26        PreHeader {
27            version: bh.version,
28            parent_id: bh.parent_id,
29            timestamp: bh.timestamp,
30            n_bits: bh.n_bits,
31            height: bh.height,
32            miner_pk: bh.autolykos_solution.miner_pk,
33            votes: bh.votes,
34        }
35    }
36}
37
38#[cfg(feature = "arbitrary")]
39mod arbitrary {
40    use proptest::array::{uniform3, uniform32};
41    use proptest::prelude::*;
42
43    use crate::EcPoint;
44
45    use super::*;
46
47    impl Arbitrary for PreHeader {
48        type Parameters = ();
49        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
50            (
51                uniform32(1u8..),
52                // Timestamps between 2000-2050
53                946_674_000_000..2_500_400_300_000u64,
54                any::<u64>(),
55                1_000_000u32..10_000_000u32,
56                any::<Box<EcPoint>>(),
57                uniform3(1u8..),
58            )
59                .prop_map(|(parent_id, timestamp, n_bits, height, miner_pk, votes)| {
60                    let parent_id = BlockId(parent_id.into());
61                    let votes = Votes(votes);
62                    Self {
63                        version: 1,
64                        parent_id,
65                        timestamp,
66                        n_bits,
67                        height,
68                        miner_pk,
69                        votes,
70                    }
71                })
72                .boxed()
73        }
74
75        type Strategy = BoxedStrategy<PreHeader>;
76    }
77}