linera_service/cli_wrappers/
wallet.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use std::{
5    borrow::Cow,
6    collections::BTreeMap,
7    env,
8    marker::PhantomData,
9    mem,
10    path::{Path, PathBuf},
11    pin::Pin,
12    process::Stdio,
13    str::FromStr,
14    sync,
15    time::Duration,
16};
17
18use anyhow::{bail, ensure, Context, Result};
19use async_graphql::InputType;
20use async_tungstenite::tungstenite::{client::IntoClientRequest as _, http::HeaderValue};
21use futures::{SinkExt as _, Stream, StreamExt as _, TryStreamExt as _};
22use heck::ToKebabCase;
23use linera_base::{
24    abi::ContractAbi,
25    command::{resolve_binary, CommandExt},
26    crypto::{CryptoHash, InMemorySigner},
27    data_types::{Amount, BlockHeight, Bytecode, Epoch},
28    identifiers::{
29        Account, AccountOwner, ApplicationId, ChainId, IndexAndEvent, ModuleId, StreamId,
30    },
31    vm::VmRuntime,
32};
33use linera_client::client_options::ResourceControlPolicyConfig;
34use linera_core::worker::Notification;
35use linera_execution::committee::Committee;
36use linera_faucet_client::Faucet;
37use serde::{de::DeserializeOwned, ser::Serialize};
38use serde_command_opts::to_args;
39use serde_json::{json, Value};
40use tempfile::TempDir;
41use tokio::{
42    io::{AsyncBufReadExt, BufReader},
43    process::{Child, Command},
44    sync::oneshot,
45    task::JoinHandle,
46};
47#[cfg(with_testing)]
48use {
49    futures::FutureExt as _,
50    linera_core::worker::Reason,
51    std::{collections::BTreeSet, future::Future},
52};
53
54use crate::{
55    cli::command::BenchmarkCommand,
56    cli_wrappers::{
57        local_net::{PathProvider, ProcessInbox},
58        Network,
59    },
60    util::{self, ChildExt},
61    wallet::Wallet,
62};
63
64/// The name of the environment variable that allows specifying additional arguments to be passed
65/// to the node-service command of the client.
66const CLIENT_SERVICE_ENV: &str = "LINERA_CLIENT_SERVICE_PARAMS";
67
68fn reqwest_client() -> reqwest::Client {
69    reqwest::ClientBuilder::new()
70        .timeout(Duration::from_secs(30))
71        .build()
72        .unwrap()
73}
74
75/// Wrapper to run a Linera client command.
76pub struct ClientWrapper {
77    binary_path: sync::Mutex<Option<PathBuf>>,
78    testing_prng_seed: Option<u64>,
79    storage: String,
80    wallet: String,
81    keystore: String,
82    max_pending_message_bundles: usize,
83    network: Network,
84    pub path_provider: PathProvider,
85    on_drop: OnClientDrop,
86    extra_args: Vec<String>,
87}
88
89/// Action to perform when the [`ClientWrapper`] is dropped.
90#[derive(Clone, Copy, Debug, Eq, PartialEq)]
91pub enum OnClientDrop {
92    /// Close all the chains on the wallet.
93    CloseChains,
94    /// Do not close any chains, leaving them active.
95    LeakChains,
96}
97
98impl ClientWrapper {
99    pub fn new(
100        path_provider: PathProvider,
101        network: Network,
102        testing_prng_seed: Option<u64>,
103        id: usize,
104        on_drop: OnClientDrop,
105    ) -> Self {
106        Self::new_with_extra_args(
107            path_provider,
108            network,
109            testing_prng_seed,
110            id,
111            on_drop,
112            vec!["--wait-for-outgoing-messages".to_string()],
113        )
114    }
115
116    pub fn new_with_extra_args(
117        path_provider: PathProvider,
118        network: Network,
119        testing_prng_seed: Option<u64>,
120        id: usize,
121        on_drop: OnClientDrop,
122        extra_args: Vec<String>,
123    ) -> Self {
124        let storage = format!(
125            "rocksdb:{}/client_{}.db",
126            path_provider.path().display(),
127            id
128        );
129        let wallet = format!("wallet_{}.json", id);
130        let keystore = format!("keystore_{}.json", id);
131        Self {
132            binary_path: sync::Mutex::new(None),
133            testing_prng_seed,
134            storage,
135            wallet,
136            keystore,
137            max_pending_message_bundles: 10_000,
138            network,
139            path_provider,
140            on_drop,
141            extra_args,
142        }
143    }
144
145    /// Runs `linera project new`.
146    pub async fn project_new(&self, project_name: &str, linera_root: &Path) -> Result<TempDir> {
147        let tmp = TempDir::new()?;
148        let mut command = self.command().await?;
149        command
150            .current_dir(tmp.path())
151            .arg("project")
152            .arg("new")
153            .arg(project_name)
154            .arg("--linera-root")
155            .arg(linera_root)
156            .spawn_and_wait_for_stdout()
157            .await?;
158        Ok(tmp)
159    }
160
161    /// Runs `linera project publish`.
162    pub async fn project_publish<T: Serialize>(
163        &self,
164        path: PathBuf,
165        required_application_ids: Vec<String>,
166        publisher: impl Into<Option<ChainId>>,
167        argument: &T,
168    ) -> Result<String> {
169        let json_parameters = serde_json::to_string(&())?;
170        let json_argument = serde_json::to_string(argument)?;
171        let mut command = self.command().await?;
172        command
173            .arg("project")
174            .arg("publish-and-create")
175            .arg(path)
176            .args(publisher.into().iter().map(ChainId::to_string))
177            .args(["--json-parameters", &json_parameters])
178            .args(["--json-argument", &json_argument]);
179        if !required_application_ids.is_empty() {
180            command.arg("--required-application-ids");
181            command.args(required_application_ids);
182        }
183        let stdout = command.spawn_and_wait_for_stdout().await?;
184        Ok(stdout.trim().to_string())
185    }
186
187    /// Runs `linera project test`.
188    pub async fn project_test(&self, path: &Path) -> Result<()> {
189        self.command()
190            .await
191            .context("failed to create project test command")?
192            .current_dir(path)
193            .arg("project")
194            .arg("test")
195            .spawn_and_wait_for_stdout()
196            .await?;
197        Ok(())
198    }
199
200    async fn command_with_envs_and_arguments(
201        &self,
202        envs: &[(&str, &str)],
203        arguments: impl IntoIterator<Item = Cow<'_, str>>,
204    ) -> Result<Command> {
205        let mut command = self.command_binary().await?;
206        command.current_dir(self.path_provider.path());
207        for (key, value) in envs {
208            command.env(key, value);
209        }
210        for argument in arguments {
211            command.arg(&*argument);
212        }
213        Ok(command)
214    }
215
216    async fn command_with_envs(&self, envs: &[(&str, &str)]) -> Result<Command> {
217        self.command_with_envs_and_arguments(envs, self.command_arguments())
218            .await
219    }
220
221    async fn command_with_arguments(
222        &self,
223        arguments: impl IntoIterator<Item = Cow<'_, str>>,
224    ) -> Result<Command> {
225        self.command_with_envs_and_arguments(
226            &[(
227                "RUST_LOG",
228                &std::env::var("RUST_LOG").unwrap_or(String::from("linera=debug")),
229            )],
230            arguments,
231        )
232        .await
233    }
234
235    async fn command(&self) -> Result<Command> {
236        self.command_with_envs(&[(
237            "RUST_LOG",
238            &std::env::var("RUST_LOG").unwrap_or(String::from("linera=debug")),
239        )])
240        .await
241    }
242
243    fn required_command_arguments(&self) -> impl Iterator<Item = Cow<'_, str>> + '_ {
244        [
245            "--wallet".into(),
246            self.wallet.as_str().into(),
247            "--keystore".into(),
248            self.keystore.as_str().into(),
249            "--storage".into(),
250            self.storage.as_str().into(),
251            "--send-timeout-ms".into(),
252            "500000".into(),
253            "--recv-timeout-ms".into(),
254            "500000".into(),
255        ]
256        .into_iter()
257        .chain(self.extra_args.iter().map(|s| s.as_str().into()))
258    }
259
260    /// Returns an iterator over the arguments that should be added to all command invocations.
261    fn command_arguments(&self) -> impl Iterator<Item = Cow<'_, str>> + '_ {
262        self.required_command_arguments().chain([
263            "--max-pending-message-bundles".into(),
264            self.max_pending_message_bundles.to_string().into(),
265        ])
266    }
267
268    /// Returns the [`Command`] instance configured to run the appropriate binary.
269    ///
270    /// The path is resolved once and cached inside `self` for subsequent usages.
271    async fn command_binary(&self) -> Result<Command> {
272        match self.command_with_cached_binary_path() {
273            Some(command) => Ok(command),
274            None => {
275                let resolved_path = resolve_binary("linera", env!("CARGO_PKG_NAME")).await?;
276                let command = Command::new(&resolved_path);
277
278                self.set_cached_binary_path(resolved_path);
279
280                Ok(command)
281            }
282        }
283    }
284
285    /// Returns a [`Command`] instance configured with the cached `binary_path`, if available.
286    fn command_with_cached_binary_path(&self) -> Option<Command> {
287        let binary_path = self.binary_path.lock().unwrap();
288
289        binary_path.as_ref().map(Command::new)
290    }
291
292    /// Sets the cached `binary_path` with the `new_binary_path`.
293    ///
294    /// # Panics
295    ///
296    /// If the cache is already set to a different value. In theory the two threads calling
297    /// `command_binary` can race and resolve the binary path twice, but they should always be the
298    /// same path.
299    fn set_cached_binary_path(&self, new_binary_path: PathBuf) {
300        let mut binary_path = self.binary_path.lock().unwrap();
301
302        if binary_path.is_none() {
303            *binary_path = Some(new_binary_path);
304        } else {
305            assert_eq!(*binary_path, Some(new_binary_path));
306        }
307    }
308
309    /// Runs `linera create-genesis-config`.
310    pub async fn create_genesis_config(
311        &self,
312        num_other_initial_chains: u32,
313        initial_funding: Amount,
314        policy_config: ResourceControlPolicyConfig,
315        http_allow_list: Option<Vec<String>>,
316    ) -> Result<()> {
317        let mut command = self.command().await?;
318        command
319            .args([
320                "create-genesis-config",
321                &num_other_initial_chains.to_string(),
322            ])
323            .args(["--initial-funding", &initial_funding.to_string()])
324            .args(["--committee", "committee.json"])
325            .args(["--genesis", "genesis.json"])
326            .args([
327                "--policy-config",
328                &policy_config.to_string().to_kebab_case(),
329            ]);
330        if let Some(allow_list) = http_allow_list {
331            command
332                .arg("--http-request-allow-list")
333                .arg(allow_list.join(","));
334        }
335        if let Some(seed) = self.testing_prng_seed {
336            command.arg("--testing-prng-seed").arg(seed.to_string());
337        }
338        command.spawn_and_wait_for_stdout().await?;
339        Ok(())
340    }
341
342    /// Runs `linera wallet init`. The genesis config is read from `genesis.json`, or from the
343    /// faucet if provided.
344    pub async fn wallet_init(&self, faucet: Option<&'_ Faucet>) -> Result<()> {
345        let mut command = self.command().await?;
346        command.args(["wallet", "init"]);
347        match faucet {
348            None => command.args(["--genesis", "genesis.json"]),
349            Some(faucet) => command.args(["--faucet", faucet.url()]),
350        };
351        if let Some(seed) = self.testing_prng_seed {
352            command.arg("--testing-prng-seed").arg(seed.to_string());
353        }
354        command.spawn_and_wait_for_stdout().await?;
355        Ok(())
356    }
357
358    /// Runs `linera wallet request-chain`.
359    pub async fn request_chain(
360        &self,
361        faucet: &Faucet,
362        set_default: bool,
363    ) -> Result<(ChainId, AccountOwner)> {
364        let mut command = self.command().await?;
365        command.args(["wallet", "request-chain", "--faucet", faucet.url()]);
366        if set_default {
367            command.arg("--set-default");
368        }
369        let stdout = command.spawn_and_wait_for_stdout().await?;
370        let mut lines = stdout.split_whitespace();
371        let chain_id: ChainId = lines.next().context("missing chain ID")?.parse()?;
372        let owner = lines.next().context("missing chain owner")?.parse()?;
373        Ok((chain_id, owner))
374    }
375
376    /// Runs `linera wallet publish-and-create`.
377    #[expect(clippy::too_many_arguments)]
378    pub async fn publish_and_create<
379        A: ContractAbi,
380        Parameters: Serialize,
381        InstantiationArgument: Serialize,
382    >(
383        &self,
384        contract: PathBuf,
385        service: PathBuf,
386        vm_runtime: VmRuntime,
387        parameters: &Parameters,
388        argument: &InstantiationArgument,
389        required_application_ids: &[ApplicationId],
390        publisher: impl Into<Option<ChainId>>,
391    ) -> Result<ApplicationId<A>> {
392        let json_parameters = serde_json::to_string(parameters)?;
393        let json_argument = serde_json::to_string(argument)?;
394        let mut command = self.command().await?;
395        let vm_runtime = format!("{}", vm_runtime);
396        command
397            .arg("publish-and-create")
398            .args([contract, service])
399            .args(["--vm-runtime", &vm_runtime.to_lowercase()])
400            .args(publisher.into().iter().map(ChainId::to_string))
401            .args(["--json-parameters", &json_parameters])
402            .args(["--json-argument", &json_argument]);
403        if !required_application_ids.is_empty() {
404            command.arg("--required-application-ids");
405            command.args(
406                required_application_ids
407                    .iter()
408                    .map(ApplicationId::to_string),
409            );
410        }
411        let stdout = command.spawn_and_wait_for_stdout().await?;
412        Ok(stdout.trim().parse::<ApplicationId>()?.with_abi())
413    }
414
415    /// Runs `linera publish-module`.
416    pub async fn publish_module<Abi, Parameters, InstantiationArgument>(
417        &self,
418        contract: PathBuf,
419        service: PathBuf,
420        vm_runtime: VmRuntime,
421        publisher: impl Into<Option<ChainId>>,
422    ) -> Result<ModuleId<Abi, Parameters, InstantiationArgument>> {
423        let stdout = self
424            .command()
425            .await?
426            .arg("publish-module")
427            .args([contract, service])
428            .args(["--vm-runtime", &format!("{}", vm_runtime).to_lowercase()])
429            .args(publisher.into().iter().map(ChainId::to_string))
430            .spawn_and_wait_for_stdout()
431            .await?;
432        let module_id: ModuleId = stdout.trim().parse()?;
433        Ok(module_id.with_abi())
434    }
435
436    /// Runs `linera create-application`.
437    pub async fn create_application<
438        Abi: ContractAbi,
439        Parameters: Serialize,
440        InstantiationArgument: Serialize,
441    >(
442        &self,
443        module_id: &ModuleId<Abi, Parameters, InstantiationArgument>,
444        parameters: &Parameters,
445        argument: &InstantiationArgument,
446        required_application_ids: &[ApplicationId],
447        creator: impl Into<Option<ChainId>>,
448    ) -> Result<ApplicationId<Abi>> {
449        let json_parameters = serde_json::to_string(parameters)?;
450        let json_argument = serde_json::to_string(argument)?;
451        let mut command = self.command().await?;
452        command
453            .arg("create-application")
454            .arg(module_id.forget_abi().to_string())
455            .args(["--json-parameters", &json_parameters])
456            .args(["--json-argument", &json_argument])
457            .args(creator.into().iter().map(ChainId::to_string));
458        if !required_application_ids.is_empty() {
459            command.arg("--required-application-ids");
460            command.args(
461                required_application_ids
462                    .iter()
463                    .map(ApplicationId::to_string),
464            );
465        }
466        let stdout = command.spawn_and_wait_for_stdout().await?;
467        Ok(stdout.trim().parse::<ApplicationId>()?.with_abi())
468    }
469
470    /// Runs `linera service`.
471    pub async fn run_node_service(
472        &self,
473        port: impl Into<Option<u16>>,
474        process_inbox: ProcessInbox,
475    ) -> Result<NodeService> {
476        self.run_node_service_with_options(port, process_inbox, &[], &[], false)
477            .await
478    }
479
480    /// Runs `linera service` with optional task processor configuration.
481    pub async fn run_node_service_with_options(
482        &self,
483        port: impl Into<Option<u16>>,
484        process_inbox: ProcessInbox,
485        operator_application_ids: &[ApplicationId],
486        operators: &[(String, PathBuf)],
487        read_only: bool,
488    ) -> Result<NodeService> {
489        let port = port.into().unwrap_or(8080);
490        let mut command = self.command().await?;
491        command.arg("service");
492        if let ProcessInbox::Skip = process_inbox {
493            command.arg("--listener-skip-process-inbox");
494        }
495        if let Ok(var) = env::var(CLIENT_SERVICE_ENV) {
496            command.args(var.split_whitespace());
497        }
498        for app_id in operator_application_ids {
499            command.args(["--operator-application-ids", &app_id.to_string()]);
500        }
501        for (name, path) in operators {
502            command.args(["--operators", &format!("{}={}", name, path.display())]);
503        }
504        if read_only {
505            command.arg("--read-only");
506        }
507        let child = command
508            .args(["--port".to_string(), port.to_string()])
509            .spawn_into()?;
510        let client = reqwest_client();
511        for i in 0..10 {
512            linera_base::time::timer::sleep(Duration::from_secs(i)).await;
513            let request = client
514                .get(format!("http://localhost:{}/", port))
515                .send()
516                .await;
517            if request.is_ok() {
518                tracing::info!("Node service has started");
519                return Ok(NodeService::new(port, child));
520            } else {
521                tracing::warn!("Waiting for node service to start");
522            }
523        }
524        bail!("Failed to start node service");
525    }
526
527    /// Runs `linera validator query`
528    pub async fn query_validator(&self, address: &str) -> Result<CryptoHash> {
529        let mut command = self.command().await?;
530        command.arg("validator").arg("query").arg(address);
531        let stdout = command.spawn_and_wait_for_stdout().await?;
532
533        // Parse the genesis config hash from the output.
534        // It's on a line like "Genesis config hash: <hash>"
535        let hash = stdout
536            .lines()
537            .find_map(|line| {
538                line.strip_prefix("Genesis config hash: ")
539                    .and_then(|hash_str| hash_str.trim().parse().ok())
540            })
541            .context("error while parsing the result of `linera validator query`")?;
542        Ok(hash)
543    }
544
545    /// Runs `linera validator list`.
546    pub async fn query_validators(&self, chain_id: Option<ChainId>) -> Result<()> {
547        let mut command = self.command().await?;
548        command.arg("validator").arg("list");
549        if let Some(chain_id) = chain_id {
550            command.args(["--chain-id", &chain_id.to_string()]);
551        }
552        command.spawn_and_wait_for_stdout().await?;
553        Ok(())
554    }
555
556    /// Runs `linera sync-validator`.
557    pub async fn sync_validator(
558        &self,
559        chain_ids: impl IntoIterator<Item = &ChainId>,
560        validator_address: impl Into<String>,
561    ) -> Result<()> {
562        let mut command = self.command().await?;
563        command
564            .arg("validator")
565            .arg("sync")
566            .arg(validator_address.into());
567        let mut chain_ids = chain_ids.into_iter().peekable();
568        if chain_ids.peek().is_some() {
569            command
570                .arg("--chains")
571                .args(chain_ids.map(ChainId::to_string));
572        }
573        command.spawn_and_wait_for_stdout().await?;
574        Ok(())
575    }
576
577    /// Runs `linera faucet`.
578    pub async fn run_faucet(
579        &self,
580        port: impl Into<Option<u16>>,
581        chain_id: Option<ChainId>,
582        amount: Amount,
583    ) -> Result<FaucetService> {
584        let port = port.into().unwrap_or(8080);
585        let temp_dir = tempfile::tempdir()
586            .context("Failed to create temporary directory for faucet storage")?;
587        let storage_path = temp_dir.path().join("faucet_storage.sqlite");
588        let mut command = self.command().await?;
589        let command = command
590            .arg("faucet")
591            .args(["--port".to_string(), port.to_string()])
592            .args(["--amount".to_string(), amount.to_string()])
593            .args([
594                "--storage-path".to_string(),
595                storage_path.to_string_lossy().to_string(),
596            ]);
597        if let Some(chain_id) = chain_id {
598            command.arg(chain_id.to_string());
599        }
600        let child = command.spawn_into()?;
601        let client = reqwest_client();
602        for i in 0..10 {
603            linera_base::time::timer::sleep(Duration::from_secs(i)).await;
604            let request = client
605                .get(format!("http://localhost:{}/", port))
606                .send()
607                .await;
608            if request.is_ok() {
609                tracing::info!("Faucet has started");
610                return Ok(FaucetService::new(port, child, temp_dir));
611            } else {
612                tracing::debug!("Waiting for faucet to start");
613            }
614        }
615        bail!("Failed to start faucet");
616    }
617
618    /// Runs `linera local-balance`.
619    pub async fn local_balance(&self, account: Account) -> Result<Amount> {
620        let stdout = self
621            .command()
622            .await?
623            .arg("local-balance")
624            .arg(account.to_string())
625            .spawn_and_wait_for_stdout()
626            .await?;
627        let amount = stdout
628            .trim()
629            .parse()
630            .context("error while parsing the result of `linera local-balance`")?;
631        Ok(amount)
632    }
633
634    /// Runs `linera query-balance`.
635    pub async fn query_balance(&self, account: Account) -> Result<Amount> {
636        let stdout = self
637            .command()
638            .await?
639            .arg("query-balance")
640            .arg(account.to_string())
641            .spawn_and_wait_for_stdout()
642            .await?;
643        let amount = stdout
644            .trim()
645            .parse()
646            .context("error while parsing the result of `linera query-balance`")?;
647        Ok(amount)
648    }
649
650    /// Runs `linera sync`.
651    pub async fn sync(&self, chain_id: ChainId) -> Result<()> {
652        self.command()
653            .await?
654            .arg("sync")
655            .arg(chain_id.to_string())
656            .spawn_and_wait_for_stdout()
657            .await?;
658        Ok(())
659    }
660
661    /// Runs `linera process-inbox`.
662    pub async fn process_inbox(&self, chain_id: ChainId) -> Result<()> {
663        self.command()
664            .await?
665            .arg("process-inbox")
666            .arg(chain_id.to_string())
667            .spawn_and_wait_for_stdout()
668            .await?;
669        Ok(())
670    }
671
672    /// Runs `linera transfer`.
673    pub async fn transfer(&self, amount: Amount, from: ChainId, to: ChainId) -> Result<()> {
674        self.command()
675            .await?
676            .arg("transfer")
677            .arg(amount.to_string())
678            .args(["--from", &from.to_string()])
679            .args(["--to", &to.to_string()])
680            .spawn_and_wait_for_stdout()
681            .await?;
682        Ok(())
683    }
684
685    /// Runs `linera transfer` with no logging.
686    pub async fn transfer_with_silent_logs(
687        &self,
688        amount: Amount,
689        from: ChainId,
690        to: ChainId,
691    ) -> Result<()> {
692        self.command()
693            .await?
694            .env("RUST_LOG", "off")
695            .arg("transfer")
696            .arg(amount.to_string())
697            .args(["--from", &from.to_string()])
698            .args(["--to", &to.to_string()])
699            .spawn_and_wait_for_stdout()
700            .await?;
701        Ok(())
702    }
703
704    /// Runs `linera transfer` with owner accounts.
705    pub async fn transfer_with_accounts(
706        &self,
707        amount: Amount,
708        from: Account,
709        to: Account,
710    ) -> Result<()> {
711        self.command()
712            .await?
713            .arg("transfer")
714            .arg(amount.to_string())
715            .args(["--from", &from.to_string()])
716            .args(["--to", &to.to_string()])
717            .spawn_and_wait_for_stdout()
718            .await?;
719        Ok(())
720    }
721
722    fn benchmark_command_internal(command: &mut Command, args: BenchmarkCommand) -> Result<()> {
723        let mut formatted_args = to_args(&args)?;
724        let subcommand = formatted_args.remove(0);
725        // The subcommand is followed by the flattened options, which are preceded by "options".
726        // So remove that as well.
727        formatted_args.remove(0);
728        let options = formatted_args
729            .chunks_exact(2)
730            .flat_map(|pair| {
731                let option = format!("--{}", pair[0]);
732                match pair[1].as_str() {
733                    "true" => vec![option],
734                    "false" => vec![],
735                    _ => vec![option, pair[1].clone()],
736                }
737            })
738            .collect::<Vec<_>>();
739        command
740            .args([
741                "--max-pending-message-bundles",
742                &args.transactions_per_block().to_string(),
743            ])
744            .arg("benchmark")
745            .arg(subcommand)
746            .args(options);
747        Ok(())
748    }
749
750    async fn benchmark_command_with_envs(
751        &self,
752        args: BenchmarkCommand,
753        envs: &[(&str, &str)],
754    ) -> Result<Command> {
755        let mut command = self
756            .command_with_envs_and_arguments(envs, self.required_command_arguments())
757            .await?;
758        Self::benchmark_command_internal(&mut command, args)?;
759        Ok(command)
760    }
761
762    async fn benchmark_command(&self, args: BenchmarkCommand) -> Result<Command> {
763        let mut command = self
764            .command_with_arguments(self.required_command_arguments())
765            .await?;
766        Self::benchmark_command_internal(&mut command, args)?;
767        Ok(command)
768    }
769
770    /// Runs `linera benchmark`.
771    pub async fn benchmark(&self, args: BenchmarkCommand) -> Result<()> {
772        let mut command = self.benchmark_command(args).await?;
773        command.spawn_and_wait_for_stdout().await?;
774        Ok(())
775    }
776
777    /// Runs `linera benchmark`, but detached: don't wait for the command to finish, just spawn it
778    /// and return the child process, and the handles to the stdout and stderr.
779    pub async fn benchmark_detached(
780        &self,
781        args: BenchmarkCommand,
782        tx: oneshot::Sender<()>,
783    ) -> Result<(Child, JoinHandle<()>, JoinHandle<()>)> {
784        let mut child = self
785            .benchmark_command_with_envs(args, &[("RUST_LOG", "linera=info")])
786            .await?
787            .kill_on_drop(true)
788            .stdin(Stdio::piped())
789            .stdout(Stdio::piped())
790            .stderr(Stdio::piped())
791            .spawn()?;
792
793        let pid = child.id().expect("failed to get pid");
794        let stdout = child.stdout.take().expect("stdout not open");
795        let stdout_handle = tokio::spawn(async move {
796            let mut lines = BufReader::new(stdout).lines();
797            while let Ok(Some(line)) = lines.next_line().await {
798                println!("benchmark{{pid={pid}}} {line}");
799            }
800        });
801
802        let stderr = child.stderr.take().expect("stderr not open");
803        let stderr_handle = tokio::spawn(async move {
804            let mut lines = BufReader::new(stderr).lines();
805            let mut tx = Some(tx);
806            while let Ok(Some(line)) = lines.next_line().await {
807                if line.contains("Ready to start benchmark") {
808                    tx.take()
809                        .expect("Should only send signal once")
810                        .send(())
811                        .expect("failed to send ready signal to main thread");
812                } else {
813                    println!("benchmark{{pid={pid}}} {line}");
814                }
815            }
816        });
817        Ok((child, stdout_handle, stderr_handle))
818    }
819
820    async fn open_chain_internal(
821        &self,
822        from: ChainId,
823        owner: Option<AccountOwner>,
824        initial_balance: Amount,
825        super_owner: bool,
826    ) -> Result<(ChainId, AccountOwner)> {
827        let mut command = self.command().await?;
828        command
829            .arg("open-chain")
830            .args(["--from", &from.to_string()])
831            .args(["--initial-balance", &initial_balance.to_string()]);
832
833        if let Some(owner) = owner {
834            command.args(["--owner", &owner.to_string()]);
835        }
836
837        if super_owner {
838            command.arg("--super-owner");
839        }
840
841        let stdout = command.spawn_and_wait_for_stdout().await?;
842        let mut split = stdout.split('\n');
843        let chain_id = ChainId::from_str(split.next().context("no chain ID in output")?)?;
844        let new_owner = AccountOwner::from_str(split.next().context("no owner in output")?)?;
845        if let Some(owner) = owner {
846            assert_eq!(owner, new_owner);
847        }
848        Ok((chain_id, new_owner))
849    }
850
851    /// Runs `linera open-chain --super-owner`.
852    pub async fn open_chain_super_owner(
853        &self,
854        from: ChainId,
855        owner: Option<AccountOwner>,
856        initial_balance: Amount,
857    ) -> Result<(ChainId, AccountOwner)> {
858        self.open_chain_internal(from, owner, initial_balance, true)
859            .await
860    }
861
862    /// Runs `linera open-chain`.
863    pub async fn open_chain(
864        &self,
865        from: ChainId,
866        owner: Option<AccountOwner>,
867        initial_balance: Amount,
868    ) -> Result<(ChainId, AccountOwner)> {
869        self.open_chain_internal(from, owner, initial_balance, false)
870            .await
871    }
872
873    /// Runs `linera open-chain` then `linera assign`.
874    pub async fn open_and_assign(
875        &self,
876        client: &ClientWrapper,
877        initial_balance: Amount,
878    ) -> Result<ChainId> {
879        let our_chain = self
880            .load_wallet()?
881            .default_chain()
882            .context("no default chain found")?;
883        let owner = client.keygen().await?;
884        let (new_chain, _) = self
885            .open_chain(our_chain, Some(owner), initial_balance)
886            .await?;
887        client.assign(owner, new_chain).await?;
888        Ok(new_chain)
889    }
890
891    pub async fn open_multi_owner_chain(
892        &self,
893        from: ChainId,
894        owners: BTreeMap<AccountOwner, u64>,
895        multi_leader_rounds: u32,
896        balance: Amount,
897        base_timeout_ms: u64,
898    ) -> Result<ChainId> {
899        let mut command = self.command().await?;
900        command
901            .arg("open-multi-owner-chain")
902            .args(["--from", &from.to_string()])
903            .arg("--owners")
904            .arg(serde_json::to_string(&owners)?)
905            .args(["--base-timeout-ms", &base_timeout_ms.to_string()]);
906        command
907            .args(["--multi-leader-rounds", &multi_leader_rounds.to_string()])
908            .args(["--initial-balance", &balance.to_string()]);
909
910        let stdout = command.spawn_and_wait_for_stdout().await?;
911        let mut split = stdout.split('\n');
912        let chain_id = ChainId::from_str(split.next().context("no chain ID in output")?)?;
913
914        Ok(chain_id)
915    }
916
917    pub async fn change_ownership(
918        &self,
919        chain_id: ChainId,
920        super_owners: Vec<AccountOwner>,
921        owners: Vec<AccountOwner>,
922    ) -> Result<()> {
923        let mut command = self.command().await?;
924        command
925            .arg("change-ownership")
926            .args(["--chain-id", &chain_id.to_string()]);
927        command
928            .arg("--super-owners")
929            .arg(serde_json::to_string(&super_owners)?);
930        command.arg("--owners").arg(serde_json::to_string(
931            &owners
932                .into_iter()
933                .zip(std::iter::repeat(100u64))
934                .collect::<BTreeMap<_, _>>(),
935        )?);
936        command.spawn_and_wait_for_stdout().await?;
937        Ok(())
938    }
939
940    /// Runs `linera wallet follow-chain CHAIN_ID`.
941    pub async fn follow_chain(&self, chain_id: ChainId, sync: bool) -> Result<()> {
942        let mut command = self.command().await?;
943        command
944            .args(["wallet", "follow-chain"])
945            .arg(chain_id.to_string());
946        if sync {
947            command.arg("--sync");
948        }
949        command.spawn_and_wait_for_stdout().await?;
950        Ok(())
951    }
952
953    /// Runs `linera wallet forget-chain CHAIN_ID`.
954    pub async fn forget_chain(&self, chain_id: ChainId) -> Result<()> {
955        let mut command = self.command().await?;
956        command
957            .args(["wallet", "forget-chain"])
958            .arg(chain_id.to_string());
959        command.spawn_and_wait_for_stdout().await?;
960        Ok(())
961    }
962
963    /// Runs `linera wallet set-default CHAIN_ID`.
964    pub async fn set_default_chain(&self, chain_id: ChainId) -> Result<()> {
965        let mut command = self.command().await?;
966        command
967            .args(["wallet", "set-default"])
968            .arg(chain_id.to_string());
969        command.spawn_and_wait_for_stdout().await?;
970        Ok(())
971    }
972
973    pub async fn retry_pending_block(
974        &self,
975        chain_id: Option<ChainId>,
976    ) -> Result<Option<CryptoHash>> {
977        let mut command = self.command().await?;
978        command.arg("retry-pending-block");
979        if let Some(chain_id) = chain_id {
980            command.arg(chain_id.to_string());
981        }
982        let stdout = command.spawn_and_wait_for_stdout().await?;
983        let stdout = stdout.trim();
984        if stdout.is_empty() {
985            Ok(None)
986        } else {
987            Ok(Some(CryptoHash::from_str(stdout)?))
988        }
989    }
990
991    /// Runs `linera publish-data-blob`.
992    pub async fn publish_data_blob(
993        &self,
994        path: &Path,
995        chain_id: Option<ChainId>,
996    ) -> Result<CryptoHash> {
997        let mut command = self.command().await?;
998        command.arg("publish-data-blob").arg(path);
999        if let Some(chain_id) = chain_id {
1000            command.arg(chain_id.to_string());
1001        }
1002        let stdout = command.spawn_and_wait_for_stdout().await?;
1003        let stdout = stdout.trim();
1004        Ok(CryptoHash::from_str(stdout)?)
1005    }
1006
1007    /// Runs `linera read-data-blob`.
1008    pub async fn read_data_blob(&self, hash: CryptoHash, chain_id: Option<ChainId>) -> Result<()> {
1009        let mut command = self.command().await?;
1010        command.arg("read-data-blob").arg(hash.to_string());
1011        if let Some(chain_id) = chain_id {
1012            command.arg(chain_id.to_string());
1013        }
1014        command.spawn_and_wait_for_stdout().await?;
1015        Ok(())
1016    }
1017
1018    pub fn load_wallet(&self) -> Result<Wallet> {
1019        Ok(Wallet::read(&self.wallet_path())?)
1020    }
1021
1022    pub fn load_keystore(&self) -> Result<InMemorySigner> {
1023        util::read_json(self.keystore_path())
1024    }
1025
1026    pub fn wallet_path(&self) -> PathBuf {
1027        self.path_provider.path().join(&self.wallet)
1028    }
1029
1030    pub fn keystore_path(&self) -> PathBuf {
1031        self.path_provider.path().join(&self.keystore)
1032    }
1033
1034    pub fn storage_path(&self) -> &str {
1035        &self.storage
1036    }
1037
1038    pub fn get_owner(&self) -> Option<AccountOwner> {
1039        let wallet = self.load_wallet().ok()?;
1040        wallet
1041            .get(wallet.default_chain()?)
1042            .expect("default chain must be in wallet")
1043            .owner
1044    }
1045
1046    pub fn is_chain_present_in_wallet(&self, chain: ChainId) -> bool {
1047        self.load_wallet()
1048            .ok()
1049            .is_some_and(|wallet| wallet.get(chain).is_some())
1050    }
1051
1052    pub async fn set_validator(
1053        &self,
1054        validator_key: &(String, String),
1055        port: usize,
1056        votes: usize,
1057    ) -> Result<()> {
1058        let address = format!("{}:127.0.0.1:{}", self.network.short(), port);
1059        self.command()
1060            .await?
1061            .arg("validator")
1062            .arg("add")
1063            .args(["--public-key", &validator_key.0])
1064            .args(["--account-key", &validator_key.1])
1065            .args(["--address", &address])
1066            .args(["--votes", &votes.to_string()])
1067            .spawn_and_wait_for_stdout()
1068            .await?;
1069        Ok(())
1070    }
1071
1072    pub async fn remove_validator(&self, validator_key: &str) -> Result<()> {
1073        self.command()
1074            .await?
1075            .arg("validator")
1076            .arg("remove")
1077            .args(["--public-key", validator_key])
1078            .spawn_and_wait_for_stdout()
1079            .await?;
1080        Ok(())
1081    }
1082
1083    pub async fn change_validators(
1084        &self,
1085        add_validators: &[(String, String, usize, usize)], // (public_key, account_key, port, votes)
1086        modify_validators: &[(String, String, usize, usize)], // (public_key, account_key, port, votes)
1087        remove_validators: &[String],
1088    ) -> Result<()> {
1089        use std::str::FromStr;
1090
1091        use linera_base::crypto::{AccountPublicKey, ValidatorPublicKey};
1092
1093        // Build a map that will be serialized to JSON
1094        // Use the exact types that deserialization expects
1095        let mut changes = std::collections::HashMap::new();
1096
1097        // Add/modify validators
1098        for (public_key_str, account_key_str, port, votes) in
1099            add_validators.iter().chain(modify_validators.iter())
1100        {
1101            let public_key = ValidatorPublicKey::from_str(public_key_str)
1102                .with_context(|| format!("Invalid validator public key: {}", public_key_str))?;
1103
1104            let account_key = AccountPublicKey::from_str(account_key_str)
1105                .with_context(|| format!("Invalid account public key: {}", account_key_str))?;
1106
1107            let address = format!("{}:127.0.0.1:{}", self.network.short(), port)
1108                .parse()
1109                .unwrap();
1110
1111            // Create ValidatorChange struct
1112            let change = crate::cli::validator::Change {
1113                account_key,
1114                address,
1115                votes: crate::cli::validator::Votes(
1116                    std::num::NonZero::new(*votes as u64).context("Votes must be non-zero")?,
1117                ),
1118            };
1119
1120            changes.insert(public_key, Some(change));
1121        }
1122
1123        // Remove validators (set to None)
1124        for validator_key_str in remove_validators {
1125            let public_key = ValidatorPublicKey::from_str(validator_key_str)
1126                .with_context(|| format!("Invalid validator public key: {}", validator_key_str))?;
1127            changes.insert(public_key, None);
1128        }
1129
1130        // Create temporary file with JSON
1131        let temp_file = tempfile::NamedTempFile::new()
1132            .context("Failed to create temporary file for validator changes")?;
1133        serde_json::to_writer(&temp_file, &changes)
1134            .context("Failed to write validator changes to file")?;
1135        let temp_path = temp_file.path();
1136
1137        self.command()
1138            .await?
1139            .arg("validator")
1140            .arg("update")
1141            .arg(temp_path)
1142            .arg("--yes") // Skip confirmation prompt
1143            .spawn_and_wait_for_stdout()
1144            .await?;
1145
1146        Ok(())
1147    }
1148
1149    pub async fn revoke_epochs(&self, epoch: Epoch) -> Result<()> {
1150        self.command()
1151            .await?
1152            .arg("revoke-epochs")
1153            .arg(epoch.to_string())
1154            .spawn_and_wait_for_stdout()
1155            .await?;
1156        Ok(())
1157    }
1158
1159    /// Runs `linera keygen`.
1160    pub async fn keygen(&self) -> Result<AccountOwner> {
1161        let stdout = self
1162            .command()
1163            .await?
1164            .arg("keygen")
1165            .spawn_and_wait_for_stdout()
1166            .await?;
1167        AccountOwner::from_str(stdout.as_str().trim())
1168    }
1169
1170    /// Returns the default chain.
1171    pub fn default_chain(&self) -> Option<ChainId> {
1172        self.load_wallet().ok()?.default_chain()
1173    }
1174
1175    /// Runs `linera assign`.
1176    pub async fn assign(&self, owner: AccountOwner, chain_id: ChainId) -> Result<()> {
1177        let _stdout = self
1178            .command()
1179            .await?
1180            .arg("assign")
1181            .args(["--owner", &owner.to_string()])
1182            .args(["--chain-id", &chain_id.to_string()])
1183            .spawn_and_wait_for_stdout()
1184            .await?;
1185        Ok(())
1186    }
1187
1188    /// Runs `linera set-preferred-owner` for `chain_id`.
1189    pub async fn set_preferred_owner(
1190        &self,
1191        chain_id: ChainId,
1192        owner: Option<AccountOwner>,
1193    ) -> Result<()> {
1194        let mut owner_arg = vec!["--owner".to_string()];
1195        if let Some(owner) = owner {
1196            owner_arg.push(owner.to_string());
1197        };
1198        self.command()
1199            .await?
1200            .arg("set-preferred-owner")
1201            .args(["--chain-id", &chain_id.to_string()])
1202            .args(owner_arg)
1203            .spawn_and_wait_for_stdout()
1204            .await?;
1205        Ok(())
1206    }
1207
1208    pub async fn build_application(
1209        &self,
1210        path: &Path,
1211        name: &str,
1212        is_workspace: bool,
1213    ) -> Result<(PathBuf, PathBuf)> {
1214        Command::new("cargo")
1215            .current_dir(self.path_provider.path())
1216            .arg("build")
1217            .arg("--release")
1218            .args(["--target", "wasm32-unknown-unknown"])
1219            .arg("--manifest-path")
1220            .arg(path.join("Cargo.toml"))
1221            .spawn_and_wait_for_stdout()
1222            .await?;
1223
1224        let release_dir = match is_workspace {
1225            true => path.join("../target/wasm32-unknown-unknown/release"),
1226            false => path.join("target/wasm32-unknown-unknown/release"),
1227        };
1228
1229        let contract = release_dir.join(format!("{}_contract.wasm", name.replace('-', "_")));
1230        let service = release_dir.join(format!("{}_service.wasm", name.replace('-', "_")));
1231
1232        let contract_size = fs_err::tokio::metadata(&contract).await?.len();
1233        let service_size = fs_err::tokio::metadata(&service).await?.len();
1234        tracing::info!("Done building application {name}: contract_size={contract_size}, service_size={service_size}");
1235
1236        Ok((contract, service))
1237    }
1238}
1239
1240impl Drop for ClientWrapper {
1241    fn drop(&mut self) {
1242        use std::process::Command as SyncCommand;
1243
1244        if self.on_drop != OnClientDrop::CloseChains {
1245            return;
1246        }
1247
1248        let Ok(binary_path) = self.binary_path.lock() else {
1249            tracing::error!(
1250                "Failed to close chains because a thread panicked with a lock to `binary_path`"
1251            );
1252            return;
1253        };
1254
1255        let Some(binary_path) = binary_path.as_ref() else {
1256            tracing::warn!(
1257                "Assuming no chains need to be closed, because the command binary was never \
1258                resolved and therefore presumably never called"
1259            );
1260            return;
1261        };
1262
1263        let working_directory = self.path_provider.path();
1264        let mut wallet_show_command = SyncCommand::new(binary_path);
1265
1266        for argument in self.command_arguments() {
1267            wallet_show_command.arg(&*argument);
1268        }
1269
1270        let Ok(wallet_show_output) = wallet_show_command
1271            .current_dir(working_directory)
1272            .args(["wallet", "show", "--short", "--owned"])
1273            .output()
1274        else {
1275            tracing::warn!("Failed to execute `wallet show --short` to list chains to close");
1276            return;
1277        };
1278
1279        if !wallet_show_output.status.success() {
1280            tracing::warn!("Failed to list chains in the wallet to close them");
1281            return;
1282        }
1283
1284        let Ok(chain_list_string) = String::from_utf8(wallet_show_output.stdout) else {
1285            tracing::warn!(
1286                "Failed to close chains because `linera wallet show --short` \
1287                returned a non-UTF-8 output"
1288            );
1289            return;
1290        };
1291
1292        let chain_ids = chain_list_string
1293            .split('\n')
1294            .map(|line| line.trim())
1295            .filter(|line| !line.is_empty());
1296
1297        for chain_id in chain_ids {
1298            let mut close_chain_command = SyncCommand::new(binary_path);
1299
1300            for argument in self.command_arguments() {
1301                close_chain_command.arg(&*argument);
1302            }
1303
1304            close_chain_command.current_dir(working_directory);
1305
1306            match close_chain_command.args(["close-chain", chain_id]).status() {
1307                Ok(status) if status.success() => (),
1308                Ok(failure) => tracing::warn!("Failed to close chain {chain_id}: {failure}"),
1309                Err(error) => tracing::warn!("Failed to close chain {chain_id}: {error}"),
1310            }
1311        }
1312    }
1313}
1314
1315#[cfg(with_testing)]
1316impl ClientWrapper {
1317    pub async fn build_example(&self, name: &str) -> Result<(PathBuf, PathBuf)> {
1318        self.build_application(Self::example_path(name)?.as_path(), name, true)
1319            .await
1320    }
1321
1322    pub fn example_path(name: &str) -> Result<PathBuf> {
1323        Ok(env::current_dir()?.join("../examples/").join(name))
1324    }
1325}
1326
1327fn truncate_query_output(input: &str) -> String {
1328    let max_len = 1000;
1329    if input.len() < max_len {
1330        input.to_string()
1331    } else {
1332        format!("{} ...", input.get(..max_len).unwrap())
1333    }
1334}
1335
1336fn truncate_query_output_serialize<T: Serialize>(query: T) -> String {
1337    let query = serde_json::to_string(&query).expect("Failed to serialize the failed query");
1338    let max_len = 200;
1339    if query.len() < max_len {
1340        query
1341    } else {
1342        format!("{} ...", query.get(..max_len).unwrap())
1343    }
1344}
1345
1346/// A running node service.
1347pub struct NodeService {
1348    port: u16,
1349    child: Child,
1350}
1351
1352impl NodeService {
1353    fn new(port: u16, child: Child) -> Self {
1354        Self { port, child }
1355    }
1356
1357    pub async fn terminate(mut self) -> Result<()> {
1358        self.child.kill().await.context("terminating node service")
1359    }
1360
1361    pub fn port(&self) -> u16 {
1362        self.port
1363    }
1364
1365    pub fn ensure_is_running(&mut self) -> Result<()> {
1366        self.child.ensure_is_running()
1367    }
1368
1369    pub async fn process_inbox(&self, chain_id: &ChainId) -> Result<Vec<CryptoHash>> {
1370        let query = format!("mutation {{ processInbox(chainId: \"{chain_id}\") }}");
1371        let mut data = self.query_node(query).await?;
1372        Ok(serde_json::from_value(data["processInbox"].take())?)
1373    }
1374
1375    pub async fn sync(&self, chain_id: &ChainId) -> Result<u64> {
1376        let query = format!("mutation {{ sync(chainId: \"{chain_id}\") }}");
1377        let mut data = self.query_node(query).await?;
1378        Ok(serde_json::from_value(data["sync"].take())?)
1379    }
1380
1381    pub async fn transfer(
1382        &self,
1383        chain_id: ChainId,
1384        owner: AccountOwner,
1385        recipient: Account,
1386        amount: Amount,
1387    ) -> Result<CryptoHash> {
1388        let json_owner = owner.to_value();
1389        let json_recipient = recipient.to_value();
1390        let query = format!(
1391            "mutation {{ transfer(\
1392                 chainId: \"{chain_id}\", \
1393                 owner: {json_owner}, \
1394                 recipient: {json_recipient}, \
1395                 amount: \"{amount}\") \
1396             }}"
1397        );
1398        let data = self.query_node(query).await?;
1399        serde_json::from_value(data["transfer"].clone())
1400            .context("missing transfer field in response")
1401    }
1402
1403    pub async fn balance(&self, account: &Account) -> Result<Amount> {
1404        let chain = account.chain_id;
1405        let owner = account.owner;
1406        if matches!(owner, AccountOwner::CHAIN) {
1407            let query = format!(
1408                "query {{ chain(chainId:\"{chain}\") {{
1409                    executionState {{ system {{ balance }} }}
1410                }} }}"
1411            );
1412            let response = self.query_node(query).await?;
1413            let balance = &response["chain"]["executionState"]["system"]["balance"]
1414                .as_str()
1415                .unwrap();
1416            return Ok(Amount::from_str(balance)?);
1417        }
1418        let query = format!(
1419            "query {{ chain(chainId:\"{chain}\") {{
1420                executionState {{ system {{ balances {{
1421                    entry(key:\"{owner}\") {{ value }}
1422                }} }} }}
1423            }} }}"
1424        );
1425        let response = self.query_node(query).await?;
1426        let balances = &response["chain"]["executionState"]["system"]["balances"];
1427        let balance = balances["entry"]["value"].as_str();
1428        match balance {
1429            None => Ok(Amount::ZERO),
1430            Some(amount) => Ok(Amount::from_str(amount)?),
1431        }
1432    }
1433
1434    pub fn make_application<A: ContractAbi>(
1435        &self,
1436        chain_id: &ChainId,
1437        application_id: &ApplicationId<A>,
1438    ) -> Result<ApplicationWrapper<A>> {
1439        let application_id = application_id.forget_abi().to_string();
1440        let link = format!(
1441            "http://localhost:{}/chains/{chain_id}/applications/{application_id}",
1442            self.port
1443        );
1444        Ok(ApplicationWrapper::from(link))
1445    }
1446
1447    pub async fn publish_data_blob(
1448        &self,
1449        chain_id: &ChainId,
1450        bytes: Vec<u8>,
1451    ) -> Result<CryptoHash> {
1452        let query = format!(
1453            "mutation {{ publishDataBlob(chainId: {}, bytes: {}) }}",
1454            chain_id.to_value(),
1455            bytes.to_value(),
1456        );
1457        let data = self.query_node(query).await?;
1458        serde_json::from_value(data["publishDataBlob"].clone())
1459            .context("missing publishDataBlob field in response")
1460    }
1461
1462    pub async fn publish_module<Abi, Parameters, InstantiationArgument>(
1463        &self,
1464        chain_id: &ChainId,
1465        contract: PathBuf,
1466        service: PathBuf,
1467        vm_runtime: VmRuntime,
1468    ) -> Result<ModuleId<Abi, Parameters, InstantiationArgument>> {
1469        let contract_code = Bytecode::load_from_file(&contract).await?;
1470        let service_code = Bytecode::load_from_file(&service).await?;
1471        let query = format!(
1472            "mutation {{ publishModule(chainId: {}, contract: {}, service: {}, vmRuntime: {}) }}",
1473            chain_id.to_value(),
1474            contract_code.to_value(),
1475            service_code.to_value(),
1476            vm_runtime.to_value(),
1477        );
1478        let data = self.query_node(query).await?;
1479        let module_str = data["publishModule"]
1480            .as_str()
1481            .context("module ID not found")?;
1482        let module_id: ModuleId = module_str.parse().context("could not parse module ID")?;
1483        Ok(module_id.with_abi())
1484    }
1485
1486    pub async fn query_committees(&self, chain_id: &ChainId) -> Result<BTreeMap<Epoch, Committee>> {
1487        let query = format!(
1488            "query {{ chain(chainId:\"{chain_id}\") {{
1489                executionState {{ system {{ committees }} }}
1490            }} }}"
1491        );
1492        let mut response = self.query_node(query).await?;
1493        let committees = response["chain"]["executionState"]["system"]["committees"].take();
1494        Ok(serde_json::from_value(committees)?)
1495    }
1496
1497    pub async fn events_from_index(
1498        &self,
1499        chain_id: &ChainId,
1500        stream_id: &StreamId,
1501        start_index: u32,
1502    ) -> Result<Vec<IndexAndEvent>> {
1503        let query = format!(
1504            "query {{
1505               eventsFromIndex(chainId: \"{chain_id}\", streamId: {}, startIndex: {start_index})
1506               {{ index event }}
1507             }}",
1508            stream_id.to_value()
1509        );
1510        let mut response = self.query_node(query).await?;
1511        let response = response["eventsFromIndex"].take();
1512        Ok(serde_json::from_value(response)?)
1513    }
1514
1515    pub async fn query_node(&self, query: impl AsRef<str>) -> Result<Value> {
1516        let n_try = 5;
1517        let query = query.as_ref();
1518        for i in 0..n_try {
1519            linera_base::time::timer::sleep(Duration::from_secs(i)).await;
1520            let url = format!("http://localhost:{}/", self.port);
1521            let client = reqwest_client();
1522            let result = client
1523                .post(url)
1524                .json(&json!({ "query": query }))
1525                .send()
1526                .await;
1527            if matches!(result, Err(ref error) if error.is_timeout()) {
1528                tracing::warn!(
1529                    "Timeout when sending query {} to the node service",
1530                    truncate_query_output(query)
1531                );
1532                continue;
1533            }
1534            let response = result.with_context(|| {
1535                format!(
1536                    "query_node: failed to post query={}",
1537                    truncate_query_output(query)
1538                )
1539            })?;
1540            ensure!(
1541                response.status().is_success(),
1542                "Query \"{}\" failed: {}",
1543                truncate_query_output(query),
1544                response
1545                    .text()
1546                    .await
1547                    .unwrap_or_else(|error| format!("Could not get response text: {error}"))
1548            );
1549            let value: Value = response.json().await.context("invalid JSON")?;
1550            if let Some(errors) = value.get("errors") {
1551                tracing::warn!(
1552                    "Query \"{}\" failed: {}",
1553                    truncate_query_output(query),
1554                    errors
1555                );
1556            } else {
1557                return Ok(value["data"].clone());
1558            }
1559        }
1560        bail!(
1561            "Query \"{}\" failed after {} retries.",
1562            truncate_query_output(query),
1563            n_try
1564        );
1565    }
1566
1567    pub async fn create_application<
1568        Abi: ContractAbi,
1569        Parameters: Serialize,
1570        InstantiationArgument: Serialize,
1571    >(
1572        &self,
1573        chain_id: &ChainId,
1574        module_id: &ModuleId<Abi, Parameters, InstantiationArgument>,
1575        parameters: &Parameters,
1576        argument: &InstantiationArgument,
1577        required_application_ids: &[ApplicationId],
1578    ) -> Result<ApplicationId<Abi>> {
1579        let module_id = module_id.forget_abi();
1580        let json_required_applications_ids = required_application_ids
1581            .iter()
1582            .map(ApplicationId::to_string)
1583            .collect::<Vec<_>>()
1584            .to_value();
1585        // Convert to `serde_json::Value` then `async_graphql::Value` via the trait `InputType`.
1586        let new_parameters = serde_json::to_value(parameters)
1587            .context("could not create parameters JSON")?
1588            .to_value();
1589        let new_argument = serde_json::to_value(argument)
1590            .context("could not create argument JSON")?
1591            .to_value();
1592        let query = format!(
1593            "mutation {{ createApplication(\
1594                 chainId: \"{chain_id}\",
1595                 moduleId: \"{module_id}\", \
1596                 parameters: {new_parameters}, \
1597                 instantiationArgument: {new_argument}, \
1598                 requiredApplicationIds: {json_required_applications_ids}) \
1599             }}"
1600        );
1601        let data = self.query_node(query).await?;
1602        let app_id_str = data["createApplication"]
1603            .as_str()
1604            .context("missing createApplication string in response")?
1605            .trim();
1606        Ok(app_id_str
1607            .parse::<ApplicationId>()
1608            .context("invalid application ID")?
1609            .with_abi())
1610    }
1611
1612    /// Obtains the hash and height of the `chain`'s tip block, as known by this node service.
1613    pub async fn chain_tip(&self, chain: ChainId) -> Result<Option<(CryptoHash, BlockHeight)>> {
1614        let query = format!(
1615            r#"query {{ block(chainId: "{chain}") {{
1616                hash
1617                block {{ header {{ height }} }}
1618            }} }}"#
1619        );
1620
1621        let mut response = self.query_node(&query).await?;
1622
1623        match (
1624            mem::take(&mut response["block"]["hash"]),
1625            mem::take(&mut response["block"]["block"]["header"]["height"]),
1626        ) {
1627            (Value::Null, Value::Null) => Ok(None),
1628            (Value::String(hash), Value::Number(height)) => Ok(Some((
1629                hash.parse()
1630                    .context("Received an invalid hash {hash:?} for chain tip")?,
1631                BlockHeight(height.as_u64().unwrap()),
1632            ))),
1633            invalid_data => bail!("Expected a tip hash string, but got {invalid_data:?} instead"),
1634        }
1635    }
1636
1637    /// Subscribes to the node service and returns a stream of notifications about a chain.
1638    pub async fn notifications(
1639        &self,
1640        chain_id: ChainId,
1641    ) -> Result<Pin<Box<impl Stream<Item = Result<Notification>>>>> {
1642        let query = format!("subscription {{ notifications(chainId: \"{chain_id}\") }}",);
1643        let url = format!("ws://localhost:{}/ws", self.port);
1644        let mut request = url.into_client_request()?;
1645        request.headers_mut().insert(
1646            "Sec-WebSocket-Protocol",
1647            HeaderValue::from_str("graphql-transport-ws")?,
1648        );
1649        let (mut websocket, _) = async_tungstenite::tokio::connect_async(request).await?;
1650        let init_json = json!({
1651          "type": "connection_init",
1652          "payload": {}
1653        });
1654        websocket.send(init_json.to_string().into()).await?;
1655        let text = websocket
1656            .next()
1657            .await
1658            .context("Failed to establish connection")??
1659            .into_text()?;
1660        ensure!(
1661            text == "{\"type\":\"connection_ack\"}",
1662            "Unexpected response: {text}"
1663        );
1664        let query_json = json!({
1665          "id": "1",
1666          "type": "start",
1667          "payload": {
1668            "query": query,
1669            "variables": {},
1670            "operationName": null
1671          }
1672        });
1673        websocket.send(query_json.to_string().into()).await?;
1674        Ok(Box::pin(websocket.map_err(anyhow::Error::from).and_then(
1675            |message| async {
1676                let text = message.into_text()?;
1677                let value: Value = serde_json::from_str(&text).context("invalid JSON")?;
1678                if let Some(errors) = value["payload"].get("errors") {
1679                    bail!("Notification subscription failed: {errors:?}");
1680                }
1681                serde_json::from_value(value["payload"]["data"]["notifications"].clone())
1682                    .context("Failed to deserialize notification")
1683            },
1684        )))
1685    }
1686}
1687
1688/// A running faucet service.
1689pub struct FaucetService {
1690    port: u16,
1691    child: Child,
1692    _temp_dir: tempfile::TempDir,
1693}
1694
1695impl FaucetService {
1696    fn new(port: u16, child: Child, temp_dir: tempfile::TempDir) -> Self {
1697        Self {
1698            port,
1699            child,
1700            _temp_dir: temp_dir,
1701        }
1702    }
1703
1704    pub async fn terminate(mut self) -> Result<()> {
1705        self.child
1706            .kill()
1707            .await
1708            .context("terminating faucet service")
1709    }
1710
1711    pub fn ensure_is_running(&mut self) -> Result<()> {
1712        self.child.ensure_is_running()
1713    }
1714
1715    pub fn instance(&self) -> Faucet {
1716        Faucet::new(format!("http://localhost:{}/", self.port))
1717    }
1718}
1719
1720/// A running `Application` to be queried in GraphQL.
1721pub struct ApplicationWrapper<A> {
1722    uri: String,
1723    _phantom: PhantomData<A>,
1724}
1725
1726impl<A> ApplicationWrapper<A> {
1727    pub async fn run_graphql_query(&self, query: impl AsRef<str>) -> Result<Value> {
1728        let query = query.as_ref();
1729        let value = self.run_json_query(json!({ "query": query })).await?;
1730        Ok(value["data"].clone())
1731    }
1732
1733    pub async fn run_json_query<T: Serialize>(&self, query: T) -> Result<Value> {
1734        const MAX_RETRIES: usize = 5;
1735
1736        for i in 0.. {
1737            let client = reqwest_client();
1738            let result = client.post(&self.uri).json(&query).send().await;
1739            let response = match result {
1740                Ok(response) => response,
1741                Err(error) if i < MAX_RETRIES => {
1742                    tracing::warn!(
1743                        "Failed to post query \"{}\": {error}; retrying",
1744                        truncate_query_output_serialize(&query),
1745                    );
1746                    continue;
1747                }
1748                Err(error) => {
1749                    let query = truncate_query_output_serialize(&query);
1750                    return Err(error)
1751                        .with_context(|| format!("run_json_query: failed to post query={query}"));
1752                }
1753            };
1754            ensure!(
1755                response.status().is_success(),
1756                "Query \"{}\" failed: {}",
1757                truncate_query_output_serialize(&query),
1758                response
1759                    .text()
1760                    .await
1761                    .unwrap_or_else(|error| format!("Could not get response text: {error}"))
1762            );
1763            let value: Value = response.json().await.context("invalid JSON")?;
1764            if let Some(errors) = value.get("errors") {
1765                bail!(
1766                    "Query \"{}\" failed: {}",
1767                    truncate_query_output_serialize(&query),
1768                    errors
1769                );
1770            }
1771            return Ok(value);
1772        }
1773        unreachable!()
1774    }
1775
1776    pub async fn query(&self, query: impl AsRef<str>) -> Result<Value> {
1777        let query = query.as_ref();
1778        self.run_graphql_query(&format!("query {{ {query} }}"))
1779            .await
1780    }
1781
1782    pub async fn query_json<T: DeserializeOwned>(&self, query: impl AsRef<str>) -> Result<T> {
1783        let query = query.as_ref().trim();
1784        let name = query
1785            .split_once(|ch: char| !ch.is_alphanumeric())
1786            .map_or(query, |(name, _)| name);
1787        let data = self.query(query).await?;
1788        serde_json::from_value(data[name].clone())
1789            .with_context(|| format!("{name} field missing in response"))
1790    }
1791
1792    pub async fn mutate(&self, mutation: impl AsRef<str>) -> Result<Value> {
1793        let mutation = mutation.as_ref();
1794        self.run_graphql_query(&format!("mutation {{ {mutation} }}"))
1795            .await
1796    }
1797
1798    pub async fn multiple_mutate(&self, mutations: &[String]) -> Result<Value> {
1799        let mut out = String::from("mutation {\n");
1800        for (index, mutation) in mutations.iter().enumerate() {
1801            out = format!("{}  u{}: {}\n", out, index, mutation);
1802        }
1803        out.push_str("}\n");
1804        self.run_graphql_query(&out).await
1805    }
1806}
1807
1808impl<A> From<String> for ApplicationWrapper<A> {
1809    fn from(uri: String) -> ApplicationWrapper<A> {
1810        ApplicationWrapper {
1811            uri,
1812            _phantom: PhantomData,
1813        }
1814    }
1815}
1816
1817/// Returns the timeout for tests that wait for notifications, either read from the env
1818/// variable `LINERA_TEST_NOTIFICATION_TIMEOUT_MS`, or the default value of 10 seconds.
1819#[cfg(with_testing)]
1820fn notification_timeout() -> Duration {
1821    const NOTIFICATION_TIMEOUT_MS_ENV: &str = "LINERA_TEST_NOTIFICATION_TIMEOUT_MS";
1822    const NOTIFICATION_TIMEOUT_MS_DEFAULT: u64 = 10_000;
1823
1824    match env::var(NOTIFICATION_TIMEOUT_MS_ENV) {
1825        Ok(var) => Duration::from_millis(var.parse().unwrap_or_else(|error| {
1826            panic!("{NOTIFICATION_TIMEOUT_MS_ENV} is not a valid number: {error}")
1827        })),
1828        Err(env::VarError::NotPresent) => Duration::from_millis(NOTIFICATION_TIMEOUT_MS_DEFAULT),
1829        Err(env::VarError::NotUnicode(_)) => {
1830            panic!("{NOTIFICATION_TIMEOUT_MS_ENV} must be valid Unicode")
1831        }
1832    }
1833}
1834
1835#[cfg(with_testing)]
1836pub trait NotificationsExt {
1837    /// Waits for a notification for which `f` returns `Some(t)`, and returns `t`.
1838    fn wait_for<T>(
1839        &mut self,
1840        f: impl FnMut(Notification) -> Option<T>,
1841    ) -> impl Future<Output = Result<T>>;
1842
1843    /// Waits for a `NewEvents` notification for the given block height. If no height is specified,
1844    /// any height is accepted.
1845    fn wait_for_events(
1846        &mut self,
1847        expected_height: impl Into<Option<BlockHeight>>,
1848    ) -> impl Future<Output = Result<BTreeSet<StreamId>>> {
1849        let expected_height = expected_height.into();
1850        self.wait_for(move |notification| {
1851            if let Reason::NewEvents {
1852                height,
1853                event_streams,
1854                ..
1855            } = notification.reason
1856            {
1857                if expected_height.is_none_or(|h| h == height) {
1858                    return Some(event_streams);
1859                }
1860            }
1861            None
1862        })
1863    }
1864
1865    /// Waits for a `NewBlock` notification for the given block height. If no height is specified,
1866    /// any height is accepted.
1867    fn wait_for_block(
1868        &mut self,
1869        expected_height: impl Into<Option<BlockHeight>>,
1870    ) -> impl Future<Output = Result<CryptoHash>> {
1871        let expected_height = expected_height.into();
1872        self.wait_for(move |notification| {
1873            if let Reason::NewBlock { height, hash, .. } = notification.reason {
1874                if expected_height.is_none_or(|h| h == height) {
1875                    return Some(hash);
1876                }
1877            }
1878            None
1879        })
1880    }
1881
1882    /// Waits for a `NewIncomingBundle` notification for the given sender chain and sender block
1883    /// height. If no height is specified, any height is accepted.
1884    fn wait_for_bundle(
1885        &mut self,
1886        expected_origin: ChainId,
1887        expected_height: impl Into<Option<BlockHeight>>,
1888    ) -> impl Future<Output = Result<()>> {
1889        let expected_height = expected_height.into();
1890        self.wait_for(move |notification| {
1891            if let Reason::NewIncomingBundle { height, origin } = notification.reason {
1892                if expected_height.is_none_or(|h| h == height) && origin == expected_origin {
1893                    return Some(());
1894                }
1895            }
1896            None
1897        })
1898    }
1899}
1900
1901#[cfg(with_testing)]
1902impl<S: Stream<Item = Result<Notification>>> NotificationsExt for Pin<Box<S>> {
1903    async fn wait_for<T>(&mut self, mut f: impl FnMut(Notification) -> Option<T>) -> Result<T> {
1904        let mut timeout = Box::pin(linera_base::time::timer::sleep(notification_timeout())).fuse();
1905        loop {
1906            let notification = futures::select! {
1907                () = timeout => bail!("Timeout waiting for notification"),
1908                notification = self.next().fuse() => notification.context("Stream closed")??,
1909            };
1910            if let Some(t) = f(notification) {
1911                return Ok(t);
1912            }
1913        }
1914    }
1915}