Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod lowest_fee;
pub use lowest_fee::*;
mod changeless;
pub use changeless::*;
mod max_weight;
pub use max_weight::*;

// Returns a drain if the current selection and every possible future selection would have a change
// output (otherwise Drain::none()) by using the heurisitic that if it has change with the current
Expand Down
36 changes: 36 additions & 0 deletions src/metrics/max_weight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::{bnb::BnbMetric, float::Ordf32, ChangePolicy, CoinSelector, DrainWeights, Target};

/// Wraps a [`BnbMetric`], rejecting any selection whose weight (change included) exceeds `max_weight`.
#[derive(Clone, Copy, Debug)]
pub struct MaxWeight<M> {
/// The target the inner metrics funds.
pub target: Target,
/// The change policy the inner metric uses.
pub change_policy: ChangePolicy,
/// Maximum allowed transaction weight.
pub max_weight: u64,
/// The inner metric that drives optimization.
pub metric: M,
}

impl<M: BnbMetric> BnbMetric for MaxWeight<M> {
fn score(&mut self, cs: &CoinSelector<'_>) -> Option<Ordf32> {
let score = self.metric.score(cs)?;
let drain = cs.drain(self.target, self.change_policy);
if cs.weight(self.target.outputs, drain.weights) > self.max_weight {
return None;
}
Some(score)
}

fn bound(&mut self, cs: &CoinSelector<'_>) -> Option<Ordf32> {
if cs.weight(self.target.outputs, DrainWeights::NONE) > self.max_weight {
return None;
}
self.metric.bound(cs)
}

fn requires_ordering_by_descending_value_pwu(&self) -> bool {
self.metric.requires_ordering_by_descending_value_pwu()
}
}
Loading