-
Notifications
You must be signed in to change notification settings - Fork 10
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
b0ce83f
commit 4739cd0
Showing
5 changed files
with
193 additions
and
82 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -7,3 +7,5 @@ pub mod rpcdb { | |
} | ||
|
||
pub mod service; | ||
|
||
pub use service::Database as DatabaseService; |
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 +1,80 @@ | ||
use firewood::v2::{ | ||
api::{Db, Error}, | ||
emptydb::{EmptyDb, HistoricalImpl}, | ||
}; | ||
use std::{ | ||
collections::HashMap, | ||
sync::{ | ||
atomic::{AtomicU64, Ordering}, | ||
Arc, | ||
}, | ||
}; | ||
use tokio::sync::Mutex; | ||
use tonic::Status; | ||
|
||
pub mod database; | ||
pub mod db; | ||
|
||
trait IntoStatusResult<T> { | ||
fn into_status_result(self) -> Result<T, Status>; | ||
} | ||
|
||
impl<T> IntoStatusResult<T> for Result<T, Error> { | ||
fn into_status_result(self) -> Result<T, Status> { | ||
self.map_err(|err| match err { | ||
Error::HashNotFound { provided: _ } => todo!(), | ||
Error::IncorrectRootHash { | ||
provided: _, | ||
current: _, | ||
} => todo!(), | ||
Error::IO(_) => todo!(), | ||
Error::InvalidProposal => todo!(), | ||
_ => todo!(), | ||
}) | ||
} | ||
} | ||
pub struct Database { | ||
db: EmptyDb, | ||
iterators: Arc<Mutex<Iterators>>, | ||
} | ||
|
||
impl Default for Database { | ||
fn default() -> Self { | ||
Self { | ||
db: EmptyDb, | ||
iterators: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Database { | ||
async fn revision(&self) -> Result<Arc<HistoricalImpl>, Error> { | ||
let root_hash = self.db.root_hash().await?; | ||
self.db.revision(root_hash).await | ||
} | ||
} | ||
|
||
// TODO: implement Iterator | ||
struct Iter; | ||
|
||
#[derive(Default)] | ||
struct Iterators { | ||
map: HashMap<u64, Iter>, | ||
next_id: AtomicU64, | ||
} | ||
|
||
impl Iterators { | ||
fn insert(&mut self, iter: Iter) -> u64 { | ||
let id = self.next_id.fetch_add(1, Ordering::Relaxed); | ||
self.map.insert(id, iter); | ||
id | ||
} | ||
|
||
fn _get(&self, id: u64) -> Option<&Iter> { | ||
self.map.get(&id) | ||
} | ||
|
||
fn remove(&mut self, id: u64) { | ||
self.map.remove(&id); | ||
} | ||
} |
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,101 @@ | ||
use super::{Database, IntoStatusResult}; | ||
use crate::sync::{ | ||
db_server::Db as DbServerTrait, CommitChangeProofRequest, CommitRangeProofRequest, | ||
GetChangeProofRequest, GetChangeProofResponse, GetMerkleRootResponse, GetProofRequest, | ||
GetProofResponse, GetRangeProofRequest, GetRangeProofResponse, VerifyChangeProofRequest, | ||
VerifyChangeProofResponse, | ||
}; | ||
use firewood::v2::api::Db; | ||
use tonic::{async_trait, Request, Response, Status}; | ||
|
||
#[async_trait] | ||
impl DbServerTrait for Database { | ||
async fn get_merkle_root( | ||
&self, | ||
_request: Request<()>, | ||
) -> Result<Response<GetMerkleRootResponse>, Status> { | ||
let root_hash = self.db.root_hash().await.into_status_result()?.to_vec(); | ||
|
||
let response = GetMerkleRootResponse { root_hash }; | ||
|
||
Ok(Response::new(response)) | ||
} | ||
|
||
async fn get_proof( | ||
&self, | ||
request: Request<GetProofRequest>, | ||
) -> Result<Response<GetProofResponse>, Status> { | ||
let GetProofRequest { key: _ } = request.into_inner(); | ||
let _revision = self.revision().await.into_status_result()?; | ||
|
||
todo!() | ||
} | ||
|
||
async fn get_change_proof( | ||
&self, | ||
request: Request<GetChangeProofRequest>, | ||
) -> Result<Response<GetChangeProofResponse>, Status> { | ||
let GetChangeProofRequest { | ||
start_root_hash: _, | ||
end_root_hash: _, | ||
start_key: _, | ||
end_key: _, | ||
key_limit: _, | ||
} = request.into_inner(); | ||
|
||
let _revision = self.revision().await.into_status_result()?; | ||
|
||
todo!() | ||
} | ||
|
||
async fn verify_change_proof( | ||
&self, | ||
request: Request<VerifyChangeProofRequest>, | ||
) -> Result<Response<VerifyChangeProofResponse>, Status> { | ||
let VerifyChangeProofRequest { | ||
proof: _, | ||
start_key: _, | ||
end_key: _, | ||
expected_root_hash: _, | ||
} = request.into_inner(); | ||
|
||
let _revision = self.revision().await.into_status_result()?; | ||
|
||
todo!() | ||
} | ||
|
||
async fn commit_change_proof( | ||
&self, | ||
request: Request<CommitChangeProofRequest>, | ||
) -> Result<Response<()>, Status> { | ||
let CommitChangeProofRequest { proof: _ } = request.into_inner(); | ||
|
||
todo!() | ||
} | ||
|
||
async fn get_range_proof( | ||
&self, | ||
request: Request<GetRangeProofRequest>, | ||
) -> Result<Response<GetRangeProofResponse>, Status> { | ||
let GetRangeProofRequest { | ||
root_hash: _, | ||
start_key: _, | ||
end_key: _, | ||
key_limit: _, | ||
} = request.into_inner(); | ||
|
||
todo!() | ||
} | ||
|
||
async fn commit_range_proof( | ||
&self, | ||
request: Request<CommitRangeProofRequest>, | ||
) -> Result<Response<()>, Status> { | ||
let CommitRangeProofRequest { | ||
start_key: _, | ||
range_proof: _, | ||
} = request.into_inner(); | ||
|
||
todo!() | ||
} | ||
} |