-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add(scan): Create a tower Service in zebra-scan (#8185)
* Adds ScanService and ScanTask * renames ScannerCommand to ScanTaskCommand * fixes doc errors * fixes clippy lints * panic if the scan task finishes unexpectedly * updates TODOs --------- Co-authored-by: Marek <[email protected]>
- Loading branch information
Showing
9 changed files
with
205 additions
and
2 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
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,4 @@ | ||
//! Request and response types for zebra-scan tower service. | ||
pub mod request; | ||
pub mod response; |
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,23 @@ | ||
//! `zebra_scan::service::ScanService` request types. | ||
#[derive(Debug)] | ||
/// Request types for `zebra_scan::service::ScanService` | ||
pub enum Request { | ||
/// TODO: Accept `KeyHash`es and return key hashes that are registered | ||
CheckKeyHashes(Vec<()>), | ||
|
||
/// TODO: Accept `ViewingKeyWithHash`es and return Ok(()) if successful or an error | ||
RegisterKeys(Vec<()>), | ||
|
||
/// TODO: Accept `KeyHash`es and return Ok(`Vec<KeyHash>`) with hashes of deleted keys | ||
DeleteKeys(Vec<()>), | ||
|
||
/// TODO: Accept `KeyHash`es and return `Transaction`s | ||
Results(Vec<()>), | ||
|
||
/// TODO: Accept `KeyHash`es and return a channel receiver | ||
SubscribeResults(Vec<()>), | ||
|
||
/// TODO: Accept `KeyHash`es and return transaction ids | ||
ClearResults(Vec<()>), | ||
} |
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,15 @@ | ||
//! `zebra_scan::service::ScanService` response types. | ||
use std::sync::{mpsc, Arc}; | ||
|
||
use zebra_chain::transaction::Transaction; | ||
|
||
#[derive(Debug)] | ||
/// Response types for `zebra_scan::service::ScanService` | ||
pub enum Response { | ||
/// Response to Results request | ||
Results(Vec<Transaction>), | ||
|
||
/// Response to SubscribeResults request | ||
SubscribeResults(mpsc::Receiver<Arc<Transaction>>), | ||
} |
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//! [`tower::Service`] for zebra-scan. | ||
use std::{future::Future, pin::Pin, task::Poll}; | ||
|
||
use futures::future::FutureExt; | ||
use tower::Service; | ||
|
||
use zebra_chain::parameters::Network; | ||
use zebra_state::ChainTipChange; | ||
|
||
use crate::{init::ScanTask, scan, storage::Storage, Config, Request, Response}; | ||
|
||
/// Zebra-scan [`tower::Service`] | ||
#[derive(Debug)] | ||
pub struct ScanService { | ||
/// On-disk storage | ||
db: Storage, | ||
|
||
/// Handle to scan task that's responsible for writing results | ||
scan_task: ScanTask, | ||
} | ||
|
||
impl ScanService { | ||
/// Create a new [`ScanService`]. | ||
pub fn _new( | ||
config: &Config, | ||
network: Network, | ||
state: scan::State, | ||
chain_tip_change: ChainTipChange, | ||
) -> Self { | ||
Self { | ||
db: Storage::new(config, network, false), | ||
scan_task: ScanTask::spawn(config, network, state, chain_tip_change), | ||
} | ||
} | ||
} | ||
|
||
impl Service<Request> for ScanService { | ||
type Response = Response; | ||
type Error = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
type Future = | ||
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>; | ||
|
||
fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
// TODO: If scan task returns an error, add error to the panic message | ||
assert!( | ||
!self.scan_task.handle.is_finished(), | ||
"scan task finished unexpectedly" | ||
); | ||
|
||
self.db.check_for_panics(); | ||
|
||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn call(&mut self, req: Request) -> Self::Future { | ||
match req { | ||
Request::CheckKeyHashes(_key_hashes) => { | ||
// TODO: check that these entries exist in db | ||
} | ||
|
||
Request::RegisterKeys(_viewing_key_with_hashes) => { | ||
// TODO: | ||
// - add these keys as entries in db | ||
// - send new keys to scan task | ||
} | ||
|
||
Request::DeleteKeys(_key_hashes) => { | ||
// TODO: | ||
// - delete these keys and their results from db | ||
// - send deleted keys to scan task | ||
} | ||
|
||
Request::Results(_key_hashes) => { | ||
// TODO: read results from db | ||
} | ||
|
||
Request::SubscribeResults(_key_hashes) => { | ||
// TODO: send key_hashes and mpsc::Sender to scanner task, return mpsc::Receiver to caller | ||
} | ||
|
||
Request::ClearResults(_key_hashes) => { | ||
// TODO: clear results for these keys from db | ||
} | ||
} | ||
|
||
async move { Ok(Response::Results(vec![])) }.boxed() | ||
} | ||
} |