Skip to content

Commit

Permalink
Merge pull request #5259 from stacks-network/chore/no-inf-loop-in-test
Browse files Browse the repository at this point in the history
Chore: remove infinite loop in signer during tests
  • Loading branch information
kantai authored Oct 2, 2024
2 parents 038f9e4 + 41048b1 commit 3bd42e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
25 changes: 25 additions & 0 deletions stacks-signer/src/client/stacks_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use std::collections::{HashMap, VecDeque};
use std::fmt::Display;
use std::time::{Duration, Instant};

use blockstack_lib::chainstate::nakamoto::NakamotoBlock;
use blockstack_lib::chainstate::stacks::boot::{NakamotoSignerEntry, SIGNERS_NAME};
Expand Down Expand Up @@ -564,6 +566,29 @@ impl StacksClient {
Ok(account_entry)
}

/// Post a block to the stacks-node, retry forever on errors.
///
/// In tests, this panics if the retry takes longer than 30 seconds.
pub fn post_block_until_ok<F: Display>(&self, log_fmt: &F, block: &NakamotoBlock) -> bool {
let start_time = Instant::now();
loop {
match self.post_block(block) {
Ok(block_push_result) => {
debug!("{log_fmt}: Block pushed to stacks node: {block_push_result:?}");
return block_push_result;
}
Err(e) => {
if cfg!(test) && start_time.elapsed() > Duration::from_secs(30) {
panic!(
"{log_fmt}: Timed out in test while pushing block to stacks node: {e}"
);
}
warn!("{log_fmt}: Failed to push block to stacks node: {e}. Retrying...");
}
};
}
}

/// Try to post a completed nakamoto block to our connected stacks-node
/// Returns `true` if the block was accepted or `false` if the block
/// was rejected.
Expand Down
30 changes: 2 additions & 28 deletions stacks-signer/src/v0/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,31 +185,13 @@ impl SignerTrait<SignerMessage> for Signer {
);
}
SignerMessage::BlockPushed(b) => {
let block_push_result = stacks_client.post_block(b);
if let Err(ref e) = &block_push_result {
warn!(
"{self}: Failed to post block {} (id {}): {e:?}",
&b.header.signer_signature_hash(),
&b.block_id()
);
};
// This will infinitely loop until the block is acknowledged by the node
info!(
"{self}: Got block pushed message";
"block_id" => %b.block_id(),
"signer_sighash" => %b.header.signer_signature_hash(),
);
loop {
match stacks_client.post_block(b) {
Ok(block_push_result) => {
debug!("{self}: Block pushed to stacks node: {block_push_result:?}");
break;
}
Err(e) => {
warn!("{self}: Failed to push block to stacks node: {e}. Retrying...");
}
};
}
stacks_client.post_block_until_ok(self, &b);
}
SignerMessage::MockProposal(mock_proposal) => {
let epoch = match stacks_client.get_node_epoch() {
Expand Down Expand Up @@ -908,15 +890,7 @@ impl Signer {
"{self}: Broadcasting Stacks block {} to node",
&block.block_id()
);
if let Err(e) = stacks_client.post_block(&block) {
warn!(
"{self}: Failed to post block {block_hash}: {e:?}";
"stacks_block_id" => %block.block_id(),
"parent_block_id" => %block.header.parent_block_id,
"burnchain_consensus_hash" => %block.header.consensus_hash
);
return;
}
stacks_client.post_block_until_ok(self, &block);

if let Err(e) = self.signer_db.set_block_broadcasted(
self.reward_cycle,
Expand Down

0 comments on commit 3bd42e0

Please sign in to comment.