Skip to content

Commit 275c32b

Browse files
authored
⚡️ Deduplicate buffered proposals, drop on restart (#1374)
1 parent 0d170da commit 275c32b

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

crates/hyle-model/src/node/mempool.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ impl Hashed<DataProposalHash> for DataProposal {
142142
hash
143143
}
144144
}
145+
146+
// Warning: hashing DPs can be slow, so use with care
147+
impl std::hash::Hash for DataProposal {
148+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
149+
self.hashed().hash(state);
150+
}
151+
}
152+
145153
impl Display for DataProposalHash {
146154
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147155
write!(f, "{}", self.0)

src/mempool.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use hyle_modules::{
2626
modules::Module, utils::static_type_map::Pick,
2727
};
2828
use hyle_net::{logged_task::logged_task, ordered_join_set::OrderedJoinSet};
29+
use indexmap::IndexSet;
2930
use metrics::MempoolMetrics;
3031
use serde::{Deserialize, Serialize};
3132
use staking::state::Staking;
@@ -107,7 +108,11 @@ pub struct MempoolStore {
107108
waiting_dissemination_txs: BorshableIndexMap<TxHash, Transaction>,
108109
#[borsh(skip)]
109110
own_data_proposal_in_preparation: JoinSet<(DataProposalHash, DataProposal)>,
110-
buffered_proposals: BTreeMap<LaneId, Vec<DataProposal>>,
111+
// Skipped to clear on reset
112+
#[borsh(skip)]
113+
buffered_proposals: BTreeMap<LaneId, IndexSet<DataProposal>>, // This is an indexSet just so we can pop by idx.
114+
// Skipped to clear on reset
115+
#[borsh(skip)]
111116
buffered_podas: BTreeMap<LaneId, BTreeMap<DataProposalHash, Vec<UnaggregatedPoDA>>>,
112117

113118
// verify_tx.rs
@@ -680,17 +685,19 @@ impl Mempool {
680685
self.lanes
681686
.put_no_verification(lane_id.clone(), (metadata, data_proposal))?;
682687

683-
let mut waiting_proposals = match self.buffered_proposals.get_mut(lane_id) {
688+
// Retry all buffered proposals in this lane.
689+
// We'll re-buffer them in the on_data_proposal logic if they fail to be processed.
690+
let waiting_proposals = match self.buffered_proposals.get_mut(lane_id) {
684691
Some(waiting_proposals) => std::mem::take(waiting_proposals),
685-
None => vec![],
692+
None => Default::default(),
686693
};
687694

688695
// TODO: retry remaining wp when one succeeds to be processed
689-
for wp in waiting_proposals.iter_mut() {
696+
for wp in waiting_proposals.into_iter() {
690697
if self.lanes.contains(lane_id, &wp.hashed()) {
691698
continue;
692699
}
693-
self.on_data_proposal(lane_id, wp.hashed(), std::mem::take(wp))
700+
self.on_data_proposal(lane_id, wp.hashed(), wp)
694701
.context("Consuming waiting data proposal")?;
695702
}
696703

src/mempool/verify_tx.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ impl super::Mempool {
170170
);
171171
}
172172
DataProposalVerdict::Wait => {
173-
debug!("Buffering DataProposal");
173+
debug!("Buffering DataProposal {}", data_proposal_hash);
174174
// Push the data proposal in the waiting list
175175
self.buffered_proposals
176176
.entry(lane_id.clone())
177177
.or_default()
178-
.push(data_proposal);
178+
.insert(data_proposal);
179179
}
180180
DataProposalVerdict::Refuse => {
181-
debug!("Refuse vote for DataProposal");
181+
debug!("Refuse vote for DataProposal {}", data_proposal.hashed());
182182
}
183183
}
184184
Ok(())
@@ -242,8 +242,10 @@ impl super::Mempool {
242242
}
243243
});
244244
if let Some(child_idx) = child_idx {
245-
// We have a buffered proposal that is a child of this DP
246-
dp = Some(buffered_proposals.swap_remove(child_idx));
245+
// We have a buffered proposal that is a child of this DP, process it.
246+
// (I _would_ use a HashSet, but this requires https://github.com/rust-lang/rust/issues/59618
247+
// which is coming in 1.88)
248+
dp = buffered_proposals.swap_remove_index(child_idx);
247249
}
248250
}
249251
if let Some(dp) = dp {

0 commit comments

Comments
 (0)