Skip to content

Commit 65e2a72

Browse files
committed
Chainparams: Use a regular factory for creating chainparams
1 parent 4608c72 commit 65e2a72

File tree

6 files changed

+63
-20
lines changed

6 files changed

+63
-20
lines changed

src/chainparams.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ class CMainParams : public CChainParams {
135135
};
136136
}
137137
};
138-
static CMainParams mainParams;
139138

140139
/**
141140
* Testnet (v3)
@@ -196,7 +195,6 @@ class CTestNetParams : public CMainParams {
196195

197196
}
198197
};
199-
static CTestNetParams testNetParams;
200198

201199
/**
202200
* Regression test
@@ -242,33 +240,54 @@ class CRegTestParams : public CTestNetParams {
242240
};
243241
}
244242
};
245-
static CRegTestParams regTestParams;
246243

247244
static CChainParams *pCurrentParams = 0;
245+
static CChainParams* pSwitchingParams = 0;
248246

249247
const CChainParams &Params() {
250248
assert(pCurrentParams);
251249
return *pCurrentParams;
252250
}
253251

254-
CChainParams& Params(std::string chain)
252+
CChainParams* ParamsFactory(std::string chain)
255253
{
256254
if (chain == "main")
257-
return mainParams;
255+
return new CMainParams();
258256
if (chain == "testnet")
259-
return testNetParams;
257+
return new CTestNetParams();
260258
if (chain == "regtest")
261-
return regTestParams;
259+
return new CRegTestParams();
262260
throw std::runtime_error("Unknown chain " + chain + "\n");
263261
}
264262

263+
const CChainParams& Params(std::string chain)
264+
{
265+
if (pSwitchingParams)
266+
delete(pSwitchingParams);
267+
pSwitchingParams = ParamsFactory(chain);
268+
return *pSwitchingParams;
269+
}
270+
265271
void SelectParams(std::string chain)
266272
{
267273
SelectBaseParams(chain);
268-
pCurrentParams = &Params(chain);
274+
if (pCurrentParams)
275+
delete(pCurrentParams);
276+
pCurrentParams = ParamsFactory(chain);
269277
}
270278

271279
void SelectParamsFromCommandLine()
272280
{
273281
SelectParams(ChainNameFromCommandLine());
274282
}
283+
284+
void ClearSelectedParams()
285+
{
286+
if (pCurrentParams)
287+
delete(pCurrentParams);
288+
pCurrentParams = NULL;
289+
if (pSwitchingParams)
290+
delete(pSwitchingParams);
291+
pSwitchingParams = NULL;
292+
ClearSelectedBaseParams();
293+
}

src/chainparams.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,22 @@ class CChainParams
9696
Checkpoints::CCheckpointData checkpointData;
9797
};
9898

99+
/**
100+
* Creates a CChainParams of the chosen chain and returns a
101+
* pointer to it. The caller has to delete the object.
102+
* Raises an error if the chain is not supported.
103+
*/
104+
CChainParams* ParamsFactory(std::string chain);
105+
106+
/** Functions that relay on internal state */
99107
/**
100108
* Return the currently selected parameters. This won't change after app
101109
* startup, except for unit tests.
102110
*/
103111
const CChainParams &Params();
104112

105113
/** Return parameters for the given chain. */
106-
CChainParams& Params(std::string chain);
114+
const CChainParams& Params(std::string chain);
107115

108116
/** Sets the params returned by Params() to those for the given chain. */
109117
void SelectParams(std::string chain);
@@ -113,5 +121,7 @@ void SelectParams(std::string chain);
113121
* and SelectParams() to select the appropriate chain.
114122
*/
115123
void SelectParamsFromCommandLine();
124+
/** Unselects params and frees memory. */
125+
void ClearSelectedParams();
116126

117127
#endif // BITCOIN_CHAINPARAMS_H

src/chainparamsbase.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class CBaseMainParams : public CBaseChainParams
2424
nRPCPort = 8332;
2525
}
2626
};
27-
static CBaseMainParams mainParams;
2827

