Skip to content

Commit

Permalink
shuffle extrinsics at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszaaa committed Sep 13, 2021
1 parent 99fa608 commit b57a1ac
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 40 deletions.
3 changes: 1 addition & 2 deletions client/block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ where
&self.api,
&self.block_id,
previous_block_extrinsics,
seed.clone().seed
&seed.seed
)
};

Expand Down Expand Up @@ -287,7 +287,6 @@ where
);
header.set_extrinsics_root(extrinsics_root);
header.set_seed(seed);
log::debug!(target: "mat", "SEED :{:?}", header.seed());

let proof = self.api.extract_proof();

Expand Down
1 change: 0 additions & 1 deletion client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,6 @@ where

let prev_header = self.client.header(BlockId::Hash(parent_hash)).unwrap().unwrap();
let seed = header.seed();
debug!(target: "babe", "PREV SEED {:?}", pre_header);
let key = &viable_epoch.as_ref().authorities[babe_pre_digest.authority_index() as usize];
self.validate_seed_signature(prev_header.seed(), seed, key.0.as_ref())?;

Expand Down
22 changes: 3 additions & 19 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use parking_lot::{Mutex, RwLock};
use codec::{Encode, Decode};
use hash_db::Prefix;
use sp_core::{
ShufflingSeed,
convert_hash,
storage::{well_known_keys, ChildInfo, PrefixedStorageKey, StorageData, StorageKey},
ChangesTrieConfiguration, ExecutionContext, NativeOrEncoded,
Expand All @@ -38,7 +37,7 @@ use sp_runtime::{
Justification, BuildStorage,
generic::{BlockId, SignedBlock, DigestItem},
traits::{
Hash, Block as BlockT, Header as HeaderT, Zero, NumberFor,
Block as BlockT, Header as HeaderT, Zero, NumberFor,
HashFor, SaturatedConversion, One, DigestFor, UniqueSaturatedInto,
},
};
Expand Down Expand Up @@ -857,7 +856,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
(true, Some(_), _) => {}
// We should enact state, but don't have any storage changes, so we need to execute the
// block.
(true, ref mut storage_changes @ None, Some(ref body)) => {
(true, ref mut storage_changes @ None, Some(ref _body)) => {
let runtime_api = self.runtime_api();
let execution_context = if import_block.origin == BlockOrigin::NetworkInitialSync {
ExecutionContext::Syncing
Expand All @@ -872,22 +871,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
let prev_header = self.backend.blockchain().header(BlockId::Hash(*parent_hash)).unwrap().unwrap();
let mut header = import_block.header.clone();
header.set_extrinsics_root(*prev_header.extrinsics_root());
log::debug!(target: "mat", "SEED :{:?}", header.seed());
let block = if previous_block_extrinsics.len() > 1{
let shuffled_extrinsics = extrinsic_shuffler::shuffle::<Block,Self>(&runtime_api, &at, previous_block_extrinsics, header.seed().seed);
// temporarly shuffle extrinsics here as we cannot pass shuffling seed to
// runtime easily
// 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);
Block::new(header.clone(), shuffled_extrinsics)
}else{
Block::new(header.clone(), previous_block_extrinsics)
};



let block = Block::new(header.clone(), previous_block_extrinsics);

runtime_api.execute_block_with_context(
&at,
Expand Down
7 changes: 3 additions & 4 deletions client/shuffler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn fisher_yates<T>(data: &mut Vec<T>, seed: [u8; 32]) {

pub fn shuffle_using_seed<E: Encode>(
extrinsics: Vec<(Option<AccountId32>, E)>,
seed: H256,
seed: &H256,
) -> Vec<E> {
log::debug!(target: "block_shuffler", "shuffling extrinsics with seed: {:2X?}", seed.as_bytes());
log::debug!(target: "block_shuffler", "origin order: [");
Expand All @@ -105,8 +105,7 @@ pub fn shuffle_using_seed<E: Encode>(
});

// shuffle slots
//TODO get rid of clone!
fisher_yates(&mut slots, seed.as_fixed_bytes().clone());
fisher_yates(&mut slots, seed.to_fixed_bytes());

// fill slots using extrinsics in order
// [ Alice, Bob, ... , Alice, Bob ]
Expand Down Expand Up @@ -140,7 +139,7 @@ pub fn shuffle<'a, Block, Api>(
api: &ApiRef<'a, Api::Api>,
block_id: &BlockId<Block>,
extrinsics: Vec<Block::Extrinsic>,
seed: H256,
seed: &H256,
) -> Vec<Block::Extrinsic>
where
Block: BlockT,
Expand Down
18 changes: 4 additions & 14 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ use sp_runtime::{
transaction_validity::{TransactionValidity, TransactionSource},
};
use codec::{Codec, Encode};
// use extrinsic_shuffler::shuffle_using_seed;
use frame_system::{extrinsics_root, DigestOf};
/// Trait that can be used to execute a block.
pub trait ExecuteBlock<Block: BlockT> {
Expand Down Expand Up @@ -297,7 +296,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 @@ -312,19 +311,10 @@ where
// execute extrinsics
let (header, extrinsics) = block.deconstruct();

// 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 shuffled_extrinsics = extrinsic_shuffler::shuffle_using_seed::<Block::Extrinsic>(extrinsics_with_author, &header.seed().seed);
Self::execute_extrinsics_with_book_keeping(shuffled_extrinsics, *header.number());

// 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 b57a1ac

Please sign in to comment.