Skip to content

Commit

Permalink
Merge pull request #2266 from input-output-hk/dlachaume/2246/implemen…
Browse files Browse the repository at this point in the history
…t-incremental-cardano-db-client-cli

Feat: Implement `cardano-db-v2` command in client CLI (`list` and `show`)
  • Loading branch information
dlachaume authored Feb 3, 2025
2 parents 3266e77 + 25a9fde commit 3abeead
Show file tree
Hide file tree
Showing 19 changed files with 595 additions and 64 deletions.
88 changes: 60 additions & 28 deletions .github/workflows/test-client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,25 @@ jobs:
if: runner.os != 'Windows'
shell: bash
run: |
CTX_CAPABILITY=$(wget -q -O - $AGGREGATOR_ENDPOINT | jq '.capabilities.signed_entity_types | contains(["CardanoTransactions"])')
CSD_CAPABILITY=$(wget -q -O - $AGGREGATOR_ENDPOINT | jq '.capabilities.signed_entity_types | contains(["CardanoStakeDistribution"])')
echo "ctx_enabled=$CTX_CAPABILITY" >> $GITHUB_OUTPUT
echo "csd_enabled=$CSD_CAPABILITY" >> $GITHUB_OUTPUT
CARDANO_TRANSACTIONS_CAPABILITY=$(wget -q -O - $AGGREGATOR_ENDPOINT | jq '.capabilities.signed_entity_types | contains(["CardanoTransactions"])')
CARDANO_STAKE_DISTRIBUTION_CAPABILITY=$(wget -q -O - $AGGREGATOR_ENDPOINT | jq '.capabilities.signed_entity_types | contains(["CardanoStakeDistribution"])')
CARDANO_DATABASE_V2_CAPABILITY=$(wget -q -O - $AGGREGATOR_ENDPOINT | jq '.capabilities.signed_entity_types | contains(["CardanoDatabase"])')
echo "cardano_transactions_enabled=$CARDANO_TRANSACTIONS_CAPABILITY" >> $GITHUB_OUTPUT
echo "cardano_stake_distribution_enabled=$CARDANO_STAKE_DISTRIBUTION_CAPABILITY" >> $GITHUB_OUTPUT
echo "cardano_database_v2_enabled=$CARDANO_DATABASE_V2_CAPABILITY" >> $GITHUB_OUTPUT
- name: Assessing aggregator capabilities (Windows)
id: aggregator_capability_windows
if: runner.os == 'Windows'
shell: bash
run: |
aria2c -o aggregator_capabilities.json $AGGREGATOR_ENDPOINT
CTX_CAPABILITY=$(jq '.capabilities.signed_entity_types | contains(["CardanoTransactions"])' aggregator_capabilities.json)
CSD_CAPABILITY=$(jq '.capabilities.signed_entity_types | contains(["CardanoStakeDistribution"])' aggregator_capabilities.json)
echo "ctx_enabled=$CTX_CAPABILITY" >> $GITHUB_OUTPUT
echo "csd_enabled=$CSD_CAPABILITY" >> $GITHUB_OUTPUT
CARDANO_TRANSACTIONS_CAPABILITY=$(jq '.capabilities.signed_entity_types | contains(["CardanoTransactions"])' aggregator_capabilities.json)
CARDANO_STAKE_DISTRIBUTION_CAPABILITY=$(jq '.capabilities.signed_entity_types | contains(["CardanoStakeDistribution"])' aggregator_capabilities.json)
CARDANO_DATABASE_V2_CAPABILITY=$(jq '.capabilities.signed_entity_types | contains(["CardanoDatabase"])' aggregator_capabilities.json)
echo "cardano_transactions_enabled=$CARDANO_TRANSACTIONS_CAPABILITY" >> $GITHUB_OUTPUT
echo "cardano_stake_distribution_enabled=$CARDANO_STAKE_DISTRIBUTION_CAPABILITY" >> $GITHUB_OUTPUT
echo "cardano_database_v2_enabled=$CARDANO_DATABASE_V2_CAPABILITY" >> $GITHUB_OUTPUT
- name: Checkout binary
uses: dawidd6/action-download-artifact@v6
Expand All @@ -134,14 +138,14 @@ jobs:
working-directory: ./bin
run: ./mithril-client ${{ steps.prepare.outputs.debug_level }} --version

