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
45
46
47
48
49
50
51
52
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//!
//! # A Recover Mechanism for Blockchain Scene
//!
//! automatic operations:
//! - create a light-weight(COW) snapshot for each block
//! - clean up expired snapshots
//!

#![cfg(target_os = "linux")]
#![deny(warnings)]
#![deny(missing_docs)]

mod api;
mod driver;

pub use api::server::run_daemon;

use clap::Parser;
use driver::{btrfs, external, zfs};
use ruc::{cmd, *};
use std::{fmt, result::Result as StdResult, str::FromStr};

/// Maximum number of snapshots that can be kept
pub const CAP_MAX: u64 = 4096;

/// `itv.pow(i)`, only useful in `SnapAlgo::Fade` alfo
pub const STEP_CNT: usize = 10;

/// The co-responding VAR-name of `--snapshot-volume`
pub const ENV_VAR_BTM_VOLUME: &str = "BTM_VOLUME";

/// Config structure of snapshot
#[derive(Clone, Debug, Parser)]
pub struct BtmCfg {
    /// a global switch for enabling snapshot functions
    #[clap(long)]
    pub enable: bool,
    /// interval between adjacent snapshots, default to 10 blocks
    #[clap(short, long, default_value_t = 10)]
    pub itv: u64,
    /// the maximum number of snapshots that will be stored, default to 100
    #[clap(short, long, default_value_t = 100)]
    pub cap: u64,
    /// Zfs or Btrfs or External, will try a guess if missing
    #[clap(short, long, default_value_t = SnapMode::Zfs)]
    pub mode: SnapMode,
    /// Fair or Fade, default to 'Fair'
    #[clap(short, long, default_value_t = SnapAlgo::Fair)]
    pub algo: SnapAlgo,
    /// a data volume containing both ledger data and tendermint data
    #[clap(short = 'p', long, default_value_t = String::from("zfs/data"))]
    pub volume: String,
}

impl Default for BtmCfg {
    fn default() -> Self {
        BtmCfg {
            enable: false,
            itv: 10,
            cap: 100,
            mode: SnapMode::Zfs,
            algo: SnapAlgo::Fair,
            volume: "zfs/data".to_owned(),
        }
    }
}

impl BtmCfg {
    /// create a simple instance
    #[inline(always)]
    pub fn new() -> Self {
        Self::new_enabled()
    }

    #[inline(always)]
    fn new_enabled() -> Self {
        BtmCfg {
            enable: true,
            ..Self::default()
        }
    }

    /// Used in client side
    #[inline(always)]
    pub fn new_client_hdr() -> Self {
        Self::new_enabled()
    }

    /// generate a snapshot for the latest state of blockchain
    #[inline(always)]
    pub fn snapshot(&self, idx: u64) -> Result<()> {
        alt!(!self.enable, return Ok(()));

        // sync data to disk before snapshoting
        nix::unistd::sync();

        match self.mode {
            SnapMode::Zfs => zfs::gen_snapshot(self, idx).c(d!()),
            SnapMode::Btrfs => btrfs::gen_snapshot(self, idx).c(d!()),
            SnapMode::External => external::gen_snapshot(self, idx).c(d!()),
        }
    }

    /// rollback the state of blockchain to a specificed height
    #[inline(always)]
    pub fn rollback(&self, idx: Option<u64>, strict: bool) -> Result<()> {
        match self.mode {
            SnapMode::Zfs => zfs::rollback(self, idx, strict).c(d!()),
            SnapMode::Btrfs => btrfs::rollback(self, idx, strict).c(d!()),
            SnapMode::External => Err(eg!("please use `btm` tool in `External` mode")),
        }
    }

    /// Get snapshot list in 'DESC' order.
    #[inline(always)]
    pub fn get_sorted_snapshots(&self) -> Result<Vec<u64>> {
        match self.mode {
            SnapMode::Zfs => zfs::sorted_snapshots(self).c(d!()),
            SnapMode::Btrfs => btrfs::sorted_snapshots(self).c(d!()),
            SnapMode::External => Err(eg!("please use `btm` tool in `External` mode")),
        }
    }

    /// try to guess a correct mode
    /// NOTE: not suitable for `External` mode
    #[inline(always)]
    pub fn guess_mode(volume: &str) -> Result<SnapMode> {
        zfs::check(volume)
            .c(d!())
            .map(|_| SnapMode::Zfs)
            .or_else(|e| btrfs::check(volume).c(d!(e)).map(|_| SnapMode::Btrfs))
    }

