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
// Strong Documentation Lints
#![deny(
	rustdoc::broken_intra_doc_links,
	rustdoc::missing_crate_level_docs,
	rustdoc::invalid_codeblock_attributes,
	missing_docs
)]

//! Custom APIs for [MSA](../pallet_msa/index.html)

use common_helpers::rpc::map_rpc_result;
use common_primitives::{
	msa::{
		DelegationResponse, DelegatorId, KeyInfoResponse, MessageSourceId, ProviderId, SchemaGrant,
	},
	node::BlockNumber,
	offchain::get_msa_account_storage_key_name,
	schema::SchemaId,
};
use jsonrpsee::{
	core::{async_trait, RpcResult},
	proc_macros::rpc,
	tracing::warn,
	types::{error::ErrorObjectOwned, ErrorObject},
};
use pallet_msa_runtime_api::MsaRuntimeApi;
use parity_scale_codec::{Codec, Decode};
use parking_lot::RwLock;
use rayon::prelude::*;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_core::Bytes;
use sp_runtime::traits::Block as BlockT;
use std::sync::Arc;

#[cfg(test)]
mod tests;

/// Frequency MSA Custom RPC API
#[rpc(client, server)]
pub trait MsaApi<BlockHash, AccountId> {
	/// Check for a list of delegations
	/// Given a single provider, test a list of potential delegators
	/// At a given block number
	#[method(name = "msa_checkDelegations")]
	fn check_delegations(
		&self,
		delegator_msa_ids: Vec<DelegatorId>,
		provider_msa_id: ProviderId,
		block_number: BlockNumber,
		schema_id: Option<SchemaId>,
	) -> RpcResult<Vec<(DelegatorId, bool)>>;

	/// Retrieve the list of currently granted schemas given a delegator and provider pair
	#[method(name = "msa_grantedSchemaIdsByMsaId")]
	fn get_granted_schemas_by_msa_id(
		&self,
		delegator_msa_id: DelegatorId,
		provider_msa_id: ProviderId,
	) -> RpcResult<Option<Vec<SchemaGrant<SchemaId, BlockNumber>>>>;

	/// Retrieve the list of all delegations for a MsaId
	#[method(name = "msa_getAllGrantedDelegationsByMsaId")]
	fn get_all_granted_delegations_by_msa_id(
		&self,
		delegator_msa_id: DelegatorId,
	) -> RpcResult<Vec<DelegationResponse<SchemaId, BlockNumber>>>;

	/// Retrieve the list of keys for msa id
	#[method(name = "msa_getKeysByMsaId")]
	fn get_keys_by_msa_id(
		&self,
		msa_id: MessageSourceId,
	) -> RpcResult<Option<KeyInfoResponse<AccountId>>>;
}

/// The client handler for the API used by Frequency Service RPC with `jsonrpsee`
pub struct MsaHandler<C, M, OffchainDB> {
	client: Arc<C>,
	offchain: Arc<RwLock<Option<OffchainDB>>>,
	_marker: std::marker::PhantomData<M>,
}

impl<C, M, OffchainDB> MsaHandler<C, M, OffchainDB>
where
	OffchainDB: Send + Sync,
{
	/// Create new instance with the given reference to the client.
	pub fn new(client: Arc<C>, offchain: Option<OffchainDB>) -> Self {
		Self { client, offchain: Arc::new(RwLock::new(offchain)), _marker: Default::default() }
	}
}

/// Errors that occur on the client RPC
#[derive(Debug)]
pub enum MsaOffchainRpcError {
	/// Error acquiring lock
	ErrorAcquiringLock,
	/// Error decoding data
	ErrorDecodingData,
	/// Offchain indexing is not enabled
	OffchainIndexingNotEnabled,
}

impl From<MsaOffchainRpcError> for ErrorObjectOwned {
	fn from(e: MsaOffchainRpcError) -> Self {
		let msg = format!("{:?}", e);

		match e {
			MsaOffchainRpcError::ErrorAcquiringLock => ErrorObject::owned(1, msg, None::<()>),
			MsaOffchainRpcError::ErrorDecodingData => ErrorObject::owned(2, msg, None::<()>),
			MsaOffchainRpcError::OffchainIndexingNotEnabled =>
				ErrorObject::owned(3, msg, None::<()>),
		}
	}
}

#[async_trait]
impl<C, Block, OffchainDB, AccountId> MsaApiServer<<Block as BlockT>::Hash, AccountId>
	for MsaHandler<C, Block, OffchainDB>
where
	Block: BlockT,
	C: Send + Sync + 'static,
	C: ProvideRuntimeApi<Block>,
	C: HeaderBackend<Block>,
	C::Api: MsaRuntimeApi<Block, AccountId>,
	AccountId: Codec,
	OffchainDB: sp_core::offchain::OffchainStorage + 'static,
{
	fn check_delegations(
		&self,
		delegator_msa_ids: Vec<DelegatorId>,
		provider_msa_id: ProviderId,
		block_number: BlockNumber,
		schema_id: Option<SchemaId>,
	) -> RpcResult<Vec<(DelegatorId, bool)>> {
		let at = self.client.info().best_hash;
		let results = delegator_msa_ids
			.par_iter()
			.map(|delegator_msa_id| {
				let api = self.client.runtime_api();
				// api.has_delegation returns  Result<bool, ApiError>), so _or(false) should not happen,
				// but just in case, protect against panic
				let has_delegation: bool = match api.has_delegation(
					at,
					*delegator_msa_id,
					provider_msa_id,
					block_number,
					schema_id,
				) {
					Ok(result) => result,
					Err(e) => {
						warn!("ApiError from has_delegation! {:?}", e);
						false
					},
				};
				(*delegator_msa_id, has_delegation)
			})
			.collect();
		Ok(results)
	}

	fn get_granted_schemas_by_msa_id(
		&self,
		delegator_msa_id: DelegatorId,
		provider_msa_id: ProviderId,
	) -> RpcResult<Option<Vec<SchemaGrant<SchemaId, BlockNumber>>>> {
		let api = self.client.runtime_api();
		let at = self.client.info().best_hash;
		let runtime_api_result =
			api.get_granted_schemas_by_msa_id(at, delegator_msa_id, provider_msa_id);
		map_rpc_result(runtime_api_result)
	}

	fn get_all_granted_delegations_by_msa_id(
		&self,
		delegator_msa_id: DelegatorId,
	) -> RpcResult<Vec<DelegationResponse<SchemaId, BlockNumber>>> {
		let api = self.client.runtime_api();
		let at = self.client.info().best_hash;
		let runtime_api_result = api.get_all_granted_delegations_by_msa_id(at, delegator_msa_id);
		map_rpc_result(runtime_api_result)
	}

	fn get_keys_by_msa_id(
		&self,
		msa_id: MessageSourceId,
	) -> RpcResult<Option<KeyInfoResponse<AccountId>>> {
		let msa_key = get_msa_account_storage_key_name(msa_id);
		let reader = self.offchain.try_read().ok_or(MsaOffchainRpcError::ErrorAcquiringLock)?;
		let raw: Option<Bytes> = reader
			.as_ref()
			.ok_or(MsaOffchainRpcError::OffchainIndexingNotEnabled)?
			.get(sp_offchain::STORAGE_PREFIX, &msa_key)
			.map(Into::into);
		if let Some(rr) = raw {
			let inside = rr.0;
			let keys = Vec::<AccountId>::decode(&mut &inside[..])
				.map_err(|_| MsaOffchainRpcError::ErrorDecodingData)?;
			return Ok(Some(KeyInfoResponse { msa_id, msa_keys: keys }))
		}
		Ok(None)
	}
}