Skip to content

Commit 8b924ea

Browse files
committed
address comments
1 parent f5d6045 commit 8b924ea

File tree

6 files changed

+50
-103
lines changed

6 files changed

+50
-103
lines changed

src/chain_sync/sync_status.rs

Lines changed: 19 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::sync::Arc;
1313
use tracing::log;
1414

1515
// Node considered synced if the head is within this threshold.
16-
const SYNCED_EPOCH_THRESHOLD: u64 = 5;
16+
const SYNCED_EPOCH_THRESHOLD: u64 = 10;
1717

1818
/// Represents the overall synchronization status of the Forest node.
1919
#[derive(
@@ -41,10 +41,10 @@ pub enum NodeSyncStatus {
4141
#[strum(to_string = "Synced")]
4242
Synced,
4343
/// An error occurred during the sync process.
44-
#[strum(to_string = "error")]
44+
#[strum(to_string = "Error")]
4545
Error,
4646
/// Node is configured to not sync (offline mode).
47-
#[strum(to_string = "offline")]
47+
#[strum(to_string = "Offline")]
4848
Offline,
4949
}
5050

@@ -74,7 +74,7 @@ pub enum ForkSyncStage {
7474
#[strum(to_string = "Stalled")]
7575
Stalled,
7676
/// An error occurred processing this specific fork.
77-
#[strum(to_string = "error")]
77+
#[strum(to_string = "Error")]
7878
Error,
7979
}
8080

@@ -105,24 +105,24 @@ pub struct ForkSyncInfo {
105105
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, JsonSchema)]
106106
pub struct SyncStatusReport {
107107
/// Overall status of the node's synchronization.
108-
status: NodeSyncStatus,
108+
pub(crate) status: NodeSyncStatus,
109109
/// The epoch of the heaviest validated tipset on the node's main chain.
110-
current_head_epoch: ChainEpoch,
110+
pub(crate) current_head_epoch: ChainEpoch,
111111
/// The tipset key of the current heaviest validated tipset.
112112
#[schemars(with = "crate::lotus_json::LotusJson<TipsetKey>")]
113113
#[serde(with = "crate::lotus_json")]
114-
current_head_key: Option<TipsetKey>,
114+
pub(crate) current_head_key: Option<TipsetKey>,
115115
// Current highest epoch on the network.
116-
network_head_epoch: ChainEpoch,
116+
pub(crate) network_head_epoch: ChainEpoch,
117117
/// Estimated number of epochs the node is behind the network head.
118118
/// Can be negative if the node is slightly ahead, due to estimation variance.
119-
epochs_behind: i64,
119+
pub(crate) epochs_behind: i64,
120120
/// List of active fork synchronization tasks the node is currently handling.
121-
active_forks: Vec<ForkSyncInfo>,
121+
pub(crate) active_forks: Vec<ForkSyncInfo>,
122122
/// When the node process started.
123-
node_start_time: DateTime<Utc>,
123+
pub(crate) node_start_time: DateTime<Utc>,
124124
/// Last time this status report was generated.
125-
last_updated: DateTime<Utc>,
125+
pub(crate) last_updated: DateTime<Utc>,
126126
}
127127

128128
lotus_json_with_self!(SyncStatusReport);
@@ -135,58 +135,6 @@ impl SyncStatusReport {
135135
}
136136
}
137137

