oasis_runtime_sdk/modules/rofl/app/
env.rs

1use std::{collections::BTreeMap, sync::Arc};
2
3use anyhow::{anyhow, Result};
4use tokio::sync::mpsc;
5
6use crate::{
7    core::{common::namespace::Namespace, host::Host, identity::Identity, protocol::Protocol},
8    crypto::signature::Signer,
9};
10
11use super::{client, processor, App};
12
13/// Application environment.
14pub struct Environment<A: App> {
15    app: Arc<A>,
16    client: client::Client<A>,
17    signer: Arc<dyn Signer>,
18    identity: Arc<Identity>,
19    host: Arc<Protocol>,
20    cmdq: mpsc::WeakSender<processor::Command>,
21}
22
23impl<A> Environment<A>
24where
25    A: App,
26{
27    /// Create a new environment talking to the given processor.
28    pub(super) fn new(
29        state: Arc<processor::State<A>>,
30        cmdq: mpsc::WeakSender<processor::Command>,
31    ) -> Self {
32        Self {
33            app: state.app.clone(),
34            signer: state.signer.clone(),
35            identity: state.identity.clone(),
36            host: state.host.clone(),
37            client: client::Client::new(state, cmdq.clone()),
38            cmdq,
39        }
40    }
41
42    /// Application instance.
43    pub fn app(&self) -> Arc<A> {
44        self.app.clone()
45    }
46
47    /// Runtime client.
48    pub fn client(&self) -> &client::Client<A> {
49        &self.client
50    }
51
52    /// Transaction signer.
53    pub fn signer(&self) -> Arc<dyn Signer> {
54        self.signer.clone()
55    }
56
57    /// Runtime identity.
58    pub fn identity(&self) -> Arc<Identity> {
59        self.identity.clone()
60    }
61
62    /// Host interface.
63    pub fn host(&self) -> Arc<dyn Host> {
64        self.host.clone()
65    }
66
67    /// Runtime identifier.
68    pub fn runtime_id(&self) -> Namespace {
69        self.host.get_runtime_id()
70    }
71
72    /// Untrusted local runtime configuration defined by the node operator where the node hosting
73    /// the ROFL app is running.
74    pub fn untrusted_local_config(&self) -> BTreeMap<String, cbor::Value> {
75        self.host.get_host_info().local_config
76    }
77
78    /// Send a command to the processor.
79    pub(super) async fn send_command(&self, cmd: processor::Command) -> Result<()> {
80        let cmdq = self
81            .cmdq
82            .upgrade()
83            .ok_or(anyhow!("processor has shut down"))?;
84        cmdq.send(cmd).await?;
85        Ok(())
86    }
87}
88
89impl<A> Clone for Environment<A>
90where
91    A: App,
92{
93    fn clone(&self) -> Self {
94        Self {
95            app: self.app.clone(),
96            signer: self.signer.clone(),
97            identity: self.identity.clone(),
98            host: self.host.clone(),
99            client: self.client.clone(),
100            cmdq: self.cmdq.clone(),
101        }
102    }
103}