-
Notifications
You must be signed in to change notification settings - Fork 505
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
feat: add standard debug rpc #1237
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
// This file is part of Frontier. | ||
// | ||
// Copyright (c) 2023 Parity Technologies (UK) Ltd. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//! Debug rpc interface. | ||
|
||
use ethereum_types::H256; | ||
use jsonrpsee::{core::RpcResult, proc_macros::rpc}; | ||
|
||
use crate::types::{BlockNumberOrHash, Bytes}; | ||
|
||
/// Net rpc interface. | ||
#[rpc(server)] | ||
#[async_trait] | ||
pub trait DebugApi { | ||
/// Returns an RLP-encoded header with the given number or hash. | ||
#[method(name = "debug_getRawHeader")] | ||
async fn raw_header(&self, number: BlockNumberOrHash) -> RpcResult<Option<Bytes>>; | ||
|
||
/// Returns an RLP-encoded block with the given number or hash. | ||
#[method(name = "debug_getRawBlock")] | ||
async fn raw_block(&self, number: BlockNumberOrHash) -> RpcResult<Option<Bytes>>; | ||
|
||
/// Returns a EIP-2718 binary-encoded transaction with the given hash. | ||
#[method(name = "debug_getRawTransaction")] | ||
async fn raw_transaction(&self, hash: H256) -> RpcResult<Option<Bytes>>; | ||
|
||
/// Returns an array of EIP-2718 binary-encoded receipts with the given number of hash. | ||
#[method(name = "debug_getRawReceipts")] | ||
async fn raw_receipts(&self, number: BlockNumberOrHash) -> RpcResult<Vec<Bytes>>; | ||
|
||
/// Returns an array of recent bad blocks that the client has seen on the network. | ||
#[method(name = "debug_getBadBlocks")] | ||
fn bad_blocks(&self, number: BlockNumberOrHash) -> RpcResult<Vec<()>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
// This file is part of Frontier. | ||
// | ||
// Copyright (c) 2023 Parity Technologies (UK) Ltd. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use std::{marker::PhantomData, sync::Arc}; | ||
|
||
use ethereum::EnvelopedEncodable; | ||
use ethereum_types::H256; | ||
use jsonrpsee::core::{async_trait, RpcResult}; | ||
use rlp::Encodable; | ||
// Substrate | ||
use sc_client_api::backend::{Backend, StorageProvider}; | ||
use sp_api::ProvideRuntimeApi; | ||
use sp_blockchain::HeaderBackend; | ||
use sp_runtime::traits::Block as BlockT; | ||
// Frontier | ||
use fc_rpc_core::{types::*, DebugApiServer}; | ||
use fc_storage::OverrideHandle; | ||
use fp_rpc::EthereumRuntimeRPCApi; | ||
|
||
use crate::{cache::EthBlockDataCacheTask, frontier_backend_client, internal_err}; | ||
|
||
/// Debug API implementation. | ||
pub struct Debug<B: BlockT, C, BE> { | ||
client: Arc<C>, | ||
backend: Arc<dyn fc_api::Backend<B>>, | ||
overrides: Arc<OverrideHandle<B>>, | ||
block_data_cache: Arc<EthBlockDataCacheTask<B>>, | ||
_marker: PhantomData<BE>, | ||
} | ||
|
||
impl<B: BlockT, C, BE> Debug<B, C, BE> { | ||
pub fn new( | ||
client: Arc<C>, | ||
backend: Arc<dyn fc_api::Backend<B>>, | ||
overrides: Arc<OverrideHandle<B>>, | ||
block_data_cache: Arc<EthBlockDataCacheTask<B>>, | ||
) -> Self { | ||
Self { | ||
client, | ||
backend, | ||
overrides, | ||
block_data_cache, | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
async fn block_by(&self, number: BlockNumberOrHash) -> RpcResult<Option<ethereum::BlockV2>> | ||
where | ||
C: HeaderBackend<B> + StorageProvider<B, BE> + 'static, | ||
BE: Backend<B>, | ||
{ | ||
let id = match frontier_backend_client::native_block_id::<B, C>( | ||
self.client.as_ref(), | ||
self.backend.as_ref(), | ||
Some(number), | ||
) | ||
.await? | ||
{ | ||
Some(id) => id, | ||
None => return Ok(None), | ||
}; | ||
|
||
let substrate_hash = self | ||
.client | ||
.expect_block_hash_from_id(&id) | ||
.map_err(|_| internal_err(format!("Expect block number from id: {}", id)))?; | ||
let schema = fc_storage::onchain_storage_schema(self.client.as_ref(), substrate_hash); | ||
let block = self | ||
.block_data_cache | ||
.current_block(schema, substrate_hash) | ||
.await; | ||
Ok(block) | ||
} | ||
|
||
async fn transaction_by( | ||
&self, | ||
transaction_hash: H256, | ||
) -> RpcResult<Option<ethereum::TransactionV2>> | ||
where | ||
C: HeaderBackend<B> + StorageProvider<B, BE> + 'static, | ||
BE: Backend<B>, | ||
{ | ||
let (eth_block_hash, index) = match frontier_backend_client::load_transactions::<B, C>( | ||
self.client.as_ref(), | ||
self.backend.as_ref(), | ||
transaction_hash, | ||
true, | ||
) | ||
.await? | ||
{ | ||
Some((hash, index)) => (hash, index as usize), | ||
None => return Ok(None), | ||
}; | ||
|
||
let substrate_hash = match frontier_backend_client::load_hash::<B, C>( | ||
self.client.as_ref(), | ||
self.backend.as_ref(), | ||
eth_block_hash, | ||
) | ||
.await? | ||
{ | ||
Some(hash) => hash, | ||
None => return Ok(None), | ||
}; | ||
|
||
let schema = fc_storage::onchain_storage_schema(self.client.as_ref(), substrate_hash); | ||
let block = self | ||
.block_data_cache | ||
.current_block(schema, substrate_hash) | ||
.await; | ||
if let Some(block) = block { | ||
Ok(Some(block.transactions[index].clone())) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
async fn receipts_by( | ||
&self, | ||
number: BlockNumberOrHash, | ||
) -> RpcResult<Option<Vec<ethereum::ReceiptV3>>> | ||
where | ||
C: HeaderBackend<B> + StorageProvider<B, BE> + 'static, | ||
BE: Backend<B>, | ||
{ | ||
let id = match frontier_backend_client::native_block_id::<B, C>( | ||
self.client.as_ref(), | ||
self.backend.as_ref(), | ||
Some(number), | ||
) | ||
.await? | ||
{ | ||
Some(id) => id, | ||
None => return Ok(None), | ||
}; | ||
|
||
let substrate_hash = self | ||
.client | ||
.expect_block_hash_from_id(&id) | ||
.map_err(|_| internal_err(format!("Expect block number from id: {}", id)))?; | ||
|
||
let schema = fc_storage::onchain_storage_schema(self.client.as_ref(), substrate_hash); | ||
let handler = self | ||
.overrides | ||
.schemas | ||
.get(&schema) | ||
.unwrap_or(&self.overrides.fallback); | ||
let receipts = handler.current_receipts(substrate_hash); | ||
Ok(receipts) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<B, C, BE> DebugApiServer for Debug<B, C, BE> | ||
where | ||
B: BlockT, | ||
C: ProvideRuntimeApi<B>, | ||
C::Api: EthereumRuntimeRPCApi<B>, | ||
C: HeaderBackend<B> + StorageProvider<B, BE> + 'static, | ||
BE: Backend<B> + 'static, | ||
{ | ||
async fn raw_header(&self, number: BlockNumberOrHash) -> RpcResult<Option<Bytes>> { | ||
let block = self.block_by(number).await?; | ||
Ok(block.map(|block| Bytes::new(block.header.rlp_bytes().to_vec()))) | ||
} | ||
|
||
async fn raw_block(&self, number: BlockNumberOrHash) -> RpcResult<Option<Bytes>> { | ||
let block = self.block_by(number).await?; | ||
Ok(block.map(|block| Bytes::new(block.rlp_bytes().to_vec()))) | ||
} | ||
|
||
async fn raw_transaction(&self, hash: H256) -> RpcResult<Option<Bytes>> { | ||
let transaction = self.transaction_by(hash).await?; | ||
Ok(transaction.map(|transaction| Bytes::new(transaction.encode().to_vec()))) | ||
} | ||
|
||
async fn raw_receipts(&self, number: BlockNumberOrHash) -> RpcResult<Vec<Bytes>> { | ||
let receipts = self.receipts_by(number).await?.unwrap_or_default(); | ||
Ok(receipts | ||
.into_iter() | ||
.map(|receipt| Bytes::new(receipt.encode().to_vec())) | ||
.collect::<Vec<_>>()) | ||
} | ||
|
||
fn bad_blocks(&self, _number: BlockNumberOrHash) -> RpcResult<Vec<()>> { | ||
// `debug_getBadBlocks` wouldn't really be useful in a Substrate context. | ||
// The rationale for that is for debugging multi-client consensus issues, which we'll never face | ||
// (we may have multiple clients in the future, but for runtime it's only "multi-wasm-runtime", never "multi-EVM"). | ||
// We can simply return empty array for this API. | ||
Ok(vec![]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Methods like
block_by
,transaction_by
,receipts_by
have a lot of duplicate code with the methods in the eth module. I plan to add a helpful mapping backend type in the future RPs to reduce duplicate code and improve maintainability.