Skip to content

Commit 08f1a7e

Browse files
committed
fix: avoid allocation of into_iter
1 parent 7331a62 commit 08f1a7e

3 files changed

Lines changed: 59 additions & 78 deletions

File tree

backend/src/bridge/cache.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl BridgeStatusCache {
140140
self.deposits
141141
.iter()
142142
.filter(|(_, entry)| filter(&entry.data.status))
143-
.map(|(txid, entry)| (*txid, entry.data.clone()))
143+
.map(|(txid, entry)| (*txid, entry.data))
144144
.collect()
145145
}
146146

@@ -152,7 +152,7 @@ impl BridgeStatusCache {
152152
self.withdrawals
153153
.iter()
154154
.filter(|(_, entry)| filter(&entry.data.status))
155-
.map(|(request_id, entry)| (*request_id, entry.data.clone()))
155+
.map(|(request_id, entry)| (*request_id, entry.data))
156156
.collect()
157157
}
158158

@@ -164,7 +164,7 @@ impl BridgeStatusCache {
164164
self.reimbursements
165165
.iter()
166166
.filter(|(_, entry)| filter(&entry.data.status))
167-
.map(|(txid, entry)| (*txid, entry.data.clone()))
167+
.map(|(txid, entry)| (*txid, entry.data))
168168
.collect()
169169
}
170170

backend/src/bridge/status.rs

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,6 @@ async fn get_tx_confirmations(esplora_url: &str, txid: Txid, chain_tip_height: u
4949
.unwrap_or(0)
5050
}
5151

