extract out all primitives

This commit is contained in:
Robert Habermeier
2018-02-01 11:13:55 +01:00
parent a3b9c2af7d
commit 188332cc4b
17 changed files with 386 additions and 87 deletions
+22 -3
View File
@@ -16,9 +16,10 @@
//! Block and header type definitions.
use bytes;
use bytes::{self, Vec};
use hash::H256;
use parachain;
use transaction::UncheckedTransaction;
/// Used to refer to a block number.
pub type Number = u64;
@@ -33,6 +34,22 @@ pub type TransactionHash = H256;
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Log(#[serde(with="bytes")] pub Vec<u8>);
/// A Polkadot relay chain block.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Block {
/// The block header.
pub header: Header,
/// All relay-chain transactions.
pub transactions: Vec<UncheckedTransaction>,
}
/// The digest of a block, useful for light-clients.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Digest {
/// All logs that have happened in the block.
pub logs: Vec<Log>,
}
/// A relay chain block header.
///
/// https://github.com/w3f/polkadot-spec/blob/master/spec.md#header
@@ -46,10 +63,12 @@ pub struct Header {
pub number: Number,
/// State root after this transition.
pub state_root: H256,
/// The root of the trie that represents this block's transactions, indexed by a 32-byte integer.
pub transaction_root: H256,
/// Parachain activity bitfield
pub parachain_activity: parachain::Activity,
/// Logs (generated by execution)
pub logs: Vec<Log>,
/// The digest of activity on the block.
pub digest: Digest,
}
/// A relay chain block body.
+4 -4
View File
@@ -57,7 +57,7 @@ pub fn blake2_128(data: &[u8]) -> [u8; 16] {
/// Do a XX 128-bit hash and place result in `dest`.
pub fn twox_128_into(data: &[u8], dest: &mut [u8; 16]) {
use ::std::hash::Hasher;
use ::core::hash::Hasher;
let mut h0 = twox_hash::XxHash::with_seed(0);
let mut h1 = twox_hash::XxHash::with_seed(1);
h0.write(data);
@@ -71,14 +71,14 @@ pub fn twox_128_into(data: &[u8], dest: &mut [u8; 16]) {
/// Do a XX 128-bit hash and return result.
pub fn twox_128(data: &[u8]) -> [u8; 16] {
let mut r: [u8; 16] = unsafe { ::std::mem::uninitialized() };
let mut r: [u8; 16] = [0; 16];
twox_128_into(data, &mut r);
r
}
/// Do a XX 256-bit hash and place result in `dest`.
pub fn twox_256_into(data: &[u8], dest: &mut [u8; 32]) {
use ::std::hash::Hasher;
use ::core::hash::Hasher;
use byteorder::{ByteOrder, LittleEndian};
let mut h0 = twox_hash::XxHash::with_seed(0);
let mut h1 = twox_hash::XxHash::with_seed(1);
@@ -100,7 +100,7 @@ pub fn twox_256_into(data: &[u8], dest: &mut [u8; 32]) {
/// Do a XX 256-bit hash and return result.
pub fn twox_256(data: &[u8]) -> [u8; 32] {
let mut r: [u8; 32] = unsafe { ::std::mem::uninitialized() };
let mut r: [u8; 32] = [0; 32];
twox_256_into(data, &mut r);
r
}
+1 -1
View File
@@ -25,7 +25,7 @@ impl<'a> HexDisplay<'a> {
}
impl<'a> ::core::fmt::Display for HexDisplay<'a> {
fn fmt(&self, fmtr: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
fn fmt(&self, fmtr: &mut ::core::fmt::Formatter) -> Result<(), ::core::fmt::Error> {
for byte in self.0 {
try!( fmtr.write_fmt(format_args!("{:02x}", byte)));
}
+33 -8
View File
@@ -53,23 +53,48 @@ extern crate alloc;
mod bytes;
pub mod block;
pub mod contract;
pub mod ed25519;
pub mod hash;
pub mod hashing;
pub mod hexdisplay;
pub mod parachain;
pub mod proposal;
pub mod runtime_function;
pub mod transaction;
pub mod uint;
pub mod validator;
pub mod ed25519;
pub mod hexdisplay;
pub mod hashing;
/// Alias to 160-bit hash when used in the context of an account address.
pub type Address = hash::H160;
/// Alias to 520-bit hash when used in the context of a signature.
pub type Signature = hash::H512;
pub use self::hash::{H160, H256};
pub use self::uint::{U256, U512};
pub use hashing::{blake2_256, twox_128, twox_256};
/// Virtual account ID that represents the idea of a dispatch/statement being signed by everybody
/// (who matters). Essentially this means that a majority of validators have decided it is
/// "correct".
pub const EVERYBODY: AccountId = [255u8; 32];
/// Alias to Ed25519 pubkey that identifies an account.
pub type AccountId = [u8; 32];
/// The Ed25519 pub key of an session that belongs to an authority. This is used as what the
/// external environment/consensus algorithm calls an "authority".
pub type SessionKey = AccountId;
/// Indentifier for a chain.
pub type ChainID = u64;
/// Index of a block in the chain.
pub type BlockNumber = u64;
/// Index of a transaction.
pub type TxOrder = u64;
/// A hash of some data.
pub type Hash = [u8; 32];
/// Alias to 520-bit hash when used in the context of a signature.
pub type Signature = hash::H512;
/// A hash function.
pub fn hash(data: &[u8]) -> hash::H256 {
blake2_256(data).into()
+2 -2
View File
@@ -57,11 +57,11 @@ pub struct CandidateReceipt {
/// The ID of the parachain this is a candidate for.
pub parachain_index: Id,
/// The collator's account ID
pub collator: ::Address,
pub collator: ::AccountId,
/// The head-data
pub head_data: HeadData,
/// Balance uploads to the relay chain.
pub balance_uploads: Vec<(::Address, ::uint::U256)>,
pub balance_uploads: Vec<(::AccountId, ::uint::U256)>,
/// Egress queue roots.
pub egress_queue_roots: Vec<(Id, ::hash::H256)>,
/// Fees paid from the chain to the relay chain validators
+89
View File
@@ -0,0 +1,89 @@
// 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/>.
//! Proposals for relay-chain governance.
//!
//! This describes a combination of a function ID and data that can be used to call into
//! an internal function.
use bytes;
/// Internal functions that can be dispatched to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum InternalFunction {
/// Set the system's code.
SystemSetCode = 0,
/// Set the number of sessions per era.
StakingSetSessionsPerEra = 1,
/// Set the minimum bonding duration for staking.
StakingSetBondingDuration = 2,
/// Set the validator count for staking.
StakingSetValidatorCount = 3,
/// Set the per-mille of validator approval required for governance changes.
GovernanceSetApprovalPpmRequired = 4,
/// Set the session length.
SessionSetLength = 5,
}
impl InternalFunction {
/// Derive `Some` value from a `u8`, or `None` if it's invalid.
pub fn from_u8(value: u8) -> Option<InternalFunction> {
use self::*;
let functions = [
InternalFunction::SystemSetCode,
InternalFunction::StakingSetSessionsPerEra,
InternalFunction::StakingSetBondingDuration,
InternalFunction::StakingSetValidatorCount,
InternalFunction::GovernanceSetApprovalPpmRequired,
InternalFunction::SessionSetLength
];
if (value as usize) < functions.len() {
Some(functions[value as usize])
} else {
None
}
}
}
/// An internal function.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Proposal {
/// The privileged function to call.
pub function: InternalFunction,
/// The serialised data to call it with.
#[serde(with = "bytes")]
pub input_data: Vec<u8>,
}
#[cfg(test)]
mod test {
use super::*;
use support::StaticHexInto;
#[test]
fn slicing_should_work() {
let p = Proposal {
function: InternalFunction::SystemSetCode,
input_data: b"Hello world".to_vec(),
};
let v = p.to_vec();
assert_eq!(v, "000b00000048656c6c6f20776f726c64".convert::<Vec<u8>>());
let o = Proposal::from_slice(&v).unwrap();
assert_eq!(p, o);
}
}
@@ -0,0 +1,54 @@
// 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/>.
//! Polkadot runtime functions.
//! This describes a function that can be called from an external transaction.
/// Public functions that can be dispatched to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum Function {
/// Staking subsystem: begin staking.
StakingStake = 0,
/// Staking subsystem: stop staking.
StakingUnstake = 1,
/// Staking subsystem: transfer stake.
StakingTransfer = 2,
/// Set temporary session key as a validator.
SessionSetKey = 3,
/// Set the timestamp.
TimestampSet = 4,
/// Make a proposal for the governance system.
GovernancePropose = 5,
/// Approve a proposal for the governance system.
GovernanceApprove = 6,
}
impl Function {
/// Derive `Some` value from a `u8`, or `None` if it's invalid.
pub fn from_u8(value: u8) -> Option<Function> {
match value {
0 => Some(Function::StakingStake),
1 => Some(Function::StakingUnstake),
2 => Some(Function::StakingTransfer),
3 => Some(Function::SessionSetKey),
4 => Some(Function::TimestampSet),
5 => Some(Function::GovernancePropose),
6 => Some(Function::GovernanceApprove),
_ => None,
}
}
}
+61
View File
@@ -0,0 +1,61 @@
// 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/>.
//! Transaction type.
use bytes::{self, Vec};
use runtime_function::Function;
#[cfg(feature = "std")]
use std::fmt;
#[cfg(not(feature = "std"))]
use alloc::fmt;
/// A vetted and verified transaction from the external world.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Transaction {
/// Who signed it (note this is not a signature).
pub signed: ::AccountId,
/// The number of transactions have come before from the same signer.
pub nonce: ::TxOrder,
/// The function that should be called.
pub function: Function,
/// Serialised input data to the function.
#[serde(with = "bytes")]
pub input_data: Vec<u8>,
}
/// A transactions right from the external world. Unchecked.
#[derive(Eq, Clone, Serialize, Deserialize)]
pub struct UncheckedTransaction {
/// The actual transaction information.
pub transaction: Transaction,
/// The signature; should be an Ed25519 signature applied to the serialised `transaction` field.
pub signature: ::Signature,
}
impl PartialEq for UncheckedTransaction {
fn eq(&self, other: &Self) -> bool {
self.signature.iter().eq(other.signature.iter()) && self.transaction == other.transaction
}
}
impl fmt::Debug for UncheckedTransaction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "UncheckedTransaction({:?})", self.transaction)
}
}
-20
View File
@@ -44,26 +44,6 @@ pub struct ValidationResult {
pub balance_uploads: Vec<BalanceUpload>,
}
// TODO [ToDr] This shouldn't be here!
/// Validator logic.
pub trait Validator {
/// Validation error.
type Error: ::std::error::Error;
/// Validates if the provided proof holds given a current ingress queue.
///
/// In case of success produces egress posts.
fn validate(
&self,
code: &[u8],
// TODO [ToDr] actually consolidate
consolidated_ingress: &[(u64, Vec<parachain::Message>)],
balance_downloads: &[BalanceDownload],
block_data: &parachain::BlockData,
previous_head_data: &parachain::HeadData,
) -> Result<ValidationResult, Self::Error>;
}
#[cfg(test)]
mod tests {
use super::*;