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
// Copyright (C) 2019-2020 Boyu Yang
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::{collections::HashMap, convert::TryFrom};

use property::Property;
use serde_derive::Deserialize;

use ckb_types::H256;

use crate::{
    blockchain::{Bundled, DepGroupId, Network},
    error::{Error, Result},
};

lazy_static::lazy_static! {
    static ref SYSTEM_DEPS: SystemDeps = SystemDeps::initialize().unwrap();
}

#[derive(Deserialize)]
struct RawSpecHashes {
    genesis: H256,
    cellbase: H256,
    system_cells: Vec<RawSystemCell>,
    dep_groups: Vec<RawDepGroup>,
}

#[derive(Deserialize)]
struct RawSystemCell {
    path: String,
    tx_hash: H256,
    index: usize,
    data_hash: H256,
    type_hash: Option<H256>,
}

#[derive(Deserialize)]
struct RawDepGroup {
    included_cells: Vec<String>,
    tx_hash: H256,
    index: usize,
}

pub struct SystemDeps(HashMap<Network, SpecHashes>);

#[derive(Property, Debug, Clone)]
#[property(get(public), set(disable), mut(disable))]
pub struct SpecHashes {
    genesis: H256,
    cellbase: H256,
    system_cells: HashMap<Bundled, SystemCell>,
    dep_groups: HashMap<DepGroupId, DepGroup>,
}

#[derive(Property, Debug, Clone)]
#[property(get(public), set(disable), mut(disable))]
pub struct SystemCell {
    tx_hash: H256,
    index: usize,
    data_hash: H256,
    type_hash: Option<H256>,
}

#[derive(Property, Debug, Clone)]
#[property(get(public), set(disable), mut(disable))]
pub struct DepGroup {
    tx_hash: H256,
    index: usize,
}

fn network_from_spec_name(spec_name: &str) -> Result<Network> {
    match spec_name {
        "ckb" => Ok("mainnet"),
        "ckb_testnet" => Ok("testnet"),
        "ckb_staging" => Ok("staging"),
        "ckb_dev" => Ok("develop"),
        s => Ok(s),
    }
    .and_then(Network::try_from)
}

impl SystemDeps {
    pub fn read() -> &'static Self {
        &SYSTEM_DEPS
    }

    fn initialize() -> Result<Self> {
        let raw_spec_hashes: HashMap<String, RawSpecHashes> =
            toml::from_str(include_str!(concat!(env!("OUT_DIR"), "/hashes.toml"))).unwrap();
        raw_spec_hashes
            .into_iter()
            .map(|(spec_name, raw_spec_values)| {
                let network = network_from_spec_name(&spec_name)?;
                let RawSpecHashes {
                    genesis,
                    cellbase,
                    system_cells,
                    dep_groups,
                } = raw_spec_values;
                let system_cells = system_cells
                    .into_iter()
                    .map(|raw_system_cell| {
                        let RawSystemCell {
                            path,
                            tx_hash,
                            index,
                            data_hash,
                            type_hash,
                        } = raw_system_cell;
                        let bundled = Bundled::try_from(path.as_str())?;
                        let system_cell = SystemCell {
                            tx_hash,
                            index,
                            data_hash,
                            type_hash,
                        };
                        Ok((bundled, system_cell))
                    })
                    .collect::<Result<HashMap<_, _>>>()?;
                let dep_groups = dep_groups
                    .into_iter()
                    .map(|raw_dep_group| {
                        let RawDepGroup {
                            included_cells,
                            tx_hash,
                            index,
                        } = raw_dep_group;
                        let included_cells = included_cells
                            .into_iter()
                            .map(|path| Bundled::try_from(path.as_str()))
                            .collect::<Result<Vec<Bundled>>>()?;
                        let dep_group_id = DepGroupId::try_from(&included_cells[..])?;
                        let dep_group = DepGroup { tx_hash, index };
                        Ok((dep_group_id, dep_group))
                    })
                    .collect::<Result<HashMap<_, _>>>()?;
                let spec_hashes = SpecHashes {
                    genesis,
                    cellbase,
                    system_cells,
                    dep_groups,
                };
                Ok((network, spec_hashes))
            })
            .collect::<Result<HashMap<_, _>>>()
            .map(Self)
    }

    pub fn lookup_system_cell(&self, network: Network, key: Bundled) -> Result<&SystemCell> {
        self.0
            .get(&network)
            .and_then(|spec| spec.system_cells.get(&key))
            .ok_or_else(|| Error::UnknownSystemCell(network, key))
    }

    pub fn lookup_dep_group(&self, network: Network, key: DepGroupId) -> Result<&DepGroup> {
        self.0
            .get(&network)
            .and_then(|spec| spec.dep_groups.get(&key))
            .ok_or_else(|| Error::UnknownDepGroup(network, key))
    }
}