Skip to content

Commit

Permalink
support json rpc 2.0 with author_submitAndWatchExtrinsic trusted ca…
Browse files Browse the repository at this point in the history
…lls (#1623)

* add workaround to support json rpc 2.0 with `author_submitAndWatch` trusted call

* update trusted_op_direct_request

* fix obsolete imports in rpc_responder

* make direct request no longer generic, it can only return the hash anyhow, we can easily introduce it later.

* fix deserializing rpc response

* [cli/trusted_operation] remove unnecessary nesting

* fix clippy

* extract await status update

* [cli] fix correctly extract hash in benchmarks

* fmt

* add check that we can only us direct_calls in send direct request

* improve logs

* add comment about storing initial top hash

* add todos for rpc refactor.

* better comment

* add more comments

* fix handling of indirect calls

* derive copy for trusted operation status

* fix type inference in trusted transfer cmd

* [cli] merge await status update from benchmark and send_direct_request into one function.

* [cli] partially fix benchmarks

* fix clippy

* fix local setup for two workers

* [cli] fix: always close client in trusted_operation and return if the status is invalid.
  • Loading branch information
clangenb authored Oct 27, 2024
1 parent 036e859 commit a2e0037
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 177 deletions.
38 changes: 23 additions & 15 deletions cli/src/benchmark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use crate::{
get_layer_two_nonce,
trusted_cli::TrustedCli,
trusted_command_utils::{get_identifiers, get_keystore_path, get_pair_from_str},
trusted_operation::{get_json_request, get_state, perform_trusted_operation, wait_until},
trusted_operation::{
await_status, await_subscription_response, get_json_request, get_state,
perform_trusted_operation,
},
Cli, CliResult, CliResultOk, SR25519_KEY_TYPE,
};
use codec::Decode;
Expand All @@ -35,7 +38,7 @@ use itp_stf_primitives::{
};
use itp_types::{
AccountInfo, Balance, ShardIdentifier, TrustedOperationStatus,
TrustedOperationStatus::{InSidechainBlock, Submitted},
TrustedOperationStatus::InSidechainBlock,
};
use log::*;
use rand::Rng;
Expand All @@ -47,7 +50,7 @@ use sp_keystore::Keystore;
use std::{
boxed::Box,
string::ToString,
sync::mpsc::{channel, Receiver},
sync::mpsc::{channel, Receiver, Sender},
thread, time,
time::Instant,
vec::Vec,
Expand Down Expand Up @@ -89,6 +92,7 @@ struct BenchmarkClient {
account: sr25519_core::Pair,
current_balance: u128,
client_api: DirectClient,
sender: Sender<String>,
receiver: Receiver<String>,
}

Expand All @@ -104,8 +108,8 @@ impl BenchmarkClient {

debug!("setup sender and receiver");
let (sender, receiver) = channel();
client_api.watch(initial_request, sender);
BenchmarkClient { account, current_balance: initial_balance, client_api, receiver }
client_api.watch(initial_request, sender.clone());
BenchmarkClient { account, current_balance: initial_balance, client_api, sender, receiver }
}
}

Expand Down Expand Up @@ -161,7 +165,7 @@ impl BenchmarkCommand {
&mrenclave,
&shard,
)
.into_trusted_operation(trusted_args.direct);
.into_trusted_operation(true);

// For the last account we wait for confirmation in order to ensure all accounts were setup correctly
let wait_for_confirmation = i == self.number_clients - 1;
Expand Down Expand Up @@ -218,7 +222,8 @@ impl BenchmarkCommand {

let last_iteration = i == self.number_iterations - 1;
let jsonrpc_call = get_json_request(shard, &top, shielding_pubkey);
client.client_api.send(&jsonrpc_call).unwrap();

client.client_api.watch(jsonrpc_call, client.sender.clone());
let result = wait_for_top_confirmation(
self.wait_for_confirmation || last_iteration,
&client,
Expand Down Expand Up @@ -337,15 +342,22 @@ fn wait_for_top_confirmation(
) -> BenchmarkTransaction {
let started = Instant::now();

let submitted = wait_until(&client.receiver, is_submitted);
// the first response of `submitAndWatch` is just the plain top hash
let submitted = match await_subscription_response(&client.receiver) {
Ok(hash) => Some((hash, Instant::now())),
Err(e) => {
error!("recv error: {e:?}");
None
},
};

let confirmed = if wait_for_sidechain_block {
// We wait for the transaction hash that actually matches the submitted hash
loop {
let transaction_information = wait_until(&client.receiver, is_sidechain_block);
if let Some((hash, _)) = transaction_information {
let transaction_information = await_status(&client.receiver, is_sidechain_block).ok();
if let Some((hash, _status)) = transaction_information {
if hash == submitted.unwrap().0 {
break transaction_information
break Some((hash, Instant::now()))
}
}
}
Expand All @@ -364,10 +376,6 @@ fn wait_for_top_confirmation(
}
}

fn is_submitted(s: TrustedOperationStatus) -> bool {
matches!(s, Submitted)
}

fn is_sidechain_block(s: TrustedOperationStatus) -> bool {
matches!(s, InSidechainBlock(_))
}
Expand Down
12 changes: 9 additions & 3 deletions cli/src/evm/commands/evm_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
get_layer_two_evm_nonce, get_layer_two_nonce,
trusted_cli::TrustedCli,
trusted_command_utils::{get_identifiers, get_pair_from_str},
trusted_operation::perform_trusted_operation,
trusted_operation::{perform_trusted_operation, send_direct_request},
Cli, CliResult, CliResultOk,
};
use ita_stf::{Index, TrustedCall, TrustedGetter};
Expand All @@ -31,6 +31,7 @@ use itp_types::AccountId;
use log::*;
use sp_core::{crypto::Ss58Codec, Pair, H160, U256};
use std::{boxed::Box, vec::Vec};

#[derive(Parser)]
pub struct EvmCallCommands {
/// Sender's incognito AccountId in ss58check format, mnemonic or hex seed
Expand Down Expand Up @@ -81,7 +82,12 @@ impl EvmCallCommands {
)
.sign(&KeyPair::Sr25519(Box::new(sender)), nonce, &mrenclave, &shard)
.into_trusted_operation(trusted_args.direct);
Ok(perform_trusted_operation::<()>(cli, trusted_args, &function_call)
.map(|_| CliResultOk::None)?)

if trusted_args.direct {
Ok(send_direct_request(cli, trusted_args, &function_call).map(|_| CliResultOk::None)?)
} else {
Ok(perform_trusted_operation::<()>(cli, trusted_args, &function_call)
.map(|_| CliResultOk::None)?)
}
}
}
9 changes: 7 additions & 2 deletions cli/src/evm/commands/evm_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
get_layer_two_evm_nonce, get_layer_two_nonce,
trusted_cli::TrustedCli,
trusted_command_utils::{get_identifiers, get_pair_from_str},
trusted_operation::perform_trusted_operation,
trusted_operation::{perform_trusted_operation, send_direct_request},
Cli, CliResult, CliResultOk,
};
use ita_stf::{evm_helpers::evm_create_address, Index, TrustedCall, TrustedGetter};
Expand All @@ -33,6 +33,7 @@ use pallet_evm::{AddressMapping, HashedAddressMapping};
use sp_core::{crypto::Ss58Codec, Pair, H160, U256};
use sp_runtime::traits::BlakeTwo256;
use std::vec::Vec;

#[derive(Parser)]
pub struct EvmCreateCommands {
/// Sender's incognito AccountId in ss58check format, mnemonic or hex seed
Expand Down Expand Up @@ -79,7 +80,11 @@ impl EvmCreateCommands {
.sign(&from.into(), nonce, &mrenclave, &shard)
.into_trusted_operation(trusted_args.direct);

perform_trusted_operation::<()>(cli, trusted_args, &top)?;
if trusted_args.direct {
send_direct_request(cli, trusted_args, &top).map(|_| CliResultOk::None)?;
} else {
perform_trusted_operation::<()>(cli, trusted_args, &top).map(|_| CliResultOk::None)?;
}

let execution_address = evm_create_address(sender_evm_acc, evm_account_nonce);
info!("trusted call evm_create executed");
Expand Down
9 changes: 8 additions & 1 deletion cli/src/guess_the_number/commands/guess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
Cli, CliResult, CliResultOk,
};

use crate::trusted_operation::send_direct_request;
use ita_stf::{
guess_the_number::GuessTheNumberTrustedCall, Getter, Index, TrustedCall, TrustedCallSigned,
};
Expand Down Expand Up @@ -55,6 +56,12 @@ impl GuessCommand {
)
.sign(&KeyPair::Sr25519(Box::new(signer)), nonce, &mrenclave, &shard)
.into_trusted_operation(trusted_args.direct);
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top).map(|_| CliResultOk::None)?)

if trusted_args.direct {
Ok(send_direct_request(cli, trusted_args, &top).map(|_| CliResultOk::None)?)
} else {
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top)
.map(|_| CliResultOk::None)?)
}
}
}
9 changes: 8 additions & 1 deletion cli/src/guess_the_number/commands/push_by_one_day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
Cli, CliResult, CliResultOk,
};

