Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
36c171c
Introduce basic skeleton for Polkador runtime.
gavofyork Jan 8, 2018
f79b7bb
Clean up the runtime skeleton.
gavofyork Jan 8, 2018
b48e053
Make initial runtime skeleton compile.
gavofyork Jan 8, 2018
4e25b24
Compile polkadot-runtime both for Wasm ad native, allowing for testin…
gavofyork Jan 9, 2018
b28402c
More fleshing out on runtime.
gavofyork Jan 9, 2018
bac50a4
Update native support.
gavofyork Jan 9, 2018
9a4360f
Fix warning.
gavofyork Jan 9, 2018
0890e72
Update gitignore
gavofyork Jan 9, 2018
4cf5fd1
Update path.
gavofyork Jan 9, 2018
12b15fd
Fix path.
gavofyork Jan 9, 2018
47260c2
Remove accidentally committed files.
gavofyork Jan 9, 2018
800eb20
Add wasm binaries.
gavofyork Jan 9, 2018
10eaefe
Fix test.
gavofyork Jan 9, 2018
24ba809
Native storage support API.
gavofyork Jan 9, 2018
36a49ff
Add environmental module
gavofyork Jan 10, 2018
50e7222
Add native environment to make native source-code compatible with wasm.
gavofyork Jan 10, 2018
3e17caa
Finish up & polish environment stuff.
gavofyork Jan 10, 2018
e807c9a
Avoid using reentrancy issues.
gavofyork Jan 10, 2018
402ae29
Add some docs and a test.
gavofyork Jan 10, 2018
762826b
Remove unneeded function.
gavofyork Jan 10, 2018
7e969c8
Documentation
gavofyork Jan 11, 2018
64b0670
Tweak docs
gavofyork Jan 11, 2018
d34f62a
Remove TODOs.
gavofyork Jan 11, 2018
89f35e3
Balance transfers + util methods.
gavofyork Jan 14, 2018
5e42240
Rejig tests and ensure authorities are addressed consistently.
gavofyork Jan 14, 2018
d97e2d6
Add marshaller for xfer function
gavofyork Jan 14, 2018
26ccc14
Transaction dispatch test.
gavofyork Jan 15, 2018
7258e1f
Minor fix.
gavofyork Jan 15, 2018
c02e335
Add test for ser/de transaction.
gavofyork Jan 15, 2018
28fa761
Add ser/de for header.
gavofyork Jan 15, 2018
cd93a37
Add tests for header ser/de
gavofyork Jan 16, 2018
b55095a
Introduce basic block decoding/execution framework.
gavofyork Jan 16, 2018
288f653
Introduce block decoding/execution framework (p2)
gavofyork Jan 16, 2018
c64976b
Big refactor.
gavofyork Jan 16, 2018
40e28fc
Split out joiner.
gavofyork Jan 16, 2018
ab37f64
Hide away support modules.
gavofyork Jan 16, 2018
4844086
Fix up wasm runtime.
gavofyork Jan 16, 2018
28d775d
use externalities for chain_id
gavofyork Jan 16, 2018
75cedd9
Clean up (Test)Externalities.
gavofyork Jan 16, 2018
0feadc6
Repot and introduce keccak-256 external.
gavofyork Jan 17, 2018
15ae775
Signing with crypto.
gavofyork Jan 17, 2018
5650997
fix unsafety hole in environmental using function
rphmeier Jan 17, 2018
7820149
Introduce Ed25519 crypto.
gavofyork Jan 17, 2018
4ef4239
Repotting.
gavofyork Jan 17, 2018
8fa858c
Merge branch 'signing-ring' into polkadot-runtime-skeleton
gavofyork Jan 17, 2018
78a22d4
Add ed25519_verify external.
gavofyork Jan 17, 2018
93f7aed
Introduce Ed25519 verify as an external.
gavofyork Jan 17, 2018
cdc1387
fix unsafety hole around unwinding
rphmeier Jan 17, 2018
0477b7e
Compile fixes.
gavofyork Jan 17, 2018
66c9384
use new environmental API
rphmeier Jan 17, 2018
121aa0b
Merge branch 'polkadot-runtime-skeleton' into environmental-api
rphmeier Jan 17, 2018
eebc071
Tests for ed25519 verify.
gavofyork Jan 18, 2018
5685173
Polish
gavofyork Jan 18, 2018
f3d415c
Introduce UncheckedTransaction & test.
gavofyork Jan 18, 2018
2c9cb7b
Implement basic block and tx processing
gavofyork Jan 18, 2018
eda7d71
Introduce static hex and valid signature for block test.
gavofyork Jan 18, 2018
fd9d8dc
Merge pull request #35 from paritytech/environmental-api
gavofyork Jan 18, 2018
e8cd918
Repot session.
gavofyork Jan 18, 2018
3d1ff94
comments.
gavofyork Jan 18, 2018
92a12f0
Refactor and timestamp test
gavofyork Jan 19, 2018
5523277
Remove fluff
gavofyork Jan 19, 2018
39128b7
Merge branch 'polkadot-runtime-skeleton' of github.com:paritytech/pol…
gavofyork Jan 19, 2018
9a8f61d
Remove fluff.
gavofyork Jan 19, 2018
90f965a
Staking eras and tests.
gavofyork Jan 19, 2018
58670fc
Implement sessions.
gavofyork Jan 19, 2018
999025f
Polish
gavofyork Jan 19, 2018
4fde6f0
Test sessions.
gavofyork Jan 19, 2018
9c8cafd
Introduce better hashing.
gavofyork Jan 19, 2018
ae5a9c9
Fix tests.
gavofyork Jan 19, 2018
639554c
Introduce staking.
gavofyork Jan 19, 2018
0e88dad
Tests for simple staking system.
gavofyork Jan 19, 2018
80aa38c
Build fix for wasm.
gavofyork Jan 19, 2018
ef5d78a
Fix tests.
gavofyork Jan 19, 2018
e068f44
Repotting and docs.
gavofyork Jan 19, 2018
2499910
Docs and licence.
gavofyork Jan 19, 2018
0c53f71
Documentation.
gavofyork Jan 19, 2018
850f7ee
Remove superfluous code.
gavofyork Jan 19, 2018
dde319c
Remove dummy key.
gavofyork Jan 19, 2018
17ef2d0
Remove other superfluous file.
gavofyork Jan 19, 2018
8adb7c6
Optimise with swap_remove
gavofyork Jan 20, 2018
cbd65cb
Merge remote-tracking branch 'origin/master' into polkadot-runtime-sk…
gavofyork Jan 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make initial runtime skeleton compile.
  • Loading branch information
