Skip to content

Commit d2ba08d

Browse files
committed
feat: add MaxWeight to cap transaction weight during selection
1 parent a2c9abc commit d2ba08d

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/metrics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ mod lowest_fee;
55
pub use lowest_fee::*;
66
mod changeless;
77
pub use changeless::*;
8+
mod max_weight;
9+
pub use max_weight::*;
810

911
// Returns a drain if the current selection and every possible future selection would have a change
1012
// output (otherwise Drain::none()) by using the heurisitic that if it has change with the current

src/metrics/max_weight.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use crate::{bnb::BnbMetric, float::Ordf32, ChangePolicy, CoinSelector, DrainWeights, Target};
2+
3+
/// Wraps a [`BnbMetric`], rejecting any selection whose weight (change included) exceeds `max_weight`.
4+
#[derive(Clone, Copy, Debug)]
5+
pub struct MaxWeight<M> {
6+
/// The target the inner metrics funds.
7+
pub target: Target,
8+
/// The change policy the inner metric uses.
9+
pub change_policy: ChangePolicy,
10+
/// Maximum allowed transaction weight.
11+
pub max_weight: u64,
12+
/// The inner metric that drives optimization.
13+
pub metric: M,
14+
}
15+
16+
impl<M: BnbMetric> BnbMetric for MaxWeight<M> {
17+
fn score(&mut self, cs: &CoinSelector<'_>) -> Option<Ordf32> {
18+
let score = self.metric.score(cs)?;
19+
let drain = cs.drain(self.target, self.change_policy);
20+
if cs.weight(self.target.outputs, drain.weights) > self.max_weight {
21+
return None;
22+
}
23+
Some(score)
24+
}
25+
26+
fn bound(&mut self, cs: &CoinSelector<'_>) -> Option<Ordf32> {
27+
if cs.weight(self.target.outputs, DrainWeights::NONE) > self.max_weight {
28+
return None;
29+
}
30+
self.metric.bound(cs)
31+
}
32+
33+
fn requires_ordering_by_descending_value_pwu(&self) -> bool {
34+
self.metric.requires_ordering_by_descending_value_pwu()
35+
}
36+
}

0 commit comments

Comments
 (0)