Skip to content

Commit

Permalink
Merge bitcoin#20211: Use -Wswitch for TxoutType where possible
Browse files Browse the repository at this point in the history
fa650ca Use -Wswitch for TxoutType where possible (MarcoFalke)
fa59e0b test: Add missing script_standard_Solver_success cases (MarcoFalke)

Pull request description:

  This removes unused `default:` cases for all `switch` statements on `TxoutType` and adds the cases (`MULTISIG`, `NULL_DATA`, `NONSTANDARD`) to `ExtractDestination` for clarity.

  Also, the compiler is now able to use `-Wswitch`.

ACKs for top commit:
  practicalswift:
    cr ACK fa650ca: patch looks correct and `assert(false);` is better than UB :)
  hebasto:
    ACK fa650ca, I have reviewed the code and it looks OK, I agree it can be merged.

Tree-SHA512: 282458b6523bd8923a0c0f5c423d1db2dce2a2d1b1d1dae455415c6fc995bb41ce82c1f9b0a1c0dcc6d874d171e04c30eca585f147582f52c7048c140358630a
  • Loading branch information
MarcoFalke authored and vijaydasmp committed Jan 19, 2024
1 parent a50ce91 commit b43adbd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
9 changes: 3 additions & 6 deletions src/script/sign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
std::vector<valtype> vSolutions;
whichTypeRet = Solver(scriptPubKey, vSolutions);

switch (whichTypeRet)
{
switch (whichTypeRet) {
case TxoutType::NONSTANDARD:
case TxoutType::NULL_DATA:
return false;
Expand Down Expand Up @@ -151,10 +150,8 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
}
return ok;
}

default:
return false;
}
} // no default case, so the compiler can warn about missing cases
assert(false);
}

static CScript PushAll(const std::vector<valtype>& values)
Expand Down
21 changes: 12 additions & 9 deletions src/script/standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ CKeyID ToKeyID(const PKHash& key_hash)

const char* GetTxnOutputType(TxoutType t)
{
switch (t)
{
switch (t) {
case TxoutType::NONSTANDARD: return "nonstandard";
case TxoutType::PUBKEY: return "pubkey";
case TxoutType::PUBKEYHASH: return "pubkeyhash";
Expand Down Expand Up @@ -138,26 +137,30 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
std::vector<valtype> vSolutions;
TxoutType whichType = Solver(scriptPubKey, vSolutions);

if (whichType == TxoutType::PUBKEY) {
switch (whichType) {
case TxoutType::PUBKEY: {
CPubKey pubKey(vSolutions[0]);
if (!pubKey.IsValid())
return false;

addressRet = PKHash(pubKey);
return true;
}
else if (whichType == TxoutType::PUBKEYHASH)
{
case TxoutType::PUBKEYHASH: {
addressRet = PKHash(uint160(vSolutions[0]));
return true;
}
else if (whichType == TxoutType::SCRIPTHASH)
{
case TxoutType::SCRIPTHASH: {
addressRet = ScriptHash(uint160(vSolutions[0]));
return true;
}
// Multisig txns have more than one address...
return false;
case TxoutType::MULTISIG:
// Multisig txns have more than one address...
case TxoutType::NULL_DATA:
case TxoutType::NONSTANDARD:
return false;
} // no default case, so the compiler can warn about missing cases
assert(false);
}

bool ExtractDestinations(const CScript& scriptPubKey, TxoutType& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,9 +1109,9 @@ static std::string RecurseImportData(const CScript& script, ImportData& import_d
case TxoutType::NULL_DATA:
return "unspendable script";
case TxoutType::NONSTANDARD:
default:
return "unrecognized script";
}
} // no default case, so the compiler can warn about missing cases
CHECK_NONFATAL(false);
}

static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys)
Expand Down
5 changes: 2 additions & 3 deletions src/wallet/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ IsMineResult IsMineInner(const LegacyScriptPubKeyMan& keystore, const CScript& s
TxoutType whichType = Solver(scriptPubKey, vSolutions);

CKeyID keyID;
switch (whichType)
{
switch (whichType) {
case TxoutType::NONSTANDARD:
case TxoutType::NULL_DATA:
break;
Expand Down Expand Up @@ -154,7 +153,7 @@ IsMineResult IsMineInner(const LegacyScriptPubKeyMan& keystore, const CScript& s
}
break;
}
}
} // no default case, so the compiler can warn about missing cases

if (ret == IsMineResult::NO && keystore.HaveWatchOnly(scriptPubKey)) {
ret = std::max(ret, IsMineResult::WATCH_ONLY);
Expand Down

0 comments on commit b43adbd

Please sign in to comment.