- name: Cardano-db / list and get last digest
- name: Cardano Database Snapshot / list and get last digest
shell: bash
working-directory: ./bin
run: |
./mithril-client ${{ steps.prepare.outputs.debug_level }} cardano-db snapshot list
echo "CDB_SNAPSHOT_DIGEST=$(./mithril-client cardano-db snapshot list --json | jq -r '.[0].digest')" >> $GITHUB_ENV
- name: Cardano-db / download & restore latest
- name: Cardano Database Snapshot / download & restore latest
shell: bash
working-directory: ./bin
run: ./mithril-client ${{ steps.prepare.outputs.debug_level }} cardano-db download $CDB_SNAPSHOT_DIGEST
Expand All @@ -159,27 +163,27 @@ jobs:
run: ./mithril-client ${{ steps.prepare.outputs.debug_level }} mithril-stake-distribution download $MITHRIL_STAKE_DISTRIBUTION_HASH

- name: Cardano transaction / list and get last snapshot
if: steps.aggregator_capability_unix.outputs.ctx_enabled == 'true' || steps.aggregator_capability_windows.outputs.ctx_enabled == 'true'
if: steps.aggregator_capability_unix.outputs.cardano_transactions_enabled == 'true' || steps.aggregator_capability_windows.outputs.cardano_transactions_enabled == 'true'
shell: bash
working-directory: ./bin
run: |
./mithril-client ${{ steps.prepare.outputs.debug_level }} cardano-transaction snapshot list
echo "CTX_SNAPSHOT_HASH=$(./mithril-client cardano-transaction snapshot list --json | jq -r '.[0].hash')" >> $GITHUB_ENV
- name: Cardano transaction / show snapshot
if: steps.aggregator_capability_unix.outputs.ctx_enabled == 'true' || steps.aggregator_capability_windows.outputs.ctx_enabled == 'true'
if: steps.aggregator_capability_unix.outputs.cardano_transactions_enabled == 'true' || steps.aggregator_capability_windows.outputs.cardano_transactions_enabled == 'true'
shell: bash
working-directory: ./bin
run: ./mithril-client cardano-transaction snapshot show $CTX_SNAPSHOT_HASH

- name: Cardano transaction certify
if: steps.aggregator_capability_unix.outputs.ctx_enabled == 'true' || steps.aggregator_capability_windows.outputs.ctx_enabled == 'true'
if: steps.aggregator_capability_unix.outputs.cardano_transactions_enabled == 'true' || steps.aggregator_capability_windows.outputs.cardano_transactions_enabled == 'true'
shell: bash
working-directory: ./bin
run: ./mithril-client ${{ steps.prepare.outputs.debug_level }} cardano-transaction certify $TRANSACTIONS_HASHES_TO_CERTIFY

- name: Cardano Stake Distribution / list and get last epoch and hash
if: steps.aggregator_capability_unix.outputs.csd_enabled == 'true' || steps.aggregator_capability_windows.outputs.csd_enabled == 'true'
if: steps.aggregator_capability_unix.outputs.cardano_stake_distribution_enabled == 'true' || steps.aggregator_capability_windows.outputs.cardano_stake_distribution_enabled == 'true'
shell: bash
working-directory: ./bin
run: |
Expand All @@ -189,17 +193,31 @@ jobs:
echo "CARDANO_STAKE_DISTRIBUTION_HASH=$(echo "$CMD_OUTPUT" | jq -r '.[0].hash')" >> $GITHUB_ENV
- name: Cardano Stake Distribution / download & restore latest by epoch
if: steps.aggregator_capability_unix.outputs.csd_enabled == 'true' || steps.aggregator_capability_windows.outputs.csd_enabled == 'true'
if: steps.aggregator_capability_unix.outputs.cardano_stake_distribution_enabled == 'true' || steps.aggregator_capability_windows.outputs.cardano_stake_distribution_enabled == 'true'
shell: bash
working-directory: ./bin
run: ./mithril-client ${{ steps.prepare.outputs.debug_level }} cardano-stake-distribution download $CARDANO_STAKE_DISTRIBUTION_EPOCH

- name: Cardano Stake Distribution / download & restore latest by hash
if: steps.aggregator_capability_unix.outputs.csd_enabled == 'true' || steps.aggregator_capability_windows.outputs.csd_enabled == 'true'
if: steps.aggregator_capability_unix.outputs.cardano_stake_distribution_enabled == 'true' || steps.aggregator_capability_windows.outputs.cardano_stake_distribution_enabled == 'true'
shell: bash
working-directory: ./bin
run: ./mithril-client ${{ steps.prepare.outputs.debug_level }} cardano-stake-distribution download $CARDANO_STAKE_DISTRIBUTION_HASH

