Skip to content

Commit

Permalink
more rpc methods (#1636)
Browse files Browse the repository at this point in the history
* implement rpc system_version

* started...

* SidechainBlockHeader cache crate implemented. tests pass

* write to header cache

* implement chain_getHeader

* implement cli get-sidechain-header. works

* system_version rpc and cli

* fix tests

* clippy
  • Loading branch information
brenzi authored Nov 6, 2024
1 parent 10fffb6 commit 1d15915
Show file tree
Hide file tree
Showing 27 changed files with 528 additions and 19 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2577,6 +2577,7 @@ dependencies = [
"itp-time-utils",
"itp-types",
"itp-utils",
"its-primitives",
"log 0.4.20",
"pallet-balances",
"pallet-enclave-bridge",
Expand Down Expand Up @@ -3710,6 +3711,16 @@ dependencies = [
"thiserror 1.0.9",
]

[[package]]
name = "its-block-header-cache"
version = "0.1.0"
dependencies = [
"its-primitives",
"sgx_tstd",
"thiserror 1.0.44",
"thiserror 1.0.9",
]

[[package]]
name = "its-block-verification"
version = "0.9.0"
Expand Down Expand Up @@ -3754,6 +3765,7 @@ dependencies = [
"itp-types",
"itp-utils",
"its-block-composer",
"its-block-header-cache",
"its-block-verification",
"its-consensus-common",
"its-consensus-slots",
Expand Down Expand Up @@ -3788,6 +3800,7 @@ dependencies = [
"itp-test",
"itp-types",
"itp-utils",
"its-block-header-cache",
"its-block-verification",
"its-primitives",
"its-state",
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ members = [
"core-primitives/utils",
"service",
"sidechain/block-composer",
"sidechain/block-header-cache",
"sidechain/block-verification",
"sidechain/consensus/aura",
"sidechain/consensus/common",
Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ itp-stf-primitives = { path = "../core-primitives/stf-primitives" }
itp-time-utils = { path = "../core-primitives/time-utils" }
itp-types = { path = "../core-primitives/types" }
itp-utils = { path = "../core-primitives/utils" }
its-primitives = { path = "../sidechain/primitives" }

[features]
default = []
Expand Down
7 changes: 7 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use crate::commands::Commands;
use clap::Parser;
use ita_stf::{guess_the_number::GuessTheNumberInfo, ParentchainsInfo};
use itp_node_api::api_client::Metadata;
use its_primitives::types::header::SidechainHeader;
use sp_application_crypto::KeyTypeId;
use sp_core::{H160, H256};
use thiserror::Error;
Expand Down Expand Up @@ -113,9 +114,15 @@ pub enum CliResultOk {
U32 {
value: u32,
},
SidechainHeader {
header: SidechainHeader,
},
ParentchainsInfo {
info: ParentchainsInfo,
},
String {
value: String,
},
GuessAttempts {
value: u8,
},
Expand Down
62 changes: 62 additions & 0 deletions cli/src/trusted_base_cli/commands/get_header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use crate::{
command_utils::get_worker_api_direct, trusted_cli::TrustedCli, Cli, CliError, CliResult,
CliResultOk,
};
use codec::Decode;
use itc_rpc_client::direct_client::DirectApi;
use itp_rpc::{RpcRequest, RpcResponse, RpcReturnValue};
use itp_types::DirectRequestStatus;
use itp_utils::FromHexPrefixed;
use its_primitives::types::header::SidechainHeader;
use log::error;

#[derive(Parser)]
pub struct GetSidechainHeaderCommand {}

impl GetSidechainHeaderCommand {
pub(crate) fn run(&self, cli: &Cli, _trusted_args: &TrustedCli) -> CliResult {
let direct_api = get_worker_api_direct(cli);
let rpc_method = "chain_getHeader".to_owned();
let jsonrpc_call: String = RpcRequest::compose_jsonrpc_call(rpc_method, vec![]).unwrap();
let rpc_response_str = direct_api.get(&jsonrpc_call).unwrap();
// Decode RPC response.
let rpc_response: RpcResponse = serde_json::from_str(&rpc_response_str)
.map_err(|err| CliError::WorkerRpcApi { msg: err.to_string() })?;
let rpc_return_value = RpcReturnValue::from_hex(&rpc_response.result)
// Replace with `inspect_err` once it's stable.
.map_err(|err| {
error!("Failed to decode RpcReturnValue: {:?}", err);
CliError::WorkerRpcApi { msg: "failed to decode RpcReturnValue".to_string() }
})?;

if rpc_return_value.status == DirectRequestStatus::Error {
error!("{}", String::decode(&mut rpc_return_value.value.as_slice()).unwrap());
return Err(CliError::WorkerRpcApi { msg: "rpc error".to_string() })
}

let header = SidechainHeader::decode(&mut rpc_return_value.value.as_slice())
// Replace with `inspect_err` once it's stable.
.map_err(|err| {
error!("Failed to decode sidechain header: {:?}", err);
CliError::WorkerRpcApi { msg: err.to_string() }
})?;
println!("{:?}", header);
Ok(CliResultOk::SidechainHeader { header })
}
}
2 changes: 2 additions & 0 deletions cli/src/trusted_base_cli/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod balance;
pub mod get_fingerprint;
pub mod get_header;
pub mod get_parentchains_info;
pub mod get_shard;
pub mod get_shard_vault;
Expand All @@ -8,6 +9,7 @@ pub mod get_total_issuance;
pub mod nonce;
pub mod transfer;
pub mod unshield_funds;
pub mod version;

#[cfg(feature = "test")]
pub mod set_balance;
63 changes: 63 additions & 0 deletions cli/src/trusted_base_cli/commands/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use crate::{
command_utils::get_worker_api_direct, trusted_cli::TrustedCli, Cli, CliError, CliResult,
CliResultOk,
};
use codec::Decode;
use itc_rpc_client::direct_client::DirectApi;
use itp_rpc::{RpcRequest, RpcResponse, RpcReturnValue};

use itp_types::DirectRequestStatus;
use itp_utils::FromHexPrefixed;

use log::error;

#[derive(Parser)]
pub struct VersionCommand {}

impl VersionCommand {
pub(crate) fn run(&self, cli: &Cli, _trusted_args: &TrustedCli) -> CliResult {
let direct_api = get_worker_api_direct(cli);
let rpc_method = "system_version".to_owned();
let jsonrpc_call: String = RpcRequest::compose_jsonrpc_call(rpc_method, vec![]).unwrap();
let rpc_response_str = direct_api.get(&jsonrpc_call).unwrap();
// Decode RPC response.
let rpc_response: RpcResponse = serde_json::from_str(&rpc_response_str)
.map_err(|err| CliError::WorkerRpcApi { msg: err.to_string() })?;
let rpc_return_value = RpcReturnValue::from_hex(&rpc_response.result)
// Replace with `inspect_err` once it's stable.
.map_err(|err| {
error!("Failed to decode RpcReturnValue: {:?}", err);
CliError::WorkerRpcApi { msg: "failed to decode RpcReturnValue".to_string() }
})?;

if rpc_return_value.status == DirectRequestStatus::Error {
error!("{}", String::decode(&mut rpc_return_value.value.as_slice()).unwrap());
return Err(CliError::WorkerRpcApi { msg: "rpc error".to_string() })
}

let version = String::from_utf8(rpc_return_value.value)
// Replace with `inspect_err` once it's stable.
.map_err(|err| {
error!("Failed to decode sidechain header: {:?}", err);
CliError::WorkerRpcApi { msg: err.to_string() }
})?;
println!("{}", version);
Ok(CliResultOk::String { value: version })
}
}
15 changes: 12 additions & 3 deletions cli/src/trusted_base_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use crate::trusted_base_cli::commands::set_balance::SetBalanceCommand;
use crate::{
trusted_base_cli::commands::{
balance::BalanceCommand, get_fingerprint::GetFingerprintCommand,
get_parentchains_info::GetParentchainsInfoCommand, get_shard::GetShardCommand,
get_shard_vault::GetShardVaultCommand, get_total_issuance::GetTotalIssuanceCommand,
nonce::NonceCommand, transfer::TransferCommand, unshield_funds::UnshieldFundsCommand,
get_header::GetSidechainHeaderCommand, get_parentchains_info::GetParentchainsInfoCommand,
get_shard::GetShardCommand, get_shard_vault::GetShardVaultCommand,
get_total_issuance::GetTotalIssuanceCommand, nonce::NonceCommand,
transfer::TransferCommand, unshield_funds::UnshieldFundsCommand, version::VersionCommand,
},
trusted_cli::TrustedCli,
trusted_command_utils::get_keystore_path,
Expand Down Expand Up @@ -69,11 +70,17 @@ pub enum TrustedBaseCommand {
/// get shard vault for shielding (if defined for this worker)
GetShardVault(GetShardVaultCommand),

/// get sidechain header
GetSidechainHeader(GetSidechainHeaderCommand),

/// get total issuance of this shard's native token
GetTotalIssuance(GetTotalIssuanceCommand),

/// get info for all parentchains' sync status
GetParentchainsInfo(GetParentchainsInfoCommand),

/// get a version string for the enclave
Version(VersionCommand),
}

impl TrustedBaseCommand {
Expand All @@ -91,7 +98,9 @@ impl TrustedBaseCommand {
TrustedBaseCommand::GetParentchainsInfo(cmd) => cmd.run(cli, trusted_cli),
TrustedBaseCommand::GetShard(cmd) => cmd.run(cli, trusted_cli),
TrustedBaseCommand::GetShardVault(cmd) => cmd.run(cli, trusted_cli),
TrustedBaseCommand::GetSidechainHeader(cmd) => cmd.run(cli, trusted_cli),
TrustedBaseCommand::GetTotalIssuance(cmd) => cmd.run(cli, trusted_cli),
TrustedBaseCommand::Version(cmd) => cmd.run(cli, trusted_cli),
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions enclave-runtime/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ dependencies = [
"itp-top-pool-author",
"itp-types",
"itp-utils",
"its-block-header-cache",
"its-block-verification",
"its-primitives",
"its-rpc-handler",
Expand Down Expand Up @@ -2426,6 +2427,15 @@ dependencies = [
"thiserror",
]

[[package]]
name = "its-block-header-cache"
version = "0.1.0"
dependencies = [
"its-primitives",
"sgx_tstd",
"thiserror",
]

[[package]]
name = "its-block-verification"
version = "0.9.0"
Expand Down Expand Up @@ -2462,6 +2472,7 @@ dependencies = [
"itp-types",
"itp-utils",
"its-block-composer",
"its-block-header-cache",
"its-block-verification",
"its-consensus-common",
"its-consensus-slots",
Expand Down Expand Up @@ -2491,6 +2502,7 @@ dependencies = [
"itp-sgx-crypto",
"itp-types",
"itp-utils",
"its-block-header-cache",
"its-block-verification",
"its-primitives",
"its-state",
Expand Down
1 change: 1 addition & 0 deletions enclave-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ itp-top-pool = { path = "../core-primitives/top-pool", default-features = false,
itp-top-pool-author = { path = "../core-primitives/top-pool-author", default-features = false, features = ["sgx"] }
itp-types = { path = "../core-primitives/types", default-features = false }
itp-utils = { path = "../core-primitives/utils", default-features = false }
its-block-header-cache = { path = "../sidechain/block-header-cache", default-features = false, features = ["sgx"] }
its-block-verification = { path = "../sidechain/block-verification", default-features = false }
its-primitives = { path = "../sidechain/primitives", default-features = false }
its-rpc-handler = { path = "../sidechain/rpc-handler", default-features = false, features = ["sgx"] }
Expand Down
6 changes: 5 additions & 1 deletion enclave-runtime/src/initialization/global_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ use itp_top_pool_author::{
author::{Author, AuthorTopFilter},
};
use itp_types::{Block as ParentchainBlock, SignedBlock as SignedParentchainBlock};
use its_block_header_cache::SidechainBlockHeaderCache;
use its_primitives::{
traits::{Block as SidechainBlockTrait, SignedBlock as SignedSidechainBlockTrait},
types::block::SignedBlock as SignedSidechainBlock,
types::{block::SignedBlock as SignedSidechainBlock, header::SidechainHeader},
};
use its_sidechain::{
aura::block_importer::BlockImporter as SidechainBlockImporter,
Expand Down Expand Up @@ -421,6 +422,9 @@ lazy_static! {

/// Global nonce cache for the Target B parentchain..
pub static ref GLOBAL_TARGET_B_PARENTCHAIN_NONCE_CACHE: Arc<NonceCache> = Default::default();

/// Global sidechain header cache
pub static ref GLOBAL_SIDECHAIN_BLOCK_HEADER_CACHE: Arc<SidechainBlockHeaderCache<SidechainHeader>> = Default::default();
}

/// Solochain Handler.
Expand Down
15 changes: 11 additions & 4 deletions enclave-runtime/src/initialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ use crate::{
GLOBAL_ATTESTATION_HANDLER_COMPONENT, GLOBAL_INTEGRITEE_PARENTCHAIN_LIGHT_CLIENT_SEAL,
GLOBAL_OCALL_API_COMPONENT, GLOBAL_RPC_WS_HANDLER_COMPONENT,
GLOBAL_SHIELDING_KEY_REPOSITORY_COMPONENT, GLOBAL_SIDECHAIN_BLOCK_COMPOSER_COMPONENT,
GLOBAL_SIDECHAIN_BLOCK_SYNCER_COMPONENT, GLOBAL_SIDECHAIN_IMPORT_QUEUE_COMPONENT,
GLOBAL_SIDECHAIN_IMPORT_QUEUE_WORKER_COMPONENT, GLOBAL_SIGNING_KEY_REPOSITORY_COMPONENT,
GLOBAL_STATE_HANDLER_COMPONENT, GLOBAL_STATE_KEY_REPOSITORY_COMPONENT,
GLOBAL_STATE_OBSERVER_COMPONENT, GLOBAL_TARGET_A_PARENTCHAIN_LIGHT_CLIENT_SEAL,
GLOBAL_SIDECHAIN_BLOCK_HEADER_CACHE, GLOBAL_SIDECHAIN_BLOCK_SYNCER_COMPONENT,
GLOBAL_SIDECHAIN_IMPORT_QUEUE_COMPONENT, GLOBAL_SIDECHAIN_IMPORT_QUEUE_WORKER_COMPONENT,
GLOBAL_SIGNING_KEY_REPOSITORY_COMPONENT, GLOBAL_STATE_HANDLER_COMPONENT,
GLOBAL_STATE_KEY_REPOSITORY_COMPONENT, GLOBAL_STATE_OBSERVER_COMPONENT,
GLOBAL_TARGET_A_PARENTCHAIN_LIGHT_CLIENT_SEAL,
GLOBAL_TARGET_B_PARENTCHAIN_LIGHT_CLIENT_SEAL, GLOBAL_TOP_POOL_AUTHOR_COMPONENT,
GLOBAL_WEB_SOCKET_SERVER_COMPONENT,
},
Expand Down Expand Up @@ -80,6 +81,9 @@ use jsonrpc_core::IoHandler;
use log::*;
use sp_core::crypto::Pair;
use std::{collections::HashMap, path::PathBuf, string::String, sync::Arc};

const VERSION: &str = env!("CARGO_PKG_VERSION");

pub(crate) fn init_enclave(
mu_ra_url: String,
untrusted_worker_url: String,
Expand Down Expand Up @@ -182,6 +186,8 @@ pub(crate) fn init_enclave(
getter_executor,
shielding_key_repository,
ocall_api.clone(),
VERSION.into(),
GLOBAL_SIDECHAIN_BLOCK_HEADER_CACHE.clone(),
);

#[cfg(feature = "sidechain")]
Expand Down Expand Up @@ -236,6 +242,7 @@ pub(crate) fn init_enclave_sidechain_components() -> EnclaveResult<()> {
top_pool_author,
parentchain_block_import_dispatcher,
ocall_api.clone(),
GLOBAL_SIDECHAIN_BLOCK_HEADER_CACHE.clone(),
));

let sidechain_block_import_queue = GLOBAL_SIDECHAIN_IMPORT_QUEUE_COMPONENT.get()?;
Expand Down
Loading

0 comments on commit 1d15915

Please sign in to comment.