Skip to content

Commit 4ebf670

Browse files
author
Eason
committed
feat(cli): add version check
1 parent 2cb483c commit 4ebf670

7 files changed

Lines changed: 67 additions & 16 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ edition = "2021"
66
repository = "https://github.com/axonweb3/axon"
77

88
[dependencies]
9+
clap = { version = "4.2", features = ["cargo"] }
10+
911
# byzantine = { path = "./byzantine" }
1012
core-api = { path = "./core/api" }
1113
core-cli = { path = "./core/cli" }

common/config-parser/src/types.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ impl Config {
6161
path_state
6262
}
6363

64-
pub fn data_path_for_crosschain(&self) -> PathBuf {
65-
let mut path_state = self.data_path.clone();
66-
path_state.push("rocksdb");
67-
path_state.push("crosschain");
68-
path_state
69-
}
70-
7164
pub fn data_path_for_txs_wal(&self) -> PathBuf {
7265
let mut path_state = self.data_path.clone();
7366
path_state.push("txs_wal");
@@ -79,6 +72,12 @@ impl Config {
7972
path_state.push("consensus_wal");
8073
path_state
8174
}
75+
76+
pub fn data_path_for_version(&self) -> PathBuf {
77+
let mut path_state = self.data_path.clone();
78+
path_state.push("axon.ver");
79+
path_state
80+
}
8281
}
8382

8483
#[derive(Clone, Debug, Deserialize)]

core/cli/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ edition = "2021"
55
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
66

77
[dependencies]
8-
clap = { version = "4.0", features = ["cargo"] }
8+
clap = "4.2"
9+
semver = "1.0"
910
serde = { version = "1.0", features = ["derive"] }
1011
serde_json = "1.0"
1112

core/cli/src/lib.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
use std::fs::File;
2+
use std::io::{Read, Seek, SeekFrom, Write};
13
use std::path::Path;
24

3-
use clap::{crate_version, Arg, ArgMatches, Command};
5+
use clap::builder::{IntoResettable, Str};
6+
use clap::{Arg, ArgMatches, Command};
7+
use semver::Version;
48

59
use common_config_parser::{parse_file, types::Config};
610
use core_run::Axon;
711
use protocol::types::RichBlock;
812

913
pub struct AxonCli {
14+
version: Version,
1015
matches: ArgMatches,
1116
}
1217

1318
impl AxonCli {
14-
pub fn init() -> Self {
19+
pub fn init(ver: impl IntoResettable<Str>) -> Self {
1520
let matches = Command::new("axon")
16-
.version(crate_version!())
21+
.version(ver)
1722
.arg(
1823
Arg::new("config_path")
1924
.short('c')
@@ -30,10 +35,12 @@ impl AxonCli {
3035
.required(true)
3136
.num_args(1),
3237
)
33-
.subcommand(Command::new("run").about("Run axon process"))
34-
.get_matches();
38+
.subcommand(Command::new("run").about("Run axon process"));
3539

36-
AxonCli { matches }
40+
AxonCli {
41+
version: Version::parse(matches.get_version().unwrap()).unwrap(),
42+
matches: matches.get_matches(),
43+
}
3744
}
3845

3946
pub fn start(&self) {
@@ -50,10 +57,50 @@ impl AxonCli {
5057
)
5158
.unwrap();
5259

60+
self.check_version(&config);
61+
5362
register_log(&config);
5463

5564
Axon::new(config, genesis).run().unwrap();
5665
}
66+
67+
fn check_version(&self, config: &Config) {
68+
if !config.data_path.exists() {
69+
std::fs::create_dir_all(&config.data_path).unwrap();
70+
}
71+
72+
let f_path = config.data_path_for_version();
73+
let mut f = File::options()
74+
.create(true)
75+
.read(true)
76+
.write(true)
77+
.open(f_path)
78+
.unwrap();
79+
80+
let mut ver_str = String::new();
81+
f.read_to_string(&mut ver_str).unwrap();
82+
83+
if ver_str.is_empty() {
84+
return f.write_all(self.version.to_string().as_bytes()).unwrap();
85+
}
86+
87+
let prev_version = Version::parse(&ver_str).unwrap();
88+
if prev_version < latest_compatible_version() {
89+
println!(
90+
"The previous version {:?} is not compatible with the current version {:?}",
91+
prev_version, self.version
92+
);
93+
std::process::exit(0);
94+
}
95+
96+
f.seek(SeekFrom::Start(0)).unwrap();
97+
f.write_all(self.version.to_string().as_bytes()).unwrap();
98+
f.sync_all().unwrap();
99+
}
100+
}
101+
102+
fn latest_compatible_version() -> Version {
103+
Version::parse("0.1.0-alpha.9").unwrap()
57104
}
58105

59106
fn register_log(config: &Config) {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ use core_executor::FEE_ALLOCATOR;
88

99
pub fn run(fee_allocator: impl FeeAllocate + 'static) {
1010
FEE_ALLOCATOR.swap(Arc::new(Box::new(fee_allocator)));
11-
AxonCli::init().start();
11+
AxonCli::init(clap::crate_version!()).start();
1212
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core_cli::AxonCli;
22

33
fn main() {
4-
AxonCli::init().start();
4+
AxonCli::init(clap::crate_version!()).start();
55
}

0 commit comments

Comments
 (0)