referrerpolicy=no-referrer-when-downgrade

sc_hop/
runtime_api.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17//! Dynamic-dispatch wrappers for the HOP runtime API.
18//!
19//! Calling the API by SCALE-encoded bytes lets the node interact with any
20//! runtime that exposes the named methods, without imposing a static
21//! `HopRuntimeApi` bound on the client's `RuntimeApi` type. Detection happens
22//! via `ApiExt::has_api_with` at startup; from there the node either carries
23//! a promoter (which uses these wrappers) or runs cleanup-only.
24
25use codec::{Decode, Encode};
26use sp_api::{ApiError, CallApiAt, CallApiAtParams, CallContext};
27use sp_runtime::{traits::Block as BlockT, AccountId32, MultiSignature, MultiSigner};
28
29fn call<Block, C, Args, R>(
30	client: &C,
31	at: Block::Hash,
32	method: &'static str,
33	args: Args,
34) -> Result<R, ApiError>
35where
36	Block: BlockT,
37	C: CallApiAt<Block>,
38	Args: Encode,
39	R: Decode,
40{
41	let raw = client.call_api_at(CallApiAtParams {
42		at,
43		function: method,
44		arguments: args.encode(),
45		overlayed_changes: &Default::default(),
46		call_context: CallContext::Offchain,
47		recorder: &None,
48		extensions: &Default::default(),
49	})?;
50	R::decode(&mut &*raw).map_err(|error| ApiError::FailedToDecodeReturnValue {
51		function: method,
52		error,
53		raw,
54	})
55}
56
57/// `HopRuntimeApi::max_promotion_size`.
58pub fn max_promotion_size<Block, C>(client: &C, at: Block::Hash) -> Result<u32, ApiError>
59where
60	Block: BlockT,
61	C: CallApiAt<Block>,
62{
63	call::<Block, _, _, _>(client, at, "HopRuntimeApi_max_promotion_size", ())
64}
65
66/// `HopRuntimeApi::can_account_promote`.
67pub fn can_account_promote<Block, C>(
68	client: &C,
69	at: Block::Hash,
70	who: AccountId32,
71	data_len: u32,
72) -> Result<bool, ApiError>
73where
74	Block: BlockT,
75	C: CallApiAt<Block>,
76{
77	call::<Block, _, _, _>(client, at, "HopRuntimeApi_can_account_promote", (who, data_len))
78}
79
80/// `HopRuntimeApi::create_promotion_extrinsic`.
81pub fn create_promotion_extrinsic<Block, C>(
82	client: &C,
83	at: Block::Hash,
84	data: Vec<u8>,
85	signer: MultiSigner,
86	signature: MultiSignature,
87	submit_timestamp: u64,
88) -> Result<<Block as BlockT>::Extrinsic, ApiError>
89where
90	Block: BlockT,
91	C: CallApiAt<Block>,
92{
93	call::<Block, _, _, _>(
94		client,
95		at,
96		"HopRuntimeApi_create_promotion_extrinsic",
97		(data, signer, signature, submit_timestamp),
98	)
99}
100
101/// `HopRuntimeApi::is_promoted_on_chain`.
102pub fn is_promoted_on_chain<Block, C>(
103	client: &C,
104	at: Block::Hash,
105	hash: [u8; 32],
106) -> Result<bool, ApiError>
107where
108	Block: BlockT,
109	C: CallApiAt<Block>,
110{
111	call::<Block, _, _, _>(client, at, "HopRuntimeApi_is_promoted_on_chain", hash)
112}