Skip to content

Commit

Permalink
client-rpc changes for v0.9.43
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavcpawar committed Jul 20, 2023
1 parent 196efcb commit f164c5f
Showing 1 changed file with 59 additions and 58 deletions.
117 changes: 59 additions & 58 deletions client/rpc/debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
use futures::{SinkExt, StreamExt};
use jsonrpsee::core::RpcResult;
use futures::StreamExt;
use jsonrpsee::core::{async_trait, RpcResult};
pub use edgeware_rpc_core_debug::{DebugServer, TraceParams};

use tokio::{
Expand Down Expand Up @@ -62,7 +62,7 @@ impl Debug {
}
}

#[jsonrpsee::core::async_trait]
#[async_trait]
impl DebugServer for Debug {
/// Handler for `debug_traceTransaction` request. Communicates with the service-defined task
/// using channels.
Expand All @@ -71,13 +71,12 @@ impl DebugServer for Debug {
transaction_hash: H256,
params: Option<TraceParams>,
) -> RpcResult<single::TransactionTrace> {
let mut requester = self.requester.clone();
let requester = self.requester.clone();

let (tx, rx) = oneshot::channel();
// Send a message from the rpc handler to the service level task.
requester
.send(((RequesterInput::Transaction(transaction_hash), params), tx))
.await
.unbounded_send(((RequesterInput::Transaction(transaction_hash), params), tx))
.map_err(|err| {
internal_err(format!(
"failed to send request to debug service : {:?}",
Expand All @@ -99,13 +98,12 @@ impl DebugServer for Debug {
id: RequestBlockId,
params: Option<TraceParams>,
) -> RpcResult<Vec<single::TransactionTrace>> {
let mut requester = self.requester.clone();
let requester = self.requester.clone();

let (tx, rx) = oneshot::channel();
// Send a message from the rpc handler to the service level task.
requester
.send(((RequesterInput::Block(id), params), tx))
.await
.unbounded_send(((RequesterInput::Block(id), params), tx))
.map_err(|err| {
internal_err(format!(
"failed to send request to debug service : {:?}",
Expand Down Expand Up @@ -144,13 +142,13 @@ where
pub fn task(
client: Arc<C>,
backend: Arc<BE>,
frontier_backend: Arc<fc_db::Backend<B>>,
frontier_backend: Arc<dyn fc_db::BackendReader<B> + Send + Sync>,
permit_pool: Arc<Semaphore>,
overrides: Arc<OverrideHandle<B>>,
raw_max_memory_usage: usize,
) -> (impl Future<Output = ()>, DebugRequester) {
let (tx, mut rx): (DebugRequester, _) =
sc_utils::mpsc::tracing_unbounded("debug-requester");
sc_utils::mpsc::tracing_unbounded("debug-requester", 100_000);

let fut = async move {
loop {
Expand Down Expand Up @@ -284,7 +282,7 @@ where
fn handle_block_request(
client: Arc<C>,
backend: Arc<BE>,
frontier_backend: Arc<fc_db::Backend<B>>,
frontier_backend: Arc<dyn fc_db::BackendReader<B> + Send + Sync>,
request_block_id: RequestBlockId,
params: Option<TraceParams>,
overrides: Arc<OverrideHandle<B>>,
Expand All @@ -303,12 +301,12 @@ where
Err(internal_err("'pending' blocks are not supported"))
}
RequestBlockId::Hash(eth_hash) => {
match frontier_backend_client::load_hash::<B, C>(
match futures::executor::block_on(frontier_backend_client::load_hash::<B, C>(
client.as_ref(),
frontier_backend.as_ref(),
eth_hash,
) {
Ok(Some(id)) => Ok(id),
)) {
Ok(Some(hash)) => Ok(BlockId::Hash(hash)),
Ok(_) => Err(internal_err("Block hash not found".to_string())),
Err(e) => Err(e),
}
Expand All @@ -320,24 +318,24 @@ where
// Get Blockchain backend
let blockchain = backend.blockchain();
// Get the header I want to work with.
let header = match client.header(reference_id) {
let Ok(hash) = client.expect_block_hash_from_id(&reference_id) else {
return Err(internal_err("Block header not found"))
};
let header = match client.header(hash) {
Ok(Some(h)) => h,
_ => return Err(internal_err("Block header not found")),
};

// Get parent blockid.
let parent_block_id = BlockId::Hash(*header.parent_hash());
let parent_block_hash = *header.parent_hash();

let schema = frontier_backend_client::onchain_storage_schema::<B, C, BE>(
client.as_ref(),
reference_id,
);
let schema = fc_storage::onchain_storage_schema::<B, C, BE>(client.as_ref(), hash);

// Using storage overrides we align with `:ethereum_schema` which will result in proper
// SCALE decoding in case of migration.
let statuses = match overrides.schemas.get(&schema) {
Some(schema) => schema
.current_transaction_statuses(&reference_id)
.current_transaction_statuses(hash)
.unwrap_or_default(),
_ => {
return Err(internal_err(format!(
Expand All @@ -357,17 +355,17 @@ where

// Get block extrinsics.
let exts = blockchain
.body(reference_id)
.body(hash)
.map_err(|e| internal_err(format!("Fail to read blockchain db: {:?}", e)))?
.unwrap_or_default();

// Trace the block.
let f = || -> RpcResult<_> {
api.initialize_block(&parent_block_id, &header)
api.initialize_block(parent_block_hash, &header)
.map_err(|e| internal_err(format!("Runtime api access error: {:?}", e)))?;

let _result = api
.trace_block(&parent_block_id, exts, eth_tx_hashes)
.trace_block(parent_block_hash, exts, eth_tx_hashes)
.map_err(|e| {
internal_err(format!(
"Blockchain error when replaying block {} : {:?}",
Expand Down Expand Up @@ -419,55 +417,61 @@ where
fn handle_transaction_request(
client: Arc<C>,
backend: Arc<BE>,
frontier_backend: Arc<fc_db::Backend<B>>,
frontier_backend: Arc<dyn fc_db::BackendReader<B> + Send + Sync>,
transaction_hash: H256,
params: Option<TraceParams>,
overrides: Arc<OverrideHandle<B>>,
raw_max_memory_usage: usize,
) -> RpcResult<Response> {
let (tracer_input, trace_type) = Self::handle_params(params)?;

let (hash, index) = match frontier_backend_client::load_transactions::<B, C>(
client.as_ref(),
frontier_backend.as_ref(),
transaction_hash,
false,
) {
Ok(Some((hash, index))) => (hash, index as usize),
Ok(None) => return Err(internal_err("Transaction hash not found".to_string())),
Err(e) => return Err(e),
};
let (hash, index) =
match futures::executor::block_on(frontier_backend_client::load_transactions::<B, C>(
client.as_ref(),
frontier_backend.as_ref(),
transaction_hash,
false,
)) {
Ok(Some((hash, index))) => (hash, index as usize),
Ok(None) => return Err(internal_err("Transaction hash not found".to_string())),
Err(e) => return Err(e),
};

let reference_id =
match futures::executor::block_on(frontier_backend_client::load_hash::<B, C>(
client.as_ref(),
frontier_backend.as_ref(),
hash,
)) {
Ok(Some(hash)) => BlockId::Hash(hash),
Ok(_) => return Err(internal_err("Block hash not found".to_string())),
Err(e) => return Err(e),
};

let reference_id = match frontier_backend_client::load_hash::<B, C>(
client.as_ref(),
frontier_backend.as_ref(),
hash,
) {
Ok(Some(hash)) => hash,
Ok(_) => return Err(internal_err("Block hash not found".to_string())),
Err(e) => return Err(e),
};
// Get ApiRef. This handle allow to keep changes between txs in an internal buffer.
let api = client.runtime_api();
// Get Blockchain backend
let blockchain = backend.blockchain();
// Get the header I want to work with.
let header = match client.header(reference_id) {
let Ok(reference_hash) = client.expect_block_hash_from_id(&reference_id) else {
return Err(internal_err("Block header not found"))
};
let header = match client.header(reference_hash) {
Ok(Some(h)) => h,
_ => return Err(internal_err("Block header not found")),
};
// Get parent blockid.
let parent_block_id = BlockId::Hash(*header.parent_hash());
let parent_block_hash = *header.parent_hash();

// Get block extrinsics.
let exts = blockchain
.body(reference_id)
.body(reference_hash)
.map_err(|e| internal_err(format!("Fail to read blockchain db: {:?}", e)))?
.unwrap_or_default();

// Get DebugRuntimeApi version
let trace_api_version = if let Ok(Some(api_version)) =
api.api_version::<dyn DebugRuntimeApi<B>>(&parent_block_id)
api.api_version::<dyn DebugRuntimeApi<B>>(parent_block_hash)
{
api_version
} else {
Expand All @@ -476,19 +480,16 @@ where
));
};

let schema = frontier_backend_client::onchain_storage_schema::<B, C, BE>(
client.as_ref(),
reference_id,
);
let schema = fc_storage::onchain_storage_schema::<B, C, BE>(client.as_ref(), reference_hash);

// Get the block that contains the requested transaction. Using storage overrides we align
// with `:ethereum_schema` which will result in proper SCALE decoding in case of migration.
let reference_block = match overrides.schemas.get(&schema) {
Some(schema) => schema.current_block(&reference_id),
Some(schema) => schema.current_block(reference_hash),
_ => {
return Err(internal_err(format!(
"No storage override at {:?}",
reference_id
reference_hash
)))
}
};
Expand All @@ -498,12 +499,12 @@ where
let transactions = block.transactions;
if let Some(transaction) = transactions.get(index) {
let f = || -> RpcResult<_> {
api.initialize_block(&parent_block_id, &header)
api.initialize_block(parent_block_hash, &header)
.map_err(|e| internal_err(format!("Runtime api access error: {:?}", e)))?;

if trace_api_version >= 4 {
let _result = api
.trace_transaction(&parent_block_id, exts, &transaction)
.trace_transaction(parent_block_hash, exts, &transaction)
.map_err(|e| {
internal_err(format!(
"Runtime api access error (version {:?}): {:?}",
Expand All @@ -517,7 +518,7 @@ where
ethereum::TransactionV2::Legacy(tx) =>
{
#[allow(deprecated)]
api.trace_transaction_before_version_4(&parent_block_id, exts, &tx)
api.trace_transaction_before_version_4(parent_block_hash, exts, &tx)
.map_err(|e| {
internal_err(format!(
"Runtime api access error (legacy): {:?}",
Expand Down

0 comments on commit f164c5f

Please sign in to comment.