1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::{
    block::{self, Block},
    chaintypes::ChainLength,
    date::BlockDate,
    fragment::{Contents, ContentsBuilder, Fragment},
    header::{BlockVersion, Header},
    key::Hash,
    testing::data::LeaderPair,
    testing::{data::StakePool, TestGen},
};
use chain_time::TimeEra;

pub struct GenesisPraosBlockBuilder {
    date: Option<BlockDate>,
    chain_length: Option<ChainLength>,
    parent_id: Option<Hash>,
    contents_builder: ContentsBuilder,
}

impl Default for GenesisPraosBlockBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl GenesisPraosBlockBuilder {
    pub fn new() -> Self {
        GenesisPraosBlockBuilder {
            date: None,
            chain_length: None,
            parent_id: None,
            contents_builder: ContentsBuilder::new(),
        }
    }

    pub fn with_parent(&mut self, parent: &Header) -> &mut Self {
        self.with_parent_id(parent.hash());
        self.with_date(parent.block_date());
        self.with_chain_length(parent.chain_length());
        self
    }

    pub fn with_parent_id(&mut self, parent_id: Hash) -> &mut Self {
        self.parent_id = Some(parent_id);
        self
    }

    pub fn with_date(&mut self, date: BlockDate) -> &mut Self {
        self.date = Some(date);
        self
    }

    pub fn with_chain_length(&mut self, chain_length: ChainLength) -> &mut Self {
        self.chain_length = Some(chain_length);
        self
    }

    pub fn with_fragment(&mut self, fragment: Fragment) -> &mut Self {
        self.contents_builder.push(fragment);
        self
    }

    pub fn with_fragments(&mut self, fragments: Vec<Fragment>) -> &mut Self {
        for fragment in fragments {
            self.with_fragment(fragment);
        }
        self
    }

    pub fn build(&self, stake_pool: &StakePool, time_era: &TimeEra) -> Block {
        if self.date.is_none() || self.chain_length.is_none() || self.parent_id.is_none() {
            panic!("date,chain_length or hash is not set");
        }
        let vrf_proof = TestGen::vrf_proof(stake_pool);
        let contents: Contents = self.contents_builder.clone().into();
        block::builder(BlockVersion::KesVrfproof, contents, |builder| {
            Ok::<_, ()>(
                builder
                    .set_parent(
                        &self.parent_id.unwrap(),
                        self.chain_length.unwrap().increase(),
                    )
                    .set_date(self.date.unwrap().next(time_era))
                    .into_genesis_praos_builder()
                    .unwrap()
                    .set_consensus_data(&stake_pool.id(), &vrf_proof)
                    .sign_using(stake_pool.kes().private_key())
                    .generalize(),
            )
        })
        .unwrap()
    }
}

pub struct BftBlockBuilder {
    date: Option<BlockDate>,
    chain_length: Option<ChainLength>,
    parent_id: Option<Hash>,
    contents_builder: ContentsBuilder,
}

impl Default for BftBlockBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl BftBlockBuilder {
    pub fn new() -> Self {
        Self {
            date: None,
            chain_length: None,
            parent_id: None,
            contents_builder: ContentsBuilder::new(),
        }
    }

    pub fn with_parent(&mut self, parent: &Header) -> &mut Self {
        self.with_parent_id(parent.hash());
        self.with_date(parent.block_date());
        self.with_chain_length(parent.chain_length());
        self
    }

    pub fn with_parent_id(&mut self, parent_id: Hash) -> &mut Self {
        self.parent_id = Some(parent_id);
        self
    }

    pub fn with_date(&mut self, date: BlockDate) -> &mut Self {
        self.date = Some(date);
        self
    }

    pub fn with_chain_length(&mut self, chain_length: ChainLength) -> &mut Self {
        self.chain_length = Some(chain_length);
        self
    }

    pub fn with_fragment(&mut self, fragment: Fragment) -> &mut Self {
        self.contents_builder.push(fragment);
        self
    }

    pub fn with_fragments(&mut self, fragments: Vec<Fragment>) -> &mut Self {
        for fragment in fragments {
            self.with_fragment(fragment);
        }
        self
    }

    pub fn build(&self, leader: &LeaderPair, time_era: &TimeEra) -> Block {
        if self.date.is_none() || self.chain_length.is_none() || self.parent_id.is_none() {
            panic!("date,chain_length or hash is not set");
        }
        let contents: Contents = self.contents_builder.clone().into();
        block::builder(BlockVersion::Ed25519Signed, contents, |header_builder| {
            Ok::<_, ()>(
                header_builder
                    .set_parent(&self.parent_id.unwrap(), self.chain_length.unwrap())
                    .set_date(self.date.unwrap().next(time_era))
                    .into_bft_builder()
                    .unwrap()
                    .sign_using(&leader.key())
                    .generalize(),
            )
        })
        .unwrap()
    }
}