Skip to content

Commit fa32adf

Browse files
author
MarcoFalke
committed
scripted-diff: TxoutType C++11 scoped enum class
-BEGIN VERIFY SCRIPT- # General rename helper: $1 -> $2 rename_global() { sed -i "s/\<$1\>/$2/g" $(git grep -l "$1"); } # Helper to rename TxoutType $1 rename_value() { sed -i "s/ TX_$1,/ $1,/g" src/script/standard.h; # First strip the prefix in the definition (header) rename_global TX_$1 "TxoutType::$1"; # Then replace globally } # Change the type globally to bring it in line with the style-guide # (clsses are UpperCamelCase) rename_global 'enum txnouttype' 'enum class TxoutType' rename_global 'txnouttype' 'TxoutType' # Now rename each enum value rename_value 'NONSTANDARD' rename_value 'PUBKEY' rename_value 'PUBKEYHASH' rename_value 'SCRIPTHASH' rename_value 'MULTISIG' rename_value 'NULL_DATA' rename_value 'WITNESS_V0_KEYHASH' rename_value 'WITNESS_V0_SCRIPTHASH' rename_value 'WITNESS_UNKNOWN' -END VERIFY SCRIPT-
1 parent fa95a69 commit fa32adf

18 files changed

+208
-208
lines changed

src/bloom.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ bool CBloomFilter::IsRelevantAndUpdate(const CTransaction& tx)
135135
else if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_P2PUBKEY_ONLY)
136136
{
137137
std::vector<std::vector<unsigned char> > vSolutions;
138-
txnouttype type = Solver(txout.scriptPubKey, vSolutions);
139-
if (type == TX_PUBKEY || type == TX_MULTISIG) {
138+
TxoutType type = Solver(txout.scriptPubKey, vSolutions);
139+
if (type == TxoutType::PUBKEY || type == TxoutType::MULTISIG) {
140140
insert(COutPoint(hash, i));
141141
}
142142
}

src/core_write.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,27 @@ void ScriptToUniv(const CScript& script, UniValue& out, bool include_address)
140140
out.pushKV("hex", HexStr(script.begin(), script.end()));
141141

142142
std::vector<std::vector<unsigned char>> solns;
143-
txnouttype type = Solver(script, solns);
143+
TxoutType type = Solver(script, solns);
144144
out.pushKV("type", GetTxnOutputType(type));
145145

146146
CTxDestination address;
147-
if (include_address && ExtractDestination(script, address) && type != TX_PUBKEY) {
147+
if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
148148
out.pushKV("address", EncodeDestination(address));
149149
}
150150
}
151151

152152
void ScriptPubKeyToUniv(const CScript& scriptPubKey,
153153
UniValue& out, bool fIncludeHex)
154154
{
155-
txnouttype type;
155+
TxoutType type;
156156
std::vector<CTxDestination> addresses;
157157
int nRequired;
158158

159159
out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
160160
if (fIncludeHex)
161161
out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
162162

163-
if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired) || type == TX_PUBKEY) {
163+
if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired) || type == TxoutType::PUBKEY) {
164164
out.pushKV("type", GetTxnOutputType(type));
165165
return;
166166
}

src/policy/policy.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
5050
return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
5151
}
5252

