refactor: add UNKNOWN OutputType

add to enum, array and handle UNKNOWN in various case statements
This commit is contained in:
josibake committed 2022-08-10 10:17:54 +02:00
1 parent ac59112a6a
commit f5649db9d5
4 files changed
+16 -2

No files matched your search

+8 -2
View File
@@ -20,6 +20,7 @@ static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy";
static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit"; static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit";
static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32"; static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
static const std::string OUTPUT_TYPE_STRING_BECH32M = "bech32m"; static const std::string OUTPUT_TYPE_STRING_BECH32M = "bech32m";
static const std::string OUTPUT_TYPE_STRING_UNKNOWN = "unknown";
std::optional<OutputType> ParseOutputType(const std::string& type) std::optional<OutputType> ParseOutputType(const std::string& type)
{ {
@@ -31,6 +32,8 @@ std::optional<OutputType> ParseOutputType(const std::string& type)
return OutputType::BECH32; return OutputType::BECH32;
} else if (type == OUTPUT_TYPE_STRING_BECH32M) { } else if (type == OUTPUT_TYPE_STRING_BECH32M) {
return OutputType::BECH32M; return OutputType::BECH32M;
} else if (type == OUTPUT_TYPE_STRING_UNKNOWN) {
return OutputType::UNKNOWN;
} }
return std::nullopt; return std::nullopt;
} }
@@ -42,6 +45,7 @@ const std::string& FormatOutputType(OutputType type)
case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT; case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT;
case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32; case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32;
case OutputType::BECH32M: return OUTPUT_TYPE_STRING_BECH32M; case OutputType::BECH32M: return OUTPUT_TYPE_STRING_BECH32M;
case OutputType::UNKNOWN: return OUTPUT_TYPE_STRING_UNKNOWN;
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
assert(false); assert(false);
} }
@@ -61,7 +65,8 @@ CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
return witdest; return witdest;
} }
} }
case OutputType::BECH32M: {} // This function should never be used with BECH32M, so let it assert case OutputType::BECH32M:
case OutputType::UNKNOWN: {} // This function should never be used with BECH32M or UNKNOWN, so let it assert
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
assert(false); assert(false);
} }
@@ -101,7 +106,8 @@ CTxDestination AddAndGetDestinationForScript(FillableSigningProvider& keystore,
return ScriptHash(witprog); return ScriptHash(witprog);
} }
} }
case OutputType::BECH32M: {} // This function should not be used for BECH32M, so let it assert case OutputType::BECH32M:
case OutputType::UNKNOWN: {} // This function should not be used for BECH32M or UNKNOWN, so let it assert
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
assert(false); assert(false);
} }
+2
View File
@@ -19,6 +19,7 @@ enum class OutputType {
P2SH_SEGWIT, P2SH_SEGWIT,
BECH32, BECH32,
BECH32M, BECH32M,
UNKNOWN,
}; };
static constexpr auto OUTPUT_TYPES = std::array{ static constexpr auto OUTPUT_TYPES = std::array{
@@ -26,6 +27,7 @@ static constexpr auto OUTPUT_TYPES = std::array{
OutputType::P2SH_SEGWIT, OutputType::P2SH_SEGWIT,
OutputType::BECH32, OutputType::BECH32,
OutputType::BECH32M, OutputType::BECH32M,
OutputType::UNKNOWN,
}; };
std::optional<OutputType> ParseOutputType(const std::string& str); std::optional<OutputType> ParseOutputType(const std::string& str);
+5
View File
@@ -1966,6 +1966,11 @@ bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(const CExtKey& master_
desc_prefix = "tr(" + xpub + "/86'"; desc_prefix = "tr(" + xpub + "/86'";
break; break;
} }
case OutputType::UNKNOWN: {
// We should never have a DescriptorScriptPubKeyMan for an UNKNOWN OutputType,
// so if we get to this point something is wrong
assert(false);
}
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
assert(!desc_prefix.empty()); assert(!desc_prefix.empty());
+1
View File
@@ -3421,6 +3421,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
for (bool internal : {false, true}) { for (bool internal : {false, true}) {
for (OutputType t : OUTPUT_TYPES) { for (OutputType t : OUTPUT_TYPES) {
if (t == OutputType::UNKNOWN) continue;
auto spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this)); auto spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this));
if (IsCrypted()) { if (IsCrypted()) {
if (IsLocked()) { if (IsLocked()) {