kitchensink_runtime/constants.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! A set of constant values used in substrate runtime.
19
20/// Money matters.
21pub mod currency {
22 use node_primitives::Balance;
23
24 pub const MILLICENTS: Balance = 1_000_000_000;
25 pub const CENTS: Balance = 1_000 * MILLICENTS; // assume this is worth about a cent.
26 pub const DOLLARS: Balance = 100 * CENTS;
27
28 pub const fn deposit(items: u32, bytes: u32) -> Balance {
29 items as Balance * 15 * CENTS + (bytes as Balance) * 6 * CENTS
30 }
31}
32
33/// Time.
34pub mod time {
35 use node_primitives::{BlockNumber, Moment};
36
37 /// Since BABE is probabilistic this is the average expected block time that
38 /// we are targeting. Blocks will be produced at a minimum duration defined
39 /// by `SLOT_DURATION`, but some slots will not be allocated to any
40 /// authority and hence no block will be produced. We expect to have this
41 /// block time on average following the defined slot duration and the value
42 /// of `c` configured for BABE (where `1 - c` represents the probability of
43 /// a slot being empty).
44 /// This value is only used indirectly to define the unit constants below
45 /// that are expressed in blocks. The rest of the code should use
46 /// `SLOT_DURATION` instead (like the Timestamp pallet for calculating the
47 /// minimum period).
48 ///
49 /// If using BABE with secondary slots (default) then all of the slots will
50 /// always be assigned, in which case `MILLISECS_PER_BLOCK` and
51 /// `SLOT_DURATION` should have the same value.
52 ///
53 /// <https://research.web3.foundation/Polkadot/protocols/block-production/Babe#6-practical-results>
54 pub const MILLISECS_PER_BLOCK: Moment = 3000;
55 pub const SECS_PER_BLOCK: Moment = MILLISECS_PER_BLOCK / 1000;
56
57 // NOTE: Currently it is not possible to change the slot duration after the chain has started.
58 // Attempting to do so will brick block production.
59 pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK;
60
61 // 1 in 4 blocks (on average, not counting collisions) will be primary BABE blocks.
62 pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
63
64 // NOTE: Currently it is not possible to change the epoch duration after the chain has started.
65 // Attempting to do so will brick block production.
66 pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 10 * MINUTES;
67 pub const EPOCH_DURATION_IN_SLOTS: u64 = {
68 const SLOT_FILL_RATE: f64 = MILLISECS_PER_BLOCK as f64 / SLOT_DURATION as f64;
69
70 (EPOCH_DURATION_IN_BLOCKS as f64 * SLOT_FILL_RATE) as u64
71 };
72
73 // These time units are defined in number of blocks.
74 pub const MINUTES: BlockNumber = 60 / (SECS_PER_BLOCK as BlockNumber);
75 pub const HOURS: BlockNumber = MINUTES * 60;
76 pub const DAYS: BlockNumber = HOURS * 24;
77}