-
Notifications
You must be signed in to change notification settings - Fork 498
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 pending
support for eth_getBlockByNumber
#1048
Merged
sorpaas
merged 8 commits into
polkadot-evm:master
from
moonbeam-foundation:tgm-pending-block
Jun 23, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d66aaa1
Add `pending` support for `eth_getBlockByNumber`
tgmichel 0921b4c
header not needed
tgmichel 8ee21b8
cleanup
tgmichel 53494cb
prettier
tgmichel addb94f
update some fields to be optional on pending
tgmichel 01cb4cc
update test
tgmichel a6c87c4
cleanup
tgmichel f45774d
Merge branch 'master' into tgm-pending-block
tgmichel 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
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,6 +23,7 @@ use jsonrpsee::core::RpcResult; | |||||
// Substrate | ||||||
use sc_client_api::backend::{Backend, StorageProvider}; | ||||||
use sc_transaction_pool::ChainApi; | ||||||
use sc_transaction_pool_api::InPoolTransaction; | ||||||
use sp_api::ProvideRuntimeApi; | ||||||
use sp_blockchain::HeaderBackend; | ||||||
use sp_core::hashing::keccak_256; | ||||||
|
@@ -43,6 +44,7 @@ where | |||||
C::Api: EthereumRuntimeRPCApi<B>, | ||||||
C: HeaderBackend<B> + StorageProvider<B, BE> + 'static, | ||||||
BE: Backend<B>, | ||||||
A: ChainApi<Block = B> + 'static, | ||||||
{ | ||||||
pub async fn block_by_hash(&self, hash: H256, full: bool) -> RpcResult<Option<RichBlock>> { | ||||||
let client = Arc::clone(&self.client); | ||||||
|
@@ -78,6 +80,7 @@ where | |||||
Some(hash), | ||||||
full, | ||||||
base_fee, | ||||||
false, | ||||||
); | ||||||
|
||||||
let substrate_hash = H256::from_slice(substrate_hash.as_ref()); | ||||||
|
@@ -103,54 +106,99 @@ where | |||||
let client = Arc::clone(&self.client); | ||||||
let block_data_cache = Arc::clone(&self.block_data_cache); | ||||||
let backend = Arc::clone(&self.backend); | ||||||
let graph = Arc::clone(&self.graph); | ||||||
|
||||||
let id = match frontier_backend_client::native_block_id::<B, C>( | ||||||
match frontier_backend_client::native_block_id::<B, C>( | ||||||
client.as_ref(), | ||||||
backend.as_ref(), | ||||||
Some(number), | ||||||
) | ||||||
.await? | ||||||
{ | ||||||
Some(id) => id, | ||||||
None => return Ok(None), | ||||||
}; | ||||||
let substrate_hash = client | ||||||
.expect_block_hash_from_id(&id) | ||||||
.map_err(|_| internal_err(format!("Expect block number from id: {}", id)))?; | ||||||
Some(id) => { | ||||||
let substrate_hash = 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(client.as_ref(), substrate_hash); | ||||||
let schema = fc_storage::onchain_storage_schema(client.as_ref(), substrate_hash); | ||||||
|
||||||
let block = block_data_cache.current_block(schema, substrate_hash).await; | ||||||
let statuses = block_data_cache | ||||||
.current_transaction_statuses(schema, substrate_hash) | ||||||
.await; | ||||||
let block = block_data_cache.current_block(schema, substrate_hash).await; | ||||||
let statuses = block_data_cache | ||||||
.current_transaction_statuses(schema, substrate_hash) | ||||||
.await; | ||||||
|
||||||
let base_fee = client.runtime_api().gas_price(substrate_hash).ok(); | ||||||
let base_fee = client.runtime_api().gas_price(substrate_hash).ok(); | ||||||
|
||||||
match (block, statuses) { | ||||||
(Some(block), Some(statuses)) => { | ||||||
let hash = H256::from(keccak_256(&rlp::encode(&block.header))); | ||||||
match (block, statuses) { | ||||||
(Some(block), Some(statuses)) => { | ||||||
let hash = H256::from(keccak_256(&rlp::encode(&block.header))); | ||||||
let mut rich_block = rich_block_build( | ||||||
block, | ||||||
statuses.into_iter().map(Option::Some).collect(), | ||||||
Some(hash), | ||||||
full, | ||||||
base_fee, | ||||||
false, | ||||||
); | ||||||
|
||||||
let mut rich_block = rich_block_build( | ||||||
block, | ||||||
statuses.into_iter().map(Option::Some).collect(), | ||||||
Some(hash), | ||||||
full, | ||||||
base_fee, | ||||||
); | ||||||
let substrate_hash = H256::from_slice(substrate_hash.as_ref()); | ||||||
if let Some(parent_hash) = self | ||||||
.forced_parent_hashes | ||||||
.as_ref() | ||||||
.and_then(|parent_hashes| parent_hashes.get(&substrate_hash).cloned()) | ||||||
{ | ||||||
rich_block.inner.header.parent_hash = parent_hash | ||||||
} | ||||||
|
||||||
let substrate_hash = H256::from_slice(substrate_hash.as_ref()); | ||||||
if let Some(parent_hash) = self | ||||||
.forced_parent_hashes | ||||||
.as_ref() | ||||||
.and_then(|parent_hashes| parent_hashes.get(&substrate_hash).cloned()) | ||||||
{ | ||||||
rich_block.inner.header.parent_hash = parent_hash | ||||||
Ok(Some(rich_block)) | ||||||
} | ||||||
_ => Ok(None), | ||||||
} | ||||||
} | ||||||
None if number == BlockNumber::Pending => { | ||||||
let api = client.runtime_api(); | ||||||
let best_hash = client.info().best_hash; | ||||||
|
||||||
Ok(Some(rich_block)) | ||||||
// Get current in-pool transactions | ||||||
let mut xts: Vec<<B as BlockT>::Extrinsic> = Vec::new(); | ||||||
// ready validated pool | ||||||
xts.extend( | ||||||
graph | ||||||
.validated_pool() | ||||||
.ready() | ||||||
.map(|in_pool_tx| in_pool_tx.data().clone()) | ||||||
.collect::<Vec<<B as BlockT>::Extrinsic>>(), | ||||||
); | ||||||
|
||||||
// future validated pool | ||||||
xts.extend( | ||||||
graph | ||||||
.validated_pool() | ||||||
.futures() | ||||||
.iter() | ||||||
.map(|(_hash, extrinsic)| extrinsic.clone()) | ||||||
.collect::<Vec<<B as BlockT>::Extrinsic>>(), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
); | ||||||
|
||||||
let (block, statuses) = api | ||||||
.pending_block(best_hash, xts) | ||||||
.map_err(|_| internal_err(format!("Runtime access error at {}", best_hash)))?; | ||||||
|
||||||
let base_fee = api.gas_price(best_hash).ok(); | ||||||
|
||||||
match (block, statuses) { | ||||||
(Some(block), Some(statuses)) => Ok(Some(rich_block_build( | ||||||
block, | ||||||
statuses.into_iter().map(Option::Some).collect(), | ||||||
None, | ||||||
tgmichel marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
full, | ||||||
base_fee, | ||||||
true, | ||||||
))), | ||||||
_ => Ok(None), | ||||||
} | ||||||
} | ||||||
_ => Ok(None), | ||||||
None => Ok(None), | ||||||
} | ||||||
} | ||||||
|
||||||
|
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
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
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.