Skip to content

Commit 46e93e2

Browse files
committed
switch to jsonrpsee client
well well taplo fmt lets try again fix clippy
1 parent b28f1ae commit 46e93e2

File tree

10 files changed

+57
-54
lines changed

10 files changed

+57
-54
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2700,6 +2700,7 @@ dependencies = [
27002700
"substrate-api-client",
27012701
"substrate-client-keystore",
27022702
"teerex-primitives",
2703+
"tokio",
27032704
"ws",
27042705
]
27052706

@@ -8217,7 +8218,6 @@ dependencies = [
82178218
"sp-runtime-interface",
82188219
"thiserror-core",
82198220
"url 2.3.1",
8220-
"ws",
82218221
]
82228222

82238223
[[package]]

cli/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ rayon = "1.5.1"
2020
serde = { version = "1.0", features = ["derive"] }
2121
serde_json = "1.0"
2222
sgx_crypto_helper = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git" }
23+
tokio = { version = "1.6.1", features = ["full"] }
2324
ws = { version = "0.9.1", features = ["ssl"] }
2425

2526
# scs / integritee
2627
my-node-runtime = { package = "integritee-node-runtime", git = "https://github.com/integritee-network/integritee-node.git", branch = "polkadot-v0.9.39" }
2728
pallet-evm = { optional = true, git = "https://github.com/integritee-network/frontier.git", branch = "polkadot-v0.9.39" }
28-
substrate-api-client = { features = ["ws-client"], git = "https://github.com/scs/substrate-api-client.git", branch = "polkadot-v0.9.39-tag-v0.9.0" }
29+
substrate-api-client = { git = "https://github.com/scs/substrate-api-client.git", branch = "polkadot-v0.9.39-tag-v0.9.0" }
2930
substrate-client-keystore = { git = "https://github.com/scs/substrate-api-client.git", branch = "polkadot-v0.9.39-tag-v0.9.0" }
3031
teerex-primitives = { git = "https://github.com/integritee-network/pallets.git", branch = "polkadot-v0.9.39" }
3132