- name: Cardano Database V2 Snapshot / list and get last hash
if: steps.aggregator_capability_unix.outputs.cardano_database_v2_enabled == 'true' || steps.aggregator_capability_windows.outputs.cardano_database_v2_enabled == 'true'
shell: bash
working-directory: ./bin
run: |
./mithril-client ${{ steps.prepare.outputs.debug_level }} --unstable cardano-db-v2 snapshot list
echo "CARDANO_DATABASE_V2_SNAPSHOT_HASH=$(./mithril-client --unstable cardano-db-v2 snapshot list --json | jq -r '.[0].hash')" >> $GITHUB_ENV
- name: Cardano Database V2 Snapshot / show snapshot
if: steps.aggregator_capability_unix.outputs.cardano_database_v2_enabled == 'true' || steps.aggregator_capability_windows.outputs.cardano_database_v2_enabled == 'true'
shell: bash
working-directory: ./bin
run: ./mithril-client --unstable cardano-db-v2 snapshot show $CARDANO_DATABASE_V2_SNAPSHOT_HASH

test-docker:
strategy:
fail-fast: false
Expand All @@ -225,10 +243,12 @@ jobs:
id: aggregator_capability
shell: bash
run: |
CTX_CAPABILITY=$(wget -q -O - $AGGREGATOR_ENDPOINT | jq '.capabilities.signed_entity_types | contains(["CardanoTransactions"])')
CSD_CAPABILITY=$(wget -q -O - $AGGREGATOR_ENDPOINT | jq '.capabilities.signed_entity_types | contains(["CardanoStakeDistribution"])')
echo "ctx_enabled=$CTX_CAPABILITY" >> $GITHUB_OUTPUT
echo "csd_enabled=$CSD_CAPABILITY" >> $GITHUB_OUTPUT
CARDANO_TRANSACTIONS_CAPABILITY=$(wget -q -O - $AGGREGATOR_ENDPOINT | jq '.capabilities.signed_entity_types | contains(["CardanoTransactions"])')
CARDANO_STAKE_DISTRIBUTION_CAPABILITY=$(wget -q -O - $AGGREGATOR_ENDPOINT | jq '.capabilities.signed_entity_types | contains(["CardanoStakeDistribution"])')
CARDANO_DATABASE_V2_CAPABILITY=$(wget -q -O - $AGGREGATOR_ENDPOINT | jq '.capabilities.signed_entity_types | contains(["CardanoDatabase"])')
echo "cardano_transactions_enabled=$CARDANO_TRANSACTIONS_CAPABILITY" >> $GITHUB_OUTPUT
echo "cardano_stake_distribution_enabled=$CARDANO_STAKE_DISTRIBUTION_CAPABILITY" >> $GITHUB_OUTPUT
echo "cardano_database_v2_enabled=$CARDANO_DATABASE_V2_CAPABILITY" >> $GITHUB_OUTPUT
- name: Prepare Mithril client command
id: command
Expand All @@ -240,13 +260,13 @@ jobs:
shell: bash
run: ${{ steps.command.outputs.mithril_client }} ${{ steps.prepare.outputs.debug_level }} --version

- name: Cardano-db / list and get last digest
- name: Cardano Database Snapshot / list and get last digest
shell: bash
run: |
${{ steps.command.outputs.mithril_client }} cardano-db snapshot list
echo "CDB_SNAPSHOT_DIGEST=$(${{ steps.command.outputs.mithril_client }} cardano-db snapshot list --json | jq -r '.[0].digest')" >> $GITHUB_ENV
- name: Cardano-db / download & restore latest
- name: Cardano Database Snapshot / download & restore latest
shell: bash
run: ${{ steps.command.outputs.mithril_client }} ${{ steps.prepare.outputs.debug_level }} cardano-db download $CDB_SNAPSHOT_DIGEST --download-dir /app

Expand All @@ -261,24 +281,24 @@ jobs:
run: ${{ steps.command.outputs.mithril_client }} ${{ steps.prepare.outputs.debug_level }} mithril-stake-distribution download $MITHRIL_STAKE_DISTRIBUTION_HASH --download-dir /app

