Skip to content

Commit

Permalink
Merge pull request #9 from paritytech/stas-6-validate-block-parent-hash
Browse files Browse the repository at this point in the history
Pass parent hash to the validate_block function
  • Loading branch information
bkchr authored May 7, 2019
2 parents 2700e3b + 41f4460 commit 15362b6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
6 changes: 4 additions & 2 deletions runtime/src/validate_block/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ trait Storage {
#[cfg(not(feature = "std"))]
#[doc(hidden)]
pub fn validate_block<B: BlockT, E: ExecuteBlock<B>>(
mut block_data: &[u8],
mut arguments: &[u8],
) {
use codec::Decode;

let block_data = crate::ParachainBlockData::<B>::decode(&mut block_data)
let (parent_hash, block_data): (B::Hash, crate::ParachainBlockData::<B>) = Decode::decode(&mut arguments)
.expect("Could not decode parachain block.");
// TODO: Add `PolkadotInherent`.
let block = B::new(block_data.header, block_data.extrinsics);
assert!(parent_hash == *block.header().parent_hash(), "Invalid parent hash");

let storage = WitnessStorage::<B>::new(
block_data.witness_data,
block_data.witness_data_storage_root,
Expand Down
12 changes: 6 additions & 6 deletions runtime/src/validate_block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ macro_rules! register_validate_block_impl {

#[no_mangle]
unsafe fn validate_block(
block_data: *const u8,
block_data_len: u64,
arguments: *const u8,
arguments_len: u64,
) {
let block_data = $crate::slice::from_raw_parts(
block_data,
block_data_len as usize,
let arguments = $crate::slice::from_raw_parts(
arguments,
arguments_len as usize,
);

$crate::validate_block::implementation::validate_block::<
$block, $block_executor
>(block_data);
>(arguments);
}
}
};
Expand Down
40 changes: 31 additions & 9 deletions runtime/src/validate_block/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use runtime_primitives::traits::{Block as BlockT, Header as HeaderT};
use executor::{WasmExecutor, error::Result, wasmi::RuntimeValue::{I64, I32}};
use test_client::{
TestClientBuilder, TestClient,
runtime::{Block, Transfer}, TestClientBuilderExt,
runtime::{Block, Transfer, Hash}, TestClientBuilderExt,
client_ext::TestClient as _,
};

use std::collections::HashMap;
Expand All @@ -33,21 +34,21 @@ use codec::Encode;
const WASM_CODE: &[u8] =
include_bytes!("../../../test/runtime/wasm/target/wasm32-unknown-unknown/release/cumulus_test_runtime.compact.wasm");

fn call_validate_block(block_data: ParachainBlockData<Block>) -> Result<()> {
fn call_validate_block(parent_hash: Hash, block_data: ParachainBlockData<Block>) -> Result<()> {
let mut ext = TestExternalities::default();
WasmExecutor::new().call_with_custom_signature(
&mut ext,
1024,
&WASM_CODE,
"validate_block",
|alloc| {
let block_data = block_data.encode();
let block_data_offset = alloc(&block_data)?;
let arguments = (parent_hash, block_data).encode();
let arguments_offset = alloc(&arguments)?;

Ok(
vec![
I32(block_data_offset as i32),
I64(block_data.len() as i64),
I32(arguments_offset as i32),
I64(arguments.len() as i64),
]
)
},
Expand Down Expand Up @@ -131,7 +132,7 @@ fn validate_block_with_no_extrinsics() {
witness_data,
witness_data_storage_root
);
call_validate_block(block_data).expect("Calls `validate_block`");
call_validate_block(client.genesis_hash(), block_data).expect("Calls `validate_block`");
}

#[test]
Expand All @@ -150,5 +151,26 @@ fn validate_block_with_extrinsics() {
witness_data,
witness_data_storage_root
);
call_validate_block(block_data).expect("Calls `validate_block`");
}
call_validate_block(client.genesis_hash(), block_data).expect("Calls `validate_block`");
}

#[test]
#[should_panic]
fn validate_block_invalid_parent_hash() {
let client = create_test_client();
let witness_data_storage_root = *client
.best_block_header()
.expect("Best block exists")
.state_root();
let (block, witness_data) = build_block_with_proof(&client, Vec::new());
let (mut header, extrinsics) = block.deconstruct();
header.set_parent_hash(Hash::from_low_u64_be(1));

let block_data = ParachainBlockData::new(
header,
extrinsics,
witness_data,
witness_data_storage_root
);
call_validate_block(client.genesis_hash(), block_data).expect("Calls `validate_block`");
}

0 comments on commit 15362b6

Please sign in to comment.