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
use super::addressbook::AddressbookStorage;
use super::contracts::ContractStorage;
use super::keyfile::KeystoreError;
use super::{
    build_addressbook_storage, build_contract_storage, build_keyfile_storage, build_path,
    KeyfileStorage,
};
use std::collections::HashMap;
use std::path::Path;

const CHAIN_NAMES: &'static [&'static str; 8] = &[
    "eth",
    "morden",
    "ropsten",
    "rinkeby",
    "rootstock-main",
    "rootstock-test",
    "kovan",
    "mainnet",
];

/// Controller to switch storage according to specified chain
pub struct StorageController {
    keyfile_storages: HashMap<String, Box<KeyfileStorage>>,
    contract_storages: HashMap<String, Box<ContractStorage>>,
    addressbook_storages: HashMap<String, Box<AddressbookStorage>>,
}

fn chain_dir(name: &str) -> &str {
    match name {
        "etc" => "mainnet",
        "etc-morden" => "morden",
        _ => name
    }
}

impl StorageController {
    /// Create new `StorageController`
    /// with a subfolders for
    pub fn new<P: AsRef<Path>>(base_path: P) -> Result<StorageController, KeystoreError> {
        let mut st = StorageController::default();

        for id in CHAIN_NAMES {
            st.keyfile_storages.insert(
                id.to_string(),
                build_keyfile_storage(build_path(base_path.as_ref(), id, "keystore"))?,
            );
            st.contract_storages.insert(
                id.to_string(),
                build_contract_storage(build_path(base_path.as_ref(), id, "contracts"))?,
            );
            st.addressbook_storages.insert(
                id.to_string(),
                build_addressbook_storage(build_path(base_path.as_ref(), id, "addressbook"))?,
            );
        }

        Ok(st)
    }

    /// Get `KeyFile` storage for specified chain
    pub fn get_keystore(&self, chain: &str) -> Result<&Box<KeyfileStorage>, KeystoreError> {
        match self.keyfile_storages.get(chain_dir(chain)) {
            Some(st) => Ok(st),
            None => Err(KeystoreError::StorageError(format!(
                "No storage for: {}",
                chain
            ))),
        }
    }

    /// Get `Contract` storage for specified chain
    pub fn get_contracts(&self, chain: &str) -> Result<&Box<ContractStorage>, KeystoreError> {
        match self.contract_storages.get(chain_dir(chain)) {
            Some(st) => Ok(st),
            None => Err(KeystoreError::StorageError(format!(
                "No storage for: {}",
                chain
            ))),
        }
    }

    /// Get `Addressbook` storage for specified chain
    pub fn get_addressbook(&self, chain: &str) -> Result<&Box<AddressbookStorage>, KeystoreError> {
        match self.addressbook_storages.get(chain_dir(chain)) {
            Some(st) => Ok(st),
            None => Err(KeystoreError::StorageError(format!(
                "No storage for: {}",
                chain
            ))),
        }
    }
}

impl Default for StorageController {
    fn default() -> Self {
        StorageController {
            keyfile_storages: HashMap::new(),
            contract_storages: HashMap::new(),
            addressbook_storages: HashMap::new(),
        }
    }
}