138-
pub(crate) fn set_current_chain_head_key(&mut self, tipset_key: TipsetKey) {
139-
self.current_head_key = Some(tipset_key);
140-
}
141-
142-
pub(crate) fn set_current_chain_head_epoch(&mut self, epoch: ChainEpoch) {
143-
self.current_head_epoch = epoch;
144-
}
145-
146-
pub(crate) fn get_current_chain_head_epoch(&self) -> ChainEpoch {
147-
self.current_head_epoch
148-
}
149-
150-
pub(crate) fn set_network_head(&mut self, epoch: ChainEpoch) {
151-
self.network_head_epoch = epoch;
152-
}
153-
154-
pub(crate) fn get_network_head_epoch(&self) -> ChainEpoch {
155-
self.network_head_epoch
156-
}
157-
158-
pub(crate) fn get_current_chain_head_key(&self) -> Option<&TipsetKey> {
159-
self.current_head_key.as_ref()
160-
}
161-
162-
pub(crate) fn set_epochs_behind(&mut self, epochs_behind: i64) {
163-
self.epochs_behind = epochs_behind;
164-
}
165-
166-
pub(crate) fn get_epochs_behind(&self) -> i64 {
167-
self.epochs_behind
168-
}
169-
170-
pub(crate) fn set_status(&mut self, status: NodeSyncStatus) {
171-
self.status = status;
172-
}
173-
174-
pub(crate) fn get_status(&self) -> NodeSyncStatus {
175-
self.status
176-
}
177-
178-
pub(crate) fn update_active_forks(&mut self, active_forks: Vec<ForkSyncInfo>) {
179-
self.active_forks = active_forks;
180-
}
181-
182-
pub(crate) fn get_active_forks(&self) -> &Vec<ForkSyncInfo> {
183-
&self.active_forks
184-
}
185-
186-
pub fn get_last_updated(&self) -> &DateTime<Utc> {
187-
&self.last_updated
188-
}
189-
190138
pub(crate) fn update<DB: Blockstore + Sync + Send + 'static>(
191139
&mut self,
192140
state_manager: &Arc<StateManager<DB>>,
@@ -195,8 +143,8 @@ impl SyncStatusReport {
195143
) {
196144
let heaviest = state_manager.chain_store().heaviest_tipset();
197145
let current_chain_head_epoch = heaviest.epoch();
198-
self.set_current_chain_head_key(heaviest.key().clone());
199-
self.set_current_chain_head_epoch(current_chain_head_epoch);
146+
self.current_head_key = Some(heaviest.key().clone());
147+
self.current_head_epoch = current_chain_head_epoch;
200148

201149
let now = Utc::now();
202150
let now_ts = now.timestamp() as u64;
@@ -207,13 +155,13 @@ impl SyncStatusReport {
207155
seconds_per_epoch,
208156
);
209157

210-
self.set_network_head(network_head_epoch);
211-
self.set_epochs_behind(network_head_epoch.saturating_sub(current_chain_head_epoch));
158+
self.network_head_epoch = network_head_epoch;
159+
self.epochs_behind = network_head_epoch.saturating_sub(current_chain_head_epoch);
212160
log::trace!(
213161
"Sync status report: current head epoch: {}, network head epoch: {}, epochs behind: {}",
214162
current_chain_head_epoch,
215163
network_head_epoch,
216-
self.get_epochs_behind()
164+
self.epochs_behind
217165
);
218166

219167
let time_diff = now_ts.saturating_sub(heaviest.min_timestamp());
@@ -228,8 +176,8 @@ impl SyncStatusReport {
228176
}
229177
};
230178

231-
self.set_status(status);
232-
self.update_active_forks(current_active_forks);
179+
self.status = status;
180+
self.active_forks = current_active_forks;
233181
self.last_updated = now;
234182
}
235183

