Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
a693a0b
Add `engine` crate
cmichi Feb 25, 2021
e3ea5e4
Add `env_types` crate
cmichi Feb 25, 2021
eff3ada
Adapt `env`, `lang` and `storage`
cmichi Feb 25, 2021
da08ba7
Adapt examples
cmichi Feb 25, 2021
def45c2
Adapt CI
cmichi Mar 2, 2021
3956a2c
Symlink license and readme
cmichi Mar 2, 2021
a0012e9
Throw `TypedEncoded` out of `engine`
cmichi Mar 3, 2021
606c69b
Improve Erc20
cmichi Mar 3, 2021
6850b4b
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi Mar 3, 2021
9c5f2ba
Bump versions to rc3
cmichi Mar 3, 2021
cc8143d
Fix clippy error: Manual implementation of `Option::map` (#717)
Mar 5, 2021
edab873
Implement comments
cmichi Mar 5, 2021
340185d
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi Mar 16, 2021
cb54a77
Fix yml
cmichi Mar 16, 2021
0e0c044
Improve structure
cmichi Mar 16, 2021
be2b598
Add tests
cmichi Mar 18, 2021
02be20c
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi Mar 19, 2021
46adef3
Fix function signature
cmichi Mar 22, 2021
1caada4
Get rid of `engine`s singleton
cmichi Apr 1, 2021
8a2831a
Revert instantiate stuff
cmichi Apr 13, 2021
c8117a9
Implement review comments
cmichi Apr 12, 2021
da71eea
Make `Storage` non-generic
cmichi Apr 13, 2021
b8ba878
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi Apr 13, 2021
b81e4c2
Improve API for emmitted events
cmichi Apr 13, 2021
bff4609
Migrate to `panic_any`
cmichi Apr 15, 2021
655b425
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi Apr 15, 2021
63da646
Clean up import
cmichi Apr 15, 2021
813301e
Import `panic_any`
cmichi Apr 15, 2021
c651d53
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi Apr 28, 2021
8391f8c
Implement comments
cmichi Apr 28, 2021
863628d
Fix param
cmichi Apr 28, 2021
b5c3688
Use type
cmichi Apr 29, 2021
eafe7ac
Store balances in chain storage
cmichi Apr 29, 2021
8f64f38
Fix tests
cmichi Apr 29, 2021
68734c8
Use individual storage per contract
cmichi Apr 29, 2021
35dd9e6
Implement comments
cmichi May 4, 2021
8fec9ae
Merge branch 'master' into cmichi-implement-new-offchain-engine-mvp
cmichi May 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make Storage non-generic
  • Loading branch information
cmichi committed Apr 13, 2021
commit da71eea9813f27cee4e05d1ad567cdf8d50c20b2
10 changes: 4 additions & 6 deletions crates/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl ReturnCode {
/// The off-chain engine.
pub struct Engine {
/// The environment storage.
pub storage: Storage<Key, Vec<u8>>,
pub storage: Storage,
/// Holds the balance of each account.
pub balances: HashMap<AccountId, Balance>,
/// The current execution context.
Expand Down Expand Up @@ -192,9 +192,7 @@ impl Engine {
}

// We ignore if storage is already set for this key
let _ = self
.storage
.insert(Key::from_bytes(key), encoded_value.to_vec());
let _ = self.storage.insert(key.to_vec(), encoded_value.to_vec());
}

/// Removes the value from storage entries at the given key.
Expand All @@ -205,7 +203,7 @@ impl Engine {
self.recorder.dec_cells_per_account(account_id);
}

self.storage.remove(&Key::from_bytes(key));
self.storage.remove(key);
}

/// Returns the decoded storage at the key if any.
Expand All @@ -215,7 +213,7 @@ impl Engine {
self.recorder.inc_reads(account_id);
}

match self.storage.get(&Key::from_bytes(key)) {
match self.storage.get(key) {
Some(val) => {
output[0..val.len()].copy_from_slice(val);
Ok(())
Expand Down
55 changes: 24 additions & 31 deletions crates/engine/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use core::hash::Hash;
use std::{
borrow::Borrow,
cmp::Eq,
collections::HashMap,
};
use std::collections::HashMap;

/// Provides the storage backend.
#[derive(Default)]
pub struct Storage<K, V> {
hmap: HashMap<K, V>,
pub struct Storage {
hmap: HashMap<Vec<u8>, Vec<u8>>,
}

impl<K, V> Storage<K, V>
where
K: Eq + Hash,
{
impl Storage {
/// Creates a new storage instance.
pub fn new() -> Self {
Storage {
Expand All @@ -42,26 +34,18 @@ where
}

/// Returns a reference to the value corresponding to the key.
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
pub fn get(&self, key: &[u8]) -> Option<&Vec<u8>> {
self.hmap.get(key)
}

/// Removes a key from the storage, returning the value at the key if the key
/// was previously in storage.
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
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: K, value: V) -> Option<V> {
pub fn insert(&mut self, key: Vec<u8>, value: Vec<u8>) -> Option<Vec<u8>> {
self.hmap.insert(key, value)
}

Expand All @@ -77,16 +61,25 @@ mod tests {

#[test]
fn basic_operations() {
let mut storage = Storage::<u32, bool>::new();
let mut storage = Storage::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(&42), None);
assert_eq!(storage.insert(42, true), None);
assert_eq!(storage.get(&42), Some(&true));
assert_eq!(storage.insert(42, false), Some(true));
assert_eq!(storage.get(&42), Some(&false));
assert_eq!(storage.insert(43, true), None);
assert_eq!(storage.get(&key1), None);
assert_eq!(storage.insert(key1.clone(), val1.clone()), None);
assert_eq!(storage.get(&key1), Some(&val1));
assert_eq!(
storage.insert(key1.clone(), val2.clone()),
Some(val1.clone())
);
assert_eq!(storage.get(&key1), Some(&val2));
assert_eq!(storage.insert(key2.clone(), val3.clone()), None);
assert_eq!(storage.len(), 2);
assert_eq!(storage.remove(&43), Some(true));
assert_eq!(storage.remove(&key2), Some(val3));
assert_eq!(storage.len(), 1);
storage.clear();
assert_eq!(storage.len(), 0);
Expand Down