- name: Cardano transaction / list and get last snapshot
if: steps.aggregator_capability.outputs.ctx_enabled == 'true'
if: steps.aggregator_capability.outputs.cardano_transactions_enabled == 'true'
shell: bash
run: |
${{ steps.command.outputs.mithril_client }} cardano-transaction snapshot list
echo "CTX_SNAPSHOT_HASH=$(${{ steps.command.outputs.mithril_client }} cardano-transaction snapshot list --json | jq -r '.[0].hash')" >> $GITHUB_ENV
- name: Cardano transaction / show snapshot
if: steps.aggregator_capability.outputs.ctx_enabled == 'true'
if: steps.aggregator_capability.outputs.cardano_transactions_enabled == 'true'
shell: bash
run: ${{ steps.command.outputs.mithril_client }} cardano-transaction snapshot show $CTX_SNAPSHOT_HASH

- name: Cardano transaction certify
if: steps.aggregator_capability.outputs.ctx_enabled == 'true'
if: steps.aggregator_capability.outputs.cardano_transactions_enabled == 'true'
shell: bash
run: ${{ steps.command.outputs.mithril_client }} ${{ steps.prepare.outputs.debug_level }} cardano-transaction certify $TRANSACTIONS_HASHES_TO_CERTIFY

- name: Cardano Stake Distribution / list and get last epoch and hash
if: steps.aggregator_capability.outputs.csd_enabled == 'true'
if: steps.aggregator_capability.outputs.cardano_stake_distribution_enabled == 'true'
shell: bash
run: |
${{ steps.command.outputs.mithril_client }} cardano-stake-distribution list
Expand All @@ -287,15 +307,27 @@ jobs:
echo "CARDANO_STAKE_DISTRIBUTION_HASH=$(echo "$CMD_OUTPUT" | jq -r '.[0].hash')" >> $GITHUB_ENV
- name: Cardano Stake Distribution / download & restore latest by epoch
if: steps.aggregator_capability.outputs.csd_enabled == 'true'
if: steps.aggregator_capability.outputs.cardano_stake_distribution_enabled == 'true'
shell: bash
run: ${{ steps.command.outputs.mithril_client }} ${{ steps.prepare.outputs.debug_level }} cardano-stake-distribution download $CARDANO_STAKE_DISTRIBUTION_EPOCH --download-dir /app

- name: Cardano Stake Distribution / download & restore latest by hash
if: steps.aggregator_capability.outputs.csd_enabled == 'true'
if: steps.aggregator_capability.outputs.cardano_stake_distribution_enabled == 'true'
shell: bash
run: ${{ steps.command.outputs.mithril_client }} ${{ steps.prepare.outputs.debug_level }} cardano-stake-distribution download $CARDANO_STAKE_DISTRIBUTION_HASH --download-dir /app

- name: Cardano Database V2 Snapshot / list and get last digest
if: steps.aggregator_capability.outputs.cardano_database_v2_enabled == 'true'
shell: bash
run: |
${{ steps.command.outputs.mithril_client }} --unstable cardano-db-v2 snapshot list
echo "CARDANO_DATABASE_V2_SNAPSHOT_HASH=$(${{ steps.command.outputs.mithril_client }} --unstable cardano-db-v2 snapshot list --json | jq -r '.[0].hash')" >> $GITHUB_ENV
- name: Cardano Database V2 Snapshot / show snapshot
if: steps.aggregator_capability.outputs.cardano_database_v2_enabled == 'true'
shell: bash
run: ${{ steps.command.outputs.mithril_client }} --unstable cardano-db-v2 snapshot show $CARDANO_DATABASE_V2_SNAPSHOT_HASH

test-mithril-client-wasm:
strategy:
fail-fast: false
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mithril-client-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-client-cli"
version = "0.10.8"
version = "0.10.9"
description = "A Mithril Client"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion mithril-client-cli/src/commands/cardano_db/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cli_table::{format::Justify, print_stdout, Cell, Table};

use crate::{
commands::{client_builder_with_fallback_genesis_key, SharedArgs},
utils::CardanoDbUtils,
CommandContext,
};
use mithril_client::MithrilResult;
Expand Down Expand Up @@ -39,7 +40,7 @@ impl CardanoDbListCommand {
format!("{}", item.beacon.immutable_file_number).cell(),
item.network.cell(),
item.digest.cell(),
item.size.cell(),
CardanoDbUtils::format_bytes_to_gigabytes(item.size).cell(),
format!("{}", item.locations.len()).cell(),
item.created_at.to_string().cell(),
]
Expand Down
4 changes: 2 additions & 2 deletions mithril-client-cli/src/commands/cardano_db/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cli_table::{print_stdout, Cell, Table};

