-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Refactor: One CBaseChainParams should be enough #12128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/chainparamsbase.h
Outdated
@@ -24,9 +26,12 @@ class CBaseChainParams | |||
const std::string& DataDir() const { return strDataDir; } | |||
int RPCPort() const { return nRPCPort; } | |||
|
|||
protected: | |||
CBaseChainParams() {} | |||
CBaseChainParams(const std::string& strDataDir, int nRPCPort) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
codestyle: just use data_dir and rpc_port.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and then nRPCPort(rpc_port), strDataDir(data_dir) {}
constructor initializer list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK.
src/chainparamsbase.h
Outdated
|
||
protected: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that subclasses are removed, these can be private I think.
src/chainparamsbase.h
Outdated
@@ -15,6 +15,8 @@ | |||
*/ | |||
class CBaseChainParams | |||
{ | |||
/** Default constructor is private to force the use of the parametrized one */ | |||
CBaseChainParams() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use C++11 deleted function syntax? CBaseChainParams() = delete
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jtimon ^
src/chainparamsbase.cpp
Outdated
@@ -73,11 +35,11 @@ const CBaseChainParams& BaseParams() | |||
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain) | |||
{ | |||
if (chain == CBaseChainParams::MAIN) | |||
return std::unique_ptr<CBaseChainParams>(new CBaseMainParams()); | |||
return std::unique_ptr<CBaseChainParams>(new CBaseChainParams("", 8332)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Could use MakeUnique<CBaseChainParams>("", 8332)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried, but it seems it's a C++14 feature, not C++11. :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c++14 provides make_unique, but MakeUnique is just a function in util:
Lines 336 to 338 in 0910cbe
//! Substitute for C++14 std::make_unique. | |
template <typename T, typename... Args> | |
std::unique_ptr<T> MakeUnique(Args&&... args) |
a26c919
to
9b69179
Compare
Fixed nits |
utACK after #12128 (comment) is addressed. |
9b69179
to
bbd0d1c
Compare
Fixed last nit (using util function MakeUnique). |
bbd0d1c
to
277b4a9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 277b4a994c83f131024ba8e24e3dc247078a0c82 but I agree with @jimpo's nit that the default constructor should just be deleted. (Could delete the copy constructors too, but that doesn't achieve a lot)
src/chainparamsbase.h
Outdated
int nRPCPort; | ||
std::string strDataDir; | ||
|
||
/** Default constructor is private to force the use of the parametrized one */ | ||
CBaseChainParams() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having a non-default constructor already prevents automatic declaration of the default constructor; but "CBaseChainParams() = delete;" seems clearer still.
Also, since nRPCPort and strDataDir are set via initializer list, and aren't fiddled with later (they're private and all the member functions are marked const), they could be marked "const" too.
Tested with these changes:
+ const int nRPCPort;
+ const std::string strDataDir;
+ CBaseChainParams() = delete;
+ CBaseChainParams(const CBaseChainParams&) = delete;
+ CBaseChainParams& operator=(const CBaseChainParams&) = delete;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having a non-default constructor already prevents automatic declaration of the default constructor
Does it? I really think that's not the case, do you have any reference?
but "CBaseChainParams() = delete;" seems clearer still.
Perhaps. I've never seen this contruct but if people prefer this I'm happy to change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(= delete
is new in C++11)
Reference: http://en.cppreference.com/w/cpp/language/default_constructor
If no user-declared constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class.
[[c++11:]] If some user-declared constructors are present, the user may still force the automatic generation of a default constructor by the compiler that would be implicitly-declared otherwise with the keyword default.
Example:
class X { public: int a; char b; X(int _a, char _b) : a(_a), b(_b) { } };
X x;
gcc:
test.cc:2:3: error: no matching function for call to ‘X::X()’
X x;
^
test.cc:1:34: note: candidate: X::X(int, char)
test.cc:1:34: note: candidate expects 2 arguments, 0 provided
test.cc:1:7: note: candidate: constexpr X::X(const X&)
test.cc:1:7: note: candidate expects 1 argument, 0 provided
test.cc:1:7: note: candidate: constexpr X::X(X&&)
test.cc:1:7: note: candidate expects 1 argument, 0 provided
clang:
test.cc:2:3: error: no matching constructor for initialization of 'X'
X x;
^
test.cc:1:7: note: candidate constructor (the implicit copy constructor) not
viable: requires 1 argument, but 0 were provided
test.cc:1:7: note: candidate constructor (the implicit move constructor) not
viable: requires 1 argument, but 0 were provided
test.cc:1:34: note: candidate constructor not viable: requires 2 arguments,
but 0 were provided
class X { public: int a; char b; X(int _a, char _b) : a(_a), b(_b) { } };
^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking more about this, I see how your interest in also prohibiting the copy constructor and the override of the assign operator may make sense in many cases, but I think in this case we're just fine prohibiting only the default contructor. Anything copied from an instance initialized with the parametrized contructor should be fine.
But thank you for teaching me newer c++ features, I love to be able to ditch that hackish design pattern.
I will
CBaseChainParams() = delete;
Actually the fact that an upgrade of the language supports what I was "simulating" gives me much more confidence on that part. Thank you again for stopping me from reinventing the wheel.
277b4a9
to
1687cb4
Compare
Fixed nit (moved to deleted constructor). |
utACK 1687cb4 |
utACK 1687cb4 |
utACK 1687cb4. Nice change. |
utACK 1687cb4 |
1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón) Pull request description: There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields. This starts as a +10-43 diff Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d
Summary: 1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón) Pull request description: There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields. This starts as a +10-43 diff Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d Backport of Core PR12128 bitcoin/bitcoin#12128 Test Plan: make check Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Differential Revision: https://reviews.bitcoinabc.org/D3804
Summary: 1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón) Pull request description: There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields. This starts as a +10-43 diff Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d Backport of Core PR12128 bitcoin/bitcoin#12128 Test Plan: make check Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Differential Revision: https://reviews.bitcoinabc.org/D3804
Summary: 1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón) Pull request description: There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields. This starts as a +10-43 diff Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d Backport of Core PR12128 bitcoin/bitcoin#12128 Test Plan: make check Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Differential Revision: https://reviews.bitcoinabc.org/D3804
Summary: 1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón) Pull request description: There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields. This starts as a +10-43 diff Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d Backport of Core PR12128 bitcoin/bitcoin#12128 Test Plan: make check Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Differential Revision: https://reviews.bitcoinabc.org/D3804
1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón) Pull request description: There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields. This starts as a +10-43 diff Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d
…ugh (#3865) 1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón) Pull request description: There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields. This starts as a +10-43 diff Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
a58be04 Use a unique RPC port for regtest (Fuzzbawls) d9f1710 One CBaseChainParams should be enough (Fuzzbawls) Pull request description: Coming from bitcoin#12128 > There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields. > This starts as a +10-43 diff Since we already require c++14, can use `std::make_unique` directly. Note: I did not change the regtest RPC port here, but maybe this is the perfect opportunity to do so if we want it to not conflict with testnet's RPC port ACKs for top commit: random-zebra: utACK a58be04 furszy: utACK a58be04 Tree-SHA512: 2c4d70931e7228cec35113ed28732410efc6f0daca75eef7abc2f4337919fca170407635044ca4096a5af5a802280f414f5422b6d2607fbeca1e62f78386107d
There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields.
This starts as a +10-43 diff