oasis_runtime_sdk/
history.rs1use oasis_core_runtime::{
3 consensus::{state::ConsensusState, verifier::Verifier, Event},
4 future::block_on,
5 types::EventKind,
6};
7
8const MODULE_NAME: &str = "history";
10
11#[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
23pub trait HistoryHost {
25 fn consensus_state_at(&self, height: u64) -> Result<ConsensusState, Error>;
27
28 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}