Skip to content

Commit 0f7cc31

Browse files
committed
Implement proper RBF logic satisfying rule 4
Removes `min_fee` constraint in favour of proper rule 4 handling (min_fee was pretty useless). See for rule 4: https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md#current-replace-by-fee-policy LowestFee BnB metric updated to bound correctly for this. A few other coin_selector API changes
1 parent 7a28288 commit 0f7cc31

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/metrics/lowest_fee.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ impl BnbMetric for LowestFee {
5252

5353
let drain_value = cs.drain_value(self.target, self.change_policy);
5454

55+
// I think this whole if statement could be removed if we made this metric decide the change policy
5556
if let Some(drain_value) = drain_value {
56-
// it's possible that adding another input might reduce your long term fee if it gets
57-
// rid of an expensive change output. Our strategy is to take the lowest sat per
58-
// value candidate we have and use it as a benchmark. We imagine it has the perfect
59-
// value (but the same sats per weight unit) to get rid of the change output by
60-
// adding negative effective value (i.e. perfectly reducing excess to the point
57+
// it's possible that adding another input might reduce your long term fee if it
58+
// gets rid of an expensive change output. Our strategy is to take the lowest sat
59+
// per value candidate we have and use it as a benchmark. We imagine it has the
60+
// perfect value (but the same sats per weight unit) to get rid of the change output
61+
// by adding negative effective value (i.e. perfectly reducing excess to the point
6162
// where change wouldn't be added according to the policy).
6263
//
6364
// TODO: This metric could be tighter by being more complicated but this seems to be
@@ -91,6 +92,19 @@ impl BnbMetric for LowestFee {
9192
}
9293
}
9394
}
95+
} else {
96+
// Ok but maybe adding change could improve the metric?
97+
let cost_of_adding_change = self
98+
.change_policy
99+
.drain_weights
100+
.waste(self.target.fee.rate, self.long_term_feerate);
101+
let cost_of_no_change = cs.excess(self.target, Drain::none());
102+
103+
let best_score_with_change =
104+
Ordf32(current_score.0 - cost_of_no_change as f32 + cost_of_adding_change);
105+
if best_score_with_change < current_score {
106+
return Some(best_score_with_change);
107+
}
94108
}
95109

96110
Some(current_score)

tests/lowest_fee.proptest-regressions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ cc 9c841bb85574de2412972df187e7ebd01f7a06a178a67f4d99c0178dd578ac34 # shrinks to
88
cc e30499b75a1846759fc9ffd7ee558b08a4795598cf7919f6be6d62cc7a79d4cb # shrinks to n_candidates = 25, target_value = 56697, base_weight = 621, min_fee = 0, feerate = 9.417939, feerate_lt_diff = 0.0, drain_weight = 100, drain_spend_weight = 1, drain_dust = 100
99
cc c580ee452624915fc710d5fe724c7a9347472ccd178f66c9db9479cfc6168f48 # shrinks to n_candidates = 25, target_value = 488278, base_weight = 242, min_fee = 0, feerate = 6.952743, feerate_lt_diff = 0.0, drain_weight = 100, drain_spend_weight = 1, drain_dust = 100
1010
cc 850e0115aeeb7ed50235fdb4b5183eb5bf8309a45874dc261e3d3fd2d8c84660 # shrinks to n_candidates = 8, target_value = 444541, base_weight = 253, min_fee = 0, feerate = 55.98181, feerate_lt_diff = 36.874306, drain_weight = 490, drain_spend_weight = 1779, drain_dust = 100
11+
cc ca9831dfe4ad27fc705ae4af201b9b50739404bda0e1e130072858634f8d25d9 # added by llfourn from ci: has a case where the lowest fee metric would benefit by adding change

0 commit comments

Comments
 (0)