2928
/**
3029
* Testnet (v3)
@@ -38,7 +37,6 @@ class CBaseTestNetParams : public CBaseMainParams
3837
strDataDir = "testnet3";
3938
}
4039
};
41-
static CBaseTestNetParams testNetParams;
4240

4341
/*
4442
* Regression test
@@ -51,7 +49,6 @@ class CBaseRegTestParams : public CBaseTestNetParams
5149
strDataDir = "regtest";
5250
}
5351
};
54-
static CBaseRegTestParams regTestParams;
5552

5653
static CBaseChainParams* pCurrentBaseParams = 0;
5754

@@ -61,20 +58,22 @@ const CBaseChainParams& BaseParams()
6158
return *pCurrentBaseParams;
6259
}
6360

64-
CBaseChainParams& BaseParams(std::string chain)
61+
CBaseChainParams* FactoryBaseParams(std::string chain)
6562
{
6663
if (chain == "main")
67-
return mainParams;
64+
return new CBaseMainParams();
6865
if (chain == "testnet")
69-
return testNetParams;
66+
return new CBaseTestNetParams();
7067
if (chain == "regtest")
71-
return regTestParams;
68+
return new CBaseRegTestParams();
7269
throw std::runtime_error("Unknown chain " + chain + "\n");
7370
}
7471

7572
void SelectBaseParams(std::string chain)
7673
{
77-
pCurrentBaseParams = &BaseParams(chain);
74+
if (pCurrentBaseParams)
75+
delete(pCurrentBaseParams);
76+
pCurrentBaseParams = FactoryBaseParams(chain);
7877
}
7978

8079
std::string GetParamsHelpMessages()
@@ -110,3 +109,10 @@ bool AreBaseParamsConfigured()
110109
{
111110
return pCurrentBaseParams != NULL;
112111
}
112+
113+
void ClearSelectedBaseParams()
114+
{
115+
if (pCurrentBaseParams)
116+
delete(pCurrentBaseParams);
117+
pCurrentBaseParams = NULL;
118+
}

src/chainparamsbase.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,22 @@ class CBaseChainParams
3030
};
3131

3232
/**
33-
* Returns a reference to a CBaseChainParams of the chosen chain.
33+
* Creates a CBaseChainParams of the chosen chain and returns a
34+
* pointer to it. The caller has to delete the object.
3435
* Raises an error if the chain is not supported.
3536
*/
36-
CBaseChainParams& BaseParams(std::string chain);
37-
/** Returns a string with the help messages for the chainparams options. */
37+
CBaseChainParams* BaseParams(std::string chain);
38+
/**
39+
* Returns a string with the help messages for the chainparams options.
40+
*/
3841
std::string GetParamsHelpMessages();
3942
/**
4043
* Looks for -regtest, -testnet and -chain ["main" by default] and returns the appropriate chain name.
4144
* Raises an error if an invalid combination is given.
4245
*/
4346
std::string ChainNameFromCommandLine();
4447

48+
/** Functions that relay on internal state */
4549
/**
4650
* Return the currently selected parameters. This won't change after app
4751
* startup, except for unit tests.
@@ -61,5 +65,7 @@ void SelectBaseParamsFromCommandLine();
6165
* Return true if SelectBaseParamsFromCommandLine() has been called to select a chain.
6266
*/
6367
bool AreBaseParamsConfigured();
68+
/** Unselects params and frees memory. */
69+
void ClearSelectedBaseParams();
6470

6571
#endif // BITCOIN_CHAINPARAMSBASE_H

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ void Shutdown()
196196
pwalletMain = NULL;
197197
#endif
198198
ECC_Stop();
199+
ClearSelectedParams();
199200
LogPrintf("%s: done\n", __func__);
200201
}
201202

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ TestingSetup::~TestingSetup()
8484
bitdb.Reset();
8585
#endif
8686
boost::filesystem::remove_all(pathTemp);
87+
ClearSelectedParams();
8788
}
8889

8990
void Shutdown(void* parg)

0 commit comments

Comments
 (0)