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