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

offchain storage lock #6010

Merged
merged 46 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c1032ae
feat/offchain/storage: add remove interface method
May 11, 2020
3fa59ee
feat/offchain/storeage: add remote to StorageValueRef
May 13, 2020
4557bd7
feat/offchain/storage: add storage lock
May 13, 2020
be1988f
fix/review: Apply suggestions from code review
drahnr May 13, 2020
ce60655
refactor/offchain/storage/lock: introduce `Lockable` trait part 1 of 2
May 13, 2020
c058ad9
chore/offchain/rename: _remove -> clean
May 14, 2020
1cb9909
feat/offchain/storage/lock: add TimeAndBlock based part 2 of 2
May 14, 2020
c6f60c7
fix/offchain/storage/lock: block and time expiry must be && not ||
May 14, 2020
09e0c2c
chore/offchain/storage: minor fmt doc comments
May 14, 2020
9d6716c
doc/comment: prefer markdown emphasis over CAPS
May 18, 2020
74e2e8c
doc/comment: rewrap multiline module level docs
May 18, 2020
05d7e54
doc/comment: rephrase
May 18, 2020
47d492f
impl sleep_until and use the actual time for the test env
May 18, 2020
48d15ec
feat/test: add more tests, ignore some sample impl doctests
May 18, 2020
add98ae
fix/review: Apply suggestions from code review
drahnr May 18, 2020
9d4ef5d
doc/comment: better description
May 18, 2020
1bc3b6a
fix/review: Apply suggestions from code review
drahnr May 18, 2020
debe87d
chore/storage: lifetime cleanup
May 18, 2020
d6a5306
fix/cleanup: trait bounds, cargo-spellcheck + extra explanations
May 20, 2020
50e5ef2
fix/doc: periods +-
May 20, 2020
37a8b40
Merge remote-tracking branch 'origin' into bernhard/offchain-storage-…
May 20, 2020
0bc2047
fix/review: Apply suggestions from code review
drahnr May 22, 2020
cc85933
cleanup: remove explicit lifetime bound, copy -> clone
May 22, 2020
8e14a12
fix/review: make trait Lockable contain only static, try_lock should …
May 22, 2020
828e5b9
chore/lifetimes: remove a couple of lifetime bounds which the compile…
May 22, 2020
2a99191
refactor: migrate to an instant based
May 22, 2020
fb38887
fix/feedback: fix, reduce, rename, docs update pending
May 25, 2020
c3dd254
docs/reword: adjust to changed code
May 26, 2020
b899583
fix/offchain/testing: timestamp and sleep_until shall not block
May 26, 2020
7dddd76
chore/lines: lines must < 100 chars
May 26, 2020
cf1a87a
fix/docs: add missing pub field doc comments
May 26, 2020
e6e6ec2
refactor/x: try_lock does not need to return an Option<_>
May 26, 2020
78bf501
refactor/simplify: a better way of waiting for a lock to resolve
May 26, 2020
2d6fcba
docs: consistency
May 27, 2020
7febfe9
fix/line: < 100
May 27, 2020
d6e16f4
Merge remote-tracking branch 'origin/master' into bernhard/offchain-s…
May 27, 2020
230ac43
fix/doctest/use: avoid crate::
May 27, 2020
9737473
fix/doctest: *
May 27, 2020
2155ad8
fix/review: remove unused trait bound
May 28, 2020
192df57
fix/review: pretty by const fn
May 28, 2020
ef160b9
fix/review: reduce default timeout to 20s
May 28, 2020
80abc9d
docs: grammar
May 28, 2020
efab9cf
fix/review: add with_block_deadline
May 28, 2020
ae0f8dc
doc: revamp BlockNumberProvider documentation to be less frame centric
May 28, 2020
230c0d9
chore: fmt
May 28, 2020
8e425ac
docs: add missing doc comment
May 28, 2020
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
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]) {
drahnr marked this conversation as resolved.
Show resolved Hide resolved
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_clear(&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
18 changes: 18 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]);

/// Clear a storage entry 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 @@ -348,6 +351,12 @@ pub trait Externalities: Send {
/// 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.
drahnr marked this conversation as resolved.
Show resolved Hide resolved
fn local_storage_clear(&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 @@ -512,6 +521,10 @@ impl<T: Externalities + ?Sized> Externalities for Box<T> {
(&mut **self).local_storage_set(kind, key, value)
}

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

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

fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]) {
self.check(Capability::OffchainWorkerDbWrite, "local_storage_clear");
self.externalities.local_storage_clear(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_clear(&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_clear(&mut self, kind: StorageKind, key: &[u8]) {
self.extension::<OffchainExt>()
.expect("local_storage_clear can be called only in the offchain worker context")
.local_storage_clear(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
1 change: 1 addition & 0 deletions primitives/runtime/src/offchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

pub mod http;
pub mod storage;
pub mod storage_lock;

pub use sp_core::offchain::*;
5 changes: 5 additions & 0 deletions primitives/runtime/src/offchain/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ impl<'a> StorageValueRef<'a> {
})
}

/// Remove the associated value from the storage.
pub fn clear(&mut self) {
sp_io::offchain::local_storage_clear(self.kind, self.key)
}

/// Retrieve & decode the value from storage.
///
/// Note that if you want to do some checks based on the value
Expand Down
Loading