Skip to content

Commit 382cb80

Browse files
authored
Merge pull request #3552 from stacks-network/feat/stackerdb-discovery
Feat/stackerdb discovery
2 parents 4e411e1 + 6966f0c commit 382cb80

File tree

21 files changed

+8095
-6095
lines changed

21 files changed

+8095
-6095
lines changed

stackslib/src/burnchains/bitcoin/indexer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ impl BitcoinIndexer {
245245
true,
246246
false,
247247
)
248-
.unwrap();
248+
.expect(&format!(
249+
"Failed to open {:?}",
250+
&working_dir_path.to_str().unwrap().to_string()
251+
));
249252

250253
BitcoinIndexer {
251254
config: BitcoinIndexerConfig::default_regtest(

stackslib/src/chainstate/coordinator/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,12 @@ impl<'a, T: BlockEventDispatcher, U: RewardSetProvider, B: BurnchainHeaderReader
503503
SortitionDB::get_canonical_sortition_tip(sortition_db.conn()).unwrap();
504504

505505
let atlas_config = atlas_config.unwrap_or(AtlasConfig::new(false));
506-
let atlas_db =
507-
AtlasDB::connect(atlas_config.clone(), &format!("{}/atlas", path), true).unwrap();
506+
let atlas_db = AtlasDB::connect(
507+
atlas_config.clone(),
508+
&format!("{}/atlas.sqlite", path),
509+
true,
510+
)
511+
.unwrap();
508512

509513
ChainsCoordinator {
510514
canonical_sortition_tip: Some(canonical_sortition_tip),

stackslib/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ check if the associated microblocks can be downloaded
580580
None,
581581
0,
582582
UrlString::try_from("abc").unwrap(),
583+
vec![],
583584
);
584585

585586
let header_hashes = {

stackslib/src/net/chat.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,19 @@ impl Neighbor {
467467
Ok(())
468468
}
469469

470-
pub fn from_handshake(
470+
/// Instantiate a Neighbor from HandshakeData, merging the information we have on-disk in the
471+
/// PeerDB with information in the handshake.
472+
/// * If we already know about this neighbor, then all previously-calculated state and local
473+
/// configuration state will be loaded as well. This includes things like the calculated
474+
/// in/out-degree and last-contact time, as well as the allow/deny time limits.
475+
/// * If we do not know about this neighbor, then the above state will not be loaded.
476+
/// Returns (the neighbor, whether or not the neighbor was known)
477+
pub fn load_and_update(
471478
conn: &DBConn,
472479
peer_version: u32,
473480
network_id: u32,
474481
handshake_data: &HandshakeData,
475-
) -> Result<Neighbor, net_error> {
482+
) -> Result<(Neighbor, bool), net_error> {
476483
let addr = NeighborKey::from_handshake(peer_version, network_id, handshake_data);
477484
let pubk = handshake_data
478485
.node_public_key
@@ -482,15 +489,15 @@ impl Neighbor {
482489
let peer_opt = PeerDB::get_peer(conn, network_id, &addr.addrbytes, addr.port)
483490
.map_err(net_error::DBError)?;
484491

485-
let mut neighbor = match peer_opt {
492+
let (mut neighbor, present) = match peer_opt {
486493
Some(neighbor) => {
487494
let mut ret = neighbor;
488495
ret.addr = addr.clone();
489-
ret
496+
(ret, true)
490497
}
491498
None => {
492499
let ret = Neighbor::empty(&addr, &pubk, handshake_data.expire_block_height);
493-
ret
500+
(ret, false)
494501
}
495502
};
496503

@@ -510,7 +517,7 @@ impl Neighbor {
510517
}
511518

512519
neighbor.handshake_update(conn, &handshake_data)?;
513-
Ok(neighbor)
520+
Ok((neighbor, present))
514521
}
515522

516523
pub fn from_conversation(
@@ -1250,13 +1257,13 @@ impl ConversationP2P {
12501257
if updated {
12511258
// save the new key
12521259
let mut tx = peerdb.tx_begin().map_err(net_error::DBError)?;
1253-
let mut neighbor = Neighbor::from_handshake(
1260+
let (mut neighbor, _) = Neighbor::load_and_update(
12541261
&mut tx,
12551262
message.preamble.peer_version,
12561263
message.preamble.network_id,
12571264
&handshake_data,
12581265
)?;
1259-
neighbor.save_update(&mut tx)?;
1266+
neighbor.save_update(&mut tx, None)?;
12601267
tx.commit()
12611268
.map_err(|e| net_error::DBError(db_error::SqliteError(e)))?;
12621269

@@ -1277,10 +1284,7 @@ impl ConversationP2P {
12771284
accept_data,
12781285
StackerDBHandshakeData {
12791286
rc_consensus_hash: chain_view.rc_consensus_hash.clone(),
1280-
// placeholder sbtc address for now
1281-
smart_contracts: vec![
1282-
ContractId::parse("SP000000000000000000002Q6VF78.sbtc").unwrap()
1283-
],
1287+
smart_contracts: local_peer.stacker_dbs.clone(),
12841288
},
12851289
)
12861290
} else {
@@ -1401,11 +1405,12 @@ impl ConversationP2P {
14011405
let epoch = self.get_current_epoch(chain_view.burn_block_height);
14021406

14031407
// get neighbors at random as long as they're fresh, and as long as they're compatible with
1404-
// the current system epoch
1405-
let mut neighbors = PeerDB::get_random_neighbors(
1408+
// the current system epoch.
1409+
let mut neighbors = PeerDB::get_fresh_random_neighbors(
14061410
peer_dbconn,
14071411
self.network_id,
14081412
epoch.network_epoch,
1413+
(get_epoch_time_secs() as u64).saturating_sub(self.connection.options.max_neighbor_age),
14091414
MAX_NEIGHBORS_DATA_LEN,
14101415
chain_view.burn_block_height,
14111416
false,
@@ -2664,6 +2669,7 @@ mod test {
26642669
data_url.clone(),
26652670
&asn4_entries,
26662671
Some(&initial_neighbors),
2672+
&vec![ContractId::parse("SP000000000000000000002Q6VF78.sbtc").unwrap()],
26672673
)
26682674
.unwrap();
26692675
let sortdb = SortitionDB::connect(
@@ -3188,15 +3194,16 @@ mod test {
31883194
assert_eq!(data.handshake.data_url, "http://peer2.com".into());
31893195
assert_eq!(data.heartbeat_interval, conn_opts.heartbeat);
31903196

3191-
// remote peer always replies with its supported smart contracts
3192-
assert_eq!(
3193-
db_data.smart_contracts,
3194-
vec![ContractId::parse("SP000000000000000000002Q6VF78.sbtc").unwrap()]
3195-
);
3196-
31973197
if peer_1_rc_consensus_hash == peer_2_rc_consensus_hash {
31983198
assert_eq!(db_data.rc_consensus_hash, chain_view_1.rc_consensus_hash);
31993199

3200+
// remote peer always replies with its supported smart contracts
3201+
assert_eq!(
3202+
db_data.smart_contracts,
3203+
vec![ContractId::parse("SP000000000000000000002Q6VF78.sbtc")
3204+
.unwrap()]
3205+
);
3206+
32003207
// peers learn each others' smart contract DBs
32013208
eprintln!(
32023209
"{:?}, {:?}",
@@ -5519,6 +5526,7 @@ mod test {
55195526
None,
55205527
get_epoch_time_secs() + 123456,
55215528
UrlString::try_from("http://foo.com").unwrap(),
5529+
vec![],
55225530
);
55235531
let mut convo = ConversationP2P::new(
55245532
123,

stackslib/src/net/connection.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ use crate::net::StacksP2P;
5252
use crate::net::download::BLOCK_DOWNLOAD_INTERVAL;
5353
use crate::net::inv::{INV_REWARD_CYCLES, INV_SYNC_INTERVAL};
5454
use crate::net::neighbors::{
55-
NEIGHBOR_REQUEST_TIMEOUT, NEIGHBOR_WALK_INTERVAL, NUM_INITIAL_WALKS, WALK_MAX_DURATION,
56-
WALK_MIN_DURATION, WALK_RESET_INTERVAL, WALK_RESET_PROB, WALK_RETRY_COUNT, WALK_STATE_TIMEOUT,
55+
MAX_NEIGHBOR_AGE, NEIGHBOR_REQUEST_TIMEOUT, NEIGHBOR_WALK_INTERVAL, NUM_INITIAL_WALKS,
56+
WALK_MAX_DURATION, WALK_MIN_DURATION, WALK_RESET_INTERVAL, WALK_RESET_PROB, WALK_RETRY_COUNT,
57+
WALK_STATE_TIMEOUT,
5758
};
5859

5960
use clarity::vm::{costs::ExecutionCost, types::BOUND_VALUE_SERIALIZATION_HEX};
@@ -339,6 +340,7 @@ pub struct ConnectionOptions {
339340
pub max_neighbors_of_neighbor: u64,
340341
pub max_http_clients: u64,
341342
pub neighbor_request_timeout: u64,
343+
pub max_neighbor_age: u64,
342344
pub num_initial_walks: u64,
343345
pub walk_retry_count: u64,
344346
pub walk_interval: u64,
@@ -423,6 +425,7 @@ impl std::default::Default for ConnectionOptions {
423425
max_neighbors_of_neighbor: 10,
424426
max_http_clients: 10,
425427
neighbor_request_timeout: NEIGHBOR_REQUEST_TIMEOUT, // how long to wait for a neighbor request
428+
max_neighbor_age: MAX_NEIGHBOR_AGE,
426429
num_initial_walks: NUM_INITIAL_WALKS,
427430
walk_retry_count: WALK_RETRY_COUNT,
428431
walk_interval: NEIGHBOR_WALK_INTERVAL, // how often to do a neighbor walk.

0 commit comments

Comments
 (0)