Skip to content

Commit

Permalink
fix: Fix incorrect das-db prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkingLee committed Feb 7, 2024
1 parent 410e558 commit fdc4610
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
2 changes: 2 additions & 0 deletions crates/das-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub use alloc::{
vec,
};

pub(crate) const DEFAULT_PREFIX: &[u8] = b"das_default_prefix";

pub mod traits;
#[cfg(feature = "sqlite")]
pub mod sqlite;
Expand Down
71 changes: 37 additions & 34 deletions crates/das-db/src/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,59 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::traits::DasKv;
use crate::Vec;
use crate::{traits::DasKv, Vec, DEFAULT_PREFIX};

use sp_core::offchain::StorageKind;

// Implementation for the non-outside environment
#[derive(Debug, Clone, Default)]
pub struct OffchainKv {
prefix: Vec<u8>,
prefix: Vec<u8>,
}

impl OffchainKv {
fn get_prefixed_key(&self, key: &[u8]) -> Vec<u8> {
let mut prefixed_key = self.prefix.clone();
prefixed_key.extend_from_slice(key);
prefixed_key
}
fn get_prefixed_key(&self, key: &[u8]) -> Vec<u8> {
let mut prefixed_key = self.prefix.clone();
prefixed_key.extend_from_slice(key);
prefixed_key
}
}

impl DasKv for OffchainKv {
fn get(&mut self, key: &[u8]) -> Option<Vec<u8>> {
let prefixed_key = self.get_prefixed_key(key);
sp_io::offchain::local_storage_get(StorageKind::PERSISTENT, &prefixed_key)
}
fn get(&mut self, key: &[u8]) -> Option<Vec<u8>> {
let prefixed_key = self.get_prefixed_key(key);
sp_io::offchain::local_storage_get(StorageKind::PERSISTENT, &prefixed_key)
}

fn set(&mut self, key: &[u8], value: &[u8]) {
let prefixed_key = self.get_prefixed_key(key);
sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, &prefixed_key, value);
}
fn set(&mut self, key: &[u8], value: &[u8]) {
let prefixed_key = self.get_prefixed_key(key);
sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, &prefixed_key, value);
}

fn remove(&mut self, key: &[u8]) {
let prefixed_key = self.get_prefixed_key(key);
sp_io::offchain::local_storage_clear(StorageKind::PERSISTENT, &prefixed_key);
}
fn remove(&mut self, key: &[u8]) {
let prefixed_key = self.get_prefixed_key(key);
sp_io::offchain::local_storage_clear(StorageKind::PERSISTENT, &prefixed_key);
}

fn contains(&mut self, key: &[u8]) -> bool {
self.get(key).is_some()
}
fn contains(&mut self, key: &[u8]) -> bool {
self.get(key).is_some()
}

fn compare_and_set(&mut self, key: &[u8], old_value: Option<&[u8]>, new_value: &[u8]) -> bool {
let prefixed_key = self.get_prefixed_key(key);
let old_value = old_value.map(|v| v.to_vec());
sp_io::offchain::local_storage_compare_and_set(StorageKind::PERSISTENT, &prefixed_key, old_value, new_value)
}
fn compare_and_set(&mut self, key: &[u8], old_value: Option<&[u8]>, new_value: &[u8]) -> bool {
let prefixed_key = self.get_prefixed_key(key);
let old_value = old_value.map(|v| v.to_vec());
sp_io::offchain::local_storage_compare_and_set(
StorageKind::PERSISTENT,
&prefixed_key,
old_value,
new_value,
)
}
}

impl OffchainKv {
pub fn new(prefix: Option<&[u8]>) -> Self {
let default_prefix = b"default_prefix";
let prefix = prefix.unwrap_or(default_prefix).to_vec();
OffchainKv { prefix }
}
}
pub fn new(prefix: Option<&[u8]>) -> Self {
let prefix = prefix.unwrap_or(DEFAULT_PREFIX).to_vec();
OffchainKv { prefix }
}
}
4 changes: 1 addition & 3 deletions crates/das-db/src/offchain_outside.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use core::marker::PhantomData;

use crate::traits::DasKv;
use crate::{traits::DasKv, DEFAULT_PREFIX};

use sc_client_api::Backend;
use sc_offchain::OffchainDb;
Expand All @@ -23,8 +23,6 @@ use sp_runtime::traits::Block;

use sp_core::offchain::StorageKind;

const DEFAULT_PREFIX: &[u8] = b"das_default_prefix";

#[derive(Debug, Clone)]
pub struct OffchainKvOutside<B: Block, BE: Backend<B>> {
db: OffchainDb<BE::OffchainStorage>,
Expand Down
4 changes: 2 additions & 2 deletions crates/pallet-melo-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ impl<T: Config> Pallet<T> {
.iter()
.enumerate()
.filter_map(|(i, metadata)| {
let mut db = OffchainKv::new(Some(DB_PREFIX));
let mut db = OffchainKv::new();
match ReliabilityId::app_confidence(metadata.app_id, metadata.nonce)
.get_confidence(&mut db)
{
Expand All @@ -551,7 +551,7 @@ impl<T: Config> Pallet<T> {
/// chain. Returns a vector of block numbers representing the unavailable blocks.
pub fn fetch_unavailability_blocks() -> Vec<BlockNumberFor<T>> {
let now = <frame_system::Pallet<T>>::block_number();
let mut db = OffchainKv::new(Some(DB_PREFIX));
let mut db = OffchainKv::new();

let last: BlockNumberFor<T> =
match ReliabilityManager::new(db.clone()).get_last_processed_block() {
Expand Down

0 comments on commit fdc4610

Please sign in to comment.