Branch data Line data Source code
1 : : // Copyright (c) 2019-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <coins.h>
6 : : #include <consensus/validation.h>
7 : : #include <script/signingprovider.h>
8 : : #include <test/util/transaction_utils.h>
9 : :
10 : 0 : CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey, CAmount nValue)
11 : : {
12 : 0 : CMutableTransaction txCredit;
13 : 0 : txCredit.version = 1;
14 : 0 : txCredit.nLockTime = 0;
15 : 0 : txCredit.vin = {
16 [ # # # # ]: 0 : CTxIn{COutPoint{}, CScript{} << CScriptNum(0) << CScriptNum(0), CTxIn::SEQUENCE_FINAL},
17 [ # # # # : 0 : };
# # ]
18 : 0 : txCredit.vout = {
19 : 0 : CTxOut{nValue, scriptPubKey},
20 [ # # # # : 0 : };
# # ]
21 : :
22 : 0 : return txCredit;
23 [ # # # # : 0 : }
# # ]
24 : :
25 : 0 : CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CScriptWitness& scriptWitness, const CTransaction& txCredit)
26 : : {
27 : 0 : CMutableTransaction txSpend;
28 : 0 : txSpend.version = 1;
29 : 0 : txSpend.nLockTime = 0;
30 : 0 : txSpend.vin = {
31 : 0 : CTxIn{COutPoint{txCredit.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL},
32 [ # # # # : 0 : };
# # ]
33 [ # # ]: 0 : txSpend.vin[0].scriptWitness = scriptWitness;
34 : 0 : txSpend.vout = {
35 [ # # ]: 0 : CTxOut{txCredit.vout[0].nValue, CScript{}},
36 [ # # # # : 0 : };
# # ]
37 : :
38 : 0 : return txSpend;
39 [ # # # # ]: 0 : }
40 : :
41 : 0 : std::vector<CMutableTransaction> SetupDummyInputs(FillableSigningProvider& keystoreRet, CCoinsViewCache& coinsRet, const std::array<CAmount,4>& nValues)
42 : : {
43 : 0 : std::vector<CMutableTransaction> dummyTransactions;
44 [ # # ]: 0 : dummyTransactions.resize(2);
45 : :
46 : : // Add some keys to the keystore:
47 : 0 : CKey key[4];
48 [ # # ]: 0 : for (int i = 0; i < 4; i++) {
49 [ # # ]: 0 : key[i].MakeNewKey(i % 2);
50 [ # # ]: 0 : keystoreRet.AddKey(key[i]);
51 : : }
52 : :
53 : : // Create some dummy input transactions
54 [ # # ]: 0 : dummyTransactions[0].vout = {
55 [ # # # # : 0 : CTxOut{nValues[0], CScript{} << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG},
# # ]
56 [ # # # # : 0 : CTxOut{nValues[1], CScript{} << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG},
# # ]
57 [ # # # # : 0 : };
# # ]
58 [ # # # # ]: 0 : AddCoins(coinsRet, CTransaction(dummyTransactions[0]), 0);
59 : :
60 [ # # ]: 0 : dummyTransactions[1].vout = {
61 [ # # # # : 0 : CTxOut{nValues[2], GetScriptForDestination(PKHash(key[2].GetPubKey()))},
# # ]
62 [ # # # # : 0 : CTxOut{nValues[3], GetScriptForDestination(PKHash(key[3].GetPubKey()))},
# # ]
63 [ # # # # : 0 : };
# # ]
64 [ # # # # ]: 0 : AddCoins(coinsRet, CTransaction(dummyTransactions[1]), 0);
65 : :
66 : 0 : return dummyTransactions;
67 : 0 : }
[ # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
68 : :
69 : 65 : void BulkTransaction(CMutableTransaction& tx, int32_t target_weight)
70 : : {
71 [ + - + - ]: 65 : tx.vout.emplace_back(0, CScript() << OP_RETURN);
72 : 65 : auto unpadded_weight{GetTransactionWeight(CTransaction(tx))};
73 [ - + ]: 65 : assert(target_weight >= unpadded_weight);
74 : :
75 : : // determine number of needed padding bytes by converting weight difference to vbytes
76 : 65 : auto dummy_vbytes = (target_weight - unpadded_weight + (WITNESS_SCALE_FACTOR - 1)) / WITNESS_SCALE_FACTOR;
77 : : // compensate for the increase of the compact-size encoded script length
78 : : // (note that the length encoding of the unpadded output script needs one byte)
79 [ + - ]: 65 : dummy_vbytes -= GetSizeOfCompactSize(dummy_vbytes) - 1;
80 : :
81 : : // pad transaction by repeatedly appending a dummy opcode to the output script
82 : 65 : tx.vout[0].scriptPubKey.insert(tx.vout[0].scriptPubKey.end(), dummy_vbytes, OP_1);
83 : :
84 : : // actual weight should be at most 3 higher than target weight
85 [ - + ]: 65 : assert(GetTransactionWeight(CTransaction(tx)) >= target_weight);
86 [ - + ]: 65 : assert(GetTransactionWeight(CTransaction(tx)) <= target_weight + 3);
87 : 65 : }
88 : :
89 : 745 : bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType, SignatureData& sig_data)
90 : : {
91 [ - + - + ]: 745 : assert(nIn < txTo.vin.size());
92 : :
93 : 745 : MutableTransactionSignatureCreator creator(txTo, nIn, amount, {.sighash_type = nHashType});
94 : :
95 [ + - ]: 745 : bool ret = ProduceSignature(provider, creator, fromPubKey, sig_data);
96 [ + - + - ]: 745 : UpdateInput(txTo.vin.at(nIn), sig_data);
97 : 745 : return ret;
98 : 745 : }
99 : :
100 : 19 : bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType, SignatureData& sig_data)
101 : : {
102 [ - + - + ]: 19 : assert(nIn < txTo.vin.size());
103 [ - + ]: 19 : const CTxIn& txin = txTo.vin[nIn];
104 [ - + - + ]: 19 : assert(txin.prevout.n < txFrom.vout.size());
105 : 19 : const CTxOut& txout = txFrom.vout[txin.prevout.n];
106 : :
107 : 19 : return SignSignature(provider, txout.scriptPubKey, txTo, nIn, txout.nValue, nHashType, sig_data);
108 : : }
|