mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 00:01:09 +00:00
Allow transaction for offchain indexing (#7290)
* Moving offchain change set to state machine overlay change set, preparing use of change set internally. * Make change set generic over key and value, and use it for offchain indexing. * test ui change * remaining delta * generating with standard method * Remove 'drain_committed' function, and documentation. * Default constructor for enabling offchain indexing. * Remove offchain change specific iterators. * remove pub accessor * keep previous hierarchy, just expose iterator instead. * Update primitives/state-machine/src/overlayed_changes/mod.rs Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * fix line break * missing renamings * fix import * fix new state-machine tests. * Don't expose InnerValue type. * Add test similar to set_storage. * Remove conditional offchain storage (hard to instantiate correctly). * fix * offchain as children cannot fail if top doesn't Co-authored-by: Addie Wagenknecht <addie@nortd.com> Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
This commit is contained in:
@@ -746,6 +746,14 @@ impl TransactionPoolExt {
|
||||
}
|
||||
}
|
||||
|
||||
/// Change to be applied to the offchain worker db in regards to a key.
|
||||
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
||||
pub enum OffchainOverlayedChange {
|
||||
/// Remove the data associated with the key
|
||||
Remove,
|
||||
/// Overwrite the value of an associated key
|
||||
SetValue(Vec<u8>),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@@ -83,98 +83,3 @@ impl OffchainStorage for InMemOffchainStorage {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Change to be applied to the offchain worker db in regards to a key.
|
||||
#[derive(Debug,Clone,Hash,Eq,PartialEq)]
|
||||
pub enum OffchainOverlayedChange {
|
||||
/// Remove the data associated with the key
|
||||
Remove,
|
||||
/// Overwrite the value of an associated key
|
||||
SetValue(Vec<u8>),
|
||||
}
|
||||
|
||||
/// In-memory storage for offchain workers recoding changes for the actual offchain storage implementation.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct OffchainOverlayedChanges {
|
||||
changes: HashMap<(Vec<u8>, Vec<u8>), OffchainOverlayedChange>,
|
||||
}
|
||||
|
||||
impl OffchainOverlayedChanges {
|
||||
/// Consume the offchain storage and iterate over all key value pairs.
|
||||
pub fn into_iter(self) -> impl Iterator<Item = ((Vec<u8>, Vec<u8>), OffchainOverlayedChange)> {
|
||||
self.changes.into_iter()
|
||||
}
|
||||
|
||||
/// Iterate over all key value pairs by reference.
|
||||
pub fn iter(&self) -> impl Iterator<Item = (&(Vec<u8>, Vec<u8>), &OffchainOverlayedChange)> {
|
||||
self.changes.iter()
|
||||
}
|
||||
|
||||
/// Drain all elements of changeset.
|
||||
pub fn drain(&mut self) -> impl Iterator<Item = ((Vec<u8>, Vec<u8>), OffchainOverlayedChange)> + '_ {
|
||||
self.changes.drain()
|
||||
}
|
||||
|
||||
/// Remove a key and its associated value from the offchain database.
|
||||
pub fn remove(&mut self, prefix: &[u8], key: &[u8]) {
|
||||
self.changes.insert((prefix.to_vec(), key.to_vec()), OffchainOverlayedChange::Remove);
|
||||
}
|
||||
|
||||
/// Set the value associated with a key under a prefix to the value provided.
|
||||
pub fn set(&mut self, prefix: &[u8], key: &[u8], value: &[u8]) {
|
||||
self.changes.insert(
|
||||
(prefix.to_vec(), key.to_vec()),
|
||||
OffchainOverlayedChange::SetValue(value.to_vec()),
|
||||
);
|
||||
}
|
||||
|
||||
/// Obtain a associated value to the given key in storage with prefix.
|
||||
pub fn get(&self, prefix: &[u8], key: &[u8]) -> Option<OffchainOverlayedChange> {
|
||||
let key = (prefix.to_vec(), key.to_vec());
|
||||
self.changes.get(&key).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use super::super::STORAGE_PREFIX;
|
||||
|
||||
#[test]
|
||||
fn test_drain() {
|
||||
let mut ooc = OffchainOverlayedChanges::default();
|
||||
ooc.set(STORAGE_PREFIX,b"kkk", b"vvv");
|
||||
let drained = ooc.drain().count();
|
||||
assert_eq!(drained, 1);
|
||||
let leftover = ooc.iter().count();
|
||||
assert_eq!(leftover, 0);
|
||||
|
||||
ooc.set(STORAGE_PREFIX, b"a", b"v");
|
||||
ooc.set(STORAGE_PREFIX, b"b", b"v");
|
||||
ooc.set(STORAGE_PREFIX, b"c", b"v");
|
||||
ooc.set(STORAGE_PREFIX, b"d", b"v");
|
||||
ooc.set(STORAGE_PREFIX, b"e", b"v");
|
||||
assert_eq!(ooc.iter().count(), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_accumulated_set_remove_set() {
|
||||
let mut ooc = OffchainOverlayedChanges::default();
|
||||
ooc.set(STORAGE_PREFIX, b"ppp", b"qqq");
|
||||
ooc.remove(STORAGE_PREFIX, b"ppp");
|
||||
// keys are equiv, so it will overwrite the value and the overlay will contain
|
||||
// one item
|
||||
assert_eq!(ooc.iter().count(), 1);
|
||||
|
||||
ooc.set(STORAGE_PREFIX, b"ppp", b"rrr");
|
||||
let mut iter = ooc.into_iter();
|
||||
assert_eq!(
|
||||
iter.next(),
|
||||
Some(
|
||||
((STORAGE_PREFIX.to_vec(), b"ppp".to_vec()),
|
||||
OffchainOverlayedChange::SetValue(b"rrr".to_vec()))
|
||||
)
|
||||
);
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,8 @@ use std::{
|
||||
use crate::OpaquePeerId;
|
||||
use crate::offchain::{
|
||||
self,
|
||||
storage::{InMemOffchainStorage, OffchainOverlayedChange, OffchainOverlayedChanges},
|
||||
OffchainOverlayedChange,
|
||||
storage::InMemOffchainStorage,
|
||||
HttpError,
|
||||
HttpRequestId as RequestId,
|
||||
HttpRequestStatus as RequestStatus,
|
||||
@@ -80,9 +81,12 @@ impl TestPersistentOffchainDB {
|
||||
}
|
||||
|
||||
/// Apply a set of off-chain changes directly to the test backend
|
||||
pub fn apply_offchain_changes(&mut self, changes: &mut OffchainOverlayedChanges) {
|
||||
pub fn apply_offchain_changes(
|
||||
&mut self,
|
||||
changes: impl Iterator<Item = ((Vec<u8>, Vec<u8>), OffchainOverlayedChange)>,
|
||||
) {
|
||||
let mut me = self.persistent.write();
|
||||
for ((_prefix, key), value_operation) in changes.drain() {
|
||||
for ((_prefix, key), value_operation) in changes {
|
||||
match value_operation {
|
||||
OffchainOverlayedChange::SetValue(val) => me.set(Self::PREFIX, key.as_slice(), val.as_slice()),
|
||||
OffchainOverlayedChange::Remove => me.remove(Self::PREFIX, key.as_slice()),
|
||||
|
||||
Reference in New Issue
Block a user