ergo_lib/chain/ergo_state_context.rs
1//! Blockchain state
2use ergo_chain_types::{Header, PreHeader};
3
4use super::parameters::Parameters;
5
6/// Fixed number of last block headers in descending order (first header is the newest one)
7pub type Headers = [Header; 10];
8
9/// Blockchain state (last headers, etc.)
10#[derive(PartialEq, Eq, Debug, Clone)]
11pub struct ErgoStateContext {
12 /// Block header with the current `spendingTransaction`, that can be predicted
13 /// by a miner before it's formation
14 pub pre_header: PreHeader,
15 /// Fixed number of last block headers in descending order (first header is the newest one)
16 pub headers: Headers,
17 /// Parameters that can be adjusted by voting
18 pub parameters: Parameters,
19}
20
21impl ErgoStateContext {
22 /// Create an ErgoStateContext instance
23 /// # Parameters
24 /// For signing, [Parameters::default()] is sufficient. For consensus-critical applications that validate transactions it is important that parameters represent the latest state of the blockchain
25 pub fn new(
26 pre_header: PreHeader,
27 headers: Headers,
28 parameters: Parameters,
29 ) -> ErgoStateContext {
30 ErgoStateContext {
31 pre_header,
32 headers,
33 parameters,
34 }
35 }
36}
37
38#[cfg(feature = "arbitrary")]
39mod arbitrary {
40 use super::*;
41 use proptest::prelude::*;
42
43 impl Arbitrary for ErgoStateContext {
44 type Parameters = ();
45 type Strategy = BoxedStrategy<Self>;
46
47 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
48 // TODO: parameters should implement arbitrary as well, based on minimum/maximum constraints of each parameter
49 (any::<PreHeader>(), any::<Headers>())
50 .prop_map(|(pre_header, headers)| {
51 Self::new(pre_header, headers, Parameters::default())
52 })
53 .boxed()
54 }
55 }
56}