ckb_jsonrpc_types/
primitive.rs

1use crate::{Uint32, Uint64};
2use ckb_types::core::EpochNumberWithFraction as CkbEpochNumberWithFraction;
3
4/// Consecutive block number starting from 0.
5///
6/// This is a 64-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint64](type.Uint64.html#examples).
7pub type BlockNumber = Uint64;
8/// Consecutive epoch number starting from 0.
9///
10/// This is a 64-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint64](type.Uint64.html#examples).
11pub type EpochNumber = Uint64;
12/// The epoch indicator of a block. It shows which epoch the block is in, and the elapsed epoch fraction after adding this block.
13///
14/// This is a 64-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint64](type.Uint64.html#examples).
15///
16/// The lower 56 bits of the epoch field are split into 3 parts (listed in the order from higher bits to lower bits):
17///
18/// * The highest 16 bits represent the epoch length
19/// * The next 16 bits represent the current block index in the epoch, starting from 0.
20/// * The lowest 24 bits represent the current epoch number.
21///
22/// Assume there's a block, which number is 11555 and in epoch 50. The epoch 50 starts from block
23/// 11000 and have 1000 blocks. The epoch field for this particular block will then be 1,099,520,939,130,930,
24/// which is calculated in the following way:
25///
26/// ```text
27/// 50 | ((11555 - 11000) << 24) | (1000 << 40)
28/// ```
29pub type EpochNumberWithFraction = Uint64;
30/// The capacity of a cell is the value of the cell in Shannons. It is also the upper limit of the cell occupied storage size where every 100,000,000 Shannons give 1-byte storage.
31///
32/// This is a 64-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint64](type.Uint64.html#examples).
33pub type Capacity = Uint64;
34/// Count of cycles consumed by CKB VM to run scripts.
35///
36/// This is a 64-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint64](type.Uint64.html#examples).
37pub type Cycle = Uint64;
38/// The Unix timestamp in milliseconds (1 second is 1000 milliseconds).
39///
40/// For example, 1588233578000 is Thu, 30 Apr 2020 07:59:38 +0000
41///
42/// This is a 64-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint64](type.Uint64.html#examples).
43pub type Timestamp = Uint64;
44/// The simple increasing integer version.
45///
46/// This is a 32-bit unsigned integer type encoded as the 0x-prefixed hex string in JSON. See examples of [Uint32](type.Uint32.html#examples).
47pub type Version = Uint32;
48
49/// This trait is a restriction for type `Uint64`, so we can only get epoch_number, epoch_index
50/// and epoch_length from the type `EpochNumberWithFraction` instead of all `Uint64`
51pub trait AsEpochNumberWithFraction {
52    /// Return the epoch number of current block
53    fn epoch_number(&self) -> u64;
54    /// Return the index in epoch of current block
55    fn epoch_index(&self) -> u64;
56    /// Return the epoch length of current block
57    fn epoch_length(&self) -> u64;
58}
59
60impl AsEpochNumberWithFraction for EpochNumberWithFraction {
61    fn epoch_number(&self) -> u64 {
62        (self.value() >> CkbEpochNumberWithFraction::NUMBER_OFFSET)
63            & CkbEpochNumberWithFraction::NUMBER_MASK
64    }
65
66    fn epoch_index(&self) -> u64 {
67        (self.value() >> CkbEpochNumberWithFraction::INDEX_OFFSET)
68            & CkbEpochNumberWithFraction::INDEX_MASK
69    }
70
71    fn epoch_length(&self) -> u64 {
72        (self.value() >> CkbEpochNumberWithFraction::LENGTH_OFFSET)
73            & CkbEpochNumberWithFraction::LENGTH_MASK
74    }
75}