Skip to content

Commit b0a794a

Browse files
authored
Create trait RunnableWithStore to simplify the database tool (#3722)
## Motivation Minimize boilerplate + clean up names follows #3724 ## Proposal * (light) refactor how `--config` is handled, notably making it a global option * create a trait `RunnableWithStore` so that the database command can be written in a single match. * `linera storage initialize` now takes a genesis config (similar to `linera-server initialize` which could be killed next) * a few minor code improvements ## Test Plan CI
1 parent 327e480 commit b0a794a

File tree

13 files changed

+432
-729
lines changed

13 files changed

+432
-729
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
run: |
5959
cargo build --features storage-service
6060
mkdir /tmp/local-linera-net
61-
cargo run --features storage-service --bin linera -- net up --storage service:tcp:localhost:1235:table --policy-config testnet --path /tmp/local-linera-net --validators 4 --shards 4 &
61+
cargo run --features storage-service --bin linera -- net up --storage service:tcp:$LINERA_STORAGE_SERVICE:table --policy-config testnet --path /tmp/local-linera-net --validators 4 --shards 4 &
6262
- name: Create two epochs and run the faucet
6363
run: |
6464
cargo build --bin linera

CLI.md

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@ This document contains the help content for the `linera` command-line program.
5151
* [`linera net up`](#linera-net-up)
5252
* [`linera net helper`](#linera-net-helper)
5353
* [`linera storage`](#linera-storage)
54-
* [`linera storage delete_all`](#linera-storage-delete_all)
55-
* [`linera storage delete_namespace`](#linera-storage-delete_namespace)
56-
* [`linera storage check_existence`](#linera-storage-check_existence)
57-
* [`linera storage check_absence`](#linera-storage-check_absence)
54+
* [`linera storage delete-all`](#linera-storage-delete-all)
55+
* [`linera storage delete-namespace`](#linera-storage-delete-namespace)
56+
* [`linera storage check-existence`](#linera-storage-check-existence)
5857
* [`linera storage initialize`](#linera-storage-initialize)
59-
* [`linera storage list_namespaces`](#linera-storage-list_namespaces)
60-
* [`linera storage list_blob_ids`](#linera-storage-list_blob_ids)
61-
* [`linera storage list_chain_ids`](#linera-storage-list_chain_ids)
58+
* [`linera storage list-namespaces`](#linera-storage-list-namespaces)
59+
* [`linera storage list-blob-ids`](#linera-storage-list-blob-ids)
60+
* [`linera storage list-chain-ids`](#linera-storage-list-chain-ids)
6261

6362
## `linera`
6463

@@ -982,7 +981,6 @@ Start a Local Linera Network
982981
Default value: `10`
983982
* `--testing-prng-seed <TESTING_PRNG_SEED>` — Force this wallet to generate keys using a PRNG and a given seed. USE FOR TESTING ONLY
984983
* `--path <PATH>` — Run with a specific path where the wallet and validator input files are. If none, then a temporary directory is created
985-
* `--storage <STORAGE>` — Run with a specific storage. If none, then a linera-storage-service is started on a random free port
986984
* `--external-protocol <EXTERNAL_PROTOCOL>` — External protocol used, either `grpc` or `grpcs`
987985

988986
Default value: `grpc`
@@ -1015,110 +1013,73 @@ Operation on the storage
10151013

10161014
###### **Subcommands:**
10171015

1018-
* `delete_all` — Delete all the namespaces in the database
1019-
* `delete_namespace` — Delete a single namespace from the database
1020-
* `check_existence` — Check existence of a namespace in the database
1021-
* `check_absence` — Check absence of a namespace in the database
1016+
* `delete-all` — Delete all the namespaces in the database
1017+
* `delete-namespace` — Delete a single namespace from the database
1018+
* `check-existence` — Check existence of a namespace in the database
10221019
* `initialize` — Initialize a namespace in the database
1023-
* `list_namespaces` — List the namespaces in the database
1024-
* `list_blob_ids` — List the blob IDs in the database
1025-
* `list_chain_ids` — List the chain IDs in the database
1020+
* `list-namespaces` — List the namespaces in the database
1021+
* `list-blob-ids` — List the blob IDs in the database
1022+
* `list-chain-ids` — List the chain IDs in the database
10261023

10271024

10281025

1029-
## `linera storage delete_all`
1026+
## `linera storage delete-all`
10301027

10311028
Delete all the namespaces in the database
10321029

1033-
**Usage:** `linera storage delete_all --storage <STORAGE_CONFIG>`
1030+
**Usage:** `linera storage delete-all`
10341031

1035-
###### **Options:**
1036-
1037-
* `--storage <STORAGE_CONFIG>` — Storage configuration for the blockchain history
10381032

10391033

1040-
1041-
## `linera storage delete_namespace`
1034+
## `linera storage delete-namespace`
10421035

10431036
Delete a single namespace from the database
10441037

1045-
**Usage:** `linera storage delete_namespace --storage <STORAGE_CONFIG>`
1038+
**Usage:** `linera storage delete-namespace`
10461039

1047-
###### **Options:**
1048-
1049-
* `--storage <STORAGE_CONFIG>` — Storage configuration for the blockchain history
10501040

10511041

1052-
1053-
## `linera storage check_existence`
1042+
## `linera storage check-existence`
10541043

10551044
Check existence of a namespace in the database
10561045

1057-
**Usage:** `linera storage check_existence --storage <STORAGE_CONFIG>`
1058-
1059-
###### **Options:**
1060-
1061-
* `--storage <STORAGE_CONFIG>` — Storage configuration for the blockchain history
1062-
1063-
1064-
1065-
## `linera storage check_absence`
1066-
1067-
Check absence of a namespace in the database
1068-
1069-
**Usage:** `linera storage check_absence --storage <STORAGE_CONFIG>`
1070-
1071-
###### **Options:**
1072-
1073-
* `--storage <STORAGE_CONFIG>` — Storage configuration for the blockchain history
1046+
**Usage:** `linera storage check-existence`
10741047

10751048

10761049

10771050
## `linera storage initialize`
10781051

10791052
Initialize a namespace in the database
10801053

1081-
**Usage:** `linera storage initialize --storage <STORAGE_CONFIG>`
1054+
**Usage:** `linera storage initialize --genesis <GENESIS_CONFIG_PATH>`
10821055

10831056
###### **Options:**
10841057

1085-
* `--storage <STORAGE_CONFIG>` — Storage configuration for the blockchain history
1058+
* `--genesis <GENESIS_CONFIG_PATH>`
10861059

10871060

10881061

1089-
## `linera storage list_namespaces`
1062+
## `linera storage list-namespaces`
10901063

10911064
List the namespaces in the database
10921065

1093-
**Usage:** `linera storage list_namespaces --storage <STORAGE_CONFIG>`
1094-
1095-
###### **Options:**
1096-
1097-
* `--storage <STORAGE_CONFIG>` — Storage configuration for the blockchain history
1066+
**Usage:** `linera storage list-namespaces`
10981067

10991068

11001069

1101-
## `linera storage list_blob_ids`
1070+
## `linera storage list-blob-ids`
11021071

11031072
List the blob IDs in the database
11041073

1105-
**Usage:** `linera storage list_blob_ids --storage <STORAGE_CONFIG>`
1106-
1107-
###### **Options:**
1108-
1109-
* `--storage <STORAGE_CONFIG>` — Storage configuration for the blockchain history
1074+
**Usage:** `linera storage list-blob-ids`
11101075

11111076

11121077

1113-
## `linera storage list_chain_ids`
1078+
## `linera storage list-chain-ids`
11141079

11151080
List the chain IDs in the database
11161081

1117-
**Usage:** `linera storage list_chain_ids --storage <STORAGE_CONFIG>`
1118-
1119-
###### **Options:**
1120-
1121-
* `--storage <STORAGE_CONFIG>` — Storage configuration for the blockchain history
1082+
**Usage:** `linera storage list-chain-ids`
11221083

11231084

11241085

docker/compose-server-init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22

33
while true; do
4-
./linera storage check_existence --storage "scylladb:tcp:scylla:9042"
4+
./linera storage check-existence --storage "scylladb:tcp:scylla:9042"
55
status=$?
66

77
if [ $status -eq 0 ]; then

docker/proxy-init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22

33
while true; do
4-
./linera storage check_existence --storage "scylladb:tcp:scylla-client.scylla.svc.cluster.local:9042"
4+
./linera storage check-existence --storage "scylladb:tcp:scylla-client.scylla.svc.cluster.local:9042"
55
status=$?
66

77
if [ "$status" -eq 0 ]; then

docker/server-init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22

33
while true; do
4-
./linera storage check_existence --storage "scylladb:tcp:scylla-client.scylla.svc.cluster.local:9042"
4+
./linera storage check-existence --storage "scylladb:tcp:scylla-client.scylla.svc.cluster.local:9042"
55
status=$?
66

77
if [ $status -eq 0 ]; then

linera-service/src/linera/command.rs

Lines changed: 58 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Zefchain Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::{num::NonZeroU16, path::PathBuf};
4+
use std::{borrow::Cow, num::NonZeroU16, path::PathBuf};
55

66
use chrono::{DateTime, Utc};
77
use linera_base::{
@@ -19,9 +19,8 @@ use linera_client::{
1919
util,
2020
};
2121
use linera_rpc::config::CrossChainConfig;
22-
use linera_service::{
23-
storage::StorageConfigNamespace,
24-
util::{DEFAULT_PAUSE_AFTER_GQL_MUTATIONS_SECS, DEFAULT_PAUSE_AFTER_LINERA_SERVICE_SECS},
22+
use linera_service::util::{
23+
DEFAULT_PAUSE_AFTER_GQL_MUTATIONS_SECS, DEFAULT_PAUSE_AFTER_LINERA_SERVICE_SECS,
2524
};
2625

2726
#[derive(Clone, clap::Subcommand)]
@@ -784,87 +783,78 @@ pub enum ClientCommand {
784783
},
785784
}
786785

786+
impl ClientCommand {
787+
/// Returns the log file name to use based on the [`ClientCommand`] that will run.
788+
pub fn log_file_name(&self) -> Cow<'static, str> {
789+
match self {
790+
ClientCommand::Transfer { .. }
791+
| ClientCommand::OpenChain { .. }
792+
| ClientCommand::OpenMultiOwnerChain { .. }
793+
| ClientCommand::ChangeOwnership { .. }
794+
| ClientCommand::ChangeApplicationPermissions { .. }
795+
| ClientCommand::CloseChain { .. }
796+
| ClientCommand::LocalBalance { .. }
797+
| ClientCommand::QueryBalance { .. }
798+
| ClientCommand::SyncBalance { .. }
799+
| ClientCommand::Sync { .. }
800+
| ClientCommand::ProcessInbox { .. }
801+
| ClientCommand::QueryValidator { .. }
802+
| ClientCommand::QueryValidators { .. }
803+
| ClientCommand::SyncValidator { .. }
804+
| ClientCommand::SetValidator { .. }
805+
| ClientCommand::RemoveValidator { .. }
806+
| ClientCommand::ResourceControlPolicy { .. }
807+
| ClientCommand::FinalizeCommittee
808+
| ClientCommand::CreateGenesisConfig { .. }
809+
| ClientCommand::PublishModule { .. }
810+
| ClientCommand::PublishDataBlob { .. }
811+
| ClientCommand::ReadDataBlob { .. }
812+
| ClientCommand::CreateApplication { .. }
813+
| ClientCommand::PublishAndCreate { .. }
814+
| ClientCommand::Keygen
815+
| ClientCommand::Assign { .. }
816+
| ClientCommand::Wallet { .. }
817+
| ClientCommand::RetryPendingBlock { .. } => "client".into(),
818+
#[cfg(feature = "benchmark")]
819+
ClientCommand::Benchmark { .. } => "benchmark".into(),
820+
ClientCommand::Net { .. } => "net".into(),
821+
ClientCommand::Project { .. } => "project".into(),
822+
ClientCommand::Watch { .. } => "watch".into(),
823+
ClientCommand::Storage { .. } => "storage".into(),
824+
ClientCommand::Service { port, .. } => format!("service-{port}").into(),
825+
ClientCommand::Faucet { .. } => "faucet".into(),
826+
ClientCommand::HelpMarkdown | ClientCommand::ExtractScriptFromMarkdown { .. } => {
827+
"tool".into()
828+
}
829+
}
830+
}
831+
}
832+
787833
#[derive(Clone, clap::Parser)]
788834
pub enum DatabaseToolCommand {
789835
/// Delete all the namespaces in the database
790-
#[command(name = "delete_all")]
791-
DeleteAll {
792-
/// Storage configuration for the blockchain history.
793-
#[arg(long = "storage")]
794-
storage_config: String,
795-
},
836+
DeleteAll,
796837

797838
/// Delete a single namespace from the database
798-
#[command(name = "delete_namespace")]
799-
DeleteNamespace {
800-
/// Storage configuration for the blockchain history.
801-
#[arg(long = "storage")]
802-
storage_config: String,
803-
},
839+
DeleteNamespace,
804840

805841
/// Check existence of a namespace in the database
806-
#[command(name = "check_existence")]
807-
CheckExistence {
808-
/// Storage configuration for the blockchain history.
809-
#[arg(long = "storage")]
810-
storage_config: String,
811-
},
812-
813-
/// Check absence of a namespace in the database
814-
#[command(name = "check_absence")]
815-
CheckAbsence {
816-
/// Storage configuration for the blockchain history.
817-
#[arg(long = "storage")]
818-
storage_config: String,
819-
},
842+
CheckExistence,
820843

821844
/// Initialize a namespace in the database
822-
#[command(name = "initialize")]
823845
Initialize {
824-
/// Storage configuration for the blockchain history.
825-
#[arg(long = "storage")]
826-
storage_config: String,
846+
#[arg(long = "genesis")]
847+
genesis_config_path: PathBuf,
827848
},
828849

829850
/// List the namespaces in the database
830-
#[command(name = "list_namespaces")]
831-
ListNamespaces {
832-
/// Storage configuration for the blockchain history.
833-
#[arg(long = "storage")]
834-
storage_config: String,
835-
},
851+
ListNamespaces,
836852

837853
/// List the blob IDs in the database
838-
#[command(name = "list_blob_ids")]
839-
ListBlobIds {
840-
/// Storage configuration for the blockchain history.
841-
#[arg(long = "storage")]
842-
storage_config: String,
843-
},
854+
ListBlobIds,
844855

845856
/// List the chain IDs in the database
846-
#[command(name = "list_chain_ids")]
847-
ListChainIds {
848-
/// Storage configuration for the blockchain history.
849-
#[arg(long = "storage")]
850-
storage_config: String,
851-
},
852-
}
853-
854-
impl DatabaseToolCommand {
855-
pub fn storage_config(&self) -> Result<StorageConfigNamespace, anyhow::Error> {
856-
let storage_config = match self {
857-
DatabaseToolCommand::DeleteAll { storage_config } => storage_config,
858-
DatabaseToolCommand::DeleteNamespace { storage_config } => storage_config,
859-
DatabaseToolCommand::CheckExistence { storage_config } => storage_config,
860-
DatabaseToolCommand::CheckAbsence { storage_config } => storage_config,
861-
DatabaseToolCommand::Initialize { storage_config } => storage_config,
862-
DatabaseToolCommand::ListNamespaces { storage_config } => storage_config,
863-
DatabaseToolCommand::ListBlobIds { storage_config } => storage_config,
864-
DatabaseToolCommand::ListChainIds { storage_config } => storage_config,
865-
};
866-
storage_config.parse::<StorageConfigNamespace>()
867-
}
857+
ListChainIds,
868858
}
869859

870860
#[allow(clippy::large_enum_variant)]
@@ -933,11 +923,6 @@ pub enum NetCommand {
933923
#[arg(long)]
934924
path: Option<String>,
935925

936-
/// Run with a specific storage.
937-
/// If none, then a linera-storage-service is started on a random free port.
938-
#[arg(long)]
939-
storage: Option<String>,
940-
941926
/// External protocol used, either `grpc` or `grpcs`.
942927
#[arg(long, default_value = "grpc")]
943928
external_protocol: String,

0 commit comments

Comments
 (0)