52-
/// Helper function to filter bridge status items based on confirmations
53-
async fn filter_by_confirmations<T>(
54-
txid: Txid,
55-
item: T,
56-
config: &BridgeMonitoringConfig,
57-
chain_tip_height: u64,
58-
) -> Option<(T, u64)>
59-
where
60-
T: Clone,
61-
{
62-
let confirmations = get_tx_confirmations(config.esplora_url(), txid, chain_tip_height).await;
63-
64-
if confirmations < config.max_tx_confirmations() {
65-
Some((item, confirmations))
66-
} else {
67-
None
68-
}
69-
}
70-
7152
/// Determine which cached deposit entries should be purged
7253
async fn determine_deposits_to_purge(
7354
cache: &BridgeStatusCache,
@@ -148,7 +129,9 @@ async fn determine_reimbursements_to_purge(
148129
ReimbursementStatus::Complete => reimbursement_info
149130
.payout_txid
150131
.unwrap_or(reimbursement_info.claim_txid),
151-
ReimbursementStatus::Challenged | ReimbursementStatus::InProgress => unreachable!(),
132+
ReimbursementStatus::Challenged
133+
| ReimbursementStatus::InProgress
134+
| ReimbursementStatus::NotStarted => unreachable!(),
152135
};
153136

154137
let current_confirmations =
@@ -208,13 +191,13 @@ pub async fn bridge_monitoring_task(context: Arc<BridgeMonitoringContext>) {
208191
let cache = context.status_cache.read().await;
209192
let deposits: Vec<Txid> = cache
210193
.filter_deposits(|s| matches!(s, DepositStatus::InProgress))
211-
.into_iter()
212-
.map(|(txid, _)| txid)
194+
.iter()
195+
.map(|(txid, _)| *txid)
213196
.collect();
214197
let withdrawals: Vec<Buf32> = cache
215198
.filter_withdrawals(|s| matches!(s, WithdrawalStatus::InProgress))
216-
.into_iter()
217-
.map(|(request_id, _)| request_id)
199+
.iter()
200+
.map(|(request_id, _)| *request_id)
218201
.collect();
219202
let reimbursements: Vec<Txid> = cache
220203
.filter_reimbursements(|s| {
@@ -223,8 +206,8 @@ pub async fn bridge_monitoring_task(context: Arc<BridgeMonitoringContext>) {
223206
ReimbursementStatus::InProgress | ReimbursementStatus::Challenged
224207
)
225208
})
226-
.into_iter()
227-
.map(|(txid, _)| txid)
209+
.iter()
210+
.map(|(txid, _)| *txid)
228211
.collect();
229212
(deposits, withdrawals, reimbursements)
230213
};
@@ -233,8 +216,8 @@ pub async fn bridge_monitoring_task(context: Arc<BridgeMonitoringContext>) {
233216
let deposit_updates: Vec<(Txid, DepositInfo, u64)> =
234217
get_deposits(&context.config, chain_tip_height, &active_deposits)
235218
.await
236-
.into_iter()
237-
.map(|(info, confirmations)| (info.deposit_request_txid, info, confirmations))
219+
.iter()
220+
.map(|(info, confirmations)| (info.deposit_request_txid, *info, *confirmations))
238221
.collect();
239222

240223
{
@@ -251,8 +234,8 @@ pub async fn bridge_monitoring_task(context: Arc<BridgeMonitoringContext>) {
251234
let withdrawal_updates: Vec<(Buf32, WithdrawalInfo, u64)> =
252235
get_withdrawals(&context.config, chain_tip_height, &active_withdrawals)
253236
.await
254-
.into_iter()
255-
.map(|(info, confirmations)| (info.withdrawal_request_txid, info, confirmations))
237+
.iter()
238+
.map(|(info, confirmations)| (info.withdrawal_request_txid, *info, *confirmations))
256239
.collect();
257240

258241
{
@@ -269,8 +252,8 @@ pub async fn bridge_monitoring_task(context: Arc<BridgeMonitoringContext>) {
269252
let reimbursement_updates: Vec<(Txid, ReimbursementInfo, u64)> =
270253
get_reimbursements(&context.config, chain_tip_height, &active_reimbursements)
271254
.await
272-
.into_iter()
273-
.map(|(info, confirmations)| (info.claim_txid, info, confirmations))
255+
.iter()
256+
.map(|(info, confirmations)| (info.claim_txid, *info, *confirmations))
274257
.collect();
275258

276259
{
@@ -388,15 +371,10 @@ async fn get_deposits(
388371
RpcDepositStatus::InProgress => unreachable!(), // Already matched
389372
};
390373

391-
if let Some(result) = filter_by_confirmations(
392-
txid,
393-
DepositInfo::from(&dep_info),
394-
config,
395-
chain_tip_height,
396-
)
397-
.await
398-
{
399-
deposit_infos.push(result);
374+
let confirmations =
375+
get_tx_confirmations(config.esplora_url(), txid, chain_tip_height).await;
376+
if confirmations < config.max_tx_confirmations() {
377+
deposit_infos.push((DepositInfo::from(&dep_info), confirmations));
400378
}
401379
}
402380
}
@@ -471,15 +449,11 @@ async fn get_withdrawals(
471449
withdrawal_infos.push((WithdrawalInfo::from(&wd_info), 0));
472450
}
473451
RpcWithdrawalStatus::Complete { fulfillment_txid } => {
474-
if let Some(result) = filter_by_confirmations(
475-
*fulfillment_txid,
476-
WithdrawalInfo::from(&wd_info),
477-
config,
478-
chain_tip_height,
479-
)
480-
.await
481-
{
482-
withdrawal_infos.push(result);
452+
let confirmations =
453+
get_tx_confirmations(config.esplora_url(), *fulfillment_txid, chain_tip_height)
454+
.await;
455+
if confirmations < config.max_tx_confirmations() {
456+
withdrawal_infos.push((WithdrawalInfo::from(&wd_info), confirmations));
483457
}
484458
}
485459
}
@@ -563,15 +537,10 @@ async fn get_reimbursements(
563537
| RpcReimbursementStatus::InProgress { .. }
564538
| RpcReimbursementStatus::Challenged { .. } => unreachable!(), // Already matched
565539
};
566-
if let Some(result) = filter_by_confirmations(
567-
txid,
568-
ReimbursementInfo::from(&claim_info),
569-
config,
570-
chain_tip_height,
571-
)
572-
.await
573-
{
574-
reimbursement_infos.push(result);
540+
let confirmations =
541+
get_tx_confirmations(config.esplora_url(), txid, chain_tip_height).await;
542+
if confirmations < config.max_tx_confirmations() {
543+
reimbursement_infos.push((ReimbursementInfo::from(&claim_info), confirmations));
575544
}
576545
}
577546
}

backend/src/bridge/types.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) struct TxStatus {
3434
pub(crate) block_height: Option<u64>,
3535
}
3636

37-
#[derive(Serialize, Deserialize, Clone, Debug)]
37+
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
3838
pub(crate) enum DepositStatus {
3939
#[serde(rename = "In progress")]
4040
InProgress,
@@ -43,7 +43,7 @@ pub(crate) enum DepositStatus {
4343
}
4444

4545
/// Deposit information passed to status dashboard
46-
#[derive(Debug, Clone, Deserialize, Serialize)]
46+
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
4747
pub(crate) struct DepositInfo {
4848
pub(crate) deposit_request_txid: Txid,
4949
pub(crate) deposit_txid: Option<Txid>,
@@ -73,15 +73,15 @@ impl From<&RpcDepositInfo> for DepositInfo {
7373
}
7474

7575
/// Withdrawal status
76-
#[derive(Serialize, Deserialize, Clone, Debug)]
76+
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
7777
pub(crate) enum WithdrawalStatus {
7878
#[serde(rename = "In progress")]
7979
InProgress,
8080
Complete,
8181
}
8282

8383
/// Withdrawal information passed to status dashboard
84-
#[derive(Debug, Clone, Deserialize, Serialize)]
84+
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
8585
pub(crate) struct WithdrawalInfo {
8686
pub(crate) withdrawal_request_txid: Buf32,
8787
pub(crate) fulfillment_txid: Option<Txid>,
@@ -106,20 +106,32 @@ impl From<&RpcWithdrawalInfo> for WithdrawalInfo {
106106
}
107107

108108
/// Reimbursement status
109-
#[derive(Serialize, Deserialize, Clone, Debug)]
110-
pub enum ReimbursementStatus {
109+
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
110+
pub(crate) enum ReimbursementStatus {
111+
#[serde(rename = "Not started")]
112+
NotStarted,
111113
#[serde(rename = "In progress")]
112114
InProgress,
113115
Challenged,
114116
Cancelled,
115117
Complete,
116118
}
117119

120+
/// Challenge step for reimbursements
121+
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
122+
pub(crate) enum ChallengeStep {
123+
#[serde(rename = "N/A")]
124+
NotApplicable,
125+
Claim,
126+
Challenge,
127+
Assert,
128+
}
129+
118130
/// Claim and reimbursement information passed to status dashboard
119-
#[derive(Debug, Clone, Deserialize, Serialize)]
131+
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
120132
pub(crate) struct ReimbursementInfo {
121133
pub(crate) claim_txid: Txid,
122-
pub(crate) challenge_step: String,
134+
pub(crate) challenge_step: ChallengeStep,
123135
pub(crate) payout_txid: Option<Txid>,
124136
pub(crate) status: ReimbursementStatus,
125137
}
@@ -129,31 +141,31 @@ impl From<&RpcClaimInfo> for ReimbursementInfo {
129141
match &rpc_info.status {
130142
RpcReimbursementStatus::NotStarted => Self {
131143
claim_txid: rpc_info.claim_txid,
132-
challenge_step: "N/A".to_string(),
144+
challenge_step: ChallengeStep::NotApplicable,
133145
payout_txid: None,
134-
status: ReimbursementStatus::InProgress,
146+
status: ReimbursementStatus::NotStarted,
135147
},
136-
RpcReimbursementStatus::InProgress { challenge_step, .. } => Self {
148+
RpcReimbursementStatus::InProgress { .. } => Self {
137149
claim_txid: rpc_info.claim_txid,
138-
challenge_step: format!("{challenge_step:?}"),
150+
challenge_step: ChallengeStep::Claim,
139151
payout_txid: None,
140152
status: ReimbursementStatus::InProgress,
141153
},
142-
RpcReimbursementStatus::Challenged { challenge_step, .. } => Self {
154+
RpcReimbursementStatus::Challenged { .. } => Self {
143155
claim_txid: rpc_info.claim_txid,
144-
challenge_step: format!("{challenge_step:?}"),
156+
challenge_step: ChallengeStep::Challenge,
145157
payout_txid: None,
146158
status: ReimbursementStatus::Challenged,
147159
},
148160
RpcReimbursementStatus::Cancelled => Self {
149161
claim_txid: rpc_info.claim_txid,
150-
challenge_step: "N/A".to_string(),
162+
challenge_step: ChallengeStep::NotApplicable,
151163
payout_txid: None,
152164
status: ReimbursementStatus::Cancelled,
153165
},
154166
RpcReimbursementStatus::Complete { payout_txid } => Self {
155167
claim_txid: rpc_info.claim_txid,
156-
challenge_step: "N/A".to_string(),
168+
challenge_step: ChallengeStep::NotApplicable,
157169
payout_txid: Some(*payout_txid),
158170
status: ReimbursementStatus::Complete,
159171
},

0 commit comments

Comments
 (0)