Switch to parachain::ValidationParams

This commit is contained in:
Bastian Köcher
2019-06-21 13:17:37 +02:00
parent b59952178a
commit a36527e360
6 changed files with 145 additions and 1374 deletions
+21 -22
View File
@@ -19,16 +19,17 @@ use crate::{ParachainBlockData, WitnessData};
use rio::TestExternalities;
use keyring::AccountKeyring;
use runtime_primitives::{generic::BlockId, traits::{Block as BlockT, Header as HeaderT}};
use executor::{WasmExecutor, error::Result, wasmi::RuntimeValue::{I64, I32}};
use executor::{WasmExecutor, error::Result, wasmi::RuntimeValue::I32};
use test_client::{
TestClientBuilder, TestClientBuilderExt, DefaultTestClientBuilderExt, Client, LongestChain,
runtime::{Block, Transfer, Hash, WASM_BINARY}
runtime::{Block, Transfer, Hash, WASM_BINARY, Header}
};
use consensus_common::SelectChain;
use parachain::ValidationParams;
use codec::Encode;
fn call_validate_block(parent_hash: Hash, block_data: ParachainBlockData<Block>) -> Result<()> {
fn call_validate_block(parent_head: Header, block_data: ParachainBlockData<Block>) -> Result<()> {
let mut ext = TestExternalities::default();
WasmExecutor::new().call_with_custom_signature(
&mut ext,
@@ -36,13 +37,17 @@ fn call_validate_block(parent_hash: Hash, block_data: ParachainBlockData<Block>)
&WASM_BINARY,
"validate_block",
|alloc| {
let arguments = (parent_hash, block_data).encode();
let arguments_offset = alloc(&arguments)?;
let params = ValidationParams {
block_data: block_data.encode(),
parent_head: parent_head.encode(),
ingress: Vec::new(),
}.encode();
let params_offset = alloc(&params)?;
Ok(
vec![
I32(arguments_offset as i32),
I64(arguments.len() as i64),
I32(params_offset as i32),
I32(params.len() as i32),
]
)
},
@@ -111,10 +116,8 @@ fn build_block_with_proof(
#[test]
fn validate_block_with_no_extrinsics() {
let (client, longest_chain) = create_test_client();
let witness_data_storage_root = *longest_chain
.best_chain()
.expect("Best block exists")
.state_root();
let parent_head = longest_chain.best_chain().expect("Best block exists");
let witness_data_storage_root = *parent_head.state_root();
let (block, witness_data) = build_block_with_proof(&client, Vec::new());
let (header, extrinsics) = block.deconstruct();
@@ -124,16 +127,14 @@ fn validate_block_with_no_extrinsics() {
witness_data,
witness_data_storage_root
);
call_validate_block(client.info().chain.genesis_hash, block_data).expect("Calls `validate_block`");
call_validate_block(parent_head, block_data).expect("Calls `validate_block`");
}
#[test]
fn validate_block_with_extrinsics() {
let (client, longest_chain) = create_test_client();
let witness_data_storage_root = *longest_chain
.best_chain()
.expect("Best block exists")
.state_root();
let parent_head = longest_chain.best_chain().expect("Best block exists");
let witness_data_storage_root = *parent_head.state_root();
let (block, witness_data) = build_block_with_proof(&client, create_extrinsics());
let (header, extrinsics) = block.deconstruct();
@@ -143,17 +144,15 @@ fn validate_block_with_extrinsics() {
witness_data,
witness_data_storage_root
);
call_validate_block(client.info().chain.genesis_hash, block_data).expect("Calls `validate_block`");
call_validate_block(parent_head, block_data).expect("Calls `validate_block`");
}
#[test]
#[should_panic]
fn validate_block_invalid_parent_hash() {
let (client, longest_chain) = create_test_client();
let witness_data_storage_root = *longest_chain
.best_chain()
.expect("Best block exists")
.state_root();
let parent_head = longest_chain.best_chain().expect("Best block exists");
let witness_data_storage_root = *parent_head.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));
@@ -164,5 +163,5 @@ fn validate_block_invalid_parent_hash() {
witness_data,
witness_data_storage_root
);
call_validate_block(client.info().chain.genesis_hash, block_data).expect("Calls `validate_block`");
call_validate_block(parent_head, block_data).expect("Calls `validate_block`");
}