substrate_api_client/api/runtime_api/
block_builder.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, UncheckedExtrinsic};
17#[cfg(all(not(feature = "sync-api"), not(feature = "std")))]
18use alloc::boxed::Box;
19use alloc::{vec, vec::Vec};
20use sp_core::{Bytes, Encode};
21use sp_inherents::{CheckInherentsResult, InherentData};
22use sp_runtime::ApplyExtrinsicResult;
23
24#[maybe_async::maybe_async(?Send)]
25pub trait BlockBuilderApi: RuntimeApi {
26	type ApplyExtrinsicResult;
27	type Block;
28	type InherentData;
29	type CheckInherentsResult;
30	type Header;
31
32	/// Apply the given extrinsic.
33	async fn apply_extrinsic<Address, Call, Signature, TransactionExtension>(
34		&self,
35		extrinsic: UncheckedExtrinsic<Address, Call, Signature, TransactionExtension>,
36		at_block: Option<Self::Hash>,
37	) -> Result<Self::ApplyExtrinsicResult>
38	where
39		Address: Encode,
40		Call: Encode,
41		Signature: Encode,
42		TransactionExtension: Encode;
43
44	/// Apply the given opaque extrinsic.
45	async fn apply_opaque_extrinsic(
46		&self,
47		extrinsic: Vec<u8>,
48		at_block: Option<Self::Hash>,
49	) -> Result<Self::ApplyExtrinsicResult>;
50
51	/// Check that the inherents are valid.
52	async fn check_inherents(
53		&self,
54		block: Self::Block,
55		data: Self::InherentData,
56		at_block: Option<Self::Hash>,
57	) -> Result<Self::CheckInherentsResult>;
58
59	/// Finish the current block.
60	async fn finalize_block(&self, at_block: Option<Self::Hash>) -> Result<Self::Header>;
61
62	/// Generate inherent extrinsics and return them as encoded Bytes.
63	async fn inherent_extrinsics(
64		&self,
65		inherent: Self::InherentData,
66		at_block: Option<Self::Hash>,
67	) -> Result<Vec<Bytes>>;
68}
69
70#[maybe_async::maybe_async(?Send)]
71impl<T, Client> BlockBuilderApi for RuntimeApiClient<T, Client>
72where
73	T: Config,
74	Client: Request,
75{
76	type ApplyExtrinsicResult = ApplyExtrinsicResult;
77	type Block = T::Block;
78	type InherentData = InherentData;
79	type CheckInherentsResult = CheckInherentsResult;
80	type Header = T::Header;
81
82	async fn apply_extrinsic<Address, Call, Signature, TransactionExtension>(
83		&self,
84		extrinsic: UncheckedExtrinsic<Address, Call, Signature, TransactionExtension>,
85		at_block: Option<Self::Hash>,
86	) -> Result<Self::ApplyExtrinsicResult>
87	where
88		Address: Encode,
89		Call: Encode,
90		Signature: Encode,
91		TransactionExtension: Encode,
92	{
93		self.apply_opaque_extrinsic(extrinsic.encode(), at_block).await
94	}
95
96	async fn apply_opaque_extrinsic(
97		&self,
98		extrinsic: Vec<u8>,
99		at_block: Option<Self::Hash>,
100	) -> Result<Self::ApplyExtrinsicResult> {
101		self.runtime_call("BlockBuilder_apply_extrinsic", vec![extrinsic], at_block)
102			.await
103	}
104
105	async fn check_inherents(
106		&self,
107		block: Self::Block,
108		data: Self::InherentData,
109		at_block: Option<Self::Hash>,
110	) -> Result<Self::CheckInherentsResult> {
111		self.runtime_call(
112			"BlockBuilder_check_inherents",
113			vec![block.encode(), data.encode()],
114			at_block,
115		)
116		.await
117	}
118
119	async fn finalize_block(&self, at_block: Option<Self::Hash>) -> Result<Self::Header> {
120		self.runtime_call("BlockBuilder_finalize_block", vec![], at_block).await
121	}
122
123	async fn inherent_extrinsics(
124		&self,
125		inherent: Self::InherentData,
126		at_block: Option<Self::Hash>,
127	) -> Result<Vec<Bytes>> {
128		self.runtime_call("BlockBuilder_inherent_extrinsics", vec![inherent.encode()], at_block)
129			.await
130	}
131}