Skip to content

Commit

Permalink
temporarly move extrinsics shuffling to client side. When block object
Browse files Browse the repository at this point in the history
is passed to runtime code (execute_block_with_context) the block struct
is serialized & deserialized. In the same time we dont include
Header::seed field in serialization & deserialization because it breaks
JS api compatibility. That will be address in the future.
  • Loading branch information
mateuszaaa committed Sep 9, 2021
1 parent 5b0a05f commit 9688f20
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
13 changes: 11 additions & 2 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use sp_runtime::{
Justification, BuildStorage,
generic::{BlockId, SignedBlock, DigestItem},
traits::{
Block as BlockT, Header as HeaderT, Zero, NumberFor,
Hash, Block as BlockT, Header as HeaderT, Zero, NumberFor,
HashFor, SaturatedConversion, One, DigestFor, UniqueSaturatedInto,
},
};
Expand Down Expand Up @@ -897,10 +897,19 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
let prev_header = self.backend.blockchain().header(BlockId::Hash(*parent_hash)).unwrap().unwrap();
header.set_extrinsics_root(*prev_header.extrinsics_root());


// temporarly shuffle extrinsics here as we cannot pass shuffling seed to
// runtime easily
let shuffled_extrinsics = extrinsic_shuffler::shuffle::<Block,Self>(&runtime_api, &at, previous_block_extrinsics, seed);
// temporarly update extrinsics_root that stores hash of ordered extrinsics so it can be
// validated properly
let trie_root = HashFor::<Block>::ordered_trie_root(shuffled_extrinsics.iter().map(codec::Encode::encode).collect());
header.set_extrinsics_root(trie_root);

runtime_api.execute_block_with_context(
&at,
execution_context,
Block::new(header, previous_block_extrinsics),
Block::new(header.clone(), shuffled_extrinsics),
)?;
}
None => {
Expand Down
22 changes: 14 additions & 8 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ where
}

/// Actually execute all transitions for `block`.
pub fn execute_block(block: Block, info: Vec<Option<AccountId32>>) {
pub fn execute_block(block: Block, _info: Vec<Option<AccountId32>>) {
sp_io::init_tracing();
sp_tracing::within_span! {
sp_tracing::info_span!( "execute_block", ?block);
Expand All @@ -311,14 +311,20 @@ where

// execute extrinsics
let (header, extrinsics) = block.deconstruct();
let extrinsics_with_author: Vec<(Option<_>,_)> = info.into_iter().zip(extrinsics.into_iter()).collect();

let mut seed: [u8;32] = Default::default();
seed.copy_from_slice(header.seed().as_ref());
let shuffled_extrinsics = shuffle_using_seed::<Block>(extrinsics_with_author, seed);

Self::execute_extrinsics_with_book_keeping(shuffled_extrinsics, *header.number());

// TODO: shuffling temporarly moved to native code.
// Motivation:
// There is no easy way to pass seed from native to wasm runtime. Shuffling at
// runtime can be reverted once we have fully working Header::seed field
// (including serialization & deserialization that is currently missing)

// let extrinsics_with_author: Vec<(Option<_>,_)> = info.into_iter().zip(extrinsics.into_iter()).collect();
// let mut seed: [u8;32] = Default::default();
// seed.copy_from_slice(header.seed().as_ref());
// let shuffled_extrinsics = shuffle_using_seed::<Block>(extrinsics_with_author, seed);
// Self::execute_extrinsics_with_book_keeping(shuffled_extrinsics, *header.number());

Self::execute_extrinsics_with_book_keeping(extrinsics, *header.number());
if !signature_batching.verify() {
panic!("Signature verification failed.");
}
Expand Down

0 comments on commit 9688f20

Please sign in to comment.