Skip to content

Commit f9d1342

Browse files
committed
reorg fix
1 parent b71ea44 commit f9d1342

File tree

5 files changed

+371
-78
lines changed

5 files changed

+371
-78
lines changed

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ pub struct Net {
4141
/// Optional static ban list of libp2p PeerIds. Connections and dials to these peers are blocked.
4242
#[serde(default)]
4343
pub banned_peer_ids: Vec<String>,
44+
/// Suppress routine network gossip logs by default (overridden by CLI --quiet-net)
45+
#[serde(default)]
46+
pub quiet_by_default: bool,
4447
}
4548

4649
#[derive(Debug, Deserialize, Clone)]

src/epoch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ impl Manager {
377377
match window {
378378
Some(w) if w.len() as u64 == RETARGET_INTERVAL => calculate_retarget_consensus(&w),
379379
_ => {
380-
eprintln!("🔥 Retarget window incomplete starting at {}", start);
381-
prev_anchor.as_ref().map_or((TARGET_LEADING_ZEROS, DEFAULT_MEM_KIB), |p| (p.difficulty, p.mem_kib))
380+
eprintln!(" Retarget window incomplete starting at {} — waiting before emitting retarget epoch {}", start, current_epoch);
381+
continue;
382382
}
383383
}
384384
} else {

src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ async fn main() -> anyhow::Result<()> {
264264
println!("--- unchained Node ---");
265265

266266
let cli = Cli::parse();
267-
if cli.quiet_net { network::set_quiet_logging(true); }
268267

269268
// Try reading config from CLI path, then from the executable directory, else fallback to embedded default
270269
let mut cfg = match config::load(&cli.config) {
@@ -298,7 +297,10 @@ async fn main() -> anyhow::Result<()> {
298297
}
299298
};
300299

301-
// Resolve storage path: if relative, place under user's home at ~/.unchained/unchained_data
300+
// Apply quiet logging preference: CLI flag overrides config
301+
if cli.quiet_net {
302+
network::set_quiet_logging(true);
303+
}
302304
if std::path::Path::new(&cfg.storage.path).is_relative() {
303305
let home = std::env::var("HOME")
304306
.or_else(|_| std::env::var("USERPROFILE"))
@@ -309,6 +311,11 @@ async fn main() -> anyhow::Result<()> {
309311
cfg.storage.path = abs.to_string_lossy().into_owned();
310312
}
311313

314+
// If no CLI quiet flag, honor config default
315+
if !cli.quiet_net && cfg.net.quiet_by_default {
316+
network::set_quiet_logging(true);
317+
}
318+
312319
let db = match std::panic::catch_unwind(|| storage::open(&cfg.storage)) {
313320
Ok(db) => db,
314321
Err(_) => return Err(anyhow::anyhow!("failed to open database")),

src/miner.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,28 @@ impl Miner {
374374
let creator_address = self.wallet.address();
375375
let mem_kib = anchor.mem_kib;
376376
let difficulty = anchor.difficulty;
377+
// Safety: at retarget boundaries, only mine when our local retarget window is complete.
378+
// This avoids producing anchors with params that will be rejected once history arrives.
379+
if anchor.num > 0 && anchor.num % crate::consensus::RETARGET_INTERVAL == 0 {
380+
if let Ok(window_opt) = self.db.get_or_build_retarget_window(anchor.num) {
381+
let ready = window_opt.as_ref().map(|w| w.len() as u64 == crate::consensus::RETARGET_INTERVAL).unwrap_or(false);
382+
if !ready {
383+
println!(
384+
"⏳ Retarget guard: delaying mining for epoch #{} until historical window [{}..{}] is complete",
385+
anchor.num,
386+
anchor.num.saturating_sub(crate::consensus::RETARGET_INTERVAL),
387+
anchor.num.saturating_sub(1)
388+
);
389+
return Ok(());
390+
}
391+
} else {
392+
println!(
393+
"⏳ Retarget guard: delaying mining for epoch #{} (window fetch error)",
394+
anchor.num
395+
);
396+
return Ok(());
397+
}
398+
}
377399
let mut attempts = 0u64;
378400
let max_attempts = self.cfg.max_attempts;
379401

0 commit comments

Comments
 (0)