mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 20:31:13 +00:00
jsonrpsee integration (#8783)
* Add tokio * No need to map CallError to CallError * jsonrpsee proc macros (#9673) * port error types to `JsonRpseeError` * migrate chain module to proc macro api * make it compile with proc macros * update branch * update branch * update to jsonrpsee master * port system rpc * port state rpc * port childstate & offchain * frame system rpc * frame transaction payment * bring back CORS hack to work with polkadot UI * port babe rpc * port manual seal rpc * port frame mmr rpc * port frame contracts rpc * port finality grandpa rpc * port sync state rpc * resolve a few TODO + no jsonrpc deps * Update bin/node/rpc-client/src/main.rs * Update bin/node/rpc-client/src/main.rs * Update bin/node/rpc-client/src/main.rs * Update bin/node/rpc-client/src/main.rs * Port over system_ rpc tests * Make it compile * Use prost 0.8 * Use prost 0.8 * Make it compile * Ignore more failing tests * Comment out WIP tests * fix nit in frame system api * Update lockfile * No more juggling tokio versions * No more wait_for_stop ? * Remove browser-testing * Arguments must be arrays * Use same argument names * Resolve todo: no wait_for_stop for WS server Add todo: is parse_rpc_result used? Cleanup imports * fmt * log * One test passes * update jsonrpsee * update jsonrpsee * cleanup rpc-servers crate * jsonrpsee: add host and origin filtering (#9787) * add access control in the jsonrpsee servers * use master * fix nits * rpc runtime_version safe * fix nits * fix grumbles * remove unused files * resolve some todos * jsonrpsee more cleanup (#9803) * more cleanup * resolve TODOs * fix some unwraps * remove type hints * update jsonrpsee * downgrade zeroize * pin jsonrpsee rev * remove unwrap nit * Comment out more tests that aren't ported * Comment out more tests * Fix tests after merge * Subscription test * Invalid nonce test * Pending exts * WIP removeExtrinsic test * Test remove_extrinsic * Make state test: should_return_storage work * Uncomment/fix the other non-subscription related state tests * test: author_insertKey * test: author_rotateKeys * Get rest of state tests passing * asyncify a little more * Add todo to note #msg change * Crashing test for has_session_keys * Fix error conversion to avoid stack overflows Port author_hasSessionKeys test fmt * test author_hasKey * Add two missing tests Add a check on the return type Add todos for James's concerns * RPC tests for state, author and system (#9859) * Fix test runner * Impl Default for SubscriptionTaskExecutor * Keep the minimul amount of code needed to compile tests * Re-instate `RpcSession` (for now) * cleanup * Port over RPC tests * Add tokio * No need to map CallError to CallError * Port over system_ rpc tests * Make it compile * Use prost 0.8 * Use prost 0.8 * Make it compile * Ignore more failing tests * Comment out WIP tests * Update lockfile * No more juggling tokio versions * No more wait_for_stop ? * Remove browser-testing * Arguments must be arrays * Use same argument names * Resolve todo: no wait_for_stop for WS server Add todo: is parse_rpc_result used? Cleanup imports * fmt * log * One test passes * Comment out more tests that aren't ported * Comment out more tests * Fix tests after merge * Subscription test * Invalid nonce test * Pending exts * WIP removeExtrinsic test * Test remove_extrinsic * Make state test: should_return_storage work * Uncomment/fix the other non-subscription related state tests * test: author_insertKey * test: author_rotateKeys * Get rest of state tests passing * asyncify a little more * Add todo to note #msg change * Crashing test for has_session_keys * Fix error conversion to avoid stack overflows Port author_hasSessionKeys test fmt * test author_hasKey * Add two missing tests Add a check on the return type Add todos for James's concerns * offchain rpc tests * Address todos * fmt Co-authored-by: James Wilson <james@jsdw.me> * fix drop in state test * update jsonrpsee * fix ignored system test * fix chain tests * remove some boiler plate * Port BEEFY RPC (#9883) * Merge master * Port beefy RPC (ty @niklas!) * trivial changes left over from merge * Remove unused code * Update jsonrpsee * fix build * make tests compile again * beefy update jsonrpsee * fix: respect rpc methods policy * update cargo.lock * update jsonrpsee * update jsonrpsee * downgrade error logs * update jsonrpsee * Fix typo * remove unused file * Better name * Port Babe RPC tests * Put docs back * Resolve todo * Port tests for System RPCs * Resolve todo * fix build * Updated jsonrpsee to current master * fix: port finality grandpa rpc tests * Move .into() outside of the match * more review grumbles * jsonrpsee: add `rpc handlers` back (#10245) * add back RpcHandlers * cargo fmt * fix docs * fix grumble: remove needless alloc * resolve TODO * fmt * Fix typo * grumble: Use constants based on BASE_ERROR * grumble: DRY whitelisted listening addresses grumble: s/JSONRPC/JSON-RPC/ * cleanup * grumbles: Making readers aware of the possibility of gaps * review grumbles * grumbles * remove notes from niklasad1 * Update `jsonrpsee` * fix: jsonrpsee features * jsonrpsee: fallback to random port in case the specified port failed (#10304) * jsonrpsee: fallback to random port * better comment * Update client/rpc-servers/src/lib.rs Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com> * Update client/rpc-servers/src/lib.rs Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com> * address grumbles * cargo fmt * addrs already slice Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com> * Update jsonrpsee to 092081a0a2b8904c6ebd2cd99e16c7bc13ffc3ae * lockfile * update jsonrpsee * fix warning * Don't fetch jsonrpsee from crates * make tests compile again * fix rpc tests * remove unused deps * update tokio * fix rpc tests again * fix: test runner `HttpServerBuilder::builder` fails unless it's called within tokio runtime * cargo fmt * grumbles: fix subscription aliases * make clippy happy * update remaining subscriptions alias * cleanup * cleanup * fix chain subscription: less boiler plate (#10285) * fix chain subscription: less boiler plate * fix bad merge * cargo fmt * Switch to jsonrpsee 0.5 * fix build * add missing features * fix nit: remove needless Box::pin * Integrate jsonrpsee metrics (#10395) * draft metrics impl * Use latest api * Add missing file * Http server metrics * cleanup * bump jsonrpsee * Remove `ServerMetrics` and use a single middleware for both connection counting (aka sessions) and call metrics. * fix build * remove needless Arc::clone * Update to jsonrpsee 0.6 * lolz * fix metrics * Revert "lolz" This reverts commit eed6c6a56e78d8e307b4950f4c52a1c3a2322ba1. * fix: in-memory rpc support subscriptions * commit Cargo.lock * Update tests to 0.7 * fix TODOs * ws server: generate subscriptionIDs as Strings Some libraries seems to expect the subscription IDs to be Strings, let's not break this in this PR. * Increase timeout * Port over tests * cleanup * Using error codes from the spec * fix clippy * cargo fmt * update jsonrpsee * fix nits * fix: rpc_query * enable custom subid gen through spawn_tasks * remove unsed deps * unify tokio deps * Revert "enable custom subid gen through spawn_tasks" This reverts commit 5c5eb70328fe39d154fdb55c56e637b4548cf470. * fix bad merge of `test-utils` * fix more nits * downgrade wasm-instrument to 0.1.0 * [jsonrpsee]: enable custom RPC subscription ID generatation (#10731) * enable custom subid gen through spawn_tasks * fix nits * Update client/service/src/builder.rs Co-authored-by: David <dvdplm@gmail.com> * add Poc; needs jsonrpsee pr * update jsonrpsee * add re-exports * add docs Co-authored-by: David <dvdplm@gmail.com> * cargo fmt * fmt * port RPC-API dev * Remove unused file * fix nit: remove async trait * fix doc links * fix merge nit: remove jsonrpc deps * kill namespace on rpc apis * companion for jsonrpsee v0.10 (#11158) * companion for jsonrpsee v0.10 * update versions v0.10.0 * add some fixes * spelling * fix spaces Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com> * send error before subs are closed * fix unsubscribe method names: chain * fix tests * jsonrpc server: print binded local address * grumbles: kill SubscriptionTaskExecutor * Update client/sync-state-rpc/src/lib.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * Update client/rpc/src/chain/chain_full.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * Update client/rpc/src/chain/chain_full.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * sync-state-rpc: kill anyhow * no more anyhow * remove todo * jsonrpsee: fix bad params in subscriptions. (#11251) * update jsonrpsee * fix error responses * revert error codes * dont do weird stuff in drop impl * rpc servers: remove needless clone * Remove silly constants * chore: update jsonrpsee v0.12 * commit Cargo.lock * deps: downgrade git2 * feat: CLI flag max subscriptions per connection * metrics: use old logging format * fix: read WS address from substrate output (#11379) Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com> Co-authored-by: James Wilson <james@jsdw.me> Co-authored-by: Maciej Hirsz <hello@maciej.codes> Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -18,9 +18,13 @@
|
||||
|
||||
//! RPC api for babe.
|
||||
|
||||
use futures::{FutureExt, TryFutureExt};
|
||||
use jsonrpc_core::Error as RpcError;
|
||||
use jsonrpc_derive::rpc;
|
||||
use futures::TryFutureExt;
|
||||
use jsonrpsee::{
|
||||
core::{async_trait, Error as JsonRpseeError, RpcResult},
|
||||
proc_macros::rpc,
|
||||
types::{error::CallError, ErrorObject},
|
||||
};
|
||||
|
||||
use sc_consensus_babe::{authorship, Config, Epoch};
|
||||
use sc_consensus_epochs::{descendent_query, Epoch as EpochT, SharedEpochChanges};
|
||||
use sc_rpc_api::DenyUnsafe;
|
||||
@@ -35,19 +39,17 @@ use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as _};
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
type FutureResult<T> = jsonrpc_core::BoxFuture<Result<T, RpcError>>;
|
||||
|
||||
/// Provides rpc methods for interacting with Babe.
|
||||
#[rpc]
|
||||
#[rpc(client, server)]
|
||||
pub trait BabeApi {
|
||||
/// Returns data about which slots (primary or secondary) can be claimed in the current epoch
|
||||
/// with the keys in the keystore.
|
||||
#[rpc(name = "babe_epochAuthorship")]
|
||||
fn epoch_authorship(&self) -> FutureResult<HashMap<AuthorityId, EpochAuthorship>>;
|
||||
#[method(name = "babe_epochAuthorship")]
|
||||
async fn epoch_authorship(&self) -> RpcResult<HashMap<AuthorityId, EpochAuthorship>>;
|
||||
}
|
||||
|
||||
/// Implements the BabeRpc trait for interacting with Babe.
|
||||
pub struct BabeRpcHandler<B: BlockT, C, SC> {
|
||||
/// Provides RPC methods for interacting with Babe.
|
||||
pub struct BabeRpc<B: BlockT, C, SC> {
|
||||
/// shared reference to the client.
|
||||
client: Arc<C>,
|
||||
/// shared reference to EpochChanges
|
||||
@@ -62,7 +64,7 @@ pub struct BabeRpcHandler<B: BlockT, C, SC> {
|
||||
deny_unsafe: DenyUnsafe,
|
||||
}
|
||||
|
||||
impl<B: BlockT, C, SC> BabeRpcHandler<B, C, SC> {
|
||||
impl<B: BlockT, C, SC> BabeRpc<B, C, SC> {
|
||||
/// Creates a new instance of the BabeRpc handler.
|
||||
pub fn new(
|
||||
client: Arc<C>,
|
||||
@@ -76,7 +78,8 @@ impl<B: BlockT, C, SC> BabeRpcHandler<B, C, SC> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, C, SC> BabeApi for BabeRpcHandler<B, C, SC>
|
||||
#[async_trait]
|
||||
impl<B: BlockT, C, SC> BabeApiServer for BabeRpc<B, C, SC>
|
||||
where
|
||||
B: BlockT,
|
||||
C: ProvideRuntimeApi<B>
|
||||
@@ -86,71 +89,63 @@ where
|
||||
C::Api: BabeRuntimeApi<B>,
|
||||
SC: SelectChain<B> + Clone + 'static,
|
||||
{
|
||||
fn epoch_authorship(&self) -> FutureResult<HashMap<AuthorityId, EpochAuthorship>> {
|
||||
if let Err(err) = self.deny_unsafe.check_if_safe() {
|
||||
return async move { Err(err.into()) }.boxed()
|
||||
}
|
||||
async fn epoch_authorship(&self) -> RpcResult<HashMap<AuthorityId, EpochAuthorship>> {
|
||||
self.deny_unsafe.check_if_safe()?;
|
||||
let header = self.select_chain.best_chain().map_err(Error::Consensus).await?;
|
||||
let epoch_start = self
|
||||
.client
|
||||
.runtime_api()
|
||||
.current_epoch_start(&BlockId::Hash(header.hash()))
|
||||
.map_err(|err| Error::StringError(format!("{:?}", err)))?;
|
||||
|
||||
let (babe_config, keystore, shared_epoch, client, select_chain) = (
|
||||
self.babe_config.clone(),
|
||||
self.keystore.clone(),
|
||||
self.shared_epoch_changes.clone(),
|
||||
self.client.clone(),
|
||||
self.select_chain.clone(),
|
||||
);
|
||||
let epoch = epoch_data(
|
||||
&self.shared_epoch_changes,
|
||||
&self.client,
|
||||
&self.babe_config,
|
||||
*epoch_start,
|
||||
&self.select_chain,
|
||||
)
|
||||
.await?;
|
||||
let (epoch_start, epoch_end) = (epoch.start_slot(), epoch.end_slot());
|
||||
let mut claims: HashMap<AuthorityId, EpochAuthorship> = HashMap::new();
|
||||
|
||||
async move {
|
||||
let header = select_chain.best_chain().map_err(Error::Consensus).await?;
|
||||
let epoch_start = client
|
||||
.runtime_api()
|
||||
.current_epoch_start(&BlockId::Hash(header.hash()))
|
||||
.map_err(|err| Error::StringError(err.to_string()))?;
|
||||
let epoch =
|
||||
epoch_data(&shared_epoch, &client, &babe_config, *epoch_start, &select_chain)
|
||||
.await?;
|
||||
let (epoch_start, epoch_end) = (epoch.start_slot(), epoch.end_slot());
|
||||
let keys = {
|
||||
epoch
|
||||
.authorities
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, a)| {
|
||||
if SyncCryptoStore::has_keys(
|
||||
&*self.keystore,
|
||||
&[(a.0.to_raw_vec(), AuthorityId::ID)],
|
||||
) {
|
||||
Some((a.0.clone(), i))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
|
||||
let mut claims: HashMap<AuthorityId, EpochAuthorship> = HashMap::new();
|
||||
|
||||
let keys = {
|
||||
epoch
|
||||
.authorities
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, a)| {
|
||||
if SyncCryptoStore::has_keys(
|
||||
&*keystore,
|
||||
&[(a.0.to_raw_vec(), AuthorityId::ID)],
|
||||
) {
|
||||
Some((a.0.clone(), i))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
|
||||
for slot in *epoch_start..*epoch_end {
|
||||
if let Some((claim, key)) =
|
||||
authorship::claim_slot_using_keys(slot.into(), &epoch, &keystore, &keys)
|
||||
{
|
||||
match claim {
|
||||
PreDigest::Primary { .. } => {
|
||||
claims.entry(key).or_default().primary.push(slot);
|
||||
},
|
||||
PreDigest::SecondaryPlain { .. } => {
|
||||
claims.entry(key).or_default().secondary.push(slot);
|
||||
},
|
||||
PreDigest::SecondaryVRF { .. } => {
|
||||
claims.entry(key).or_default().secondary_vrf.push(slot);
|
||||
},
|
||||
};
|
||||
}
|
||||
for slot in *epoch_start..*epoch_end {
|
||||
if let Some((claim, key)) =
|
||||
authorship::claim_slot_using_keys(slot.into(), &epoch, &self.keystore, &keys)
|
||||
{
|
||||
match claim {
|
||||
PreDigest::Primary { .. } => {
|
||||
claims.entry(key).or_default().primary.push(slot);
|
||||
},
|
||||
PreDigest::SecondaryPlain { .. } => {
|
||||
claims.entry(key).or_default().secondary.push(slot);
|
||||
},
|
||||
PreDigest::SecondaryVRF { .. } => {
|
||||
claims.entry(key).or_default().secondary_vrf.push(slot.into());
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Ok(claims)
|
||||
}
|
||||
.boxed()
|
||||
|
||||
Ok(claims)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,13 +171,13 @@ pub enum Error {
|
||||
StringError(String),
|
||||
}
|
||||
|
||||
impl From<Error> for jsonrpc_core::Error {
|
||||
impl From<Error> for JsonRpseeError {
|
||||
fn from(error: Error) -> Self {
|
||||
jsonrpc_core::Error {
|
||||
message: format!("{}", error),
|
||||
code: jsonrpc_core::ErrorCode::ServerError(1234),
|
||||
data: None,
|
||||
}
|
||||
JsonRpseeError::Call(CallError::Custom(ErrorObject::owned(
|
||||
1234,
|
||||
error.to_string(),
|
||||
None::<()>,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,7 +221,6 @@ mod tests {
|
||||
TestClientBuilderExt,
|
||||
};
|
||||
|
||||
use jsonrpc_core::IoHandler;
|
||||
use sc_consensus_babe::{block_import, AuthorityPair, Config};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -243,9 +237,9 @@ mod tests {
|
||||
(keystore, keystore_path)
|
||||
}
|
||||
|
||||
fn test_babe_rpc_handler(
|
||||
fn test_babe_rpc_module(
|
||||
deny_unsafe: DenyUnsafe,
|
||||
) -> BabeRpcHandler<Block, TestClient, sc_consensus::LongestChain<Backend, Block>> {
|
||||
) -> BabeRpc<Block, TestClient, sc_consensus::LongestChain<Backend, Block>> {
|
||||
let builder = TestClientBuilder::new();
|
||||
let (client, longest_chain) = builder.build_with_longest_chain();
|
||||
let client = Arc::new(client);
|
||||
@@ -256,40 +250,30 @@ mod tests {
|
||||
let epoch_changes = link.epoch_changes().clone();
|
||||
let keystore = create_temp_keystore::<AuthorityPair>(Sr25519Keyring::Alice).0;
|
||||
|
||||
BabeRpcHandler::new(
|
||||
client.clone(),
|
||||
epoch_changes,
|
||||
keystore,
|
||||
config,
|
||||
longest_chain,
|
||||
deny_unsafe,
|
||||
)
|
||||
BabeRpc::new(client.clone(), epoch_changes, keystore, config, longest_chain, deny_unsafe)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn epoch_authorship_works() {
|
||||
let handler = test_babe_rpc_handler(DenyUnsafe::No);
|
||||
let mut io = IoHandler::new();
|
||||
#[tokio::test]
|
||||
async fn epoch_authorship_works() {
|
||||
let babe_rpc = test_babe_rpc_module(DenyUnsafe::No);
|
||||
let api = babe_rpc.into_rpc();
|
||||
|
||||
io.extend_with(BabeApi::to_delegate(handler));
|
||||
let request = r#"{"jsonrpc":"2.0","method":"babe_epochAuthorship","params": [],"id":1}"#;
|
||||
let response = r#"{"jsonrpc":"2.0","result":{"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY":{"primary":[0],"secondary":[1,2,4],"secondary_vrf":[]}},"id":1}"#;
|
||||
let (response, _) = api.raw_json_request(request).await.unwrap();
|
||||
let expected = r#"{"jsonrpc":"2.0","result":{"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY":{"primary":[0],"secondary":[1,2,4],"secondary_vrf":[]}},"id":1}"#;
|
||||
|
||||
assert_eq!(Some(response.into()), io.handle_request_sync(request));
|
||||
assert_eq!(&response, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn epoch_authorship_is_unsafe() {
|
||||
let handler = test_babe_rpc_handler(DenyUnsafe::Yes);
|
||||
let mut io = IoHandler::new();
|
||||
#[tokio::test]
|
||||
async fn epoch_authorship_is_unsafe() {
|
||||
let babe_rpc = test_babe_rpc_module(DenyUnsafe::Yes);
|
||||
let api = babe_rpc.into_rpc();
|
||||
|
||||
io.extend_with(BabeApi::to_delegate(handler));
|
||||
let request = r#"{"jsonrpc":"2.0","method":"babe_epochAuthorship","params": [],"id":1}"#;
|
||||
let request = r#"{"jsonrpc":"2.0","method":"babe_epochAuthorship","params":[],"id":1}"#;
|
||||
let (response, _) = api.raw_json_request(request).await.unwrap();
|
||||
let expected = r#"{"jsonrpc":"2.0","error":{"code":-32601,"message":"RPC call is unsafe to be called externally"},"id":1}"#;
|
||||
|
||||
let response = io.handle_request_sync(request).unwrap();
|
||||
let mut response: serde_json::Value = serde_json::from_str(&response).unwrap();
|
||||
let error: RpcError = serde_json::from_value(response["error"].take()).unwrap();
|
||||
|
||||
assert_eq!(error, sc_rpc_api::UnsafeRpcError.into())
|
||||
assert_eq!(&response, expected);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user