substrate_api_client/api/runtime_api/
api_core.rs

1/*
2   Copyright 2024 Supercomputing Systems AG
3   Licensed under the Apache License, Version 2.0 (the "License");
4   you may not use this file except in compliance with the License.
5   You may obtain a copy of the License at
6	   http://www.apache.org/licenses/LICENSE-2.0
7   Unless required by applicable law or agreed to in writing, software
8   distributed under the License is distributed on an "AS IS" BASIS,
9   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10   See the License for the specific language governing permissions and
11   limitations under the License.
12*/
13
14use super::{RuntimeApi, RuntimeApiClient};
15use crate::{api::Result, rpc::Request};
16use ac_primitives::{config::Config, RuntimeVersion};
17#[cfg(all(not(feature = "sync-api"), not(feature = "std")))]
18use alloc::boxed::Box;
19use alloc::vec;
20use sp_core::{Bytes, Encode};
21
22#[maybe_async::maybe_async(?Send)]
23pub trait CoreApi: RuntimeApi {
24	type Block;
25	type Header;
26	type RuntimeVersion;
27
28	/// Execute the given block.
29	async fn execute_block(&self, block: Self::Block, at_block: Option<Self::Hash>) -> Result<()>;
30
31	/// Execute the given opaque block.
32	async fn execute_opaque_block(&self, block: Bytes, at_block: Option<Self::Hash>) -> Result<()>;
33
34	/// Initialize a block with the given header.
35	async fn initialize_block(
36		&self,
37		header: Self::Header,
38		at_block: Option<Self::Hash>,
39	) -> Result<()>;
40
41	/// Returns the version of the runtime.
42	async fn version(&self, at_block: Option<Self::Hash>) -> Result<Self::RuntimeVersion>;
43}
44
45#[maybe_async::maybe_async(?Send)]
46impl<T, Client> CoreApi for RuntimeApiClient<T, Client>
47where
48	T: Config,
49	Client: Request,
50{
51	type Block = T::Block;
52	type Header = T::Header;
53	type RuntimeVersion = RuntimeVersion;
54
55	async fn execute_block(&self, block: Self::Block, at_block: Option<Self::Hash>) -> Result<()> {
56		self.execute_opaque_block(block.encode().into(), at_block).await
57	}
58
59	async fn execute_opaque_block(&self, block: Bytes, at_block: Option<Self::Hash>) -> Result<()> {
60		self.runtime_call("Core_execute_block", vec![block.0], at_block).await
61	}
62
63	async fn initialize_block(
64		&self,
65		header: Self::Header,
66		at_block: Option<Self::Hash>,
67	) -> Result<()> {
68		self.runtime_call("Core_initialize_block", vec![header.encode()], at_block)
69			.await
70	}
71
72	async fn version(&self, at_block: Option<Self::Hash>) -> Result<Self::RuntimeVersion> {
73		self.runtime_call("Core_version", vec![], at_block).await
74	}
75}