Skip to content

Commit 7633516

Browse files
committed
[core] Minor refacturing + unused variable removed
1 parent 926c073 commit 7633516

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

src/miner.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
211211
//and higher priority to zpivs that are large in value
212212
int64_t nTimeSeen = GetAdjustedTime();
213213
double nConfs = 100000;
214-
double dPriorityPrev = dPriority;
215214

216215
auto it = mapZerocoinspends.find(txid);
217216
if (it != mapZerocoinspends.end()) {
@@ -221,19 +220,11 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
221220
mapZerocoinspends[txid] = nTimeSeen;
222221
}
223222

224-
//zPIV spends can have very large priority, prevent datatype problems
225223
double nTimePriority = std::pow(GetAdjustedTime() - nTimeSeen, 6);
226-
double fLimit = std::numeric_limits<double>::max() - dPriority;
227-
if (fLimit > (nTimePriority * nConfs))
228-
dPriority += nTimePriority * nConfs;
229-
else
230-
dPriority = std::numeric_limits<double>::max();
231-
232-
fLimit = std::numeric_limits<double>::max() / dPriority;
233-
if (fLimit > nTotalIn)
234-
dPriority *= nTotalIn;
235-
else
236-
dPriority = std::numeric_limits<double>::max();
224+
225+
// zPIV spends can have very large priority, use non-overflowing safe functions
226+
dPriority = double_safe_addition(dPriority, (nTimePriority * nConfs));
227+
dPriority = double_safe_multiplication(dPriority, nTotalIn);
237228

238229
continue;
239230
}
@@ -279,13 +270,9 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
279270

280271
int nConf = nHeight - coins->nHeight;
281272

282-
//zPIV spends can have very large priority, prevent datatype problems
283-
double fLimit = std::numeric_limits<double>::max() - dPriority;
273+
// zPIV spends can have very large priority, use non-overflowing safe functions
274+
dPriority = double_safe_addition(dPriority, ((double)nValueIn * nConf));
284275

285-
if (fLimit > ((double)nValueIn * nConf))
286-
dPriority += (double)nValueIn * nConf;
287-
else
288-
dPriority = std::numeric_limits<double>::max();
289276
}
290277
if (fMissingInputs) continue;
291278

src/util.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,26 @@ boost::filesystem::path GetTempPath()
738738
#endif
739739
}
740740

741+
double double_safe_addition(double fValue, double fIncrement)
742+
{
743+
double fLimit = std::numeric_limits<double>::max() - fValue;
744+
745+
if (fLimit > fIncrement)
746+
return fValue + fIncrement;
747+
else
748+
return std::numeric_limits<double>::max();
749+
}
750+
751+
double double_safe_multiplication(double fValue, double fmultiplicator)
752+
{
753+
double fLimit = std::numeric_limits<double>::max() / fmultiplicator;
754+
755+
if (fLimit > fmultiplicator)
756+
return fValue * fmultiplicator;
757+
else
758+
return std::numeric_limits<double>::max();
759+
}
760+
741761
void runCommand(std::string strCommand)
742762
{
743763
int nErr = ::system(strCommand.c_str());

src/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ static inline bool error(const char* format)
106106
return false;
107107
}
108108

109+
double double_safe_addition(double fValue, double fIncrement);
110+
double double_safe_multiplication(double fValue, double fmultiplicator);
109111
void PrintExceptionContinue(std::exception* pex, const char* pszThread);
110112
void ParseParameters(int argc, const char* const argv[]);
111113
void FileCommit(FILE* fileout);

0 commit comments

Comments
 (0)