Skip to content

Commit 811b07e

Browse files
authored
Merge pull request #1710 from input-output-hk/ensemble/1690/remove_snapshot_command
Remove snapshot command
2 parents 7ee2ee0 + 1aa6623 commit 811b07e

File tree

12 files changed

+195
-103
lines changed

12 files changed

+195
-103
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ As a minor extension, we have adopted a slightly different versioning convention
99

1010
## Mithril Distribution [XXXX.X] - UNRELEASED
1111

12+
- **BREAKING** changes in Mithril client CLI:
13+
- The deprecated `snapshot` command is removed from the Mithril client CLI
14+
- Use the `cardano-db snapshot` command instead.
15+
1216
- Support computation of the Cardano Transactions signature and proving with the pre-computed Block Range Merkle Roots retrieved from the database.
1317

1418
- Prune Cardano Transactions from the signer database after the Block Range Merkle Roots have been computed.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/website/root/manual/developer-docs/nodes/mithril-client.md

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ This program shows, downloads and verifies certified blockchain artifacts.
126126
Usage: mithril-client [OPTIONS] <COMMAND>
127127

128128
Commands:
129-
snapshot Deprecated, use `cardano-db` instead
130129
cardano-db Cardano db management (alias: cdb)
131130
mithril-stake-distribution Mithril Stake Distribution management (alias: msd)
132131
cardano-transaction [unstable] Cardano transactions management (alias: ctx)
@@ -176,7 +175,7 @@ GENESIS_VERIFICATION_KEY=$(wget -q -O - **YOUR_GENESIS_VERIFICATION_KEY**) AGGRE
176175
To display results in JSON format for the `list` and `show` commands, simply use the `--json` (or `-j`) option:
177176

178177
```bash
179-
./mithril-client-cli snapshot list --json
178+
./mithril-client-cli cardano-db snapshot list --json
180179
```
181180

182181
:::
@@ -278,7 +277,7 @@ make docker-run
278277

279278
Here are the subcommands available:
280279

281-
### Cardano DB (previously: Snapshot)
280+
### Cardano DB
282281

283282
| Subcommand | Performed action |
284283
|------------|------------------|
@@ -287,20 +286,6 @@ Here are the subcommands available:
287286
| **snapshot list** | Lists available cardano-db snapshots|
288287
| **snapshot show** | Shows information about a cardano-db snapshot|
289288

290-
### Snapshot
291-
292-
:::warning
293-
The `snapshot` commands are now **deprecated** and has been superseded by the `cardano-db` commands.
294-
The `snapshot` commands will be removed in the near future.
295-
:::
296-
297-
| Subcommand | Performed action |
298-
|------------|------------------|
299-
| **download** | Downloads and restores a cardano-db snapshot|
300-
| **help** | Prints this message or the help for the given subcommand(s)|
301-
| **list** | Lists available cardano-db snapshots|
302-
| **show** | Shows information about a cardano-db snapshot|
303-
304289
### Mithril stake distribution
305290

306291
| Subcommand | Performed action |
@@ -339,20 +324,20 @@ Here is a list of the available parameters:
339324
| `log_format_json` | `--log-format-json` | - | - | Enable JSON output for logs | - | - | - |
340325
| `log_output` | `--log-output` | `-o` | - | Redirect the logs to a file | - | `./mithril-client.log` | - |
341326

342-
`cardano-db snapshot show` or `snapshot show` command:
327+
`cardano-db snapshot show` command:
343328

344329
| Parameter | Command line (long) | Command line (short) | Environment variable | Description | Default value | Example | Mandatory |
345330
|-----------|---------------------|:---------------------:|----------------------|-------------|---------------|---------|:---------:|
346331
| `digest` | `--digest` | - | `DIGEST` | Cardano DB digest or `latest` for the latest digest | - | - | :heavy_check_mark: |
347332
| `json` | `--json` | - | - | Enable JSON output for command results | - | - | - |
348333

349-
`cardano-db snapshot list` or `snapshot list` command:
334+
`cardano-db snapshot list` command:
350335

