diff --git a/firewood/src/lib.rs b/firewood/src/lib.rs index 2007dfcde..24682eda7 100644 --- a/firewood/src/lib.rs +++ b/firewood/src/lib.rs @@ -194,6 +194,5 @@ pub mod storage; pub mod api; pub(crate) mod config; pub mod nibbles; -pub mod service; pub mod v2; diff --git a/firewood/src/service/client.rs b/firewood/src/service/client.rs deleted file mode 100644 index b1d7b38ac..000000000 --- a/firewood/src/service/client.rs +++ /dev/null @@ -1,201 +0,0 @@ -// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. -// See the file LICENSE.md for licensing terms. - -/// Client side connection structure -/// -/// A connection is used to send messages to the firewood thread. -use std::fmt::Debug; -use std::mem::take; -use std::thread::JoinHandle; -use std::{path::Path, thread}; - -use tokio::sync::{mpsc, oneshot}; - -use crate::api::Revision; - -use crate::merkle::MerkleError; -use crate::v2::api::Proof; -use crate::{ - db::{DbConfig, DbError}, - merkle::TrieHash, -}; -use async_trait::async_trait; - -use super::server::FirewoodService; -use super::{Request, RevRequest, RevisionHandle}; - -/// A `Connection` represents a connection to the thread running firewood -/// The type specified is how you want to refer to your key values; this is -/// something like `Vec` or `&[u8]` -#[derive(Debug)] -pub struct Connection { - sender: Option>>, - handle: Option>, -} - -impl Drop for Connection { - fn drop(&mut self) { - drop(take(&mut self.sender)); - take(&mut self.handle) - .unwrap() - .join() - .expect("Couldn't join with the firewood thread"); - } -} - -impl Connection { - #[allow(dead_code)] - fn new>(path: P, cfg: DbConfig) -> Self { - let (sender, receiver) = mpsc::channel(1_000) - as ( - tokio::sync::mpsc::Sender>, - tokio::sync::mpsc::Receiver>, - ); - let owned_path = path.as_ref().to_path_buf(); - let handle = thread::Builder::new() - .name("firewood-receiver".to_owned()) - .spawn(move || FirewoodService::new(receiver, owned_path, cfg)) - .expect("thread creation failed"); - Self { - sender: Some(sender), - handle: Some(handle), - } - } -} - -impl super::RevisionHandle { - pub async fn close(self) { - let _ = self - .sender - .send(Request::RevRequest(RevRequest::Drop { handle: self.id })) - .await; - } -} - -#[async_trait] -impl Revision for super::RevisionHandle { - async fn kv_root_hash(&self) -> Result { - let (send, recv) = oneshot::channel(); - let msg = Request::RevRequest(RevRequest::RootHash { - handle: self.id, - respond_to: send, - }); - let _ = self.sender.send(msg).await; - recv.await.expect("Actor task has been killed") - } - - async fn kv_get + Send + Sync>(&self, key: K) -> Result, DbError> { - let (send, recv) = oneshot::channel(); - let _ = Request::RevRequest::(RevRequest::Get { - handle: self.id, - key: key.as_ref().to_vec(), - respond_to: send, - }); - recv.await.expect("Actor task has been killed") - } - - async fn prove + Send + Sync>(&self, key: K) -> Result, MerkleError> { - let (send, recv) = oneshot::channel(); - let msg = Request::RevRequest(RevRequest::Prove { - handle: self.id, - key: key.as_ref().to_vec(), - respond_to: send, - }); - self.sender.send(msg).await.expect("channel failed"); - recv.await.expect("channel failed") - } - - async fn verify_range_proof + Send + Sync>( - &self, - _proof: Proof, - _first_key: K, - _last_key: K, - _keys: Vec, - _values: Vec, - ) { - todo!() - } - async fn root_hash(&self) -> Result { - let (send, recv) = oneshot::channel(); - let msg = Request::RevRequest(RevRequest::RootHash { - handle: self.id, - respond_to: send, - }); - self.sender.send(msg).await.expect("channel failed"); - recv.await.expect("channel failed") - } - - async fn dump(&self, _writer: W) -> Result<(), DbError> { - todo!() - } - - #[cfg(feature = "eth")] - async fn dump_account + Send + Sync>( - &self, - _key: K, - _writer: W, - ) -> Result<(), DbError> { - todo!() - } - - async fn kv_dump(&self, _writer: W) -> Result<(), DbError> { - unimplemented!(); - } - - #[cfg(feature = "eth")] - async fn get_balance + Send + Sync>( - &self, - _key: K, - ) -> Result { - todo!() - } - - #[cfg(feature = "eth")] - async fn get_code + Send + Sync>(&self, _key: K) -> Result, DbError> { - todo!() - } - - #[cfg(feature = "eth")] - async fn get_nonce + Send + Sync>( - &self, - _key: K, - ) -> Result { - todo!() - } - - #[cfg(feature = "eth")] - async fn get_state + Send + Sync>( - &self, - _key: K, - _sub_key: K, - ) -> Result, DbError> { - todo!() - } -} - -#[async_trait] -impl crate::api::Db, N> for Connection -where - tokio::sync::mpsc::Sender>: From>>, -{ - async fn get_revision(&self, root_hash: TrieHash) -> Option> { - let (send, recv) = oneshot::channel(); - let msg = Request::NewRevision { - root_hash, - respond_to: send, - }; - self.sender - .as_ref() - .unwrap() - .send(msg) - .await - .expect("channel failed"); - let id = recv.await.unwrap(); - id.map(|id| RevisionHandle { - sender: self.sender.as_ref().unwrap().clone(), - id, - }) - } -} - -// TODO: add a meaningful test with `Proposal` and `Revisions`. diff --git a/firewood/src/service/mod.rs b/firewood/src/service/mod.rs deleted file mode 100644 index 77587d744..000000000 --- a/firewood/src/service/mod.rs +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. -// See the file LICENSE.md for licensing terms. - -use tokio::sync::{mpsc, oneshot}; - -use crate::{ - db::DbError, - merkle::{MerkleError, TrieHash}, - v2::api::Proof, -}; - -mod client; -mod server; - -pub type BatchId = u32; -pub type RevId = u32; - -#[derive(Debug)] -pub struct RevisionHandle { - sender: mpsc::Sender>, - id: u32, -} - -/// Client side request object -#[derive(Debug)] -pub enum Request { - NewRevision { - root_hash: TrieHash, - respond_to: oneshot::Sender>, - }, - - RevRequest(RevRequest), -} - -type OwnedKey = Vec; -#[allow(dead_code)] -type OwnedVal = Vec; - -#[derive(Debug)] -pub enum RevRequest { - Get { - handle: RevId, - key: OwnedKey, - respond_to: oneshot::Sender, DbError>>, - }, - Prove { - handle: RevId, - key: OwnedKey, - respond_to: oneshot::Sender, MerkleError>>, - }, - RootHash { - handle: RevId, - respond_to: oneshot::Sender>, - }, - Drop { - handle: RevId, - }, -} diff --git a/firewood/src/service/server.rs b/firewood/src/service/server.rs deleted file mode 100644 index 043f0d76b..000000000 --- a/firewood/src/service/server.rs +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. -// See the file LICENSE.md for licensing terms. - -use std::{ - collections::HashMap, - path::PathBuf, - sync::atomic::{AtomicU32, Ordering}, -}; -use tokio::sync::mpsc::Receiver; - -use crate::db::{Db, DbConfig, DbError}; - -use super::{Request, RevId}; - -macro_rules! get_rev { - ($rev: ident, $handle: ident, $out: expr) => { - match $rev.get(&$handle) { - None => { - let _ = $out.send(Err(DbError::InvalidParams)); - continue; - } - Some(x) => x, - } - }; -} -#[derive(Copy, Debug, Clone)] -pub struct FirewoodService {} - -impl FirewoodService { - pub fn new( - mut receiver: Receiver>, - owned_path: PathBuf, - cfg: DbConfig, - ) -> Self { - let db = Db::new(owned_path, &cfg).unwrap(); - let mut revs = HashMap::new(); - let lastid = AtomicU32::new(0); - loop { - let msg = match receiver.blocking_recv() { - Some(msg) => msg, - None => break, - }; - match msg { - Request::NewRevision { - root_hash, - respond_to, - } => { - let id: RevId = lastid.fetch_add(1, Ordering::Relaxed); - let msg = match db.get_revision(&root_hash) { - Some(rev) => { - revs.insert(id, rev); - Some(id) - } - None => None, - }; - let _ = respond_to.send(msg); - } - - Request::RevRequest(req) => match req { - super::RevRequest::Get { - handle, - key, - respond_to, - } => { - let rev = get_rev!(revs, handle, respond_to); - let msg = rev.kv_get(key); - let _ = respond_to.send(msg.map_or(Err(DbError::KeyNotFound), Ok)); - } - super::RevRequest::Prove { - handle: _handle, - key: _key, - respond_to: _respond_to, - } => { - todo!() - } - super::RevRequest::RootHash { handle, respond_to } => { - let rev = get_rev!(revs, handle, respond_to); - let msg = rev.kv_root_hash(); - let _ = respond_to.send(msg); - } - super::RevRequest::Drop { handle } => { - revs.remove(&handle); - } - }, - } - } - FirewoodService {} - } -} diff --git a/firewood/src/v2/db.rs b/firewood/src/v2/db.rs index 89853eb86..a210d1302 100644 --- a/firewood/src/v2/db.rs +++ b/firewood/src/v2/db.rs @@ -82,33 +82,3 @@ where Ok(proposal) } } - -#[derive(Debug, Default)] -pub struct DbView; - -#[async_trait] -impl api::DbView for DbView { - async fn root_hash(&self) -> Result { - todo!() - } - - async fn val(&self, _key: K) -> Result>, api::Error> { - todo!() - } - - async fn single_key_proof( - &self, - _key: K, - ) -> Result>>, api::Error> { - todo!() - } - - async fn range_proof( - &self, - _first_key: Option, - _last_key: Option, - _limit: usize, - ) -> Result>, api::Error> { - todo!() - } -}