Skip to content

Commit e9d68fd

Browse files
committed
grin v5.3 (0061) Enable block archival sync (mimblewimble#3579)
* wip - body sync for full archive * allow chain compaction during sync * placeholder for logic to ensure archive nodes sync from archive nodes * body sync from archival peers * allow chain compaction during sync * placeholder for logic to ensure archive nodes sync from archive nodes
1 parent b6c9b8c commit e9d68fd

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

chain/src/chain.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ impl Chain {
257257
Ok(chain)
258258
}
259259

260+
/// Are we running with archive_mode enabled?
261+
pub fn archive_mode(&self) -> bool {
262+
self.archive_mode
263+
}
264+
260265
/// Return our shared header MMR handle.
261266
pub fn header_pmmr(&self) -> Arc<RwLock<PMMRHandle<BlockHeader>>> {
262267
self.header_pmmr.clone()
@@ -1105,6 +1110,11 @@ impl Chain {
11051110
/// If beyond the horizon then we cannot sync via recent full blocks
11061111
/// and we need a state (txhashset) sync.
11071112
pub fn check_txhashset_needed(&self, fork_point: &BlockHeader) -> Result<bool, Error> {
1113+
if self.archive_mode() {
1114+
debug!("check_txhashset_needed: we are running with archive_mode=true, not needed");
1115+
return Ok(false);
1116+
}
1117+
11081118
let header_head = self.header_head()?;
11091119
let horizon = global::cut_through_horizon() as u64;
11101120
Ok(fork_point.height < header_head.height.saturating_sub(horizon))
@@ -1291,7 +1301,7 @@ impl Chain {
12911301
header_pmmr: &txhashset::PMMRHandle<BlockHeader>,
12921302
batch: &store::Batch<'_>,
12931303
) -> Result<(), Error> {
1294-
if self.archive_mode {
1304+
if self.archive_mode() {
12951305
return Ok(());
12961306
}
12971307

@@ -1377,13 +1387,14 @@ impl Chain {
13771387
}
13781388

13791389
// If we are not in archival mode remove historical blocks from the db.
1380-
if !self.archive_mode {
1390+
if !self.archive_mode() {
13811391
self.remove_historical_blocks(&header_pmmr, &batch)?;
13821392
}
13831393

13841394
// Make sure our output_pos index is consistent with the UTXO set.
13851395
txhashset.init_output_pos_index(&header_pmmr, &batch)?;
13861396

1397+
// TODO - Why is this part of chain compaction?
13871398
// Rebuild our NRD kernel_pos index based on recent kernel history.
13881399
txhashset.init_recent_kernel_pos_index(&header_pmmr, &batch)?;
13891400

servers/src/common/adapters.rs

-5
Original file line numberDiff line numberDiff line change
@@ -958,11 +958,6 @@ where
958958
}
959959

960960
fn check_compact(&self) {
961-
// Skip compaction if we are syncing.
962-
if self.sync_state.is_syncing() {
963-
return;
964-
}
965-
966961
// Roll the dice to trigger compaction at 1/COMPACTION_CHECK chance per block,
967962
// uses a different thread to avoid blocking the caller thread (likely a peer)
968963
let mut rng = thread_rng();

servers/src/grin/sync/body_sync.rs

+37-18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use chrono::prelude::{DateTime, Utc};
1616
use chrono::Duration;
17+
use p2p::Capabilities;
1718
use rand::prelude::*;
1819
use std::cmp;
1920
use std::sync::Arc;
@@ -71,6 +72,11 @@ impl BodySync {
7172
Ok(false)
7273
}
7374

75+
/// Is our local node running in archive_mode?
76+
fn archive_mode(&self) -> bool {
77+
self.chain.archive_mode()
78+
}
79+
7480
/// Return true if txhashset download is needed (when requested block is under the horizon).
7581
/// Otherwise go request some missing blocks and return false.
7682
fn body_sync(&mut self) -> Result<bool, chain::Error> {
@@ -85,26 +91,39 @@ impl BodySync {
8591
return Ok(true);
8692
}
8793

88-
// Find connected peers with strictly greater difficulty than us.
89-
let peers_iter = || {
90-
self.peers
91-
.iter()
92-
.with_difficulty(|x| x > head.total_difficulty)
93-
.connected()
94-
};
94+
let peers = {
95+
// Find connected peers with strictly greater difficulty than us.
96+
let peers_iter = || {
97+
// If we are running with archive mode enabled we only want to sync
98+
// from other archive nodes.
99+
let cap = if self.archive_mode() {
100+
Capabilities::BLOCK_HIST
101+
} else {
102+
Capabilities::UNKNOWN
103+
};
104+
105+
self.peers
106+
.iter()
107+
.with_capabilities(cap)
108+
.with_difficulty(|x| x > head.total_difficulty)
109+
.connected()
110+
};
111+
112+
// We prefer outbound peers with greater difficulty.
113+
let mut peers: Vec<_> = peers_iter().outbound().into_iter().collect();
114+
if peers.is_empty() {
115+
debug!("no outbound peers with more work, considering inbound");
116+
peers = peers_iter().inbound().into_iter().collect();
117+
}
95118

96-
// We prefer outbound peers with greater difficulty.
97-
let mut peers: Vec<_> = peers_iter().outbound().into_iter().collect();
98-
if peers.is_empty() {
99-
debug!("no outbound peers with more work, considering inbound");
100-
peers = peers_iter().inbound().into_iter().collect();
101-
}
119+
// If we have no peers (outbound or inbound) then we are done for now.
120+
if peers.is_empty() {
121+
debug!("no peers (inbound or outbound) with more work");
122+
return Ok(false);
123+
}
102124

103-
// If we have no peers (outbound or inbound) then we are done for now.
104-
if peers.is_empty() {
105-
debug!("no peers (inbound or outbound) with more work");
106-
return Ok(false);
107-
}
125+
peers
126+
};
108127

109128
// if we have 5 peers to sync from then ask for 50 blocks total (peer_count *
110129
// 10) max will be 80 if all 8 peers are advertising more work

0 commit comments

Comments
 (0)