oasis_core_runtime/app/
application.rs

1//! Runtime apps.
2use std::sync::Arc;
3
4use anyhow::{bail, Result};
5use async_trait::async_trait;
6
7use crate::{
8    common::sgx,
9    consensus::roothash,
10    dispatcher::{Initializer, PostInitState, PreInitState},
11    host::Host,
12};
13
14use super::Config;
15
16/// A runtime application.
17#[allow(unused_variables)]
18#[async_trait]
19pub trait App: Send + Sync {
20    /// Whether this is a ROFL application.
21    fn is_rofl(&self) -> bool {
22        true
23    }
24
25    /// Returns the application's configuration settings.
26    fn get_config(&self) -> Config {
27        Config::default()
28    }
29
30    /// Called on application initialization.
31    fn on_init(&mut self, host: Arc<dyn Host>) -> Result<()> {
32        // Default implementation does nothing.
33        Ok(())
34    }
35
36    /// Quote policy to use for verifying our own enclave identity.
37    async fn quote_policy(&self) -> Result<sgx::QuotePolicy> {
38        // Default implementation uses a sane policy.
39        Ok(sgx::QuotePolicy {
40            ias: Some(sgx::ias::QuotePolicy {
41                disabled: true, // Disable legacy EPID attestation.
42                ..Default::default()
43            }),
44            pcs: Some(sgx::pcs::QuotePolicy {
45                // Allow TDX since that is not part of the default policy.
46                tdx: Some(sgx::pcs::TdxQuotePolicy {
47                    allowed_tdx_modules: vec![],
48                }),
49                ..Default::default()
50            }),
51        })
52    }
53
54    /// Called on new runtime block being received.
55    async fn on_runtime_block(&self, blk: &roothash::AnnotatedBlock) -> Result<()> {
56        // Default implementation does nothing.
57        Ok(())
58    }
59
60    /// Called on new runtime event being detected.
61    async fn on_runtime_event(
62        &self,
63        blk: &roothash::AnnotatedBlock,
64        tags: &[Vec<u8>],
65    ) -> Result<()> {
66        // Default implementation does nothing.
67        Ok(())
68    }
69
70    /// Called for runtime queries.
71    async fn query(&self, method: &str, args: Vec<u8>) -> Result<Vec<u8>> {
72        // Default implementation rejects all requests.
73        bail!("method not supported");
74    }
75}
76
77/// An application which doesn't do anything.
78pub struct NoopApp;
79
80#[async_trait]
81impl App for NoopApp {
82    fn is_rofl(&self) -> bool {
83        false
84    }
85}
86
87/// Create a new runtime initializer for an application.
88pub fn new(app: Box<dyn App>) -> Box<dyn Initializer> {
89    Box::new(|_state: PreInitState<'_>| -> PostInitState {
90        PostInitState {
91            app: Some(app),
92            ..Default::default()
93        }
94    })
95}