cli/src/command_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use crate::Cli;
1919
use base58::FromBase58;
2020
use itc_rpc_client::direct_client::{DirectApi, DirectClient as DirectWorkerApi};
21-
use itp_node_api::api_client::{ParentchainApi, WsRpcClient};
21+
use itp_node_api::api_client::{JsonrpseeClient, ParentchainApi};
2222
use log::*;
2323
use my_node_runtime::{AccountId, Signature};
2424
use sgx_crypto_helper::rsa3072::Rsa3072PubKey;
@@ -40,7 +40,7 @@ pub(crate) fn get_shielding_key(cli: &Cli) -> Result<Rsa3072PubKey, String> {
4040
pub(crate) fn get_chain_api(cli: &Cli) -> ParentchainApi {
4141
let url = format!("{}:{}", cli.node_url, cli.node_port);
4242
info!("connecting to {}", url);
43-
ParentchainApi::new(WsRpcClient::new(&url).unwrap()).unwrap()
43+
ParentchainApi::new(JsonrpseeClient::new(&url).unwrap()).unwrap()
4444
}
4545

4646
pub(crate) fn get_accountid_from_str(account: &str) -> AccountId {

cli/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ pub struct Cli {
7272
command: Commands,
7373
}
7474

75-
fn main() {
75+
#[tokio::main]
76+
async fn main() {
7677
env_logger::init();
7778

7879
let cli = Cli::parse();

core-primitives/node-api/api-client-extensions/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
//! Some substrate-api-client extension traits.
1919
20-
pub use substrate_api_client::{api::Error as ApiClientError, rpc::WsRpcClient, Api};
20+
pub use substrate_api_client::{api::Error as ApiClientError, rpc::JsonrpseeClient, Api};
2121

2222
pub mod account;
2323
pub mod chain;

core-primitives/node-api/api-client-types/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ default = ["std"]
2424
std = [
2525
"itp-types/std",
2626
"substrate-api-client/std",
27-
"substrate-api-client/ws-client",
27+
"substrate-api-client/jsonrpsee-client",
2828
"sp-core/std",
2929
"sp-runtime/std",
3030
"my-node-runtime",

core-primitives/node-api/api-client-types/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ mod api {
6565

6666
pub use substrate_api_client::{
6767
api::Error as ApiClientError,
68-
rpc::{Error as RpcClientError, WsRpcClient},
68+
rpc::{Error as RpcClientError, JsonrpseeClient},
6969
};
7070

7171
pub type ParentchainApi =
72-
Api<sp_core::sr25519::Pair, WsRpcClient, ParentchainExtrinsicParams, Runtime>;
72+
Api<sp_core::sr25519::Pair, JsonrpseeClient, ParentchainExtrinsicParams, Runtime>;
7373
}

core-primitives/node-api/factory/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
*/
1818

19-
use itp_api_client_types::{ParentchainApi, WsRpcClient};
19+
use itp_api_client_types::{JsonrpseeClient, ParentchainApi};
2020
use sp_core::sr25519;
2121

2222
/// Trait to create a node API, based on a node URL and signer.
@@ -51,7 +51,7 @@ impl NodeApiFactory {
5151

5252
impl CreateNodeApi for NodeApiFactory {
5353
fn create_api(&self) -> Result<ParentchainApi> {
54-
let rpc_client = WsRpcClient::new(self.node_url.as_str())
54+
let rpc_client = JsonrpseeClient::new(self.node_url.as_str())
5555
.map_err(NodeApiFactoryError::FailedToCreateRpcClient)?;
5656
let mut api =
5757
ParentchainApi::new(rpc_client).map_err(NodeApiFactoryError::FailedToCreateNodeApi)?;

service/src/main.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ use its_storage::{interface::FetchBlocks, BlockPruner, SidechainStorageLock};
7474
use log::*;
7575
use my_node_runtime::{Hash, Header, RuntimeEvent};
7676
use sgx_types::*;
77+
use std::thread;
7778
use substrate_api_client::{
7879
primitives::StorageChangeSet, rpc::HandleSubscription, GetHeader, SubmitAndWatch,
7980
SubscribeChain, SubscribeFrameSystem, XtStatus,
@@ -85,7 +86,7 @@ use sgx_verify::extract_tcb_info_from_raw_dcap_quote;
8586
use sp_core::crypto::{AccountId32, Ss58Codec};
8687
use sp_keyring::AccountKeyring;
8788
use sp_runtime::traits::Header as HeaderTrait;
88-
use std::{path::PathBuf, str, sync::Arc, thread, time::Duration};
89+
use std::{path::PathBuf, str, sync::Arc, time::Duration};
8990
use teerex_primitives::ShardIdentifier;
9091

9192
mod account_funding;
@@ -113,7 +114,8 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
113114
pub type EnclaveWorker =
114115
Worker<Config, NodeApiFactory, Enclave, InitializationHandler<WorkerModeProvider>>;
115116

116-
fn main() {
117+
#[tokio::main]
118+
async fn main() {
117119
// Setup logging
118120
env_logger::init();
119121

@@ -204,7 +206,8 @@ fn main() {
204206
node_api,
205207
tokio_handle,
206208
initialization_handler,
207-
);
209+
)
210+
.await;
208211
} else if let Some(smatches) = matches.subcommand_matches("request-state") {
209212
println!("*** Requesting state from a registered worker \n");
210213
let node_api =
@@ -273,7 +276,7 @@ fn main() {
273276

274277
/// FIXME: needs some discussion (restructuring?)
275278
#[allow(clippy::too_many_arguments)]
276-
fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
279+
async fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
277280
config: Config,
278281
shard: &ShardIdentifier,
279282
enclave: Arc<E>,
@@ -317,7 +320,7 @@ fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
317320
let is_development_mode = run_config.dev;
318321
let ra_url = config.mu_ra_url();
319322
let enclave_api_key_prov = enclave.clone();
320-
thread::spawn(move || {
323+
tokio::task::spawn_blocking(move || {
321324
enclave_run_state_provisioning_server(
322325
enclave_api_key_prov.as_ref(),
323326
sgx_quote_sign_type_t::SGX_UNLINKABLE_SIGNATURE,
@@ -375,7 +378,7 @@ fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
375378
{
376379
let direct_invocation_server_addr = config.trusted_worker_url_internal();
377380
let enclave_for_direct_invocation = enclave.clone();
378-
thread::spawn(move || {
381+
tokio::task::spawn_blocking(move || {
379382
println!(
380383
"[+] Trusted RPC direct invocation server listening on {}",
381384
direct_invocation_server_addr
@@ -503,22 +506,22 @@ fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
503506
sidechain_storage,
504507
&last_synced_header,
505508
)
509+
.await
506510
.unwrap();
507511
}
508512

509513
// ------------------------------------------------------------------------
510514
// start parentchain syncing loop (subscribe to header updates)
511-
thread::Builder::new()
512-
.name("parentchain_sync_loop".to_owned())
513-
.spawn(move || {
514-
if let Err(e) =
515-
subscribe_to_parentchain_new_headers(parentchain_handler, last_synced_header)
516-
{
517-
error!("Parentchain block syncing terminated with a failure: {:?}", e);
518-
}
519-
println!("[!] Parentchain block syncing has terminated");
520-
})
521-
.unwrap();
515+
tokio::task::spawn_blocking(move || {
516+
if let Err(e) =
517+
subscribe_to_parentchain_new_headers(parentchain_handler, last_synced_header)
518+
{
519+
error!("Parentchain block syncing terminated with a failure: {:?}", e);
520+
}
521+
println!("[!] Parentchain block syncing has terminated");
522+
})
523+
.await
524+
.unwrap();
522525
}
523526

524527
// ------------------------------------------------------------------------
@@ -552,7 +555,7 @@ fn spawn_worker_for_shard_polling<InitializationHandler>(
552555
InitializationHandler: TrackInitialization + Sync + Send + 'static,
553556
{
554557
let shard_for_initialized = *shard;
555-
thread::spawn(move || {
558+
tokio::task::spawn_blocking(move || {
556559
const POLL_INTERVAL_SECS: u64 = 2;
557560

558561
loop {
@@ -716,7 +719,7 @@ fn fetch_marblerun_events_every_hour<E>(
716719
E: RemoteAttestation + Clone + Sync + Send + 'static,
717720
{
718721
let enclave = enclave.clone();
719-
let handle = thread::spawn(move || {
722+
let handle = tokio::task::spawn(move || {
720723
const POLL_INTERVAL_5_MINUTES_IN_SECS: u64 = 5 * 60;
721724
loop {
722725
info!("Polling marblerun events for quotes to register");
@@ -729,7 +732,7 @@ fn fetch_marblerun_events_every_hour<E>(
729732
marblerun_base_url.clone(),
730733
);
731734

732-
thread::sleep(Duration::from_secs(POLL_INTERVAL_5_MINUTES_IN_SECS));
735+
tokio::time::sleep(Duration::from_secs(POLL_INTERVAL_5_MINUTES_IN_SECS));
733736
}
734737
});
735738

service/src/sidechain_setup.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use its_consensus_slots::start_slot_worker;
3333
use its_primitives::types::block::SignedBlock as SignedSidechainBlock;
3434
use its_storage::{interface::FetchBlocks, start_sidechain_pruning_loop, BlockPruner};
3535
use log::*;
36-
use std::{sync::Arc, thread};
36+
use std::sync::Arc;
3737
use tokio::runtime::Handle;
3838

3939
pub(crate) fn sidechain_start_untrusted_rpc_server<Enclave, SidechainStorage>(
@@ -54,7 +54,7 @@ pub(crate) fn sidechain_start_untrusted_rpc_server<Enclave, SidechainStorage>(
5454
});
5555
}
5656

57-
pub(crate) fn sidechain_init_block_production<Enclave, SidechainStorage, ParentchainHandler>(
57+
pub(crate) async fn sidechain_init_block_production<Enclave, SidechainStorage, ParentchainHandler>(
5858
enclave: Arc<Enclave>,
5959
register_enclave_xt_header: &Header,
6060
we_are_primary_validateer: bool,
@@ -89,30 +89,28 @@ where
8989
// Start interval sidechain block production (execution of trusted calls, sidechain block production).
9090
let sidechain_enclave_api = enclave;
9191
println!("[+] Spawning thread for sidechain block production");
92-
thread::Builder::new()
93-
.name("interval_block_production_timer".to_owned())
94-
.spawn(move || {
95-
let future = start_slot_worker(
96-
|| execute_trusted_calls(sidechain_enclave_api.as_ref()),
97-
SLOT_DURATION,
98-
);
99-
block_on(future);
100-
println!("[!] Sidechain block production loop has terminated");
101-
})
102-
.map_err(|e| Error::Custom(Box::new(e)))?;
92+
tokio::task::spawn_blocking(move || {
93+
let future = start_slot_worker(
94+
|| execute_trusted_calls(sidechain_enclave_api.as_ref()),
95+
SLOT_DURATION,
96+
);
97+
block_on(future);
98+
println!("[!] Sidechain block production loop has terminated");
99+
})
100+
.await
101+
.map_err(|e| Error::Custom(Box::new(e)))?;
103102

104103
// ------------------------------------------------------------------------
105104
// start sidechain pruning loop
106-
thread::Builder::new()
107-
.name("sidechain_pruning_loop".to_owned())
108-
.spawn(move || {
109-
start_sidechain_pruning_loop(
110-
&sidechain_storage,
111-
SIDECHAIN_PURGE_INTERVAL,
112-
SIDECHAIN_PURGE_LIMIT,
113-
);
114-
})
115-
.map_err(|e| Error::Custom(Box::new(e)))?;
105+
tokio::task::spawn_blocking(move || {
106+
start_sidechain_pruning_loop(
107+
&sidechain_storage,
108+
SIDECHAIN_PURGE_INTERVAL,
109+
SIDECHAIN_PURGE_LIMIT,
110+
);
111+
})
112+
.await
113+
.map_err(|e| Error::Custom(Box::new(e)))?;
116114

117115
Ok(updated_header.unwrap_or_else(|| last_synced_header.clone()))
118116
}

0 commit comments

Comments
 (0)