Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
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
Add storage_append to runtime
  • Loading branch information
cecton committed May 7, 2020
commit 5daea6b6df6d394613d74127b985ff11ee8a128e
37 changes: 36 additions & 1 deletion runtime/src/validate_block/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use trie_db::{Trie, TrieDB, TrieDBIterator};

use parachain::primitives::{HeadData, ValidationCode, ValidationParams, ValidationResult};

use codec::{Decode, Encode};
use codec::{Decode, Encode, EncodeAppend};

use cumulus_primitives::{
validation_function_params::ValidationFunctionParams,
Expand Down Expand Up @@ -72,6 +72,18 @@ trait Storage {

/// Clear all keys that start with the given prefix.
fn clear_prefix(&mut self, prefix: &[u8]);

/// Append the value to the given key
fn storage_append(&mut self, key: &[u8], value: Vec<u8>);
}

/// Implement `Encode` by forwarding the stored raw vec.
struct EncodeOpaqueValue(Vec<u8>);

impl Encode for EncodeOpaqueValue {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.0)
}
}

/// Validate a given parachain block on a validator.
Expand Down Expand Up @@ -113,6 +125,7 @@ pub fn validate_block<B: BlockT, E: ExecuteBlock<B>>(params: ValidationParams) -
sp_io::storage::host_root.replace_implementation(host_storage_root),
sp_io::storage::host_clear_prefix.replace_implementation(host_storage_clear_prefix),
sp_io::storage::host_changes_root.replace_implementation(host_storage_changes_root),
sp_io::storage::host_append.replace_implementation(host_storage_append),
)
};

Expand Down Expand Up @@ -220,6 +233,24 @@ impl<B: BlockT> Storage for WitnessStorage<B> {
self.overlay.insert(key, None);
}
}

fn storage_append(&mut self, key: &[u8], value: Vec<u8>) {
let key_vec = key.to_vec();
let mut value_vec = Vec::with_capacity(1);
value_vec.push(EncodeOpaqueValue(value));

if let Some(Some(item)) = self.overlay.remove(&key_vec) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to use the entry api here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I want this time. I'm doing a remove to extract the value to transform it and re-insert it in the hashmap. I don't need a mutable reference here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want it ;) sp_std::mem::take is your friend.

Copy link
Contributor Author

@cecton cecton May 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably fixed in here: #92 (comment)

self.overlay.insert(
key_vec,
match Vec::<EncodeOpaqueValue>::append_or_new(item, &value_vec) {
Ok(item) => Some(item),
Err(_) => Some(value_vec.encode()),
},
);
} else {
self.overlay.insert(key_vec, Some(value_vec.encode()));
}
}
}

fn host_storage_read(key: &[u8], value_out: &mut [u8], value_offset: u32) -> Option<u32> {
Expand Down Expand Up @@ -263,3 +294,7 @@ fn host_storage_changes_root(_: &[u8]) -> Option<Vec<u8>> {
// TODO implement it properly
None
}

fn host_storage_append(key: &[u8], value: Vec<u8>) {
storage().storage_append(key, value);
}