Skip to content

Commit

Permalink
feat: crate das-db
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkingLee committed Oct 16, 2023
1 parent 67f50cb commit ac4f622
Show file tree
Hide file tree
Showing 8 changed files with 421 additions and 0 deletions.
60 changes: 60 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"crates/frame-system-ext",
"crates/melo-erasure-coding",
"crates/pallet-melo-store",
"crates/das-db",
"crates/meloxt",
"runtime",
]
Expand Down
33 changes: 33 additions & 0 deletions crates/das-db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "melo-das-db"
version = "0.0.1"
description = ""
license = "Apache-2.0"
authors = ["DKLee <[email protected]>"]
edition = "2021"

[dependencies]
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42"}
sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42"}
sc-client-api = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42"}
sc-offchain = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42"}
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42"}

rusqlite = { version = "0.28.0", optional = true, features = ["bundled"] }

[dev-dependencies]

[features]
default = ["std","outside"]
std = [
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"rusqlite",
]
outside = [
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"rusqlite",
]
21 changes: 21 additions & 0 deletions crates/das-db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 ZeroDAO
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![cfg_attr(not(feature = "std"), no_std)]

pub mod traits;
#[cfg(feature = "std")]
pub mod sqlite;
pub mod offchain;
#[cfg(feature = "outside")]
pub mod offchain_outside;
65 changes: 65 additions & 0 deletions crates/das-db/src/offchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2023 ZeroDAO
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::traits::DasKv;

use sp_core::offchain::StorageKind;

// Implementation for the non-outside environment
pub struct OffchainKv {
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
}
}

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 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 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)
}
}

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 }
}
}
69 changes: 69 additions & 0 deletions crates/das-db/src/offchain_outside.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2023 ZeroDAO
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::traits::DasKv;

use sc_client_api::Backend;
use sc_offchain::OffchainDb;
use sp_core::offchain::DbExternalities;
use sp_runtime::traits::Block;

use sp_core::offchain::StorageKind;
// Implementation for the outside environment
pub struct OffchainKvOutside<B: Block, BE: Backend<B>> {
db: OffchainDb<BE::OffchainStorage>,
prefix: Vec<u8>,
}

impl<B: Block, BE: Backend<B>> OffchainKvOutside<B, BE> {
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<B: Block, BE: Backend<B>> DasKv for OffchainKvOutside<B, BE> {
fn get(&mut self, key: &[u8]) -> Option<Vec<u8>> {
let prefixed_key = self.get_prefixed_key(key);
self.db.local_storage_get(StorageKind::PERSISTENT, &prefixed_key)
}

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

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

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);
self.db.local_storage_compare_and_set(StorageKind::PERSISTENT, &prefixed_key, old_value, new_value)
}
}

impl<B: Block, BE: Backend<B>> OffchainKvOutside<B, BE> {
pub fn new(db: OffchainDb<BE::OffchainStorage>, prefix: Option<&[u8]>) -> Self {
let default_prefix = b"default_prefix";
let prefix = prefix.unwrap_or(default_prefix).to_vec();
OffchainKvOutside { db, prefix }
}
}
Loading

0 comments on commit ac4f622

Please sign in to comment.