substrate_api_client/api/
api_client.rs

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
   Copyright 2019 Supercomputing Systems AG
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
	   http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

use crate::{
	api::error::{Error, Result},
	rpc::Request,
	runtime_api::RuntimeApiClient,
	GetAccountInformation,
};
use ac_compose_macros::rpc_params;
use ac_node_api::metadata::Metadata;
use ac_primitives::{Config, ExtrinsicParams, SignExtrinsic};
#[cfg(not(feature = "sync-api"))]
use alloc::boxed::Box;
use alloc::sync::Arc;
use codec::Decode;
use core::convert::TryFrom;
use frame_metadata::RuntimeMetadataPrefixed;
use log::{debug, info};
use sp_core::Bytes;
use sp_version::RuntimeVersion;

/// Api to talk with substrate-nodes
///
/// It is generic over the `Request` trait, so you can use any rpc-backend you like.
#[derive(Clone)]
pub struct Api<T: Config, Client> {
	signer: Option<T::ExtrinsicSigner>,
	genesis_hash: T::Hash,
	metadata: Metadata,
	runtime_version: RuntimeVersion,
	client: Arc<Client>,
	runtime_api: RuntimeApiClient<T, Client>,
	additional_extrinsic_params:
		Option<<T::ExtrinsicParams as ExtrinsicParams<T::Index, T::Hash>>::AdditionalParams>,
}

impl<T: Config, Client> Api<T, Client> {
	/// Create a new api instance without any node interaction.
	pub fn new_offline(
		genesis_hash: T::Hash,
		metadata: Metadata,
		runtime_version: RuntimeVersion,
		client: Client,
	) -> Self {
		let client = Arc::new(client);
		let runtime_api = RuntimeApiClient::new(client.clone());
		Self {
			signer: None,
			genesis_hash,
			metadata,
			runtime_version,
			client,
			runtime_api,
			additional_extrinsic_params: None,
		}
	}

	/// Set the api signer account.
	pub fn set_signer(&mut self, signer: T::ExtrinsicSigner) {
		self.signer = Some(signer);
	}

	/// Get the private key pair of the api signer.
	pub fn signer(&self) -> Option<&T::ExtrinsicSigner> {
		self.signer.as_ref()
	}

	/// Get the cached genesis hash of the substrate node.
	pub fn genesis_hash(&self) -> T::Hash {
		self.genesis_hash
	}

	/// Get the cached metadata of the substrate node.
	pub fn metadata(&self) -> &Metadata {
		&self.metadata
	}

	/// Get the cached runtime version of the substrate node.
	pub fn runtime_version(&self) -> &RuntimeVersion {
		&self.runtime_version
	}

	/// Get the cached spec version of the substrate node.
	pub fn spec_version(&self) -> u32 {
		self.runtime_version.spec_version
	}

	/// Get the rpc client.
	pub fn client(&self) -> &Client {
		&self.client
	}

	/// Set the additional params.
	pub fn set_additional_params(
		&mut self,
		add_params: <T::ExtrinsicParams as ExtrinsicParams<T::Index, T::Hash>>::AdditionalParams,
	) {
		self.additional_extrinsic_params = Some(add_params);
	}

	/// Access the RuntimeApi.
	pub fn runtime_api(&self) -> &RuntimeApiClient<T, Client> {
		&self.runtime_api
	}

	/// Get the extrinsic params with the set additional params. If no additional params are set,
	/// the default is taken.
	pub fn extrinsic_params(&self, nonce: T::Index) -> T::ExtrinsicParams {
		let additional_extrinsic_params =
			self.additional_extrinsic_params.clone().unwrap_or_default();
		T::ExtrinsicParams::new(
			self.runtime_version.spec_version,
			self.runtime_version.transaction_version,
			nonce,
			self.genesis_hash,
			additional_extrinsic_params,
		)
	}
}

