State machine crate and rust impl of executor (#17)

* Executor.

* Rust contracts implementation.

* Add documentation and fix transfer data.

* Fix auth contract interface.

* Add some executor tests.

* Validator set contract.
This commit is contained in:
Tomasz Drwięga
2017-11-12 16:53:11 +01:00
committed by Robert Habermeier
parent e9177294f6
commit 2fa0239dab
13 changed files with 507 additions and 4 deletions
+38
View File
@@ -0,0 +1,38 @@
// 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/>.
use primitives::Address;
use state_machine::Externalities;
use error::Result;
use executor::RustExecutor;
/// Data and some sort of Authentication Data
type DataAndAuth = (Vec<u8>, Vec<u8>);
/// Authentication contract rust implementation.
#[derive(Debug, Default)]
pub struct Contract;
impl Contract {
/// Verify authentication data.
///
/// Given Message and Authentication Data verifies it and returns:
/// 1. None in case it doesn't match (i.e. signature is invalid)
/// 2. A address who signed that Message.
pub fn check_auth<E: Externalities<RustExecutor>>(&self, _ext: &E, _data: DataAndAuth) -> Result<Option<Address>> {
unimplemented!()
}
}
+64
View File
@@ -0,0 +1,64 @@
// 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/>.
use primitives::Address;
use primitives::uint::U256;
use state_machine::Externalities;
use error::Result;
use executor::RustExecutor;
#[derive(Debug, Serialize, Deserialize)]
pub struct Transfer {
/// Transfer value
value: U256,
/// Transfer destination
to: Address,
/// Replay protection
nonce: U256,
/// Data authorizing the transfer (we can derive sender from it)
authentication_data: Vec<u8>,
}
/// Balances contract rust implementation.
#[derive(Debug, Default)]
pub struct Contract;
impl Contract {
/// Returns a balance of given address.
pub fn balance_of<E: Externalities<RustExecutor>>(&self, _ext: &E, _data: Address) -> Result<U256> {
unimplemented!()
}
/// Returns the next nonce to authorize the transfer from given address.
pub fn next_nonce<E: Externalities<RustExecutor>>(&self, _ext: &E, _data: Address) -> Result<U256> {
unimplemented!()
}
/// Checks preconditions for transfer.
/// Should verify:
/// - signature
/// - replay protection
/// - enough balance
pub fn transfer_preconditions<E: Externalities<RustExecutor>>(&self, _db: &E, _data: Transfer) -> Result<bool> {
unimplemented!()
}
/// Perform a transfer.
/// This should first make sure that precondtions are satisfied and later perform the transfer.
pub fn transfer<E: Externalities<RustExecutor>>(&self, _ext: &mut E, _data: Transfer) -> Result<bool> {
unimplemented!()
}
}
+50
View File
@@ -0,0 +1,50 @@
// 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/>.
//! Rust executor possible errors.
#![allow(missing_docs)]
use serializer;
use state_machine;
error_chain! {
foreign_links {
InvalidData(serializer::Error);
}
errors {
/// Method is not found
MethodNotFound(t: String) {
description("method not found"),
display("Method not found: '{}'", t),
}
/// Code is invalid (expected single byte)
InvalidCode(c: Vec<u8>) {
description("invalid code"),
display("Invalid Code: {:?}", c),
}
/// Externalities have failed.
Externalities(e: Box<state_machine::Error>) {
description("externalities failure"),
display("Externalities error: {}", e),
}
}
}
impl state_machine::Error for Error {}
+136
View File
@@ -0,0 +1,136 @@
// 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/>.
//! Rust implementation of Polkadot contracts.
use primitives::contract::{CallData, OutData};
use serializer::{from_slice as de, to_vec as ser};
use state_machine::{Externalities, Executor};
use error::{Error, ErrorKind, Result};
use auth;
use balances;
use validator_set;
/// Dummy rust executor for contracts.
///
/// Instead of actually executing the provided code it just
/// dispatches the calls to pre-defined hardcoded implementations in rust.
#[derive(Debug, Default)]
pub struct RustExecutor {
auth: auth::Contract,
balance: balances::Contract,
validator_set: validator_set::Contract,
}
impl RustExecutor {
const AUTH: u8 = 1;
const BALANCES: u8 = 2;
const VALIDATOR_SET: u8 = 3;
}
impl Executor for RustExecutor {
type Error = Error;
fn static_call<E: Externalities<Self>>(
&self,
ext: &E,
code: &[u8],
method: &str,
data: &CallData,
) -> Result<OutData> {
ensure!(code.len() == 1, ErrorKind::InvalidCode(code.to_vec()));
Ok(OutData(match code[0] {
Self::AUTH => match method {
"check_auth" => ser(&self.auth.check_auth(ext, de(&data.0)?)?),
m => bail!(ErrorKind::MethodNotFound(m.to_owned())),
},
Self::BALANCES => match method {
"balance_of" => ser(&self.balance.balance_of(ext, de(&data.0)?)?),
"next_nonce" => ser(&self.balance.next_nonce(ext, de(&data.0)?)?),
"transfer_preconditions" => ser(&self.balance.transfer_preconditions(ext, de(&data.0)?)?),
m => bail!(ErrorKind::MethodNotFound(m.to_owned())),
},
Self::VALIDATOR_SET => match method {
"validator_set" => ser(&self.validator_set.validator_set(ext, de(&data.0)?)?),
m => bail!(ErrorKind::MethodNotFound(m.to_owned())),
},
c => bail!(ErrorKind::InvalidCode(vec![c])),
}))
}
fn call<E: Externalities<Self>>(
&self,
ext: &mut E,
code: &[u8],
method: &str,
data: &CallData,
) -> Result<OutData> {
ensure!(code.len() == 1, ErrorKind::InvalidCode(code.to_vec()));
Ok(OutData(match code[0] {
Self::BALANCES=> match method {
"transfer" => ser(&self.balance.transfer(ext, de(&data.0)?)?),
m => bail!(ErrorKind::MethodNotFound(m.to_owned())),
},
c => bail!(ErrorKind::InvalidCode(vec![c])),
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, Default)]
struct TestExternalities;
impl Externalities<RustExecutor> for TestExternalities {
type Error = Error;
}
#[test]
fn should_fail_for_empty_or_unknown_code() {
// given
let mut ext = TestExternalities::default();
let executor = RustExecutor::default();
assert_matches!(
*executor.call(&mut ext, &[], "any", &CallData(vec![])).unwrap_err().kind(),
ErrorKind::InvalidCode(ref code) if code.is_empty()
);
assert_matches!(
*executor.call(&mut ext, &[1, 2], "any", &CallData(vec![])).unwrap_err().kind(),
ErrorKind::InvalidCode(ref code) if code.len() == 2
);
assert_matches!(
*executor.call(&mut ext, &[255,], "any", &CallData(vec![])).unwrap_err().kind(),
ErrorKind::InvalidCode(_)
);
}
#[test]
fn should_fail_on_invalid_method() {
// given
let mut ext = TestExternalities::default();
let executor = RustExecutor::default();
assert_matches!(
*executor.call(&mut ext, &[2], "any", &CallData(vec![])).unwrap_err().kind(),
ErrorKind::MethodNotFound(ref method) if &*method == "any"
);
}
}
+47
View File
@@ -0,0 +1,47 @@
// 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/>.
//! Temporary crate for contracts implementations.
//!
//! This will be replaced with WASM contracts stored on-chain.
#![warn(missing_docs)]
extern crate polkadot_primitives as primitives;
extern crate polkadot_serializer as serializer;
extern crate polkadot_state_machine as state_machine;
extern crate serde;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate serde_derive;
#[cfg(test)]
#[macro_use]
extern crate assert_matches;
mod auth;
mod balances;
mod validator_set;
pub mod error;
pub mod executor;
/// Creates new RustExecutor for contracts.
pub fn new() -> executor::RustExecutor {
executor::RustExecutor::default()
}
+31
View File
@@ -0,0 +1,31 @@
// 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/>.
use primitives::Address;
use state_machine::Externalities;
use error::Result;
use executor::RustExecutor;
/// Harcoded validator set contract.
#[derive(Debug, Default)]
pub struct Contract;
impl Contract {
/// Returns current validator set.
pub fn validator_set<E: Externalities<RustExecutor>>(&self, _db: &E, _data: ()) -> Result<Vec<Address>> {
unimplemented!()
}
}