53-
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType)
53+
bool IsStandard(const CScript& scriptPubKey, TxoutType& whichType)
5454
{
5555
std::vector<std::vector<unsigned char> > vSolutions;
5656
whichType = Solver(scriptPubKey, vSolutions);
5757

58-
if (whichType == TX_NONSTANDARD) {
58+
if (whichType == TxoutType::NONSTANDARD) {
5959
return false;
60-
} else if (whichType == TX_MULTISIG) {
60+
} else if (whichType == TxoutType::MULTISIG) {
6161
unsigned char m = vSolutions.front()[0];
6262
unsigned char n = vSolutions.back()[0];
6363
// Support up to x-of-3 multisig txns as standard
6464
if (n < 1 || n > 3)
6565
return false;
6666
if (m < 1 || m > n)
6767
return false;
68-
} else if (whichType == TX_NULL_DATA &&
68+
} else if (whichType == TxoutType::NULL_DATA &&
6969
(!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes)) {
7070
return false;
7171
}
@@ -110,16 +110,16 @@ bool IsStandardTx(const CTransaction& tx, bool permit_bare_multisig, const CFeeR
110110
}
111111

112112
unsigned int nDataOut = 0;
113-
txnouttype whichType;
113+
TxoutType whichType;
114114
for (const CTxOut& txout : tx.vout) {
115115
if (!::IsStandard(txout.scriptPubKey, whichType)) {
116116
reason = "scriptpubkey";
117117
return false;
118118
}
119119

120-
if (whichType == TX_NULL_DATA)
120+
if (whichType == TxoutType::NULL_DATA)
121121
nDataOut++;
122-
else if ((whichType == TX_MULTISIG) && (!permit_bare_multisig)) {
122+
else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) {
123123
reason = "bare-multisig";
124124
return false;
125125
} else if (IsDust(txout, dust_relay_fee)) {
@@ -163,10 +163,10 @@ bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
163163
const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
164164

165165
std::vector<std::vector<unsigned char> > vSolutions;
166-
txnouttype whichType = Solver(prev.scriptPubKey, vSolutions);
167-
if (whichType == TX_NONSTANDARD) {
166+
TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
167+
if (whichType == TxoutType::NONSTANDARD) {
168168
return false;
169-
} else if (whichType == TX_SCRIPTHASH) {
169+
} else if (whichType == TxoutType::SCRIPTHASH) {
170170
std::vector<std::vector<unsigned char> > stack;
171171
// convert the scriptSig into a stack, so we can inspect the redeemScript
172172
if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))

src/policy/policy.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFee);
8181

8282
bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFee);
8383

84-
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType);
84+
bool IsStandard(const CScript& scriptPubKey, TxoutType& whichType);
8585
/**
8686
* Check for standard transaction types
8787
* @return True if all outputs (scriptPubKeys) use only standard transaction forms

src/rpc/rawtransaction.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,9 @@ static UniValue decoderawtransaction(const JSONRPCRequest& request)
512512
static std::string GetAllOutputTypes()
513513
{
514514
std::vector<std::string> ret;
515-
using U = std::underlying_type<txnouttype>::type;
516-
for (U i = (U)TX_NONSTANDARD; i <= (U)TX_WITNESS_UNKNOWN; ++i) {
517-
ret.emplace_back(GetTxnOutputType(static_cast<txnouttype>(i)));
515+
using U = std::underlying_type<TxoutType>::type;
516+
for (U i = (U)TxoutType::NONSTANDARD; i <= (U)TxoutType::WITNESS_UNKNOWN; ++i) {
517+
ret.emplace_back(GetTxnOutputType(static_cast<TxoutType>(i)));
518518
}
519519
return Join(ret, ", ");
520520
}
@@ -580,10 +580,10 @@ static UniValue decodescript(const JSONRPCRequest& request)
580580
// is a witness program, don't return addresses for a segwit programs.
581581
if (type.get_str() == "pubkey" || type.get_str() == "pubkeyhash" || type.get_str() == "multisig" || type.get_str() == "nonstandard") {
582582
std::vector<std::vector<unsigned char>> solutions_data;
583-
txnouttype which_type = Solver(script, solutions_data);
583+
TxoutType which_type = Solver(script, solutions_data);
584584
// Uncompressed pubkeys cannot be used with segwit checksigs.
585585
// If the script contains an uncompressed pubkey, skip encoding of a segwit program.
586-
if ((which_type == TX_PUBKEY) || (which_type == TX_MULTISIG)) {
586+
if ((which_type == TxoutType::PUBKEY) || (which_type == TxoutType::MULTISIG)) {
587587
for (const auto& solution : solutions_data) {
588588
if ((solution.size() != 1) && !CPubKey(solution).IsCompressed()) {
589589
return r;
@@ -592,9 +592,9 @@ static UniValue decodescript(const JSONRPCRequest& request)
592592
}
593593
UniValue sr(UniValue::VOBJ);
594594
CScript segwitScr;
595-
if (which_type == TX_PUBKEY) {
595+
if (which_type == TxoutType::PUBKEY) {
596596
segwitScr = GetScriptForDestination(WitnessV0KeyHash(Hash160(solutions_data[0].begin(), solutions_data[0].end())));
597-
} else if (which_type == TX_PUBKEYHASH) {
597+
} else if (which_type == TxoutType::PUBKEYHASH) {
598598
segwitScr = GetScriptForDestination(WitnessV0KeyHash(uint160{solutions_data[0]}));
599599
} else {
600600
// Scripts that are not fit for P2WPKH are encoded as P2WSH.

src/script/descriptor.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -985,39 +985,39 @@ std::unique_ptr<PubkeyProvider> InferPubkey(const CPubKey& pubkey, ParseScriptCo
985985
std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptContext ctx, const SigningProvider& provider)
986986
{
987987
std::vector<std::vector<unsigned char>> data;
988-
txnouttype txntype = Solver(script, data);
988+
TxoutType txntype = Solver(script, data);
989989

990-
if (txntype == TX_PUBKEY) {
990+
if (txntype == TxoutType::PUBKEY) {
991991
CPubKey pubkey(data[0].begin(), data[0].end());
992992
if (pubkey.IsValid()) {
993993
return MakeUnique<PKDescriptor>(InferPubkey(pubkey, ctx, provider));
994994
}
995995
}
996-
if (txntype == TX_PUBKEYHASH) {
996+
if (txntype == TxoutType::PUBKEYHASH) {
997997
uint160 hash(data[0]);
998998
CKeyID keyid(hash);
999999
CPubKey pubkey;
10001000
if (provider.GetPubKey(keyid, pubkey)) {
10011001
return MakeUnique<PKHDescriptor>(InferPubkey(pubkey, ctx, provider));
10021002
}
10031003
}
1004-
if (txntype == TX_WITNESS_V0_KEYHASH && ctx != ParseScriptContext::P2WSH) {
1004+
if (txntype == TxoutType::WITNESS_V0_KEYHASH && ctx != ParseScriptContext::P2WSH) {
10051005
uint160 hash(data[0]);
10061006
CKeyID keyid(hash);
10071007
CPubKey pubkey;
10081008
if (provider.GetPubKey(keyid, pubkey)) {
10091009
return MakeUnique<WPKHDescriptor>(InferPubkey(pubkey, ctx, provider));
10101010
}
10111011
}
1012-
if (txntype == TX_MULTISIG) {
1012+
if (txntype == TxoutType::MULTISIG) {
10131013
std::vector<std::unique_ptr<PubkeyProvider>> providers;
10141014
for (size_t i = 1; i + 1 < data.size(); ++i) {
10151015
CPubKey pubkey(data[i].begin(), data[i].end());
10161016
providers.push_back(InferPubkey(pubkey, ctx, provider));
10171017
}
10181018
return MakeUnique<MultisigDescriptor>((int)data[0][0], std::move(providers));
10191019
}
1020-
if (txntype == TX_SCRIPTHASH && ctx == ParseScriptContext::TOP) {
1020+
if (txntype == TxoutType::SCRIPTHASH && ctx == ParseScriptContext::TOP) {
10211021
uint160 hash(data[0]);
10221022
CScriptID scriptid(hash);
10231023
CScript subscript;
@@ -1026,7 +1026,7 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
10261026
if (sub) return MakeUnique<SHDescriptor>(std::move(sub));
10271027
}
10281028
}
1029-
if (txntype == TX_WITNESS_V0_SCRIPTHASH && ctx != ParseScriptContext::P2WSH) {
1029+
if (txntype == TxoutType::WITNESS_V0_SCRIPTHASH && ctx != ParseScriptContext::P2WSH) {
10301030
CScriptID scriptid;
10311031
CRIPEMD160().Write(data[0].data(), data[0].size()).Finalize(scriptid.begin());
10321032
CScript subscript;

src/script/sign.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ static bool CreateSig(const BaseSignatureCreator& creator, SignatureData& sigdat
9292
/**
9393
* Sign scriptPubKey using signature made with creator.
9494
* Signatures are returned in scriptSigRet (or returns false if scriptPubKey can't be signed),
95-
* unless whichTypeRet is TX_SCRIPTHASH, in which case scriptSigRet is the redemption script.
95+
* unless whichTypeRet is TxoutType::SCRIPTHASH, in which case scriptSigRet is the redemption script.
9696
* Returns false if scriptPubKey could not be completely satisfied.
9797
*/
9898
static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey,
99-
std::vector<valtype>& ret, txnouttype& whichTypeRet, SigVersion sigversion, SignatureData& sigdata)
99+
std::vector<valtype>& ret, TxoutType& whichTypeRet, SigVersion sigversion, SignatureData& sigdata)
100100
{
101101
CScript scriptRet;
102102
uint160 h160;
@@ -108,15 +108,15 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
108108

109109
switch (whichTypeRet)
110110
{
111-
case TX_NONSTANDARD:
112-
case TX_NULL_DATA:
113-
case TX_WITNESS_UNKNOWN:
111+
case TxoutType::NONSTANDARD:
112+
case TxoutType::NULL_DATA:
113+
case TxoutType::WITNESS_UNKNOWN:
114114
return false;
115-
case TX_PUBKEY:
115+
case TxoutType::PUBKEY:
116116
if (!CreateSig(creator, sigdata, provider, sig, CPubKey(vSolutions[0]), scriptPubKey, sigversion)) return false;
117117
ret.push_back(std::move(sig));
118118
return true;
119-
case TX_PUBKEYHASH: {
119+
case TxoutType::PUBKEYHASH: {
120120
CKeyID keyID = CKeyID(uint160(vSolutions[0]));
121121
CPubKey pubkey;
122122
if (!GetPubKey(provider, sigdata, keyID, pubkey)) {
@@ -129,7 +129,7 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
129129
ret.push_back(ToByteVector(pubkey));
130130
return true;
131131
}
132-
case TX_SCRIPTHASH:
132+
case TxoutType::SCRIPTHASH:
133133
h160 = uint160(vSolutions[0]);
134134
if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) {
135135
ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
@@ -139,7 +139,7 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
139139
sigdata.missing_redeem_script = h160;
140140
return false;
141141

142-
case TX_MULTISIG: {
142+
case TxoutType::MULTISIG: {
143143
size_t required = vSolutions.front()[0];
144144
ret.push_back(valtype()); // workaround CHECKMULTISIG bug
145145
for (size_t i = 1; i < vSolutions.size() - 1; ++i) {
@@ -159,11 +159,11 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
159159
}
160160
return ok;
161161
}
162-
case TX_WITNESS_V0_KEYHASH:
162+
case TxoutType::WITNESS_V0_KEYHASH:
163163
ret.push_back(vSolutions[0]);
164164
return true;
165165

166-
case TX_WITNESS_V0_SCRIPTHASH:
166+
case TxoutType::WITNESS_V0_SCRIPTHASH:
167167
CRIPEMD160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(h160.begin());
168168
if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) {
169169
ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
@@ -198,44 +198,44 @@ bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreato
198198
if (sigdata.complete) return true;
199199

200200
std::vector<valtype> result;
201-
txnouttype whichType;
201+
TxoutType whichType;
202202
bool solved = SignStep(provider, creator, fromPubKey, result, whichType, SigVersion::BASE, sigdata);
203203
bool P2SH = false;
204204
CScript subscript;
205205
sigdata.scriptWitness.stack.clear();
206206

207-
if (solved && whichType == TX_SCRIPTHASH)
207+
if (solved && whichType == TxoutType::SCRIPTHASH)
208208
{
209209
// Solver returns the subscript that needs to be evaluated;
210210
// the final scriptSig is the signatures from that
211211
// and then the serialized subscript:
212212
subscript = CScript(result[0].begin(), result[0].end());
213213
sigdata.redeem_script = subscript;
214-
solved = solved && SignStep(provider, creator, subscript, result, whichType, SigVersion::BASE, sigdata) && whichType != TX_SCRIPTHASH;
214+
solved = solved && SignStep(provider, creator, subscript, result, whichType, SigVersion::BASE, sigdata) && whichType != TxoutType::SCRIPTHASH;
215215
P2SH = true;
216216
}
217217

218-
if (solved && whichType == TX_WITNESS_V0_KEYHASH)
218+
if (solved && whichType == TxoutType::WITNESS_V0_KEYHASH)
219219
{
220220
CScript witnessscript;
221221
witnessscript << OP_DUP << OP_HASH160 << ToByteVector(result[0]) << OP_EQUALVERIFY << OP_CHECKSIG;
222-
txnouttype subType;
222+
TxoutType subType;
223223
solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata);
224224
sigdata.scriptWitness.stack = result;
225225
sigdata.witness = true;
226226
result.clear();
227227
}
228-
else if (solved && whichType == TX_WITNESS_V0_SCRIPTHASH)
228+
else if (solved && whichType == TxoutType::WITNESS_V0_SCRIPTHASH)
229229
{
230230
CScript witnessscript(result[0].begin(), result[0].end());
231231
sigdata.witness_script = witnessscript;
232-
txnouttype subType;
233-
solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata) && subType != TX_SCRIPTHASH && subType != TX_WITNESS_V0_SCRIPTHASH && subType != TX_WITNESS_V0_KEYHASH;
232+
TxoutType subType;
233+
solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata) && subType != TxoutType::SCRIPTHASH && subType != TxoutType::WITNESS_V0_SCRIPTHASH && subType != TxoutType::WITNESS_V0_KEYHASH;
234234
result.push_back(std::vector<unsigned char>(witnessscript.begin(), witnessscript.end()));
235235
sigdata.scriptWitness.stack = result;
236236
sigdata.witness = true;
237237
result.clear();
238-
} else if (solved && whichType == TX_WITNESS_UNKNOWN) {
238+
} else if (solved && whichType == TxoutType::WITNESS_UNKNOWN) {
239239
sigdata.witness = true;
240240
}
241241

@@ -301,11 +301,11 @@ SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nI
301301

302302
// Get scripts
303303
std::vector<std::vector<unsigned char>> solutions;
304-
txnouttype script_type = Solver(txout.scriptPubKey, solutions);
304+
TxoutType script_type = Solver(txout.scriptPubKey, solutions);
305305
SigVersion sigversion = SigVersion::BASE;
306306
CScript next_script = txout.scriptPubKey;
307307

308-
if (script_type == TX_SCRIPTHASH && !stack.script.empty() && !stack.script.back().empty()) {
308+
if (script_type == TxoutType::SCRIPTHASH && !stack.script.empty() && !stack.script.back().empty()) {
309309
// Get the redeemScript
310310
CScript redeem_script(stack.script.back().begin(), stack.script.back().end());
311311
data.redeem_script = redeem_script;
@@ -315,7 +315,7 @@ SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nI
315315
script_type = Solver(next_script, solutions);
316316
stack.script.pop_back();
317317
}
318-
if (script_type == TX_WITNESS_V0_SCRIPTHASH && !stack.witness.empty() && !stack.witness.back().empty()) {
318+
if (script_type == TxoutType::WITNESS_V0_SCRIPTHASH && !stack.witness.empty() && !stack.witness.back().empty()) {
319319
// Get the witnessScript
320320
CScript witness_script(stack.witness.back().begin(), stack.witness.back().end());
321321
data.witness_script = witness_script;
@@ -328,7 +328,7 @@ SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nI
328328
stack.witness.clear();
329329
sigversion = SigVersion::WITNESS_V0;
330330
}
331-
if (script_type == TX_MULTISIG && !stack.script.empty()) {
331+
if (script_type == TxoutType::MULTISIG && !stack.script.empty()) {
332332
// Build a map of pubkey -> signature by matching sigs to pubkeys:
333333
assert(solutions.size() > 1);
334334
unsigned int num_pubkeys = solutions.size()-2;
@@ -454,13 +454,13 @@ bool IsSegWitOutput(const SigningProvider& provider, const CScript& script)
454454
{
455455
std::vector<valtype> solutions;
456456
auto whichtype = Solver(script, solutions);
457-
if (whichtype == TX_WITNESS_V0_SCRIPTHASH || whichtype == TX_WITNESS_V0_KEYHASH || whichtype == TX_WITNESS_UNKNOWN) return true;
458-
if (whichtype == TX_SCRIPTHASH) {
457+
if (whichtype == TxoutType::WITNESS_V0_SCRIPTHASH || whichtype == TxoutType::WITNESS_V0_KEYHASH || whichtype == TxoutType::WITNESS_UNKNOWN) return true;
458+
if (whichtype == TxoutType::SCRIPTHASH) {
459459
auto h160 = uint160(solutions[0]);
460460
CScript subscript;
461461
if (provider.GetCScript(CScriptID{h160}, subscript)) {
462462
whichtype = Solver(subscript, solutions);
463-
if (whichtype == TX_WITNESS_V0_SCRIPTHASH || whichtype == TX_WITNESS_V0_KEYHASH || whichtype == TX_WITNESS_UNKNOWN) return true;
463+
if (whichtype == TxoutType::WITNESS_V0_SCRIPTHASH || whichtype == TxoutType::WITNESS_V0_KEYHASH || whichtype == TxoutType::WITNESS_UNKNOWN) return true;
464464
}
465465
}
466466
return false;

0 commit comments

Comments
 (0)