-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67f50cb
commit ac4f622
Showing
8 changed files
with
421 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
Oops, something went wrong.