1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
use {
18
    sc_chain_spec::{ChainSpecExtension, ChainSpecGroup},
19
    serde::{Deserialize, Serialize},
20
    std::collections::BTreeMap,
21
};
22

            
23
/// Specialized `ChainSpec` for container chains that only allows raw genesis format.
24
pub type RawChainSpec = sc_service::GenericChainSpec<Extensions>;
25

            
26
/// Helper type that implements the traits needed to be used as a "GenesisConfig",
27
/// but whose implementation panics because we only expect it to be used with raw ChainSpecs,
28
/// so it will never be serialized or deserialized.
29
/// This is because container chains must use raw chain spec files where the "genesis"
30
/// field only has one field: "raw".
31
pub struct RawGenesisConfig {
32
    pub storage_raw: BTreeMap<Vec<u8>, Vec<u8>>,
33
}
34

            
35
impl Serialize for RawGenesisConfig {
36
    fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
37
    where
38
        S: serde::Serializer,
39
    {
40
        panic!("RawGenesisConfigDummy should never be serialized")
41
    }
42
}
43

            
44
impl<'de> Deserialize<'de> for RawGenesisConfig {
45
    fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
46
    where
47
        D: serde::Deserializer<'de>,
48
    {
49
        panic!("Attempted to read a non-raw ContainerChain ChainSpec.\nHelp: add `--raw` flag to `build-spec` command to generate a raw chain spec")
50
    }
51
}
52

            
53
impl sp_runtime::BuildStorage for RawGenesisConfig {
54
    fn assimilate_storage(&self, storage: &mut sp_core::storage::Storage) -> Result<(), String> {
55
        storage
56
            .top
57
            .extend(self.storage_raw.iter().map(|(k, v)| (k.clone(), v.clone())));
58

            
59
        Ok(())
60
    }
61
}
62

            
63
/// The extensions for the [`ChainSpec`].
64
2682
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]
65
#[serde(deny_unknown_fields)]
66
pub struct Extensions {
67
    /// The relay chain of the Parachain.
68
    pub relay_chain: String,
69
    /// The id of the Parachain.
70
    pub para_id: u32,
71
}
72

            
73
impl Extensions {
74
    /// Try to get the extension from the given `ChainSpec`.
75
1602
    pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {
76
1602
        sc_chain_spec::get_extension(chain_spec.extensions())
77
1602
    }
78
}