cfxcore/sync/message/
snapshot_chunk_request.rs1use crate::{
6 message::{
7 GetMaybeRequestId, Message, MessageProtocolVersionBound, MsgId,
8 RequestId, SetRequestId,
9 },
10 sync::{
11 message::{
12 msgid, Context, DynamicCapability, Handleable, KeyContainer,
13 SnapshotChunkResponse,
14 },
15 request_manager::{AsAny, Request},
16 state::storage::{Chunk, ChunkKey, SnapshotSyncCandidate},
17 Error, ProtocolConfiguration, SYNC_PROTO_V1, SYNC_PROTO_V3,
18 },
19};
20use malloc_size_of_derive::MallocSizeOf as DeriveMallocSizeOf;
21use network::service::ProtocolVersion;
22use rlp::Encodable;
23use rlp_derive::{RlpDecodable, RlpEncodable};
24use std::{any::Any, time::Duration};
25
26#[derive(Debug, Clone, RlpDecodable, RlpEncodable, DeriveMallocSizeOf)]
27pub struct SnapshotChunkRequest {
28 pub request_id: u64,
31 pub snapshot_to_sync: SnapshotSyncCandidate,
32 pub chunk_key: ChunkKey,
33}
34
35build_msg_with_request_id_impl! {
36 SnapshotChunkRequest, msgid::GET_SNAPSHOT_CHUNK,
37 "SnapshotChunkRequest", SYNC_PROTO_V1, SYNC_PROTO_V3
38}
39
40impl SnapshotChunkRequest {
41 pub fn new(
42 snapshot_sync_candidate: SnapshotSyncCandidate, chunk_key: ChunkKey,
43 ) -> Self {
44 SnapshotChunkRequest {
45 request_id: 0,
46 snapshot_to_sync: snapshot_sync_candidate,
47 chunk_key,
48 }
49 }
50}
51
52impl Handleable for SnapshotChunkRequest {
53 fn handle(self, ctx: &Context) -> Result<(), Error> {
54 let snapshot_epoch_id = match &self.snapshot_to_sync {
55 SnapshotSyncCandidate::FullSync {
56 snapshot_epoch_id, ..
57 } => snapshot_epoch_id,
58 };
59 let chunk = match Chunk::load(
60 snapshot_epoch_id,
61 &self.chunk_key,
62 &ctx.manager.graph.data_man.storage_manager,
63 ctx.manager.protocol_config.chunk_size_byte * 2,
64 ) {
65 Ok(Some(chunk)) => chunk,
66 Err(r) => return Err(r),
67 _ => Chunk::default(),
68 };
69
70 ctx.send_response(&SnapshotChunkResponse {
71 request_id: self.request_id,
72 chunk,
73 })
74 }
75}
76
77impl AsAny for SnapshotChunkRequest {
78 fn as_any(&self) -> &dyn Any { self }
79
80 fn as_any_mut(&mut self) -> &mut dyn Any { self }
81}
82
83impl Request for SnapshotChunkRequest {
84 fn timeout(&self, conf: &ProtocolConfiguration) -> Duration {
85 conf.snapshot_chunk_request_timeout
86 }
87
88 fn on_removed(&self, _inflight_keys: &KeyContainer) {}
89
90 fn with_inflight(&mut self, _inflight_keys: &KeyContainer) {}
91
92 fn is_empty(&self) -> bool { false }
93
94 fn resend(&self) -> Option<Box<dyn Request>> { None }
95
96 fn required_capability(&self) -> Option<DynamicCapability> { None }
97}