Skip to content

Commit e230887

Browse files
committed
refactor: Use ChainType enum exhaustively
This is a follow up of bitcoin#27491, more concretely bitcoin#27491 (comment), for not using default cases (as per the style guide), and bitcoin#27491 (comment) and bitcoin#27491 (comment) for avoiding dead code. Also change chain name to chain type in docstrings
1 parent fc06881 commit e230887

File tree

7 files changed

+9
-13
lines changed

7 files changed

+9
-13
lines changed

src/bitcoin-cli.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,10 @@ class NetinfoRequestHandler : public BaseRequestHandler
434434
return " signet";
435435
case ChainType::REGTEST:
436436
return " regtest";
437-
default:
437+
case ChainType::MAIN:
438438
return "";
439439
}
440+
assert(false);
440441
}
441442
std::string PingTimeToString(double seconds) const
442443
{

src/chainparams.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, c
117117
return CChainParams::RegTest(opts);
118118
}
119119
}
120-
throw std::invalid_argument(strprintf("%s: Invalid ChainType value", __func__));
120+
assert(false);
121121
}
122122

123123
void SelectParams(const ChainType chain)

src/chainparams.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class ArgsManager;
2525

2626
/**
2727
* Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
28-
* @returns a CChainParams* of the chosen chain.
29-
* @throws a std::runtime_error if the chain is not supported.
3028
*/
3129
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain);
3230

@@ -37,8 +35,7 @@ std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, c
3735
const CChainParams &Params();
3836

3937
/**
40-
* Sets the params returned by Params() to those for the given chain name.
41-
* @throws std::runtime_error when the chain is not supported.
38+
* Sets the params returned by Params() to those for the given chain type.
4239
*/
4340
void SelectParams(const ChainType chain);
4441

src/chainparamsbase.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const ChainType chain)
4848
case ChainType::REGTEST:
4949
return std::make_unique<CBaseChainParams>("regtest", 18443, 18445);
5050
}
51-
throw std::invalid_argument(strprintf("%s: Invalid ChainType value", __func__));
51+
assert(false);
5252
}
5353

5454
void SelectBaseParams(const ChainType chain)

src/chainparamsbase.h

-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class CBaseChainParams
3535

3636
/**
3737
* Creates and returns a std::unique_ptr<CBaseChainParams> of the chosen chain.
38-
* @returns a CBaseChainParams* of the chosen chain.
39-
* @throws a std::runtime_error if the chain is not supported.
4038
*/
4139
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const ChainType chain);
4240

src/common/args.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,14 @@ class ArgsManager
325325
void ForceSetArg(const std::string& strArg, const std::string& strValue);
326326

327327
/**
328-
* Returns the appropriate chain name from the program arguments.
328+
* Returns the appropriate chain type from the program arguments.
329329
* @return ChainType::MAIN by default; raises runtime error if an invalid
330330
* combination, or unknown chain is given.
331331
*/
332332
ChainType GetChainType() const;
333333

334334
/**
335-
* Returns the appropriate chain name string from the program arguments.
335+
* Returns the appropriate chain type string from the program arguments.
336336
* @return ChainType::MAIN string by default; raises runtime error if an
337337
* invalid combination is given.
338338
*/
@@ -423,7 +423,7 @@ class ArgsManager
423423

424424
/**
425425
* Return -regtest/-signet/-testnet/-chain= setting as a ChainType enum if a
426-
* recognized chain name was set, or as a string if an unrecognized chain
426+
* recognized chain type was set, or as a string if an unrecognized chain
427427
* name was set. Raise an exception if an invalid combination of flags was
428428
* provided.
429429
*/

src/test/argsman_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ BOOST_AUTO_TEST_CASE(util_ParseInvalidParameters)
255255
BOOST_CHECK(!test.ParseParameters(2, (char**)argv, error));
256256
BOOST_CHECK_EQUAL(error, "Invalid parameter -unregistered");
257257

258-
// Make sure registered parameters prefixed with a chain name trigger errors.
258+
// Make sure registered parameters prefixed with a chain type trigger errors.
259259
// (Previously, they were accepted and ignored.)
260260
argv[1] = "-test.registered";
261261
BOOST_CHECK(!test.ParseParameters(2, (char**)argv, error));

0 commit comments

Comments
 (0)