diff --git a/Cargo.lock b/Cargo.lock index e592dfb01f..656b05f2ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2709,6 +2709,7 @@ dependencies = [ "substrate-client-keystore", "teerex-primitives", "thiserror 1.0.40", + "tokio", "ws", ] @@ -8226,7 +8227,6 @@ dependencies = [ "sp-runtime", "sp-runtime-interface", "url 2.3.1", - "ws", ] [[package]] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 7c4065a7fd..6d8c7cf645 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -22,13 +22,14 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" sgx_crypto_helper = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git" } thiserror = "1.0" +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" } pallet-teerex = { git = "https://github.com/integritee-network/pallets.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" } +substrate-api-client = { git = "https://github.com/scs/substrate-api-client.git", branch = "polkadot-v0.9.39" } substrate-client-keystore = { git = "https://github.com/scs/substrate-api-client.git", branch = "polkadot-v0.9.39" } 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 6deb825ce2..9e3b84a4eb 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -74,7 +74,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 866f23881b..fc05c29ccc 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 fa3335e27b..d922c83514 100644 --- a/core-primitives/node-api/api-client-types/src/lib.rs +++ b/core-primitives/node-api/api-client-types/src/lib.rs @@ -66,7 +66,7 @@ mod api { pub use substrate_api_client::{ api::Error as ApiClientError, - rpc::{Error as RpcClientError, WsRpcClient}, + rpc::{Error as RpcClientError, JsonrpseeClient}, }; pub type SignedBlock = GenericSignedBlock; @@ -74,5 +74,5 @@ mod api { StaticExtrinsicSigner; 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 55ccd5dc25..b68e94ba99 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, ParentchainExtrinsicSigner, WsRpcClient}; +use itp_api_client_types::{JsonrpseeClient, ParentchainApi, ParentchainExtrinsicSigner}; use sp_core::sr25519; /// Trait to create a node API, based on a node URL and signer. @@ -63,7 +63,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/enclave-runtime/Cargo.lock b/enclave-runtime/Cargo.lock index acbfd039c6..242bdc5340 100644 --- a/enclave-runtime/Cargo.lock +++ b/enclave-runtime/Cargo.lock @@ -34,7 +34,7 @@ dependencies = [ "bitvec", "derive_more", "either", - "frame-metadata 15.1.0", + "frame-metadata 15.0.0 (git+https://github.com/paritytech/frame-metadata)", "hex", "log", "parity-scale-codec", @@ -984,8 +984,8 @@ dependencies = [ [[package]] name = "frame-metadata" -version = "15.1.0" -source = "git+https://github.com/paritytech/frame-metadata#438a5b098bb9d5b5a09bdc5b68275b2c5e63a010" +version = "15.0.0" +source = "git+https://github.com/paritytech/frame-metadata#040a53c63af277e280282028b73b992eac9c35a2" dependencies = [ "cfg-if 1.0.0", "parity-scale-codec", @@ -999,7 +999,7 @@ version = "4.0.0-dev" source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.39#1837f423b494254e1d27834b1c9da34b2c0c2375" dependencies = [ "bitflags", - "frame-metadata 15.0.0", + "frame-metadata 15.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "frame-support-procedural", "impl-trait-for-tuples", "k256", @@ -2112,7 +2112,7 @@ name = "itp-storage" version = "0.9.0" dependencies = [ "derive_more", - "frame-metadata 15.0.0", + "frame-metadata 15.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "frame-support", "hash-db", "itp-types", @@ -4366,7 +4366,7 @@ dependencies = [ "ac-node-api", "ac-primitives", "derive_more", - "frame-metadata 15.1.0", + "frame-metadata 15.0.0 (git+https://github.com/paritytech/frame-metadata)", "hex", "log", "parity-scale-codec", diff --git a/service/src/main.rs b/service/src/main.rs index 5f8adfdfec..814e7de728 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::{ rpc::HandleSubscription, GetHeader, SubmitAndWatch, SubscribeChain, SubscribeEvents, XtStatus, }; @@ -84,7 +85,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 @@ pub type EnclaveWorker = Worker>; pub type Event = substrate_api_client::EventRecord; -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(); } // ------------------------------------------------------------------------ @@ -550,7 +553,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 { @@ -703,7 +706,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"); @@ -716,7 +719,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())) }