0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-14 11:26:09 -05:00

test: add BulkTransaction helper to unit test transaction utils

The padding method used matches the one used in MiniWallet,
`MiniWallet._bulk_tx`.
This commit is contained in:
Sebastian Falbesoner 2024-09-03 21:55:02 +02:00
parent d4b5553849
commit ed7d224666
2 changed files with 25 additions and 0 deletions

View file

@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <coins.h> #include <coins.h>
#include <consensus/validation.h>
#include <script/signingprovider.h> #include <script/signingprovider.h>
#include <test/util/transaction_utils.h> #include <test/util/transaction_utils.h>
@ -69,3 +70,23 @@ std::vector<CMutableTransaction> SetupDummyInputs(FillableSigningProvider& keyst
return dummyTransactions; return dummyTransactions;
} }
void BulkTransaction(CMutableTransaction& tx, int32_t target_weight)
{
tx.vout.emplace_back(0, CScript() << OP_RETURN);
auto unpadded_weight{GetTransactionWeight(CTransaction(tx))};
assert(target_weight >= unpadded_weight);
// determine number of needed padding bytes by converting weight difference to vbytes
auto dummy_vbytes = (target_weight - unpadded_weight + (WITNESS_SCALE_FACTOR - 1)) / WITNESS_SCALE_FACTOR;
// compensate for the increase of the compact-size encoded script length
// (note that the length encoding of the unpadded output script needs one byte)
dummy_vbytes -= GetSizeOfCompactSize(dummy_vbytes) - 1;
// pad transaction by repeatedly appending a dummy opcode to the output script
tx.vout[0].scriptPubKey.insert(tx.vout[0].scriptPubKey.end(), dummy_vbytes, OP_1);
// actual weight should be at most 3 higher than target weight
assert(GetTransactionWeight(CTransaction(tx)) >= target_weight);
assert(GetTransactionWeight(CTransaction(tx)) <= target_weight + 3);
}

View file

@ -26,4 +26,8 @@ CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CSc
// the second nValues[2] and nValues[3] outputs paid to a TxoutType::PUBKEYHASH. // the second nValues[2] and nValues[3] outputs paid to a TxoutType::PUBKEYHASH.
std::vector<CMutableTransaction> SetupDummyInputs(FillableSigningProvider& keystoreRet, CCoinsViewCache& coinsRet, const std::array<CAmount,4>& nValues); std::vector<CMutableTransaction> SetupDummyInputs(FillableSigningProvider& keystoreRet, CCoinsViewCache& coinsRet, const std::array<CAmount,4>& nValues);
// bulk transaction to reach a certain target weight,
// by appending a single output with padded output script
void BulkTransaction(CMutableTransaction& tx, int32_t target_weight);
#endif // BITCOIN_TEST_UTIL_TRANSACTION_UTILS_H #endif // BITCOIN_TEST_UTIL_TRANSACTION_UTILS_H