From 27a8acdb04064fa70f476ed3fae83b46ac2eb418 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sat, 5 Oct 2024 19:57:30 +0100 Subject: [PATCH] move chain service to separate file --- binaries/cuprated/src/blockchain.rs | 1 + .../cuprated/src/blockchain/chain_service.rs | 72 +++++++++++++++++++ binaries/cuprated/src/blockchain/manager.rs | 2 +- binaries/cuprated/src/blockchain/types.rs | 64 ----------------- 4 files changed, 74 insertions(+), 65 deletions(-) create mode 100644 binaries/cuprated/src/blockchain/chain_service.rs diff --git a/binaries/cuprated/src/blockchain.rs b/binaries/cuprated/src/blockchain.rs index 1a9c0b7c..a06f3fa7 100644 --- a/binaries/cuprated/src/blockchain.rs +++ b/binaries/cuprated/src/blockchain.rs @@ -19,6 +19,7 @@ use cuprate_types::{ use crate::constants::PANIC_CRITICAL_SERVICE_ERROR; +mod chain_service; pub mod interface; mod manager; mod syncer; diff --git a/binaries/cuprated/src/blockchain/chain_service.rs b/binaries/cuprated/src/blockchain/chain_service.rs new file mode 100644 index 00000000..eeaf4a06 --- /dev/null +++ b/binaries/cuprated/src/blockchain/chain_service.rs @@ -0,0 +1,72 @@ +use std::task::{Context, Poll}; + +use futures::{future::BoxFuture, FutureExt, TryFutureExt}; +use tower::Service; + +use cuprate_blockchain::service::BlockchainReadHandle; +use cuprate_p2p::block_downloader::{ChainSvcRequest, ChainSvcResponse}; +use cuprate_types::blockchain::{BlockchainReadRequest, BlockchainResponse}; + +/// That service that allows retrieving the chain state to give to the P2P crates, so we can figure out +/// what blocks we need. +/// +/// This has a more minimal interface than [`BlockchainReadRequest`] to make using the p2p crates easier. +#[derive(Clone)] +pub struct ChainService(pub BlockchainReadHandle); + +impl Service for ChainService { + type Response = ChainSvcResponse; + type Error = tower::BoxError; + type Future = BoxFuture<'static, Result>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + self.0.poll_ready(cx).map_err(Into::into) + } + + fn call(&mut self, req: ChainSvcRequest) -> Self::Future { + let map_res = |res: BlockchainResponse| match res { + BlockchainResponse::CompactChainHistory { + block_ids, + cumulative_difficulty, + } => ChainSvcResponse::CompactHistory { + block_ids, + cumulative_difficulty, + }, + BlockchainResponse::FindFirstUnknown(res) => ChainSvcResponse::FindFirstUnknown(res), + _ => panic!("Blockchain returned wrong response"), + }; + + match req { + ChainSvcRequest::CompactHistory => self + .0 + .call(BlockchainReadRequest::CompactChainHistory) + .map_ok(map_res) + .map_err(Into::into) + .boxed(), + ChainSvcRequest::FindFirstUnknown(req) => self + .0 + .call(BlockchainReadRequest::FindFirstUnknown(req)) + .map_ok(map_res) + .map_err(Into::into) + .boxed(), + ChainSvcRequest::CumulativeDifficulty => self + .0 + .call(BlockchainReadRequest::CompactChainHistory) + .map_ok(|res| { + // TODO create a custom request instead of hijacking this one. + // TODO: use the context cache. + let BlockchainResponse::CompactChainHistory { + cumulative_difficulty, + .. + } = res + else { + panic!("Blockchain returned wrong response"); + }; + + ChainSvcResponse::CumulativeDifficulty(cumulative_difficulty) + }) + .map_err(Into::into) + .boxed(), + } + } +} diff --git a/binaries/cuprated/src/blockchain/manager.rs b/binaries/cuprated/src/blockchain/manager.rs index 6019592f..f6c11fc0 100644 --- a/binaries/cuprated/src/blockchain/manager.rs +++ b/binaries/cuprated/src/blockchain/manager.rs @@ -24,9 +24,9 @@ use cuprate_types::{ use crate::{ blockchain::{ + chain_service::ChainService, interface::COMMAND_TX, syncer, - types::ChainService, types::{ConcreteBlockVerifierService, ConsensusBlockchainReadHandle}, }, constants::PANIC_CRITICAL_SERVICE_ERROR, diff --git a/binaries/cuprated/src/blockchain/types.rs b/binaries/cuprated/src/blockchain/types.rs index 1bf921ec..e3ee62b3 100644 --- a/binaries/cuprated/src/blockchain/types.rs +++ b/binaries/cuprated/src/blockchain/types.rs @@ -22,67 +22,3 @@ pub type ConcreteTxVerifierService = TxVerifierService tower::BoxError>; - -/// That service that allows retrieving the chain state to give to the P2P crates, so we can figure out -/// what blocks we need. -/// -/// This has a more minimal interface than [`BlockchainReadRequest`] to make using the p2p crates easier. -#[derive(Clone)] -pub struct ChainService(pub BlockchainReadHandle); - -impl Service for ChainService { - type Response = ChainSvcResponse; - type Error = tower::BoxError; - type Future = BoxFuture<'static, Result>; - - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - self.0.poll_ready(cx).map_err(Into::into) - } - - fn call(&mut self, req: ChainSvcRequest) -> Self::Future { - let map_res = |res: BlockchainResponse| match res { - BlockchainResponse::CompactChainHistory { - block_ids, - cumulative_difficulty, - } => ChainSvcResponse::CompactHistory { - block_ids, - cumulative_difficulty, - }, - BlockchainResponse::FindFirstUnknown(res) => ChainSvcResponse::FindFirstUnknown(res), - _ => panic!("Blockchain returned wrong response"), - }; - - match req { - ChainSvcRequest::CompactHistory => self - .0 - .call(BlockchainReadRequest::CompactChainHistory) - .map_ok(map_res) - .map_err(Into::into) - .boxed(), - ChainSvcRequest::FindFirstUnknown(req) => self - .0 - .call(BlockchainReadRequest::FindFirstUnknown(req)) - .map_ok(map_res) - .map_err(Into::into) - .boxed(), - ChainSvcRequest::CumulativeDifficulty => self - .0 - .call(BlockchainReadRequest::CompactChainHistory) - .map_ok(|res| { - // TODO create a custom request instead of hijacking this one. - // TODO: use the context cache. - let BlockchainResponse::CompactChainHistory { - cumulative_difficulty, - .. - } = res - else { - panic!("Blockchain returned wrong response"); - }; - - ChainSvcResponse::CumulativeDifficulty(cumulative_difficulty) - }) - .map_err(Into::into) - .boxed(), - } - } -}