use crate::trusted_operation::send_direct_request;
use ita_stf::{
guess_the_number::GuessTheNumberTrustedCall, Getter, Index, TrustedCall, TrustedCallSigned,
};
Expand Down Expand Up @@ -53,6 +54,12 @@ impl PushByOneDayCommand {
)
.sign(&KeyPair::Sr25519(Box::new(signer)), nonce, &mrenclave, &shard)
.into_trusted_operation(trusted_args.direct);
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top).map(|_| CliResultOk::None)?)

if trusted_args.direct {
Ok(send_direct_request(cli, trusted_args, &top).map(|_| CliResultOk::None)?)
} else {
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top)
.map(|_| CliResultOk::None)?)
}
}
}
10 changes: 8 additions & 2 deletions cli/src/guess_the_number/commands/set_winnings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
get_layer_two_nonce,
trusted_cli::TrustedCli,
trusted_command_utils::{get_identifiers, get_pair_from_str},
trusted_operation::perform_trusted_operation,
trusted_operation::{perform_trusted_operation, send_direct_request},
Cli, CliResult, CliResultOk,
};
use ita_parentchain_interface::integritee::Balance;
Expand Down Expand Up @@ -59,6 +59,12 @@ impl SetWinningsCommand {
)
.sign(&KeyPair::Sr25519(Box::new(signer)), nonce, &mrenclave, &shard)
.into_trusted_operation(trusted_args.direct);
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top).map(|_| CliResultOk::None)?)

