oasis_runtime_sdk/
history.rs

1//! Historic state access.
2use oasis_core_runtime::{
3    consensus::{state::ConsensusState, verifier::Verifier, Event},
4    future::block_on,
5    types::EventKind,
6};
7
8/// Unique module name.
9const MODULE_NAME: &str = "history";
10
11/// History host errors.
12#[derive(Debug, thiserror::Error, oasis_runtime_sdk_macros::Error)]
13pub enum Error {
14    #[error("failed to fetch block from host")]
15    #[sdk_error(code = 1)]
16    FailedToFetchBlock,
17
18    #[error("failed to fetch events from host")]
19    #[sdk_error(code = 2)]
20    FailedToFetchEvents,
21}
22
23/// Interface to the runtime host to fetch historic information.
24pub trait HistoryHost {
25    /// Fetch historic consensus state after executing the block at given height.
26    fn consensus_state_at(&self, height: u64) -> Result<ConsensusState, Error>;
27
28    /// Fetch events emitted during execution of the block at given height.
29    fn consensus_events_at(&self, height: u64, kind: EventKind) -> Result<Vec<Event>, Error>;
30}
31
32impl HistoryHost for Box<dyn HistoryHost> {
33    fn consensus_state_at(&self, height: u64) -> Result<ConsensusState, Error> {
34        HistoryHost::consensus_state_at(&**self, height)
35    }
36
37    fn consensus_events_at(&self, height: u64, kind: EventKind) -> Result<Vec<Event>, Error> {
38        HistoryHost::consensus_events_at(&**self, height, kind)
39    }
40}
41
42impl<V: Verifier> HistoryHost for V {
43    fn consensus_state_at(&self, height: u64) -> Result<ConsensusState, Error> {
44        block_on(self.state_at(height)).map_err(|_| Error::FailedToFetchBlock)
45    }
46
47    fn consensus_events_at(&self, height: u64, kind: EventKind) -> Result<Vec<Event>, Error> {
48        block_on(self.events_at(height, kind)).map_err(|_| Error::FailedToFetchEvents)
49    }
50}