    #[inline(always)]
    fn get_cap(&self) -> u64 {
        alt!(self.cap > CAP_MAX, CAP_MAX, self.cap)
    }

    /// List all existing snapshots.
    pub fn list_snapshots(&self) -> Result<()> {
        println!("Available snapshots are listed below:");
        self.get_sorted_snapshots().c(d!()).map(|list| {
            list.into_iter().rev().for_each(|h| {
                println!("    {}", h);
            })
        })
    }

    /// Clean all existing snapshots.
    pub fn clean_snapshots(&self) -> Result<()> {
        self.get_sorted_snapshots().c(d!()).map(|list| {
            list.into_iter().rev().for_each(|height| {
                let cmd = match self.mode {
                    SnapMode::Btrfs => {
                        format!("btrfs subvolume delete {}@{}", &self.volume, height)
                    }
                    SnapMode::Zfs => format!("zfs destroy {}@{}", &self.volume, height),
                    _ => pnk!(Err(eg!("Unsupported deriver"))),
                };
                info_omit!(cmd::exec_output(&cmd));
            });
        })
    }
}

/// # Inner Operations
///
/// assume:
/// - root volume of zfs is `zfs`
/// - root volume of btrfs is `/btrfs`
/// - business data is stored in `<root volume>/data`
/// - target block height to recover is 123456
///
/// ## snapshot
///
/// ```shell
/// # zfs filesystem
/// zfs destroy zfs/data@123456 2>/dev/null
/// zfs snapshot zfs/data@123456
///
/// # btrfs filesystem
/// rm -rf /btrfs/data@123456 2>/dev/null
/// btrfs subvolume snapshot /btrfs/data /btrfs/data@123456
/// ```
///
/// ## rollback
///
/// ```shell
/// # zfs filesystem
/// zfs rollback -r zfs/data@123456
///
/// # btrfs filesystem
/// rm -rf /btrfs/data || exit 1
/// btrfs subvolume snapshot /btrfs/data@123456 /btrfs/data
/// ```
#[derive(Clone, Copy, Debug)]
pub enum SnapMode {
    /// available on some Linux distributions and FreeBSD
    /// - Ubuntu Linux
    /// - Gentoo Linux
    /// - FreeBSD
    /// - ...
    Zfs,
    /// available on most Linux distributions,
    /// but its user experience is worse than zfs
    Btrfs,
    /// TODO: unimplemented!
    /// rely on an external independent process
    External,
}

impl SnapMode {
    #[inline(always)]
    #[allow(missing_docs)]
    pub fn from_string(m: &str) -> Result<Self> {
        match m.to_lowercase().as_str() {
            "zfs" => Ok(Self::Zfs),
            "btrfs" => Ok(Self::Btrfs),
            "external" => Ok(Self::External),
            _ => Err(eg!()),
        }
    }
}

impl Default for SnapMode {
    fn default() -> Self {
        Self::Zfs
    }
}

impl fmt::Display for SnapMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let contents = match self {
            Self::Zfs => "Zfs",
            Self::Btrfs => "Btrfs",
            Self::External => "External",
        };
        write!(f, "{}", contents)
    }
}

impl FromStr for SnapMode {
    type Err = String;
    fn from_str(s: &str) -> StdResult<Self, Self::Err> {
        Self::from_string(s).c(d!()).map_err(|e| e.to_string())
    }
}

/// Snapshot management algorithm
#[derive(Clone, Copy, Debug)]
pub enum SnapAlgo {
    /// snapshots are saved at fixed intervals
    Fair,
    /// snapshots are saved in decreasing density
    Fade,
}

impl SnapAlgo {
    #[inline(always)]
    #[allow(missing_docs)]
    pub fn from_string(m: &str) -> Result<Self> {
        match m.to_lowercase().as_str() {
            "fair" => Ok(Self::Fair),
            "fade" => Ok(Self::Fade),
            _ => Err(eg!()),
        }
    }
}

impl Default for SnapAlgo {
    fn default() -> Self {
        Self::Fair
    }
}

impl fmt::Display for SnapAlgo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let contents = match self {
            Self::Fair => "Fair",
            Self::Fade => "Fade",
        };
        write!(f, "{}", contents)
    }
}

impl FromStr for SnapAlgo {
    type Err = String;
    fn from_str(s: &str) -> StdResult<Self, Self::Err> {
        Self::from_string(s).c(d!()).map_err(|e| e.to_string())
    }
}