Skip to main content

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