use crate::{
commands::{client_builder_with_fallback_genesis_key, SharedArgs},
utils::ExpanderUtils,
utils::{CardanoDbUtils, ExpanderUtils},
CommandContext,
};
use mithril_client::MithrilResult;
Expand Down Expand Up @@ -70,7 +70,7 @@ impl CardanoDbShowCommand {
vec!["Digest".cell(), cardano_db_message.digest.cell()],
vec![
"Size".cell(),
format!("{}", &cardano_db_message.size).cell(),
CardanoDbUtils::format_bytes_to_gigabytes(cardano_db_message.size).cell(),
],
vec![
"Cardano node version".cell(),
Expand Down
68 changes: 68 additions & 0 deletions mithril-client-cli/src/commands/cardano_db_v2/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use clap::Parser;
use cli_table::{format::Justify, print_stdout, Cell, Table};

use mithril_client::MithrilResult;

use crate::{
commands::{client_builder_with_fallback_genesis_key, SharedArgs},
utils::CardanoDbUtils,
CommandContext,
};

/// Clap command to list existing cardano db snapshots
#[derive(Parser, Debug, Clone)]
pub struct CardanoDbListCommand {
#[clap(flatten)]
shared_args: SharedArgs,
}

impl CardanoDbListCommand {
/// Is JSON output enabled
pub fn is_json_output_enabled(&self) -> bool {
self.shared_args.json
}

/// Main command execution
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
let params = context.config_parameters()?;
let client = client_builder_with_fallback_genesis_key(&params)?
.with_logger(context.logger().clone())
.build()?;
let items = client.cardano_database().list().await?;

if self.is_json_output_enabled() {
println!("{}", serde_json::to_string(&items)?);
} else {
let items = items
.into_iter()
.map(|item| {
vec![
format!("{}", item.beacon.epoch).cell(),
format!("{}", item.beacon.immutable_file_number).cell(),
item.hash.cell(),
item.merkle_root.cell(),
CardanoDbUtils::format_bytes_to_gigabytes(item.total_db_size_uncompressed)
.cell(),
format!("{}", item.compression_algorithm).cell(),
item.cardano_node_version.cell(),
item.created_at.to_string().cell(),
]
})
.collect::<Vec<_>>()
.table()
.title(vec![
"Epoch".cell(),
"Immutable".cell(),
"Hash".cell(),
"Merkle root".cell(),
"Database size".cell().justify(Justify::Right),
"Compression".cell(),
"Cardano node".cell(),
"Created".cell().justify(Justify::Right),
]);
print_stdout(items)?;
}

Ok(())
}
}
49 changes: 49 additions & 0 deletions mithril-client-cli/src/commands/cardano_db_v2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! Commands for the cardano db v2 artifact
mod list;
mod show;

pub use list::*;
pub use show::*;

use crate::CommandContext;
use clap::Subcommand;
use mithril_client::MithrilResult;

/// Cardano db v2 management (alias: cdbv2)
#[derive(Subcommand, Debug, Clone)]
pub enum CardanoDbV2Commands {
/// Cardano db snapshot v2 commands
#[clap(subcommand)]
Snapshot(CardanoDbV2SnapshotCommands),
}

/// Cardano db v2 snapshots
#[derive(Subcommand, Debug, Clone)]
pub enum CardanoDbV2SnapshotCommands {
/// List available cardano db v2 snapshots
#[clap(arg_required_else_help = false)]
List(CardanoDbListCommand),

/// Show detailed information about a cardano db v2 snapshot
#[clap(arg_required_else_help = true)]
Show(CardanoDbShowCommand),
}

impl CardanoDbV2Commands {
/// Execute Cardano db v2 command
pub async fn execute(&self, config_builder: CommandContext) -> MithrilResult<()> {
match self {
Self::Snapshot(cmd) => cmd.execute(config_builder).await,
}
}
}

impl CardanoDbV2SnapshotCommands {
/// Execute Cardano db v2 snapshot command
pub async fn execute(&self, config_builder: CommandContext) -> MithrilResult<()> {
match self {
Self::List(cmd) => cmd.execute(config_builder).await,
Self::Show(cmd) => cmd.execute(config_builder).await,
}
}
}
Loading

0 comments on commit 3abeead

Please sign in to comment.