linera_sdk/test/
chain.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A reference to a single microchain inside a [`TestValidator`].
5//!
6//! This allows manipulating a test microchain.
7
8use std::{
9    collections::HashMap,
10    io,
11    path::{Path, PathBuf},
12    sync::Arc,
13};
14
15use cargo_toml::Manifest;
16use futures::future;
17use linera_base::{
18    crypto::{AccountPublicKey, AccountSecretKey},
19    data_types::{
20        Amount, ApplicationDescription, Blob, BlockHeight, Bytecode, ChainDescription,
21        CompressedBytecode, Epoch,
22    },
23    identifiers::{AccountOwner, ApplicationId, ChainId, ModuleId, OwnerSpender},
24    vm::VmRuntime,
25};
26use linera_chain::{types::ConfirmedBlockCertificate, ChainExecutionContext};
27use linera_core::{data_types::ChainInfoQuery, worker::WorkerError};
28use linera_execution::{
29    system::{SystemOperation, SystemQuery, SystemResponse},
30    ExecutionError, Operation, Query, QueryOutcome, QueryResponse, ResourceTracker,
31};
32use linera_storage::Storage as _;
33use serde::Serialize;
34use tokio::{fs, sync::Mutex};
35
36use super::{BlockBuilder, TestValidator};
37use crate::{abis::fungible::FungibleTokenAbi, ContractAbi, ServiceAbi};
38
39/// A reference to a single microchain inside a [`TestValidator`].
40pub struct ActiveChain {
41    key_pair: AccountSecretKey,
42    description: ChainDescription,
43    tip: Arc<Mutex<Option<ConfirmedBlockCertificate>>>,
44    validator: TestValidator,
45}
46
47impl Clone for ActiveChain {
48    fn clone(&self) -> Self {
49        ActiveChain {
50            key_pair: self.key_pair.copy(),
51            description: self.description.clone(),
52            tip: self.tip.clone(),
53            validator: self.validator.clone(),
54        }
55    }
56}
57
58impl ActiveChain {
59    /// Creates a new [`ActiveChain`] instance referencing a new empty microchain in the
60    /// `validator`.
61    ///
62    /// The microchain has a single owner that uses the `key_pair` to produce blocks. The
63    /// `description` is used as the identifier of the microchain.
64    pub fn new(
65        key_pair: AccountSecretKey,
66        description: ChainDescription,
67        validator: TestValidator,
68    ) -> Self {
69        ActiveChain {
70            key_pair,
71            description,
72            tip: Arc::default(),
73            validator,
74        }
75    }
76
77    /// Returns the [`ChainId`] of this microchain.
78    pub fn id(&self) -> ChainId {
79        self.description.id()
80    }
81
82    /// Returns the [`AccountPublicKey`] of the active owner of this microchain.
83    pub fn public_key(&self) -> AccountPublicKey {
84        self.key_pair.public()
85    }
86
87    /// Returns the [`AccountSecretKey`] of the active owner of this microchain.
88    pub fn key_pair(&self) -> &AccountSecretKey {
89        &self.key_pair
90    }
91
92    /// Sets the [`AccountSecretKey`] to use for signing new blocks.
93    pub fn set_key_pair(&mut self, key_pair: AccountSecretKey) {
94        self.key_pair = key_pair
95    }
96
97    /// Returns the current [`Epoch`] the chain is in.
98    pub async fn epoch(&self) -> Epoch {
99        *Box::pin(self.validator.worker().chain_state_view(self.id()))
100            .await
101            .expect("Failed to load chain")
102            .execution_state
103            .system
104            .epoch
105            .get()
106    }
107
108    /// Reads the current shared balance available to all of the owners of this microchain.
109    pub async fn chain_balance(&self) -> Amount {
110        let query = Query::System(SystemQuery);
111
112        let (QueryOutcome { response, .. }, _) = self
113            .validator
114            .worker()
115            .query_application(self.id(), query, None)
116            .await
117            .expect("Failed to query chain's balance");
118
119        let QueryResponse::System(SystemResponse { balance, .. }) = response else {
120            panic!("Unexpected response from system application");
121        };
122
123        balance
124    }
125
126    /// Reads the current account balance on this microchain of an [`AccountOwner`].
127    pub async fn owner_balance(&self, owner: &AccountOwner) -> Option<Amount> {
128        let chain_state = Box::pin(self.validator.worker().chain_state_view(self.id()))
129            .await
130            .expect("Failed to read chain state");
131
132        chain_state
133            .execution_state
134            .system
135            .balances
136            .get(owner)
137            .await
138            .expect("Failed to read owner balance")
139    }
140
141    /// Reads the current account balance on this microchain of all [`AccountOwner`]s.
142    pub async fn owner_balances(
143        &self,
144        owners: impl IntoIterator<Item = AccountOwner>,
145    ) -> HashMap<AccountOwner, Option<Amount>> {
146        let chain_state = Box::pin(self.validator.worker().chain_state_view(self.id()))
147            .await
148            .expect("Failed to read chain state");
149
150        let mut balances = HashMap::new();
151
152        for owner in owners {
153            let balance = chain_state
154                .execution_state
155                .system
156                .balances
157                .get(&owner)
158                .await
159                .expect("Failed to read an owner's balance");
160
161            balances.insert(owner, balance);
162        }
163
164        balances
165    }
166
167    /// Reads a list of [`AccountOwner`]s that have a non-zero balance on this microchain.
168    pub async fn accounts(&self) -> Vec<AccountOwner> {
169        let chain_state = Box::pin(self.validator.worker().chain_state_view(self.id()))
170            .await
171            .expect("Failed to read chain state");
172
173        chain_state
174            .execution_state
175            .system
176            .balances
177            .indices()
178            .await
179            .expect("Failed to list accounts on the chain")
180    }
181
182    /// Reads all the non-zero account balances on this microchain.
183    pub async fn all_owner_balances(&self) -> HashMap<AccountOwner, Amount> {
184        self.owner_balances(self.accounts().await)
185            .await
186            .into_iter()
187            .map(|(owner, balance)| {
188                (
189                    owner,
190                    balance.expect("`accounts` should only return accounts with non-zero balance"),
191                )
192            })
193            .collect()
194    }
195
196    /// Adds a block to this microchain.
197    ///
198    /// The `block_builder` parameter is a closure that should use the [`BlockBuilder`] parameter
199    /// to provide the block's contents.
200    ///
201    /// Returns the block certificate and a [`ResourceTracker`] containing execution costs.
202    pub async fn add_block(
203        &self,
204        block_builder: impl FnOnce(&mut BlockBuilder),
205    ) -> (ConfirmedBlockCertificate, ResourceTracker) {
206        self.try_add_block(block_builder)
207            .await
208            .expect("Failed to execute block.")
209    }
210
211    /// Adds a block to this microchain, passing the blobs to be used during certificate handling.
212    ///
213    /// The `block_builder` parameter is a closure that should use the [`BlockBuilder`] parameter
214    /// to provide the block's contents.
215    ///
216    /// Returns the block certificate and a [`ResourceTracker`] containing execution costs.
217    pub async fn add_block_with_blobs(
218        &self,
219        block_builder: impl FnOnce(&mut BlockBuilder),
220        blobs: Vec<Blob>,
221    ) -> (ConfirmedBlockCertificate, ResourceTracker) {
222        self.try_add_block_with_blobs(block_builder, blobs)
223            .await
224            .expect("Failed to execute block.")
225    }
226
227    /// Tries to add a block to this microchain.
228    ///
229    /// The `block_builder` parameter is a closure that should use the [`BlockBuilder`] parameter
230    /// to provide the block's contents.
231    ///
232    /// Returns the block certificate and a [`ResourceTracker`] containing execution costs.
233    pub async fn try_add_block(
234        &self,
235        block_builder: impl FnOnce(&mut BlockBuilder),
236    ) -> Result<(ConfirmedBlockCertificate, ResourceTracker), WorkerError> {
237        self.try_add_block_with_blobs(block_builder, vec![]).await
238    }
239
240    /// Tries to add a block to this microchain, writing some `blobs` to storage if needed.
241    ///
242    /// The `block_builder` parameter is a closure that should use the [`BlockBuilder`] parameter
243    /// to provide the block's contents.
244    ///
245    /// The blobs are either all written to storage, if executing the block fails due to a missing
246    /// blob, or none are written to storage if executing the block succeeds without the blobs.
247    ///
248    /// Returns the block certificate and a [`ResourceTracker`] containing execution costs.
249    async fn try_add_block_with_blobs(
250        &self,
251        block_builder: impl FnOnce(&mut BlockBuilder),
252        blobs: Vec<Blob>,
253    ) -> Result<(ConfirmedBlockCertificate, ResourceTracker), WorkerError> {
254        let mut tip = self.tip.lock().await;
255        let mut block = BlockBuilder::new(
256            self.description.id(),
257            self.key_pair.public().into(),
258            Box::pin(self.epoch()).await,
259            tip.as_ref(),
260            self.validator.clone(),
261        );
262
263        block_builder(&mut block);
264
265        // TODO(#2066): Remove boxing once call-stack is shallower
266        let (certificate, resource_tracker) = Box::pin(block.try_sign(&blobs)).await?;
267
268        let result = self
269            .validator
270            .worker()
271            .fully_handle_certificate_with_notifications(certificate.clone(), &())
272            .await;
273        if let Err(WorkerError::BlobsNotFound(_)) = &result {
274            self.validator.storage().maybe_write_blobs(&blobs).await?;
275            self.validator
276                .worker()
277                .fully_handle_certificate_with_notifications(certificate.clone(), &())
278                .await
279                .expect("Rejected certificate");
280        } else {
281            result.expect("Rejected certificate");
282        }
283
284        *tip = Some(certificate.clone());
285
286        Ok((certificate, resource_tracker))
287    }
288
289    /// Receives all queued messages in all inboxes of this microchain.
290    ///
291    /// Adds a block to this microchain that receives all queued messages in the microchains
292    /// inboxes.
293    ///
294    /// Returns the certificate and resource tracker of the latest block added to the chain, if
295    /// any.
296    pub async fn handle_received_messages(
297        &self,
298    ) -> Option<(ConfirmedBlockCertificate, ResourceTracker)> {
299        let chain_id = self.id();
300        let information = self
301            .validator
302            .worker()
303            .handle_chain_info_query(ChainInfoQuery::new(chain_id).with_pending_message_bundles())
304            .await
305            .expect("Failed to query chain's pending messages");
306        let messages = information.info.requested_pending_message_bundles;
307        // Empty blocks are not allowed.
308        // Return early if there are no messages to process and we'd end up with an empty proposal.
309        if messages.is_empty() {
310            return None;
311        }
312        let result = Box::pin(self.add_block(|block| {
313            block.with_incoming_bundles(messages);
314        }))
315        .await;
316        Some(result)
317    }
318
319    /// Processes all new events from streams this chain subscribes to.
320    ///
321    /// Adds a block to this microchain that processes the new events.
322    ///
323    /// Returns the certificate and resource tracker of the block added to the chain.
324    pub async fn handle_new_events(&self) -> (ConfirmedBlockCertificate, ResourceTracker) {
325        let chain_id = self.id();
326        let worker = self.validator.worker();
327        let subscription_map = Box::pin(worker.chain_state_view(chain_id))
328            .await
329            .expect("Failed to query chain state view")
330            .execution_state
331            .system
332            .event_subscriptions
333            .index_values()
334            .await
335            .expect("Failed to query chain's event subscriptions");
336        let futures = subscription_map
337            .into_iter()
338            .map(|((chain_id, stream_id), subscriptions)| {
339                let worker = worker.clone();
340                async move {
341                    let next_index = Box::pin(worker.chain_state_view(chain_id))
342                        .await
343                        .expect("Failed to query chain state view")
344                        .execution_state
345                        .system
346                        .stream_event_counts
347                        .get(&stream_id)
348                        .await
349                        .expect("Failed to query chain's event counts");
350                    let Some(next_index) =
351                        next_index.filter(|next_index| *next_index > subscriptions.min_next_index)
352                    else {
353                        return Vec::new();
354                    };
355                    subscriptions
356                        .applications
357                        .into_iter()
358                        .filter(|(_, app_index)| *app_index < next_index)
359                        .map(|(application_id, _)| SystemOperation::UpdateStream {
360                            application_id,
361                            chain_id,
362                            stream_id: stream_id.clone(),
363                            next_index,
364                        })
365                        .collect::<Vec<_>>()
366                }
367            });
368        let updates: Vec<SystemOperation> = future::join_all(futures)
369            .await
370            .into_iter()
371            .flatten()
372            .collect();
373        assert!(!updates.is_empty(), "No new events to process");
374
375        Box::pin(self.add_block(|block| {
376            for update in updates {
377                block.with_system_operation(update);
378            }
379        }))
380        .await
381    }
382
383    /// Publishes the module in the crate calling this method to this microchain.
384    ///
385    /// Searches the Cargo manifest for binaries that end with `contract` and `service`, builds
386    /// them for WebAssembly and uses the generated binaries as the contract and service bytecode files
387    /// to be published on this chain. Returns the module ID to reference the published module.
388    pub async fn publish_current_module<Abi, Parameters, InstantiationArgument>(
389        &self,
390    ) -> ModuleId<Abi, Parameters, InstantiationArgument> {
391        Box::pin(self.publish_bytecode_files_in(".")).await
392    }
393
394    /// Publishes the bytecode files in the crate at `repository_path`.
395    ///
396    /// Searches the Cargo manifest for binaries that end with `contract` and `service`, builds
397    /// them for WebAssembly and uses the generated binaries as the contract and service bytecode files
398    /// to be published on this chain. Returns the module ID to reference the published module.
399    pub async fn publish_bytecode_files_in<Abi, Parameters, InstantiationArgument>(
400        &self,
401        repository_path: impl AsRef<Path>,
402    ) -> ModuleId<Abi, Parameters, InstantiationArgument> {
403        let repository_path = fs::canonicalize(repository_path)
404            .await
405            .expect("Failed to obtain absolute application repository path");
406        Self::build_bytecode_files_in(&repository_path);
407        let (contract, service) = Self::find_compressed_bytecode_files_in(&repository_path).await;
408        let contract_blob = Blob::new_contract_bytecode(contract);
409        let service_blob = Blob::new_service_bytecode(service);
410        let contract_blob_hash = contract_blob.id().hash;
411        let service_blob_hash = service_blob.id().hash;
412        let vm_runtime = VmRuntime::Wasm;
413
414        let module_id = ModuleId::new(contract_blob_hash, service_blob_hash, vm_runtime);
415
416        let (certificate, _) = Box::pin(self.add_block_with_blobs(
417            |block| {
418                block.with_system_operation(SystemOperation::PublishModule { module_id });
419            },
420            vec![contract_blob, service_blob],
421        ))
422        .await;
423
424        let block = certificate.inner().block();
425        assert_eq!(block.messages().len(), 1);
426        assert_eq!(block.messages()[0].len(), 0);
427
428        module_id.with_abi()
429    }
430
431    /// Compiles the crate in the `repository` path.
432    pub fn build_bytecode_files_in(repository: &Path) {
433        let output = std::process::Command::new("cargo")
434            .args(["build", "--release", "--target", "wasm32-unknown-unknown"])
435            .current_dir(repository)
436            .output()
437            .expect("Failed to build Wasm binaries");
438
439        assert!(
440            output.status.success(),
441            "Failed to build bytecode binaries.\nstdout: {}\nstderr: {}",
442            String::from_utf8_lossy(&output.stdout),
443            String::from_utf8_lossy(&output.stderr)
444        );
445    }
446
447    /// Searches the Cargo manifest of the crate calling this method for binaries to use as the
448    /// contract and service bytecode files.
449    ///
450    /// Returns a tuple with the loaded contract and service [`Bytecode`]s,
451    /// ready to be published.
452    pub async fn find_bytecode_files_in(repository: &Path) -> (Bytecode, Bytecode) {
453        let manifest_path = repository.join("Cargo.toml");
454        let cargo_manifest =
455            Manifest::from_path(manifest_path).expect("Failed to load Cargo.toml manifest");
456
457        let binaries = cargo_manifest
458            .bin
459            .into_iter()
460            .filter_map(|binary| binary.name)
461            .filter(|name| name.ends_with("service") || name.ends_with("contract"))
462            .collect::<Vec<_>>();
463
464        assert_eq!(
465            binaries.len(),
466            2,
467            "Could not figure out contract and service bytecode binaries.\
468            Please specify them manually using `publish_module`."
469        );
470
471        let (contract_binary, service_binary) = if binaries[0].ends_with("contract") {
472            (&binaries[0], &binaries[1])
473        } else {
474            (&binaries[1], &binaries[0])
475        };
476
477        let base_path = Self::find_output_directory_of(repository)
478            .await
479            .expect("Failed to look for output binaries");
480        let contract_path = base_path.join(format!("{contract_binary}.wasm"));
481        let service_path = base_path.join(format!("{service_binary}.wasm"));
482
483        let contract = Bytecode::load_from_file(contract_path)
484            .await
485            .expect("Failed to load contract bytecode from file");
486        let service = Bytecode::load_from_file(service_path)
487            .await
488            .expect("Failed to load service bytecode from file");
489        (contract, service)
490    }
491
492    /// Returns a tuple with the loaded contract and service [`CompressedBytecode`]s,
493    /// ready to be published.
494    pub async fn find_compressed_bytecode_files_in(
495        repository: &Path,
496    ) -> (CompressedBytecode, CompressedBytecode) {
497        let (contract, service) = Self::find_bytecode_files_in(repository).await;
498        tokio::task::spawn_blocking(move || (contract.compress(), service.compress()))
499            .await
500            .expect("Failed to compress bytecode files")
501    }
502
503    /// Searches for the directory where the built WebAssembly binaries should be.
504    ///
505    /// Assumes that the binaries will be built and placed inside a
506    /// `target/wasm32-unknown-unknown/release` sub-directory. However, since the crate with the
507    /// binaries could be part of a workspace, that output sub-directory must be searched in parent
508    /// directories as well.
509    async fn find_output_directory_of(repository: &Path) -> Result<PathBuf, io::Error> {
510        let output_sub_directory = Path::new("target/wasm32-unknown-unknown/release");
511        let mut current_directory = repository;
512        let mut output_path = current_directory.join(output_sub_directory);
513
514        while !fs::try_exists(&output_path).await? {
515            current_directory = current_directory.parent().unwrap_or_else(|| {
516                panic!(
517                    "Failed to find Wasm binary output directory in {}",
518                    repository.display()
519                )
520            });
521
522            output_path = current_directory.join(output_sub_directory);
523        }
524
525        Ok(output_path)
526    }
527
528    /// Returns the height of the tip of this microchain.
529    pub async fn get_tip_height(&self) -> BlockHeight {
530        self.tip
531            .lock()
532            .await
533            .as_ref()
534            .expect("Block was not successfully added")
535            .inner()
536            .block()
537            .header
538            .height
539    }
540
541    /// Creates an application on this microchain, using the module referenced by `module_id`.
542    ///
543    /// Returns the [`ApplicationId`] of the created application.
544    ///
545    /// If necessary, this microchain will subscribe to the microchain that published the
546    /// module to use, and fetch it.
547    ///
548    /// The application is instantiated using the instantiation parameters, which consist of the
549    /// global static `parameters`, the one time `instantiation_argument` and the
550    /// `required_application_ids` of the applications that the new application will depend on.
551    pub async fn create_application<Abi, Parameters, InstantiationArgument>(
552        &mut self,
553        module_id: ModuleId<Abi, Parameters, InstantiationArgument>,
554        parameters: Parameters,
555        instantiation_argument: InstantiationArgument,
556        required_application_ids: Vec<ApplicationId>,
557    ) -> ApplicationId<Abi>
558    where
559        Abi: ContractAbi,
560        Parameters: Serialize,
561        InstantiationArgument: Serialize,
562    {
563        let parameters = serde_json::to_vec(&parameters).unwrap();
564        let instantiation_argument = serde_json::to_vec(&instantiation_argument).unwrap();
565
566        let (creation_certificate, _) = Box::pin(self.add_block(|block| {
567            block.with_system_operation(SystemOperation::CreateApplication {
568                module_id: module_id.forget_abi(),
569                parameters: parameters.clone(),
570                instantiation_argument,
571                required_application_ids: required_application_ids.clone(),
572            });
573        }))
574        .await;
575
576        let block = creation_certificate.inner().block();
577        assert_eq!(block.messages().len(), 1);
578
579        let description = ApplicationDescription {
580            module_id: module_id.forget_abi(),
581            creator_chain_id: block.header.chain_id,
582            block_height: block.header.height,
583            application_index: 0,
584            parameters,
585            required_application_ids,
586        };
587
588        ApplicationId::<()>::from(&description).with_abi()
589    }
590
591    /// Fallible version of [`create_application`](Self::create_application).
592    ///
593    /// Returns the [`ApplicationId`] on success, or a [`WorkerError`] if instantiation fails.
594    pub async fn try_create_application<Abi, Parameters, InstantiationArgument>(
595        &mut self,
596        module_id: ModuleId<Abi, Parameters, InstantiationArgument>,
597        parameters: Parameters,
598        instantiation_argument: InstantiationArgument,
599        required_application_ids: Vec<ApplicationId>,
600    ) -> Result<ApplicationId<Abi>, WorkerError>
601    where
602        Abi: ContractAbi,
603        Parameters: Serialize,
604        InstantiationArgument: Serialize,
605    {
606        let parameters = serde_json::to_vec(&parameters).unwrap();
607        let instantiation_argument = serde_json::to_vec(&instantiation_argument).unwrap();
608
609        let (creation_certificate, _) = self
610            .try_add_block(|block| {
611                block.with_system_operation(SystemOperation::CreateApplication {
612                    module_id: module_id.forget_abi(),
613                    parameters: parameters.clone(),
614                    instantiation_argument,
615                    required_application_ids: required_application_ids.clone(),
616                });
617            })
618            .await?;
619
620        let block = creation_certificate.inner().block();
621
622        let description = ApplicationDescription {
623            module_id: module_id.forget_abi(),
624            creator_chain_id: block.header.chain_id,
625            block_height: block.header.height,
626            application_index: 0,
627            parameters,
628            required_application_ids,
629        };
630
631        Ok(ApplicationId::<()>::from(&description).with_abi())
632    }
633
634    /// Returns whether this chain has been closed.
635    pub async fn is_closed(&self) -> bool {
636        let chain = Box::pin(self.validator.worker().chain_state_view(self.id()))
637            .await
638            .expect("Failed to load chain");
639        *chain.execution_state.system.closed.get()
640    }
641
642    /// Executes a `query` on an `application`'s state on this microchain.
643    ///
644    /// Returns the deserialized response from the `application`.
645    pub async fn query<Abi>(
646        &self,
647        application_id: ApplicationId<Abi>,
648        query: Abi::Query,
649    ) -> QueryOutcome<Abi::QueryResponse>
650    where
651        Abi: ServiceAbi,
652    {
653        self.try_query(application_id, query)
654            .await
655            .expect("Failed to execute application service query")
656    }
657
658    /// Attempts to execute a `query` on an `application`'s state on this microchain.
659    ///
660    /// Returns the deserialized response from the `application`.
661    pub async fn try_query<Abi>(
662        &self,
663        application_id: ApplicationId<Abi>,
664        query: Abi::Query,
665    ) -> Result<QueryOutcome<Abi::QueryResponse>, TryQueryError>
666    where
667        Abi: ServiceAbi,
668    {
669        let query_bytes = serde_json::to_vec(&query)?;
670
671        let (
672            QueryOutcome {
673                response,
674                operations,
675            },
676            _,
677        ) = self
678            .validator
679            .worker()
680            .query_application(
681                self.id(),
682                Query::User {
683                    application_id: application_id.forget_abi(),
684                    bytes: query_bytes,
685                },
686                None,
687            )
688            .await?;
689
690        let deserialized_response = match response {
691            QueryResponse::User(bytes) => {
692                serde_json::from_slice(&bytes).expect("Failed to deserialize query response")
693            }
694            QueryResponse::System(_) => {
695                unreachable!("User query returned a system response")
696            }
697        };
698
699        Ok(QueryOutcome {
700            response: deserialized_response,
701            operations,
702        })
703    }
704
705    /// Executes a GraphQL `query` on an `application`'s state on this microchain.
706    ///
707    /// Returns the deserialized GraphQL JSON response from the `application`.
708    pub async fn graphql_query<Abi>(
709        &self,
710        application_id: ApplicationId<Abi>,
711        query: impl Into<async_graphql::Request>,
712    ) -> QueryOutcome<serde_json::Value>
713    where
714        Abi: ServiceAbi<Query = async_graphql::Request, QueryResponse = async_graphql::Response>,
715    {
716        let query = query.into();
717        let query_str = query.query.clone();
718
719        self.try_graphql_query(application_id, query)
720            .await
721            .unwrap_or_else(|error| panic!("Service query {query_str:?} failed: {error}"))
722    }
723
724    /// Attempts to execute a GraphQL `query` on an `application`'s state on this microchain.
725    ///
726    /// Returns the deserialized GraphQL JSON response from the `application`.
727    pub async fn try_graphql_query<Abi>(
728        &self,
729        application_id: ApplicationId<Abi>,
730        query: impl Into<async_graphql::Request>,
731    ) -> Result<QueryOutcome<serde_json::Value>, TryGraphQLQueryError>
732    where
733        Abi: ServiceAbi<Query = async_graphql::Request, QueryResponse = async_graphql::Response>,
734    {
735        let query = query.into();
736        let QueryOutcome {
737            response,
738            operations,
739        } = self.try_query(application_id, query).await?;
740
741        if !response.errors.is_empty() {
742            return Err(TryGraphQLQueryError::Service(response.errors));
743        }
744        let json_response = response.data.into_json()?;
745
746        Ok(QueryOutcome {
747            response: json_response,
748            operations,
749        })
750    }
751
752    /// Executes a GraphQL `mutation` on an `application` and proposes a block with the resulting
753    /// scheduled operations.
754    ///
755    /// Returns the certificate of the new block.
756    pub async fn graphql_mutation<Abi>(
757        &self,
758        application_id: ApplicationId<Abi>,
759        query: impl Into<async_graphql::Request>,
760    ) -> ConfirmedBlockCertificate
761    where
762        Abi: ServiceAbi<Query = async_graphql::Request, QueryResponse = async_graphql::Response>,
763    {
764        self.try_graphql_mutation(application_id, query)
765            .await
766            .expect("Failed to execute service GraphQL mutation")
767    }
768
769    /// Attempts to execute a GraphQL `mutation` on an `application` and proposes a block with the
770    /// resulting scheduled operations.
771    ///
772    /// Returns the certificate of the new block.
773    pub async fn try_graphql_mutation<Abi>(
774        &self,
775        application_id: ApplicationId<Abi>,
776        query: impl Into<async_graphql::Request>,
777    ) -> Result<ConfirmedBlockCertificate, TryGraphQLMutationError>
778    where
779        Abi: ServiceAbi<Query = async_graphql::Request, QueryResponse = async_graphql::Response>,
780    {
781        let QueryOutcome { operations, .. } = self.try_graphql_query(application_id, query).await?;
782
783        let (certificate, _) = Box::pin(self.try_add_block(|block| {
784            for operation in operations {
785                match operation {
786                    Operation::User {
787                        application_id,
788                        bytes,
789                    } => {
790                        block.with_raw_operation(application_id, bytes);
791                    }
792                    Operation::System(system_operation) => {
793                        block.with_system_operation(*system_operation);
794                    }
795                }
796            }
797        }))
798        .await?;
799
800        Ok(certificate)
801    }
802
803    /// Queries the balance of an account owned by `account_owner` on this chain.
804    pub async fn query_account(
805        &self,
806        application_id: ApplicationId<FungibleTokenAbi>,
807        account_owner: AccountOwner,
808    ) -> Option<Amount> {
809        use async_graphql::InputType as _;
810
811        let query = format!(
812            "query {{ accounts {{ entry(key: {}) {{ value }} }} }}",
813            account_owner.to_value()
814        );
815        let QueryOutcome { response, .. } = self.graphql_query(application_id, query).await;
816        let balance = response.pointer("/accounts/entry/value")?.as_str()?;
817
818        Some(
819            balance
820                .parse()
821                .expect("Account balance cannot be parsed as a number"),
822        )
823    }
824
825    /// Queries the allowance for an owner-spender pair on this chain.
826    pub async fn query_allowance(
827        &self,
828        application_id: ApplicationId<FungibleTokenAbi>,
829        owner: AccountOwner,
830        spender: AccountOwner,
831    ) -> Option<Amount> {
832        use async_graphql::InputType as _;
833
834        let owner_spender = OwnerSpender::new(owner, spender);
835        let query = format!(
836            "query {{ allowances {{ entry(key: {}) {{ value }} }} }}",
837            owner_spender.to_value()
838        );
839        let QueryOutcome { response, .. } = self.graphql_query(application_id, query).await;
840        let allowance = response.pointer("/allowances/entry/value")?.as_str()?;
841
842        Some(
843            allowance
844                .parse()
845                .expect("Allowance cannot be parsed as a number"),
846        )
847    }
848}
849
850/// Failure to query an application's service on a chain.
851#[derive(Debug, thiserror::Error)]
852pub enum TryQueryError {
853    /// The query request failed to serialize to JSON.
854    #[error("Failed to serialize query request")]
855    Serialization(#[from] serde_json::Error),
856
857    /// Executing the service to handle the query failed.
858    #[error("Failed to execute service query")]
859    Execution(#[from] WorkerError),
860}
861
862/// Failure to perform a GraphQL query on an application on a chain.
863#[derive(Debug, thiserror::Error)]
864pub enum TryGraphQLQueryError {
865    /// The [`async_graphql::Request`] failed to serialize to JSON.
866    #[error("Failed to serialize GraphQL query request")]
867    RequestSerialization(#[source] serde_json::Error),
868
869    /// Execution of the service failed.
870    #[error("Failed to execute service query")]
871    Execution(#[from] WorkerError),
872
873    /// The response returned from the service was not valid JSON.
874    #[error("Unexpected non-JSON service query response")]
875    ResponseDeserialization(#[from] serde_json::Error),
876
877    /// The service reported some errors.
878    #[error("Service returned errors: {_0:#?}")]
879    Service(Vec<async_graphql::ServerError>),
880}
881
882impl From<TryQueryError> for TryGraphQLQueryError {
883    fn from(query_error: TryQueryError) -> Self {
884        match query_error {
885            TryQueryError::Serialization(error) => {
886                TryGraphQLQueryError::RequestSerialization(error)
887            }
888            TryQueryError::Execution(error) => TryGraphQLQueryError::Execution(error),
889        }
890    }
891}
892
893impl TryGraphQLQueryError {
894    /// Returns the inner [`ExecutionError`] in this error.
895    ///
896    /// # Panics
897    ///
898    /// If this is not caused by an [`ExecutionError`].
899    pub fn expect_execution_error(self) -> ExecutionError {
900        let TryGraphQLQueryError::Execution(worker_error) = self else {
901            panic!("Expected an `ExecutionError`. Got: {self:#?}");
902        };
903
904        worker_error.expect_execution_error(ChainExecutionContext::Query)
905    }
906}
907
908/// Failure to perform a GraphQL mutation on an application on a chain.
909#[derive(Debug, thiserror::Error)]
910pub enum TryGraphQLMutationError {
911    /// The GraphQL query for the mutation failed.
912    #[error(transparent)]
913    Query(#[from] TryGraphQLQueryError),
914
915    /// The block with the mutation's scheduled operations failed to be proposed.
916    #[error("Failed to propose block with operations scheduled by the GraphQL mutation")]
917    Proposal(#[from] WorkerError),
918}
919
920impl TryGraphQLMutationError {
921    /// Returns the inner [`ExecutionError`] in this [`TryGraphQLMutationError::Proposal`] error.
922    ///
923    /// # Panics
924    ///
925    /// If this is not caused by an [`ExecutionError`] during a block proposal.
926    pub fn expect_proposal_execution_error(self, transaction_index: u32) -> ExecutionError {
927        let TryGraphQLMutationError::Proposal(proposal_error) = self else {
928            panic!("Expected an `ExecutionError` during the block proposal. Got: {self:#?}");
929        };
930
931        proposal_error.expect_execution_error(ChainExecutionContext::Operation(transaction_index))
932    }
933}