chain_spec.rs 3.13 KB
Newer Older
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
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::{
    super::Result,
    OffBalance,
    OffTimestamp,
};
use crate::env::EnvTypes;

/// The chain specification.
pub struct ChainSpec {
    /// The current gas price.
    gas_price: OffBalance,
    /// The minimum value an account of the chain may have.
    minimum_balance: OffBalance,
    /// The tombstone deposit.
    tombstone_deposit: OffBalance,
    /// The targeted block time.
    block_time: OffTimestamp,
}

impl ChainSpec {
    /// Creates a new uninitialized chain specification.
    pub fn uninitialized() -> Self {
        Self {
            gas_price: OffBalance::uninitialized(),
            minimum_balance: OffBalance::uninitialized(),
            tombstone_deposit: OffBalance::uninitialized(),
            block_time: OffTimestamp::uninitialized(),
        }
    }

45 46 47 48 49 50 51 52
    /// Resets the chain spec to uninitialized state.
    pub fn reset(&mut self) {
        self.gas_price = OffBalance::uninitialized();
        self.minimum_balance = OffBalance::uninitialized();
        self.tombstone_deposit = OffBalance::uninitialized();
        self.block_time = OffTimestamp::uninitialized();
    }

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
    /// Default initialization for the off-chain specification.
    pub fn initialize_as_default<T>(&mut self) -> crate::env::Result<()>
    where
        T: EnvTypes,
        <T as EnvTypes>::AccountId: From<[u8; 32]>,
    {
        self.gas_price
            .try_initialize::<T::Balance>(&T::Balance::from(100))?;
        self.minimum_balance
            .try_initialize::<T::Balance>(&T::Balance::from(42))?;
        self.tombstone_deposit
            .try_initialize::<T::Balance>(&T::Balance::from(16))?;
        self.block_time
            .try_initialize::<T::Timestamp>(&T::Timestamp::from(5))?;
        Ok(())
    }

    /// Returns the gas price for the chain.
    pub fn gas_price<T>(&self) -> Result<T::Balance>
    where
        T: EnvTypes,
    {
        self.gas_price.decode().map_err(Into::into)
    }

    /// Returns the minimum balance for an account on the chain.
    pub fn minimum_balance<T>(&self) -> Result<T::Balance>
    where
        T: EnvTypes,
    {
        self.minimum_balance.decode().map_err(Into::into)
    }

    /// Returns the tombstone deposit for the chain.
    pub fn tombstone_deposit<T>(&self) -> Result<T::Balance>
    where
        T: EnvTypes,
    {
        self.tombstone_deposit.decode().map_err(Into::into)
    }

    /// Returns the targeted block time for the chain.
    pub fn block_time<T>(&self) -> Result<T::Timestamp>
    where
        T: EnvTypes,
    {
        self.block_time.decode().map_err(Into::into)
    }
}