gavofyork committed Jan 8, 2018
commit b48e05330b0ef2d6fe2713ba3ef98d62ad350b13
173 changes: 98 additions & 75 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,54 @@ use alloc::vec::Vec;

#[macro_use]
extern crate runtime_support;
use runtime_support::{set_storage, storage, storage_into, print, Value20};
use runtime_support::{set_storage, storage, storage_into};

/*
use std::sync::{rc, RefCell, Once, ONCE_INIT};
use std::mem;

#[derive(Clone)]
struct SingletonReader {
inner: Rc<RefCell<Environment>>,
/// The hash of an ECDSA pub key which is used to identify an external transactor.
pub type AccountID = [u8; 32];
/// The ECDSA pub key of an authority. This is what the external environment/consensus algorithm
/// refers to as a "authority".
pub type SessionKey = AccountID;
pub type Balance = u64;
pub type ChainID = u64;
pub type Hash = [u8; 32];
pub type BlockNumber = u64;
pub type Timestamp = u64;
pub type TxOrder = u64;

/// The functions that a transaction can call (and be dispatched to).
pub enum Function {
StakingStake(),
StakingUnstake(),
ConsensusSetSessionKey(SessionKey),
}

fn singleton() -> SingletonReader {
// Initialize it to a null value
static mut SINGLETON: *const SingletonReader = 0 as *const SingletonReader;
static ONCE: Once = ONCE_INIT;

unsafe {
ONCE.call_once(|| {
// Make it
let singleton = SingletonReader {
inner: Rc::new(RefCell::new(Default::default())),
};

// Put it in the heap so it can outlive this call
SINGLETON = mem::transmute(Box::new(singleton));
});

// Now we give out a copy of the data that is safe to use concurrently.
(*SINGLETON).clone()
}
impl Function {
/// Dispatch the function.
pub fn dispatch(self) -> Vec<u8> { unimplemented!() }
}
*/

/// The hash of an ECDSA pub key which is used to identify an external transactor.
type AccountID = [u8; 32];
/// The ECDSA pub key of an authority. This is what the external environment/consensus algorithm
/// refers to as a "authority".
type SessionKey = [u8; 65];
type Balance = u64;
type ChainID = u64;
type Hash = [u8; 32];
type BlockNumber = u64;
/// A proportion (rational number).
struct Proportion { nom: u64, denom: u64, };
type Timestamp = u64;
type TxOrder = u64;

