From 46e93e2612ae8fe64757ee9cd53c3d8da013685d Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 31 Mar 2023 11:55:06 +0200 Subject: [PATCH] switch to jsonrpsee client well well taplo fmt lets try again fix clippy --- Cargo.lock | 2 +- cli/Cargo.toml | 3 +- cli/src/command_utils.rs | 4 +- cli/src/main.rs | 3 +- .../node-api/api-client-extensions/src/lib.rs | 2 +- .../node-api/api-client-types/Cargo.toml | 2 +- .../node-api/api-client-types/src/lib.rs | 4 +- core-primitives/node-api/factory/src/lib.rs | 4 +- service/src/main.rs | 43 +++++++++--------- service/src/sidechain_setup.rs | 44 +++++++++---------- 10 files changed, 57 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3175a88fc4..e192f9096e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2700,6 +2700,7 @@ dependencies = [ "substrate-api-client", "substrate-client-keystore", "teerex-primitives", + "tokio", "ws", ] @@ -8217,7 +8218,6 @@ dependencies = [ "sp-runtime-interface", "thiserror-core", "url 2.3.1", - "ws", ] [[package]] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 3aa4d5667b..cf3b3734ba 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -20,12 +20,13 @@ rayon = "1.5.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" sgx_crypto_helper = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git" } +tokio = { version = "1.6.1", features = ["full"] } ws = { version = "0.9.1", features = ["ssl"] } # scs / integritee my-node-runtime = { package = "integritee-node-runtime", git = "https://github.com/integritee-network/integritee-node.git", branch = "polkadot-v0.9.39" } pallet-evm = { optional = true, git = "https://github.com/integritee-network/frontier.git", branch = "polkadot-v0.9.39" } -substrate-api-client = { features = ["ws-client"], git = "https://github.com/scs/substrate-api-client.git", branch = "polkadot-v0.9.39-tag-v0.9.0" } +substrate-api-client = { git = "https://github.com/scs/substrate-api-client.git", branch = "polkadot-v0.9.39-tag-v0.9.0" } substrate-client-keystore = { git = "https://github.com/scs/substrate-api-client.git", branch = "polkadot-v0.9.39-tag-v0.9.0" } teerex-primitives = { git = "https://github.com/integritee-network/pallets.git", branch = "polkadot-v0.9.39" } diff --git a/cli/src/command_utils.rs b/cli/src/command_utils.rs index 6989542959..9a26721d36 100644 --- a/cli/src/command_utils.rs +++ b/cli/src/command_utils.rs @@ -18,7 +18,7 @@ use crate::Cli; use base58::FromBase58; use itc_rpc_client::direct_client::{DirectApi, DirectClient as DirectWorkerApi}; -use itp_node_api::api_client::{ParentchainApi, WsRpcClient}; +use itp_node_api::api_client::{JsonrpseeClient, ParentchainApi}; use log::*; use my_node_runtime::{AccountId, Signature}; use sgx_crypto_helper::rsa3072::Rsa3072PubKey; @@ -40,7 +40,7 @@ pub(crate) fn get_shielding_key(cli: &Cli) -> Result { pub(crate) fn get_chain_api(cli: &Cli) -> ParentchainApi { let url = format!("{}:{}", cli.node_url, cli.node_port); info!("connecting to {}", url); - ParentchainApi::new(WsRpcClient::new(&url).unwrap()).unwrap() + ParentchainApi::new(JsonrpseeClient::new(&url).unwrap()).unwrap() } pub(crate) fn get_accountid_from_str(account: &str) -> AccountId { diff --git a/cli/src/main.rs b/cli/src/main.rs index 0cb45e3e05..194a0c806a 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -72,7 +72,8 @@ pub struct Cli { command: Commands, } -fn main() { +#[tokio::main] +async fn main() { env_logger::init(); let cli = Cli::parse(); diff --git a/core-primitives/node-api/api-client-extensions/src/lib.rs b/core-primitives/node-api/api-client-extensions/src/lib.rs index 4265e33393..5728f15897 100644 --- a/core-primitives/node-api/api-client-extensions/src/lib.rs +++ b/core-primitives/node-api/api-client-extensions/src/lib.rs @@ -17,7 +17,7 @@ //! Some substrate-api-client extension traits. -pub use substrate_api_client::{api::Error as ApiClientError, rpc::WsRpcClient, Api}; +pub use substrate_api_client::{api::Error as ApiClientError, rpc::JsonrpseeClient, Api}; pub mod account; pub mod chain; diff --git a/core-primitives/node-api/api-client-types/Cargo.toml b/core-primitives/node-api/api-client-types/Cargo.toml index 205ce327c4..19edcff162 100644 --- a/core-primitives/node-api/api-client-types/Cargo.toml +++ b/core-primitives/node-api/api-client-types/Cargo.toml @@ -24,7 +24,7 @@ default = ["std"] std = [ "itp-types/std", "substrate-api-client/std", - "substrate-api-client/ws-client", + "substrate-api-client/jsonrpsee-client", "sp-core/std", "sp-runtime/std", "my-node-runtime", diff --git a/core-primitives/node-api/api-client-types/src/lib.rs b/core-primitives/node-api/api-client-types/src/lib.rs index 405dbb4f2e..95e6a01580 100644 --- a/core-primitives/node-api/api-client-types/src/lib.rs +++ b/core-primitives/node-api/api-client-types/src/lib.rs @@ -65,9 +65,9 @@ mod api { pub use substrate_api_client::{ api::Error as ApiClientError, - rpc::{Error as RpcClientError, WsRpcClient}, + rpc::{Error as RpcClientError, JsonrpseeClient}, }; pub type ParentchainApi = - Api; + Api; } diff --git a/core-primitives/node-api/factory/src/lib.rs b/core-primitives/node-api/factory/src/lib.rs index fb37dc6ec2..abf1789d90 100644 --- a/core-primitives/node-api/factory/src/lib.rs +++ b/core-primitives/node-api/factory/src/lib.rs @@ -16,7 +16,7 @@ */ -use itp_api_client_types::{ParentchainApi, WsRpcClient}; +use itp_api_client_types::{JsonrpseeClient, ParentchainApi}; use sp_core::sr25519; /// Trait to create a node API, based on a node URL and signer. @@ -51,7 +51,7 @@ impl NodeApiFactory { impl CreateNodeApi for NodeApiFactory { fn create_api(&self) -> Result { - let rpc_client = WsRpcClient::new(self.node_url.as_str()) + let rpc_client = JsonrpseeClient::new(self.node_url.as_str()) .map_err(NodeApiFactoryError::FailedToCreateRpcClient)?; let mut api = ParentchainApi::new(rpc_client).map_err(NodeApiFactoryError::FailedToCreateNodeApi)?; diff --git a/service/src/main.rs b/service/src/main.rs index 7c2a6dd4e9..a4880cc0be 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -74,6 +74,7 @@ use its_storage::{interface::FetchBlocks, BlockPruner, SidechainStorageLock}; use log::*; use my_node_runtime::{Hash, Header, RuntimeEvent}; use sgx_types::*; +use std::thread; use substrate_api_client::{ primitives::StorageChangeSet, rpc::HandleSubscription, GetHeader, SubmitAndWatch, SubscribeChain, SubscribeFrameSystem, XtStatus, @@ -85,7 +86,7 @@ use sgx_verify::extract_tcb_info_from_raw_dcap_quote; use sp_core::crypto::{AccountId32, Ss58Codec}; use sp_keyring::AccountKeyring; use sp_runtime::traits::Header as HeaderTrait; -use std::{path::PathBuf, str, sync::Arc, thread, time::Duration}; +use std::{path::PathBuf, str, sync::Arc, time::Duration}; use teerex_primitives::ShardIdentifier; mod account_funding; @@ -113,7 +114,8 @@ const VERSION: &str = env!("CARGO_PKG_VERSION"); pub type EnclaveWorker = Worker>; -fn main() { +#[tokio::main] +async fn main() { // Setup logging env_logger::init(); @@ -204,7 +206,8 @@ fn main() { node_api, tokio_handle, initialization_handler, - ); + ) + .await; } else if let Some(smatches) = matches.subcommand_matches("request-state") { println!("*** Requesting state from a registered worker \n"); let node_api = @@ -273,7 +276,7 @@ fn main() { /// FIXME: needs some discussion (restructuring?) #[allow(clippy::too_many_arguments)] -fn start_worker( +async fn start_worker( config: Config, shard: &ShardIdentifier, enclave: Arc, @@ -317,7 +320,7 @@ fn start_worker( let is_development_mode = run_config.dev; let ra_url = config.mu_ra_url(); let enclave_api_key_prov = enclave.clone(); - thread::spawn(move || { + tokio::task::spawn_blocking(move || { enclave_run_state_provisioning_server( enclave_api_key_prov.as_ref(), sgx_quote_sign_type_t::SGX_UNLINKABLE_SIGNATURE, @@ -375,7 +378,7 @@ fn start_worker( { let direct_invocation_server_addr = config.trusted_worker_url_internal(); let enclave_for_direct_invocation = enclave.clone(); - thread::spawn(move || { + tokio::task::spawn_blocking(move || { println!( "[+] Trusted RPC direct invocation server listening on {}", direct_invocation_server_addr @@ -503,22 +506,22 @@ fn start_worker( sidechain_storage, &last_synced_header, ) + .await .unwrap(); } // ------------------------------------------------------------------------ // start parentchain syncing loop (subscribe to header updates) - thread::Builder::new() - .name("parentchain_sync_loop".to_owned()) - .spawn(move || { - if let Err(e) = - subscribe_to_parentchain_new_headers(parentchain_handler, last_synced_header) - { - error!("Parentchain block syncing terminated with a failure: {:?}", e); - } - println!("[!] Parentchain block syncing has terminated"); - }) - .unwrap(); + tokio::task::spawn_blocking(move || { + if let Err(e) = + subscribe_to_parentchain_new_headers(parentchain_handler, last_synced_header) + { + error!("Parentchain block syncing terminated with a failure: {:?}", e); + } + println!("[!] Parentchain block syncing has terminated"); + }) + .await + .unwrap(); } // ------------------------------------------------------------------------ @@ -552,7 +555,7 @@ fn spawn_worker_for_shard_polling( InitializationHandler: TrackInitialization + Sync + Send + 'static, { let shard_for_initialized = *shard; - thread::spawn(move || { + tokio::task::spawn_blocking(move || { const POLL_INTERVAL_SECS: u64 = 2; loop { @@ -716,7 +719,7 @@ fn fetch_marblerun_events_every_hour( E: RemoteAttestation + Clone + Sync + Send + 'static, { let enclave = enclave.clone(); - let handle = thread::spawn(move || { + let handle = tokio::task::spawn(move || { const POLL_INTERVAL_5_MINUTES_IN_SECS: u64 = 5 * 60; loop { info!("Polling marblerun events for quotes to register"); @@ -729,7 +732,7 @@ fn fetch_marblerun_events_every_hour( marblerun_base_url.clone(), ); - thread::sleep(Duration::from_secs(POLL_INTERVAL_5_MINUTES_IN_SECS)); + tokio::time::sleep(Duration::from_secs(POLL_INTERVAL_5_MINUTES_IN_SECS)); } }); diff --git a/service/src/sidechain_setup.rs b/service/src/sidechain_setup.rs index 9827cd53a2..de2e3abed5 100644 --- a/service/src/sidechain_setup.rs +++ b/service/src/sidechain_setup.rs @@ -33,7 +33,7 @@ use its_consensus_slots::start_slot_worker; use its_primitives::types::block::SignedBlock as SignedSidechainBlock; use its_storage::{interface::FetchBlocks, start_sidechain_pruning_loop, BlockPruner}; use log::*; -use std::{sync::Arc, thread}; +use std::sync::Arc; use tokio::runtime::Handle; pub(crate) fn sidechain_start_untrusted_rpc_server( @@ -54,7 +54,7 @@ pub(crate) fn sidechain_start_untrusted_rpc_server( }); } -pub(crate) fn sidechain_init_block_production( +pub(crate) async fn sidechain_init_block_production( enclave: Arc, register_enclave_xt_header: &Header, we_are_primary_validateer: bool, @@ -89,30 +89,28 @@ where // Start interval sidechain block production (execution of trusted calls, sidechain block production). let sidechain_enclave_api = enclave; println!("[+] Spawning thread for sidechain block production"); - thread::Builder::new() - .name("interval_block_production_timer".to_owned()) - .spawn(move || { - let future = start_slot_worker( - || execute_trusted_calls(sidechain_enclave_api.as_ref()), - SLOT_DURATION, - ); - block_on(future); - println!("[!] Sidechain block production loop has terminated"); - }) - .map_err(|e| Error::Custom(Box::new(e)))?; + tokio::task::spawn_blocking(move || { + let future = start_slot_worker( + || execute_trusted_calls(sidechain_enclave_api.as_ref()), + SLOT_DURATION, + ); + block_on(future); + println!("[!] Sidechain block production loop has terminated"); + }) + .await + .map_err(|e| Error::Custom(Box::new(e)))?; // ------------------------------------------------------------------------ // start sidechain pruning loop - thread::Builder::new() - .name("sidechain_pruning_loop".to_owned()) - .spawn(move || { - start_sidechain_pruning_loop( - &sidechain_storage, - SIDECHAIN_PURGE_INTERVAL, - SIDECHAIN_PURGE_LIMIT, - ); - }) - .map_err(|e| Error::Custom(Box::new(e)))?; + tokio::task::spawn_blocking(move || { + start_sidechain_pruning_loop( + &sidechain_storage, + SIDECHAIN_PURGE_INTERVAL, + SIDECHAIN_PURGE_LIMIT, + ); + }) + .await + .map_err(|e| Error::Custom(Box::new(e)))?; Ok(updated_header.unwrap_or_else(|| last_synced_header.clone())) }