1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use codec::Codec;
use jsonrpsee::{
core::{async_trait, Error as JsonRpseeError, RpcResult},
proc_macros::rpc,
types::error::{CallError, ErrorCode, ErrorObject},
};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::{traits::Block as BlockT, DispatchError};
use std::sync::Arc;
pub use btc_relay_rpc_runtime_api::BtcRelayApi as BtcRelayRuntimeApi;
#[rpc(client, server)]
pub trait BtcRelayApi<BlockHash, H256Le> {
#[method(name = "btcRelay_verifyBlockHeaderInclusion")]
fn verify_block_header_inclusion(
&self,
block_hash: H256Le,
at: Option<BlockHash>,
) -> RpcResult<Result<(), DispatchError>>;
}
fn internal_err<T: ToString>(message: T) -> JsonRpseeError {
JsonRpseeError::Call(CallError::Custom(ErrorObject::owned(
ErrorCode::InternalError.code(),
message.to_string(),
None::<()>,
)))
}
pub struct BtcRelay<C, B> {
client: Arc<C>,
_marker: std::marker::PhantomData<B>,
}
impl<C, B> BtcRelay<C, B> {
pub fn new(client: Arc<C>) -> Self {
BtcRelay {
client,
_marker: Default::default(),
}
}
}
#[async_trait]
impl<C, Block, H256Le> BtcRelayApiServer<<Block as BlockT>::Hash, H256Le> for BtcRelay<C, Block>
where
Block: BlockT,
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: BtcRelayRuntimeApi<Block, H256Le>,
H256Le: Codec,
{
fn verify_block_header_inclusion(
&self,
block_hash: H256Le,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Result<(), DispatchError>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);
api.verify_block_header_inclusion(at, block_hash)
.map_err(|e| internal_err(format!("execution error: Unable to dry run extrinsic {:?}", e)))
}
}