Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jsonrpsee client #1266

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2709,6 +2709,7 @@ dependencies = [
"substrate-client-keystore",
"teerex-primitives",
"thiserror 1.0.40",
"tokio",
"ws",
]

Expand Down Expand Up @@ -8226,7 +8227,6 @@ dependencies = [
"sp-runtime",
"sp-runtime-interface",
"url 2.3.1",
"ws",
]

[[package]]
Expand Down
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
12 changes: 6 additions & 6 deletions enclave-runtime/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
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