Skip to content

Commit 50853bb

Browse files
authored
Merge pull request #885 from h4sh3d/refactor/temp-safety-refactor-2
Refactor: complete temp safety renaming and cleaning
2 parents af28952 + b2cdde7 commit 50853bb

4 files changed

Lines changed: 31 additions & 35 deletions

File tree

src/swapd/runtime.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use super::{
1010
temporal_safety::TemporalSafety,
1111
StateReport,
1212
};
13+
use crate::swapd::temporal_safety::SWEEP_MONERO_THRESHOLD;
1314
use crate::swapd::Opts;
1415
use crate::syncerd::bitcoin_syncer::p2wpkh_signed_tx_fee;
1516
use crate::syncerd::types::{Event, TransactionConfirmations};
@@ -86,10 +87,9 @@ pub fn run(config: ServiceConfig, opts: Opts) -> Result<(), Error> {
8687
let temporal_safety = TemporalSafety {
8788
cancel_timelock: cancel_timelock.as_u32(),
8889
punish_timelock: punish_timelock.as_u32(),
89-
btc_finality_thr: arbitrating_finality.into(),
90-
race_thr: arbitrating_safety.into(),
91-
xmr_finality_thr: accordant_finality.into(),
92-
sweep_monero_thr: crate::swapd::temporal_safety::SWEEP_MONERO_THRESHOLD,
90+
arb_finality: arbitrating_finality.into(),
91+
safety: arbitrating_safety.into(),
92+
acc_finality: accordant_finality.into(),
9393
};
9494

9595
temporal_safety.valid_params()?;
@@ -540,7 +540,7 @@ impl Runtime {
540540
id,
541541
confirmations,
542542
self.swap_id(),
543-
self.temporal_safety.xmr_finality_thr,
543+
self.temporal_safety.acc_finality,
544544
endpoints,
545545
);
546546

@@ -608,7 +608,7 @@ impl Runtime {
608608
id,
609609
&Some(*confirmations),
610610
self.swap_id(),
611-
self.temporal_safety.btc_finality_thr,
611+
self.temporal_safety.arb_finality,
612612
endpoints,
613613
);
614614
// saving requests of interest for later replaying latest event
@@ -628,7 +628,7 @@ impl Runtime {
628628
id,
629629
confirmations,
630630
self.swap_id(),
631-
self.temporal_safety.btc_finality_thr,
631+
self.temporal_safety.arb_finality,
632632
endpoints,
633633
);
634634
// saving requests of interest for later replaying latest event
@@ -848,8 +848,8 @@ impl Runtime {
848848
let acc_confs_needs = self
849849
.syncer_state
850850
.get_confs(TxLabel::AccLock)
851-
.map(|confs| self.temporal_safety.sweep_monero_thr.saturating_sub(confs))
852-
.unwrap_or(self.temporal_safety.sweep_monero_thr);
851+
.map(|confs| SWEEP_MONERO_THRESHOLD.saturating_sub(confs))
852+
.unwrap_or(SWEEP_MONERO_THRESHOLD);
853853
let sweep_block = self.syncer_state.height(Blockchain::Monero) + acc_confs_needs as u64;
854854
self.log_info(format!(
855855
"Tx {} needs {} more confirmations to spending maturity, and has {} confirmations.\n\

src/swapd/state_report.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use farcaster_core::{blockchain::Blockchain, transaction::TxLabel};
88
use strict_encoding::{NetworkDecode, NetworkEncode};
99

1010
use crate::bus::{Progress, StateTransition};
11+
use crate::swapd::temporal_safety::SWEEP_MONERO_THRESHOLD;
1112

1213
use super::{syncer_client::SyncerState, temporal_safety::TemporalSafety};
1314

@@ -70,13 +71,13 @@ impl StateReport {
7071
.map(|confs| temp_safety.blocks_until_cancel(confs)),
7172
blocks_until_safe_buy: syncer_state
7273
.get_confs(TxLabel::Lock)
73-
.map(|c| temp_safety.btc_finality_thr.saturating_sub(c)),
74+
.map(|c| temp_safety.arb_finality.saturating_sub(c)),
7475
blocks_until_punish_possible: syncer_state
7576
.get_confs(TxLabel::Cancel)
7677
.map(|confs| temp_safety.blocks_until_punish_after_cancel(confs)),
7778
blocks_until_safe_monero_buy_sweep: syncer_state
7879
.get_confs(TxLabel::AccLock)
79-
.map(|c| temp_safety.sweep_monero_thr.saturating_sub(c)),
80+
.map(|c| SWEEP_MONERO_THRESHOLD.saturating_sub(c)),
8081
}
8182
}
8283

src/swapd/swap_state.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use microservices::esb::Handler;
2222
use monero::ViewPair;
2323
use strict_encoding::{StrictDecode, StrictEncode};
2424

25+
use crate::swapd::temporal_safety::SWEEP_MONERO_THRESHOLD;
2526
use crate::{
2627
bus::{ctl::MoneroFundingInfo, p2p::Reveal},
2728
syncerd::AddressTransaction,
@@ -1325,7 +1326,7 @@ fn try_bob_buy_seen_to_bob_buy_sweeping(
13251326
confirmations: Some(confirmations),
13261327
..
13271328
},
1328-
))) if confirmations >= runtime.temporal_safety.sweep_monero_thr => {
1329+
))) if confirmations >= SWEEP_MONERO_THRESHOLD => {
13291330
// safe cast
13301331
let request = SyncMsg::Task(Task::SweepAddress(task));
13311332
runtime.log_info(format!(
@@ -2119,7 +2120,7 @@ fn try_alice_refund_to_alice_refund_sweeping(
21192120
confirmations: Some(confirmations),
21202121
..
21212122
},
2122-
))) if confirmations >= runtime.temporal_safety.sweep_monero_thr => {
2123+
))) if confirmations >= SWEEP_MONERO_THRESHOLD => {
21232124
runtime.log_info(format!(
21242125
"Monero are spendable now (height {}), sweeping ephemeral swap_key_manager",
21252126
runtime.syncer_state.monero_height.label(),

src/swapd/temporal_safety.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,23 @@ pub struct TemporalSafety {
2727
/// Second timelock in the protocol, if this timelock is reached Bob miss-behaved and will lose
2828
/// money
2929
pub punish_timelock: BlockHeight,
30-
/// The minimum number of blocks that should remain unmined before the next transaction to be
31-
/// considered safe
32-
pub race_thr: BlockSpan,
30+
/// Avoid broadcasting a transaction if a race can happen with the next available execution
31+
/// fork in # blocks
32+
pub safety: BlockSpan,
3333
/// Number of confirmation required for the arbitrating blockchain to consider a tx final
34-
pub btc_finality_thr: BlockSpan,
34+
pub arb_finality: BlockSpan,
3535
/// Number of confirmation required for the accordant blockchain to consider a tx final
36-
pub xmr_finality_thr: BlockSpan,
37-
// FIXME: this should be removed, used only const instead
38-
pub sweep_monero_thr: BlockSpan,
36+
pub acc_finality: BlockSpan,
3937
}
4038

4139
impl TemporalSafety {
4240
/// Validate if temporal parameters are coherent
4341
pub fn valid_params(&self) -> Result<(), Error> {
44-
let btc_finality = self.btc_finality_thr;
42+
let finality = self.arb_finality;
4543
let cancel = self.cancel_timelock;
4644
let punish = self.punish_timelock;
47-
let race = self.race_thr;
48-
if btc_finality < cancel
49-
&& cancel < punish
50-
&& btc_finality < race
51-
&& punish > race
52-
&& cancel > race
45+
let race = self.safety;
46+
if finality < cancel && cancel < punish && finality < race && punish > race && cancel > race
5347
{
5448
Ok(())
5549
} else {
@@ -61,23 +55,23 @@ impl TemporalSafety {
6155

6256
/// Returns whether tx is final given the finality threshold set for the chain
6357
pub fn final_tx(&self, confs: u32, blockchain: Blockchain) -> bool {
64-
let finality_thr = match blockchain {
65-
Blockchain::Bitcoin => self.btc_finality_thr,
66-
Blockchain::Monero => self.xmr_finality_thr,
58+
let finality = match blockchain {
59+
Blockchain::Bitcoin => self.arb_finality,
60+
Blockchain::Monero => self.acc_finality,
6761
};
68-
confs >= finality_thr
62+
confs >= finality
6963
}
7064

7165
/// Lock must be final, cancel cannot be raced, add + 1 to offset initial lock confirmation
7266
pub fn stop_funding_before_cancel(&self, lock_confirmations: u32) -> bool {
7367
self.final_tx(lock_confirmations, Blockchain::Bitcoin)
74-
&& lock_confirmations > (self.cancel_timelock - self.race_thr + 1)
68+
&& lock_confirmations > (self.cancel_timelock - self.safety + 1)
7569
}
7670

7771
// Blocks remaining until funding will be stopped for safety, because it is too close to
7872
// cancel. Adds the same +1 offset as in stop_funding_before_cancel
7973
pub fn blocks_until_stop_funding(&self, lock_confirmations: u32) -> i64 {
80-
self.cancel_timelock as i64 - (self.race_thr as i64 + 1 + lock_confirmations as i64)
74+
self.cancel_timelock as i64 - (self.safety as i64 + 1 + lock_confirmations as i64)
8175
}
8276

8377
/// Lock must be final, valid after lock_minedblock + cancel_timelock
@@ -94,13 +88,13 @@ impl TemporalSafety {
9488
/// Lock must be final, but buy shall not be raced with cancel
9589
pub fn safe_buy(&self, lock_confirmations: u32) -> bool {
9690
self.final_tx(lock_confirmations, Blockchain::Bitcoin)
97-
&& lock_confirmations <= (self.cancel_timelock - self.race_thr)
91+
&& lock_confirmations <= (self.cancel_timelock - self.safety)
9892
}
9993

10094
/// Cancel must be final, but refund shall not be raced with punish
10195
pub fn safe_refund(&self, cancel_confirmations: u32) -> bool {
10296
self.final_tx(cancel_confirmations, Blockchain::Bitcoin)
103-
&& cancel_confirmations <= (self.punish_timelock - self.race_thr)
97+
&& cancel_confirmations <= (self.punish_timelock - self.safety)
10498
}
10599

106100
/// Cancel must be final, valid after cancel_confirmations > punish_timelock

0 commit comments

Comments
 (0)