-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstate_report.rs
More file actions
103 lines (97 loc) · 3.95 KB
/
state_report.rs
File metadata and controls
103 lines (97 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2020-2022 Farcaster Devs & LNP/BP Standards Association
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
use farcaster_core::{blockchain::Blockchain, transaction::TxLabel};
use strict_encoding::{NetworkDecode, NetworkEncode};
use crate::bus::{Progress, StateTransition};
use crate::swapd::temporal_safety::SWEEP_MONERO_THRESHOLD;
use super::{syncer_client::SyncerState, temporal_safety::TemporalSafety};
#[derive(Clone, Debug, Eq, PartialEq, Hash, Display, NetworkEncode, NetworkDecode)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
#[display(Debug)]
pub struct StateReport {
pub state: String,
pub arb_block_height: u64,
pub acc_block_height: u64,
pub arb_locked: bool,
pub acc_locked: bool,
pub canceled: bool,
pub buy_seen: bool,
pub refund_seen: bool,
pub overfunded: bool,
pub arb_lock_confirmations: Option<u32>,
pub acc_lock_confirmations: Option<u32>,
pub cancel_confirmations: Option<u32>,
pub refund_confirmations: Option<u32>,
pub punish_confirmations: Option<u32>,
pub buy_confirmations: Option<u32>,
pub blocks_until_cancel_possible: Option<i64>,
pub blocks_until_punish_possible: Option<i64>,
pub blocks_until_safe_buy: Option<u32>,
pub blocks_until_safe_monero_buy_sweep: Option<u32>,
}
impl StateReport {
pub fn new(
state: String,
temp_safety: &TemporalSafety,
syncer_state: &SyncerState,
) -> StateReport {
StateReport {
state,
arb_block_height: syncer_state.bitcoin_height,
acc_block_height: syncer_state.monero_height,
arb_locked: temp_safety.final_tx(
syncer_state.get_confs(TxLabel::Lock).unwrap_or(0),
Blockchain::Bitcoin,
),
acc_locked: temp_safety.final_tx(
syncer_state.get_confs(TxLabel::AccLock).unwrap_or(0),
Blockchain::Monero,
),
canceled: temp_safety.final_tx(
syncer_state.get_confs(TxLabel::Cancel).unwrap_or(0),
Blockchain::Bitcoin,
),
buy_seen: syncer_state.get_confs(TxLabel::Buy).is_some(),
refund_seen: syncer_state.get_confs(TxLabel::Refund).is_some(),
overfunded: false, // FIXME
arb_lock_confirmations: syncer_state.get_confs(TxLabel::Lock),
acc_lock_confirmations: syncer_state.get_confs(TxLabel::AccLock),
cancel_confirmations: syncer_state.get_confs(TxLabel::Cancel),
refund_confirmations: syncer_state.get_confs(TxLabel::Refund),
punish_confirmations: syncer_state.get_confs(TxLabel::Punish),
buy_confirmations: syncer_state.get_confs(TxLabel::Buy),
blocks_until_cancel_possible: syncer_state
.get_confs(TxLabel::Lock)
.map(|confs| temp_safety.blocks_until_cancel(confs)),
blocks_until_safe_buy: syncer_state
.get_confs(TxLabel::Lock)
.map(|c| temp_safety.arb_finality.saturating_sub(c)),
blocks_until_punish_possible: syncer_state
.get_confs(TxLabel::Cancel)
.map(|confs| temp_safety.blocks_until_punish_after_cancel(confs)),
blocks_until_safe_monero_buy_sweep: syncer_state
.get_confs(TxLabel::AccLock)
.map(|c| SWEEP_MONERO_THRESHOLD.saturating_sub(c)),
}
}
pub fn generate_progress_update_or_transition(
&self,
new_state_report: &StateReport,
) -> Progress {
if self.state == new_state_report.state {
Progress::StateUpdate(new_state_report.clone())
} else {
Progress::StateTransition(StateTransition {
old_state: self.clone(),
new_state: new_state_report.clone(),
})
}
}
}