351336
| Parameter | Command line (long) | Command line (short) | Environment variable | Description | Default value | Example | Mandatory |
352337
|-----------|---------------------|:---------------------:|----------------------|-------------|---------------|---------|:---------:|
353338
| `json` | `--json` | - | - | Enable JSON output for command results | - | - | - |
354339

355-
`cardano-db download` or `snapshot download` command:
340+
`cardano-db download` command:
356341

357342
| Parameter | Command line (long) | Command line (short) | Environment variable | Description | Default value | Example | Mandatory |
358343
|-----------|---------------------|:---------------------:|----------------------|-------------|---------------|---------|:---------:|

mithril-client-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-client-cli"
3-
version = "0.8.2"
3+
version = "0.9.0"
44
description = "A Mithril Client"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-client-cli/src/commands/cardano_db/mod.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,47 +54,3 @@ impl CardanoDbSnapshotCommands {
5454
}
5555
}
5656
}
57-
58-
/// Legacy snapshot commands, deprecated in favor of [CardanoDbCommands].
59-
pub mod deprecated {
60-
use super::*;
61-
62-
/// Cardano db snapshot management
63-
#[derive(Subcommand, Debug, Clone)]
64-
pub enum SnapshotCommands {
65-
/// List available Cardano db snapshots
66-
#[clap(arg_required_else_help = false)]
67-
List(CardanoDbListCommand),
68-
69-
/// Show detailed information about a Cardano db snapshot
70-
#[clap(arg_required_else_help = true)]
71-
Show(CardanoDbShowCommand),
72-
73-
/// Download a Cardano db snapshot and verify its associated certificate
74-
#[clap(arg_required_else_help = true)]
75-
Download(CardanoDbDownloadCommand),
76-
}
77-
78-
impl SnapshotCommands {
79-
/// Execute snapshot command
80-
pub async fn execute(
81-
&self,
82-
config_builder: ConfigBuilder<DefaultState>,
83-
) -> MithrilResult<()> {
84-
match self {
85-
Self::List(cmd) => cmd.execute(config_builder).await,
86-
Self::Download(cmd) => cmd.execute(config_builder).await,
87-
Self::Show(cmd) => cmd.execute(config_builder).await,
88-
}
89-
}
90-
91-
/// Is JSON output enabled
92-
pub fn is_json_output_enabled(&self) -> bool {
93-
match self {
94-
Self::List(cmd) => cmd.is_json_output_enabled(),
95-
Self::Download(cmd) => cmd.is_json_output_enabled(),
96-
Self::Show(cmd) => cmd.is_json_output_enabled(),
97-
}
98-
}
99-
}
100-
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
use clap::{
2+
builder::{StyledStr, Styles},
3+
error::{ContextKind, ContextValue},
4+
};
5+
6+
use crate::ClapError;
7+
8+
/// Stores the deprecated command name and the new command name to use.
9+
#[derive(Clone)]
10+
pub struct DeprecatedCommand {
11+
command: String,
12+
new_command: String,
13+
}
14+
15+
impl DeprecatedCommand {
16+
/// Create information about a deprecated command
17+
pub fn new<S: ToString>(command: S, new_command: S) -> Self {
18+
Self {
19+
command: command.to_string(),
20+
new_command: new_command.to_string(),
21+
}
22+
}
23+
}
24+
25+
/// Tool to handle deprecated Clap commands.
26+
pub struct Deprecation;
27+
28+
impl Deprecation {
29+
fn find_deprecated_command(
30+
error: &ClapError,
31+
deprecated_commands: Vec<DeprecatedCommand>,
32+
) -> Option<DeprecatedCommand> {
33+
if let Some(context_value) = error.get(ContextKind::InvalidSubcommand) {
34+
let command_name = context_value.to_string();
35+
deprecated_commands
36+
.into_iter()
37+
.find(|dc| command_name == dc.command)
38+
} else {
39+
None
40+
}
41+
}
42+
43+
/// Modify result to add information on deprecated commands.
44+
pub fn handle_deprecated_commands<A>(
45+
matches_result: Result<A, ClapError>,
46+
styles: Styles,
47+
deprecated_commands: Vec<DeprecatedCommand>,
48+
) -> Result<A, ClapError> {
49+
matches_result.map_err(|mut e: ClapError| {
50+
if let Some(deprecated_command) = Self::find_deprecated_command(&e, deprecated_commands)
51+
{
52+
let message = format!(
53+
"'{}{}{}' command is deprecated, use '{}{}{}' command instead",
54+
styles.get_error().render(),
55+
deprecated_command.command,
56+
styles.get_error().render_reset(),
57+
styles.get_valid().render(),
58+
deprecated_command.new_command,
59+
styles.get_valid().render_reset(),
60+
);
61+
e.insert(
62+
ContextKind::Suggested,
63+
ContextValue::StyledStrs(vec![StyledStr::from(&message)]),
64+
);
65+
}
66+
e
67+
})
68+
}
69+
}
70+
71+
#[cfg(test)]
72+
mod tests {
73+
use super::*;
74+
use clap::error::{ContextKind, ContextValue, ErrorKind};
75+
use clap::{CommandFactory, Parser, Subcommand};
76+
77+
#[derive(Parser, Debug, Clone)]
78+
pub struct MyCommand {
79+
#[clap(subcommand)]
80+
command: MySubCommands,
81+
}
82+
83+
#[derive(Subcommand, Debug, Clone)]
84+
enum MySubCommands {}
85+
86+
#[test]
87+
fn invalid_sub_command_message_for_a_non_deprecated_command_is_not_modified() {
88+
fn build_error() -> Result<MyCommand, ClapError> {
89+
let mut e =
90+
ClapError::new(ErrorKind::InvalidSubcommand).with_cmd(&MyCommand::command());
91+
92+
e.insert(
93+
ContextKind::InvalidSubcommand,
94+
ContextValue::String("invalid_command".to_string()),
95+
);
96+
97+
Err(e)
98+
}
99+
100+
let default_error_message = build_error().err().unwrap().to_string();
101+
102+
let result = Deprecation::handle_deprecated_commands(
103+
build_error(),
104+
Styles::plain(),
105+
vec![DeprecatedCommand::new("old_command", "new_command")],
106+
);
107+
assert!(result.is_err());
108+
let message = result.err().unwrap().to_string();
109+
assert_eq!(default_error_message, message);
110+
}
111+
112+
#[test]
113+
fn replace_error_message_on_deprecated_commands_and_show_the_new_command() {
114+
let mut e = ClapError::new(ErrorKind::InvalidSubcommand).with_cmd(&MyCommand::command());
115+
e.insert(
116+
ContextKind::InvalidSubcommand,
117+
ContextValue::String("old_command".to_string()),
118+
);
119+
120+
let result = Deprecation::handle_deprecated_commands(
121+
Err(e) as Result<MyCommand, ClapError>,
122+
Styles::plain(),
123+
vec![DeprecatedCommand::new("old_command", "new_command")],
124+
);
125+
assert!(result.is_err());
126+
let message = result.err().unwrap().to_string();
127+
assert!(message.contains("'old_command'"));
128+
assert!(message.contains("deprecated"));
129+
assert!(message.contains("'new_command'"));
130+
}
131+
}

mithril-client-cli/src/commands/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
66
pub mod cardano_db;
77
pub mod cardano_transaction;
8+
mod deprecation;
89
pub mod mithril_stake_distribution;
910

11+
pub use deprecation::{DeprecatedCommand, Deprecation};
12+
1013
use mithril_client::{ClientBuilder, MithrilResult};
1114
use slog_scope::logger;
1215

mithril-client-cli/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
pub mod commands;
1111
mod configuration;
1212
mod utils;
13+
14+
/// Error Clap
15+
pub type ClapError = clap::error::Error;

mithril-client-cli/src/main.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use mithril_client::MithrilResult;
1515
use mithril_doc::{Documenter, GenerateDocCommands, StructDoc};
1616

1717
use mithril_client_cli::commands::{
18-
cardano_db::{deprecated::SnapshotCommands, CardanoDbCommands},
19-
cardano_transaction::CardanoTransactionCommands,
20-
mithril_stake_distribution::MithrilStakeDistributionCommands,
18+
cardano_db::CardanoDbCommands, cardano_transaction::CardanoTransactionCommands,
19+
mithril_stake_distribution::MithrilStakeDistributionCommands, DeprecatedCommand, Deprecation,
2120
};
21+
use mithril_client_cli::ClapError;
2222

2323
enum LogOutputType {
2424
StdErr,
@@ -139,6 +139,24 @@ impl Args {
139139

140140
Ok(Logger::root(Arc::new(drain), slog::o!()))
141141
}
142+
143+
fn parse_with_decorator(
144+
decorator: &dyn Fn(Result<Self, ClapError>) -> Result<Self, ClapError>,
145+
) -> Self {
146+
let result = decorator(Self::try_parse());
147+
match result {
148+
Ok(s) => s,
149+
Err(e) => e.exit(),
150+
}
151+
}
152+
153+
fn handle_deprecated_decorator(
154+
args_result: Result<Self, ClapError>,
155+
deprecated_commands: Vec<DeprecatedCommand>,
156+
) -> Result<Self, ClapError> {
157+
let styles = Args::command().get_styles().clone();
158+
Deprecation::handle_deprecated_commands(args_result, styles, deprecated_commands)
159+
}
142160
}
143161

144162
impl Source for Args {
@@ -163,11 +181,6 @@ impl Source for Args {
163181

164182
#[derive(Subcommand, Debug, Clone)]
165183
enum ArtifactCommands {
166-
/// Deprecated, use `cardano-db` instead
167-
#[clap(subcommand)]
168-
#[deprecated(since = "0.7.3", note = "use `CardanoDb` commands instead")]
169-
Snapshot(SnapshotCommands),
170-
171184
#[clap(subcommand, alias("cdb"))]
172185
CardanoDb(CardanoDbCommands),
173186

@@ -188,16 +201,6 @@ impl ArtifactCommands {
188201
config_builder: ConfigBuilder<DefaultState>,
189202
) -> MithrilResult<()> {
190203
match self {
191-
#[allow(deprecated)]
192-
Self::Snapshot(cmd) => {
193-
let message = "`snapshot` command is deprecated, use `cardano-db` instead";
194-
if cmd.is_json_output_enabled() {
195-
eprintln!(r#"{{"warning": "{}", "type": "deprecation"}}"#, message);
196-
} else {
197-
eprintln!("{}", message);
198-
};
199-
cmd.execute(config_builder).await
200-
}
201204
Self::CardanoDb(cmd) => cmd.execute(config_builder).await,
202205
Self::MithrilStakeDistribution(cmd) => cmd.execute(config_builder).await,
203206
Self::CardanoTransaction(ctx) => {
@@ -222,7 +225,12 @@ impl ArtifactCommands {
222225
#[tokio::main]
223226
async fn main() -> MithrilResult<()> {
224227
// Load args
225-
let args = Args::parse();
228+
let args = Args::parse_with_decorator(&|result: Result<Args, ClapError>| {
229+
Args::handle_deprecated_decorator(
230+
result,
231+
vec![DeprecatedCommand::new("snapshot", "cardano-db")],
232+
)
233+
});
226234
let _guard = slog_scope::set_global_logger(args.build_logger()?);
227235

228236
#[cfg(feature = "bundle_openssl")]

mithril-test-lab/mithril-end-to-end/src/assertions/check.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
attempt, utils::AttemptResult, CardanoTransactionCommand, Client, ClientCommand,
3-
MithrilStakeDistributionCommand, SnapshotCommand,
2+
attempt, utils::AttemptResult, CardanoDbCommand, CardanoTransactionCommand, Client,
3+
ClientCommand, MithrilStakeDistributionCommand,
44
};
55
use anyhow::{anyhow, Context};
66
use mithril_common::{
@@ -283,7 +283,7 @@ pub async fn assert_is_creating_certificate_with_enough_signers(
283283

284284
pub async fn assert_client_can_verify_snapshot(client: &mut Client, digest: &str) -> StdResult<()> {
285285
client
286-
.run(ClientCommand::Snapshot(SnapshotCommand::Download {
286+
.run(ClientCommand::CardanoDb(CardanoDbCommand::Download {
287287
digest: digest.to_string(),
288288
}))
289289
.await?;

0 commit comments

Comments
 (0)