impl<T, Client> Api<T, Client>
where
	T: Config,
	Client: Request,
{
	/// Create a new Api client with call to the node to retrieve metadata.
	#[maybe_async::async_impl]
	pub async fn new(client: Client) -> Result<Self> {
		let genesis_hash_future = Self::get_genesis_hash(&client);
		let metadata_future = Self::get_metadata(&client);
		let runtime_version_future = Self::get_runtime_version(&client);

		let (genesis_hash, metadata, runtime_version) = futures_util::future::try_join3(
			genesis_hash_future,
			metadata_future,
			runtime_version_future,
		)
		.await?;
		info!("Got genesis hash: {:?}", genesis_hash);
		debug!("Metadata: {:?}", metadata);
		info!("Runtime Version: {:?}", runtime_version);
		Ok(Self::new_offline(genesis_hash, metadata, runtime_version, client))
	}

	/// Create a new Api client with call to the node to retrieve metadata.
	#[maybe_async::sync_impl]
	pub fn new(client: Client) -> Result<Self> {
		let genesis_hash = Self::get_genesis_hash(&client)?;
		info!("Got genesis hash: {:?}", genesis_hash);

		let metadata = Self::get_metadata(&client)?;
		debug!("Metadata: {:?}", metadata);

		let runtime_version = Self::get_runtime_version(&client)?;
		info!("Runtime Version: {:?}", runtime_version);

		Ok(Self::new_offline(genesis_hash, metadata, runtime_version, client))
	}
}

#[maybe_async::maybe_async(?Send)]
pub trait UpdateRuntime {
	/// Updates the runtime and metadata of the api via node query.
	/// Ideally, this function is called if a substrate update runtime event is encountered.
	async fn update_runtime(&mut self) -> Result<()>;
}

#[maybe_async::maybe_async(?Send)]
impl<T, Client> UpdateRuntime for Api<T, Client>
where
	T: Config,
	Client: Request,
{
	#[maybe_async::sync_impl]
	fn update_runtime(&mut self) -> Result<()> {
		let metadata = Self::get_metadata(&self.client)?;
		let runtime_version = Self::get_runtime_version(&self.client)?;

		debug!("Metadata: {:?}", metadata);
		info!("Runtime Version: {:?}", runtime_version);

		self.metadata = metadata;
		self.runtime_version = runtime_version;
		Ok(())
	}

	#[maybe_async::async_impl(?Send)]
	async fn update_runtime(&mut self) -> Result<()> {
		let metadata_future = Self::get_metadata(&self.client);
		let runtime_version_future = Self::get_runtime_version(&self.client);

		let (metadata, runtime_version) =
			futures_util::future::try_join(metadata_future, runtime_version_future).await?;

		debug!("Metadata: {:?}", metadata);
		info!("Runtime Version: {:?}", runtime_version);

		self.metadata = metadata;
		self.runtime_version = runtime_version;
		Ok(())
	}
}

impl<T, Client> Api<T, Client>
where
	T: Config,
	Client: Request,
{
	/// Get the public part of the api signer account.
	pub fn signer_account(&self) -> Option<&T::AccountId> {
		let pair = self.signer.as_ref()?;
		Some(pair.public_account_id())
	}

	/// Get nonce of self signer account.
	#[maybe_async::maybe_async(?Send)]
	pub async fn get_nonce(&self) -> Result<T::Index> {
		let account = self.signer_account().ok_or(Error::NoSigner)?;
		self.get_account_nonce(account).await
	}
}

/// Private node query methods. They should be used internally only, because the user should retrieve the data from the struct cache.
/// If an up-to-date query is necessary, cache should be updated beforehand.
impl<T, Client> Api<T, Client>
where
	T: Config,
	Client: Request,
{
	/// Get the genesis hash from node via websocket query.
	#[maybe_async::maybe_async(?Send)]
	async fn get_genesis_hash(client: &Client) -> Result<T::Hash> {
		let genesis: Option<T::Hash> =
			client.request("chain_getBlockHash", rpc_params![Some(0)]).await?;
		genesis.ok_or(Error::FetchGenesisHash)
	}

	/// Get runtime version from node via websocket query.
	#[maybe_async::maybe_async(?Send)]
	async fn get_runtime_version(client: &Client) -> Result<RuntimeVersion> {
		let version: RuntimeVersion =
			client.request("state_getRuntimeVersion", rpc_params![]).await?;
		Ok(version)
	}

	/// Get metadata from node via websocket query.
	#[maybe_async::maybe_async(?Send)]
	async fn get_metadata(client: &Client) -> Result<Metadata> {
		let metadata_bytes: Bytes = client.request("state_getMetadata", rpc_params![]).await?;

		let metadata = RuntimeMetadataPrefixed::decode(&mut metadata_bytes.0.as_slice())?;
		Metadata::try_from(metadata).map_err(|e| e.into())
	}
}
#[cfg(test)]
mod tests {
	use super::*;
	use crate::rpc::mocks::RpcClientMock;
	use ac_primitives::{
		DefaultRuntimeConfig, GenericAdditionalParams, GenericExtrinsicParams, PlainTip,
	};
	use frame_metadata::{v14::ExtrinsicMetadata, RuntimeMetadata};
	use scale_info::form::PortableForm;
	use sp_core::H256;
	use std::{collections::HashMap, fs};

