lib.rs 2.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

17
//! WASM validation for adder parachain.
18

19
20
21
#![no_std]

#![feature(
22
	alloc, core_intrinsics, lang_items, core_panic_info, alloc_error_handler
23
24
25
)]

#[global_allocator]
26
static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;
27
28
29

use core::{intrinsics, panic};
use parachain::ValidationResult;
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
30
use parachain::codec::{Encode, Decode};
31
use adder::{HeadData, BlockData};
32

Adrian Brink's avatar
Adrian Brink committed
33
#[panic_handler]
Sergey Pepyakin's avatar
Sergey Pepyakin committed
34
#[no_mangle]
Gav Wood's avatar
Gav Wood committed
35
pub fn panic(_info: &panic::PanicInfo) -> ! {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
36
37
38
39
40
	unsafe {
		intrinsics::abort()
	}
}

41
#[alloc_error_handler]
42
#[no_mangle]
43
pub fn oom(_: ::core::alloc::Layout) -> ! {
44
45
46
47
48
49
50
	unsafe {
		intrinsics::abort();
	}
}

#[no_mangle]
pub extern fn validate(offset: usize, len: usize) -> usize {
51
	let params = unsafe { ::parachain::wasm_api::load_params(offset, len) };
52
53
54
55
56
57
	let parent_head = HeadData::decode(&mut &params.parent_head[..])
		.expect("invalid parent head format.");

	let block_data = BlockData::decode(&mut &params.block_data[..])
		.expect("invalid block data format.");

58
	let parent_hash = ::tiny_keccak::keccak256(&params.parent_head[..]);
59

60
61
62
63
64
65
66
	// we also add based on incoming data from messages. ignoring unknown message
	// kinds.
	let from_messages = ::adder::process_messages(
		params.ingress.iter().map(|incoming| &incoming.data[..])
	);

	match ::adder::execute(parent_hash, parent_head, &block_data, from_messages) {
67
68
69
		Ok(new_head) => parachain::wasm_api::write_result(
			ValidationResult { head_data: new_head.encode() }
		),
70
71
		Err(_) => panic!("execution failure"),
	}
72
}