Introduce first groundwork for Wasm executor (#27)

* Introduce first groundwork for Wasm executor.

* Remove old Rust-runtime code.

* Avoid commiting compled files.

* Add runtime precompile.

* Rename so module makes more sense.

* Further renaming.

* Ensure tests work.

* Allow bringing in of externalities.

- Add util functions/macros.
- Add uncompacted runtime.
- Add some external crates from pwasm-std for managing allocs/memory
stuff.

* Nice macros for imports.

* Allow passing in of data through allocators.

Make memcpy and malloc work.
Basic allocator.

* Can now pass in bytes to WasmExecutor.

* Additional cleanup.

* Switch usages of `OutData` to `u64`

No need to be able to return bytes anymore.

* convert to safe but extremely verbose type conversion.

@rphmeier any more concise way of doing this?

* Remove StaticExternalities distinction.

* Remove another unused use.

* Refactor wasm utils out

* Remove extraneous copies that weren't really testing anything.

* Try to use wasm 0.15

* Make it work!

* Call-time externalities working.

* Add basic externalities.

* Fix grumbles and note unwraps to be sorted.

* Test storage externality.

Unforunately had to change signatures of externalities to avoid
immutable function returning a reference. Not sure what to do about
this...

* Fix nits.

* Compile collation logic.

* Move back to refs. Yey.

* Remove "object" id for storage access.

* Fix test.

* Fix up rest of tests.

* remove unwrap.

* Expose set/get code in externalities

Also improve tests and add nice wrappers in rust-wasm.

* Add validator set.

* Introduce validator set into externalities and test.

* Add another external function.

* Remove code and validators; use storage for everything.

* Introduce validators function.

* Tests (and a fix) for the validators getter.

* Allow calls into runtime to return data.

* Remove unneeded trace.

* Make runtime printing a bit nicer.

* Create separate runtimes for testing and polkadot.

* Remove commented code.

* Use new path.

* Refactor into shared support module.

* Fix warning.

* Remove unwraps.

* Make macro a little less unhygenic.

* Add wasm files.
This commit is contained in:
Gav Wood
2018-01-08 16:48:45 +01:00
committed by Robert Habermeier
parent 45c3e40a62
commit a670208a33
44 changed files with 1087 additions and 611 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ extern crate error_chain;
extern crate jsonrpc_macros;
#[cfg(test)]
extern crate polkadot_contracts;
extern crate polkadot_executor;
#[cfg(test)]
#[macro_use]
extern crate assert_matches;
+8 -8
View File
@@ -22,8 +22,8 @@ mod error;
mod tests;
use client::{self, Client};
use primitives::{block, Address, H256};
use primitives::contract::{CallData, OutData, StorageData};
use primitives::{block};
use primitives::contract::{CallData, StorageKey, StorageData};
use state_machine;
use self::error::Result;
@@ -33,11 +33,11 @@ build_rpc_trait! {
pub trait StateApi {
/// Returns a storage entry.
#[rpc(name = "state_getStorage")]
fn storage(&self, Address, H256, block::HeaderHash) -> Result<StorageData>;
fn storage(&self, StorageKey, block::HeaderHash) -> Result<StorageData>;
/// Call a contract.
#[rpc(name = "state_call")]
fn call(&self, Address, String, CallData, block::HeaderHash) -> Result<OutData>;
fn call(&self, String, CallData, block::HeaderHash) -> Result<Vec<u8>>;
}
}
@@ -45,11 +45,11 @@ impl<B, E> StateApi for Client<B, E> where
B: client::Blockchain + Send + Sync + 'static,
E: state_machine::CodeExecutor + Send + Sync + 'static,
{
fn storage(&self, address: Address, key: H256, block: block::HeaderHash) -> Result<StorageData> {
Ok(self.storage(&block, &address, &key)?)
fn storage(&self, key: StorageKey, block: block::HeaderHash) -> Result<StorageData> {
Ok(self.storage(&block, &key)?)
}
fn call(&self, address: Address, method: String, data: CallData, block: block::HeaderHash) -> Result<OutData> {
Ok(self.call(&block, &address, &method, &data)?)
fn call(&self, method: String, data: CallData, block: block::HeaderHash) -> Result<Vec<u8>> {
Ok(self.call(&block, &method, &data)?.return_data)
}
}
+6 -5
View File
@@ -15,28 +15,29 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use polkadot_contracts as contracts;
use polkadot_executor as executor;
use self::error::{Error, ErrorKind};
use test_helpers::Blockchain;
#[test]
fn should_return_storage() {
let client = Client::new(Blockchain::default(), contracts::executor());
let client = Client::new(Blockchain::default(), executor::executor());
assert_matches!(
StateApi::storage(&client, 5.into(), 10.into(), 0.into()),
StateApi::storage(&client, StorageKey(vec![10]), 0.into()),
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 client = Client::new(Blockchain::default(), contracts::executor());
let client = Client::new(Blockchain::default(), executor::executor());
assert_matches!(
StateApi::call(&client, 1.into(), "balanceOf".into(), CallData(vec![1,2,3]), 0.into()),
StateApi::call(&client, "balanceOf".into(), CallData(vec![1,2,3]), 0.into()),
Err(Error(ErrorKind::Client(client::error::ErrorKind::Execution(_)), _))
)
}