oasis_core_runtime/consensus/tendermint/verifier/
handle.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4use crossbeam::channel;
5use tokio::sync::oneshot;
6
7use crate::{
8    consensus::{
9        beacon::EpochTime,
10        roothash::Header,
11        state::ConsensusState,
12        tendermint::decode_light_block,
13        verifier::{self, Error},
14        Event, LightBlock,
15    },
16    protocol::Protocol,
17    types::EventKind,
18};
19
20use super::types::Command;
21
22pub struct Handle {
23    pub protocol: Arc<Protocol>,
24    pub command_sender: channel::Sender<Command>,
25}
26
27#[async_trait]
28impl verifier::Verifier for Handle {
29    async fn sync(&self, height: u64) -> Result<(), Error> {
30        let (sender, receiver) = oneshot::channel();
31        self.command_sender
32            .send(Command::Synchronize(height, sender))
33            .map_err(|_| Error::Internal)?;
34
35        receiver.await.map_err(|_| Error::Internal)?
36    }
37
38    async fn verify(
39        &self,
40        consensus_block: LightBlock,
41        runtime_header: Header,
42        epoch: EpochTime,
43    ) -> Result<ConsensusState, Error> {
44        let (sender, receiver) = oneshot::channel();
45        self.command_sender
46            .send(Command::Verify(
47                consensus_block,
48                runtime_header,
49                epoch,
50                sender,
51                false,
52            ))
53            .map_err(|_| Error::Internal)?;
54
55        receiver.await.map_err(|_| Error::Internal)?
56    }
57
58    async fn verify_for_query(
59        &self,
60        consensus_block: LightBlock,
61        runtime_header: Header,
62        epoch: EpochTime,
63    ) -> Result<ConsensusState, Error> {
64        let (sender, receiver) = oneshot::channel();
65        self.command_sender
66            .send(Command::Verify(
67                consensus_block,
68                runtime_header,
69                epoch,
70                sender,
71                true,
72            ))
73            .map_err(|_| Error::Internal)?;
74
75        receiver.await.map_err(|_| Error::Internal)?
76    }
77
78    async fn unverified_state(&self, consensus_block: LightBlock) -> Result<ConsensusState, Error> {
79        let untrusted_block =
80            decode_light_block(consensus_block).map_err(Error::VerificationFailed)?;
81        // NOTE: No actual verification is performed.
82        let state_root = untrusted_block.get_state_root();
83        Ok(ConsensusState::from_protocol(
84            self.protocol.clone(),
85            state_root.version + 1,
86            state_root,
87        ))
88    }
89
90    async fn latest_state(&self) -> Result<ConsensusState, Error> {
91        let (sender, receiver) = oneshot::channel();
92        self.command_sender
93            .send(Command::LatestState(sender))
94            .map_err(|_| Error::Internal)?;
95
96        receiver.await.map_err(|_| Error::Internal)?
97    }
98
99    async fn state_at(&self, height: u64) -> Result<ConsensusState, Error> {
100        let (sender, receiver) = oneshot::channel();
101        self.command_sender
102            .send(Command::StateAt(height, sender))
103            .map_err(|_| Error::Internal)?;
104
105        receiver.await.map_err(|_| Error::Internal)?
106    }
107
108    async fn events_at(&self, height: u64, kind: EventKind) -> Result<Vec<Event>, Error> {
109        let (sender, receiver) = oneshot::channel();
110        self.command_sender
111            .send(Command::EventsAt(height, kind, sender))
112            .map_err(|_| Error::Internal)?;
113
114        receiver.await.map_err(|_| Error::Internal)?
115    }
116
117    async fn latest_height(&self) -> Result<u64, Error> {
118        let (sender, receiver) = oneshot::channel();
119        self.command_sender
120            .send(Command::LatestHeight(sender))
121            .map_err(|_| Error::Internal)?;
122
123        receiver.await.map_err(|_| Error::Internal)?
124    }
125}