Skip to content

Conversation

@jtimon
Copy link
Contributor

@jtimon jtimon commented Jul 1, 2015

This shares the first commit with #6230.
This is an alternative to theuni@123f3b0 which @theuni wasn't even proposing yet. But I would like your feedback on the factory and the container (which are shared with #6068 ).
This is blocking #6229 and #5229.
EDIT:
The fixup commit cleaning up the includes may make more sense after more of #5970 (for example, #6163, we're not that far away from making miner.cpp independent from the chainparams globals) is left for later.

@jtimon jtimon force-pushed the chainparams-globals-0.11.99 branch 2 times, most recently from de0c715 to 4f9a138 Compare July 1, 2015 12:22
@jtimon jtimon force-pushed the chainparams-globals-0.11.99 branch from 4f9a138 to 7901a6a Compare July 1, 2015 17:33
@jtimon jtimon force-pushed the chainparams-globals-0.11.99 branch from 7901a6a to db85b3d Compare July 2, 2015 11:50
@jtimon
Copy link
Contributor Author

jtimon commented Jul 2, 2015

Since I'm still working on this...closing for now. I mainly wanted to ping @theuni and what is here works and is enough to start receiving feedback.

@jtimon jtimon closed this Jul 2, 2015
@theuni
Copy link
Member

theuni commented Jul 2, 2015

@jtimon I'm not sure it's worth the complication of a factory here. Given that all chainparams and consensusparams are immutable, why not just pass around new instances when necessary? That should still be cheap, and much simpler? Especially given the trend to pass the params around as necessary rather than call Params() directly.

Something like:

static CBaseChainParams::Network nCurrentParams = CBaseChainParams::MAX_NETWORK_TYPES;

const CChainParams Params() {
    switch (nCurrentParams) {
        case CBaseChainParams::MAIN:
            return CChainParams(CMainParams());
        case CBaseChainParams::TESTNET:
            return CChainParams(CTestNetParams());
        case CBaseChainParams::REGTEST:
            return CChainParams(CRegTestParams());
        default:
            assert(false && "Unimplemented network");
            return CChainParams(CMainParams());
    }
}

void SelectParams(CBaseChainParams::Network network) {
    SelectBaseParams(network);
    nCurrentParams = network;
}

Hell, since there are no virtual functions left (nor should there be), we could even do away with the inheritance and just make CBaseChainParams::Network a ctor arg for CChainParams.

@jtimon
Copy link
Contributor Author

jtimon commented Jul 2, 2015

The main point of the factory is that it is not on globals/ because once we're finished "passing chainparams around" there should be no need for any global at all: You either take it as parameter or use the factory (this includes removing the SelectParams from BasicTestingSetup::BasicTestingSetup(), since nobody depends on the global and tests can use the factory directly).

But if copying the object in the factory (like you do with the Params() of your example), then the factory doesn't need to return a pointer (it could just be the current parametrized Params() ), and the Container would also be unnecessary (and the same goes for CPolicy).

The enum doesn't scale very well once you go beyond -testnet and -regtest (see #5229 in fact, I'm preparing a PR to add infinite new testing altchains). The same happens with the single constructor parameter.
But you are right, the inheritance is overkill here.
With a fully parametric constructor the factory can just always call that constructor with different values.
ie:

CChainParams* Params(const std::string& chain)
{
   if (chain == CBaseChainParams::MAIN)
        return new CChainParams("main", 210000, 750, 950, 1000...);
    else if (chain == CBaseChainParams::TESTNET)
 ...

@jtimon
Copy link
Contributor Author

jtimon commented Jul 6, 2015

@theuni I've been exploring your suggestion and it's not so straight-forward as you may think.
If Params(string) returns a new object instead of a reference some places will crash, like this one:
https://github.com/bitcoin/bitcoin/blob/master/src/test/alert_tests.cpp#L125
It also means that Params() and BaseParams() have to return a new object as well instead of a const reference.
It works fine for ParamsBase, which doesn't have a similar &ParamsBase(enum/string) psedo-factory.
But I really prefer to just remove the interface functions in this case (where it's easy, see jtimon@7fb9f6a ).
Can we first agree that it will be better to remove SelectParamsFromCommandLine and SelectBaseParamsFromCommandLine before separating the globals?
I have reopened #6235 with that.
@rustyrussell you may be interested in this discussion, or reviewing #6235.

@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants