Skip to content

Commit 625f132

Browse files
authored
Merge branch 'develop' into feat/audit-fixes
2 parents 85e41f7 + 70bef50 commit 625f132

File tree

19 files changed

+236
-518
lines changed

19 files changed

+236
-518
lines changed

stacks-signer/src/client/stacks_client.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,10 @@ impl StacksClient {
590590
.map_err(|e| backoff::Error::transient(e.into()))?;
591591
let status = response.status();
592592
if status.is_success() {
593-
return response
594-
.json()
595-
.map_err(|e| backoff::Error::permanent(e.into()));
593+
return response.json().map_err(|e| {
594+
warn!("Failed to parse the GetStackers response: {e}");
595+
backoff::Error::permanent(e.into())
596+
});
596597
}
597598
let error_data = response.json::<GetStackersErrorResp>().map_err(|e| {
598599
warn!("Failed to parse the GetStackers error response: {e}");

stackslib/src/net/download/nakamoto/download_state_machine.rs

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ pub struct NakamotoDownloadStateMachine {
115115
unconfirmed_tenure_downloads: HashMap<NeighborAddress, NakamotoUnconfirmedTenureDownloader>,
116116
/// Ongoing confirmed tenure downloads for when we know the start and end block hashes.
117117
tenure_downloads: NakamotoTenureDownloaderSet,
118-
/// resolved tenure-start blocks
119-
tenure_start_blocks: HashMap<StacksBlockId, NakamotoBlock>,
120118
/// comms to remote neighbors
121119
pub(super) neighbor_rpc: NeighborRPC,
122120
/// Nakamoto chain tip
@@ -140,7 +138,6 @@ impl NakamotoDownloadStateMachine {
140138
unconfirmed_tenure_download_schedule: VecDeque::new(),
141139
tenure_downloads: NakamotoTenureDownloaderSet::new(),
142140
unconfirmed_tenure_downloads: HashMap::new(),
143-
tenure_start_blocks: HashMap::new(),
144141
neighbor_rpc: NeighborRPC::new(),
145142
nakamoto_tip,
146143
last_unconfirmed_download_run_ms: 0,
@@ -367,48 +364,6 @@ impl NakamotoDownloadStateMachine {
367364
)
368365
}
369366

370-
/// Find all stored (but not necessarily processed) tenure-start blocks for a list
371-
/// of wanted tenures that this node has locally. NOTE: these tenure-start blocks
372-
/// do not correspond to the tenure; they correspond to the _parent_ tenure (since a
373-
/// `WantedTenure` captures the tenure-start block hash of the parent tenure; the same data
374-
/// captured by a sortition).
375-
///
376-
/// This method is static to ease testing.
377-
///
378-
/// Returns Ok(()) on success and fills in newly-discovered blocks into `tenure_start_blocks`.
379-
/// Returns Err(..) on DB error.
380-
pub(crate) fn load_tenure_start_blocks(
381-
wanted_tenures: &[WantedTenure],
382-
chainstate: &mut StacksChainState,
383-
tenure_start_blocks: &mut HashMap<StacksBlockId, NakamotoBlock>,
384-
) -> Result<(), NetError> {
385-
for wt in wanted_tenures {
386-
let candidate_tenure_start_blocks = chainstate
387-
.nakamoto_blocks_db()
388-
.get_nakamoto_tenure_start_blocks(&wt.tenure_id_consensus_hash)?;
389-
390-
for candidate_tenure_start_block in candidate_tenure_start_blocks.into_iter() {
391-
tenure_start_blocks.insert(
392-
candidate_tenure_start_block.block_id(),
393-
candidate_tenure_start_block,
394-
);
395-
}
396-
}
397-
Ok(())
398-
}
399-
400-
/// Update our local tenure start block data
401-
fn update_tenure_start_blocks(
402-
&mut self,
403-
chainstate: &mut StacksChainState,
404-
) -> Result<(), NetError> {
405-
Self::load_tenure_start_blocks(
406-
&self.wanted_tenures,
407-
chainstate,
408-
&mut self.tenure_start_blocks,
409-
)
410-
}
411-
412367
/// Update `self.wanted_tenures` with newly-discovered sortition data.
413368
fn extend_wanted_tenures(
414369
&mut self,
@@ -670,7 +625,6 @@ impl NakamotoDownloadStateMachine {
670625
&mut self,
671626
network: &PeerNetwork,
672627
sortdb: &SortitionDB,
673-
chainstate: &mut StacksChainState,
674628
) -> Result<(), NetError> {
675629
let sort_tip = &network.burnchain_tip;
676630

@@ -688,7 +642,6 @@ impl NakamotoDownloadStateMachine {
688642
// not at a reward cycle boundary, so just extend self.wanted_tenures
689643
debug!("Extend wanted tenures since no sort_rc change and we have tenure data");
690644
self.extend_wanted_tenures(network, sortdb)?;
691-
self.update_tenure_start_blocks(chainstate)?;
692645
return Ok(());
693646
}
694647

@@ -728,7 +681,6 @@ impl NakamotoDownloadStateMachine {
728681
self.wanted_tenures = new_wanted_tenures;
729682
self.reward_cycle = sort_rc;
730683

731-
self.update_tenure_start_blocks(chainstate)?;
732684
Ok(())
733685
}
734686

@@ -1485,21 +1437,6 @@ impl NakamotoDownloadStateMachine {
14851437
// run all downloaders
14861438
let new_blocks = self.tenure_downloads.run(network, &mut self.neighbor_rpc);
14871439

1488-
// give blocked downloaders their tenure-end blocks from other downloaders that have
1489-
// obtained their tenure-start blocks
1490-
let new_tenure_starts = self.tenure_downloads.find_new_tenure_start_blocks();
1491-
self.tenure_start_blocks
1492-
.extend(new_tenure_starts.into_iter());
1493-
1494-
let dead = self
1495-
.tenure_downloads
1496-
.handle_tenure_end_blocks(&self.tenure_start_blocks);
1497-
1498-
// bookkeeping
1499-
for naddr in dead.into_iter() {
1500-
self.neighbor_rpc.add_dead(network, &naddr);
1501-
}
1502-
15031440
new_blocks
15041441
}
15051442

@@ -1729,7 +1666,7 @@ impl NakamotoDownloadStateMachine {
17291666
) -> Result<HashMap<ConsensusHash, Vec<NakamotoBlock>>, NetError> {
17301667
self.nakamoto_tip = network.stacks_tip.block_id();
17311668
debug!("Downloader: Nakamoto tip is {:?}", &self.nakamoto_tip);
1732-
self.update_wanted_tenures(&network, sortdb, chainstate)?;
1669+
self.update_wanted_tenures(&network, sortdb)?;
17331670
self.update_processed_tenures(chainstate)?;
17341671
let new_blocks = self.run_downloads(burnchain_height, network, sortdb, chainstate, ibd);
17351672
self.last_sort_tip = Some(network.burnchain_tip.clone());

stackslib/src/net/download/nakamoto/tenure_downloader.rs

Lines changed: 6 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,7 @@ use crate::util_lib::db::{DBConn, Error as DBError};
6868
pub enum NakamotoTenureDownloadState {
6969
/// Getting the tenure-start block (the given StacksBlockId is it's block ID).
7070
GetTenureStartBlock(StacksBlockId),
71-
/// Waiting for the child tenure's tenure-start block to arrive, which is usually (but not
72-
/// always) handled by the execution of another NakamotoTenureDownloader. The only
73-
/// exceptions are as follows:
74-
///
75-
/// * if this tenure contains the anchor block, and it's the last tenure in the
76-
/// reward cycle. In this case, the end-block must be directly fetched, since there will be no
77-
/// follow-on NakamotTenureDownloader in the same reward cycle who can provide this.
78-
///
79-
/// * if this tenure is the highest complete tenure, and we just learned the start-block of the
80-
/// ongoing tenure, then a NakamotoTenureDownloader will be instantiated with this tenure-end-block
81-
/// already known. This step will be skipped because the end-block is already present in the
82-
/// state machine.
83-
///
84-
/// * if the deadline (second parameter) is exceeded, the state machine transitions to
85-
/// GetTenureEndBlock.
86-
///
87-
/// The two fields here are:
88-
/// * the block ID of the last block in the tenure (which happens to be the block ID of the
89-
/// start block of the next tenure)
90-
/// * the deadline by which this state machine needs to have obtained the tenure end-block
91-
/// before transitioning to `GetTenureEndBlock`.
92-
WaitForTenureEndBlock(StacksBlockId, Instant),
93-
/// Getting the tenure-end block directly. This only happens for tenures whose end-blocks
94-
/// cannot be provided by tenure downloaders within the same reward cycle, and for tenures in
95-
/// which we cannot quickly get the tenure-end block.
71+
/// Getting the tenure-end block.
9672
///
9773
/// The field here is the block ID of the tenure end block.
9874
GetTenureEndBlock(StacksBlockId),
@@ -163,8 +139,7 @@ pub struct NakamotoTenureDownloader {
163139
pub tenure_start_block: Option<NakamotoBlock>,
164140
/// Pre-stored tenure end block.
165141
/// An instance of this state machine will be used to fetch the highest-confirmed tenure, once
166-
/// the start-block for the current tenure is downloaded. This is that start-block, which is
167-
/// used to transition from the `WaitForTenureEndBlock` step to the `GetTenureBlocks` step.
142+
/// the start-block for the current tenure is downloaded.
168143
pub tenure_end_block: Option<NakamotoBlock>,
169144
/// Tenure blocks
170145
pub tenure_blocks: Option<Vec<NakamotoBlock>>,
@@ -205,16 +180,6 @@ impl NakamotoTenureDownloader {
205180
self
206181
}
207182

208-
/// Is this downloader waiting for the tenure-end block data from some other downloader? Per
209-
/// the struct documentation, this is case 2(a).
210-
pub fn is_waiting(&self) -> bool {
211-
if let NakamotoTenureDownloadState::WaitForTenureEndBlock(..) = self.state {
212-
return true;
213-
} else {
214-
return false;
215-
}
216-
}
217-
218183
/// Validate and accept a given tenure-start block. If accepted, then advance the state.
219184
/// Returns Ok(()) if the start-block is valid.
220185
/// Returns Err(..) if it is not valid.
@@ -266,66 +231,15 @@ impl NakamotoTenureDownloader {
266231
tenure_end_block.block_id(),
267232
&self.tenure_id_consensus_hash
268233
);
269-
self.state = NakamotoTenureDownloadState::WaitForTenureEndBlock(
270-
tenure_end_block.block_id(),
271-
Instant::now()
272-
.checked_add(Duration::new(WAIT_FOR_TENURE_END_BLOCK_TIMEOUT, 0))
273-
.ok_or(NetError::OverflowError("Deadline is too big".into()))?,
274-
);
275234
self.try_accept_tenure_end_block(&tenure_end_block)?;
276235
} else {
277-
// need to get tenure_end_block. By default, assume that another
278-
// NakamotoTenureDownloader will provide this block, and allow the
279-
// NakamotoTenureDownloaderSet instance that manages a collection of these
280-
// state-machines make the call to require this one to fetch the block directly.
281-
self.state = NakamotoTenureDownloadState::WaitForTenureEndBlock(
282-
self.tenure_end_block_id.clone(),
283-
Instant::now()
284-
.checked_add(Duration::new(WAIT_FOR_TENURE_END_BLOCK_TIMEOUT, 0))
285-
.ok_or(NetError::OverflowError("Deadline is too big".into()))?,
286-
);
236+
// need to get tenure_end_block.
237+
self.state =
238+
NakamotoTenureDownloadState::GetTenureEndBlock(self.tenure_end_block_id.clone());
287239
}
288240
Ok(())
289241
}
290242

291-
/// Transition this state-machine from waiting for its tenure-end block from another
292-
/// state-machine to directly fetching it. This only needs to happen if the tenure this state
293-
/// machine is downloading contains the PoX anchor block, and it's also the last confirmed
294-
/// tenurein this reward cycle.
295-
///
296-
/// This function is called by `NakamotoTenureDownloadSet`, which instantiates, schedules, and
297-
/// runs a set of these machines based on the peers' inventory vectors. But because we don't
298-
/// know if this is the PoX anchor block tenure (or even the last tenure) until we have
299-
/// inventory vectors for this tenure's reward cycle, this state-transition must be driven
300-
/// after this machine's instantiation.
301-
pub fn transition_to_fetch_end_block(&mut self) -> Result<(), NetError> {
302-
let NakamotoTenureDownloadState::WaitForTenureEndBlock(end_block_id, ..) = self.state
303-
else {
304-
return Err(NetError::InvalidState);
305-
};
306-
debug!(
307-
"Transition downloader to {} to directly fetch tenure-end block {} (direct transition)",
308-
&self.naddr, &end_block_id
309-
);
310-
self.state = NakamotoTenureDownloadState::GetTenureEndBlock(end_block_id);
311-
Ok(())
312-
}
313-
314-
/// Transition to fetching the tenure-end block directly if waiting has taken too long.
315-
pub fn transition_to_fetch_end_block_on_timeout(&mut self) {
316-
if let NakamotoTenureDownloadState::WaitForTenureEndBlock(end_block_id, wait_deadline) =
317-
self.state
318-
{
319-
if wait_deadline < Instant::now() {
320-
debug!(
321-
"Transition downloader to {} to directly fetch tenure-end block {} (timed out)",
322-
&self.naddr, &end_block_id
323-
);
324-
self.state = NakamotoTenureDownloadState::GetTenureEndBlock(end_block_id);
325-
}
326-
}
327-
}
328-
329243
/// Validate and accept a tenure-end block. If accepted, then advance the state.
330244
/// Once accepted, this function extracts the tenure-change transaction and block header from
331245
/// this block (it does not need the entire block).
@@ -338,8 +252,7 @@ impl NakamotoTenureDownloader {
338252
) -> Result<(), NetError> {
339253
if !matches!(
340254
&self.state,
341-
NakamotoTenureDownloadState::WaitForTenureEndBlock(..)
342-
| NakamotoTenureDownloadState::GetTenureEndBlock(_)
255+
NakamotoTenureDownloadState::GetTenureEndBlock(_)
343256
) {
344257
warn!("Invalid state for this method";
345258
"state" => %self.state);
@@ -577,14 +490,6 @@ impl NakamotoTenureDownloader {
577490
debug!("Request tenure-start block {}", &start_block_id);
578491
StacksHttpRequest::new_get_nakamoto_block(peerhost, start_block_id.clone())
579492
}
580-
NakamotoTenureDownloadState::WaitForTenureEndBlock(_block_id, _deadline) => {
581-
// we're waiting for some other downloader's block-fetch to complete
582-
debug!(
583-
"Waiting for tenure-end block {} until {:?}",
584-
&_block_id, _deadline
585-
);
586-
return Ok(None);
587-
}
588493
NakamotoTenureDownloadState::GetTenureEndBlock(end_block_id) => {
589494
debug!("Request tenure-end block {}", &end_block_id);
590495
StacksHttpRequest::new_get_nakamoto_block(peerhost, end_block_id.clone())
@@ -665,10 +570,6 @@ impl NakamotoTenureDownloader {
665570
self.try_accept_tenure_start_block(block)?;
666571
Ok(None)
667572
}
668-
NakamotoTenureDownloadState::WaitForTenureEndBlock(..) => {
669-
debug!("Invalid state -- Got download response for WaitForTenureBlock");
670-
Err(NetError::InvalidState)
671-
}
672573
NakamotoTenureDownloadState::GetTenureEndBlock(_block_id) => {
673574
debug!("Got download response to tenure-end block {}", &_block_id);
674575
let block = response.decode_nakamoto_block().map_err(|e| {

0 commit comments

Comments
 (0)