6
6
//!
7
7
//! Note: This fork terminology is different from fork in blockchain.
8
8
9
+ use std:: fmt;
10
+
9
11
use crate :: conversion:: from_saturating;
10
12
11
13
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , PartialOrd ) ]
12
14
/// Counter that is incremented every time there is a roll-back in live-chain.
13
15
pub struct Fork ( u64 ) ;
14
16
15
17
impl Fork {
18
+ /// Fork for data that read from the blockchain during a backfill on initial sync
19
+ pub const BACKFILL : Self = Self ( 1 ) ;
20
+ /// Fork count for the first live block.
21
+ pub const FIRST_LIVE : Self = Self ( 2 ) ;
22
+ /// Fork for immutable data. This indicates that there is no roll-back.
23
+ pub const IMMUTABLE : Self = Self ( 0 ) ;
24
+
25
+ /// Is the fork for immutable data.
26
+ #[ must_use]
27
+ pub fn is_immutable ( & self ) -> bool {
28
+ self == & Self :: IMMUTABLE
29
+ }
30
+
31
+ /// Is the fork for backfill data.
32
+ #[ must_use]
33
+ pub fn is_backfill ( & self ) -> bool {
34
+ self == & Self :: BACKFILL
35
+ }
36
+
37
+ /// Is the fork for live data.
38
+ #[ must_use]
39
+ pub fn is_live ( & self ) -> bool {
40
+ self >= & Self :: FIRST_LIVE
41
+ }
42
+
16
43
/// Convert an `<T>` to `Fork` (saturate if out of range).
17
44
pub fn from_saturating <
18
45
T : Copy
@@ -38,6 +65,17 @@ impl Fork {
38
65
}
39
66
}
40
67
68
+ impl fmt:: Display for Fork {
69
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
70
+ match self . 0 {
71
+ 0 => write ! ( f, "IMMUTABLE" ) ,
72
+ 1 => write ! ( f, "BACKFILL" ) ,
73
+ // For live forks: 2 maps to LIVE:1, 3 maps to LIVE:2 etc.
74
+ 2 ..=u64:: MAX => write ! ( f, "LIVE:{}" , self . 0 - 1 ) ,
75
+ }
76
+ }
77
+ }
78
+
41
79
impl From < u64 > for Fork {
42
80
fn from ( value : u64 ) -> Self {
43
81
Self ( value)
0 commit comments