mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 22:51:13 +00:00
Fix warning and directory restructure.
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "substrate-client"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
error-chain = "0.11"
|
||||
log = "0.3"
|
||||
parking_lot = "0.4"
|
||||
substrate-primitives = { path = "../primitives", version = "0.1" }
|
||||
substrate-state-machine = { path = "../state-machine", version = "0.1" }
|
||||
substrate-serializer = { path = "../serializer" }
|
||||
substrate-executor = { path = "../executor" }
|
||||
substrate-codec = { path = "../codec", version = "0.1" }
|
||||
triehash = "0.1"
|
||||
hex-literal = "0.1"
|
||||
ed25519 = { path = "../ed25519", version = "0.1" }
|
||||
@@ -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 Client data backend
|
||||
|
||||
use state_machine;
|
||||
use error;
|
||||
use primitives::block;
|
||||
use blockchain::{self, BlockId};
|
||||
|
||||
/// Block insertion transction. Keeps hold if the inseted block state and data.
|
||||
pub trait BlockImportOperation {
|
||||
/// Associated state backend type.
|
||||
type State: state_machine::backend::Backend;
|
||||
|
||||
/// Returns pending state.
|
||||
fn state(&self) -> error::Result<Self::State>;
|
||||
/// Append block data to the transaction.
|
||||
fn import_block(&mut self, header: block::Header, body: Option<block::Body>, is_new_best: bool) -> error::Result<()>;
|
||||
/// Inject storage data into the database.
|
||||
fn reset_storage<I: Iterator<Item=(Vec<u8>, Vec<u8>)>>(&mut self, iter: I) -> error::Result<()>;
|
||||
}
|
||||
|
||||
/// Client backend. Manages the data layer.
|
||||
pub trait Backend {
|
||||
/// Associated block insertion transaction type.
|
||||
type BlockImportOperation: BlockImportOperation;
|
||||
/// Associated blockchain backend type.
|
||||
type Blockchain: blockchain::Backend;
|
||||
/// Associated state backend type.
|
||||
type State: state_machine::backend::Backend;
|
||||
|
||||
/// Begin a new block insertion transaction with given parent block id.
|
||||
fn begin_transaction(&self, block: BlockId) -> error::Result<Self::BlockImportOperation>;
|
||||
/// Commit block insertion.
|
||||
fn commit_transaction(&self, transaction: Self::BlockImportOperation) -> error::Result<()>;
|
||||
/// Returns reference to blockchain backend.
|
||||
fn blockchain(&self) -> &Self::Blockchain;
|
||||
/// Returns state backend for specified block.
|
||||
fn state_at(&self, block: BlockId) -> error::Result<Self::State>;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
// 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 blockchain trait
|
||||
|
||||
use std::fmt::{Display, Formatter, Error as FmtError};
|
||||
use primitives::block;
|
||||
use error::Result;
|
||||
|
||||
/// Block indentification.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum BlockId {
|
||||
/// Identify by block header hash.
|
||||
Hash(block::HeaderHash),
|
||||
/// Identify by block number.
|
||||
Number(block::Number),
|
||||
}
|
||||
|
||||
impl Display for BlockId {
|
||||
fn fmt(&self, f: &mut Formatter) -> ::std::result::Result<(), FmtError> {
|
||||
match *self {
|
||||
BlockId::Hash(h) => h.fmt(f),
|
||||
BlockId::Number(n) => n.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Blockchain database backend. Does not perform any validation.
|
||||
pub trait Backend: Send + Sync {
|
||||
/// Get block header. Returns `None` if block is not found.
|
||||
fn header(&self, id: BlockId) -> Result<Option<block::Header>>;
|
||||
/// Get block body. Returns `None` if block is not found.
|
||||
fn body(&self, id: BlockId) -> Result<Option<block::Body>>;
|
||||
/// Get blockchain info.
|
||||
fn info(&self) -> Result<Info>;
|
||||
/// Get block status.
|
||||
fn status(&self, id: BlockId) -> Result<BlockStatus>;
|
||||
/// Get block hash by number. Returns `None` if the header is not in the chain.
|
||||
fn hash(&self, number: block::Number) -> Result<Option<block::HeaderHash>>;
|
||||
}
|
||||
|
||||
/// Block import outcome
|
||||
pub enum ImportResult<E> {
|
||||
/// Imported successfully.
|
||||
Imported,
|
||||
/// Block already exists, skippped.
|
||||
AlreadyInChain,
|
||||
/// Unknown parent.
|
||||
UnknownParent,
|
||||
/// Other errror.
|
||||
Err(E),
|
||||
}
|
||||
|
||||
/// Blockchain info
|
||||
#[derive(Debug)]
|
||||
pub struct Info {
|
||||
/// Best block hash.
|
||||
pub best_hash: block::HeaderHash,
|
||||
/// Best block number.
|
||||
pub best_number: block::Number,
|
||||
/// Genesis block hash.
|
||||
pub genesis_hash: block::HeaderHash,
|
||||
}
|
||||
|
||||
/// Block status.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum BlockStatus {
|
||||
/// Already in the blockchain.
|
||||
InChain,
|
||||
/// Not in the queue or the blockchain.
|
||||
Unknown,
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// 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 client possible errors.
|
||||
|
||||
use std;
|
||||
use state_machine;
|
||||
use blockchain;
|
||||
|
||||
error_chain! {
|
||||
errors {
|
||||
/// Backend error.
|
||||
Backend {
|
||||
description("Unrecoverable backend error"),
|
||||
display("Backend error"),
|
||||
}
|
||||
|
||||
/// Unknown block.
|
||||
UnknownBlock(h: blockchain::BlockId) {
|
||||
description("unknown block"),
|
||||
display("UnknownBlock: {}", h),
|
||||
}
|
||||
|
||||
/// Execution error.
|
||||
Execution(e: Box<state_machine::Error>) {
|
||||
description("execution error"),
|
||||
display("Execution: {}", e),
|
||||
}
|
||||
|
||||
/// Blockchain error.
|
||||
Blockchain(e: Box<std::error::Error + Send>) {
|
||||
description("Blockchain error"),
|
||||
display("Blockchain: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO [ToDr] Temporary, state_machine::Error should be a regular error not Box.
|
||||
impl From<Box<state_machine::Error>> for Error {
|
||||
fn from(e: Box<state_machine::Error>) -> Self {
|
||||
ErrorKind::Execution(e).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<state_machine::backend::Void> for Error {
|
||||
fn from(_e: state_machine::backend::Void) -> Self {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Chain a blockchain error.
|
||||
pub fn from_blockchain(e: Box<std::error::Error + Send>) -> Self {
|
||||
ErrorKind::Blockchain(e).into()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
// 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/>.
|
||||
|
||||
//! In memory client backend
|
||||
|
||||
use std::collections::HashMap;
|
||||
use parking_lot::RwLock;
|
||||
use state_machine;
|
||||
use error;
|
||||
use backend;
|
||||
use primitives;
|
||||
use ser;
|
||||
use primitives::block::{self, HeaderHash};
|
||||
use blockchain::{self, BlockId, BlockStatus};
|
||||
|
||||
fn header_hash(header: &block::Header) -> block::HeaderHash {
|
||||
primitives::hashing::blake2_256(&ser::to_vec(header)).into()
|
||||
}
|
||||
|
||||
struct PendingBlock {
|
||||
block: Block,
|
||||
is_best: bool,
|
||||
}
|
||||
|
||||
struct Block {
|
||||
header: block::Header,
|
||||
body: Option<block::Body>,
|
||||
}
|
||||
|
||||
/// In-memory transaction.
|
||||
pub struct BlockImportOperation {
|
||||
pending_block: Option<PendingBlock>,
|
||||
pending_state: state_machine::backend::InMemory,
|
||||
}
|
||||
|
||||
struct BlockchainStorage {
|
||||
blocks: HashMap<HeaderHash, Block>,
|
||||
hashes: HashMap<block::Number, HeaderHash>,
|
||||
best_hash: HeaderHash,
|
||||
best_number: block::Number,
|
||||
genesis_hash: HeaderHash,
|
||||
}
|
||||
|
||||
/// In-memory blockchain. Supports concurrent reads.
|
||||
pub struct Blockchain {
|
||||
storage: RwLock<BlockchainStorage>,
|
||||
}
|
||||
|
||||
impl Blockchain {
|
||||
fn id(&self, id: BlockId) -> Option<HeaderHash> {
|
||||
match id {
|
||||
BlockId::Hash(h) => Some(h),
|
||||
BlockId::Number(n) => self.storage.read().hashes.get(&n).cloned(),
|
||||
}
|
||||
}
|
||||
|
||||
fn new() -> Blockchain {
|
||||
Blockchain {
|
||||
storage: RwLock::new(
|
||||
BlockchainStorage {
|
||||
blocks: HashMap::new(),
|
||||
hashes: HashMap::new(),
|
||||
best_hash: HeaderHash::default(),
|
||||
best_number: 0,
|
||||
genesis_hash: HeaderHash::default(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&self, hash: HeaderHash, header: block::Header, body: Option<block::Body>, is_new_best: bool) {
|
||||
let number = header.number;
|
||||
let mut storage = self.storage.write();
|
||||
storage.blocks.insert(hash, Block {
|
||||
header: header,
|
||||
body: body,
|
||||
});
|
||||
storage.hashes.insert(number, hash);
|
||||
if is_new_best {
|
||||
storage.best_hash = hash;
|
||||
storage.best_number = number;
|
||||
}
|
||||
if number == 0 {
|
||||
storage.genesis_hash = hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl blockchain::Backend for Blockchain {
|
||||
fn header(&self, id: BlockId) -> error::Result<Option<block::Header>> {
|
||||
Ok(self.id(id).and_then(|hash| self.storage.read().blocks.get(&hash).map(|b| b.header.clone())))
|
||||
}
|
||||
|
||||
fn body(&self, id: BlockId) -> error::Result<Option<block::Body>> {
|
||||
Ok(self.id(id).and_then(|hash| self.storage.read().blocks.get(&hash).and_then(|b| b.body.clone())))
|
||||
}
|
||||
|
||||
fn info(&self) -> error::Result<blockchain::Info> {
|
||||
let storage = self.storage.read();
|
||||
Ok(blockchain::Info {
|
||||
best_hash: storage.best_hash,
|
||||
best_number: storage.best_number,
|
||||
genesis_hash: storage.genesis_hash,
|
||||
})
|
||||
}
|
||||
|
||||
fn status(&self, id: BlockId) -> error::Result<BlockStatus> {
|
||||
match self.id(id).map_or(false, |hash| self.storage.read().blocks.contains_key(&hash)) {
|
||||
true => Ok(BlockStatus::InChain),
|
||||
false => Ok(BlockStatus::Unknown),
|
||||
}
|
||||
}
|
||||
|
||||
fn hash(&self, number: block::Number) -> error::Result<Option<block::HeaderHash>> {
|
||||
Ok(self.id(BlockId::Number(number)))
|
||||
}
|
||||
}
|
||||
|
||||
impl backend::BlockImportOperation for BlockImportOperation {
|
||||
type State = state_machine::backend::InMemory;
|
||||
|
||||
fn state(&self) -> error::Result<Self::State> {
|
||||
Ok(self.pending_state.clone())
|
||||
}
|
||||
|
||||
fn import_block(&mut self, header: block::Header, body: Option<block::Body>, is_new_best: bool) -> error::Result<()> {
|
||||
assert!(self.pending_block.is_none(), "Only one block per transaction is allowed");
|
||||
self.pending_block = Some(PendingBlock {
|
||||
block: Block {
|
||||
header: header,
|
||||
body: body,
|
||||
},
|
||||
is_best: is_new_best,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_storage<I: Iterator<Item=(Vec<u8>, Vec<u8>)>>(&mut self, iter: I) -> error::Result<()> {
|
||||
self.pending_state = state_machine::backend::InMemory::from(iter.collect());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// In-memory backend. Keeps all states and blocks in memory. Useful for testing.
|
||||
pub struct Backend {
|
||||
states: RwLock<HashMap<block::HeaderHash, state_machine::backend::InMemory>>,
|
||||
blockchain: Blockchain,
|
||||
}
|
||||
|
||||
impl Backend {
|
||||
/// Create a new instance of in-mem backend.
|
||||
pub fn new() -> Backend {
|
||||
Backend {
|
||||
states: RwLock::new(HashMap::new()),
|
||||
blockchain: Blockchain::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl backend::Backend for Backend {
|
||||
type BlockImportOperation = BlockImportOperation;
|
||||
type Blockchain = Blockchain;
|
||||
type State = state_machine::backend::InMemory;
|
||||
|
||||
fn begin_transaction(&self, block: BlockId) -> error::Result<Self::BlockImportOperation> {
|
||||
let state = match block {
|
||||
BlockId::Hash(h) if h.is_zero() => Self::State::default(),
|
||||
_ => self.state_at(block)?,
|
||||
};
|
||||
|
||||
Ok(BlockImportOperation {
|
||||
pending_block: None,
|
||||
pending_state: state,
|
||||
})
|
||||
}
|
||||
|
||||
fn commit_transaction(&self, transaction: Self::BlockImportOperation) -> error::Result<()> {
|
||||
if let Some(pending_block) = transaction.pending_block {
|
||||
let hash = header_hash(&pending_block.block.header);
|
||||
self.states.write().insert(hash, transaction.pending_state);
|
||||
self.blockchain.insert(hash, pending_block.block.header, pending_block.block.body, pending_block.is_best);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn blockchain(&self) -> &Blockchain {
|
||||
&self.blockchain
|
||||
}
|
||||
|
||||
fn state_at(&self, block: BlockId) -> error::Result<Self::State> {
|
||||
match self.blockchain.id(block).and_then(|id| self.states.read().get(&id).cloned()) {
|
||||
Some(state) => Ok(state),
|
||||
None => Err(error::ErrorKind::UnknownBlock(block).into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
// 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 Client
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
extern crate substrate_primitives as primitives;
|
||||
extern crate substrate_state_machine as state_machine;
|
||||
extern crate substrate_serializer as ser;
|
||||
extern crate substrate_codec as codec;
|
||||
extern crate substrate_executor;
|
||||
extern crate ed25519;
|
||||
|
||||
extern crate triehash;
|
||||
extern crate parking_lot;
|
||||
#[macro_use] extern crate error_chain;
|
||||
#[macro_use] extern crate log;
|
||||
|
||||
pub mod error;
|
||||
pub mod blockchain;
|
||||
pub mod backend;
|
||||
pub mod in_mem;
|
||||
|
||||
pub use blockchain::Info as ChainInfo;
|
||||
pub use blockchain::BlockId;
|
||||
|
||||
use primitives::block;
|
||||
use primitives::storage::{StorageKey, StorageData};
|
||||
|
||||
use blockchain::Backend as BlockchainBackend;
|
||||
use backend::BlockImportOperation;
|
||||
use state_machine::backend::Backend as StateBackend;
|
||||
|
||||
/// Polkadot Client
|
||||
#[derive(Debug)]
|
||||
pub struct Client<B, E> where B: backend::Backend {
|
||||
backend: B,
|
||||
executor: E,
|
||||
}
|
||||
|
||||
/// Client info
|
||||
#[derive(Debug)]
|
||||
pub struct ClientInfo {
|
||||
/// Best block hash.
|
||||
pub chain: ChainInfo,
|
||||
/// Best block number in the queue.
|
||||
pub best_queued_number: Option<block::Number>,
|
||||
/// Best queued block hash.
|
||||
pub best_queued_hash: Option<block::HeaderHash>,
|
||||
}
|
||||
|
||||
/// Information regarding the result of a call.
|
||||
pub struct CallResult {
|
||||
/// The data that was returned from the call.
|
||||
pub return_data: Vec<u8>,
|
||||
/// The changes made to the state by the call.
|
||||
pub changes: state_machine::OverlayedChanges,
|
||||
}
|
||||
|
||||
/// Block import result.
|
||||
#[derive(Debug)]
|
||||
pub enum ImportResult {
|
||||
/// Added to the import queue.
|
||||
Queued,
|
||||
/// Already in the import queue.
|
||||
AlreadyQueued,
|
||||
/// Already in the blockchain.
|
||||
AlreadyInChain,
|
||||
/// Block or parent is known to be bad.
|
||||
KnownBad,
|
||||
/// Block parent is not in the chain.
|
||||
UnknownParent,
|
||||
}
|
||||
|
||||
/// Block status.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum BlockStatus {
|
||||
/// Added to the import queue.
|
||||
Queued,
|
||||
/// Already in the blockchain.
|
||||
InChain,
|
||||
/// Block or parent is known to be bad.
|
||||
KnownBad,
|
||||
/// Not in the queue or the blockchain.
|
||||
Unknown,
|
||||
}
|
||||
|
||||
/// Create an instance of in-memory client.
|
||||
pub fn new_in_mem<E, F>(
|
||||
executor: E,
|
||||
build_genesis: F
|
||||
) -> error::Result<Client<in_mem::Backend, E>>
|
||||
where
|
||||
E: state_machine::CodeExecutor,
|
||||
F: FnOnce() -> (block::Header, Vec<(Vec<u8>, Vec<u8>)>)
|
||||
{
|
||||
Client::new(in_mem::Backend::new(), executor, build_genesis)
|
||||
}
|
||||
|
||||
impl<B, E> Client<B, E> where
|
||||
B: backend::Backend,
|
||||
E: state_machine::CodeExecutor,
|
||||
error::Error: From<<<B as backend::Backend>::State as state_machine::backend::Backend>::Error>,
|
||||
{
|
||||
/// Creates new Polkadot Client with given blockchain and code executor.
|
||||
pub fn new<F>(
|
||||
backend: B,
|
||||
executor: E,
|
||||
build_genesis: F
|
||||
) -> error::Result<Self>
|
||||
where
|
||||
F: FnOnce() -> (block::Header, Vec<(Vec<u8>, Vec<u8>)>)
|
||||
{
|
||||
if backend.blockchain().header(BlockId::Number(0))?.is_none() {
|
||||
trace!("Empty database, writing genesis block");
|
||||
let (genesis_header, genesis_store) = build_genesis();
|
||||
let mut tx = backend.begin_transaction(BlockId::Hash(block::HeaderHash::default()))?;
|
||||
tx.reset_storage(genesis_store.into_iter())?;
|
||||
tx.import_block(genesis_header, None, true)?;
|
||||
backend.commit_transaction(tx)?;
|
||||
}
|
||||
Ok(Client {
|
||||
backend,
|
||||
executor,
|
||||
})
|
||||
}
|
||||
|
||||
fn state_at(&self, hash: &block::HeaderHash) -> error::Result<B::State> {
|
||||
self.backend.state_at(BlockId::Hash(*hash))
|
||||
}
|
||||
|
||||
/// Return single storage entry of contract under given address in state in a block of given hash.
|
||||
pub fn storage(&self, hash: &block::HeaderHash, key: &StorageKey) -> error::Result<StorageData> {
|
||||
Ok(self.state_at(hash)?
|
||||
.storage(&key.0)
|
||||
.map(|x| StorageData(x.to_vec()))?)
|
||||
}
|
||||
|
||||
/// Execute a call to a contract on top of state in a block of given hash.
|
||||
///
|
||||
/// No changes are made.
|
||||
pub fn call(&self, hash: &block::HeaderHash, method: &str, call_data: &[u8]) -> error::Result<CallResult> {
|
||||
let state = self.state_at(hash)?;
|
||||
let mut changes = state_machine::OverlayedChanges::default();
|
||||
|
||||
let _ = state_machine::execute(
|
||||
&state,
|
||||
&mut changes,
|
||||
&self.executor,
|
||||
method,
|
||||
call_data,
|
||||
)?;
|
||||
Ok(CallResult { return_data: vec![], changes })
|
||||
}
|
||||
|
||||
/// Queue a block for import.
|
||||
pub fn import_block(&self, header: block::Header, body: Option<block::Body>) -> error::Result<ImportResult> {
|
||||
// TODO: import lock
|
||||
// TODO: validate block
|
||||
match self.backend.blockchain().status(BlockId::Hash(header.parent_hash))? {
|
||||
blockchain::BlockStatus::InChain => (),
|
||||
blockchain::BlockStatus::Unknown => return Ok(ImportResult::UnknownParent),
|
||||
}
|
||||
|
||||
let mut transaction = self.backend.begin_transaction(BlockId::Number(header.number))?;
|
||||
let mut _state = transaction.state()?;
|
||||
// TODO: execute block on _state
|
||||
|
||||
let is_new_best = header.number == self.backend.blockchain().info()?.best_number + 1;
|
||||
transaction.import_block(header, body, is_new_best)?;
|
||||
self.backend.commit_transaction(transaction)?;
|
||||
Ok(ImportResult::Queued)
|
||||
}
|
||||
|
||||
/// Get blockchain info.
|
||||
pub fn info(&self) -> error::Result<ClientInfo> {
|
||||
let info = self.backend.blockchain().info().map_err(|e| error::Error::from_blockchain(Box::new(e)))?;
|
||||
Ok(ClientInfo {
|
||||
chain: info,
|
||||
best_queued_hash: None,
|
||||
best_queued_number: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get block status.
|
||||
pub fn block_status(&self, hash: &block::HeaderHash) -> error::Result<BlockStatus> {
|
||||
// TODO: more efficient implementation
|
||||
match self.backend.blockchain().header(BlockId::Hash(*hash)).map_err(|e| error::Error::from_blockchain(Box::new(e)))?.is_some() {
|
||||
true => Ok(BlockStatus::InChain),
|
||||
false => Ok(BlockStatus::Unknown),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get block hash by number.
|
||||
pub fn block_hash(&self, block_number: block::Number) -> error::Result<Option<block::HeaderHash>> {
|
||||
self.backend.blockchain().hash(block_number)
|
||||
}
|
||||
|
||||
/// Get block header by hash.
|
||||
pub fn header(&self, hash: &block::HeaderHash) -> error::Result<Option<block::Header>> {
|
||||
self.backend.blockchain().header(BlockId::Hash(*hash))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "substrate-codec"
|
||||
description = "Serialization and deserialization codec for runtime values"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
substrate-runtime-std = { path = "../runtime-std", default_features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = ["substrate-runtime-std/std"]
|
||||
@@ -0,0 +1,81 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Endian manager.
|
||||
|
||||
/// Trait to allow conversion to a know endian representation when sensitive.
|
||||
/// Types implementing this trait must have a size > 0.
|
||||
// note: the copy bound and static lifetimes are necessary for safety of `Slicable` blanket
|
||||
// implementation.
|
||||
pub trait EndianSensitive: Copy + 'static {
|
||||
fn to_le(self) -> Self { self }
|
||||
fn to_be(self) -> Self { self }
|
||||
fn from_le(self) -> Self { self }
|
||||
fn from_be(self) -> Self { self }
|
||||
fn as_be_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { f(&self) }
|
||||
fn as_le_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { f(&self) }
|
||||
}
|
||||
|
||||
macro_rules! impl_endians {
|
||||
( $( $t:ty ),* ) => { $(
|
||||
impl EndianSensitive for $t {
|
||||
fn to_le(self) -> Self { <$t>::to_le(self) }
|
||||
fn to_be(self) -> Self { <$t>::to_be(self) }
|
||||
fn from_le(self) -> Self { <$t>::from_le(self) }
|
||||
fn from_be(self) -> Self { <$t>::from_be(self) }
|
||||
fn as_be_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { let d = self.to_be(); f(&d) }
|
||||
fn as_le_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { let d = self.to_le(); f(&d) }
|
||||
}
|
||||
)* }
|
||||
}
|
||||
macro_rules! impl_non_endians {
|
||||
( $( $t:ty ),* ) => { $(
|
||||
impl EndianSensitive for $t {}
|
||||
)* }
|
||||
}
|
||||
|
||||
// NOTE: See test to ensure correctness.
|
||||
impl EndianSensitive for bool {}
|
||||
|
||||
impl_endians!(u16, u32, u64, usize, i16, i32, i64, isize);
|
||||
impl_non_endians!(u8, i8, [u8; 1], [u8; 2], [u8; 3], [u8; 4], [u8; 5], [u8; 6], [u8; 7], [u8; 8],
|
||||
[u8; 10], [u8; 12], [u8; 14], [u8; 16], [u8; 20], [u8; 24], [u8; 28], [u8; 32], [u8; 40],
|
||||
[u8; 48], [u8; 56], [u8; 64], [u8; 80], [u8; 96], [u8; 112], [u8; 128]);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::EndianSensitive;
|
||||
|
||||
#[test]
|
||||
fn endian_sensitive_is_copy() {
|
||||
fn _takes_copy<T: Copy>() { }
|
||||
fn _takes_endian_sensitive<T: EndianSensitive>() { _takes_copy::<T>() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn endian_sensitive_outlives_static() {
|
||||
fn _takes_static<T: 'static>() { }
|
||||
fn _takes_endian_sensitive<T: EndianSensitive>() { _takes_static::<T>() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bool_is_not_endian_sensitive() {
|
||||
let b = true;
|
||||
assert_eq!(b.to_be(), b.to_le());
|
||||
let b = false;
|
||||
assert_eq!(b.to_be(), b.to_le());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Trait
|
||||
|
||||
use rstd::iter::Extend;
|
||||
use super::slicable::Slicable;
|
||||
|
||||
/// Trait to allow itself to be serialised into a value which can be extended
|
||||
/// by bytes.
|
||||
pub trait Joiner {
|
||||
fn join<V: Slicable + Sized>(self, value: &V) -> Self;
|
||||
}
|
||||
|
||||
impl<T> Joiner for T where T: for<'a> Extend<&'a u8> {
|
||||
fn join<V: Slicable + Sized>(mut self, value: &V) -> Self {
|
||||
value.as_slice_then(|s| self.extend(s));
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Serialiser and prepender.
|
||||
|
||||
use slicable::Slicable;
|
||||
use rstd::iter::Extend;
|
||||
use rstd::vec::Vec;
|
||||
|
||||
/// Trait to allow itselg to be serialised and prepended by a given slice.
|
||||
pub trait KeyedVec {
|
||||
fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8>;
|
||||
}
|
||||
|
||||
macro_rules! impl_non_endians {
|
||||
( $( $t:ty ),* ) => { $(
|
||||
impl KeyedVec for $t {
|
||||
fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8> {
|
||||
let mut r = prepend_key.to_vec();
|
||||
r.extend(&self[..]);
|
||||
r
|
||||
}
|
||||
}
|
||||
)* }
|
||||
}
|
||||
|
||||
macro_rules! impl_endians {
|
||||
( $( $t:ty ),* ) => { $(
|
||||
impl KeyedVec for $t {
|
||||
fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8> {
|
||||
self.as_slice_then(|slice| {
|
||||
let mut r = prepend_key.to_vec();
|
||||
r.extend(slice);
|
||||
r
|
||||
})
|
||||
}
|
||||
}
|
||||
)* }
|
||||
}
|
||||
|
||||
impl_endians!(u8, i8, u16, u32, u64, usize, i16, i32, i64, isize);
|
||||
impl_non_endians!([u8; 1], [u8; 2], [u8; 3], [u8; 4], [u8; 5], [u8; 6], [u8; 7], [u8; 8],
|
||||
[u8; 10], [u8; 12], [u8; 14], [u8; 16], [u8; 20], [u8; 24], [u8; 28], [u8; 32], [u8; 40],
|
||||
[u8; 48], [u8; 56], [u8; 64], [u8; 80], [u8; 96], [u8; 112], [u8; 128]);
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Implements the serialization and deserialization codec for polkadot runtime
|
||||
//! values.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(not(feature = "std"), feature(alloc))]
|
||||
|
||||
extern crate substrate_runtime_std as rstd;
|
||||
|
||||
mod endiansensitive;
|
||||
mod slicable;
|
||||
mod joiner;
|
||||
mod keyedvec;
|
||||
|
||||
pub use self::endiansensitive::EndianSensitive;
|
||||
pub use self::slicable::{Slicable, NonTrivialSlicable};
|
||||
pub use self::joiner::Joiner;
|
||||
pub use self::keyedvec::KeyedVec;
|
||||
@@ -0,0 +1,142 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Serialisation.
|
||||
|
||||
use rstd::{mem, slice};
|
||||
use rstd::vec::Vec;
|
||||
use super::joiner::Joiner;
|
||||
use super::endiansensitive::EndianSensitive;
|
||||
|
||||
/// Trait that allows zero-copy read/write of value-references to/from slices in LE format.
|
||||
pub trait Slicable: Sized {
|
||||
/// Attempt to deserialise the value from a slice. Ignore trailing bytes and
|
||||
/// set the slice's start to just after the last byte consumed.
|
||||
///
|
||||
/// If `None` is returned, then the slice should be unmodified.
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self>;
|
||||
/// Convert self to an owned vector.
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
self.as_slice_then(|s| s.to_vec())
|
||||
}
|
||||
/// Convert self to a slice and then invoke the given closure with it.
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R;
|
||||
}
|
||||
|
||||
/// Trait to mark that a type is not trivially (essentially "in place") serialisable.
|
||||
pub trait NonTrivialSlicable: Slicable {}
|
||||
|
||||
impl<T: EndianSensitive> Slicable for T {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
let size = mem::size_of::<T>();
|
||||
assert!(size > 0, "EndianSensitive can never be implemented for a zero-sized type.");
|
||||
if value.len() >= size {
|
||||
let x: T = unsafe { ::rstd::ptr::read(value.as_ptr() as *const T) };
|
||||
*value = &value[size..];
|
||||
Some(x.from_le())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
self.as_le_then(|le| {
|
||||
let size = mem::size_of::<T>();
|
||||
let value_slice = unsafe {
|
||||
let ptr = le as *const _ as *const u8;
|
||||
if size != 0 {
|
||||
slice::from_raw_parts(ptr, size)
|
||||
} else {
|
||||
&[]
|
||||
}
|
||||
};
|
||||
|
||||
f(value_slice)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Slicable for Vec<u8> {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
u32::from_slice(value).map(move |len| {
|
||||
let len = len as usize;
|
||||
let res = value[..len].to_vec();
|
||||
*value = &value[len..];
|
||||
res
|
||||
})
|
||||
}
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
f(&self.to_vec())
|
||||
}
|
||||
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
let len = self.len();
|
||||
assert!(len <= u32::max_value() as usize, "Attempted to serialize vec with too many elements.");
|
||||
|
||||
let mut r: Vec<u8> = Vec::new().join(&(len as u32));
|
||||
r.extend_from_slice(self);
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Slicable> NonTrivialSlicable for Vec<T> where Vec<T>: Slicable {}
|
||||
|
||||
impl<T: NonTrivialSlicable> Slicable for Vec<T> {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
u32::from_slice(value).and_then(move |len| {
|
||||
let len = len as usize;
|
||||
let mut r = Vec::with_capacity(len);
|
||||
for _ in 0..len {
|
||||
match T::from_slice(value) {
|
||||
None => return None,
|
||||
Some(v) => r.push(v),
|
||||
}
|
||||
}
|
||||
|
||||
Some(r)
|
||||
})
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
f(&self.to_vec())
|
||||
}
|
||||
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
use rstd::iter::Extend;
|
||||
|
||||
let len = self.len();
|
||||
assert!(len <= u32::max_value() as usize, "Attempted to serialize vec with too many elements.");
|
||||
|
||||
let mut r: Vec<u8> = Vec::new().join(&(len as u32));
|
||||
for item in self {
|
||||
r.extend(item.to_vec());
|
||||
}
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn vec_is_slicable() {
|
||||
let v = b"Hello world".to_vec();
|
||||
v.as_slice_then(|ref slice|
|
||||
assert_eq!(slice, &b"\x0b\0\0\0Hello world")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "ed25519"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
ring = "0.12"
|
||||
untrusted = "0.5"
|
||||
substrate-primitives = { version = "0.1", path = "../primitives" }
|
||||
rustc-hex = "1.0"
|
||||
@@ -0,0 +1,194 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Simple Ed25519 API.
|
||||
|
||||
extern crate ring;
|
||||
extern crate substrate_primitives as primitives;
|
||||
extern crate untrusted;
|
||||
extern crate rustc_hex;
|
||||
|
||||
use ring::{rand, signature};
|
||||
use primitives::hash::H512;
|
||||
use rustc_hex::FromHex;
|
||||
|
||||
/// Alias to 520-bit hash when used in the context of a signature on the relay chain.
|
||||
pub type Signature = H512;
|
||||
|
||||
/// Verify a message without type checking the parameters' types for the right size.
|
||||
pub fn verify(sig: &[u8], message: &[u8], public: &[u8]) -> bool {
|
||||
let public_key = untrusted::Input::from(public);
|
||||
let msg = untrusted::Input::from(message);
|
||||
let sig = untrusted::Input::from(sig);
|
||||
|
||||
match signature::verify(&signature::ED25519, public_key, msg, sig) {
|
||||
Ok(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// A public key.
|
||||
#[derive(PartialEq, Clone, Debug)]
|
||||
pub struct Public ([u8; 32]);
|
||||
|
||||
/// A key pair.
|
||||
pub struct Pair(signature::Ed25519KeyPair);
|
||||
|
||||
impl Public {
|
||||
/// A new instance from the given 32-byte `data`.
|
||||
pub fn from(data: [u8; 32]) -> Self {
|
||||
Public(data)
|
||||
}
|
||||
|
||||
/// A new instance from the given slice that should be 32 bytes long.
|
||||
pub fn from_slice(data: &[u8]) -> Self {
|
||||
let mut r = [0u8; 32];
|
||||
r.copy_from_slice(data);
|
||||
Public(r)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8; 32]> for Public {
|
||||
fn as_ref(&self) -> &[u8; 32] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for Public {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&self.0[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl Pair {
|
||||
/// Generate new secure (random) key pair.
|
||||
pub fn new() -> Pair {
|
||||
let rng = rand::SystemRandom::new();
|
||||
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
|
||||
Pair(signature::Ed25519KeyPair::from_pkcs8(untrusted::Input::from(&pkcs8_bytes)).unwrap())
|
||||
}
|
||||
/// Make a new key pair from a seed phrase.
|
||||
pub fn from_seed(seed: &[u8; 32]) -> Pair {
|
||||
Pair(signature::Ed25519KeyPair::from_seed_unchecked(untrusted::Input::from(&seed[..])).unwrap())
|
||||
}
|
||||
/// Make a new key pair from the raw secret.
|
||||
pub fn from_secret(secret: &[u8; 32]) -> Pair {
|
||||
let mut pkcs8_bytes: Vec<_> = FromHex::from_hex("302e020100300506032b657004220420").unwrap();
|
||||
pkcs8_bytes.extend_from_slice(&secret[..]);
|
||||
Pair(signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(untrusted::Input::from(&pkcs8_bytes)).unwrap())
|
||||
}
|
||||
/// Make a new key pair from the raw secret and public key (it will check to make sure
|
||||
/// they correspond to each other).
|
||||
pub fn from_both(secret_public: &[u8; 64]) -> Option<Pair> {
|
||||
let mut pkcs8_bytes: Vec<_> = FromHex::from_hex("3053020101300506032b657004220420").unwrap();
|
||||
pkcs8_bytes.extend_from_slice(&secret_public[0..32]);
|
||||
pkcs8_bytes.extend_from_slice(&[0xa1u8, 0x23, 0x03, 0x21, 0x00]);
|
||||
pkcs8_bytes.extend_from_slice(&secret_public[32..64]);
|
||||
signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(untrusted::Input::from(&pkcs8_bytes)).ok().map(Pair)
|
||||
}
|
||||
/// Sign a message.
|
||||
pub fn sign(&self, message: &[u8]) -> Signature {
|
||||
let mut r = [0u8; 64];
|
||||
r.copy_from_slice(self.0.sign(message).as_ref());
|
||||
Signature::from(r)
|
||||
}
|
||||
/// Get the public key.
|
||||
pub fn public(&self) -> Public {
|
||||
let mut r = [0u8; 32];
|
||||
let pk = self.0.public_key_bytes();
|
||||
r.copy_from_slice(pk);
|
||||
Public(r)
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify a signature on a message.
|
||||
pub fn verify_strong(sig: &Signature, message: &[u8], pubkey: &Public) -> bool {
|
||||
let public_key = untrusted::Input::from(&pubkey.0[..]);
|
||||
let msg = untrusted::Input::from(message);
|
||||
let sig = untrusted::Input::from(&sig.0[..]);
|
||||
|
||||
match signature::verify(&signature::ED25519, public_key, msg, sig) {
|
||||
Ok(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for Public {
|
||||
fn from(hex: &'static str) -> Self {
|
||||
let mut r = [0u8; 32];
|
||||
let v: Vec<_> = FromHex::from_hex(hex).unwrap();
|
||||
r.copy_from_slice(&v[0..32]);
|
||||
Public(r)
|
||||
}
|
||||
}
|
||||
impl From<&'static str> for Pair {
|
||||
fn from(hex: &'static str) -> Self {
|
||||
let data: Vec<_> = FromHex::from_hex(hex).expect("Key pair given is static so hex should be good.");
|
||||
match data.len() {
|
||||
32 => {
|
||||
let mut r = [0u8; 32];
|
||||
r.copy_from_slice(&data[0..32]);
|
||||
Pair::from_secret(&r)
|
||||
}
|
||||
64 => {
|
||||
let mut r = [0u8; 64];
|
||||
r.copy_from_slice(&data[0..64]);
|
||||
Pair::from_both(&r).expect("Key pair given is static so should be good.")
|
||||
}
|
||||
_ => {
|
||||
panic!("Key pair given is static so should be correct length.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_vector_should_work() {
|
||||
let pair: Pair = "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60".into();
|
||||
let public = pair.public();
|
||||
assert_eq!(public, "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a".into());
|
||||
let message = b"";
|
||||
let signature: Signature = "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b".into();
|
||||
assert!(&pair.sign(&message[..]) == &signature);
|
||||
assert!(verify_strong(&signature, &message[..], &public));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_pair_should_work() {
|
||||
let pair = Pair::new();
|
||||
let public = pair.public();
|
||||
let message = b"Something important";
|
||||
let signature = pair.sign(&message[..]);
|
||||
assert!(verify_strong(&signature, &message[..], &public));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn seeded_pair_should_work() {
|
||||
use primitives::hexdisplay::HexDisplay;
|
||||
|
||||
let pair = Pair::from_seed(b"12345678901234567890123456789012");
|
||||
let public = pair.public();
|
||||
assert_eq!(public, "2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee".into());
|
||||
let message: Vec<_> = FromHex::from_hex("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee00000000000000002228000000d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a4500000000000000").unwrap();
|
||||
let signature = pair.sign(&message[..]);
|
||||
println!("Correct signature: {}", HexDisplay::from(&signature.0));
|
||||
assert!(verify_strong(&signature, &message[..], &public));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "environmental"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
@@ -0,0 +1,318 @@
|
||||
// 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/>.
|
||||
|
||||
//! Safe global references to stack variables.
|
||||
//!
|
||||
//! Set up a global reference with environmental! macro giving it a name and type.
|
||||
//! Use the `using` function scoped under its name to name a reference and call a function that
|
||||
//! takes no parameters yet can access said reference through the similarly placed `with` function.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```
|
||||
//! #[macro_use] extern crate environmental;
|
||||
//! // create a place for the global reference to exist.
|
||||
//! environmental!(counter: u32);
|
||||
//! fn stuff() {
|
||||
//! // do some stuff, accessing the named reference as desired.
|
||||
//! counter::with(|i| *i += 1);
|
||||
//! }
|
||||
//! fn main() {
|
||||
//! // declare a stack variable of the same type as our global declaration.
|
||||
//! let mut counter_value = 41u32;
|
||||
//! // call stuff, setting up our `counter` environment as a refrence to our counter_value var.
|
||||
//! counter::using(&mut counter_value, stuff);
|
||||
//! println!("The answer is {:?}", counter_value); // will print 42!
|
||||
//! stuff(); // safe! doesn't do anything.
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::thread::LocalKey;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn using<T: ?Sized, R, F: FnOnce() -> R>(
|
||||
global: &'static LocalKey<RefCell<Option<*mut T>>>,
|
||||
protected: &mut T,
|
||||
f: F
|
||||
) -> R {
|
||||
// store the `protected` reference as a pointer so we can provide it to logic running within
|
||||
// `f`.
|
||||
// while we record this pointer (while it's non-zero) we guarantee:
|
||||
// - it will only be used once at any time (no reentrancy);
|
||||
// - that no other thread will use it; and
|
||||
// - that we do not use the original mutating reference while the pointer.
|
||||
// exists.
|
||||
global.with(|r| {
|
||||
let original = {
|
||||
let mut global = r.borrow_mut();
|
||||
::std::mem::replace(&mut *global, Some(protected as _))
|
||||
};
|
||||
|
||||
// even if `f` panics the original will be replaced.
|
||||
struct ReplaceOriginal<'a, T: 'a + ?Sized> {
|
||||
original: Option<*mut T>,
|
||||
global: &'a RefCell<Option<*mut T>>,
|
||||
}
|
||||
|
||||
impl<'a, T: 'a + ?Sized> Drop for ReplaceOriginal<'a, T> {
|
||||
fn drop(&mut self) {
|
||||
*self.global.borrow_mut() = self.original.take();
|
||||
}
|
||||
}
|
||||
|
||||
let _guard = ReplaceOriginal {
|
||||
original,
|
||||
global: r,
|
||||
};
|
||||
|
||||
f()
|
||||
})
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn with<T: ?Sized, R, F: FnOnce(&mut T) -> R>(
|
||||
global: &'static LocalKey<RefCell<Option<*mut T>>>,
|
||||
mutator: F,
|
||||
) -> Option<R> {
|
||||
global.with(|r| unsafe {
|
||||
let ptr = r.borrow_mut();
|
||||
match *ptr {
|
||||
Some(ptr) => {
|
||||
// safe because it's only non-zero when it's being called from using, which
|
||||
// is holding on to the underlying reference (and not using it itself) safely.
|
||||
Some(mutator(&mut *ptr))
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Declare a new global reference module whose underlying value does not contain references.
|
||||
///
|
||||
/// Will create a module of a given name that contains two functions:
|
||||
/// * `pub fn using<R, F: FnOnce() -> R>(protected: &mut $t, f: F) -> R`
|
||||
/// This executes `f`, returning its value. During the call, the module's reference is set to
|
||||
/// be equal to `protected`.
|
||||
/// * `pub fn with<R, F: FnOnce(&mut $t) -> R>(f: F) -> Option<R>`
|
||||
/// This executes `f`, returning `Some` of its value if called from code that is being executed
|
||||
/// as part of a `using` call. If not, it returns `None`. `f` is provided with one argument: the
|
||||
/// same reference as provided to the most recent `using` call.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Initializing the global context with a given value.
|
||||
///
|
||||
/// ```rust
|
||||
/// #[macro_use] extern crate environmental;
|
||||
/// environmental!(counter: u32);
|
||||
/// fn main() {
|
||||
/// let mut counter_value = 41u32;
|
||||
/// counter::using(&mut counter_value, || {
|
||||
/// let odd = counter::with(|value|
|
||||
/// if *value % 2 == 1 {
|
||||
/// *value += 1; true
|
||||
/// } else {
|
||||
/// *value -= 3; false
|
||||
/// }).unwrap(); // safe because we're inside a counter::using
|
||||
/// println!("counter was {}", match odd { true => "odd", _ => "even" });
|
||||
/// });
|
||||
///
|
||||
/// println!("The answer is {:?}", counter_value); // 42
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Roughly the same, but with a trait object:
|
||||
///
|
||||
/// ```rust
|
||||
/// #[macro_use] extern crate environmental;
|
||||
///
|
||||
/// trait Increment { fn increment(&mut self); }
|
||||
///
|
||||
/// impl Increment for i32 {
|
||||
/// fn increment(&mut self) { *self += 1 }
|
||||
/// }
|
||||
///
|
||||
/// environmental!(val: Increment + 'static);
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut local = 0i32;
|
||||
/// val::using(&mut local, || {
|
||||
/// val::with(|v| for _ in 0..5 { v.increment() });
|
||||
/// });
|
||||
///
|
||||
/// assert_eq!(local, 5);
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! environmental {
|
||||
($name:ident : $t:ty) => {
|
||||
#[allow(non_camel_case_types)]
|
||||
struct $name { __private_field: () }
|
||||
|
||||
thread_local!(static GLOBAL: ::std::cell::RefCell<Option<*mut $t>>
|
||||
= ::std::cell::RefCell::new(None));
|
||||
|
||||
impl $name {
|
||||
#[allow(unused_imports)]
|
||||
|
||||
pub fn using<R, F: FnOnce() -> R>(
|
||||
protected: &mut $t,
|
||||
f: F
|
||||
) -> R {
|
||||
$crate::using(&GLOBAL, protected, f)
|
||||
}
|
||||
|
||||
pub fn with<R, F: FnOnce(&mut $t) -> R>(
|
||||
f: F
|
||||
) -> Option<R> {
|
||||
$crate::with(&GLOBAL, |x| f(x))
|
||||
}
|
||||
}
|
||||
};
|
||||
($name:ident : trait $t:ident) => {
|
||||
#[allow(non_camel_case_types)]
|
||||
struct $name { __private_field: () }
|
||||
|
||||
thread_local!(static GLOBAL: ::std::cell::RefCell<Option<*mut ($t + 'static)>>
|
||||
= ::std::cell::RefCell::new(None));
|
||||
|
||||
impl $name {
|
||||
#[allow(unused_imports)]
|
||||
|
||||
pub fn using<R, F: FnOnce() -> R>(
|
||||
protected: &mut $t,
|
||||
f: F
|
||||
) -> R {
|
||||
let lifetime_extended = unsafe {
|
||||
::std::mem::transmute::<&mut $t, &mut ($t + 'static)>(protected)
|
||||
};
|
||||
$crate::using(&GLOBAL, lifetime_extended, f)
|
||||
}
|
||||
|
||||
pub fn with<R, F: for<'a> FnOnce(&'a mut ($t + 'a)) -> R>(
|
||||
f: F
|
||||
) -> Option<R> {
|
||||
$crate::with(&GLOBAL, |x| f(x))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[test]
|
||||
fn simple_works() {
|
||||
environmental!(counter: u32);
|
||||
|
||||
fn stuff() { counter::with(|value| *value += 1); };
|
||||
|
||||
// declare a stack variable of the same type as our global declaration.
|
||||
let mut local = 41u32;
|
||||
|
||||
// call stuff, setting up our `counter` environment as a refrence to our local counter var.
|
||||
counter::using(&mut local, stuff);
|
||||
assert_eq!(local, 42);
|
||||
stuff(); // safe! doesn't do anything.
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overwrite_with_lesser_lifetime() {
|
||||
environmental!(items: Vec<u8>);
|
||||
|
||||
let mut local_items = vec![1, 2, 3];
|
||||
items::using(&mut local_items, || {
|
||||
let dies_at_end = vec![4, 5, 6];
|
||||
items::with(|items| *items = dies_at_end);
|
||||
});
|
||||
|
||||
assert_eq!(local_items, vec![4, 5, 6]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn declare_with_trait_object() {
|
||||
trait Foo {
|
||||
fn get(&self) -> i32;
|
||||
fn set(&mut self, x: i32);
|
||||
}
|
||||
|
||||
impl Foo for i32 {
|
||||
fn get(&self) -> i32 { *self }
|
||||
fn set(&mut self, x: i32) { *self = x }
|
||||
}
|
||||
|
||||
environmental!(foo: Foo + 'static);
|
||||
|
||||
fn stuff() {
|
||||
foo::with(|value| {
|
||||
let new_val = value.get() + 1;
|
||||
value.set(new_val);
|
||||
});
|
||||
}
|
||||
|
||||
let mut local = 41i32;
|
||||
foo::using(&mut local, stuff);
|
||||
|
||||
assert_eq!(local, 42);
|
||||
|
||||
stuff(); // doesn't do anything.
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unwind_recursive() {
|
||||
use std::panic;
|
||||
|
||||
environmental!(items: Vec<u8>);
|
||||
|
||||
let panicked = panic::catch_unwind(|| {
|
||||
let mut local_outer = vec![1, 2, 3];
|
||||
|
||||
items::using(&mut local_outer, || {
|
||||
let mut local_inner = vec![4, 5, 6];
|
||||
items::using(&mut local_inner, || {
|
||||
panic!("are you unsafe?");
|
||||
})
|
||||
});
|
||||
}).is_err();
|
||||
|
||||
assert!(panicked);
|
||||
|
||||
let mut was_cleared = true;
|
||||
items::with(|_items| was_cleared = false);
|
||||
|
||||
assert!(was_cleared);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn use_non_static_trait() {
|
||||
trait Sum { fn sum(&self) -> usize; }
|
||||
impl<'a> Sum for &'a [usize] {
|
||||
fn sum(&self) -> usize {
|
||||
self.iter().fold(0, |a, c| a + c)
|
||||
}
|
||||
}
|
||||
|
||||
environmental!(sum: trait Sum);
|
||||
let numbers = vec![1, 2, 3, 4, 5];
|
||||
let mut numbers = &numbers[..];
|
||||
let got_sum = sum::using(&mut numbers, || {
|
||||
sum::with(|x| x.sum())
|
||||
}).unwrap();
|
||||
|
||||
assert_eq!(got_sum, 15);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
[package]
|
||||
name = "substrate-executor"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
error-chain = "0.11"
|
||||
substrate-codec = { path = "../codec" }
|
||||
substrate-runtime-io = { path = "../runtime-io" }
|
||||
substrate-primitives = { path = "../primitives" }
|
||||
substrate-serializer = { path = "../serializer" }
|
||||
substrate-state-machine = { path = "../state-machine" }
|
||||
ed25519 = { path = "../ed25519" }
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
parity-wasm = "0.15.0"
|
||||
byteorder = "1.1"
|
||||
rustc-hex = "1.0.0"
|
||||
triehash = "0.1.0"
|
||||
hex-literal = "0.1.0"
|
||||
log = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
assert_matches = "1.1"
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Rust executor possible errors.
|
||||
|
||||
use serializer;
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
InvalidData(serializer::Error) #[doc = "Unserializable Data"];
|
||||
}
|
||||
|
||||
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 {
|
||||
description("externalities failure"),
|
||||
display("Externalities error"),
|
||||
}
|
||||
|
||||
/// Invalid index.
|
||||
InvalidIndex {
|
||||
description("index given was not in range"),
|
||||
display("Invalid index provided"),
|
||||
}
|
||||
|
||||
/// Invalid return type.
|
||||
InvalidReturn {
|
||||
description("u64 was not returned"),
|
||||
display("Invalid type returned (should be u64)"),
|
||||
}
|
||||
|
||||
/// Runtime failed.
|
||||
Runtime {
|
||||
description("runtime failure"),
|
||||
display("Runtime error"),
|
||||
}
|
||||
|
||||
/// Runtime failed.
|
||||
InvalidMemoryReference {
|
||||
description("invalid memory reference"),
|
||||
display("Invalid memory reference"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Temporary crate for contracts implementations.
|
||||
//!
|
||||
//! This will be replaced with WASM contracts stored on-chain.
|
||||
//! ** NOTE ***
|
||||
//! This is entirely deprecated with the idea of a single-module Wasm module for state transition.
|
||||
//! The dispatch table should be replaced with the specific functions needed:
|
||||
//! - execute_block(bytes)
|
||||
//! - init_block(PrevBlock?) -> InProgressBlock
|
||||
//! - add_transaction(InProgressBlock) -> InProgressBlock
|
||||
//! I leave it as is for now as it might be removed before this is ever done.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
extern crate substrate_codec as codec;
|
||||
extern crate substrate_runtime_io as runtime_io;
|
||||
extern crate substrate_primitives as primitives;
|
||||
extern crate substrate_serializer as serializer;
|
||||
extern crate substrate_state_machine as state_machine;
|
||||
extern crate ed25519;
|
||||
|
||||
extern crate serde;
|
||||
extern crate parity_wasm;
|
||||
extern crate byteorder;
|
||||
extern crate rustc_hex;
|
||||
extern crate triehash;
|
||||
#[macro_use] extern crate log;
|
||||
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
|
||||
#[cfg(test)]
|
||||
extern crate assert_matches;
|
||||
|
||||
// TODO: move into own crate
|
||||
macro_rules! map {
|
||||
($( $name:expr => $value:expr ),*) => (
|
||||
vec![ $( ( $name, $value ) ),* ].into_iter().collect()
|
||||
)
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
mod wasm_utils;
|
||||
mod wasm_executor;
|
||||
mod native_executor;
|
||||
|
||||
pub mod error;
|
||||
pub use wasm_executor::WasmExecutor;
|
||||
pub use native_executor::{NativeExecutionDispatch, NativeExecutor};
|
||||
@@ -0,0 +1,63 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use error::{Error, ErrorKind, Result};
|
||||
use runtime_io;
|
||||
use state_machine::{Externalities, CodeExecutor};
|
||||
use wasm_executor::WasmExecutor;
|
||||
|
||||
use std::panic::catch_unwind;
|
||||
|
||||
/// Delegate for dispatching a CodeExecutor call to native code.
|
||||
pub trait NativeExecutionDispatch {
|
||||
/// Get the wasm code that the native dispatch will be equivalent to.
|
||||
fn native_equivalent() -> &'static [u8];
|
||||
|
||||
/// Dispatch a method and input data to be executed natively. Returns `Some` result or `None`
|
||||
/// if the `method` is unknown. Panics if there's an unrecoverable error.
|
||||
fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>>;
|
||||
}
|
||||
|
||||
/// A generic `CodeExecutor` implementation that uses a delegate to determine wasm code equivalence
|
||||
/// and dispatch to native code when possible, falling back on `WasmExecutor` when not.
|
||||
pub struct NativeExecutor<D: NativeExecutionDispatch + Sync + Send> {
|
||||
/// Dummy field to avoid the compiler complaining about us not using `D`.
|
||||
pub _dummy: ::std::marker::PhantomData<D>,
|
||||
}
|
||||
|
||||
fn safe_call<F: ::std::panic::UnwindSafe + FnOnce() -> Option<Vec<u8>>>(f: F) -> Result<Option<Vec<u8>>> {
|
||||
catch_unwind(f).map_err(|_| ErrorKind::Runtime.into())
|
||||
}
|
||||
|
||||
impl<D: NativeExecutionDispatch + Sync + Send> CodeExecutor for NativeExecutor<D> {
|
||||
type Error = Error;
|
||||
|
||||
fn call<E: Externalities>(
|
||||
&self,
|
||||
ext: &mut E,
|
||||
code: &[u8],
|
||||
method: &str,
|
||||
data: &[u8],
|
||||
) -> Result<Vec<u8>> {
|
||||
if code == D::native_equivalent() {
|
||||
runtime_io::with_externalities(ext, || safe_call(|| D::dispatch(method, data)))?
|
||||
.ok_or(ErrorKind::MethodNotFound(method.to_owned()).into())
|
||||
} else {
|
||||
// call into wasm.
|
||||
WasmExecutor.call(ext, code, method, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,442 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Rust implementation of Substrate contracts.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashMap;
|
||||
use parity_wasm::{deserialize_buffer, ModuleInstanceInterface, ProgramInstance};
|
||||
use parity_wasm::interpreter::{ItemIndex, DummyUserError};
|
||||
use parity_wasm::RuntimeValue::{I32, I64};
|
||||
use state_machine::{Externalities, CodeExecutor};
|
||||
use error::{Error, ErrorKind, Result};
|
||||
use wasm_utils::{MemoryInstance, UserDefinedElements,
|
||||
AddModuleWithoutFullDependentInstance};
|
||||
use primitives::{blake2_256, twox_128, twox_256};
|
||||
use primitives::hexdisplay::HexDisplay;
|
||||
use triehash::ordered_trie_root;
|
||||
|
||||
struct Heap {
|
||||
end: u32,
|
||||
}
|
||||
|
||||
impl Heap {
|
||||
fn new() -> Self {
|
||||
Heap {
|
||||
end: 262144,
|
||||
}
|
||||
}
|
||||
fn allocate(&mut self, size: u32) -> u32 {
|
||||
let r = self.end;
|
||||
self.end += size;
|
||||
r
|
||||
}
|
||||
fn deallocate(&mut self, _offset: u32) {
|
||||
}
|
||||
}
|
||||
|
||||
struct FunctionExecutor<'e, E: Externalities + 'e> {
|
||||
heap: Heap,
|
||||
memory: Arc<MemoryInstance>,
|
||||
ext: &'e mut E,
|
||||
hash_lookup: HashMap<Vec<u8>, Vec<u8>>,
|
||||
}
|
||||
|
||||
impl<'e, E: Externalities> FunctionExecutor<'e, E> {
|
||||
fn new(m: &Arc<MemoryInstance>, e: &'e mut E) -> Self {
|
||||
FunctionExecutor {
|
||||
heap: Heap::new(),
|
||||
memory: Arc::clone(m),
|
||||
ext: e,
|
||||
hash_lookup: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait WritePrimitive<T: Sized> {
|
||||
fn write_primitive(&self, offset: u32, t: T) -> ::std::result::Result<(), DummyUserError>;
|
||||
}
|
||||
|
||||
impl WritePrimitive<u32> for MemoryInstance {
|
||||
fn write_primitive(&self, offset: u32, t: u32) -> ::std::result::Result<(), DummyUserError> {
|
||||
use byteorder::{LittleEndian, ByteOrder};
|
||||
let mut r = [0u8; 4];
|
||||
LittleEndian::write_u32(&mut r, t);
|
||||
self.set(offset, &r).map_err(|_| DummyUserError)
|
||||
}
|
||||
}
|
||||
|
||||
trait ReadPrimitive<T: Sized> {
|
||||
fn read_primitive(&self, offset: u32) -> ::std::result::Result<T, DummyUserError>;
|
||||
}
|
||||
|
||||
impl ReadPrimitive<u32> for MemoryInstance {
|
||||
fn read_primitive(&self, offset: u32) -> ::std::result::Result<u32, DummyUserError> {
|
||||
use byteorder::{LittleEndian, ByteOrder};
|
||||
Ok(LittleEndian::read_u32(&self.get(offset, 4).map_err(|_| DummyUserError)?))
|
||||
}
|
||||
}
|
||||
|
||||
fn ascii_format(asciish: &[u8]) -> String {
|
||||
let mut r = String::new();
|
||||
let mut latch = false;
|
||||
for c in asciish {
|
||||
match (latch, *c) {
|
||||
(false, 32...127) => r.push(*c as char),
|
||||
_ => {
|
||||
if !latch {
|
||||
r.push('#');
|
||||
latch = true;
|
||||
}
|
||||
r.push_str(&format!("{:02x}", *c));
|
||||
}
|
||||
}
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
impl_function_executor!(this: FunctionExecutor<'e, E>,
|
||||
ext_print_utf8(utf8_data: *const u8, utf8_len: u32) => {
|
||||
if let Ok(utf8) = this.memory.get(utf8_data, utf8_len as usize) {
|
||||
if let Ok(message) = String::from_utf8(utf8) {
|
||||
println!("{}", message);
|
||||
}
|
||||
}
|
||||
},
|
||||
ext_print_hex(data: *const u8, len: u32) => {
|
||||
if let Ok(hex) = this.memory.get(data, len as usize) {
|
||||
println!("{}", HexDisplay::from(&hex));
|
||||
}
|
||||
},
|
||||
ext_print_num(number: u64) => {
|
||||
println!("{}", number);
|
||||
},
|
||||
ext_memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 => {
|
||||
let sl1 = this.memory.get(s1, n as usize).map_err(|_| DummyUserError)?;
|
||||
let sl2 = this.memory.get(s2, n as usize).map_err(|_| DummyUserError)?;
|
||||
match sl1.cmp(&sl2) {
|
||||
Ordering::Greater => 1,
|
||||
Ordering::Less => -1,
|
||||
Ordering::Equal => 0,
|
||||
}
|
||||
},
|
||||
ext_memcpy(dest: *mut u8, src: *const u8, count: usize) -> *mut u8 => {
|
||||
this.memory.copy_nonoverlapping(src as usize, dest as usize, count as usize)
|
||||
.map_err(|_| DummyUserError)?;
|
||||
trace!(target: "runtime-io", "memcpy {} from {}, {} bytes", dest, src, count);
|
||||
dest
|
||||
},
|
||||
ext_memmove(dest: *mut u8, src: *const u8, count: usize) -> *mut u8 => {
|
||||
this.memory.copy(src as usize, dest as usize, count as usize)
|
||||
.map_err(|_| DummyUserError)?;
|
||||
trace!(target: "runtime-io", "memmove {} from {}, {} bytes", dest, src, count);
|
||||
dest
|
||||
},
|
||||
ext_memset(dest: *mut u8, val: u32, count: usize) -> *mut u8 => {
|
||||
this.memory.clear(dest as usize, val as u8, count as usize)
|
||||
.map_err(|_| DummyUserError)?;
|
||||
trace!(target: "runtime-io", "memset {} with {}, {} bytes", dest, val, count);
|
||||
dest
|
||||
},
|
||||
ext_malloc(size: usize) -> *mut u8 => {
|
||||
let r = this.heap.allocate(size);
|
||||
trace!(target: "runtime-io", "malloc {} bytes at {}", size, r);
|
||||
r
|
||||
},
|
||||
ext_free(addr: *mut u8) => {
|
||||
this.heap.deallocate(addr);
|
||||
trace!(target: "runtime-io", "free {}", addr)
|
||||
},
|
||||
ext_set_storage(key_data: *const u8, key_len: u32, value_data: *const u8, value_len: u32) => {
|
||||
let key = this.memory.get(key_data, key_len as usize).map_err(|_| DummyUserError)?;
|
||||
let value = this.memory.get(value_data, value_len as usize).map_err(|_| DummyUserError)?;
|
||||
if let Some(preimage) = this.hash_lookup.get(&key) {
|
||||
info!(target: "wasm-trace", "*** Setting storage: %{} -> {} [k={}]", ascii_format(&preimage), HexDisplay::from(&value), HexDisplay::from(&key));
|
||||
} else {
|
||||
info!(target: "wasm-trace", "*** Setting storage: {} -> {} [k={}]", ascii_format(&key), HexDisplay::from(&value), HexDisplay::from(&key));
|
||||
}
|
||||
this.ext.set_storage(key, value);
|
||||
},
|
||||
ext_get_allocated_storage(key_data: *const u8, key_len: u32, written_out: *mut u32) -> *mut u8 => {
|
||||
let key = this.memory.get(key_data, key_len as usize).map_err(|_| DummyUserError)?;
|
||||
let value = this.ext.storage(&key).map_err(|_| DummyUserError)?;
|
||||
|
||||
if let Some(preimage) = this.hash_lookup.get(&key) {
|
||||
info!(target: "wasm-trace", " Getting storage: %{} == {} [k={}]", ascii_format(&preimage), HexDisplay::from(&value), HexDisplay::from(&key));
|
||||
} else {
|
||||
info!(target: "wasm-trace", " Getting storage: {} == {} [k={}]", ascii_format(&key), HexDisplay::from(&value), HexDisplay::from(&key));
|
||||
}
|
||||
|
||||
let offset = this.heap.allocate(value.len() as u32) as u32;
|
||||
this.memory.set(offset, &value).map_err(|_| DummyUserError)?;
|
||||
|
||||
this.memory.write_primitive(written_out, value.len() as u32)?;
|
||||
offset
|
||||
},
|
||||
ext_get_storage_into(key_data: *const u8, key_len: u32, value_data: *mut u8, value_len: u32, value_offset: u32) -> u32 => {
|
||||
let key = this.memory.get(key_data, key_len as usize).map_err(|_| DummyUserError)?;
|
||||
let value = this.ext.storage(&key).map_err(|_| DummyUserError)?;
|
||||
if let Some(preimage) = this.hash_lookup.get(&key) {
|
||||
info!(target: "wasm-trace", " Getting storage: %{} == {} [k={}]", ascii_format(&preimage), HexDisplay::from(&value), HexDisplay::from(&key));
|
||||
} else {
|
||||
info!(target: "wasm-trace", " Getting storage: {} == {} [k={}]", ascii_format(&key), HexDisplay::from(&value), HexDisplay::from(&key));
|
||||
}
|
||||
let value = &value[value_offset as usize..];
|
||||
let written = ::std::cmp::min(value_len as usize, value.len());
|
||||
this.memory.set(value_data, &value[..written]).map_err(|_| DummyUserError)?;
|
||||
written as u32
|
||||
},
|
||||
ext_storage_root(result: *mut u8) => {
|
||||
let r = this.ext.storage_root();
|
||||
this.memory.set(result, &r[..]).map_err(|_| DummyUserError)?;
|
||||
},
|
||||
ext_enumerated_trie_root(values_data: *const u8, lens_data: *const u32, lens_len: u32, result: *mut u8) => {
|
||||
let values = (0..lens_len)
|
||||
.map(|i| this.memory.read_primitive(lens_data + i * 4))
|
||||
.collect::<::std::result::Result<Vec<u32>, DummyUserError>>()?
|
||||
.into_iter()
|
||||
.scan(0u32, |acc, v| { let o = *acc; *acc += v; Some((o, v)) })
|
||||
.map(|(offset, len)|
|
||||
this.memory.get(values_data + offset, len as usize)
|
||||
.map_err(|_| DummyUserError)
|
||||
)
|
||||
.collect::<::std::result::Result<Vec<_>, DummyUserError>>()?;
|
||||
let r = ordered_trie_root(values.into_iter());
|
||||
this.memory.set(result, &r[..]).map_err(|_| DummyUserError)?;
|
||||
},
|
||||
ext_chain_id() -> u64 => {
|
||||
this.ext.chain_id()
|
||||
},
|
||||
ext_twox_128(data: *const u8, len: u32, out: *mut u8) => {
|
||||
let result = if len == 0 {
|
||||
let hashed = twox_128(&[0u8; 0]);
|
||||
trace!(target: "xxhash", "XXhash: '' -> {}", HexDisplay::from(&hashed));
|
||||
this.hash_lookup.insert(hashed.to_vec(), vec![]);
|
||||
hashed
|
||||
} else {
|
||||
let key = this.memory.get(data, len as usize).map_err(|_| DummyUserError)?;
|
||||
let hashed_key = twox_128(&key);
|
||||
if let Ok(skey) = ::std::str::from_utf8(&key) {
|
||||
trace!(target: "xxhash", "XXhash: {} -> {}", skey, HexDisplay::from(&hashed_key));
|
||||
} else {
|
||||
trace!(target: "xxhash", "XXhash: {} -> {}", HexDisplay::from(&key), HexDisplay::from(&hashed_key));
|
||||
}
|
||||
this.hash_lookup.insert(hashed_key.to_vec(), key);
|
||||
hashed_key
|
||||
};
|
||||
|
||||
this.memory.set(out, &result).map_err(|_| DummyUserError)?;
|
||||
},
|
||||
ext_twox_256(data: *const u8, len: u32, out: *mut u8) => {
|
||||
let result = if len == 0 {
|
||||
twox_256(&[0u8; 0])
|
||||
} else {
|
||||
twox_256(&this.memory.get(data, len as usize).map_err(|_| DummyUserError)?)
|
||||
};
|
||||
this.memory.set(out, &result).map_err(|_| DummyUserError)?;
|
||||
},
|
||||
ext_blake2_256(data: *const u8, len: u32, out: *mut u8) => {
|
||||
let result = if len == 0 {
|
||||
blake2_256(&[0u8; 0])
|
||||
} else {
|
||||
blake2_256(&this.memory.get(data, len as usize).map_err(|_| DummyUserError)?)
|
||||
};
|
||||
this.memory.set(out, &result).map_err(|_| DummyUserError)?;
|
||||
},
|
||||
ext_ed25519_verify(msg_data: *const u8, msg_len: u32, sig_data: *const u8, pubkey_data: *const u8) -> u32 => {
|
||||
let mut sig = [0u8; 64];
|
||||
this.memory.get_into(sig_data, &mut sig[..]).map_err(|_| DummyUserError)?;
|
||||
let mut pubkey = [0u8; 32];
|
||||
this.memory.get_into(pubkey_data, &mut pubkey[..]).map_err(|_| DummyUserError)?;
|
||||
let msg = this.memory.get(msg_data, msg_len as usize).map_err(|_| DummyUserError)?;
|
||||
|
||||
if ::ed25519::verify(&sig, &msg, &pubkey) {
|
||||
0
|
||||
} else {
|
||||
5
|
||||
}
|
||||
}
|
||||
=> <'e, E: Externalities + 'e>
|
||||
);
|
||||
|
||||
/// Wasm rust executor for contracts.
|
||||
///
|
||||
/// Executes the provided code in a sandboxed wasm runtime.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct WasmExecutor;
|
||||
|
||||
impl CodeExecutor for WasmExecutor {
|
||||
type Error = Error;
|
||||
|
||||
fn call<E: Externalities>(
|
||||
&self,
|
||||
ext: &mut E,
|
||||
code: &[u8],
|
||||
method: &str,
|
||||
data: &[u8],
|
||||
) -> Result<Vec<u8>> {
|
||||
// TODO: handle all expects as errors to be returned.
|
||||
println!("Wasm-Calling {}({})", method, HexDisplay::from(&data));
|
||||
|
||||
let program = ProgramInstance::new().expect("this really shouldn't be able to fail; qed");
|
||||
|
||||
let module = deserialize_buffer(code.to_vec()).expect("all modules compiled with rustc are valid wasm code; qed");
|
||||
let module = program.add_module_by_sigs("test", module, map!["env" => FunctionExecutor::<E>::SIGNATURES]).expect("runtime signatures always provided; qed");
|
||||
|
||||
let memory = module.memory(ItemIndex::Internal(0)).expect("all modules compiled with rustc include memory segments; qed");
|
||||
let mut fec = FunctionExecutor::new(&memory, ext);
|
||||
|
||||
let size = data.len() as u32;
|
||||
let offset = fec.heap.allocate(size);
|
||||
memory.set(offset, &data).expect("heap always gives a sensible offset to write");
|
||||
|
||||
let returned = program
|
||||
.params_with_external("env", &mut fec)
|
||||
.map(|p| p
|
||||
.add_argument(I32(offset as i32))
|
||||
.add_argument(I32(size as i32)))
|
||||
.and_then(|p| module.execute_export(method, p))
|
||||
.map_err(|_| -> Error {
|
||||
ErrorKind::Runtime.into()
|
||||
})?;
|
||||
|
||||
if let Some(I64(r)) = returned {
|
||||
let offset = r as u32;
|
||||
let length = (r >> 32) as u32 as usize;
|
||||
memory.get(offset, length)
|
||||
.map_err(|_| ErrorKind::Runtime.into())
|
||||
.map(|v| { println!("Returned {}", HexDisplay::from(&v)); v })
|
||||
} else {
|
||||
Err(ErrorKind::InvalidReturn.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rustc_hex::FromHex;
|
||||
use codec::{Slicable, Joiner};
|
||||
use state_machine::TestExternalities;
|
||||
use primitives::Header;
|
||||
|
||||
#[test]
|
||||
fn returning_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
|
||||
let output = WasmExecutor.call(&mut ext, &test_code[..], "test_empty_return", &[]).unwrap();
|
||||
assert_eq!(output, vec![0u8; 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn panicking_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
|
||||
let output = WasmExecutor.call(&mut ext, &test_code[..], "test_panic", &[]);
|
||||
assert!(output.is_err());
|
||||
|
||||
let output = WasmExecutor.call(&mut ext, &test_code[..], "test_conditional_panic", &[2]);
|
||||
assert!(output.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
ext.set_storage(b"foo".to_vec(), b"bar".to_vec());
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
|
||||
let output = WasmExecutor.call(&mut ext, &test_code[..], "test_data_in", b"Hello world").unwrap();
|
||||
|
||||
assert_eq!(output, b"all ok!".to_vec());
|
||||
|
||||
let expected: HashMap<_, _> = map![
|
||||
b"input".to_vec() => b"Hello world".to_vec(),
|
||||
b"foo".to_vec() => b"bar".to_vec(),
|
||||
b"baz".to_vec() => b"bar".to_vec()
|
||||
];
|
||||
assert_eq!(expected, ext.storage);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blake2_256_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
assert_eq!(
|
||||
WasmExecutor.call(&mut ext, &test_code[..], "test_blake2_256", &[]).unwrap(),
|
||||
blake2_256(&b""[..]).to_vec()
|
||||
);
|
||||
assert_eq!(
|
||||
WasmExecutor.call(&mut ext, &test_code[..], "test_blake2_256", b"Hello world!").unwrap(),
|
||||
blake2_256(&b"Hello world!"[..]).to_vec()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twox_256_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
assert_eq!(
|
||||
WasmExecutor.call(&mut ext, &test_code[..], "test_twox_256", &[]).unwrap(),
|
||||
FromHex::from_hex("99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203a").unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
WasmExecutor.call(&mut ext, &test_code[..], "test_twox_256", b"Hello world!").unwrap(),
|
||||
FromHex::from_hex("b27dfd7f223f177f2a13647b533599af0c07f68bda23d96d059da2b451a35a74").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twox_128_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
assert_eq!(
|
||||
WasmExecutor.call(&mut ext, &test_code[..], "test_twox_128", &[]).unwrap(),
|
||||
FromHex::from_hex("99e9d85137db46ef4bbea33613baafd5").unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
WasmExecutor.call(&mut ext, &test_code[..], "test_twox_128", b"Hello world!").unwrap(),
|
||||
FromHex::from_hex("b27dfd7f223f177f2a13647b533599af").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ed25519_verify_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let key = ::ed25519::Pair::from_seed(&blake2_256(b"test"));
|
||||
let sig = key.sign(b"all ok!");
|
||||
let mut calldata = vec![];
|
||||
calldata.extend_from_slice(key.public().as_ref());
|
||||
calldata.extend_from_slice(sig.as_ref());
|
||||
assert_eq!(
|
||||
WasmExecutor.call(&mut ext, &test_code[..], "test_ed25519_verify", &calldata).unwrap(),
|
||||
vec![0]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enumerated_trie_root_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
assert_eq!(
|
||||
WasmExecutor.call(&mut ext, &test_code[..], "test_enumerated_trie_root", &[]).unwrap(),
|
||||
ordered_trie_root(vec![b"zero".to_vec(), b"one".to_vec(), b"two".to_vec()]).0.to_vec()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Rust implementation of Substrate contracts.
|
||||
|
||||
use std::sync::{Arc};
|
||||
use std::collections::HashMap;
|
||||
pub use std::result;
|
||||
pub use parity_wasm::builder;
|
||||
pub use parity_wasm::elements::{ValueType, Module};
|
||||
pub use parity_wasm::interpreter::{RuntimeValue, UserFunctionDescriptor, UserFunctionExecutor,
|
||||
UserDefinedElements, env_native_module, DummyUserError, ExecutionParams, UserError};
|
||||
use parity_wasm::interpreter;
|
||||
|
||||
pub type Error = interpreter::Error<DummyUserError>;
|
||||
pub type MemoryInstance = interpreter::MemoryInstance<DummyUserError>;
|
||||
pub type CallerContext<'a> = interpreter::CallerContext<'a, DummyUserError>;
|
||||
|
||||
pub trait ConvertibleToWasm { const VALUE_TYPE: ValueType; type NativeType; fn to_runtime_value(self) -> RuntimeValue; }
|
||||
impl ConvertibleToWasm for i32 { type NativeType = i32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self) } }
|
||||
impl ConvertibleToWasm for u32 { type NativeType = u32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as i32) } }
|
||||
impl ConvertibleToWasm for i64 { type NativeType = i64; const VALUE_TYPE: ValueType = ValueType::I64; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I64(self) } }
|
||||
impl ConvertibleToWasm for u64 { type NativeType = u64; const VALUE_TYPE: ValueType = ValueType::I64; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I64(self as i64) } }
|
||||
impl ConvertibleToWasm for f32 { type NativeType = f32; const VALUE_TYPE: ValueType = ValueType::F32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::F32(self) } }
|
||||
impl ConvertibleToWasm for f64 { type NativeType = f64; const VALUE_TYPE: ValueType = ValueType::F64; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::F64(self) } }
|
||||
impl ConvertibleToWasm for isize { type NativeType = i32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as i32) } }
|
||||
impl ConvertibleToWasm for usize { type NativeType = u32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as u32 as i32) } }
|
||||
impl<T> ConvertibleToWasm for *const T { type NativeType = u32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as isize as i32) } }
|
||||
impl<T> ConvertibleToWasm for *mut T { type NativeType = u32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as isize as i32) } }
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! convert_args {
|
||||
() => ([]);
|
||||
( $( $t:ty ),* ) => ( [ $( { use $crate::wasm_utils::ConvertibleToWasm; <$t>::VALUE_TYPE }, )* ] );
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! convert_fn {
|
||||
( $name:ident ( $( $params:ty ),* ) ) => ( $crate::wasm_utils::UserFunctionDescriptor::Static(stringify!($name), &convert_args!($($params),*), None) );
|
||||
( $name:ident ( $( $params:ty ),* ) -> $returns:ty ) => ( $crate::wasm_utils::UserFunctionDescriptor::Static(stringify!($name), &convert_args!($($params),*), Some({ use $crate::wasm_utils::ConvertibleToWasm; <$returns>::VALUE_TYPE }) ) );
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! reverse_params {
|
||||
// Entry point, use brackets to recursively reverse above.
|
||||
($body:tt, $self:ident, $context:ident, $( $names:ident : $params:ty ),*) => (
|
||||
reverse_params!($body $self $context [ $( $names : $params ),* ]);
|
||||
);
|
||||
($body:tt $self:ident $context:ident [] $( $names:ident : $params:ty ),*) => ({
|
||||
$(
|
||||
let $names : <$params as $crate::wasm_utils::ConvertibleToWasm>::NativeType = match $context.value_stack.pop_as() {
|
||||
Ok(value) => value,
|
||||
Err(error) => return Err(error.into()),
|
||||
};
|
||||
)*
|
||||
$body
|
||||
});
|
||||
($body:tt $self:ident $context:ident [ $name:ident : $param:ty $(, $names:ident : $params:ty )* ] $( $reversed_names:ident : $reversed_params:ty ),*) => (
|
||||
reverse_params!($body $self $context [ $( $names : $params ),* ] $name : $param $( , $reversed_names : $reversed_params )*);
|
||||
);
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! marshall {
|
||||
( $context:ident, $self:ident, ( $( $names:ident : $params:ty ),* ) => $body:tt ) => ({
|
||||
reverse_params!($body, $self, $context, $( $names : $params ),*);
|
||||
Ok(None)
|
||||
});
|
||||
( $context:ident, $self:ident, ( $( $names:ident : $params:ty ),* ) -> $returns:ty => $body:tt ) => ({
|
||||
let r : <$returns as $crate::wasm_utils::ConvertibleToWasm>::NativeType = reverse_params!($body, $self, $context, $( $names : $params ),*);
|
||||
Ok(Some({ use $crate::wasm_utils::ConvertibleToWasm; r.to_runtime_value() }))
|
||||
})
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! dispatch {
|
||||
( $objectname:ident, $( $name:ident ( $( $names:ident : $params:ty ),* ) $( -> $returns:ty )* => $body:tt ),* ) => (
|
||||
fn execute(&mut self, name: &str, context: $crate::wasm_utils::CallerContext)
|
||||
-> $crate::wasm_utils::result::Result<Option<$crate::wasm_utils::RuntimeValue>, $crate::wasm_utils::Error> {
|
||||
let $objectname = self;
|
||||
match name {
|
||||
$(
|
||||
stringify!($name) => marshall!(context, $objectname, ( $( $names : $params ),* ) $( -> $returns )* => $body),
|
||||
)*
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! signatures {
|
||||
( $( $name:ident ( $( $params:ty ),* ) $( -> $returns:ty )* ),* ) => (
|
||||
const SIGNATURES: &'static [$crate::wasm_utils::UserFunctionDescriptor] = &[
|
||||
$(
|
||||
convert_fn!( $name ( $( $params ),* ) $( -> $returns )* ),
|
||||
)*
|
||||
];
|
||||
);
|
||||
}
|
||||
|
||||
pub trait IntoUserDefinedElements {
|
||||
fn into_user_defined_elements(&mut self) -> UserDefinedElements<DummyUserError>;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_function_executor {
|
||||
( $objectname:ident : $structname:ty, $( $name:ident ( $( $names:ident : $params:ty ),* ) $( -> $returns:ty )* => $body:tt ),* => $($pre:tt)+ ) => (
|
||||
impl $($pre)+ $crate::wasm_utils::UserFunctionExecutor<$crate::wasm_utils::DummyUserError> for $structname {
|
||||
dispatch!($objectname, $( $name( $( $names : $params ),* ) $( -> $returns )* => $body ),*);
|
||||
}
|
||||
impl $($pre)+ $structname {
|
||||
signatures!($( $name( $( $params ),* ) $( -> $returns )* ),*);
|
||||
}
|
||||
impl $($pre)+ $crate::wasm_utils::IntoUserDefinedElements for $structname {
|
||||
fn into_user_defined_elements(&mut self) -> UserDefinedElements<$crate::wasm_utils::DummyUserError> {
|
||||
$crate::wasm_utils::UserDefinedElements {
|
||||
executor: Some(self),
|
||||
globals: HashMap::new(), // TODO: provide
|
||||
functions: ::std::borrow::Cow::from(Self::SIGNATURES),
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct DummyUserFunctionExecutor;
|
||||
impl<E: UserError> interpreter::UserFunctionExecutor<E> for DummyUserFunctionExecutor {
|
||||
fn execute(&mut self, _name: &str, _context: interpreter::CallerContext<E>) ->
|
||||
result::Result<Option<interpreter::RuntimeValue>, interpreter::Error<E>>
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AddModuleWithoutFullDependentInstance {
|
||||
fn add_module_by_sigs(
|
||||
&self,
|
||||
name: &str,
|
||||
module: Module,
|
||||
functions: HashMap<&str, &'static [UserFunctionDescriptor]>,
|
||||
) -> result::Result<Arc<interpreter::ModuleInstance<DummyUserError>>, interpreter::Error<DummyUserError>>;
|
||||
|
||||
fn params_with_external<'a, 'b: 'a>(&'b self, externals_name: &str, externals: &'a mut IntoUserDefinedElements) -> result::Result<ExecutionParams<'a, DummyUserError>, Error>;
|
||||
}
|
||||
|
||||
impl AddModuleWithoutFullDependentInstance for interpreter::ProgramInstance<DummyUserError> {
|
||||
fn add_module_by_sigs(
|
||||
&self,
|
||||
name: &str,
|
||||
module: Module,
|
||||
functions: HashMap<&str, &'static [UserFunctionDescriptor]>
|
||||
) -> result::Result<Arc<interpreter::ModuleInstance<DummyUserError>>, interpreter::Error<DummyUserError>> {
|
||||
let mut dufe = vec![DummyUserFunctionExecutor; functions.len()];
|
||||
let dufe_refs = dufe.iter_mut().collect::<Vec<_>>();
|
||||
let fake_module_map = functions.into_iter()
|
||||
.zip(dufe_refs.into_iter())
|
||||
.map(|((dep_mod_name, functions), dufe)| -> result::Result<_, interpreter::Error<DummyUserError>> {
|
||||
let fake_module = Arc::new(
|
||||
interpreter::env_native_module(
|
||||
self.module(dep_mod_name).ok_or(DummyUserError)?, UserDefinedElements {
|
||||
executor: Some(dufe),
|
||||
globals: HashMap::new(),
|
||||
functions: ::std::borrow::Cow::from(functions),
|
||||
}
|
||||
)?
|
||||
);
|
||||
let fake_module: Arc<interpreter::ModuleInstanceInterface<_>> = fake_module;
|
||||
Ok((dep_mod_name.into(), fake_module))
|
||||
})
|
||||
.collect::<result::Result<HashMap<_, _>, interpreter::Error<DummyUserError>>>()?;
|
||||
self.add_module(name, module, Some(&fake_module_map))
|
||||
}
|
||||
|
||||
fn params_with_external<'a, 'b: 'a>(&'b self, externals_name: &str, externals: &'a mut IntoUserDefinedElements) -> result::Result<ExecutionParams<'a, DummyUserError>, Error> {
|
||||
Ok(interpreter::ExecutionParams::with_external(
|
||||
externals_name.into(),
|
||||
Arc::new(
|
||||
interpreter::env_native_module(
|
||||
self.module(externals_name).ok_or(DummyUserError)?,
|
||||
externals.into_user_defined_elements()
|
||||
)?
|
||||
)
|
||||
))
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "crunchy"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "fixed-hash"
|
||||
version = "0.1.3"
|
||||
source = "git+https://github.com/rphmeier/primitives.git?branch=compile-for-wasm#8dc457899afdaf968ff7f16140b03d1e37b01d71"
|
||||
|
||||
[[package]]
|
||||
name = "pwasm-alloc"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"pwasm-libc 0.1.0",
|
||||
"rustc_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pwasm-libc"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "runtime-test"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"substrate-runtime-io 0.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hex"
|
||||
version = "2.0.0"
|
||||
source = "git+https://github.com/rphmeier/rustc-hex.git#ee2ec40b9062ac7769ccb9dc891d6dc2cc9009d7"
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver-parser"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "substrate-codec"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"substrate-runtime-std 0.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "substrate-primitives"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fixed-hash 0.1.3 (git+https://github.com/rphmeier/primitives.git?branch=compile-for-wasm)",
|
||||
"rustc-hex 2.0.0 (git+https://github.com/rphmeier/rustc-hex.git)",
|
||||
"serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"substrate-codec 0.1.0",
|
||||
"substrate-runtime-std 0.1.0",
|
||||
"uint 0.1.2 (git+https://github.com/rphmeier/primitives.git?branch=compile-for-wasm)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "substrate-runtime-io"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rustc_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"substrate-codec 0.1.0",
|
||||
"substrate-primitives 0.1.0",
|
||||
"substrate-runtime-std 0.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "substrate-runtime-std"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"pwasm-alloc 0.1.0",
|
||||
"pwasm-libc 0.1.0",
|
||||
"rustc_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "uint"
|
||||
version = "0.1.2"
|
||||
source = "git+https://github.com/rphmeier/primitives.git?branch=compile-for-wasm#8dc457899afdaf968ff7f16140b03d1e37b01d71"
|
||||
dependencies = [
|
||||
"byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[metadata]
|
||||
"checksum byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "652805b7e73fada9d85e9a6682a4abd490cb52d96aeecc12e33a0de34dfd0d23"
|
||||
"checksum crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a2f4a431c5c9f662e1200b7c7f02c34e91361150e382089a8f2dec3ba680cbda"
|
||||
"checksum fixed-hash 0.1.3 (git+https://github.com/rphmeier/primitives.git?branch=compile-for-wasm)" = "<none>"
|
||||
"checksum rustc-hex 2.0.0 (git+https://github.com/rphmeier/rustc-hex.git)" = "<none>"
|
||||
"checksum rustc_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b9743a7670d88d5d52950408ecdb7c71d8986251ab604d4689dd2ca25c9bca69"
|
||||
"checksum semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a3186ec9e65071a2095434b1f5bb24838d4e8e130f584c790f6033c79943537"
|
||||
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
|
||||
"checksum serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "db99f3919e20faa51bb2996057f5031d8685019b5a06139b1ce761da671b8526"
|
||||
"checksum uint 0.1.2 (git+https://github.com/rphmeier/primitives.git?branch=compile-for-wasm)" = "<none>"
|
||||
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "runtime-test"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
substrate-runtime-io = { path = "../../runtime-io", version = "0.1", default_features = false }
|
||||
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
|
||||
[workspace]
|
||||
members = []
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
cargo +nightly build --target=wasm32-unknown-unknown --release
|
||||
for i in test
|
||||
do
|
||||
wasm-gc target/wasm32-unknown-unknown/release/runtime_$i.wasm target/wasm32-unknown-unknown/release/runtime_$i.compact.wasm
|
||||
done
|
||||
@@ -0,0 +1,69 @@
|
||||
#![no_std]
|
||||
#![feature(lang_items)]
|
||||
#![cfg_attr(feature = "strict", deny(warnings))]
|
||||
|
||||
#![feature(alloc)]
|
||||
extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
#[macro_use]
|
||||
extern crate substrate_runtime_io as runtime_io;
|
||||
use runtime_io::{
|
||||
set_storage, storage, print, blake2_256,
|
||||
twox_128, twox_256, ed25519_verify, enumerated_trie_root
|
||||
};
|
||||
|
||||
fn test_blake2_256(input: &[u8]) -> Vec<u8> {
|
||||
blake2_256(&input).to_vec()
|
||||
}
|
||||
|
||||
fn test_twox_256(input: &[u8]) -> Vec<u8> {
|
||||
twox_256(&input).to_vec()
|
||||
}
|
||||
|
||||
fn test_twox_128(input: &[u8]) -> Vec<u8> {
|
||||
twox_128(&input).to_vec()
|
||||
}
|
||||
|
||||
fn test_ed25519_verify(input: &[u8]) -> Vec<u8> {
|
||||
let sig = &input[0..64];
|
||||
let pubkey = &input[64..96];
|
||||
let msg = b"all ok!";
|
||||
[ed25519_verify(sig, &msg[..], pubkey) as u8].to_vec()
|
||||
}
|
||||
|
||||
fn test_enumerated_trie_root(_input: &[u8]) -> Vec<u8> {
|
||||
enumerated_trie_root(&[&b"zero"[..], &b"one"[..], &b"two"[..]]).to_vec()
|
||||
}
|
||||
|
||||
fn test_data_in(input: &[u8]) -> Vec<u8> {
|
||||
print("set_storage");
|
||||
set_storage(b"input", &input);
|
||||
|
||||
print("storage");
|
||||
let foo = storage(b"foo");
|
||||
|
||||
print("set_storage");
|
||||
set_storage(b"baz", &foo);
|
||||
|
||||
print("finished!");
|
||||
b"all ok!".to_vec()
|
||||
}
|
||||
|
||||
fn test_empty_return(_input: &[u8]) -> Vec<u8> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn test_panic(_input: &[u8]) -> Vec<u8> {
|
||||
panic!("test panic");
|
||||
}
|
||||
|
||||
fn test_conditional_panic(input: &[u8]) -> Vec<u8> {
|
||||
if input.len() > 0 {
|
||||
panic!("test panic");
|
||||
}
|
||||
input.to_vec()
|
||||
}
|
||||
|
||||
impl_stubs!(test_data_in, test_empty_return, test_panic, test_conditional_panic,
|
||||
test_blake2_256, test_twox_256, test_twox_128, test_ed25519_verify, test_enumerated_trie_root);
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,36 @@
|
||||
[package]
|
||||
name = "substrate-primitives"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
crunchy = "0.1"
|
||||
substrate-runtime-std = { path = "../runtime-std", default_features = false }
|
||||
substrate-codec = { path = "../codec", default_features = false }
|
||||
fixed-hash = { git = "https://github.com/rphmeier/primitives.git", branch = "compile-for-wasm", default_features = false }
|
||||
rustc-hex = { git = "https://github.com/rphmeier/rustc-hex.git", version = "2.0", default_features = false }
|
||||
serde = { version = "1.0", default_features = false }
|
||||
serde_derive = { version = "1.0", optional = true }
|
||||
uint = { git = "https://github.com/rphmeier/primitives.git", branch = "compile-for-wasm", default_features = false }
|
||||
twox-hash = { version = "1.1.0", optional = true }
|
||||
byteorder = { version = "1.1", default_features = false }
|
||||
blake2-rfc = { version = "0.2.18", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
substrate-serializer = { path = "../serializer" }
|
||||
pretty_assertions = "0.4"
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"uint/std",
|
||||
"fixed-hash/std",
|
||||
"substrate-codec/std",
|
||||
"substrate-runtime-std/std",
|
||||
"serde/std",
|
||||
"rustc-hex/std",
|
||||
"twox-hash",
|
||||
"blake2-rfc",
|
||||
"serde_derive",
|
||||
"byteorder/std"
|
||||
]
|
||||
@@ -0,0 +1,213 @@
|
||||
// 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/>.
|
||||
|
||||
//! Block and header type definitions.
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use bytes;
|
||||
use rstd::vec::Vec;
|
||||
use codec::Slicable;
|
||||
use hash::H256;
|
||||
|
||||
/// Used to refer to a block number.
|
||||
pub type Number = u64;
|
||||
|
||||
/// Hash used to refer to a block hash.
|
||||
pub type HeaderHash = H256;
|
||||
|
||||
/// Hash used to refer to a transaction hash.
|
||||
pub type TransactionHash = H256;
|
||||
|
||||
/// Simple generic transaction type.
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||
pub struct Transaction(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
|
||||
|
||||
impl Slicable for Transaction {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
Vec::<u8>::from_slice(value).map(Transaction)
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
self.0.as_slice_then(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::codec::NonTrivialSlicable for Transaction { }
|
||||
|
||||
/// Execution log (event)
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||
pub struct Log(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
|
||||
|
||||
impl Slicable for Log {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
Vec::<u8>::from_slice(value).map(Log)
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
self.0.as_slice_then(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::codec::NonTrivialSlicable for Log { }
|
||||
|
||||
/// The digest of a block, useful for light-clients.
|
||||
#[derive(Clone, Default, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||
pub struct Digest {
|
||||
/// All logs that have happened in the block.
|
||||
pub logs: Vec<Log>,
|
||||
}
|
||||
|
||||
impl Slicable for Digest {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
Vec::<Log>::from_slice(value).map(|logs| Digest { logs })
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
self.logs.as_slice_then(f)
|
||||
}
|
||||
}
|
||||
|
||||
/// The body of a block is just a bunch of transactions.
|
||||
pub type Body = Vec<Transaction>;
|
||||
|
||||
/// A Substrate relay chain block.
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||
pub struct Block {
|
||||
/// The block header.
|
||||
pub header: Header,
|
||||
/// All relay-chain transactions.
|
||||
pub transactions: Body,
|
||||
}
|
||||
|
||||
impl Slicable for Block {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
Some(Block {
|
||||
header: try_opt!(Slicable::from_slice(value)),
|
||||
transactions: try_opt!(Slicable::from_slice(value)),
|
||||
})
|
||||
}
|
||||
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
let mut v = Vec::new();
|
||||
|
||||
v.extend(self.header.to_vec());
|
||||
v.extend(self.transactions.to_vec());
|
||||
|
||||
v
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
f(self.to_vec().as_slice())
|
||||
}
|
||||
}
|
||||
|
||||
/// A relay chain block header.
|
||||
///
|
||||
/// https://github.com/w3f/polkadot-spec/blob/master/spec.md#header
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
|
||||
#[cfg_attr(feature = "std", serde(deny_unknown_fields))]
|
||||
pub struct Header {
|
||||
/// Block parent's hash.
|
||||
pub parent_hash: HeaderHash,
|
||||
/// Block number.
|
||||
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,
|
||||
/// The digest of activity on the block.
|
||||
pub digest: Digest,
|
||||
}
|
||||
|
||||
impl Header {
|
||||
/// Create a new instance with default fields except `number`, which is given as an argument.
|
||||
pub fn from_block_number(number: Number) -> Self {
|
||||
Header {
|
||||
parent_hash: Default::default(),
|
||||
number,
|
||||
state_root: Default::default(),
|
||||
transaction_root: Default::default(),
|
||||
digest: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Slicable for Header {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
Some(Header {
|
||||
parent_hash: try_opt!(Slicable::from_slice(value)),
|
||||
number: try_opt!(Slicable::from_slice(value)),
|
||||
state_root: try_opt!(Slicable::from_slice(value)),
|
||||
transaction_root: try_opt!(Slicable::from_slice(value)),
|
||||
digest: try_opt!(Slicable::from_slice(value)),
|
||||
})
|
||||
}
|
||||
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
let mut v = Vec::new();
|
||||
|
||||
self.parent_hash.as_slice_then(|s| v.extend(s));
|
||||
self.number.as_slice_then(|s| v.extend(s));
|
||||
self.state_root.as_slice_then(|s| v.extend(s));
|
||||
self.transaction_root.as_slice_then(|s| v.extend(s));
|
||||
self.digest.as_slice_then(|s| v.extend(s));
|
||||
|
||||
v
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
f(self.to_vec().as_slice())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use codec::Slicable;
|
||||
use substrate_serializer as ser;
|
||||
|
||||
#[test]
|
||||
fn test_header_serialization() {
|
||||
let header = Header {
|
||||
parent_hash: 5.into(),
|
||||
number: 67,
|
||||
state_root: 3.into(),
|
||||
transaction_root: 6.into(),
|
||||
digest: Digest { logs: vec![Log(vec![1])] },
|
||||
};
|
||||
|
||||
assert_eq!(ser::to_string_pretty(&header), r#"{
|
||||
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000005",
|
||||
"number": 67,
|
||||
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000003",
|
||||
"transactionRoot": "0x0000000000000000000000000000000000000000000000000000000000000006",
|
||||
"digest": {
|
||||
"logs": [
|
||||
"0x01"
|
||||
]
|
||||
}
|
||||
}"#);
|
||||
|
||||
let v = header.to_vec();
|
||||
assert_eq!(Header::from_slice(&mut &v[..]).unwrap(), header);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
// 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/>.
|
||||
|
||||
//! Simply type for representing Vec<u8> with regards to serde.
|
||||
|
||||
use core::fmt;
|
||||
|
||||
use serde::{de, Serializer, Deserializer};
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
mod alloc_types {
|
||||
pub use ::alloc::string::String;
|
||||
pub use ::alloc::vec::Vec;
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod alloc_types {
|
||||
pub use ::std::vec::Vec;
|
||||
pub use ::std::string::String;
|
||||
}
|
||||
|
||||
pub use self::alloc_types::*;
|
||||
|
||||
/// Serializes a slice of bytes.
|
||||
pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> where
|
||||
S: Serializer,
|
||||
{
|
||||
let hex: String = ::rustc_hex::ToHex::to_hex(bytes);
|
||||
serializer.serialize_str(&format!("0x{}", hex))
|
||||
}
|
||||
|
||||
/// Serialize a slice of bytes as uint.
|
||||
///
|
||||
/// The representation will have all leading zeros trimmed.
|
||||
pub fn serialize_uint<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> where
|
||||
S: Serializer,
|
||||
{
|
||||
let non_zero = bytes.iter().take_while(|b| **b == 0).count();
|
||||
let bytes = &bytes[non_zero..];
|
||||
if bytes.is_empty() {
|
||||
return serializer.serialize_str("0x0");
|
||||
}
|
||||
|
||||
let hex: String = ::rustc_hex::ToHex::to_hex(bytes);
|
||||
let has_leading_zero = !hex.is_empty() && &hex[0..1] == "0";
|
||||
serializer.serialize_str(
|
||||
&format!("0x{}", if has_leading_zero { &hex[1..] } else { &hex })
|
||||
)
|
||||
}
|
||||
|
||||
/// Expected length of bytes vector.
|
||||
#[derive(PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub enum ExpectedLen {
|
||||
/// Any length in bytes.
|
||||
#[cfg_attr(not(feature = "std"), allow(unused))]
|
||||
Any,
|
||||
/// Exact length in bytes.
|
||||
Exact(usize),
|
||||
/// A bytes length between (min; max].
|
||||
Between(usize, usize),
|
||||
}
|
||||
|
||||
impl fmt::Display for ExpectedLen {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
ExpectedLen::Any => write!(fmt, "even length"),
|
||||
ExpectedLen::Exact(v) => write!(fmt, "length of {}", v * 2),
|
||||
ExpectedLen::Between(min, max) => write!(fmt, "length between ({}; {}]", min * 2, max * 2),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Deserialize into vector of bytes.
|
||||
#[cfg(feature = "std")]
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error> where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserialize_check_len(deserializer, ExpectedLen::Any)
|
||||
}
|
||||
|
||||
/// Deserialize into vector of bytes with additional size check.
|
||||
pub fn deserialize_check_len<'de, D>(deserializer: D, len: ExpectedLen) -> Result<Vec<u8>, D::Error> where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct Visitor {
|
||||
len: ExpectedLen,
|
||||
}
|
||||
|
||||
impl<'a> de::Visitor<'a> for Visitor {
|
||||
type Value = Vec<u8>;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "a 0x-prefixed hex string with {}", self.len)
|
||||
}
|
||||
|
||||
fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
|
||||
if v.len() < 2 || &v[0..2] != "0x" {
|
||||
return Err(E::custom("prefix is missing"))
|
||||
}
|
||||
|
||||
let is_len_valid = match self.len {
|
||||
// just make sure that we have all nibbles
|
||||
ExpectedLen::Any => v.len() % 2 == 0,
|
||||
ExpectedLen::Exact(len) => v.len() == 2 * len + 2,
|
||||
ExpectedLen::Between(min, max) => v.len() <= 2 * max + 2 && v.len() > 2 * min + 2,
|
||||
};
|
||||
|
||||
if !is_len_valid {
|
||||
return Err(E::invalid_length(v.len() - 2, &self))
|
||||
}
|
||||
|
||||
let bytes = match self.len {
|
||||
ExpectedLen::Between(..) if v.len() % 2 != 0 => {
|
||||
::rustc_hex::FromHex::from_hex(&*format!("0{}", &v[2..]))
|
||||
},
|
||||
_ => ::rustc_hex::FromHex::from_hex(&v[2..])
|
||||
};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn format_err(e: ::rustc_hex::FromHexError) -> String {
|
||||
format!("invalid hex value: {:?}", e)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
fn format_err(e: ::rustc_hex::FromHexError) -> String {
|
||||
match e {
|
||||
::rustc_hex::InvalidHexLength => format!("invalid hex value: invalid length"),
|
||||
::rustc_hex::InvalidHexCharacter(c, p) =>
|
||||
format!("invalid hex value: invalid character {} at position {}", c, p),
|
||||
}
|
||||
}
|
||||
|
||||
bytes.map_err(|e| E::custom(format_err(e)))
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
|
||||
self.visit_str(&v)
|
||||
}
|
||||
}
|
||||
// TODO [ToDr] Use raw bytes if we switch to RLP / binencoding
|
||||
// (visit_bytes, visit_bytes_buf)
|
||||
deserializer.deserialize_str(Visitor { len })
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
// 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/>.
|
||||
|
||||
//! A fixed hash type.
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use bytes;
|
||||
|
||||
macro_rules! impl_rest {
|
||||
($name: ident, $len: expr) => {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for $name {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||
bytes::serialize(&self.0, serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for $name {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
|
||||
bytes::deserialize_check_len(deserializer, bytes::ExpectedLen::Exact($len))
|
||||
.map(|x| (&*x).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl ::codec::Slicable for $name {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
<[u8; $len] as ::codec::Slicable>::from_slice(value).map($name)
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
self.0.as_slice_then(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
construct_hash!(H160, 20);
|
||||
construct_hash!(H256, 32);
|
||||
construct_hash!(H512, 64);
|
||||
impl_rest!(H160, 20);
|
||||
impl_rest!(H256, 32);
|
||||
impl_rest!(H512, 64);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use substrate_serializer as ser;
|
||||
|
||||
#[test]
|
||||
fn test_h160() {
|
||||
let tests = vec![
|
||||
(H160::from(0), "0x0000000000000000000000000000000000000000"),
|
||||
(H160::from(2), "0x0000000000000000000000000000000000000002"),
|
||||
(H160::from(15), "0x000000000000000000000000000000000000000f"),
|
||||
(H160::from(16), "0x0000000000000000000000000000000000000010"),
|
||||
(H160::from(1_000), "0x00000000000000000000000000000000000003e8"),
|
||||
(H160::from(100_000), "0x00000000000000000000000000000000000186a0"),
|
||||
(H160::from(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"),
|
||||
];
|
||||
|
||||
for (number, expected) in tests {
|
||||
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number));
|
||||
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_h256() {
|
||||
let tests = vec![
|
||||
(H256::from(0), "0x0000000000000000000000000000000000000000000000000000000000000000"),
|
||||
(H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"),
|
||||
(H256::from(15), "0x000000000000000000000000000000000000000000000000000000000000000f"),
|
||||
(H256::from(16), "0x0000000000000000000000000000000000000000000000000000000000000010"),
|
||||
(H256::from(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"),
|
||||
(H256::from(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"),
|
||||
(H256::from(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"),
|
||||
];
|
||||
|
||||
for (number, expected) in tests {
|
||||
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number));
|
||||
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid() {
|
||||
assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
|
||||
assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000g\"").unwrap_err().is_data());
|
||||
assert!(ser::from_str::<H256>("\"0x00000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
|
||||
assert!(ser::from_str::<H256>("\"\"").unwrap_err().is_data());
|
||||
assert!(ser::from_str::<H256>("\"0\"").unwrap_err().is_data());
|
||||
assert!(ser::from_str::<H256>("\"10\"").unwrap_err().is_data());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
// 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/>.
|
||||
|
||||
//! Hashing functions.
|
||||
|
||||
use blake2_rfc;
|
||||
use twox_hash;
|
||||
|
||||
/// Do a Blake2 512-bit hash and place result in `dest`.
|
||||
pub fn blake2_512_into(data: &[u8], dest: &mut [u8; 64]) {
|
||||
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(64, &[], data).as_bytes());
|
||||
}
|
||||
|
||||
/// Do a Blake2 512-bit hash and return result.
|
||||
pub fn blake2_512(data: &[u8]) -> [u8; 64] {
|
||||
let mut r = [0; 64];
|
||||
blake2_512_into(data, &mut r);
|
||||
r
|
||||
}
|
||||
|
||||
/// Do a Blake2 256-bit hash and place result in `dest`.
|
||||
pub fn blake2_256_into(data: &[u8], dest: &mut [u8; 32]) {
|
||||
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(32, &[], data).as_bytes());
|
||||
}
|
||||
|
||||
/// Do a Blake2 256-bit hash and return result.
|
||||
pub fn blake2_256(data: &[u8]) -> [u8; 32] {
|
||||
let mut r = [0; 32];
|
||||
blake2_256_into(data, &mut r);
|
||||
r
|
||||
}
|
||||
|
||||
/// Do a Blake2 128-bit hash and place result in `dest`.
|
||||
pub fn blake2_128_into(data: &[u8], dest: &mut [u8; 16]) {
|
||||
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(16, &[], data).as_bytes());
|
||||
}
|
||||
|
||||
/// Do a Blake2 128-bit hash and return result.
|
||||
pub fn blake2_128(data: &[u8]) -> [u8; 16] {
|
||||
let mut r = [0; 16];
|
||||
blake2_128_into(data, &mut r);
|
||||
r
|
||||
}
|
||||
|
||||
/// Do a XX 128-bit hash and place result in `dest`.
|
||||
pub fn twox_128_into(data: &[u8], dest: &mut [u8; 16]) {
|
||||
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);
|
||||
h1.write(data);
|
||||
let r0 = h0.finish();
|
||||
let r1 = h1.finish();
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
LittleEndian::write_u64(&mut dest[0..8], r0);
|
||||
LittleEndian::write_u64(&mut dest[8..16], r1);
|
||||
}
|
||||
|
||||
/// Do a XX 128-bit hash and return result.
|
||||
pub fn twox_128(data: &[u8]) -> [u8; 16] {
|
||||
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 ::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);
|
||||
let mut h2 = twox_hash::XxHash::with_seed(2);
|
||||
let mut h3 = twox_hash::XxHash::with_seed(3);
|
||||
h0.write(data);
|
||||
h1.write(data);
|
||||
h2.write(data);
|
||||
h3.write(data);
|
||||
let r0 = h0.finish();
|
||||
let r1 = h1.finish();
|
||||
let r2 = h2.finish();
|
||||
let r3 = h3.finish();
|
||||
LittleEndian::write_u64(&mut dest[0..8], r0);
|
||||
LittleEndian::write_u64(&mut dest[8..16], r1);
|
||||
LittleEndian::write_u64(&mut dest[16..24], r2);
|
||||
LittleEndian::write_u64(&mut dest[24..32], r3);
|
||||
}
|
||||
|
||||
/// Do a XX 256-bit hash and return result.
|
||||
pub fn twox_256(data: &[u8]) -> [u8; 32] {
|
||||
let mut r: [u8; 32] = [0; 32];
|
||||
twox_256_into(data, &mut r);
|
||||
r
|
||||
}
|
||||
@@ -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/>.
|
||||
|
||||
//! Wrapper type for byte collections that outputs hex.
|
||||
|
||||
/// Simple wrapper to display hex representation of bytes.
|
||||
pub struct HexDisplay<'a>(&'a [u8]);
|
||||
|
||||
impl<'a> HexDisplay<'a> {
|
||||
/// Create new instance that will display `d` as a hex string when displayed.
|
||||
pub fn from(d: &'a AsBytesRef) -> Self { HexDisplay(d.as_bytes_ref()) }
|
||||
}
|
||||
|
||||
impl<'a> ::core::fmt::Display for HexDisplay<'a> {
|
||||
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)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Simple trait to transferm various types to `&[u8]`
|
||||
pub trait AsBytesRef {
|
||||
/// Transferm `self` into `&[u8]`.
|
||||
fn as_bytes_ref(&self) -> &[u8];
|
||||
}
|
||||
|
||||
impl<'a> AsBytesRef for &'a [u8] {
|
||||
fn as_bytes_ref(&self) -> &[u8] { self }
|
||||
}
|
||||
|
||||
impl AsBytesRef for [u8] {
|
||||
fn as_bytes_ref(&self) -> &[u8] { &self }
|
||||
}
|
||||
|
||||
impl AsBytesRef for ::bytes::Vec<u8> {
|
||||
fn as_bytes_ref(&self) -> &[u8] { &self }
|
||||
}
|
||||
|
||||
macro_rules! impl_non_endians {
|
||||
( $( $t:ty ),* ) => { $(
|
||||
impl AsBytesRef for $t {
|
||||
fn as_bytes_ref(&self) -> &[u8] { &self[..] }
|
||||
}
|
||||
)* }
|
||||
}
|
||||
|
||||
impl_non_endians!([u8; 1], [u8; 2], [u8; 3], [u8; 4], [u8; 5], [u8; 6], [u8; 7], [u8; 8],
|
||||
[u8; 10], [u8; 12], [u8; 14], [u8; 16], [u8; 20], [u8; 24], [u8; 28], [u8; 32], [u8; 40],
|
||||
[u8; 48], [u8; 56], [u8; 64], [u8; 80], [u8; 96], [u8; 112], [u8; 128]);
|
||||
@@ -0,0 +1,88 @@
|
||||
// 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/>.
|
||||
|
||||
//! Shareable Polkadot types.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(not(feature = "std"), feature(alloc))]
|
||||
|
||||
extern crate rustc_hex;
|
||||
extern crate byteorder;
|
||||
#[macro_use]
|
||||
extern crate crunchy;
|
||||
#[macro_use]
|
||||
extern crate fixed_hash;
|
||||
#[macro_use]
|
||||
extern crate uint as uint_crate;
|
||||
extern crate substrate_codec as codec;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
extern crate serde;
|
||||
#[cfg(feature = "std")]
|
||||
extern crate twox_hash;
|
||||
#[cfg(feature = "std")]
|
||||
extern crate blake2_rfc;
|
||||
#[cfg(feature = "std")]
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
#[cfg(feature = "std")]
|
||||
extern crate core;
|
||||
|
||||
extern crate substrate_runtime_std as rstd;
|
||||
|
||||
#[cfg(test)]
|
||||
extern crate substrate_serializer;
|
||||
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
extern crate pretty_assertions;
|
||||
|
||||
// TODO: factor out to separate crate.
|
||||
macro_rules! try_opt {
|
||||
($e: expr) => {
|
||||
match $e {
|
||||
Some(x) => x,
|
||||
None => return None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub mod bytes;
|
||||
#[cfg(feature = "std")]
|
||||
pub mod hashing;
|
||||
#[cfg(feature = "std")]
|
||||
pub use hashing::{blake2_256, twox_128, twox_256};
|
||||
#[cfg(feature = "std")]
|
||||
pub mod hexdisplay;
|
||||
|
||||
pub mod storage;
|
||||
pub mod hash;
|
||||
pub mod uint;
|
||||
pub mod block;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub use self::hash::{H160, H256};
|
||||
pub use self::uint::{U256, U512};
|
||||
|
||||
pub use block::{Block, Header};
|
||||
|
||||
/// An identifier for an authority in the consensus algorithm. The same as ed25519::Public.
|
||||
pub type AuthorityId = [u8; 32];
|
||||
@@ -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/>.
|
||||
|
||||
//! Contract execution data.
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use bytes;
|
||||
use rstd::vec::Vec;
|
||||
|
||||
/// Contract storage key.
|
||||
#[derive(PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||
pub struct StorageKey(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
|
||||
|
||||
/// Contract storage entry data.
|
||||
#[derive(PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||
pub struct StorageData(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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/>.
|
||||
|
||||
//! Tests.
|
||||
@@ -0,0 +1,102 @@
|
||||
// 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/>.
|
||||
|
||||
//! An unsigned fixed-size integer.
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use bytes;
|
||||
|
||||
macro_rules! impl_serde {
|
||||
($name: ident, $len: expr) => {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for $name {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||
let mut bytes = [0u8; $len * 8];
|
||||
self.to_big_endian(&mut bytes);
|
||||
bytes::serialize_uint(&bytes, serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for $name {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
|
||||
bytes::deserialize_check_len(deserializer, bytes::ExpectedLen::Between(0, $len * 8))
|
||||
.map(|x| (&*x).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
construct_uint!(U256, 4);
|
||||
construct_uint!(U512, 8);
|
||||
impl_serde!(U256, 4);
|
||||
impl_serde!(U512, 8);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use substrate_serializer as ser;
|
||||
|
||||
macro_rules! test {
|
||||
($name: ident, $test_name: ident) => {
|
||||
#[test]
|
||||
fn $test_name() {
|
||||
let tests = vec![
|
||||
($name::from(0), "0x0"),
|
||||
($name::from(1), "0x1"),
|
||||
($name::from(2), "0x2"),
|
||||
($name::from(10), "0xa"),
|
||||
($name::from(15), "0xf"),
|
||||
($name::from(15), "0xf"),
|
||||
($name::from(16), "0x10"),
|
||||
($name::from(1_000), "0x3e8"),
|
||||
($name::from(100_000), "0x186a0"),
|
||||
($name::from(u64::max_value()), "0xffffffffffffffff"),
|
||||
($name::from(u64::max_value()) + 1.into(), "0x10000000000000000"),
|
||||
];
|
||||
|
||||
for (number, expected) in tests {
|
||||
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number));
|
||||
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
|
||||
}
|
||||
|
||||
// Invalid examples
|
||||
assert!(ser::from_str::<$name>("\"0x\"").unwrap_err().is_data());
|
||||
assert!(ser::from_str::<$name>("\"0xg\"").unwrap_err().is_data());
|
||||
assert!(ser::from_str::<$name>("\"\"").unwrap_err().is_data());
|
||||
assert!(ser::from_str::<$name>("\"10\"").unwrap_err().is_data());
|
||||
assert!(ser::from_str::<$name>("\"0\"").unwrap_err().is_data());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test!(U256, test_u256);
|
||||
test!(U512, test_u512);
|
||||
|
||||
#[test]
|
||||
fn test_large_values() {
|
||||
assert_eq!(
|
||||
ser::to_string_pretty(&!U256::zero()),
|
||||
"\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\""
|
||||
);
|
||||
assert!(
|
||||
ser::from_str::<U256>("\"0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"").unwrap_err().is_data()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "pwasm-alloc"
|
||||
version = "0.1.0"
|
||||
authors = ["Nikolay Volf <nikvolf@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/paritytech/pwasm-std"
|
||||
homepage = "https://github.com/paritytech/pwasm-std"
|
||||
documentation = "https://paritytech.github.io/pwasm-std/pwasm_std/"
|
||||
description = "Parity WebAssembly standard library internal allocator"
|
||||
keywords = ["wasm", "parity", "webassembly", "blockchain"]
|
||||
categories = ["no-std", "embedded"]
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
pwasm-libc = { path = "../pwasm-libc", version = "0.1" }
|
||||
|
||||
[build-dependencies]
|
||||
rustc_version = "0.2"
|
||||
|
||||
[features]
|
||||
strict = []
|
||||
nightly = []
|
||||
@@ -0,0 +1,12 @@
|
||||
# pwasm-libc
|
||||
|
||||
Parity WASM contracts standard library libc bindings
|
||||
|
||||
[Documentation](https://paritytech.github.io/pwasm-std/pwasm_alloc/)
|
||||
|
||||
# License
|
||||
|
||||
`pwasm_alloc` is primarily distributed under the terms of both the MIT
|
||||
license and the Apache License (Version 2.0), at your choice.
|
||||
|
||||
See LICENSE-APACHE, and LICENSE-MIT for details.
|
||||
@@ -0,0 +1,14 @@
|
||||
//! Set a nightly feature
|
||||
|
||||
extern crate rustc_version;
|
||||
use rustc_version::{version, version_meta, Channel};
|
||||
|
||||
fn main() {
|
||||
// Assert we haven't travelled back in time
|
||||
assert!(version().unwrap().major >= 1);
|
||||
|
||||
// Set cfg flags depending on release channel
|
||||
if let Channel::Nightly = version_meta().unwrap().channel {
|
||||
println!("cargo:rustc-cfg=feature=\"nightly\"");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#![warn(missing_docs)]
|
||||
#![cfg_attr(feature = "strict", deny(warnings))]
|
||||
#![no_std]
|
||||
#![crate_type = "rlib"]
|
||||
#![cfg_attr(feature = "nightly", feature(global_allocator))]
|
||||
#![cfg_attr(feature = "nightly", feature(alloc))]
|
||||
#![cfg_attr(feature = "nightly", feature(allocator_api))]
|
||||
|
||||
//! Custom allocator crate for wasm
|
||||
|
||||
/// Wasm allocator
|
||||
pub struct WasmAllocator;
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
#[global_allocator]
|
||||
static ALLOCATOR: WasmAllocator = WasmAllocator;
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
mod __impl {
|
||||
extern crate alloc;
|
||||
extern crate pwasm_libc;
|
||||
|
||||
use self::alloc::heap::{Alloc, Layout, AllocErr};
|
||||
|
||||
use super::WasmAllocator;
|
||||
|
||||
unsafe impl<'a> Alloc for &'a WasmAllocator {
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
|
||||
Ok(pwasm_libc::malloc(layout.size()))
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&mut self, ptr: *mut u8, _layout: Layout) {
|
||||
pwasm_libc::free(ptr)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "pwasm-libc"
|
||||
version = "0.1.0"
|
||||
authors = ["Sergey Pepyakin <s.pepyakin@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/paritytech/pwasm-std"
|
||||
homepage = "https://github.com/paritytech/pwasm-std"
|
||||
documentation = "https://paritytech.github.io/pwasm-std/pwasm_std/"
|
||||
description = "Parity WebAssembly standard library libc bindings"
|
||||
keywords = ["wasm", "parity", "webassembly", "blockchain"]
|
||||
categories = ["no-std", "embedded"]
|
||||
|
||||
[features]
|
||||
strict = []
|
||||
@@ -0,0 +1,12 @@
|
||||
# pwasm-libc
|
||||
|
||||
Parity WASM contracts standard library libc bindings
|
||||
|
||||
[Documentation](https://paritytech.github.io/pwasm-std/pwasm_libc/)
|
||||
|
||||
# License
|
||||
|
||||
`pwasm-libc` is primarily distributed under the terms of both the MIT
|
||||
license and the Apache License (Version 2.0), at your choice.
|
||||
|
||||
See LICENSE-APACHE, and LICENSE-MIT for details.
|
||||
@@ -0,0 +1,53 @@
|
||||
#![warn(missing_docs)]
|
||||
#![cfg_attr(feature = "strict", deny(warnings))]
|
||||
#![no_std]
|
||||
|
||||
//! libc externs crate
|
||||
|
||||
extern "C" {
|
||||
fn ext_memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
|
||||
fn ext_memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
|
||||
fn ext_memset(dest: *mut u8, c: i32, n: usize) -> *mut u8;
|
||||
fn ext_memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32;
|
||||
fn ext_malloc(size: usize) -> *mut u8;
|
||||
fn ext_free(ptr: *mut u8);
|
||||
}
|
||||
|
||||
// Declaring these function here prevents Emscripten from including it's own verisons
|
||||
// into final binary.
|
||||
|
||||
/// memcpy extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
|
||||
ext_memcpy(dest, src, n)
|
||||
}
|
||||
|
||||
/// memcmp extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
|
||||
ext_memcmp(s1, s2, n)
|
||||
}
|
||||
|
||||
/// memmove extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
|
||||
ext_memmove(dest, src, n)
|
||||
}
|
||||
|
||||
/// memset extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn memset(dest: *mut u8, c: i32, n: usize) -> *mut u8 {
|
||||
ext_memset(dest, c, n)
|
||||
}
|
||||
|
||||
/// malloc extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn malloc(size: usize) -> *mut u8 {
|
||||
ext_malloc(size)
|
||||
}
|
||||
|
||||
/// free extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn free(ptr: *mut u8) {
|
||||
ext_free(ptr);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "substrate-rpc-servers"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
substrate-rpc = { path = "../rpc", version = "0.1" }
|
||||
jsonrpc-core = { git="https://github.com/paritytech/jsonrpc.git" }
|
||||
jsonrpc-http-server = { git="https://github.com/paritytech/jsonrpc.git" }
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Substrate RPC servers.
|
||||
|
||||
#[warn(missing_docs)]
|
||||
|
||||
extern crate substrate_rpc as apis;
|
||||
|
||||
extern crate jsonrpc_core as rpc;
|
||||
extern crate jsonrpc_http_server as http;
|
||||
|
||||
use std::io;
|
||||
|
||||
/// Construct rpc `IoHandler`
|
||||
pub fn rpc_handler<S>(state: S) -> rpc::IoHandler where
|
||||
S: apis::state::StateApi,
|
||||
{
|
||||
let mut io = rpc::IoHandler::new();
|
||||
io.extend_with(state.to_delegate());
|
||||
io
|
||||
}
|
||||
|
||||
/// Start HTTP server listening on given address.
|
||||
pub fn start_http(
|
||||
addr: &std::net::SocketAddr,
|
||||
io: rpc::IoHandler,
|
||||
) -> io::Result<http::Server> {
|
||||
http::ServerBuilder::new(io)
|
||||
.threads(4)
|
||||
.rest_api(http::RestApi::Unsecure)
|
||||
.start_http(addr)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "substrate-rpc"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
error-chain = "0.11"
|
||||
jsonrpc-core = { git="https://github.com/paritytech/jsonrpc.git" }
|
||||
jsonrpc-macros = { git="https://github.com/paritytech/jsonrpc.git" }
|
||||
substrate-client = { path = "../client" }
|
||||
substrate-primitives = { path = "../primitives" }
|
||||
substrate-state-machine = { path = "../state-machine" }
|
||||
substrate-executor = { path = "../executor" }
|
||||
|
||||
[dev-dependencies]
|
||||
assert_matches = "1.1"
|
||||
substrate-executor = { path = "../executor" }
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use rpc;
|
||||
|
||||
error_chain! {
|
||||
errors {
|
||||
/// Not implemented yet
|
||||
Unimplemented {
|
||||
description("not yet implemented"),
|
||||
display("Method Not Implemented"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for rpc::Error {
|
||||
fn from(e: Error) -> Self {
|
||||
match e {
|
||||
Error(ErrorKind::Unimplemented, _) => rpc::Error {
|
||||
code: rpc::ErrorCode::ServerError(-1),
|
||||
message: "Not implemented yet".into(),
|
||||
data: None,
|
||||
},
|
||||
_ => rpc::Error::internal_error(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Substrate blockchain API.
|
||||
|
||||
use primitives::block;
|
||||
use client;
|
||||
use state_machine;
|
||||
|
||||
mod error;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use self::error::{Result, ResultExt};
|
||||
|
||||
build_rpc_trait! {
|
||||
/// Polkadot blockchain API
|
||||
pub trait ChainApi {
|
||||
/// Get header of a relay chain block.
|
||||
#[rpc(name = "chain_getHeader")]
|
||||
fn header(&self, block::HeaderHash) -> Result<Option<block::Header>>;
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, E> ChainApi for client::Client<B, E> where
|
||||
B: client::backend::Backend + Send + Sync + 'static,
|
||||
E: state_machine::CodeExecutor + Send + Sync + 'static,
|
||||
client::error::Error: From<<<B as client::backend::Backend>::State as state_machine::backend::Backend>::Error>,
|
||||
{
|
||||
fn header(&self, hash: block::HeaderHash) -> Result<Option<block::Header>> {
|
||||
client::Client::header(self, &hash).chain_err(|| "Blockchain error")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use substrate_executor as executor;
|
||||
use client;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_return_header() {
|
||||
let test_genesis_block = block::Header {
|
||||
parent_hash: 0.into(),
|
||||
number: 0,
|
||||
state_root: 0.into(),
|
||||
transaction_root: Default::default(),
|
||||
digest: Default::default(),
|
||||
};
|
||||
|
||||
let client = client::new_in_mem(executor::WasmExecutor, || (test_genesis_block.clone(), vec![])).unwrap();
|
||||
|
||||
assert_matches!(
|
||||
ChainApi::header(&client, "af65e54217fb213853703d57b80fc5b2bb834bf923046294d7a49bff62f0a8b2".into()),
|
||||
Ok(Some(ref x)) if x == &block::Header {
|
||||
parent_hash: 0.into(),
|
||||
number: 0,
|
||||
state_root: 0.into(),
|
||||
transaction_root: Default::default(),
|
||||
digest: Default::default(),
|
||||
}
|
||||
);
|
||||
|
||||
assert_matches!(
|
||||
ChainApi::header(&client, 5.into()),
|
||||
Ok(None)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Substrate RPC interfaces.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
extern crate jsonrpc_core as rpc;
|
||||
extern crate substrate_client as client;
|
||||
extern crate substrate_primitives as primitives;
|
||||
extern crate substrate_state_machine as state_machine;
|
||||
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate jsonrpc_macros;
|
||||
|
||||
#[cfg(test)]
|
||||
extern crate substrate_executor;
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
extern crate assert_matches;
|
||||
|
||||
pub mod chain;
|
||||
pub mod state;
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use client;
|
||||
use rpc;
|
||||
|
||||
error_chain! {
|
||||
links {
|
||||
Client(client::error::Error, client::error::ErrorKind) #[doc = "Client error"];
|
||||
}
|
||||
|
||||
errors {
|
||||
/// Not implemented yet
|
||||
Unimplemented {
|
||||
description("not implemented yet"),
|
||||
display("Method Not Implemented"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for rpc::Error {
|
||||
fn from(e: Error) -> Self {
|
||||
match e {
|
||||
Error(ErrorKind::Unimplemented, _) => rpc::Error {
|
||||
code: rpc::ErrorCode::ServerError(-1),
|
||||
message: "Not implemented yet".into(),
|
||||
data: None,
|
||||
},
|
||||
_ => rpc::Error::internal_error(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Polkadot state API.
|
||||
|
||||
mod error;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use client::{self, Client};
|
||||
use primitives::block;
|
||||
use primitives::storage::{StorageKey, StorageData};
|
||||
use state_machine;
|
||||
|
||||
use self::error::Result;
|
||||
|
||||
build_rpc_trait! {
|
||||
/// Polkadot state API
|
||||
pub trait StateApi {
|
||||
/// Returns a storage entry.
|
||||
#[rpc(name = "state_getStorage")]
|
||||
fn storage(&self, StorageKey, block::HeaderHash) -> Result<StorageData>;
|
||||
|
||||
/// Call a contract.
|
||||
#[rpc(name = "state_call")]
|
||||
fn call(&self, String, Vec<u8>, block::HeaderHash) -> Result<Vec<u8>>;
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, E> StateApi for Client<B, E> where
|
||||
B: client::backend::Backend + Send + Sync + 'static,
|
||||
E: state_machine::CodeExecutor + Send + Sync + 'static,
|
||||
client::error::Error: From<<<B as client::backend::Backend>::State as state_machine::backend::Backend>::Error>,
|
||||
{
|
||||
fn storage(&self, key: StorageKey, block: block::HeaderHash) -> Result<StorageData> {
|
||||
Ok(self.storage(&block, &key)?)
|
||||
}
|
||||
|
||||
fn call(&self, method: String, data: Vec<u8>, block: block::HeaderHash) -> Result<Vec<u8>> {
|
||||
Ok(self.call(&block, &method, &data)?.return_data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use super::*;
|
||||
use substrate_executor as executor;
|
||||
use self::error::{Error, ErrorKind};
|
||||
use client;
|
||||
|
||||
#[test]
|
||||
fn should_return_storage() {
|
||||
let test_genesis_block = block::Header {
|
||||
parent_hash: 0.into(),
|
||||
number: 0,
|
||||
state_root: 0.into(),
|
||||
transaction_root: Default::default(),
|
||||
digest: Default::default(),
|
||||
};
|
||||
|
||||
let client = client::new_in_mem(executor::WasmExecutor, || (test_genesis_block.clone(), vec![])).unwrap();
|
||||
let genesis_hash = "af65e54217fb213853703d57b80fc5b2bb834bf923046294d7a49bff62f0a8b2".into();
|
||||
|
||||
assert_matches!(
|
||||
StateApi::storage(&client, StorageKey(vec![10]), genesis_hash),
|
||||
Ok(ref x) if x.0.is_empty()
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore] // TODO: [ToDr] reenable once we can properly mock the wasm executor env
|
||||
fn should_call_contract() {
|
||||
// TODO [ToDr] Fix test after we are able to mock state.
|
||||
let test_genesis_block = block::Header {
|
||||
parent_hash: 0.into(),
|
||||
number: 0,
|
||||
state_root: 0.into(),
|
||||
transaction_root: Default::default(),
|
||||
digest: Default::default(),
|
||||
};
|
||||
|
||||
let client = client::new_in_mem(executor::WasmExecutor, || (test_genesis_block.clone(), vec![])).unwrap();
|
||||
let genesis_hash = "af65e54217fb213853703d57b80fc5b2bb834bf923046294d7a49bff62f0a8b2".into();
|
||||
|
||||
assert_matches!(
|
||||
StateApi::call(&client, "balanceOf".into(), vec![1,2,3], genesis_hash),
|
||||
Err(Error(ErrorKind::Client(client::error::ErrorKind::Execution(_)), _))
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
[package]
|
||||
name = "substrate-runtime-io"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
build = "build.rs"
|
||||
|
||||
[build-dependencies]
|
||||
rustc_version = "0.2"
|
||||
|
||||
[dependencies]
|
||||
substrate-runtime-std = { path = "../runtime-std", default_features = false }
|
||||
environmental = { path = "../environmental", optional = true }
|
||||
substrate-state-machine = { path = "../state-machine", optional = true }
|
||||
substrate-primitives = { path = "../primitives", default_features = false }
|
||||
substrate-codec = { path = "../codec", default_features = false }
|
||||
triehash = { version = "0.1", optional = true }
|
||||
ed25519 = { path = "../ed25519", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"environmental",
|
||||
"substrate-state-machine",
|
||||
"triehash",
|
||||
"substrate-primitives/std",
|
||||
"substrate-codec/std",
|
||||
"substrate-runtime-std/std",
|
||||
"ed25519",
|
||||
]
|
||||
nightly = []
|
||||
strict = []
|
||||
@@ -0,0 +1,14 @@
|
||||
//! Set a nightly feature
|
||||
|
||||
extern crate rustc_version;
|
||||
use rustc_version::{version, version_meta, Channel};
|
||||
|
||||
fn main() {
|
||||
// Assert we haven't travelled back in time
|
||||
assert!(version().unwrap().major >= 1);
|
||||
|
||||
// Set cfg flags depending on release channel
|
||||
if let Channel::Nightly = version_meta().unwrap().channel {
|
||||
println!("cargo:rustc-cfg=feature=\"nightly\"");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! This is part of the Substrate runtime.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(not(feature = "std"), feature(lang_items))]
|
||||
#![cfg_attr(not(feature = "std"), feature(core_intrinsics))]
|
||||
#![cfg_attr(not(feature = "std"), feature(alloc))]
|
||||
|
||||
#![cfg_attr(feature = "std", doc = "Substrate runtime standard library as compiled when linked with Rust's standard library.")]
|
||||
#![cfg_attr(not(feature = "std"), doc = "Substrate's runtime standard library as compiled without Rust's standard library.")]
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
include!("../with_std.rs");
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
include!("../without_std.rs");
|
||||
@@ -0,0 +1,185 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#[macro_use]
|
||||
extern crate environmental;
|
||||
|
||||
extern crate substrate_state_machine;
|
||||
extern crate substrate_primitives as primitives;
|
||||
extern crate triehash;
|
||||
extern crate ed25519;
|
||||
|
||||
// re-export hashing functions.
|
||||
pub use primitives::{blake2_256, twox_128, twox_256};
|
||||
|
||||
pub use substrate_state_machine::{Externalities, ExternalitiesError, TestExternalities};
|
||||
use primitives::hexdisplay::HexDisplay;
|
||||
|
||||
// TODO: use the real error, not NoError.
|
||||
|
||||
environmental!(ext : trait Externalities);
|
||||
|
||||
/// Get `key` from storage and return a `Vec`, empty if there's a problem.
|
||||
pub fn storage(key: &[u8]) -> Vec<u8> {
|
||||
ext::with(|ext| ext.storage(key).ok().map(|s| s.to_vec()))
|
||||
.expect("read_storage cannot be called outside of an Externalities-provided environment.")
|
||||
.unwrap_or_else(Vec::new)
|
||||
}
|
||||
|
||||
/// Get `key` from storage, placing the value into `value_out` (as much as possible) and return
|
||||
/// the number of bytes that the key in storage was.
|
||||
pub fn read_storage(key: &[u8], value_out: &mut [u8], value_offset: usize) -> usize {
|
||||
ext::with(|ext| {
|
||||
if let Ok(value) = ext.storage(key) {
|
||||
let value = &value[value_offset..];
|
||||
let written = ::std::cmp::min(value.len(), value_out.len());
|
||||
value_out[0..written].copy_from_slice(&value[0..written]);
|
||||
value.len()
|
||||
} else {
|
||||
// no-entry is treated as an empty vector of bytes.
|
||||
// TODO: consider allowing empty-vector to exist separately to no-entry (i.e. return
|
||||
// Option<usize>)
|
||||
0
|
||||
}
|
||||
}).expect("read_storage cannot be called outside of an Externalities-provided environment.")
|
||||
}
|
||||
|
||||
/// Set the storage to some particular key.
|
||||
pub fn set_storage(key: &[u8], value: &[u8]) {
|
||||
ext::with(|ext|
|
||||
ext.set_storage(key.to_vec(), value.to_vec())
|
||||
);
|
||||
}
|
||||
|
||||
/// The current relay chain identifier.
|
||||
pub fn chain_id() -> u64 {
|
||||
ext::with(|ext|
|
||||
ext.chain_id()
|
||||
).unwrap_or(0)
|
||||
}
|
||||
|
||||
/// "Commit" all existing operations and get the resultant storage root.
|
||||
pub fn storage_root() -> [u8; 32] {
|
||||
ext::with(|ext|
|
||||
ext.storage_root()
|
||||
).unwrap_or([0u8; 32])
|
||||
}
|
||||
|
||||
/// "Commit" all existing operations and get the resultant storage root.
|
||||
pub fn enumerated_trie_root(serialised_values: &[&[u8]]) -> [u8; 32] {
|
||||
triehash::ordered_trie_root(serialised_values.iter().map(|s| s.to_vec())).0
|
||||
}
|
||||
|
||||
/// Verify a ed25519 signature.
|
||||
pub fn ed25519_verify(sig: &[u8; 64], msg: &[u8], pubkey: &[u8; 32]) -> bool {
|
||||
ed25519::verify(sig, msg, pubkey)
|
||||
}
|
||||
|
||||
/// Execute the given closure with global function available whose functionality routes into the
|
||||
/// externalities `ext`. Forwards the value that the closure returns.
|
||||
pub fn with_externalities<R, F: FnOnce() -> R>(ext: &mut Externalities, f: F) -> R {
|
||||
ext::using(ext, f)
|
||||
}
|
||||
|
||||
/// Trait for things which can be printed.
|
||||
pub trait Printable {
|
||||
fn print(self);
|
||||
}
|
||||
|
||||
impl<'a> Printable for &'a [u8] {
|
||||
fn print(self) {
|
||||
println!("Runtime: {}", HexDisplay::from(&self));
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Printable for &'a str {
|
||||
fn print(self) {
|
||||
println!("Runtime: {}", self);
|
||||
}
|
||||
}
|
||||
|
||||
impl Printable for u64 {
|
||||
fn print(self) {
|
||||
println!("Runtime: {}", self);
|
||||
}
|
||||
}
|
||||
|
||||
/// Print a printable value.
|
||||
pub fn print<T: Printable + Sized>(value: T) {
|
||||
value.print();
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_stubs {
|
||||
($( $name:ident ),*) => {
|
||||
pub fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
|
||||
match method {
|
||||
$(
|
||||
stringify!($name) => Some($name(data)),
|
||||
)*
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod std_tests {
|
||||
use super::*;
|
||||
|
||||
macro_rules! map {
|
||||
($( $name:expr => $value:expr ),*) => (
|
||||
vec![ $( ( $name, $value ) ),* ].into_iter().collect()
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_works() {
|
||||
let mut t = TestExternalities { storage: map![], };
|
||||
assert!(with_externalities(&mut t, || {
|
||||
assert_eq!(storage(b"hello"), b"".to_vec());
|
||||
set_storage(b"hello", b"world");
|
||||
assert_eq!(storage(b"hello"), b"world".to_vec());
|
||||
assert_eq!(storage(b"foo"), b"".to_vec());
|
||||
set_storage(b"foo", &[1, 2, 3][..]);
|
||||
true
|
||||
}));
|
||||
|
||||
t.storage = map![b"foo".to_vec() => b"bar".to_vec()];
|
||||
|
||||
assert!(!with_externalities(&mut t, || {
|
||||
assert_eq!(storage(b"hello"), b"".to_vec());
|
||||
assert_eq!(storage(b"foo"), b"bar".to_vec());
|
||||
false
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_storage_works() {
|
||||
let mut t = TestExternalities { storage: map![
|
||||
b":test".to_vec() => b"\x0b\0\0\0Hello world".to_vec()
|
||||
], };
|
||||
|
||||
with_externalities(&mut t, || {
|
||||
let mut v = [0u8; 4];
|
||||
assert!(read_storage(b":test", &mut v[..], 0) >= 4);
|
||||
assert_eq!(v, [11u8, 0, 0, 0]);
|
||||
let mut w = [0u8; 11];
|
||||
assert!(read_storage(b":test", &mut w[..], 4) >= 11);
|
||||
assert_eq!(&w, b"Hello world");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
extern crate substrate_runtime_std as rstd;
|
||||
extern crate substrate_primitives as primitives;
|
||||
|
||||
use rstd::intrinsics;
|
||||
use rstd::vec::Vec;
|
||||
pub use rstd::{mem, slice};
|
||||
|
||||
#[lang = "panic_fmt"]
|
||||
#[no_mangle]
|
||||
pub extern fn panic_fmt(_fmt: ::core::fmt::Arguments, _file: &'static str, _line: u32, _col: u32) {
|
||||
unsafe {
|
||||
ext_print_utf8(_file.as_ptr() as *const u8, _file.len() as u32);
|
||||
ext_print_num(_line as u64);
|
||||
ext_print_num(_col as u64);
|
||||
intrinsics::abort()
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn ext_print_utf8(utf8_data: *const u8, utf8_len: u32);
|
||||
fn ext_print_hex(data: *const u8, len: u32);
|
||||
fn ext_print_num(value: u64);
|
||||
fn ext_set_storage(key_data: *const u8, key_len: u32, value_data: *const u8, value_len: u32);
|
||||
fn ext_get_allocated_storage(key_data: *const u8, key_len: u32, written_out: *mut u32) -> *mut u8;
|
||||
fn ext_get_storage_into(key_data: *const u8, key_len: u32, value_data: *mut u8, value_len: u32, value_offset: u32) -> u32;
|
||||
fn ext_storage_root(result: *mut u8);
|
||||
fn ext_enumerated_trie_root(values_data: *const u8, lens_data: *const u32, lens_len: u32, result: *mut u8);
|
||||
fn ext_chain_id() -> u64;
|
||||
fn ext_blake2_256(data: *const u8, len: u32, out: *mut u8);
|
||||
fn ext_twox_128(data: *const u8, len: u32, out: *mut u8);
|
||||
fn ext_twox_256(data: *const u8, len: u32, out: *mut u8);
|
||||
fn ext_ed25519_verify(msg_data: *const u8, msg_len: u32, sig_data: *const u8, pubkey_data: *const u8) -> u32;
|
||||
}
|
||||
|
||||
/// Get `key` from storage and return a `Vec`, empty if there's a problem.
|
||||
pub fn storage(key: &[u8]) -> Vec<u8> {
|
||||
let mut length: u32 = 0;
|
||||
unsafe {
|
||||
let ptr = ext_get_allocated_storage(key.as_ptr(), key.len() as u32, &mut length);
|
||||
Vec::from_raw_parts(ptr, length as usize, length as usize)
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the storage to some particular key.
|
||||
pub fn set_storage(key: &[u8], value: &[u8]) {
|
||||
unsafe {
|
||||
ext_set_storage(
|
||||
key.as_ptr(), key.len() as u32,
|
||||
value.as_ptr(), value.len() as u32
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Get `key` from storage, placing the value into `value_out` (as much as possible) and return
|
||||
/// the number of bytes that the key in storage was.
|
||||
pub fn read_storage(key: &[u8], value_out: &mut [u8], value_offset: usize) -> usize {
|
||||
unsafe {
|
||||
ext_get_storage_into(
|
||||
key.as_ptr(), key.len() as u32,
|
||||
value_out.as_mut_ptr(), value_out.len() as u32,
|
||||
value_offset as u32
|
||||
) as usize
|
||||
}
|
||||
}
|
||||
|
||||
/// The current storage's root.
|
||||
pub fn storage_root() -> [u8; 32] {
|
||||
let mut result: [u8; 32] = Default::default();
|
||||
unsafe {
|
||||
ext_storage_root(result.as_mut_ptr());
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// A trie root calculated from enumerated values.
|
||||
pub fn enumerated_trie_root(values: &[&[u8]]) -> [u8; 32] {
|
||||
let lens = values.iter().map(|v| (v.len() as u32).to_le()).collect::<Vec<_>>();
|
||||
let values = values.iter().fold(Vec::new(), |mut acc, sl| { acc.extend_from_slice(sl); acc });
|
||||
let mut result: [u8; 32] = Default::default();
|
||||
unsafe {
|
||||
ext_enumerated_trie_root(
|
||||
values.as_ptr(),
|
||||
lens.as_ptr(), lens.len() as u32,
|
||||
result.as_mut_ptr()
|
||||
);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// The current relay chain identifier.
|
||||
pub fn chain_id() -> u64 {
|
||||
unsafe {
|
||||
ext_chain_id()
|
||||
}
|
||||
}
|
||||
|
||||
/// Conduct a 256-bit Blake2 hash.
|
||||
pub fn blake2_256(data: &[u8]) -> [u8; 32] {
|
||||
let mut result: [u8; 32] = Default::default();
|
||||
unsafe {
|
||||
ext_blake2_256(data.as_ptr(), data.len() as u32, result.as_mut_ptr());
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Conduct four XX hashes to give a 256-bit result.
|
||||
pub fn twox_256(data: &[u8]) -> [u8; 32] {
|
||||
let mut result: [u8; 32] = Default::default();
|
||||
unsafe {
|
||||
ext_twox_256(data.as_ptr(), data.len() as u32, result.as_mut_ptr());
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Conduct two XX hashes to give a 128-bit result.
|
||||
pub fn twox_128(data: &[u8]) -> [u8; 16] {
|
||||
let mut result: [u8; 16] = Default::default();
|
||||
unsafe {
|
||||
ext_twox_128(data.as_ptr(), data.len() as u32, result.as_mut_ptr());
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Verify a ed25519 signature.
|
||||
pub fn ed25519_verify(sig: &[u8], msg: &[u8], pubkey: &[u8]) -> bool {
|
||||
sig.len() == 64 && pubkey.len() == 32 && unsafe {
|
||||
ext_ed25519_verify(msg.as_ptr(), msg.len() as u32, sig.as_ptr(), pubkey.as_ptr())
|
||||
} == 0
|
||||
}
|
||||
|
||||
/// Trait for things which can be printed.
|
||||
pub trait Printable {
|
||||
fn print(self);
|
||||
}
|
||||
|
||||
impl<'a> Printable for &'a [u8] {
|
||||
fn print(self) {
|
||||
unsafe {
|
||||
ext_print_hex(self.as_ptr(), self.len() as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Printable for &'a str {
|
||||
fn print(self) {
|
||||
unsafe {
|
||||
ext_print_utf8(self.as_ptr() as *const u8, self.len() as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Printable for u64 {
|
||||
fn print(self) {
|
||||
unsafe { ext_print_num(self); }
|
||||
}
|
||||
}
|
||||
|
||||
/// Print a printable value.
|
||||
pub fn print<T: Printable + Sized>(value: T) {
|
||||
value.print();
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_stubs {
|
||||
( $( $name:ident ),* ) => {
|
||||
pub mod _internal {
|
||||
$(
|
||||
#[no_mangle]
|
||||
pub fn $name(input_data: *mut u8, input_len: usize) -> u64 {
|
||||
let input = if input_len == 0 {
|
||||
&[0u8; 0]
|
||||
} else {
|
||||
unsafe {
|
||||
$crate::slice::from_raw_parts(input_data, input_len)
|
||||
}
|
||||
};
|
||||
|
||||
let output = super::$name(input);
|
||||
let r = output.as_ptr() as u64 + ((output.len() as u64) << 32);
|
||||
$crate::mem::forget(output);
|
||||
r
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "substrate-runtime-std"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
build = "build.rs"
|
||||
|
||||
[build-dependencies]
|
||||
rustc_version = "0.2"
|
||||
|
||||
[dependencies]
|
||||
pwasm-alloc = { path = "../pwasm-alloc" }
|
||||
pwasm-libc = { path = "../pwasm-libc" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
nightly = []
|
||||
strict = []
|
||||
@@ -0,0 +1,14 @@
|
||||
//! Set a nightly feature
|
||||
|
||||
extern crate rustc_version;
|
||||
use rustc_version::{version, version_meta, Channel};
|
||||
|
||||
fn main() {
|
||||
// Assert we haven't travelled back in time
|
||||
assert!(version().unwrap().major >= 1);
|
||||
|
||||
// Set cfg flags depending on release channel
|
||||
if let Channel::Nightly = version_meta().unwrap().channel {
|
||||
println!("cargo:rustc-cfg=feature=\"nightly\"");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Lowest-abstraction level for the Substrate runtime: just exports useful primitives from std
|
||||
//! or core/alloc to be used with any code that depends on the runtime.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(not(feature = "std"), feature(lang_items))]
|
||||
#![cfg_attr(not(feature = "std"), feature(core_intrinsics))]
|
||||
#![cfg_attr(not(feature = "std"), feature(alloc))]
|
||||
|
||||
#![cfg_attr(feature = "std", doc = "Polkadot runtime standard library as compiled when linked with Rust's standard library.")]
|
||||
#![cfg_attr(not(feature = "std"), doc = "Polkadot's runtime standard library as compiled without Rust's standard library.")]
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
include!("../with_std.rs");
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
include!("../without_std.rs");
|
||||
|
||||
/// Prelude of common useful imports.
|
||||
///
|
||||
/// This should include only things which are in the normal std prelude.
|
||||
pub mod prelude {
|
||||
pub use ::vec::Vec;
|
||||
pub use ::boxed::Box;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
pub use std::vec;
|
||||
pub use std::rc;
|
||||
pub use std::cell;
|
||||
pub use std::boxed;
|
||||
pub use std::slice;
|
||||
pub use std::mem;
|
||||
pub use std::ops;
|
||||
pub use std::iter;
|
||||
pub use std::ptr;
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
extern crate alloc;
|
||||
#[cfg(feature = "nightly")]
|
||||
extern crate pwasm_libc;
|
||||
#[cfg(feature = "nightly")]
|
||||
extern crate pwasm_alloc;
|
||||
|
||||
pub use alloc::vec;
|
||||
pub use alloc::boxed;
|
||||
pub use alloc::rc;
|
||||
pub use core::mem;
|
||||
pub use core::slice;
|
||||
pub use core::cell;
|
||||
pub use core::ops;
|
||||
pub use core::iter;
|
||||
pub use core::ptr;
|
||||
pub use core::intrinsics;
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "substrate-serializer"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", default_features = false }
|
||||
serde_json = "1.0"
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Substrate customizable serde serializer.
|
||||
//!
|
||||
//! The idea is that we can later change the implementation
|
||||
//! to something more compact, but for now we're using JSON.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
pub use serde_json::{from_str, from_slice, from_reader, Result, Error};
|
||||
|
||||
const PROOF: &str = "Serializers are infallible; qed";
|
||||
|
||||
/// Serialize the given data structure as a pretty-printed String of JSON.
|
||||
pub fn to_string_pretty<T: serde::Serialize + ?Sized>(value: &T) -> String {
|
||||
serde_json::to_string_pretty(value).expect(PROOF)
|
||||
}
|
||||
|
||||
/// Serialize the given data structure as a JSON byte vector.
|
||||
pub fn to_vec<T: serde::Serialize + ?Sized>(value: &T) -> Vec<u8> {
|
||||
serde_json::to_vec(value).expect(PROOF)
|
||||
}
|
||||
|
||||
/// Serialize the given data structure as JSON into the IO stream.
|
||||
pub fn to_writer<W: ::std::io::Write, T: serde::Serialize + ?Sized>(writer: W, value: &T) -> Result<()> {
|
||||
serde_json::to_writer(writer, value)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "substrate-state-machine"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
description = "Substrate State Machine"
|
||||
|
||||
[dependencies]
|
||||
substrate-primitives = { path = "../primitives", version = "0.1.0" }
|
||||
hashdb = "0.1.1"
|
||||
patricia-trie = "0.1.0"
|
||||
memorydb = "0.1.1"
|
||||
triehash = "0.1"
|
||||
byteorder = "1.1"
|
||||
hex-literal = "0.1.0"
|
||||
@@ -0,0 +1,95 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! State machine backends. These manage the code and storage of contracts.
|
||||
|
||||
use std::{error, fmt};
|
||||
|
||||
use super::{Update, MemoryState};
|
||||
|
||||
/// Output of a commit.
|
||||
pub struct Committed {}
|
||||
|
||||
/// A state backend is used to read state data and can have changes committed
|
||||
/// to it.
|
||||
pub trait Backend {
|
||||
/// An error type when fetching data is not possible.
|
||||
type Error: super::Error;
|
||||
|
||||
/// Get keyed storage associated with specific address.
|
||||
fn storage(&self, key: &[u8]) -> Result<&[u8], Self::Error>;
|
||||
|
||||
/// Commit updates to the backend and get new state.
|
||||
fn commit<I>(&mut self, changes: I) -> Committed
|
||||
where I: IntoIterator<Item=Update>;
|
||||
|
||||
/// Get all key/value pairs into a Vec.
|
||||
fn pairs(&self) -> Vec<(&[u8], &[u8])>;
|
||||
}
|
||||
|
||||
/// Error impossible.
|
||||
// TODO: use `!` type when stabilized.
|
||||
#[derive(Debug)]
|
||||
pub enum Void {}
|
||||
|
||||
impl fmt::Display for Void {
|
||||
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for Void {
|
||||
fn description(&self) -> &str { "unreachable error" }
|
||||
}
|
||||
|
||||
/// In-memory backend. Fully recomputes tries on each commit but useful for
|
||||
/// tests.
|
||||
#[derive(Default, Clone)]
|
||||
pub struct InMemory {
|
||||
inner: MemoryState, // keeps all the state in memory.
|
||||
}
|
||||
|
||||
impl InMemory {
|
||||
/// Create a new instance from a given storage map.
|
||||
pub fn from(storage: ::std::collections::HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
||||
InMemory {
|
||||
inner: MemoryState {
|
||||
storage
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Backend for InMemory {
|
||||
type Error = Void;
|
||||
|
||||
fn storage(&self, key: &[u8]) -> Result<&[u8], Self::Error> {
|
||||
Ok(self.inner.storage(key).unwrap_or(&[]))
|
||||
}
|
||||
|
||||
fn commit<I>(&mut self, changes: I) -> Committed
|
||||
where I: IntoIterator<Item=Update>
|
||||
{
|
||||
self.inner.update(changes);
|
||||
Committed {}
|
||||
}
|
||||
|
||||
fn pairs(&self) -> Vec<(&[u8], &[u8])> {
|
||||
self.inner.storage.iter().map(|(k, v)| (&k[..], &v[..])).collect()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: DB-based backend
|
||||
@@ -0,0 +1,89 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Conrete externalities implementation.
|
||||
|
||||
use std::{error, fmt};
|
||||
use triehash::trie_root;
|
||||
use backend::Backend;
|
||||
use {Externalities, ExternalitiesError, OverlayedChanges};
|
||||
|
||||
/// Errors that can occur when interacting with the externalities.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum Error<B, E> {
|
||||
/// Failure to load state data from the backend.
|
||||
#[allow(unused)]
|
||||
Backend(B),
|
||||
/// Failure to execute a function.
|
||||
#[allow(unused)]
|
||||
Executor(E),
|
||||
}
|
||||
|
||||
impl<B: fmt::Display, E: fmt::Display> fmt::Display for Error<B, E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Error::Backend(ref e) => write!(f, "Storage backend error: {}", e),
|
||||
Error::Executor(ref e) => write!(f, "Sub-call execution error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: error::Error, E: error::Error> error::Error for Error<B, E> {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
Error::Backend(..) => "backend error",
|
||||
Error::Executor(..) => "executor error",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps a read-only backend, call executor, and current overlayed changes.
|
||||
pub struct Ext<'a, B: 'a> {
|
||||
/// The overlayed changes to write to.
|
||||
pub overlay: &'a mut OverlayedChanges,
|
||||
/// The storage backend to read from.
|
||||
pub backend: &'a B,
|
||||
}
|
||||
|
||||
impl<'a, B: 'a> Externalities for Ext<'a, B>
|
||||
where B: Backend
|
||||
{
|
||||
fn storage(&self, key: &[u8]) -> Result<&[u8], ExternalitiesError> {
|
||||
match self.overlay.storage(key) {
|
||||
Some(x) => Ok(x),
|
||||
None => self.backend.storage(key).map_err(|_| ExternalitiesError),
|
||||
}
|
||||
}
|
||||
|
||||
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
|
||||
self.overlay.set_storage(key, value);
|
||||
}
|
||||
|
||||
fn chain_id(&self) -> u64 {
|
||||
42
|
||||
}
|
||||
|
||||
fn storage_root(&self) -> [u8; 32] {
|
||||
let mut all_pairs = self.backend.pairs();
|
||||
all_pairs.extend(
|
||||
self.overlay.committed.storage.iter()
|
||||
.chain(self.overlay.prospective.storage.iter())
|
||||
.map(|(k, v)| (&k[..], &v[..]))
|
||||
);
|
||||
|
||||
trie_root(all_pairs.into_iter().map(|(k, v)| (k.to_vec(), v.to_vec()))).0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Substrate state machine implementation.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
extern crate substrate_primitives as primitives;
|
||||
|
||||
#[cfg_attr(test, macro_use)]
|
||||
extern crate hex_literal;
|
||||
|
||||
extern crate hashdb;
|
||||
extern crate memorydb;
|
||||
|
||||
extern crate patricia_trie;
|
||||
extern crate triehash;
|
||||
|
||||
extern crate byteorder;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
|
||||
pub mod backend;
|
||||
mod ext;
|
||||
mod testing;
|
||||
|
||||
pub use testing::TestExternalities;
|
||||
pub use ext::Ext;
|
||||
|
||||
/// Updates to be committed to the state.
|
||||
pub enum Update {
|
||||
/// Set storage of object at given key -- empty is deletion.
|
||||
Storage(Vec<u8>, Vec<u8>),
|
||||
}
|
||||
|
||||
// in-memory section of the state.
|
||||
#[derive(Default, Clone)]
|
||||
struct MemoryState {
|
||||
storage: HashMap<Vec<u8>, Vec<u8>>,
|
||||
}
|
||||
|
||||
impl MemoryState {
|
||||
fn storage(&self, key: &[u8]) -> Option<&[u8]> {
|
||||
self.storage.get(key).map(|v| &v[..])
|
||||
}
|
||||
|
||||
fn set_storage(&mut self, key: Vec<u8>, val: Vec<u8>) {
|
||||
self.storage.insert(key, val);
|
||||
}
|
||||
|
||||
fn update<I>(&mut self, changes: I) where I: IntoIterator<Item=Update> {
|
||||
for update in changes {
|
||||
match update {
|
||||
Update::Storage(key, val) => {
|
||||
if val.is_empty() {
|
||||
self.storage.remove(&key);
|
||||
} else {
|
||||
self.storage.insert(key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The overlayed changes to state to be queried on top of the backend.
|
||||
///
|
||||
/// A transaction shares all prospective changes within an inner overlay
|
||||
/// that can be cleared.
|
||||
#[derive(Default)]
|
||||
pub struct OverlayedChanges {
|
||||
prospective: MemoryState,
|
||||
committed: MemoryState,
|
||||
}
|
||||
|
||||
impl OverlayedChanges {
|
||||
fn storage(&self, key: &[u8]) -> Option<&[u8]> {
|
||||
self.prospective.storage(key)
|
||||
.or_else(|| self.committed.storage(key))
|
||||
.and_then(|v| if v.is_empty() { None } else { Some(v) })
|
||||
}
|
||||
|
||||
fn set_storage(&mut self, key: Vec<u8>, val: Vec<u8>) {
|
||||
self.prospective.set_storage(key, val);
|
||||
}
|
||||
|
||||
/// Discard prospective changes to state.
|
||||
pub fn discard_prospective(&mut self) {
|
||||
self.prospective.storage.clear();
|
||||
}
|
||||
|
||||
/// Commit prospective changes to state.
|
||||
pub fn commit_prospective(&mut self) {
|
||||
let storage_updates = self.prospective.storage.drain()
|
||||
.map(|(key, value)| Update::Storage(key, value));
|
||||
|
||||
self.committed.update(storage_updates);
|
||||
}
|
||||
}
|
||||
|
||||
/// State Machine Error bound.
|
||||
///
|
||||
/// This should reflect WASM error type bound for future compatibility.
|
||||
pub trait Error: 'static + fmt::Debug + fmt::Display + Send {}
|
||||
impl<E> Error for E where E: 'static + fmt::Debug + fmt::Display + Send {}
|
||||
|
||||
/// Externalities Error.
|
||||
///
|
||||
/// Externalities are not really allowed to have errors, since it's assumed that dependent code
|
||||
/// would not be executed unless externalities were available. This is included for completeness,
|
||||
/// and as a transition away from the pre-existing framework.
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct ExternalitiesError;
|
||||
|
||||
impl fmt::Display for ExternalitiesError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Externalities Error") }
|
||||
}
|
||||
|
||||
fn to_keyed_vec(value: u32, mut prepend: Vec<u8>) -> Vec<u8> {
|
||||
prepend.extend((0..::std::mem::size_of::<u32>()).into_iter().map(|i| (value >> (i * 8)) as u8));
|
||||
prepend
|
||||
}
|
||||
|
||||
/// Externalities: pinned to specific active address.
|
||||
pub trait Externalities {
|
||||
/// Read storage of current contract being called.
|
||||
fn storage(&self, key: &[u8]) -> Result<&[u8], ExternalitiesError>;
|
||||
|
||||
/// Set storage of current contract being called (effective immediately).
|
||||
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>);
|
||||
|
||||
/// Get the identity of the chain.
|
||||
fn chain_id(&self) -> u64;
|
||||
|
||||
/// Get the trie root of the current storage map.
|
||||
fn storage_root(&self) -> [u8; 32];
|
||||
|
||||
/// Get the current set of authorities from storage.
|
||||
fn authorities(&self) -> Result<Vec<&[u8]>, ExternalitiesError> {
|
||||
(0..self.storage(b":auth:len")?.into_iter()
|
||||
.rev()
|
||||
.fold(0, |acc, &i| (acc << 8) + (i as u32)))
|
||||
.map(|i| self.storage(&to_keyed_vec(i, b":auth:".to_vec())))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get the runtime code.
|
||||
fn code(&self) -> Result<&[u8], ExternalitiesError> {
|
||||
self.storage(b":code")
|
||||
}
|
||||
}
|
||||
|
||||
/// Code execution engine.
|
||||
pub trait CodeExecutor: Sized + Send + Sync {
|
||||
/// Externalities error type.
|
||||
type Error: Error;
|
||||
|
||||
/// Call a given method in the runtime.
|
||||
fn call<E: Externalities>(
|
||||
&self,
|
||||
ext: &mut E,
|
||||
code: &[u8],
|
||||
method: &str,
|
||||
data: &[u8],
|
||||
) -> Result<Vec<u8>, Self::Error>;
|
||||
}
|
||||
|
||||
/// Execute a call using the given state backend, overlayed changes, and call executor.
|
||||
///
|
||||
/// On an error, no prospective changes are written to the overlay.
|
||||
///
|
||||
/// Note: changes to code will be in place if this call is made again. For running partial
|
||||
/// blocks (e.g. a transaction at a time), ensure a differrent method is used.
|
||||
pub fn execute<B: backend::Backend, Exec: CodeExecutor>(
|
||||
backend: &B,
|
||||
overlay: &mut OverlayedChanges,
|
||||
exec: &Exec,
|
||||
method: &str,
|
||||
call_data: &[u8],
|
||||
) -> Result<Vec<u8>, Box<Error>> {
|
||||
|
||||
let result = {
|
||||
let mut externalities = ext::Ext {
|
||||
backend,
|
||||
overlay: &mut *overlay
|
||||
};
|
||||
// make a copy.
|
||||
let code = externalities.storage(b":code").unwrap_or(&[]).to_vec();
|
||||
|
||||
exec.call(
|
||||
&mut externalities,
|
||||
&code,
|
||||
method,
|
||||
call_data,
|
||||
)
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(out) => {
|
||||
overlay.commit_prospective();
|
||||
Ok(out)
|
||||
}
|
||||
Err(e) => {
|
||||
overlay.discard_prospective();
|
||||
Err(Box::new(e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::backend::InMemory;
|
||||
use super::ext::Ext;
|
||||
|
||||
#[test]
|
||||
fn overlayed_storage_works() {
|
||||
let mut overlayed = OverlayedChanges::default();
|
||||
|
||||
let key = vec![42, 69, 169, 142];
|
||||
|
||||
assert!(overlayed.storage(&key).is_none());
|
||||
|
||||
overlayed.set_storage(key.clone(), vec![1, 2, 3]);
|
||||
assert_eq!(overlayed.storage(&key).unwrap(), &[1, 2, 3]);
|
||||
|
||||
overlayed.commit_prospective();
|
||||
assert_eq!(overlayed.storage(&key).unwrap(), &[1, 2, 3]);
|
||||
|
||||
overlayed.set_storage(key.clone(), vec![]);
|
||||
assert!(overlayed.storage(&key).is_none());
|
||||
|
||||
overlayed.discard_prospective();
|
||||
assert_eq!(overlayed.storage(&key).unwrap(), &[1, 2, 3]);
|
||||
|
||||
overlayed.set_storage(key.clone(), vec![]);
|
||||
overlayed.commit_prospective();
|
||||
assert!(overlayed.storage(&key).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authorities_call_works() {
|
||||
let mut ext = TestExternalities::default();
|
||||
|
||||
assert_eq!(ext.authorities(), Ok(vec![]));
|
||||
|
||||
ext.set_storage(b":auth:len".to_vec(), vec![0u8; 4]);
|
||||
assert_eq!(ext.authorities(), Ok(vec![]));
|
||||
|
||||
ext.set_storage(b":auth:len".to_vec(), vec![1u8, 0, 0, 0]);
|
||||
assert_eq!(ext.authorities(), Ok(vec![&[][..]]));
|
||||
|
||||
ext.set_storage(b":auth:\0\0\0\0".to_vec(), b"first".to_vec());
|
||||
assert_eq!(ext.authorities(), Ok(vec![&b"first"[..]]));
|
||||
|
||||
ext.set_storage(b":auth:len".to_vec(), vec![2u8, 0, 0, 0]);
|
||||
ext.set_storage(b":auth:\x01\0\0\0".to_vec(), b"second".to_vec());
|
||||
assert_eq!(ext.authorities(), Ok(vec![&b"first"[..], &b"second"[..]]));
|
||||
}
|
||||
|
||||
macro_rules! map {
|
||||
($( $name:expr => $value:expr ),*) => (
|
||||
vec![ $( ( $name, $value ) ),* ].into_iter().collect()
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlayed_storage_root_works() {
|
||||
let mut backend = InMemory::from(map![
|
||||
b"doe".to_vec() => b"reindeer".to_vec(),
|
||||
b"dog".to_vec() => b"puppyXXX".to_vec(),
|
||||
b"dogglesworth".to_vec() => b"catXXX".to_vec()
|
||||
]);
|
||||
let mut overlay = OverlayedChanges {
|
||||
committed: MemoryState { storage: map![
|
||||
b"dog".to_vec() => b"puppy".to_vec(),
|
||||
b"dogglesworth".to_vec() => b"catYYY".to_vec()
|
||||
], },
|
||||
prospective: MemoryState { storage: map![
|
||||
b"dogglesworth".to_vec() => b"cat".to_vec()
|
||||
], },
|
||||
};
|
||||
let ext = Ext {
|
||||
backend: &mut backend,
|
||||
overlay: &mut overlay,
|
||||
};
|
||||
const ROOT: [u8; 32] = hex!("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3");
|
||||
assert_eq!(ext.storage_root(), ROOT);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Test implementation for Externalities.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use super::{Externalities, ExternalitiesError};
|
||||
use triehash::trie_root;
|
||||
|
||||
/// Simple HashMap based Externalities impl.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct TestExternalities {
|
||||
/// The storage.
|
||||
pub storage: HashMap<Vec<u8>, Vec<u8>>,
|
||||
}
|
||||
|
||||
impl TestExternalities {
|
||||
/// Create a new instance with empty storage.
|
||||
pub fn new() -> Self {
|
||||
TestExternalities {
|
||||
storage: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Externalities for TestExternalities {
|
||||
fn storage(&self, key: &[u8]) -> Result<&[u8], ExternalitiesError> {
|
||||
Ok(self.storage.get(&key.to_vec()).map_or(&[] as &[u8], Vec::as_slice))
|
||||
}
|
||||
|
||||
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
|
||||
self.storage.insert(key, value);
|
||||
}
|
||||
|
||||
fn chain_id(&self) -> u64 { 42 }
|
||||
|
||||
fn storage_root(&self) -> [u8; 32] {
|
||||
trie_root(self.storage.clone()).0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn commit_should_work() {
|
||||
let mut ext = TestExternalities::new();
|
||||
ext.set_storage(b"doe".to_vec(), b"reindeer".to_vec());
|
||||
ext.set_storage(b"dog".to_vec(), b"puppy".to_vec());
|
||||
ext.set_storage(b"dogglesworth".to_vec(), b"cat".to_vec());
|
||||
const ROOT: [u8; 32] = hex!("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3");
|
||||
assert_eq!(ext.storage_root(), ROOT);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user