cuprated/p2p/
core_sync_service.rs1use std::{
2 future::{ready, Ready},
3 task::{Context, Poll},
4};
5
6use futures::{future::BoxFuture, FutureExt, TryFutureExt};
7use tower::Service;
8
9use cuprate_consensus::{
10 BlockChainContextRequest, BlockChainContextResponse, BlockchainContextService,
11};
12use cuprate_helper::{cast::usize_to_u64, map::split_u128_into_low_high_bits};
13use cuprate_p2p_core::services::{CoreSyncDataRequest, CoreSyncDataResponse};
14use cuprate_wire::CoreSyncData;
15
16#[derive(Clone)]
18pub struct CoreSyncService(pub BlockchainContextService);
19
20impl Service<CoreSyncDataRequest> for CoreSyncService {
21 type Response = CoreSyncDataResponse;
22 type Error = tower::BoxError;
23 type Future = Ready<Result<Self::Response, Self::Error>>;
24
25 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
26 Poll::Ready(Ok(()))
27 }
28
29 fn call(&mut self, _: CoreSyncDataRequest) -> Self::Future {
30 let context = self.0.blockchain_context();
31
32 let (cumulative_difficulty, cumulative_difficulty_top64) =
33 split_u128_into_low_high_bits(context.cumulative_difficulty);
34
35 ready(Ok(CoreSyncDataResponse(CoreSyncData {
36 cumulative_difficulty,
37 cumulative_difficulty_top64,
38 current_height: usize_to_u64(context.chain_height),
39 pruning_seed: 0,
40 top_id: context.top_hash,
41 top_version: context.current_hf.as_u8(),
42 })))
43 }
44}