Skip to content

Commit 9224282

Browse files
authored
Add --sync option to follow-chain. (#4023)
## Motivation Often after `follow-chain`, the user also wants to synchronize the chain. Adding this as an option allows doing that in the same command. ## Proposal Add a `--sync` option. ## Test Plan In one test I used this instead of the `sync` command. ## Release Plan - Nothing to do / These changes follow the usual release cycle. ## Links - Closes #3889. - [reviewer checklist](https://github.com/linera-io/linera-protocol/blob/main/CONTRIBUTING.md#reviewer-checklist)
1 parent eb77e86 commit 9224282

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

CLI.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,12 +857,16 @@ Request a new chain from a faucet and add it to the wallet
857857

858858
Add a new followed chain (i.e. a chain without keypair) to the wallet
859859

860-
**Usage:** `linera wallet follow-chain <CHAIN_ID>`
860+
**Usage:** `linera wallet follow-chain [OPTIONS] <CHAIN_ID>`
861861

862862
###### **Arguments:**
863863

864864
* `<CHAIN_ID>` — The chain ID
865865

866+
###### **Options:**
867+
868+
* `--sync` — Synchronize the new chain and download all its blocks from the validators
869+
866870

867871

868872
## `linera wallet forget-keys`

linera-service/src/cli/command.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,9 @@ pub enum WalletCommand {
11171117
FollowChain {
11181118
/// The chain ID.
11191119
chain_id: ChainId,
1120+
/// Synchronize the new chain and download all its blocks from the validators.
1121+
#[arg(long)]
1122+
sync: bool,
11201123
},
11211124

11221125
/// Forgets the specified chain's keys. The chain will still be followed by the

linera-service/src/cli/main.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,24 @@ impl Runnable for Job {
11781178
);
11791179
}
11801180

1181+
Wallet(WalletCommand::FollowChain {
1182+
chain_id,
1183+
sync: true,
1184+
}) => {
1185+
let mut context =
1186+
ClientContext::new(storage, options.inner.clone(), wallet, signer.into_value());
1187+
let chain_client = context.make_chain_client(chain_id);
1188+
info!("Synchronizing chain information");
1189+
let time_start = Instant::now();
1190+
chain_client.synchronize_from_validators().await?;
1191+
context.update_wallet_from_client(&chain_client).await?;
1192+
let time_total = time_start.elapsed();
1193+
info!(
1194+
"Synchronized chain information in {} ms",
1195+
time_total.as_millis()
1196+
);
1197+
}
1198+
11811199
#[cfg(feature = "benchmark")]
11821200
MultiBenchmark { .. } => {
11831201
unreachable!()
@@ -1933,7 +1951,7 @@ async fn run(options: &ClientOptions) -> Result<i32, Error> {
19331951
Ok(0)
19341952
}
19351953

1936-
WalletCommand::FollowChain { chain_id } => {
1954+
WalletCommand::FollowChain { chain_id, sync } => {
19371955
let start_time = Instant::now();
19381956
options
19391957
.wallet()
@@ -1942,6 +1960,9 @@ async fn run(options: &ClientOptions) -> Result<i32, Error> {
19421960
wallet.extend([UserChain::make_other(*chain_id, Timestamp::now())])
19431961
})
19441962
.await?;
1963+
if *sync {
1964+
options.run_with_storage(Job(options.clone())).await??;
1965+
}
19451966
info!(
19461967
"Chain followed and added in {} ms",
19471968
start_time.elapsed().as_millis()

linera-service/src/cli_wrappers/wallet.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,14 @@ impl ClientWrapper {
836836
}
837837

838838
/// Runs `linera wallet follow-chain CHAIN_ID`.
839-
pub async fn follow_chain(&self, chain_id: ChainId) -> Result<()> {
839+
pub async fn follow_chain(&self, chain_id: ChainId, sync: bool) -> Result<()> {
840840
let mut command = self.command().await?;
841841
command
842842
.args(["wallet", "follow-chain"])
843843
.arg(chain_id.to_string());
844+
if sync {
845+
command.arg("--sync");
846+
}
844847
command.spawn_and_wait_for_stdout().await?;
845848
Ok(())
846849
}

linera-service/tests/linera_net_tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3306,8 +3306,7 @@ async fn test_end_to_end_assign_greatgrandchild_chain(config: impl LineraNetConf
33063306
assert!(client2.local_balance(account2).await? > Amount::ZERO);
33073307

33083308
// Verify that a third party can also follow the chain.
3309-
client3.follow_chain(chain2).await?;
3310-
client3.sync(chain2).await?;
3309+
client3.follow_chain(chain2, true).await?;
33113310
assert!(client3.local_balance(account2).await? > Amount::ZERO);
33123311

33133312
net.ensure_is_running().await?;
@@ -3389,7 +3388,7 @@ async fn test_end_to_end_faucet(config: impl LineraNetConfig) -> Result<()> {
33893388
let chain_id = client3.request_chain(&faucet, false).await?.0;
33903389
assert!(chain_id != client3.load_wallet()?.default_chain().unwrap());
33913390
client3.forget_chain(chain_id).await?;
3392-
client3.follow_chain(chain_id).await?;
3391+
client3.follow_chain(chain_id, false).await?;
33933392

33943393
let chain3 = client3.request_chain(&faucet, true).await?.0;
33953394
assert_eq!(chain3, client3.load_wallet()?.default_chain().unwrap());

linera-service/tests/local_net_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ async fn test_end_to_end_retry_notification_stream(config: LocalNetConfig) -> Re
468468
let client2 = net.make_client().await;
469469
let mut height = 0;
470470
client2.wallet_init(None).await?;
471-
client2.follow_chain(chain).await?;
471+
client2.follow_chain(chain, false).await?;
472472

473473
// Listen for updates on root chain 0. There are no blocks on that chain yet.
474474
let port = get_node_port().await;

0 commit comments

Comments
 (0)