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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#![allow(dead_code)]

use chain_addr::Discrimination;
use chain_impl_mockchain::{chaintypes::ConsensusVersion, fee::LinearFee, milli::Milli};
use jormungandr_automation::jormungandr::NodeAlias;
use jormungandr_lib::{
    interfaces::{
        ActiveSlotCoefficient, BlockContentMaxSize, CommitteeIdDef, ConsensusLeaderId,
        ConsensusVersionDef, DiscriminationDef, KesUpdateSpeed, LinearFeeDef,
        NumberOfSlotsPerEpoch, SlotDuration,
    },
    time::SecondsSinceUnixEpoch,
};
use serde::Deserialize;

#[derive(Clone, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct BlockchainConfiguration {
    #[serde(default)]
    block0_date: SecondsSinceUnixEpoch,
    #[serde(default)]
    block_content_max_size: BlockContentMaxSize,
    #[serde(with = "ConsensusVersionDef")]
    consensus: ConsensusVersion,
    #[serde(default)]
    consensus_genesis_praos_active_slot_coeff: ActiveSlotCoefficient,
    #[serde(with = "DiscriminationDef")]
    discrimination: Discrimination,
    #[serde(default)]
    committees: Vec<CommitteeIdDef>,
    #[serde(default)]
    external_consensus_leader_ids: Vec<ConsensusLeaderId>,
    #[serde(default)]
    kes_update_speed: KesUpdateSpeed,
    #[serde(default)]
    leaders: Vec<NodeAlias>,
    #[serde(with = "LinearFeeDef")]
    linear_fee: LinearFee,
    #[serde(default)]
    slot_duration: SlotDuration,
    #[serde(default)]
    slots_per_epoch: NumberOfSlotsPerEpoch,
    #[serde(default)]
    tx_max_expiry_epochs: Option<u8>,
}

impl BlockchainConfiguration {
    pub fn block0_date(&self) -> SecondsSinceUnixEpoch {
        self.block0_date
    }

    pub fn with_block0_date(mut self, block0_date: SecondsSinceUnixEpoch) -> Self {
        self.block0_date = block0_date;
        self
    }

    pub fn block_content_max_size(&self) -> &BlockContentMaxSize {
        &self.block_content_max_size
    }

    pub fn with_block_content_max_size(
        mut self,
        block_content_max_size: BlockContentMaxSize,
    ) -> Self {
        self.block_content_max_size = block_content_max_size;
        self
    }

    pub fn consensus(&self) -> &ConsensusVersion {
        &self.consensus
    }

    pub fn with_consensus(mut self, consensus: ConsensusVersion) -> Self {
        self.consensus = consensus;
        self
    }

    pub fn consensus_genesis_praos_active_slot_coeff(&self) -> &ActiveSlotCoefficient {
        &self.consensus_genesis_praos_active_slot_coeff
    }

    pub fn with_consensus_genesis_praos_active_slot_coeff(
        mut self,
        coeff: ActiveSlotCoefficient,
    ) -> Self {
        self.consensus_genesis_praos_active_slot_coeff = coeff;
        self
    }

    pub fn discrimination(&self) -> Discrimination {
        self.discrimination
    }

    pub fn with_discrimination(mut self, discrimination: Discrimination) -> Self {
        self.discrimination = discrimination;
        self
    }

    pub fn committees(&self) -> Vec<CommitteeIdDef> {
        self.committees.clone()
    }

    pub fn with_committees(mut self, committees: Vec<CommitteeIdDef>) -> Self {
        self.committees = committees;
        self
    }

    pub fn with_committee(mut self, committee: CommitteeIdDef) -> Self {
        self.committees.push(committee);
        self
    }

    pub fn external_consensus_leader_ids(&self) -> Vec<ConsensusLeaderId> {
        self.external_consensus_leader_ids.clone()
    }

    pub fn with_external_consensus_leader_ids(
        mut self,
        external_consensus_leader_ids: Vec<ConsensusLeaderId>,
    ) -> Self {
        self.external_consensus_leader_ids = external_consensus_leader_ids;
        self
    }

    pub fn has_external_consensus_leader_ids(&self) -> bool {
        !self.external_consensus_leader_ids().is_empty()
    }

    pub fn kes_update_speed(&self) -> &KesUpdateSpeed {
        &self.kes_update_speed
    }

    pub fn leaders(&self) -> impl Iterator<Item = &NodeAlias> {
        self.leaders.iter()
    }

    pub fn with_leader<S: Into<NodeAlias>>(mut self, alias: S) -> Self {
        self.leaders.push(alias.into());
        self
    }

    pub fn with_leaders<S: Into<NodeAlias>>(mut self, aliases: Vec<S>) -> Self {
        self.leaders.extend(aliases.into_iter().map(Into::into));
        self
    }

    pub fn linear_fee(&self) -> LinearFee {
        self.linear_fee.clone()
    }

    pub fn with_linear_fee(mut self, linear_fee: LinearFee) -> Self {
        self.linear_fee = linear_fee;
        self
    }

