Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make DB types shareable accross threads #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/go_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl ZkMemoryDb {
}

// the zktrie can be created only if the corresponding root node has been added
pub fn new_trie(self: &Rc<Self>, root: &Hash) -> Option<ZkTrie> {
pub fn new_trie(self: &Arc<Self>, root: &Hash) -> Option<ZkTrie> {
let ret = unsafe { NewZkTrie(root.as_ptr(), self.db) };

if ret.is_null() {
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ mod tests {

#[test]
fn trie_works() {
use std::rc::Rc;
use std::sync::Arc;

init_hash_scheme_simple(poseidon_hash_scheme);
let mut db = ZkMemoryDb::new();
Expand All @@ -173,7 +173,7 @@ mod tests {
let buf = hex::decode(bts.get(2..).unwrap()).unwrap();
db.add_node_data(&buf).unwrap();
}
let mut db = Rc::new(db);
let mut db = Arc::new(db);

let root = hex::decode("194cfd0c3cce58ac79c5bab34b149927e0cd9280c6d61870bfb621d45533ddbc")
.unwrap();
Expand Down Expand Up @@ -261,7 +261,7 @@ mod tests {
trie.commit().unwrap();

let trie_db = trie.updated_db();
Rc::get_mut(&mut db).expect("no reference").update(trie_db);
Arc::get_mut(&mut db).expect("no reference").update(trie_db);
let trie = db.new_ref_trie(&root).unwrap();

let proof = trie.prove(&acc_buf).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions src/rs_lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::constants::*;
use std::{collections::HashMap, rc::Rc};
use std::{collections::HashMap, sync::Arc};
use zktrie_rust::{
db::ZktrieDatabase,
types::{Hashable, TrieHashScheme},
Expand Down Expand Up @@ -128,7 +128,7 @@ pub struct ZkMemoryDb {
}

#[derive(Clone)]
pub struct SharedMemoryDb(Rc<ZkMemoryDb>);
pub struct SharedMemoryDb(Arc<ZkMemoryDb>);

impl db::ZktrieDatabase for SharedMemoryDb {
fn put(&mut self, _: Vec<u8>, _: Vec<u8>) -> Result<(), raw::ImplError> {
Expand All @@ -146,7 +146,7 @@ impl trie::KeyCache<HashImpl> for SharedMemoryDb {
}

#[derive(Clone)]
pub struct UpdateDb(db::SimpleDb, Rc<ZkMemoryDb>);
pub struct UpdateDb(db::SimpleDb, Arc<ZkMemoryDb>);

impl UpdateDb {
pub fn updated_db(self) -> db::SimpleDb {
Expand Down Expand Up @@ -228,15 +228,15 @@ impl ZkMemoryDb {
}

/// the zktrie can be created only if the corresponding root node has been added
pub fn new_trie(self: &Rc<Self>, root: &Hash) -> Option<ZkTrie<UpdateDb>> {
pub fn new_trie(self: &Arc<Self>, root: &Hash) -> Option<ZkTrie<UpdateDb>> {
HashImpl::from_bytes(root.as_slice())
.ok()
.and_then(|h| ZktrieRs::new_zktrie(h, UpdateDb(Default::default(), self.clone())).ok())
.map(ZkTrie)
}

/// the zktrie can be created only if the corresponding root node has been added
pub fn new_ref_trie(self: &Rc<Self>, root: &Hash) -> Option<ZkTrie<SharedMemoryDb>> {
pub fn new_ref_trie(self: &Arc<Self>, root: &Hash) -> Option<ZkTrie<SharedMemoryDb>> {
HashImpl::from_bytes(root.as_slice())
.ok()
.and_then(|h| ZktrieRs::new_zktrie(h, SharedMemoryDb(self.clone())).ok())
Expand Down