Skip to content

Commit 4effafb

Browse files
committed
refactor: make scope works like replay set, #5971
1 parent b493d07 commit 4effafb

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

stacks-signer/src/v0/signer_state.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl LocalStateMachine {
558558
};
559559
return Err(ClientError::InvalidResponse(err_msg).into());
560560
}
561-
if let Some(new_replay_set) = self.handle_possible_bitcoin_fork(
561+
if let Some((new_replay_set, new_replay_scope)) = self.handle_possible_bitcoin_fork(
562562
db,
563563
client,
564564
&expected_burn_block,
@@ -567,6 +567,7 @@ impl LocalStateMachine {
567567
tx_replay_scope,
568568
)? {
569569
tx_replay_set = ReplayTransactionSet::new(new_replay_set);
570+
*tx_replay_scope = new_replay_scope;
570571
}
571572
}
572573

@@ -912,8 +913,11 @@ impl LocalStateMachine {
912913
expected_burn_block: &NewBurnBlock,
913914
prior_state_machine: &SignerStateMachine,
914915
is_in_tx_replay_mode: bool,
915-
tx_replay_scope: &mut Option<(NewBurnBlock, NewBurnBlock)>,
916-
) -> Result<Option<Vec<StacksTransaction>>, SignerChainstateError> {
916+
tx_replay_scope: &Option<(NewBurnBlock, NewBurnBlock)>,
917+
) -> Result<
918+
Option<(Vec<StacksTransaction>, Option<(NewBurnBlock, NewBurnBlock)>)>,
919+
SignerChainstateError,
920+
> {
917921
if expected_burn_block.burn_block_height > prior_state_machine.burn_block_height {
918922
// no bitcoin fork, because we're advancing the burn block height
919923
return Ok(None);
@@ -930,11 +934,14 @@ impl LocalStateMachine {
930934
"prior_state_machine.burn_block" => %prior_state_machine.burn_block,
931935
);
932936

933-
//TODO: Remove unwrap once decided the final tx_replay_scope structure format
934-
// and if to handle them as part of State machine update
935-
let curr_scope = tx_replay_scope.clone().unwrap();
937+
let (fork_origin, past_tip) = match tx_replay_scope {
938+
Some(scope) => scope,
939+
None => {
940+
warn!("Tx Replay: BUG! Scope cannot be None while in replay mode!");
941+
return Err(SignerChainstateError::LocalStateMachineNotReady);
942+
}
943+
};
936944

937-
let (fork_origin, past_tip) = &curr_scope;
938945
let is_deepest_fork =
939946
expected_burn_block.burn_block_height < fork_origin.burn_block_height;
940947
if !is_deepest_fork {
@@ -944,22 +951,23 @@ impl LocalStateMachine {
944951
}
945952

946953
let updated_replay_set;
954+
let updated_scope_opt;
947955
if let Some(replay_set) =
948956
self.compute_forked_txs_set(db, client, expected_burn_block, &past_tip)?
949957
{
950-
let updated_scope = (expected_burn_block.clone(), past_tip.clone());
958+
let scope = (expected_burn_block.clone(), past_tip.clone());
951959

952960
info!("Tx Replay: replay set updated with {} tx(s)", replay_set.len();
953961
"tx_replay_set" => ?replay_set,
954-
"tx_replay_scope" => ?updated_scope);
962+
"tx_replay_scope" => ?scope);
955963
updated_replay_set = replay_set;
956-
*tx_replay_scope = Some(updated_scope);
964+
updated_scope_opt = Some(scope);
957965
} else {
958966
info!("Tx Replay: replay set will be cleared, because the fork involves the previous reward cycle.");
959967
updated_replay_set = vec![];
960-
*tx_replay_scope = None;
968+
updated_scope_opt = None;
961969
}
962-
return Ok(Some(updated_replay_set));
970+
return Ok(Some((updated_replay_set, updated_scope_opt)));
963971
}
964972

965973
info!("Signer State: fork detected";
@@ -1040,17 +1048,18 @@ impl LocalStateMachine {
10401048
))
10411049
.cloned()
10421050
.collect::<Vec<_>>();
1051+
let scope_opt;
10431052
if forked_txs.len() > 0 {
1044-
let updated_scope = (expected_burn_block.clone(), potential_replay_tip);
1053+
let scope = (expected_burn_block.clone(), potential_replay_tip);
10451054
info!("Tx Replay: replay set updated with {} tx(s)", forked_txs.len();
10461055
"tx_replay_set" => ?forked_txs,
1047-
"tx_replay_scope" => ?updated_scope);
1048-
*tx_replay_scope = Some(updated_scope);
1056+
"tx_replay_scope" => ?scope);
1057+
scope_opt = Some(scope);
10491058
} else {
10501059
info!("Tx Replay: no transactions to be replayed.");
1051-
*tx_replay_scope = None;
1060+
scope_opt = None;
10521061
}
1053-
Ok(Some(forked_txs))
1062+
Ok(Some((forked_txs, scope_opt)))
10541063
}
10551064

10561065
///TODO: This method can be used to remove dublication in 'handle_possible_bitcoin_fork'

0 commit comments

Comments
 (0)