src/cli/subcommands/sync_cmd.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ impl SyncCommands {
5555
.context("Failed to get sync status")?;
5656

5757
// Skip printing if initializing, since it's not useful to print
58-
if report.get_status() == NodeSyncStatus::Initializing {
58+
if report.status == NodeSyncStatus::Initializing {
5959
continue;
6060
}
6161

6262
clear_previous_lines(&mut stdout, lines_printed_last_iteration)?;
6363

64-
lines_printed_last_iteration += print_sync_report_details(&report)
64+
lines_printed_last_iteration = print_sync_report_details(&report)
6565
.context("Failed to print sync status report")?;
6666

6767
// Exit if synced and not in watch mode.
68-
if !watch && report.get_status() == NodeSyncStatus::Synced {
68+
if !watch && report.status == NodeSyncStatus::Synced {
6969
println!("\nSync complete!");
7070
break;
7171
}
@@ -76,7 +76,7 @@ impl SyncCommands {
7676

7777
Self::Status => {
7878
let sync_status = client.call(SyncStatus::request(())?).await?;
79-
if sync_status.get_status() == NodeSyncStatus::Initializing {
79+
if sync_status.status == NodeSyncStatus::Initializing {
8080
print!("Node initializing, checking snapshot status..\n\n");
8181
// If a snapshot is required and not yet complete, return here
8282
if !check_snapshot_progress(&client, false)
@@ -117,30 +117,29 @@ fn print_sync_report_details(report: &SyncStatusReport) -> anyhow::Result<usize>
117117

118118
println!(
119119
"Status: {:?} ({} epochs behind)",
120-
report.get_status(),
121-
report.get_epochs_behind()
120+
report.status, report.epochs_behind
122121
);
123122
lines_printed_count += 1;
124123

125124
let head_key_str = report
126-
.get_current_chain_head_key()
125+
.current_head_key
126+
.as_ref()
127127
.map(tipset_key_to_string)
128128
.unwrap_or_else(|| "[unknown]".to_string());
129129
println!(
130130
"Node Head: Epoch {} ({})",
131-
report.get_current_chain_head_epoch(),
132-
head_key_str
131+
report.current_head_epoch, head_key_str
133132
);
134133
lines_printed_count += 1;
135134

136-
println!("Network Head: Epoch {}", report.get_network_head_epoch());
135+
println!("Network Head: Epoch {}", report.network_head_epoch);
137136
lines_printed_count += 1;
138137

139-
println!("Last Update: {}", report.get_last_updated().to_rfc3339());
138+
println!("Last Update: {}", report.last_updated.to_rfc3339());
140139
lines_printed_count += 1;
141140

142141
// Print active sync tasks (forks)
143-
let active_forks = report.get_active_forks();
142+
let active_forks = &report.active_forks;
144143
if active_forks.is_empty() {
145144
println!("Active Sync Tasks: None");
146145
lines_printed_count += 1;
@@ -238,7 +237,7 @@ async fn handle_initial_snapshot_check(client: &rpc::Client) -> anyhow::Result<(
238237
let initial_report = SyncStatus::call(client, ())
239238
.await
240239
.context("Failed to get sync status")?;
241-
if initial_report.get_status() == NodeSyncStatus::Initializing {
240+
if initial_report.status == NodeSyncStatus::Initializing {
242241
print!("Node initializing, checking snapshot status...\n\n");
243242
// if the snapshot download is not required, then return,
244243
// else wait till the snapshot download is completed.

src/health/endpoints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub(crate) async fn healthz(
9595

9696
fn check_sync_status_synced(state: &ForestState, acc: &mut MessageAccumulator) -> bool {
9797
// Forest must be in sync with the network
98-
if state.sync_status.read().get_status() == NodeSyncStatus::Synced {
98+
if state.sync_status.read().status == NodeSyncStatus::Synced {
9999
acc.push_ok("sync complete");
100100
true
101101
} else {
@@ -106,7 +106,7 @@ fn check_sync_status_synced(state: &ForestState, acc: &mut MessageAccumulator) -
106106

107107
fn check_sync_status_not_error(state: &ForestState, acc: &mut MessageAccumulator) -> bool {
108108
// Forest must be in sync with the network
109-
if state.sync_status.read().get_status() != NodeSyncStatus::Error {
109+
if state.sync_status.read().status != NodeSyncStatus::Error {
110110
acc.push_ok("sync ok");
111111
true
112112
} else {
@@ -128,7 +128,7 @@ fn check_epoch_up_to_date(state: &ForestState, acc: &mut MessageAccumulator) ->
128128
);
129129

130130
// The current epoch of the node must be not too far behind the network
131-
if state.sync_status.read().get_current_chain_head_epoch() >= now_epoch - MAX_EPOCH_DIFF {
131+
if state.sync_status.read().current_head_epoch >= now_epoch - MAX_EPOCH_DIFF {
132132
acc.push_ok("epoch up to date");
133133
true
134134
} else {

src/health/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ mod test {
118118
};
119119

120120
// instrument the state so that the ready requirements are met
121-
sync_status.write().set_status(NodeSyncStatus::Synced);
122-
sync_status.write().set_current_chain_head_epoch(i64::MAX);
121+
sync_status.write().status = NodeSyncStatus::Synced;
122+
sync_status.write().current_head_epoch = i64::MAX;
123123
db.set_eth_mapping_up_to_date().unwrap();
124124

125125
assert_eq!(
@@ -136,8 +136,8 @@ mod test {
136136

137137
// instrument the state so that the ready requirements are not met
138138
drop(rpc_listener);
139-
sync_status.write().set_status(NodeSyncStatus::Error);
140-
sync_status.write().set_current_chain_head_epoch(0);
139+
sync_status.write().status = NodeSyncStatus::Error;
140+
sync_status.write().current_head_epoch = 0;
141141

142142
assert_eq!(
143143
call_healthcheck(false).await.unwrap().status(),
@@ -198,7 +198,7 @@ mod test {
198198
};
199199

200200
// instrument the state so that the live requirements are met
201-
sync_status.write().set_status(NodeSyncStatus::Syncing);
201+
sync_status.write().status = NodeSyncStatus::Syncing;
202202
let peer = libp2p::PeerId::random();
203203
peer_manager.touch_peer(&peer);
204204

@@ -214,7 +214,7 @@ mod test {
214214
assert!(text.contains("[+] peers connected"));
215215

216216
// instrument the state so that the live requirements are not met
217-
sync_status.write().set_status(NodeSyncStatus::Error);
217+
sync_status.write().status = NodeSyncStatus::Error;
218218
peer_manager.remove_peer(&peer);
219219

220220
assert_eq!(
@@ -274,8 +274,8 @@ mod test {
274274
};
275275

276276
// instrument the state so that the health requirements are met
277-
sync_status.write().set_current_chain_head_epoch(i64::MAX);
278-
sync_status.write().set_status(NodeSyncStatus::Syncing);
277+
sync_status.write().current_head_epoch = i64::MAX;
278+
sync_status.write().status = NodeSyncStatus::Syncing;
279279
let peer = libp2p::PeerId::random();
280280
peer_manager.touch_peer(&peer);
281281

@@ -293,8 +293,8 @@ mod test {
293293

294294
// instrument the state so that the health requirements are not met
295295
drop(rpc_listener);
296-
sync_status.write().set_status(NodeSyncStatus::Error);
297-
sync_status.write().set_current_chain_head_epoch(0);
296+
sync_status.write().status = NodeSyncStatus::Error;
297+
sync_status.write().current_head_epoch = 0;
298298
peer_manager.remove_peer(&peer);
299299

300300
assert_eq!(

src/rpc/methods/eth.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,7 @@ impl RpcMethod<0> for EthSyncing {
16711671
) -> Result<Self::Ok, ServerError> {
16721672
let sync_status: crate::chain_sync::SyncStatusReport =
16731673
crate::rpc::sync::SyncStatus::handle(ctx, ()).await?;
1674-
match sync_status.get_status() {
1674+
match sync_status.status {
16751675
NodeSyncStatus::Syncing => {
16761676
let starting_block = match sync_status.get_min_starting_block() {
16771677
Some(e) => Ok(e),
@@ -1684,8 +1684,8 @@ impl RpcMethod<0> for EthSyncing {
16841684
Ok(EthSyncingResult {
16851685
done_sync: sync_status.is_synced(),
16861686
starting_block,
1687-
current_block: sync_status.get_current_chain_head_epoch(),
1688-
highest_block: sync_status.get_network_head_epoch(),
1687+
current_block: sync_status.current_head_epoch,
1688+
highest_block: sync_status.network_head_epoch,
16891689
})
16901690
}
16911691
_ => Err(ServerError::internal_error("sync status not found", None)),

src/rpc/methods/sync.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl RpcMethod<1> for SyncSubmitBlock {
105105
ctx: Ctx<impl Blockstore>,
106106
(block_msg,): Self::Params,
107107
) -> Result<Self::Ok, ServerError> {
108-
if !matches!(ctx.sync_status.read().get_status(), NodeSyncStatus::Synced) {
108+
if !matches!(ctx.sync_status.read().status, NodeSyncStatus::Synced) {
109109
Err(anyhow!("the node isn't in 'follow' mode"))?
110110
}
111111
let encoded_message = to_vec(&block_msg)?;
@@ -272,8 +272,8 @@ mod tests {
272272
assert_eq!(sync_status, st_copy.as_ref().read().clone());
273273

274274
// update cloned state
275-
st_copy.write().set_status(NodeSyncStatus::Syncing);
276-
st_copy.write().set_current_chain_head_epoch(4);
275+
st_copy.write().status = NodeSyncStatus::Syncing;
276+
st_copy.write().current_head_epoch = 4;
277277

278278
let sync_status = SyncStatus::handle(ctx.clone(), ()).await.unwrap();
279279

0 commit comments

Comments
 (0)