    pub fn slot_duration(&self) -> &SlotDuration {
        &self.slot_duration
    }

    pub fn with_slot_duration(mut self, slot_duration: SlotDuration) -> Self {
        self.slot_duration = slot_duration;
        self
    }

    pub fn slots_per_epoch(&self) -> &NumberOfSlotsPerEpoch {
        &self.slots_per_epoch
    }

    pub fn with_slots_per_epoch(mut self, slots_per_epoch: NumberOfSlotsPerEpoch) -> Self {
        self.slots_per_epoch = slots_per_epoch;
        self
    }

    pub fn tx_max_expiry_epochs(&self) -> Option<u8> {
        self.tx_max_expiry_epochs
    }

    pub fn with_tx_max_expiry_epochs(mut self, tx_max_expiry_epochs: Option<u8>) -> Self {
        self.tx_max_expiry_epochs = tx_max_expiry_epochs;
        self
    }
}

impl Default for BlockchainConfiguration {
    fn default() -> Self {
        Self {
            block0_date: Default::default(),
            block_content_max_size: BlockContentMaxSize::default(),
            committees: Vec::new(),
            consensus: ConsensusVersion::GenesisPraos,
            consensus_genesis_praos_active_slot_coeff: ActiveSlotCoefficient::new(
                Milli::from_millis(500),
            )
            .unwrap(),
            discrimination: Discrimination::Test,
            external_consensus_leader_ids: Vec::new(),
            kes_update_speed: KesUpdateSpeed::new(46800).unwrap(),
            leaders: Vec::new(),
            linear_fee: LinearFee::new(1, 1, 1),
            slot_duration: SlotDuration::new(2).unwrap(),
            slots_per_epoch: NumberOfSlotsPerEpoch::new(60).unwrap(),
            tx_max_expiry_epochs: None,
        }
    }
}

#[derive(Default)]
pub struct BlockchainBuilder {
    blockchain: BlockchainConfiguration,
}

impl BlockchainBuilder {
    pub fn block0_date(mut self, block0_date: SecondsSinceUnixEpoch) -> Self {
        self.blockchain = self.blockchain.with_block0_date(block0_date);
        self
    }

    pub fn block_content_max_size(mut self, block_content_max_size: BlockContentMaxSize) -> Self {
        self.blockchain = self
            .blockchain
            .with_block_content_max_size(block_content_max_size);
        self
    }

    pub fn committee(mut self, committee_id: CommitteeIdDef) -> Self {
        self.blockchain = self.blockchain.with_committee(committee_id);
        self
    }

    pub fn consensus(mut self, consensus: ConsensusVersion) -> Self {
        self.blockchain = self.blockchain.with_consensus(consensus);
        self
    }

    pub fn consensus_genesis_praos_active_slot_coeff(
        mut self,
        coeff: ActiveSlotCoefficient,
    ) -> Self {
        self.blockchain = self
            .blockchain
            .with_consensus_genesis_praos_active_slot_coeff(coeff);
        self
    }

    pub fn discrimination(mut self, discrimination: Discrimination) -> Self {
        self.blockchain = self.blockchain.with_discrimination(discrimination);
        self
    }

    pub fn committees(mut self, committees: Vec<CommitteeIdDef>) -> Self {
        self.blockchain = self.blockchain.with_committees(committees);
        self
    }

    pub fn external_consensus_leader_ids(
        mut self,
        external_consensus_leader_ids: Vec<ConsensusLeaderId>,
    ) -> Self {
        self.blockchain = self
            .blockchain
            .with_external_consensus_leader_ids(external_consensus_leader_ids);
        self
    }

    pub fn leader<S: Into<NodeAlias>>(mut self, alias: S) -> Self {
        self.blockchain = self.blockchain.with_leader(alias.into());
        self
    }

    pub fn leaders<S: Into<NodeAlias>>(mut self, aliases: Vec<S>) -> Self {
        self.blockchain = self.blockchain.with_leaders(aliases);
        self
    }
    pub fn linear_fee(mut self, linear_fee: LinearFee) -> Self {
        self.blockchain = self.blockchain.with_linear_fee(linear_fee);
        self
    }

    pub fn slot_duration(mut self, slot_duration: u8) -> Self {
        self.blockchain = self
            .blockchain
            .with_slot_duration(SlotDuration::new(slot_duration).unwrap());
        self
    }

    pub fn slots_per_epoch(mut self, slots_per_epoch: u32) -> Self {
        self.blockchain = self
            .blockchain
            .with_slots_per_epoch(NumberOfSlotsPerEpoch::new(slots_per_epoch).unwrap());
        self
    }

    pub fn tx_max_expiry_epochs(mut self, tx_max_expiry_epochs: Option<u8>) -> Self {
        self.blockchain = self
            .blockchain
            .with_tx_max_expiry_epochs(tx_max_expiry_epochs);
        self
    }

    pub fn build(self) -> BlockchainConfiguration {
        self.blockchain
    }
}