Skip to content

Commit eff7a50

Browse files
committed
WIP: JUST ONE MORE TEST TO FIX!
1 parent f8d9c97 commit eff7a50

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)?;
@@ -2740,7 +2753,7 @@ pub(crate) mod test {
27402753
}
27412754

27422755
#[test]
2743-
#[should_panic(expected = "InsufficientFunds")]
2756+
// #[should_panic(expected = "InsufficientFunds")]
27442757
fn test_create_tx_absolute_high_fee() {
27452758
let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
27462759
let addr = wallet.get_address(New).unwrap();
@@ -2749,7 +2762,10 @@ pub(crate) mod test {
27492762
.drain_to(addr.script_pubkey())
27502763
.drain_wallet()
27512764
.fee_absolute(60_000);
2752-
let (_psbt, _details) = builder.finish().unwrap();
2765+
// let (_psbt, _details) = builder.finish().unwrap();
2766+
assert!(
2767+
matches!(builder.finish(), Err(Error::Generic(s)) if s.contains("insufficient coins"))
2768+
);
27532769
}
27542770

27552771
#[test]
@@ -2786,7 +2802,7 @@ pub(crate) mod test {
27862802
}
27872803

27882804
#[test]
2789-
#[should_panic(expected = "InsufficientFunds")]
2805+
// #[should_panic(expected = "InsufficientFunds")]
27902806
fn test_create_tx_drain_to_dust_amount() {
27912807
let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
27922808
let addr = wallet.get_address(New).unwrap();
@@ -2796,7 +2812,10 @@ pub(crate) mod test {
27962812
.drain_to(addr.script_pubkey())
27972813
.drain_wallet()
27982814
.fee_rate(FeeRate::from_sat_per_vb(455.0));
2799-
builder.finish().unwrap();
2815+
// builder.finish().unwrap();
2816+
assert!(
2817+
matches!(builder.finish(), Err(Error::Generic(s)) if s.contains("insufficient coins"))
2818+
);
28002819
}
28012820

28022821
#[test]
@@ -3042,7 +3061,7 @@ pub(crate) mod test {
30423061
}
30433062

30443063
#[test]
3045-
#[should_panic(expected = "InsufficientFunds")]
3064+
// #[should_panic(expected = "InsufficientFunds")]
30463065
fn test_create_tx_manually_selected_insufficient() {
30473066
let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh());
30483067
let small_output_txid = crate::populate_test_db!(
@@ -3061,7 +3080,10 @@ pub(crate) mod test {
30613080
})
30623081
.unwrap()
30633082
.manually_selected_only();
3064-
builder.finish().unwrap();
3083+
// builder.finish().unwrap();
3084+
assert!(
3085+
matches!(builder.finish(), Err(Error::Generic(s)) if s.contains("insufficient coins"))
3086+
);
30653087
}
30663088

30673089
#[test]
@@ -3780,7 +3802,7 @@ pub(crate) mod test {
37803802
}
37813803

37823804
#[test]
3783-
#[should_panic(expected = "InsufficientFunds")]
3805+
// #[should_panic(expected = "InsufficientFunds")]
37843806
fn test_bump_fee_remove_output_manually_selected_only() {
37853807
let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh());
37863808
// receive an extra tx so that our wallet has two utxos. then we manually pick only one of
@@ -3828,11 +3850,15 @@ pub(crate) mod test {
38283850
builder
38293851
.manually_selected_only()
38303852
.fee_rate(FeeRate::from_sat_per_vb(255.0));
3831-
builder.finish().unwrap();
3853+
// builder.finish().unwrap();
3854+
assert!(
3855+
matches!(builder.finish(), Err(Error::Generic(s)) if s.contains("insufficient coins"))
3856+
);
38323857
}
38333858

38343859
#[test]
38353860
fn test_bump_fee_add_input() {
3861+
// funded wallet already has 50_000 utxo
38363862
let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh());
38373863
crate::populate_test_db!(
38383864
wallet.database.borrow_mut(),
@@ -4254,7 +4280,7 @@ pub(crate) mod test {
42544280
}
42554281

42564282
#[test]
4257-
#[should_panic(expected = "InsufficientFunds")]
4283+
// #[should_panic(expected = "InsufficientFunds")]
42584284
fn test_bump_fee_unconfirmed_inputs_only() {
42594285
// We try to bump the fee, but:
42604286
// - We can't reduce the change, as we have no change
@@ -4296,7 +4322,10 @@ pub(crate) mod test {
42964322

42974323
let mut builder = wallet.build_fee_bump(txid).unwrap();
42984324
builder.fee_rate(FeeRate::from_sat_per_vb(25.0));
4299-
builder.finish().unwrap();
4325+
assert!(
4326+
matches!(builder.finish(), Err(Error::Generic(s)) if s.contains("insufficient coins"))
4327+
);
4328+
// builder.finish().unwrap();
43004329
}
43014330

43024331
#[test]
@@ -5556,25 +5585,33 @@ pub(crate) mod test {
55565585
.add_recipient(addr.script_pubkey(), balance.immature / 2)
55575586
.current_height(confirmation_time);
55585587
assert!(matches!(
5559-
builder.finish().unwrap_err(),
5560-
Error::InsufficientFunds {
5561-
needed: _,
5562-
available: 0
5563-
}
5588+
builder.finish(),
5589+
Err(Error::Generic(s)) if s.contains("insufficient coins")
55645590
));
5591+
// assert!(matches!(
5592+
// builder.finish().unwrap_err(),
5593+
// Error::InsufficientFunds {
5594+
// needed: _,
5595+
// available: 0
5596+
// }
5597+
// ));
55655598

55665599
// Still unspendable...
55675600
let mut builder = wallet.build_tx();
55685601
builder
55695602
.add_recipient(addr.script_pubkey(), balance.immature / 2)
55705603
.current_height(not_yet_mature_time);
55715604
assert!(matches!(
5572-
builder.finish().unwrap_err(),
5573-
Error::InsufficientFunds {
5574-
needed: _,
5575-
available: 0
5576-
}
5605+
builder.finish(),
5606+
Err(Error::Generic(s)) if s.contains("insufficient coins")
55775607
));
5608+
// assert!(matches!(
5609+
// builder.finish().unwrap_err(),
5610+
// Error::InsufficientFunds {
5611+
// needed: _,
5612+
// available: 0
5613+
// }
5614+
// ));
55785615

55795616
// ...Now the coinbase is mature :)
55805617
let sync_time = SyncTime {

0 commit comments

Comments
 (0)