Make offchain indexing work (#7940)

* Make offchain indexing work

This fixes some bugs with offchain indexing to make it actually working ;)

* Fix tests

* Fix browser build

* Update client/db/src/offchain.rs

Co-authored-by: cheme <emericchevalier.pro@gmail.com>

* Remove seperation between prefix and key

Co-authored-by: cheme <emericchevalier.pro@gmail.com>
This commit is contained in:
Bastian Köcher
2021-01-21 13:12:42 +01:00
committed by GitHub
parent a3a9e7667c
commit cd0ad4805d
24 changed files with 188 additions and 306 deletions
@@ -43,7 +43,7 @@ use sp_std::collections::btree_set::BTreeSet;
use codec::{Decode, Encode};
use sp_core::storage::{well_known_keys::EXTRINSIC_INDEX, ChildInfo};
#[cfg(feature = "std")]
use sp_core::offchain::storage::OffchainOverlayedChanges;
use sp_core::offchain::storage::{OffchainOverlayedChanges, OffchainOverlayedChange};
use hash_db::Hasher;
use crate::DefaultError;
use sp_externalities::{Extensions, Extension};
@@ -101,6 +101,9 @@ pub struct OverlayedChanges {
collect_extrinsics: bool,
/// Collect statistic on this execution.
stats: StateMachineStats,
/// Offchain related changes.
#[cfg(feature = "std")]
offchain: OffchainOverlayedChanges,
}
/// A storage changes structure that can be generated by the data collected in [`OverlayedChanges`].
@@ -523,7 +526,7 @@ impl OverlayedChanges {
main_storage_changes: main_storage_changes.collect(),
child_storage_changes: child_storage_changes.map(|(sk, it)| (sk, it.0.collect())).collect(),
#[cfg(feature = "std")]
offchain_storage_changes: Default::default(),
offchain_storage_changes: std::mem::take(&mut self.offchain),
transaction,
transaction_storage_root,
#[cfg(feature = "std")]
@@ -629,6 +632,40 @@ impl OverlayedChanges {
overlay.next_change(key)
)
}
/// Set a value in the offchain storage.
#[cfg(feature = "std")]
pub fn offchain_set_storage(&mut self, prefix: &[u8], key: &[u8], value: &[u8]) {
self.offchain.set(prefix, key, value);
}
/// Clear a value in the offchain storage.
#[cfg(feature = "std")]
pub fn offchain_remove_storage(&mut self, prefix: &[u8], key: &[u8]) {
self.offchain.remove(prefix, key);
}
/// Get a value in the offchain storage.
#[cfg(feature = "std")]
pub fn offchain_get_storage(
&mut self,
prefix: &[u8],
key: &[u8],
) -> Option<OffchainOverlayedChange> {
self.offchain.get(prefix, key)
}
/// Returns a reference to the offchain overlay.
#[cfg(feature = "std")]
pub fn offchain_overlay(&self) -> &OffchainOverlayedChanges {
&self.offchain
}
/// Returns a mutable reference to the offchain overlay.
#[cfg(feature = "std")]
pub fn offchain_overlay_mut(&mut self) -> &mut OffchainOverlayedChanges {
&mut self.offchain
}
}
#[cfg(feature = "std")]
@@ -789,11 +826,9 @@ mod tests {
overlay.set_storage(b"dogglesworth".to_vec(), Some(b"cat".to_vec()));
overlay.set_storage(b"doug".to_vec(), None);
let mut offchain_overlay = Default::default();
let mut cache = StorageTransactionCache::default();
let mut ext = Ext::new(
&mut overlay,
&mut offchain_overlay,
&mut cache,
&backend,
crate::changes_trie::disabled_state::<_, u64>(),