@@ -654,25 +654,22 @@ public void completeTx(SendRequest req) throws InsufficientMoneyException {
654654 if (req .shuffleOutputs )
655655 req .tx .shuffleOutputs ();
656656
657- // Now sign the inputs, thus proving that we are entitled to redeem the connected outputs.
657+ // Now sign the legacy inputs, thus proving that we are entitled to redeem the connected outputs.
658658 if (req .signInputs )
659- signTransaction (req );
659+ signLegacyTransaction (req );
660660
661661 // Check virtual size.
662662 int baseSize = req .tx .unsafeBitcoinSerialize ().length ;
663- if (req .isSegwitCompatible ) {
664- // in segwit-compatible, the scriptSig is a 36-bytes-fixed-size hash,
665- // so we should count its bytes manually.
666- int segwitCompatibleScriptSigSize = 36 ;
667- baseSize += req .tx .getInputs ().size () * segwitCompatibleScriptSigSize ;
668- }
669663 int totalSize = baseSize ;
664+ // if the tx was signed, there's nothing else to consider
670665 if (!req .signInputs ) {
671- if (! req .isSegwitCompatible ) {
672- baseSize += estimateBytesForSigning ( bestCoinSelection );
666+ if (req .isSegwitCompatible ) {
667+ baseSize += calculateSegwitScriptSigSize ( req . tx );
673668 totalSize = baseSize ;
674- } else {
675669 totalSize += estimateBytesForSigning (bestCoinSelection );
670+ } else {
671+ baseSize += estimateBytesForSigning (bestCoinSelection );
672+ totalSize = baseSize ;
676673 }
677674 }
678675
@@ -693,12 +690,12 @@ public void completeTx(SendRequest req) throws InsufficientMoneyException {
693690 }
694691
695692 /**
696- * <p>Given a send request containing transaction, attempts to sign it's inputs. This method expects transaction
693+ * <p>Given a send request containing a legacy transaction, attempts to sign it's inputs. This method expects transaction
697694 * to have all necessary inputs connected or they will be ignored.</p>
698695 * <p>Actual signing is done by pluggable {@link #signers} and it's not guaranteed that
699696 * transaction will be complete in the end.</p>
700697 */
701- public void signTransaction (SendRequest req ) {
698+ public void signLegacyTransaction (SendRequest req ) {
702699 try {
703700 BtcTransaction tx = req .tx ;
704701 List <TransactionInput > inputs = tx .getInputs ();
@@ -748,7 +745,7 @@ private boolean adjustOutputDownwardsForFee(
748745 Coin feePerKb ,
749746 boolean ensureMinRequiredFee
750747 ) {
751- final int size = calculateTxSizeBeforeSigning (tx , isSegwit , coinSelection );
748+ final int size = calculateTxSizeForFees (tx , isSegwit , coinSelection );
752749 Coin fee = feePerKb .multiply (size ).divide (1000 );
753750 if (ensureMinRequiredFee && fee .compareTo (BtcTransaction .REFERENCE_DEFAULT_MIN_TX_FEE ) < 0 )
754751 fee = BtcTransaction .REFERENCE_DEFAULT_MIN_TX_FEE ;
@@ -1024,7 +1021,7 @@ private FeeCalculation calculateFee(
10241021 checkState (input .getScriptBytes ().length == 0 );
10251022 }
10261023
1027- int size = calculateTxSizeBeforeSigning (tx , req .isSegwitCompatible , selection );
1024+ int size = calculateTxSizeForFees (tx , req .isSegwitCompatible , selection );
10281025
10291026 Coin feePerKb = req .feePerKb ;
10301027 if (needAtLeastReferenceFee && feePerKb .compareTo (BtcTransaction .REFERENCE_DEFAULT_MIN_TX_FEE ) < 0 ) {
@@ -1043,26 +1040,51 @@ private FeeCalculation calculateFee(
10431040 return result ;
10441041 }
10451042
1046- private int calculateTxSizeBeforeSigning (BtcTransaction tx , boolean isSegwitCompatible , CoinSelection bestCoinSelection ) {
1043+ /**
1044+ * Calculates the virtual size of a Bitcoin transaction for fee estimation purposes.
1045+ * When estimating fees, we assume the transaction is not yet signed, so we need to consider
1046+ * the expected size of the signatures.
1047+ * - If the transaction is legacy (non-SegWit), signature data is part of the base size,
1048+ * and the total size is equal to the base size.
1049+ * - If the transaction is SegWit-compatible, the scriptSig is a 36-byte fixed-size hash
1050+ * located in the input, so its part of the base size. Signatures are located in the
1051+ * witness, so they are just part of the total size.
1052+ * @param tx the unsigned Bitcoin transaction
1053+ * @param isSegwitCompatible whether the transaction is SegWit-compatible
1054+ * @param bestCoinSelection the selected UTXOs for the transaction
1055+ * @return the estimated virtual size of the transaction
1056+ */
1057+ private int calculateTxSizeForFees (BtcTransaction tx , boolean isSegwitCompatible , CoinSelection bestCoinSelection ) {
10471058 int baseSize = tx .unsafeBitcoinSerialize ().length ;
1048- if (isSegwitCompatible ) {
1049- // in segwit-compatible, the scriptSig is a 36-bytes-fixed-size hash,
1050- // so we should count its bytes manually.
1051- int segwitCompatibleScriptSigSize = 36 ;
1052- baseSize += tx .getInputs ().size () * segwitCompatibleScriptSigSize ;
1053- }
1054- int totalSize = baseSize ;
1059+ int totalSize ;
10551060 if (!isSegwitCompatible ) {
10561061 baseSize += estimateBytesForSigning (bestCoinSelection );
10571062 totalSize = baseSize ;
10581063 } else {
1064+ baseSize += calculateSegwitScriptSigSize (tx );
1065+ totalSize = baseSize ;
10591066 totalSize += estimateBytesForSigning (bestCoinSelection );
10601067 }
10611068 return calculateVirtualSize (baseSize , totalSize );
10621069 }
10631070
1071+ private int calculateSegwitScriptSigSize (BtcTransaction tx ) {
1072+ int segwitCompatibleScriptSigSize = 36 ;
1073+ return tx .getInputs ().size () * segwitCompatibleScriptSigSize ;
1074+ }
1075+
1076+ /**
1077+ * Calculates the virtual size of a Bitcoin transaction as defined in BIP141.
1078+ * The virtual size is a weighted size used to properly account for the discount SegWit
1079+ * provides on witness data.
1080+ * - For legacy transactions, {@code baseSize == totalSize}, so the virtual size equals both.
1081+ * - For SegWit transactions, {@code baseSize} excludes witness data, and {@code totalSize} includes it.
1082+ *
1083+ * @param baseSize the size of the transaction excluding witness data
1084+ * @param totalSize the full size of the transaction including witness data
1085+ * @return the virtual size in vbytes
1086+ */
10641087 private int calculateVirtualSize (int baseSize , int totalSize ) {
1065- // As described in BIP141
10661088 int txWeight = totalSize + (3 * baseSize );
10671089 return txWeight / 4 ;
10681090 }
0 commit comments