Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
feat/offchain: add remove interface method
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schuster committed May 12, 2020
1 parent 2a6c18b commit a037d25
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 11 deletions.
8 changes: 8 additions & 0 deletions client/db/src/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ impl sp_core::offchain::OffchainStorage for LocalStorage {
self.db.commit(tx);
}

fn remove(&mut self, prefix: &[u8], key: &[u8]) {
let key: Vec<u8> = prefix.iter().chain(key).cloned().collect();
let mut tx = Transaction::new();
tx.remove(columns::OFFCHAIN, &key);

self.db.commit(tx);
}

fn get(&self, prefix: &[u8], key: &[u8]) -> Option<Vec<u8>> {
let key: Vec<u8> = prefix.iter().chain(key).cloned().collect();
self.db.get(columns::OFFCHAIN, &key)
Expand Down
7 changes: 7 additions & 0 deletions client/offchain/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ impl<Storage: OffchainStorage> OffchainExt for Api<Storage> {
}
}

fn local_storage_remove(&mut self, kind: StorageKind, key: &[u8]) {
match kind {
StorageKind::PERSISTENT => self.db.remove(STORAGE_PREFIX, key),
StorageKind::LOCAL => unavailable_yet(LOCAL_DB),
}
}

fn local_storage_compare_and_set(
&mut self,
kind: StorageKind,
Expand Down
19 changes: 19 additions & 0 deletions primitives/core/src/offchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub trait OffchainStorage: Clone + Send + Sync {
/// Persist a value in storage under given key and prefix.
fn set(&mut self, prefix: &[u8], key: &[u8], value: &[u8]);

/// Remove a perviously pin storage ersisted value under given key and prefix.
fn remove(&mut self, prefix: &[u8], key: &[u8]);

/// Retrieve a value from storage under given key and prefix.
fn get(&self, prefix: &[u8], key: &[u8]) -> Option<Vec<u8>>;

Expand Down Expand Up @@ -347,6 +350,13 @@ pub trait Externalities: Send {
/// Note this storage is not part of the consensus, it's only accessible by
/// offchain worker tasks running on the same machine. It IS persisted between runs.
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]);

/// Removes a value in the local storage.
///
/// Note this storage is not part of the consensus, it's only accessible by
/// offchain worker tasks running on the same machine. It IS persisted between runs.
fn local_storage_remove(&mut self, kind: StorageKind, key: &[u8]);

/// Sets a value in the local storage if it matches current value.
///
/// Since multiple offchain workers may be running concurrently, to prevent
Expand Down Expand Up @@ -511,6 +521,10 @@ impl<T: Externalities + ?Sized> Externalities for Box<T> {
(&mut **self).local_storage_set(kind, key, value)
}

fn local_storage_remove(&mut self, kind: StorageKind, key: &[u8]) {
(&mut **self).local_storage_remove(kind, key)
}

fn local_storage_compare_and_set(
&mut self,
kind: StorageKind,
Expand Down Expand Up @@ -616,6 +630,11 @@ impl<T: Externalities> Externalities for LimitedExternalities<T> {
self.externalities.local_storage_set(kind, key, value)
}

fn local_storage_remove(&mut self, kind: StorageKind, key: &[u8]) {
self.check(Capability::OffchainWorkerDbWrite, "local_storage_remove");
self.externalities.local_storage_remove(kind, key)
}

fn local_storage_compare_and_set(
&mut self,
kind: StorageKind,
Expand Down
5 changes: 5 additions & 0 deletions primitives/core/src/offchain/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ impl OffchainStorage for InMemOffchainStorage {
self.storage.insert(key, value.to_vec());
}

fn remove(&mut self, prefix: &[u8], key: &[u8]) {
let key: Vec<u8> = prefix.iter().chain(key).cloned().collect();
self.storage.remove(&key);
}

fn get(&self, prefix: &[u8], key: &[u8]) -> Option<Vec<u8>> {
let key: Vec<u8> = prefix.iter().chain(key).cloned().collect();
self.storage.get(&key).cloned()
Expand Down
8 changes: 8 additions & 0 deletions primitives/core/src/offchain/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ impl offchain::Externalities for TestOffchainExt {
}.set(b"", key, value);
}

fn local_storage_remove(&mut self, kind: StorageKind, key: &[u8]) {
let mut state = self.0.write();
match kind {
StorageKind::LOCAL => &mut state.local_storage,
StorageKind::PERSISTENT => &mut state.persistent_storage,
}.remove(b"", key);
}

fn local_storage_compare_and_set(
&mut self,
kind: StorageKind,
Expand Down
10 changes: 10 additions & 0 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,16 @@ pub trait Offchain {
.local_storage_set(kind, key, value)
}

/// Remove a value from the local storage.
///
/// Note this storage is not part of the consensus, it's only accessible by
/// offchain worker tasks running on the same machine. It IS persisted between runs.
fn local_storage_remove(&mut self, kind: StorageKind, key: &[u8]) {
self.extension::<OffchainExt>()
.expect("local_storage_remove can be called only in the offchain worker context")
.local_storage_remove(kind, key)
}

/// Sets a value in the local storage if it matches current value.
///
/// Since multiple offchain workers may be running concurrently, to prevent
Expand Down
25 changes: 14 additions & 11 deletions primitives/session/src/offchain_hashdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,20 @@ L: TrieLayout + Send,
let tree_prefix = tree_prefix.as_slice();

self.get_index(session_index)
.into_iter()
.filter_map(move |(derived_key, prefix)| {
if tree_prefix == prefix.as_slice() {
// @todo FIXME erase
offchain::local_storage_set(StorageKind::PERSISTENT, derived_key.as_slice(), &[]);
None
} else {
Some((derived_key, prefix))
}
})
.collect::<BTreeSet<_>>()
.into_iter()
.filter_map(move |(derived_key, prefix)| {
if tree_prefix == prefix.as_slice() {
// @todo FIXME erase
offchain::local_storage_set(
StorageKind::PERSISTENT,
derived_key.as_slice(),
&[]);
None
} else {
Some((derived_key, prefix))
}
})
.collect::<BTreeSet<_>>()

} else {

Expand Down

0 comments on commit a037d25

Please sign in to comment.