]> Untitled Git - bdk/commitdiff
Take into account the segwit tx header when...
authorDaniela Brozzoni <danielabrozzoni@protonmail.com>
Tue, 12 Jul 2022 13:51:27 +0000 (15:51 +0200)
committerDaniela Brozzoni <danielabrozzoni@protonmail.com>
Tue, 2 Aug 2022 10:08:53 +0000 (12:08 +0200)
...selecting coins

We take into account the larger segwit tx header for every
transaction, not just the segwit ones. The reason for this is that
we prefer to overestimate the fees for the transaction than
underestimating them - the former might create txs with a slightly
higher feerate than the requested one, while the latter might
create txs with a slightly lower one - or worse, invalid (<1 sat/vbyte)!

src/wallet/mod.rs

index dccc427db86102683c7032571f66445ebdb9e2f2..a56fdadfc540bb08daa6b881627c83af78659a2c 100644 (file)
@@ -759,6 +759,17 @@ where
 
         fee_amount += fee_rate.fee_wu(tx.weight());
 
+        // Segwit transactions' header is 2WU larger than legacy txs' header,
+        // as they contain a witness marker (1WU) and a witness flag (1WU) (see BIP144).
+        // At this point we really don't know if the resulting transaction will be segwit
+        // or legacy, so we just add this 2WU to the fee_amount - overshooting the fee amount
+        // is better than undershooting it.
+        // If we pass a fee_amount that is slightly higher than the final fee_amount, we
+        // end up with a transaction with a slightly higher fee rate than the requested one.
+        // If, instead, we undershoot, we may end up with a feerate lower than the requested one
+        // - we might come up with non broadcastable txs!
+        fee_amount += fee_rate.fee_wu(2);
+
         if params.change_policy != tx_builder::ChangeSpendPolicy::ChangeAllowed
             && self.change_descriptor.is_none()
         {