Script: take into account the various witness length fields in getNumberOfBytesRequiredToSpend()

This (hopefully entirely) fixes the slight fee underspending for spends from P2WPKH.
This commit is contained in:
Andreas Schildbach 2021-11-16 23:10:50 +01:00
parent 26cc0db3e2
commit 4ea8546cd3
2 changed files with 13 additions and 2 deletions

View File

@ -606,7 +606,12 @@ public class Script {
// scriptSig is empty
// witness: <sig> <pubKey>
int compressedPubKeySize = 33;
return SIG_SIZE + (pubKey != null ? pubKey.getPubKey().length : compressedPubKeySize);
int publicKeyLength = pubKey != null ? pubKey.getPubKey().length : compressedPubKeySize;
return VarInt.sizeOf(2) // number of witness pushes
+ VarInt.sizeOf(SIG_SIZE) // size of signature push
+ SIG_SIZE // signature push
+ VarInt.sizeOf(publicKeyLength) // size of pubKey push
+ publicKeyLength; // pubKey push
} else {
throw new IllegalStateException("Unsupported script type");
}

View File

@ -2696,7 +2696,13 @@ public class WalletTest extends TestWithWallet {
SendRequest request = SendRequest.to(OTHER_SEGWIT_ADDRESS, CENT);
request.feePerKb = Transaction.DEFAULT_TX_FEE;
mySegwitWallet.completeTx(request);
assertEquals(Coin.valueOf(14000), request.tx.getFee());
// Fee test, absolute and per virtual kilobyte
Coin fee = request.tx.getFee();
int vsize = request.tx.getVsize();
Coin feePerVkb = fee.multiply(1000).divide(vsize);
assertEquals(Coin.valueOf(14100), fee);
assertEquals(Transaction.DEFAULT_TX_FEE, feePerVkb);
}
@Test