-
Notifications
You must be signed in to change notification settings - Fork 480
Implement MVP for new off-chain testing engine #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
a693a0b
Add `engine` crate
cmichi e3ea5e4
Add `env_types` crate
cmichi eff3ada
Adapt `env`, `lang` and `storage`
cmichi da08ba7
Adapt examples
cmichi def45c2
Adapt CI
cmichi 3956a2c
Symlink license and readme
cmichi a0012e9
Throw `TypedEncoded` out of `engine`
cmichi 606c69b
Improve Erc20
cmichi 6850b4b
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi 9c5f2ba
Bump versions to rc3
cmichi cc8143d
Fix clippy error: Manual implementation of `Option::map` (#717)
edab873
Implement comments
cmichi 340185d
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi cb54a77
Fix yml
cmichi 0e0c044
Improve structure
cmichi be2b598
Add tests
cmichi 02be20c
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi 46adef3
Fix function signature
cmichi 1caada4
Get rid of `engine`s singleton
cmichi 8a2831a
Revert instantiate stuff
cmichi c8117a9
Implement review comments
cmichi da71eea
Make `Storage` non-generic
cmichi b8ba878
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi b81e4c2
Improve API for emmitted events
cmichi bff4609
Migrate to `panic_any`
cmichi 655b425
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi 63da646
Clean up import
cmichi 813301e
Import `panic_any`
cmichi c651d53
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi 8391f8c
Implement comments
cmichi 863628d
Fix param
cmichi b5c3688
Use type
cmichi eafe7ac
Store balances in chain storage
cmichi 8f64f38
Fix tests
cmichi 68734c8
Use individual storage per contract
cmichi 35dd9e6
Implement comments
cmichi 8fec9ae
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| [package] | ||
| name = "ink_engine" | ||
| version = "3.0.0-rc3" | ||
| authors = ["Parity Technologies <[email protected]>", "Michael Müller <[email protected]>"] | ||
| edition = "2018" | ||
|
|
||
| license = "Apache-2.0" | ||
| readme = "README.md" | ||
| repository = "https://github.com/paritytech/ink" | ||
| documentation = "https://docs.rs/ink_engine/" | ||
| homepage = "https://www.parity.io/" | ||
| description = "[ink!] Experimental off-chain environment for testing." | ||
| keywords = ["wasm", "parity", "webassembly", "blockchain", "edsl"] | ||
| categories = ["no-std", "embedded"] | ||
| include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"] | ||
|
|
||
| [dependencies] | ||
| scale = { package = "parity-scale-codec", version = "2.0", default-features = false, features = ["derive", "full"] } | ||
| derive_more = { version = "0.99", default-features = false, features = ["from", "display"] } | ||
|
|
||
| sha2 = { version = "0.9" } | ||
| sha3 = { version = "0.9" } | ||
| blake2 = { version = "0.9" } | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "scale/std", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../LICENSE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../README.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| // Copyright 2018-2021 Parity Technologies (UK) Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use crate::types::Balance; | ||
| use scale::KeyedVec; | ||
| use std::collections::HashMap; | ||
|
|
||
| const BALANCE_OF: &[u8] = b"balance:"; | ||
| const STORAGE_OF: &[u8] = b"contract-storage:"; | ||
|
|
||
| /// Returns the database key under which to find the balance for account `who`. | ||
| pub fn balance_of_key(who: &[u8]) -> [u8; 32] { | ||
| let keyed = who.to_vec().to_keyed_vec(BALANCE_OF); | ||
| let mut hashed_key: [u8; 32] = [0; 32]; | ||
| super::hashing::blake2b_256(&keyed[..], &mut hashed_key); | ||
| hashed_key | ||
| } | ||
|
|
||
| /// Returns the database key under which to find the balance for account `who`. | ||
| pub fn storage_of_contract_key(who: &[u8], key: &[u8]) -> [u8; 32] { | ||
| let keyed = who.to_vec().to_keyed_vec(key).to_keyed_vec(STORAGE_OF); | ||
| let mut hashed_key: [u8; 32] = [0; 32]; | ||
| super::hashing::blake2b_256(&keyed[..], &mut hashed_key); | ||
| hashed_key | ||
| } | ||
|
|
||
| /// The chain database. | ||
| /// | ||
| /// Everything is stored in here: accounts, balances, contract storage, etc.. | ||
| /// Just like in Substrate a prefix hash is computed for every contract. | ||
| #[derive(Default)] | ||
| pub struct Database { | ||
| hmap: HashMap<Vec<u8>, Vec<u8>>, | ||
| } | ||
|
|
||
| impl Database { | ||
| /// Creates a new database instance. | ||
| pub fn new() -> Self { | ||
| Database { | ||
| hmap: HashMap::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Returns the amount of entries in the database. | ||
| #[cfg(test)] | ||
| fn len(&self) -> usize { | ||
| self.hmap.len() | ||
| } | ||
|
|
||
| /// Returns a reference to the value corresponding to the key. | ||
| fn get(&self, key: &[u8]) -> Option<&Vec<u8>> { | ||
| self.hmap.get(key) | ||
| } | ||
|
|
||
| /// Returns a reference to the value corresponding to the key. | ||
| pub fn get_from_contract_storage( | ||
| &self, | ||
| account_id: &[u8], | ||
| key: &[u8], | ||
| ) -> Option<&Vec<u8>> { | ||
| let hashed_key = storage_of_contract_key(&account_id, key); | ||
| self.hmap.get(&hashed_key.to_vec()) | ||
| } | ||
|
|
||
| /// Inserts `value` into the contract storage of `account_id` at storage key `key`. | ||
| pub fn insert_into_contract_storage( | ||
| &mut self, | ||
| account_id: &[u8], | ||
| key: &[u8], | ||
| value: Vec<u8>, | ||
| ) -> Option<Vec<u8>> { | ||
| let hashed_key = storage_of_contract_key(&account_id, key); | ||
| self.hmap.insert(hashed_key.to_vec(), value) | ||
| } | ||
|
|
||
| /// Removes the value at the contract storage of `account_id` at storage key `key`. | ||
| pub fn remove_contract_storage( | ||
| &mut self, | ||
| account_id: &[u8], | ||
| key: &[u8], | ||
| ) -> Option<Vec<u8>> { | ||
| let hashed_key = storage_of_contract_key(&account_id, key); | ||
| self.hmap.remove(&hashed_key.to_vec()) | ||
| } | ||
|
|
||
| /// Removes a key from the storage, returning the value at the key if the key | ||
| /// was previously in storage. | ||
| pub fn remove(&mut self, key: &[u8]) -> Option<Vec<u8>> { | ||
| self.hmap.remove(key) | ||
| } | ||
|
|
||
| /// Sets the value of the entry, and returns the entry's old value. | ||
| pub fn insert(&mut self, key: Vec<u8>, value: Vec<u8>) -> Option<Vec<u8>> { | ||
| self.hmap.insert(key, value) | ||
| } | ||
|
|
||
| /// Clears the database, removing all key-value pairs. | ||
| pub fn clear(&mut self) { | ||
| self.hmap.clear(); | ||
| } | ||
|
|
||
| /// Returns the balance of `account_id`, if available. | ||
| pub fn get_balance(&self, account_id: &[u8]) -> Option<Balance> { | ||
| let hashed_key = balance_of_key(&account_id); | ||
| self.get(&hashed_key).map(|encoded_balance| { | ||
| scale::Decode::decode(&mut &encoded_balance[..]) | ||
| .expect("unable to decode balance from database") | ||
| }) | ||
| } | ||
|
|
||
| /// Sets the balance of `account_id` to `new_balance`. | ||
| pub fn set_balance(&mut self, account_id: &[u8], new_balance: Balance) { | ||
| let hashed_key = balance_of_key(&account_id); | ||
| let encoded_balance = scale::Encode::encode(&new_balance); | ||
| self.hmap | ||
| .entry(hashed_key.to_vec()) | ||
| .and_modify(|v| *v = encoded_balance.clone()) | ||
| .or_insert(encoded_balance); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::Database; | ||
|
|
||
| #[test] | ||
| fn basic_operations() { | ||
| let mut database = Database::new(); | ||
| let key1 = vec![42]; | ||
| let key2 = vec![43]; | ||
| let val1 = vec![44]; | ||
| let val2 = vec![45]; | ||
| let val3 = vec![46]; | ||
|
|
||
| assert_eq!(database.len(), 0); | ||
| assert_eq!(database.get(&key1), None); | ||
| assert_eq!(database.insert(key1.clone(), val1.clone()), None); | ||
| assert_eq!(database.get(&key1), Some(&val1)); | ||
| assert_eq!( | ||
| database.insert(key1.clone(), val2.clone()), | ||
| Some(val1.clone()) | ||
| ); | ||
| assert_eq!(database.get(&key1), Some(&val2)); | ||
| assert_eq!(database.insert(key2.clone(), val3.clone()), None); | ||
| assert_eq!(database.len(), 2); | ||
| assert_eq!(database.remove(&key2), Some(val3)); | ||
| assert_eq!(database.len(), 1); | ||
| database.clear(); | ||
| assert_eq!(database.len(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn contract_storage() { | ||
| let account_id = vec![1; 32]; | ||
| let mut storage = Database::new(); | ||
| let key1 = vec![42]; | ||
| let key2 = vec![43]; | ||
| let val1 = vec![44]; | ||
| let val2 = vec![45]; | ||
| let val3 = vec![46]; | ||
|
|
||
| assert_eq!(storage.len(), 0); | ||
| assert_eq!(storage.get_from_contract_storage(&account_id, &key1), None); | ||
| assert_eq!( | ||
| storage.insert_into_contract_storage(&account_id, &key1, val1.clone()), | ||
| None | ||
| ); | ||
| assert_eq!( | ||
| storage.get_from_contract_storage(&account_id, &key1), | ||
| Some(&val1) | ||
| ); | ||
| assert_eq!( | ||
| storage.insert_into_contract_storage(&account_id, &key1, val2.clone()), | ||
| Some(val1.clone()) | ||
| ); | ||
| assert_eq!( | ||
| storage.get_from_contract_storage(&account_id, &key1), | ||
| Some(&val2) | ||
| ); | ||
| assert_eq!( | ||
| storage.insert_into_contract_storage(&account_id, &key2, val3.clone()), | ||
| None | ||
| ); | ||
| assert_eq!(storage.len(), 2); | ||
| assert_eq!( | ||
| storage.remove_contract_storage(&account_id, &key2), | ||
| Some(val3) | ||
| ); | ||
| assert_eq!(storage.len(), 1); | ||
| assert_eq!( | ||
| storage.remove_contract_storage(&account_id, &key1), | ||
| Some(val2) | ||
| ); | ||
| assert_eq!(storage.len(), 0); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright 2018-2021 Parity Technologies (UK) Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use super::types::{ | ||
| AccountId, | ||
| Balance, | ||
| }; | ||
|
|
||
| /// The context of a contract execution. | ||
| #[cfg_attr(test, derive(Debug, PartialEq, Eq))] | ||
| pub struct ExecContext { | ||
| /// The caller of the contract execution. Might be user or another contract. | ||
| /// | ||
| /// We don't know the specifics of the AccountId ‒ like how many bytes or what | ||
| /// type of default `AccountId` makes sense ‒ they are left to be initialized | ||
| /// by the crate which uses the `engine`. Methods which require a caller might | ||
| /// panic when it has not been set. | ||
| pub caller: Option<AccountId>, | ||
| /// The callee of the contract execution. Might be user or another contract. | ||
| /// | ||
| /// We don't know the specifics of the AccountId ‒ like how many bytes or what | ||
| /// type of default `AccountId` makes sense ‒ they are left to be initialized | ||
| /// by the crate which uses the `engine`. Methods which require a callee might | ||
| /// panic when it has not been set. | ||
| pub callee: Option<AccountId>, | ||
| /// The value transferred to the contract as part of the call. | ||
| pub value_transferred: Balance, | ||
| } | ||
|
|
||
| #[allow(clippy::new_without_default)] | ||
| impl ExecContext { | ||
| /// Creates a new execution context. | ||
| pub fn new() -> Self { | ||
| Self { | ||
| caller: None, | ||
| callee: None, | ||
| value_transferred: 0, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the callee. | ||
| pub fn callee(&self) -> Vec<u8> { | ||
| self.callee | ||
| .as_ref() | ||
| .expect("no callee has been set") | ||
| .as_bytes() | ||
| .into() | ||
| } | ||
|
|
||
| /// Resets the execution context | ||
| pub fn reset(&mut self) { | ||
| self.caller = None; | ||
| self.callee = None; | ||
| self.value_transferred = Default::default(); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{ | ||
| AccountId, | ||
| ExecContext, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn basic_operations() { | ||
| let mut exec_cont = ExecContext::new(); | ||
|
|
||
| exec_cont.callee = Some(AccountId::from_bytes(&[13])); | ||
| exec_cont.caller = Some(AccountId::from_bytes(&[14])); | ||
| exec_cont.value_transferred = 15; | ||
| assert_eq!(exec_cont.callee(), vec![13]); | ||
|
|
||
| exec_cont.reset(); | ||
| assert_eq!(exec_cont, ExecContext::new()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.