Skip to content

Commit

Permalink
Merge pull request #5 from mangata-finance/feature/fix_multi_node_setup
Browse files Browse the repository at this point in the history
Feature/fix multi node setup
  • Loading branch information
mateuszaaa authored Sep 9, 2021
2 parents a388297 + bc2bbcc commit 6b0e372
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
29 changes: 19 additions & 10 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 @@ -870,10 +870,12 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
Some(previous_block_extrinsics) => {
//TODO include serialize/deserialize seed field in header
//and use received seed instead
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());

if previous_block_extrinsics.len() > 1{
let mut seed = extrinsic_shuffler::apply_inherents_and_fetch_seed::<Block, Self>(
let block = if previous_block_extrinsics.len() > 1{
let seed = extrinsic_shuffler::apply_inherents_and_fetch_seed::<Block, Self>(
&runtime_api,
&at,
body.clone(),
Expand All @@ -883,18 +885,25 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
sp_blockchain::Error::Backend(format!("{}", e))
})?;

let mut slice: &[u8] = & mut seed.seed;
header.set_seed(<Block::Hash>::decode(& mut slice).unwrap());
}
let shuffled_extrinsics = extrinsic_shuffler::shuffle::<Block,Self>(&runtime_api, &at, previous_block_extrinsics, 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)
};



// TODO fail gracefully
let prev_header = self.backend.blockchain().header(BlockId::Hash(*parent_hash)).unwrap().unwrap();
header.set_extrinsics_root(*prev_header.extrinsics_root());

runtime_api.execute_block_with_context(
&at,
execution_context,
Block::new(header, previous_block_extrinsics),
block,
)?;
}
None => {
Expand Down
21 changes: 14 additions & 7 deletions client/shuffler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ fn fisher_yates<T>(data: &mut Vec<T>, seed: [u8; 32]) {
}
}

pub fn shuffle_using_seed<Block: BlockT>(
extrinsics: Vec<(Option<AccountId32>, Block::Extrinsic)>,
pub fn shuffle_using_seed<E: Encode>(
extrinsics: Vec<(Option<AccountId32>, E)>,
seed: [u8; 32],
) -> Vec<Block::Extrinsic> {
) -> Vec<E> {
log::debug!(target: "block_shuffler", "shuffling extrinsics with seed: {:2X?}", &seed[..]);
log::debug!(target: "block_shuffler", "origin order: [");
for (_,tx) in extrinsics.iter() {
log::debug!(target: "block_shuffler", "{:?}", BlakeTwo256::hash(&tx.encode()));
}
log::debug!(target: "block_shuffler", "]");

// generate exact number of slots for each account
// [ Alice, Alice, Alice, ... , Bob, Bob, Bob, ... ]
let mut slots: Vec<Option<AccountId32>> =
Expand Down Expand Up @@ -120,11 +127,12 @@ pub fn shuffle_using_seed<Block: BlockT>(
})
.collect();

log::debug!(target: "block_shuffler", "shuffled order");
log::debug!(target: "block_shuffler", "shuffled order:[");
for tx in shuffled_extrinsics.iter() {
let tx_hash = BlakeTwo256::hash(&tx.encode());
log::debug!(target: "block_shuffler", "extrinsic:{:?}", tx_hash);
log::debug!(target: "block_shuffler", "{:?}", tx_hash);
}
log::debug!(target: "block_shuffler", "]");

shuffled_extrinsics
}
Expand All @@ -143,7 +151,6 @@ where
Api: ProvideRuntimeApi<Block> + 'a,
Api::Api: ExtrinsicInfoRuntimeApi<Block>,
{
log::debug!(target: "block_shuffler", "shuffling extrinsics with seed: {:#X?}", seed.seed );

let extrinsics: Vec<(Option<AccountId32>, Block::Extrinsic)> = extrinsics
.into_iter()
Expand All @@ -161,7 +168,7 @@ where
(who, tx)
}).collect();

shuffle_using_seed::<Block>(extrinsics, seed.seed)
shuffle_using_seed::<Block::Extrinsic>(extrinsics, seed.seed)
}

#[derive(derive_more::Display, Debug)]
Expand Down
20 changes: 13 additions & 7 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ use sp_runtime::{
transaction_validity::{TransactionValidity, TransactionSource},
};
use codec::{Codec, Encode};
use extrinsic_shuffler::shuffle_using_seed;
// 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 +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);
// 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)

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 6b0e372

Please sign in to comment.