Skip to content

Commit

Permalink
switch to jsonrpsee client
Browse files Browse the repository at this point in the history
well well

taplo fmt

lets try again

fix clippy
  • Loading branch information
haerdib committed Apr 4, 2023
1 parent 5fe663b commit 0b90ed6
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 53 deletions.
3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down
4 changes: 2 additions & 2 deletions cli/src/command_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,7 +40,7 @@ pub(crate) fn get_shielding_key(cli: &Cli) -> Result<Rsa3072PubKey, String> {
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 {
Expand Down
3 changes: 2 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ pub struct Cli {
command: Commands,
}

fn main() {
#[tokio::main]
async fn main() {
env_logger::init();

let cli = Cli::parse();
Expand Down
2 changes: 1 addition & 1 deletion core-primitives/node-api/api-client-extensions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion core-primitives/node-api/api-client-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions core-primitives/node-api/api-client-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ 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<Block>;
pub type ParentchainExtrinsicSigner =
StaticExtrinsicSigner<sp_core::sr25519::Pair, PairSignature>;

pub type ParentchainApi =
Api<ParentchainExtrinsicSigner, WsRpcClient, ParentchainExtrinsicParams, Runtime>;
Api<ParentchainExtrinsicSigner, JsonrpseeClient, ParentchainExtrinsicParams, Runtime>;
}
4 changes: 2 additions & 2 deletions core-primitives/node-api/factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -63,7 +63,7 @@ impl NodeApiFactory {

impl CreateNodeApi for NodeApiFactory {
fn create_api(&self) -> Result<ParentchainApi> {
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)?;
Expand Down
43 changes: 23 additions & 20 deletions service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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;
Expand Down Expand Up @@ -113,7 +114,8 @@ pub type EnclaveWorker =
Worker<Config, NodeApiFactory, Enclave, InitializationHandler<WorkerModeProvider>>;
pub type Event = substrate_api_client::EventRecord<RuntimeEvent, Hash>;

fn main() {
#[tokio::main]
async fn main() {
// Setup logging
env_logger::init();

Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -273,7 +276,7 @@ fn main() {

/// FIXME: needs some discussion (restructuring?)
#[allow(clippy::too_many_arguments)]
fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
async fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
config: Config,
shard: &ShardIdentifier,
enclave: Arc<E>,
Expand Down Expand Up @@ -317,7 +320,7 @@ fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
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,
Expand Down Expand Up @@ -375,7 +378,7 @@ fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
{
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
Expand Down Expand Up @@ -503,22 +506,22 @@ fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
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();
}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -550,7 +553,7 @@ fn spawn_worker_for_shard_polling<InitializationHandler>(
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 {
Expand Down Expand Up @@ -703,7 +706,7 @@ fn fetch_marblerun_events_every_hour<E>(
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");
Expand All @@ -716,7 +719,7 @@ fn fetch_marblerun_events_every_hour<E>(
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));
}
});

Expand Down
44 changes: 21 additions & 23 deletions service/src/sidechain_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Enclave, SidechainStorage>(
Expand All @@ -54,7 +54,7 @@ pub(crate) fn sidechain_start_untrusted_rpc_server<Enclave, SidechainStorage>(
});
}

pub(crate) fn sidechain_init_block_production<Enclave, SidechainStorage, ParentchainHandler>(
pub(crate) async fn sidechain_init_block_production<Enclave, SidechainStorage, ParentchainHandler>(
enclave: Arc<Enclave>,
register_enclave_xt_header: &Header,
we_are_primary_validateer: bool,
Expand Down Expand Up @@ -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()))
}
Expand Down

0 comments on commit 0b90ed6

Please sign in to comment.