cfxcore/pos/state_sync/
chunk_response.rs1use diem_types::{
9 ledger_info::LedgerInfoWithSignatures,
10 transaction::{TransactionListWithProof, Version},
11};
12use serde::{Deserialize, Serialize};
13use std::fmt;
14
15#[allow(clippy::large_enum_variant)]
19#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
20pub enum ResponseLedgerInfo {
21 VerifiableLedgerInfo(LedgerInfoWithSignatures),
27 ProgressiveLedgerInfo {
29 target_li: LedgerInfoWithSignatures,
32 highest_li: Option<LedgerInfoWithSignatures>,
35 },
36 LedgerInfoForWaypoint {
39 waypoint_li: LedgerInfoWithSignatures,
41 end_of_epoch_li: Option<LedgerInfoWithSignatures>,
44 },
45}
46
47impl ResponseLedgerInfo {
48 pub fn version(&self) -> Version {
51 match self {
52 ResponseLedgerInfo::VerifiableLedgerInfo(li) => {
53 li.ledger_info().version()
54 }
55 ResponseLedgerInfo::ProgressiveLedgerInfo { target_li, .. } => {
56 target_li.ledger_info().version()
57 }
58 ResponseLedgerInfo::LedgerInfoForWaypoint {
59 waypoint_li, ..
60 } => waypoint_li.ledger_info().version(),
61 }
62 }
63}
64
65#[derive(Clone, Deserialize, Eq, PartialEq, Serialize)]
68pub struct GetChunkResponse {
69 pub response_li: ResponseLedgerInfo,
72 pub txn_list_with_proof: TransactionListWithProof,
75}
76
77impl GetChunkResponse {
78 pub fn new(
79 response_li: ResponseLedgerInfo,
80 txn_list_with_proof: TransactionListWithProof,
81 ) -> Self {
82 Self {
83 response_li,
84 txn_list_with_proof,
85 }
86 }
87}
88
89impl fmt::Debug for GetChunkResponse {
90 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91 write!(f, "{}", self)
92 }
93}
94
95impl fmt::Display for GetChunkResponse {
96 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97 let txns_repr = match self.txn_list_with_proof.first_transaction_version
98 {
99 None => "empty".to_string(),
100 Some(first_version) => {
101 let last_version = first_version
102 .checked_add(self.txn_list_with_proof.len() as u64)
103 .and_then(|v| v.checked_sub(1)) .map(|v| v.to_string())
105 .unwrap_or_else(|| "Last version has overflown!".into());
106 format!("versions [{} - {}]", first_version, last_version)
107 }
108 };
109 let response_li_repr = match &self.response_li {
110 ResponseLedgerInfo::VerifiableLedgerInfo(li) => {
111 format!("[verifiable LI {}]", li.ledger_info())
112 }
113 ResponseLedgerInfo::ProgressiveLedgerInfo {
114 target_li,
115 highest_li,
116 } => format!(
117 "[progressive LI: target LI {}, highest LI {}]",
118 target_li.ledger_info(),
119 highest_li.as_ref().unwrap_or(target_li).ledger_info(),
120 ),
121 ResponseLedgerInfo::LedgerInfoForWaypoint {
122 waypoint_li,
123 end_of_epoch_li,
124 } => format!(
125 "[waypoint LI {}, end of epoch LI {}]",
126 waypoint_li.ledger_info(),
127 end_of_epoch_li.as_ref().map_or("None".to_string(), |li| li
128 .ledger_info()
129 .to_string())
130 ),
131 };
132 write!(
133 f,
134 "[ChunkResponse: response li: {}, txns: {}]",
135 response_li_repr, txns_repr,
136 )
137 }
138}