Skip to content

Commit 41048b1

Browse files
committed
loop forever in both post_block uses
1 parent 70b082a commit 41048b1

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

stacks-signer/src/client/stacks_client.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
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};
1719

1820
use blockstack_lib::chainstate::nakamoto::NakamotoBlock;
1921
use blockstack_lib::chainstate::stacks::boot::{NakamotoSignerEntry, SIGNERS_NAME};
@@ -564,6 +566,29 @@ impl StacksClient {
564566
Ok(account_entry)
565567
}
566568

569+
/// Post a block to the stacks-node, retry forever on errors.
570+
///
571+
/// In tests, this panics if the retry takes longer than 30 seconds.
572+
pub fn post_block_until_ok<F: Display>(&self, log_fmt: &F, block: &NakamotoBlock) -> bool {
573+
let start_time = Instant::now();
574+
loop {
575+
match self.post_block(block) {
576+
Ok(block_push_result) => {
577+
debug!("{log_fmt}: Block pushed to stacks node: {block_push_result:?}");
578+
return block_push_result;
579+
}
580+
Err(e) => {
581+
if cfg!(test) && start_time.elapsed() > Duration::from_secs(30) {
582+
panic!(
583+
"{log_fmt}: Timed out in test while pushing block to stacks node: {e}"
584+
);
585+
}
586+
warn!("{log_fmt}: Failed to push block to stacks node: {e}. Retrying...");
587+
}
588+
};
589+
}
590+
}
591+
567592
/// Try to post a completed nakamoto block to our connected stacks-node
568593
/// Returns `true` if the block was accepted or `false` if the block
569594
/// was rejected.

stacks-signer/src/v0/signer.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use std::collections::HashMap;
1616
use std::fmt::Debug;
1717
use std::sync::mpsc::Sender;
18-
use std::time::{Duration, Instant};
1918

2019
use blockstack_lib::chainstate::nakamoto::{NakamotoBlock, NakamotoBlockHeader};
2120
use blockstack_lib::net::api::postblock_proposal::{
@@ -192,23 +191,7 @@ impl SignerTrait<SignerMessage> for Signer {
192191
"block_id" => %b.block_id(),
193192
"signer_sighash" => %b.header.signer_signature_hash(),
194193
);
195-
let start_time = Instant::now();
196-
loop {
197-
match stacks_client.post_block(b) {
198-
Ok(block_push_result) => {
199-
debug!("{self}: Block pushed to stacks node: {block_push_result:?}");
200-
break;
201-
}
202-
Err(e) => {
203-
if cfg!(test)
204-
&& start_time.elapsed() > Duration::from_secs(30)
205-
{
206-
panic!("{self}: Timed out in test while pushing block to stacks node: {e}");
207-
}
208-
warn!("{self}: Failed to push block to stacks node: {e}. Retrying...");
209-
}
210-
};
211-
}
194+
stacks_client.post_block_until_ok(self, &b);
212195
}
213196
SignerMessage::MockProposal(mock_proposal) => {
214197
let epoch = match stacks_client.get_node_epoch() {
@@ -907,15 +890,7 @@ impl Signer {
907890
"{self}: Broadcasting Stacks block {} to node",
908891
&block.block_id()
909892
);
910-
if let Err(e) = stacks_client.post_block(&block) {
911-
warn!(
912-
"{self}: Failed to post block {block_hash}: {e:?}";
913-
"stacks_block_id" => %block.block_id(),
914-
"parent_block_id" => %block.header.parent_block_id,
915-
"burnchain_consensus_hash" => %block.header.consensus_hash
916-
);
917-
return;
918-
}
893+
stacks_client.post_block_until_ok(self, &block);
919894

920895
if let Err(e) = self.signer_db.set_block_broadcasted(
921896
self.reward_cycle,

0 commit comments

Comments
 (0)