fuel_core_chain_config/config/
consensus.rs

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
use alloc::collections::BTreeMap;
use fuel_core_types::{
    fuel_tx::Input,
    fuel_types::{
        Address,
        BlockHeight,
    },
};
use serde::{
    Deserialize,
    Serialize,
};

use crate as fuel_core_chain_config;
use fuel_core_chain_config::default_consensus_dev_key;

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub enum ConsensusConfig {
    PoA { signing_key: Address },
    PoAV2(PoAV2),
}

impl ConsensusConfig {
    pub fn default_poa() -> Self {
        ConsensusConfig::PoAV2(PoAV2 {
            genesis_signing_key: Input::owner(&default_consensus_dev_key().public_key()),
            signing_key_overrides: Default::default(),
        })
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct PoAV2 {
    genesis_signing_key: Address,
    signing_key_overrides: BTreeMap<BlockHeight, Address>,
}

impl PoAV2 {
    pub fn new(
        genesis_signing_key: Address,
        signing_key_overrides: BTreeMap<BlockHeight, Address>,
    ) -> Self {
        PoAV2 {
            genesis_signing_key,
            signing_key_overrides,
        }
    }

    /// Returns the signing key for the given block height.
    pub fn signing_key_at(&self, height: BlockHeight) -> Address {
        if self.signing_key_overrides.is_empty() {
            self.genesis_signing_key
        } else {
            self.signing_key_overrides
                .range(..=height)
                .last()
                .map(|(_, key)| key)
                .cloned()
                .unwrap_or(self.genesis_signing_key)
        }
    }

    /// Returns overrides for all the signing keys.
    pub fn get_all_overrides(&self) -> &BTreeMap<BlockHeight, Address> {
        &self.signing_key_overrides
    }

    #[cfg(feature = "test-helpers")]
    pub fn set_genesis_signing_key(&mut self, key: Address) {
        self.genesis_signing_key = key;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn signing_key_at_works() {
        // Given
        let genesis_signing_key = Address::from([1; 32]);
        let signing_key_after_10 = Address::from([2; 32]);
        let signing_key_after_20 = Address::from([3; 32]);
        let signing_key_after_30 = Address::from([4; 32]);
        let signing_key_overrides = vec![
            (10u32.into(), signing_key_after_10),
            (20u32.into(), signing_key_after_20),
            (30u32.into(), signing_key_after_30),
        ]
        .into_iter()
        .collect();
        let poa = PoAV2 {
            genesis_signing_key,
            signing_key_overrides,
        };

        // When/Then
        assert_eq!(poa.signing_key_at(0u32.into()), genesis_signing_key);
        assert_eq!(poa.signing_key_at(9u32.into()), genesis_signing_key);
        assert_eq!(poa.signing_key_at(10u32.into()), signing_key_after_10);
        assert_eq!(poa.signing_key_at(19u32.into()), signing_key_after_10);
        assert_eq!(poa.signing_key_at(20u32.into()), signing_key_after_20);
        assert_eq!(poa.signing_key_at(29u32.into()), signing_key_after_20);
        assert_eq!(poa.signing_key_at(30u32.into()), signing_key_after_30);
        assert_eq!(poa.signing_key_at(40u32.into()), signing_key_after_30);
        assert_eq!(
            poa.signing_key_at(4_000_000u32.into()),
            signing_key_after_30
        );
    }
}