struct Digest {
logs: Vec<Vec<u8>>,
pub struct Digest {
pub logs: Vec<Vec<u8>>,
}

struct Header {
parent_hash: Hash,
number: BlockNumber,
state_root: Hash,
transaction_root: Hash,
digest: Digest,
pub struct Header {
pub parent_hash: Hash,
pub number: BlockNumber,
pub state_root: Hash,
pub transaction_root: Hash,
pub digest: Digest,
}

struct Transaction {
senders: Vec<AccountID>,
function_name: String,
input_data: Vec<u8>,
nonce: TxOrder,
pub struct Transaction {
pub senders: Vec<AccountID>,
pub function: Function,
pub input_data: Vec<u8>,
pub nonce: TxOrder,
}

struct Block {
header: Header,
transactions: Vec<Transaction>,
pub struct Block {
pub header: Header,
pub transactions: Vec<Transaction>,
}

impl Header {
Expand All @@ -97,28 +76,67 @@ impl Block {
}
}

/*
use std::sync::{rc, RefCell, Once, ONCE_INIT};
use std::mem;

#[derive(Default)]
struct Environment {
header: Option<Header>,
current_user: Option<AccountID>,
}

#[derive(Clone)]
struct EnvironmentHolder {
inner: Rc<RefCell<Environment>>,
}

fn get_environment() -> EnvironmentHolder {
// Initialize it to a null value
static mut SINGLETON: *const EnvironmentHolder = 0 as *const EnvironmentHolder;
static ONCE: Once = ONCE_INIT;

unsafe {
ONCE.call_once(|| {
// Make it
let singleton = EnvironmentHolder {
inner: Rc::new(RefCell::new(Default::default())),
};

// Put it in the heap so it can outlive this call
SINGLETON = mem::transmute(Box::new(singleton));
});

// Now we give out a copy of the data that is safe to use concurrently.
(*SINGLETON).clone()
}
}
*/

// TODO: include RLP implementation
// TODO: add keccak256 (or some better hashing scheme) & ECDSA-recover (or some better sig scheme)

impl_stub!(execute_block);
fn execute_block(_input: Vec<u8>) -> Vec<u8> {
let block = Block::from_rlp(&_input);
environment::execute_block(&block)
}

impl_stub!(execute_transaction);
fn execute_transaction(_input: Vec<u8>) -> Vec<u8> {
let tx = Transaction::from_rlp(&_input);
environment::execute_transaction(&tx)
}

impl_stubs!(execute_block, execute_transaction);

/// The current relay chain identifier.
fn chain_id() -> ChainID {
// TODO: retrieve from external
unimplemented!()
}

mod environment {
use super::*;

/// The current block number being processed. Set by `execute_block`.
pub fn block_number() -> BlockNumber { unimplemented!() }

Expand Down Expand Up @@ -163,6 +181,8 @@ mod environment {
}

mod consensus {
use super::*;

fn value_vec(mut value: usize, initial: Vec<u8>) -> Vec<u8> {
let mut acc = initial;
while value > 0 {
Expand All @@ -177,11 +197,11 @@ mod consensus {
}

fn authority(index: usize) -> AccountID {
storage_into::<Value20>(&value_vec(index, b"\0authority".to_vec()))
storage_into(&value_vec(index, b"\0authority".to_vec())).unwrap()
}

fn set_authority_count(count: usize) {
(count..authority_count()).for_each(|i| set_authority(i, &[]));
(count..authority_count()).for_each(|i| set_authority(i, SessionKey::default()));
set_storage(b"\0authority_count", &value_vec(count, Vec::new()));
}

Expand All @@ -191,15 +211,15 @@ mod consensus {

/// Get the current set of authorities. These are the session keys.
pub fn authorities() -> Vec<AccountID> {
(0..authority_count()).into_iter().map(authority).map.collect()
(0..authority_count()).into_iter().map(authority).collect()
}

/// Set the current set of authorities' session keys.
///
/// Called by `next_session` only.
fn set_authorities(authorities: &[AccountID]) {
set_authority_count(authorities.len());
authorities.iter().enumerate().for_each(|(v, i)| set_authority(v, i));
authorities.iter().enumerate().for_each(|(v, &i)| set_authority(v, i));
}

/// Get the current set of validators. These are the long-term identifiers for the validators
Expand All @@ -215,9 +235,6 @@ mod consensus {
unimplemented!()
}

/// Flush out any statistics.
pub fn flush_statistics() -> Statistics { unimplemented!() }

/// Sets the session key of `_validator` to `_session`. This doesn't take effect until the next
/// session.
pub fn set_session_key(_validator: AccountID, _session: AccountID) {
Expand All @@ -240,6 +257,8 @@ mod consensus {
}

mod staking {
use super::*;

/// The length of a staking era in blocks.
fn era_length() -> BlockNumber { unimplemented!() }

Expand All @@ -252,12 +271,12 @@ mod staking {
fn balance(_who: AccountID) -> Balance { unimplemented!() }

/// Transfer some unlocked staking balance to another staker.
fn transfer_stake(_who: AccountID, dest: AccountID, value: Balance) { unimplemented!() }
fn transfer_stake(_who: AccountID, _dest: AccountID, _value: Balance) { unimplemented!() }

/// Declare the desire to stake.
///
/// Effects will be felt at the beginning of the next era.
fn stake(minimum_era_return: Proportion) { unimplemented!() }
fn stake() { unimplemented!() }

/// Retract the desire to stake.
///
Expand All @@ -266,23 +285,27 @@ mod staking {

/// Hook to be called prior to transaction processing.
pub fn pre_transactions() {
conensus::pre_transactions();
consensus::pre_transactions();
}

/// Hook to be called after to transaction processing.
pub fn post_transactions() {
// TODO: check block number and call next_era if necessary.
conensus::post_transactions();
consensus::post_transactions();
}
}

mod authentication {
fn validate_signature(self, tx: Transaction) -> ( AccountID, TxOrder );
fn nonce(self, id: AccountID) -> TxOrder;
fn authenticate(mut self, tx: Transaction) -> AccountID;
use super::*;

fn validate_signature(_tx: Transaction) -> ( AccountID, TxOrder ) { unimplemented!() }
fn nonce(_id: AccountID) -> TxOrder { unimplemented!() }
fn authenticate(_tx: Transaction) -> AccountID { unimplemented!() }
}

mod timestamp {
use super::*;

fn timestamp() -> Timestamp { unimplemented!() }
fn set_timestamp(Timestamp) { unimplemented!() }
fn set_timestamp(_now: Timestamp) { unimplemented!() }
}
52 changes: 25 additions & 27 deletions runtime/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#![cfg_attr(feature = "strict", deny(warnings))]

#![feature(alloc)]

extern crate alloc;
use alloc::vec::Vec;
use core::mem;

extern crate pwasm_libc;
extern crate pwasm_alloc;
Expand Down Expand Up @@ -33,22 +33,18 @@ pub fn storage(key: &[u8]) -> Vec<u8> {
}
}

pub trait IsValue {
const value: usize;
}

pub struct Value20; impl IsValue for Value20 { const value = 20usize; }
pub struct Value32; impl IsValue for Value32 { const value = 32usize; }
pub struct Value64; impl IsValue for Value64 { const value = 64usize; }
pub struct Value65; impl IsValue for Value65 { const value = 65usize; }

pub fn storage_into<T: IsValue>(key: &[u8]) -> Option<[u8; T::value]> {
let mut result = [0u8; T::value];
let written = unsafe {
ext_get_storage_into(&key[0], key.len() as u32, &result[0], result.len())
};
pub fn storage_into<T: Sized>(key: &[u8]) -> Option<T> {
let mut result: T;
let size = mem::size_of::<T>();
let mut written;
unsafe {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rphmeier this is pretty nasty (though i'm pretty sure it's safe). maybe you know of a better way of accomplishing the same thing?

result = mem::uninitialized();
let result_as_byte_blob = mem::transmute::<*mut T, *mut u8>(&mut result);
written = ext_get_storage_into(&key[0], key.len() as u32, result_as_byte_blob, size as u32) as usize;
}
// Only return a fully written value.
match written {
T::value => Some(result),
size => Some(result),
_ => None,
}
}
Expand Down Expand Up @@ -93,18 +89,20 @@ pub fn print<T: Printable + Sized>(value: T) {
}

#[macro_export]
macro_rules! impl_stub {
($name:ident) => {
macro_rules! impl_stubs {
( $( $name:ident ),* ) => {
pub mod _internal {
#[no_mangle]
pub fn $name(input_data: *mut u8, input_len: usize) -> u64 {
let input = unsafe {
super::alloc::vec::Vec::from_raw_parts(input_data, input_len, input_len)
};

let output = super::$name(input);
&output[0] as *const u8 as u64 + ((output.len() as u64) << 32)
}
$(
#[no_mangle]
pub fn $name(input_data: *mut u8, input_len: usize) -> u64 {
let input = unsafe {
super::alloc::vec::Vec::from_raw_parts(input_data, input_len, input_len)
};

let output = super::$name(input);
&output[0] as *const u8 as u64 + ((output.len() as u64) << 32)
}
)*
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified runtime/target/wasm32-unknown-unknown/release/runtime_test.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion runtime/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn set_authorities(authorities: &[&[u8]]) {
authorities.iter().enumerate().for_each(|(v, i)| set_authority(v, i));
}

impl_stub!(test_data_in);
impl_stubs!(test_data_in);
fn test_data_in(input: Vec<u8>) -> Vec<u8> {
print(b"set_storage" as &[u8]);
set_storage(b"input", &input);
Expand Down