if trusted_args.direct {
Ok(send_direct_request(cli, trusted_args, &top).map(|_| CliResultOk::None)?)
} else {
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top)
.map(|_| CliResultOk::None)?)
}
}
}
10 changes: 8 additions & 2 deletions cli/src/trusted_base_cli/commands/set_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
get_layer_two_nonce,
trusted_cli::TrustedCli,
trusted_command_utils::{get_identifiers, get_pair_from_str},
trusted_operation::perform_trusted_operation,
trusted_operation::{perform_trusted_operation, send_direct_request},
Cli, CliResult, CliResultOk,
};
use ita_parentchain_interface::integritee::Balance;
Expand Down Expand Up @@ -59,6 +59,12 @@ impl SetBalanceCommand {
)
.sign(&KeyPair::Sr25519(Box::new(signer)), nonce, &mrenclave, &shard)
.into_trusted_operation(trusted_args.direct);
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top).map(|_| CliResultOk::None)?)

if trusted_args.direct {
Ok(send_direct_request(cli, trusted_args, &top).map(|_| CliResultOk::None)?)
} else {
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top)
.map(|_| CliResultOk::None)?)
}
}
}
13 changes: 8 additions & 5 deletions cli/src/trusted_base_cli/commands/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
get_layer_two_nonce,
trusted_cli::TrustedCli,
trusted_command_utils::{get_accountid_from_str, get_identifiers, get_pair_from_str},
trusted_operation::perform_trusted_operation,
trusted_operation::{perform_trusted_operation, send_direct_request},
Cli, CliResult, CliResultOk,
};
use base58::ToBase58;
Expand Down Expand Up @@ -65,9 +65,12 @@ impl TransferCommand {
TrustedCall::balance_transfer(from.public().into(), to, self.amount)
.sign(&KeyPair::Sr25519(Box::new(from)), nonce, &mrenclave, &shard)
.into_trusted_operation(trusted_args.direct);
let res =
perform_trusted_operation::<()>(cli, trusted_args, &top).map(|_| CliResultOk::None)?;
info!("trusted call transfer executed");
Ok(res)

if trusted_args.direct {
Ok(send_direct_request(cli, trusted_args, &top).map(|_| CliResultOk::None)?)
} else {
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top)
.map(|_| CliResultOk::None)?)
}
}
}
11 changes: 9 additions & 2 deletions cli/src/trusted_base_cli/commands/unshield_funds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
get_layer_two_nonce,
trusted_cli::TrustedCli,
trusted_command_utils::{get_accountid_from_str, get_identifiers, get_pair_from_str},
trusted_operation::perform_trusted_operation,
trusted_operation::{perform_trusted_operation, send_direct_request},
Cli, CliResult, CliResultOk,
};
use ita_parentchain_interface::integritee::Balance;
Expand All @@ -31,6 +31,7 @@ use itp_stf_primitives::{
use log::*;
use sp_core::{crypto::Ss58Codec, Pair};
use std::boxed::Box;

#[derive(Parser)]
pub struct UnshieldFundsCommand {
/// Sender's incognito AccountId in ss58check format, mnemonic or hex seed
Expand Down Expand Up @@ -63,6 +64,12 @@ impl UnshieldFundsCommand {
TrustedCall::balance_unshield(from.public().into(), to, self.amount, shard)
.sign(&KeyPair::Sr25519(Box::new(from)), nonce, &mrenclave, &shard)
.into_trusted_operation(trusted_args.direct);
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top).map(|_| CliResultOk::None)?)

if trusted_args.direct {
Ok(send_direct_request(cli, trusted_args, &top).map(|_| CliResultOk::None)?)
} else {
Ok(perform_trusted_operation::<()>(cli, trusted_args, &top)
.map(|_| CliResultOk::None)?)
}
}
}
Loading

0 comments on commit a2e0037

Please sign in to comment.