Skip to content

Commit 38594f9

Browse files
committed
Signers should not infinitely loop when posting a block to the node
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 9dd349d commit 38594f9

File tree

2 files changed

+20
-45
lines changed

2 files changed

+20
-45
lines changed

stacks-signer/src/client/stacks_client.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
use std::collections::{HashMap, VecDeque};
17-
use std::fmt::Display;
18-
use std::time::{Duration, Instant};
1917

2018
use blockstack_lib::chainstate::nakamoto::NakamotoBlock;
2119
use blockstack_lib::chainstate::stacks::boot::{NakamotoSignerEntry, SIGNERS_NAME};
@@ -602,36 +600,6 @@ impl StacksClient {
602600
Ok(account_entry)
603601
}
604602

605-
/// Post a block to the stacks-node, retry forever on errors.
606-
///
607-
/// In tests, this panics if the retry takes longer than 30 seconds.
608-
pub fn post_block_until_ok<F: Display>(&self, log_fmt: &F, block: &NakamotoBlock) -> bool {
609-
debug!("StacksClient: Posting block to stacks node";
610-
"signer_signature_hash" => %block.header.signer_signature_hash(),
611-
"block_id" => %block.header.block_id(),
612-
"block_height" => %block.header.chain_length,
613-
);
614-
let start_time = Instant::now();
615-
loop {
616-
match self.post_block(block) {
617-
Ok(block_push_result) => {
618-
debug!("{log_fmt}: Block pushed to stacks node: {block_push_result:?}");
619-
return block_push_result;
620-
}
621-
Err(e) => {
622-
if cfg!(any(test, feature = "testing"))
623-
&& start_time.elapsed() > Duration::from_secs(30)
624-
{
625-
panic!(
626-
"{log_fmt}: Timed out in test while pushing block to stacks node: {e}"
627-
);
628-
}
629-
warn!("{log_fmt}: Failed to push block to stacks node: {e}. Retrying...");
630-
}
631-
};
632-
}
633-
}
634-
635603
/// Try to post a completed nakamoto block to our connected stacks-node
636604
/// Returns `true` if the block was accepted or `false` if the block
637605
/// was rejected.

stacks-signer/src/v0/signer.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ impl Signer {
455455
if self.test_skip_block_broadcast(b) {
456456
return;
457457
}
458-
stacks_client.post_block_until_ok(self, b);
458+
self.handle_post_block(stacks_client, b);
459459
}
460460
SignerMessage::MockProposal(mock_proposal) => {
461461
let epoch = match stacks_client.get_node_epoch() {
@@ -1528,12 +1528,11 @@ impl Signer {
15281528
}
15291529

15301530
fn broadcast_signed_block(
1531-
&self,
1531+
&mut self,
15321532
stacks_client: &StacksClient,
15331533
mut block: NakamotoBlock,
15341534
addrs_to_sigs: &HashMap<StacksAddress, MessageSignature>,
15351535
) {
1536-
let block_hash = block.header.signer_signature_hash();
15371536
// collect signatures for the block
15381537
let signatures: Vec<_> = self
15391538
.signer_addresses
@@ -1548,17 +1547,25 @@ impl Signer {
15481547
if self.test_skip_block_broadcast(&block) {
15491548
return;
15501549
}
1551-
debug!(
1552-
"{self}: Broadcasting Stacks block {} to node",
1553-
&block.block_id()
1554-
);
1555-
stacks_client.post_block_until_ok(self, &block);
1550+
self.handle_post_block(stacks_client, &block);
1551+
}
15561552

1557-
if let Err(e) = self
1558-
.signer_db
1559-
.set_block_broadcasted(&block_hash, get_epoch_time_secs())
1560-
{
1561-
warn!("{self}: Failed to set block broadcasted for {block_hash}: {e:?}");
1553+
/// Attempt to post a block to the stacks-node and handle the result
1554+
pub fn handle_post_block(&mut self, stacks_client: &StacksClient, block: &NakamotoBlock) {
1555+
let block_hash = block.header.signer_signature_hash();
1556+
match stacks_client.post_block(block) {
1557+
Ok(accepted) => {
1558+
debug!("{self}: Block {block_hash} accepted by stacks node: {accepted}");
1559+
if let Err(e) = self
1560+
.signer_db
1561+
.set_block_broadcasted(&block_hash, get_epoch_time_secs())
1562+
{
1563+
warn!("{self}: Failed to set block broadcasted for {block_hash}: {e:?}");
1564+
}
1565+
}
1566+
Err(e) => {
1567+
warn!("{self}: Failed to broadcast block {block_hash} to the node: {e}")
1568+
}
15621569
}
15631570
}
15641571

0 commit comments

Comments
 (0)