mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 15:07:59 +00:00
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:
@@ -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>(),
|
||||
|
||||
Reference in New Issue
Block a user