Skip to content

Commit 86fdda9

Browse files
committed
WIP: JUST ONE MORE TEST TO FIX!
1 parent e2797d4 commit 86fdda9

File tree

4 files changed

+88
-32
lines changed

4 files changed

+88
-32
lines changed

src/bdk_core/coin_select/coin_selector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl WeightedValue {
2626
/// `satisfaction_weight` is the weight of `scriptSigLen + scriptSig + scriptWitnessLen +
2727
/// scriptWitness`.
2828
pub fn new(value: u64, satisfaction_weight: u32, is_segwit: bool) -> WeightedValue {
29+
println!("- wv satisfaction weight: {}", satisfaction_weight);
2930
let weight = TXIN_BASE_WEIGHT + satisfaction_weight;
3031
WeightedValue {
3132
value,

src/psbt/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ mod test {
167167
let fee_amount = psbt.fee_amount();
168168
assert!(fee_amount.is_some());
169169

170+
let weight = psbt.clone().extract_tx().weight();
171+
println!("psbt weight: {}", weight);
172+
println!("inputs: {}", psbt.clone().extract_tx().input.len());
173+
170174
let unfinalized_fee_rate = psbt.fee_rate().unwrap();
171175

172176
let finalized = wallet.sign(&mut psbt, Default::default()).unwrap();
@@ -188,15 +192,27 @@ mod test {
188192
let mut builder = wallet.build_tx();
189193
builder.drain_to(addr.script_pubkey()).drain_wallet();
190194
builder.fee_rate(FeeRate::from_sat_per_vb(expected_fee_rate));
191-
let (mut psbt, _) = builder.finish().unwrap();
195+
let (mut psbt, details) = builder.finish().unwrap();
192196
let fee_amount = psbt.fee_amount();
193197
assert!(fee_amount.is_some());
194198
let unfinalized_fee_rate = psbt.fee_rate().unwrap();
199+
println!("unfinalized fee rate: {:?}", unfinalized_fee_rate);
195200

196201
let finalized = wallet.sign(&mut psbt, Default::default()).unwrap();
197202
assert!(finalized);
198203

204+
let tx = psbt.clone().extract_tx();
205+
// println!("tx: {:#?}", tx);
206+
println!("scriptSig weight: {}", tx.input[0].script_sig.len() * 4);
207+
println!("weight: {}", tx.weight());
208+
let vb = tx.weight() as f32 / 4.0;
209+
println!("vbytes: {}", vb);
210+
let fee = details.fee.unwrap_or(0);
211+
println!("fee: {}", details.fee.unwrap_or(0));
212+
println!("feerate: {} sats/vb", fee as f32 / vb);
213+
199214
let finalized_fee_rate = psbt.fee_rate().unwrap();
215+
println!("finalized fee rate: {:?}", finalized_fee_rate);
200216
assert!(finalized_fee_rate.as_sat_per_vb() >= expected_fee_rate);
201217
assert!(finalized_fee_rate.as_sat_per_vb() < unfinalized_fee_rate.as_sat_per_vb());
202218
}

src/wallet/coin_selection.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ impl CoinSelectionAlgorithm for LargestFirstCoinSelection {
215215
let pool = {
216216
let mut pool = selector.unselected().collect::<Vec<_>>();
217217
pool.sort_unstable_by_key(|(_, candidate)| candidate.value);
218+
pool.reverse();
218219
pool
219220
};
220221

@@ -226,6 +227,7 @@ impl CoinSelectionAlgorithm for LargestFirstCoinSelection {
226227
}
227228

228229
selector.select(index);
230+
println!("selected: index={}, value={}", index, _candidate.value);
229231
selection = selector.finish();
230232
}
231233

src/wallet/mod.rs

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,17 @@ where
924924
};
925925

926926
let selection = coin_selection.coin_select(&raw_candidates, selector)?;
927+
let (_, excess_strategy) = selection.best_strategy();
928+
println!(
929+
"create_tx: weight={}, fee={}, feerate={}sats/wu",
930+
excess_strategy.weight,
931+
excess_strategy.fee,
932+
excess_strategy.feerate()
933+
);
934+
println!(
935+
"create_tx: feerate={}sats/vb",
936+
excess_strategy.feerate() * 4.0
937+
);
927938

928939
// fee_amount += coin_selection.fee_amount;
929940
// let excess = &coin_selection.excess;
@@ -938,8 +949,6 @@ where
938949
})
939950
.collect();
940951

941-
let (_, excess_strategy) = selection.best_strategy();
942-
943952
if let Some(drain_value) = excess_strategy.drain_value {
944953
tx.output.push(TxOut {
945954
value: drain_value,
@@ -1016,23 +1025,27 @@ where
10161025
.map(|utxo| utxo.utxo.clone())
10171026
.collect::<Vec<_>>();
10181027

1019-
let sent = selected.iter().map(|utxo| utxo.txout().value).sum::<u64>();
1028+
let sent = selected
1029+
.iter()
1030+
.filter(|utxo| {
1031+
self.database
1032+
.borrow()
1033+
.is_mine(&utxo.txout().script_pubkey)
1034+
.unwrap_or(false)
1035+
})
1036+
.map(|utxo| utxo.txout().value)
1037+
.sum::<u64>();
10201038

10211039
let received = tx
10221040
.output
10231041
.iter()
1024-
.map(|txo| {
1025-
if self
1026-
.database
1042+
.filter(|txo| {
1043+
self.database
10271044
.borrow()
10281045
.is_mine(&txo.script_pubkey)
10291046
.unwrap_or(false)
1030-
{
1031-
txo.value
1032-
} else {
1033-
0
1034-
}
10351047
})
1048+
.map(|txo| txo.value)
10361049
.sum::<u64>();
10371050

10381051
let psbt = self.complete_transaction(tx, selected, params)?;
@@ -2748,7 +2761,7 @@ pub(crate) mod test {
27482761
}
27492762

27502763
#[test]
2751-
#[should_panic(expected = "InsufficientFunds")]
2764+
// #[should_panic(expected = "InsufficientFunds")]
27522765
fn test_create_tx_absolute_high_fee() {
27532766
let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
27542767
let addr = wallet.get_address(New).unwrap();
@@ -2757,7 +2770,10 @@ pub(crate) mod test {
27572770
.drain_to(addr.script_pubkey())
27582771
.drain_wallet()
27592772
.fee_absolute(60_000);
2760-
let (_psbt, _details) = builder.finish().unwrap();
2773+
// let (_psbt, _details) = builder.finish().unwrap();
2774+
assert!(
2775+
matches!(builder.finish(), Err(Error::Generic(s)) if s.contains("insufficient coins"))
2776+
);
27612777
}
27622778

27632779
#[test]
@@ -2794,7 +2810,7 @@ pub(crate) mod test {
27942810
}
27952811

27962812
#[test]
2797-
#[should_panic(expected = "InsufficientFunds")]
2813+
// #[should_panic(expected = "InsufficientFunds")]
27982814
fn test_create_tx_drain_to_dust_amount() {
27992815
let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
28002816
let addr = wallet.get_address(New).unwrap();
@@ -2804,7 +2820,10 @@ pub(crate) mod test {
28042820
.drain_to(addr.script_pubkey())
28052821
.drain_wallet()
28062822
.fee_rate(FeeRate::from_sat_per_vb(455.0));
2807-
builder.finish().unwrap();
2823+
// builder.finish().unwrap();
2824+
assert!(
2825+
matches!(builder.finish(), Err(Error::Generic(s)) if s.contains("insufficient coins"))
2826+
);
28082827
}
28092828

28102829
#[test]
@@ -3050,7 +3069,7 @@ pub(crate) mod test {
30503069
}
30513070

30523071
#[test]
3053-
#[should_panic(expected = "InsufficientFunds")]
3072+
// #[should_panic(expected = "InsufficientFunds")]
30543073
fn test_create_tx_manually_selected_insufficient() {
30553074
let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh());
30563075
let small_output_txid = crate::populate_test_db!(
@@ -3069,7 +3088,10 @@ pub(crate) mod test {
30693088
})
30703089
.unwrap()
30713090
.manually_selected_only();
3072-
builder.finish().unwrap();
3091+
// builder.finish().unwrap();
3092+
assert!(
3093+
matches!(builder.finish(), Err(Error::Generic(s)) if s.contains("insufficient coins"))
3094+
);
30733095
}
30743096

30753097
#[test]
@@ -3788,7 +3810,7 @@ pub(crate) mod test {
37883810
}
37893811

37903812
#[test]
3791-
#[should_panic(expected = "InsufficientFunds")]
3813+
// #[should_panic(expected = "InsufficientFunds")]
37923814
fn test_bump_fee_remove_output_manually_selected_only() {
37933815
let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh());
37943816
// receive an extra tx so that our wallet has two utxos. then we manually pick only one of
@@ -3836,11 +3858,15 @@ pub(crate) mod test {
38363858
builder
38373859
.manually_selected_only()
38383860
.fee_rate(FeeRate::from_sat_per_vb(255.0));
3839-
builder.finish().unwrap();
3861+
// builder.finish().unwrap();
3862+
assert!(
3863+
matches!(builder.finish(), Err(Error::Generic(s)) if s.contains("insufficient coins"))
3864+
);
38403865
}
38413866

38423867
#[test]
38433868
fn test_bump_fee_add_input() {
3869+
// funded wallet already has 50_000 utxo
38443870
let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh());
38453871
crate::populate_test_db!(
38463872
wallet.database.borrow_mut(),
@@ -4262,7 +4288,7 @@ pub(crate) mod test {
42624288
}
42634289

42644290
#[test]
4265-
#[should_panic(expected = "InsufficientFunds")]
4291+
// #[should_panic(expected = "InsufficientFunds")]
42664292
fn test_bump_fee_unconfirmed_inputs_only() {
42674293
// We try to bump the fee, but:
42684294
// - We can't reduce the change, as we have no change
@@ -4304,7 +4330,10 @@ pub(crate) mod test {
43044330

43054331
let mut builder = wallet.build_fee_bump(txid).unwrap();
43064332
builder.fee_rate(FeeRate::from_sat_per_vb(25.0));
4307-
builder.finish().unwrap();
4333+
assert!(
4334+
matches!(builder.finish(), Err(Error::Generic(s)) if s.contains("insufficient coins"))
4335+
);
4336+
// builder.finish().unwrap();
43084337
}
43094338

43104339
#[test]
@@ -5564,25 +5593,33 @@ pub(crate) mod test {
55645593
.add_recipient(addr.script_pubkey(), balance.immature / 2)
55655594
.current_height(confirmation_time);
55665595
assert!(matches!(
5567-
builder.finish().unwrap_err(),
5568-
Error::InsufficientFunds {
5569-
needed: _,
5570-
available: 0
5571-
}
5596+
builder.finish(),
5597+
Err(Error::Generic(s)) if s.contains("insufficient coins")
55725598
));
5599+
// assert!(matches!(
5600+
// builder.finish().unwrap_err(),
5601+
// Error::InsufficientFunds {
5602+
// needed: _,
5603+
// available: 0
5604+
// }
5605+
// ));
55735606

55745607
// Still unspendable...
55755608
let mut builder = wallet.build_tx();
55765609
builder
55775610
.add_recipient(addr.script_pubkey(), balance.immature / 2)
55785611
.current_height(not_yet_mature_time);
55795612
assert!(matches!(
5580-
builder.finish().unwrap_err(),
5581-
Error::InsufficientFunds {
5582-
needed: _,
5583-
available: 0
5584-
}
5613+
builder.finish(),
5614+
Err(Error::Generic(s)) if s.contains("insufficient coins")
55855615
));
5616+
// assert!(matches!(
5617+
// builder.finish().unwrap_err(),
5618+
// Error::InsufficientFunds {
5619+
// needed: _,
5620+
// available: 0
5621+
// }
5622+
// ));
55865623

55875624
// ...Now the coinbase is mature :)
55885625
let sync_time = SyncTime {

0 commit comments

Comments
 (0)