lib.rs 2.84 KB
Newer Older
Gav's avatar
Gav committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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/>.

//! The Polkadot runtime. This can be compiled with #[no_std], ready for Wasm.

#![cfg_attr(not(feature = "std"), no_std)]

extern crate substrate_runtime_std as rstd;
22
23
#[macro_use] extern crate substrate_runtime_io as runtime_io;
extern crate substrate_runtime_support as runtime_support;
24
#[cfg(all(feature = "std", test))] extern crate substrate_keyring as keyring;
Gav's avatar
Gav committed
25

26
#[cfg(feature = "std")] extern crate rustc_hex;
Gav's avatar
Gav committed
27
28

extern crate substrate_codec as codec;
29
#[cfg(feature = "std")] #[macro_use] extern crate substrate_primitives as primitives;
Gav's avatar
Gav committed
30
31
extern crate polkadot_primitives;

32
#[cfg(test)] #[macro_use] extern crate hex_literal;
Gav's avatar
Gav committed
33

34
pub mod api;
35
pub mod environment;
Gav's avatar
Gav committed
36
37
pub mod runtime;

38
#[cfg(feature = "std")] pub mod genesismap;
Gav's avatar
Gav committed
39
40
41
42
43

/// Type definitions and helpers for transactions.
pub mod transaction {
	use rstd::ops;
	use polkadot_primitives::Signature;
44
	pub use polkadot_primitives::{Transaction, Function, UncheckedTransaction};
Gav's avatar
Gav committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

	/// A type-safe indicator that a transaction has been checked.
	#[derive(PartialEq, Eq, Clone)]
	#[cfg_attr(feature = "std", derive(Debug))]
	pub struct CheckedTransaction(UncheckedTransaction);

	impl CheckedTransaction {
		/// Get a reference to the checked signature.
		pub fn signature(&self) -> &Signature {
			&self.0.signature
		}
	}

	impl ops::Deref for CheckedTransaction {
		type Target = Transaction;

		fn deref(&self) -> &Transaction {
			&self.0.transaction
		}
	}

66
67
68
69
70
71
72
73
74
75
76
77
78
79
	/// Check the validity of a transaction: whether it can appear at the given index
	/// and whether it is correctly authenticated.
	pub fn check(tx: UncheckedTransaction, index: u64) -> Result<CheckedTransaction, UncheckedTransaction> {
		match tx.transaction.function.inherent_index() {
			Some(correct_index) => {
				if index != correct_index || !tx.is_well_formed() { return Err(tx) }
				return Ok(CheckedTransaction(tx));
			}
			None => {
				// non-inherent functions must appear after inherent.
				if index < Function::inherent_functions() { return Err(tx) }
			}
		}

80
		let msg = ::codec::Slicable::encode(&tx.transaction);
Gav's avatar
Gav committed
81
82
83
84
85
86
87
		if ::runtime_io::ed25519_verify(&tx.signature.0, &msg, &tx.transaction.signed) {
			Ok(CheckedTransaction(tx))
		} else {
			Err(tx)
		}
	}
}