Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Optimize the extraction of root block from Extrinsic (paritytech#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu authored Nov 11, 2021
1 parent 614f2c3 commit 11061fa
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/sc-consensus-subspace/src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ where
for extrinsic in block.block.extrinsics() {
match client
.runtime_api()
.extract_root_block(&block_to_check, extrinsic.encode())
.extract_root_block(&block_to_check, extrinsic)
{
Ok(Some(root_block)) => match &mut latest_root_block {
Some(latest_root_block) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/sc-consensus-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ where
match self
.client
.runtime_api()
.extract_root_block(&BlockId::Hash(parent_hash), extrinsic.encode())
.extract_root_block(&BlockId::Hash(parent_hash), extrinsic)
{
Ok(Some(root_block)) => {
if !root_blocks_set.remove(&root_block) {
Expand Down
4 changes: 2 additions & 2 deletions crates/sp-consensus-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ sp_api::decl_runtime_apis! {
/// Get the merkle tree root of records for specified segment index
fn records_root(segment_index: u64) -> Option<Sha256Hash>;

/// Try to decode an extrinsic as `store_root_block` extrinsic and get root block out of it
fn extract_root_block(encoded_extrinsic: Vec<u8>) -> Option<RootBlock>;
/// Returns `RootBlock` if the given extrinsic has one.
fn extract_root_block(ext: &Block::Extrinsic) -> Option<RootBlock>;

/// Extract block object mapping for a given block
fn extract_block_object_mapping(block: Block) -> BlockObjectMapping;
Expand Down
19 changes: 7 additions & 12 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use codec::{Compact, CompactLen, Decode, Encode};
use codec::{Compact, CompactLen, Encode};
use frame_support::{
construct_runtime, parameter_types,
weights::{
Expand Down Expand Up @@ -453,16 +453,11 @@ pub type Executive = frame_executive::Executive<
AllPallets,
>;

fn extract_root_block(encoded_extrinsic: Vec<u8>) -> Option<RootBlock> {
if let Ok(extrinsic) = UncheckedExtrinsic::decode(&mut encoded_extrinsic.as_slice()) {
if let Call::Subspace(pallet_subspace::Call::store_root_block { root_block }) =
extrinsic.function
{
return Some(root_block);
}
fn extract_root_block(ext: &UncheckedExtrinsic) -> Option<RootBlock> {
match ext.function {
Call::Subspace(pallet_subspace::Call::store_root_block { root_block }) => Some(root_block),
_ => None,
}

None
}

fn extract_feeds_block_object_mapping(
Expand Down Expand Up @@ -697,8 +692,8 @@ impl_runtime_apis! {
Subspace::records_root(segment_index)
}

fn extract_root_block(encoded_extrinsic: Vec<u8>) -> Option<RootBlock> {
extract_root_block(encoded_extrinsic)
fn extract_root_block(ext: &<Block as BlockT>::Extrinsic) -> Option<RootBlock> {
extract_root_block(ext)
}

fn extract_block_object_mapping(block: Block) -> BlockObjectMapping {
Expand Down
4 changes: 2 additions & 2 deletions substrate/substrate-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ cfg_if! {
}

fn extract_root_block(
_encoded_extrinsic: Vec<u8>,
_ext: &<Block as BlockT>::Extrinsic
) -> Option<subspace_core_primitives::RootBlock> {
panic!("Not needed in tests")
}
Expand Down Expand Up @@ -1352,7 +1352,7 @@ cfg_if! {
}

fn extract_root_block(
_encoded_extrinsic: Vec<u8>,
_ext: &<Block as BlockT>::Extrinsic
) -> Option<subspace_core_primitives::RootBlock> {
panic!("Not needed in tests")
}
Expand Down

0 comments on commit 11061fa

Please sign in to comment.