	fn create_mock_api(
		genesis_hash: H256,
		runtime_version: RuntimeVersion,
		metadata: Metadata,
		data: HashMap<String, String>,
	) -> Api<DefaultRuntimeConfig, RpcClientMock> {
		let client = RpcClientMock::new(data);
		Api::new_offline(genesis_hash, metadata, runtime_version, client)
	}

	#[test]
	fn api_extrinsic_params_works() {
		// Create new api.
		let genesis_hash = H256::random();
		let runtime_version = RuntimeVersion::default();
		let encoded_metadata = fs::read("./ksm_metadata_v14.bin").unwrap();
		let metadata: RuntimeMetadataPrefixed =
			Decode::decode(&mut encoded_metadata.as_slice()).unwrap();
		let metadata = Metadata::try_from(metadata).unwrap();

		let mut api =
			create_mock_api(genesis_hash, runtime_version.clone(), metadata, Default::default());

		// Information for Era for mortal transactions.
		let additional_params = GenericAdditionalParams::new();
		api.set_additional_params(additional_params);

		let nonce = 6;
		let retrieved_params = api.extrinsic_params(nonce);

		let expected_params = GenericExtrinsicParams::<DefaultRuntimeConfig, PlainTip<u128>>::new(
			runtime_version.spec_version,
			runtime_version.transaction_version,
			nonce,
			genesis_hash,
			Default::default(),
		);

		assert_eq!(expected_params, retrieved_params)
	}

	#[test]
	fn api_runtime_update_works() {
		let runtime_version = RuntimeVersion { spec_version: 10, ..Default::default() };
		// Update metadata
		let encoded_metadata: Bytes = fs::read("./ksm_metadata_v14.bin").unwrap().into();
		let runtime_metadata_prefixed: RuntimeMetadataPrefixed =
			Decode::decode(&mut encoded_metadata.0.as_slice()).unwrap();
		let mut runtime_metadata = match runtime_metadata_prefixed.1 {
			RuntimeMetadata::V14(ref metadata) => metadata.clone(),
			_ => unimplemented!(),
		};

		let metadata = Metadata::try_from(runtime_metadata_prefixed).unwrap();

		runtime_metadata.extrinsic = ExtrinsicMetadata::<PortableForm> {
			ty: runtime_metadata.extrinsic.ty,
			version: 0,
			signed_extensions: Vec::new(),
		};
		let changed_runtime_metadata_prefixed =
			RuntimeMetadataPrefixed(1635018093, RuntimeMetadata::V14(runtime_metadata));
		let changed_metadata = Metadata::try_from(changed_runtime_metadata_prefixed).unwrap();

		let data = HashMap::<String, String>::from([
			(
				"chain_getBlockHash".to_owned(),
				serde_json::to_string(&Some(H256::from([1u8; 32]))).unwrap(),
			),
			(
				"state_getRuntimeVersion".to_owned(),
				serde_json::to_string(&runtime_version).unwrap(),
			),
			("state_getMetadata".to_owned(), serde_json::to_string(&encoded_metadata).unwrap()),
		]);
		let mut api =
			create_mock_api(Default::default(), Default::default(), changed_metadata, data);

		// Ensure current metadata and runtime version are different.
		assert_ne!(api.metadata.extrinsic(), metadata.extrinsic());
		assert_ne!(api.runtime_version, runtime_version);

		// Update runtime.
		api.update_runtime().unwrap();

		// Ensure metadata and runtime version have been updated.
		assert_eq!(api.metadata.extrinsic(), metadata.extrinsic());
		assert_eq!(api.runtime_version, runtime_version);
	}
}