-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
Showing
11 changed files
with
123 additions
and
235 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
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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
/// State storage implementation based on the `sqlite` | ||
#[cfg(feature = "sqlite")] | ||
pub mod sqlite; | ||
#[cfg(feature = "sqlite")] | ||
#[cfg(all(feature = "sqlite", not(feature = "redb")))] | ||
pub use sqlite::Pool as SqlitePool; | ||
|
||
#[cfg(feature = "sqlite")] | ||
#[cfg(all(feature = "sqlite", not(feature = "redb")))] | ||
pub type Storage = SqlitePool; | ||
|
||
#[cfg(feature = "rocks_db")] | ||
pub mod rocks_db; | ||
#[cfg(all(feature = "rocks_db", not(feature = "sqlite")))] | ||
use self::rocks_db::RocksDb; | ||
/// State storage implementation based on the [`redb`] | ||
#[cfg(feature = "redb")] | ||
pub mod redb; | ||
#[cfg(feature = "redb")] | ||
use self::redb::ReDb; | ||
|
||
#[cfg(all(feature = "rocks_db", not(feature = "sqlite")))] | ||
pub type Storage = RocksDb; | ||
#[cfg(feature = "redb")] | ||
pub type Storage = ReDb; |
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,79 @@ | ||
use std::path::Path; | ||
|
||
use freenet_stdlib::prelude::*; | ||
use redb::{Database, TableDefinition}; | ||
|
||
use crate::wasm_runtime::StateStorage; | ||
|
||
const CONTRACT_PARAMS_TABLE: TableDefinition<&[u8], &[u8]> = | ||
TableDefinition::new("contract_params"); | ||
const STATE_TABLE: TableDefinition<&[u8], &[u8]> = TableDefinition::new("state"); | ||
|
||
pub struct ReDb(Database); | ||
|
||
impl ReDb { | ||
pub async fn new(db_path: &Path) -> Result<Self, redb::Error> { | ||
tracing::info!("loading contract store from {db_path:?}"); | ||
|
||
Database::create(db_path).map(Self).map_err(Into::into) | ||
} | ||
} | ||
|
||
impl StateStorage for ReDb { | ||
type Error = redb::Error; | ||
|
||
async fn store(&mut self, key: ContractKey, state: WrappedState) -> Result<(), Self::Error> { | ||
let txn = self.0.begin_write()?; | ||
|
||
{ | ||
let mut tbl = txn.open_table(STATE_TABLE)?; | ||
tbl.insert(key.as_bytes(), state.as_ref())?; | ||
} | ||
txn.commit().map_err(Into::into) | ||
} | ||
|
||
async fn get(&self, key: &ContractKey) -> Result<Option<WrappedState>, Self::Error> { | ||
let txn = self.0.begin_read()?; | ||
|
||
let val = { | ||
let tbl = txn.open_table(STATE_TABLE)?; | ||
tbl.get(key.as_bytes())? | ||
}; | ||
|
||
match val { | ||
Some(v) => Ok(Some(WrappedState::new(v.value().to_vec()))), | ||
None => Ok(None), | ||
} | ||
} | ||
|
||
async fn store_params( | ||
&mut self, | ||
key: ContractKey, | ||
params: Parameters<'static>, | ||
) -> Result<(), Self::Error> { | ||
let txn = self.0.begin_write()?; | ||
|
||
{ | ||
let mut tbl = txn.open_table(CONTRACT_PARAMS_TABLE)?; | ||
tbl.insert(key.as_bytes(), params.as_ref())?; | ||
} | ||
txn.commit().map_err(Into::into) | ||
} | ||
|
||
async fn get_params<'a>( | ||
&'a self, | ||
key: &'a ContractKey, | ||
) -> Result<Option<Parameters<'static>>, Self::Error> { | ||
let txn = self.0.begin_read()?; | ||
|
||
let val = { | ||
let tbl = txn.open_table(CONTRACT_PARAMS_TABLE)?; | ||
tbl.get(key.as_bytes())? | ||
}; | ||
|
||
match val { | ||
Some(v) => Ok(Some(Parameters::from(v.value().to_vec()))), | ||
None => Ok(None), | ||
} | ||
} | ||
} |
Oops, something went wrong.