66//!
77//! Note: This fork terminology is different from fork in blockchain.
88
9+ use std:: fmt;
10+
911use crate :: conversion:: from_saturating;
1012
1113#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , PartialOrd ) ]
1214/// Counter that is incremented every time there is a roll-back in live-chain.
1315pub struct Fork ( u64 ) ;
1416
1517impl 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+
1643 /// Convert an `<T>` to `Fork` (saturate if out of range).
1744 pub fn from_saturating <
1845 T : Copy
@@ -38,6 +65,17 @@ impl Fork {
3865 }
3966}
4067
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+
4179impl From < u64 > for Fork {
4280 fn from ( value : u64 ) -> Self {
4381 Self ( value)
0 commit comments