zebra/zebra-state/src/in_memory.rs

79 lines
2.2 KiB
Rust
Raw Normal View History

//! A basic implementation of the zebra-state service entirely in memory
//!
//! This service is provided as an independent implementation of the
//! zebra-state service to use in verifying the correctness of `on_disk`'s
//! `Service` implementation.
2020-06-04 10:17:49 -07:00
use super::{Request, Response};
use futures::prelude::*;
use std::{
error,
2020-06-04 10:17:49 -07:00
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tower::{buffer::Buffer, Service};
mod block_index;
#[derive(Default)]
struct InMemoryState {
2020-06-04 10:17:49 -07:00
index: block_index::BlockIndex,
}
type Error = Box<dyn error::Error + Send + Sync + 'static>;
impl Service<Request> for InMemoryState {
2020-06-04 10:17:49 -07:00
type Response = Response;
type Error = Error;
2020-06-04 10:17:49 -07:00
type Future =
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request) -> Self::Future {
match req {
Request::AddBlock { block } => {
let result = self
.index
.insert(block)
.map(|hash| Response::Added { hash });
2020-06-04 10:17:49 -07:00
async { result }.boxed()
}
Request::GetBlock { hash } => {
let result = self
.index
.get(hash)
.map(|block| Response::Block { block })
.ok_or_else(|| "block could not be found".into());
async move { result }.boxed()
}
Request::GetTip => {
let result = self
.index
.get_tip()
.map(|hash| Response::Tip { hash })
.ok_or_else(|| "zebra-state contains no blocks".into());
async move { result }.boxed()
}
}
}
}
/// Return's a type that implement's the `zebra_state::Service` entirely in
/// memory using `HashMaps`
2020-06-04 10:17:49 -07:00
pub fn init() -> impl Service<
Request,
Response = Response,
Error = Error,
Future = impl Future<Output = Result<Response, Error>>,
2020-06-04 10:17:49 -07:00
> + Send
+ Clone
+ 'static {
Buffer::new(InMemoryState::default(), 1)
2020-06-04 10:17:49 -07:00
}