From eddd6bc11bffe0b7837f39ee4e2d1073264b4cec Mon Sep 17 00:00:00 2001 From: jamescowens Date: Fri, 20 Nov 2020 11:34:02 -0500 Subject: [PATCH] Refactor to use clamp in util.h --- src/gridcoin/gridcoin.cpp | 6 +++--- src/gridcoin/quorum.cpp | 2 +- src/gridcoin/staking/reward.cpp | 3 +-- src/miner.cpp | 2 +- src/test/gridcoin/superblock_tests.cpp | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/gridcoin/gridcoin.cpp b/src/gridcoin/gridcoin.cpp index f1734ed434..27c8280775 100644 --- a/src/gridcoin/gridcoin.cpp +++ b/src/gridcoin/gridcoin.cpp @@ -154,9 +154,9 @@ void ThreadScraperSubscriber(void* parg) void InitializeScraper(ThreadHandlerPtr threads) { // Default to 300 sec (5 min), clamp to 60 minimum, 600 maximum - converted to milliseconds. - nScraperSleep = std::min(std::max(GetArg("-scrapersleep", 300), 60), 600) * 1000; - // Default to 7200 sec (4 hrs), clamp to 300 minimum, 86400 maximum (meaning active all of the time). - nActiveBeforeSB = std::min(std::max(GetArg("-activebeforesb", 14400), 300), 86400); + nScraperSleep = clamp(GetArg("-scrapersleep", 300), 60, 600) * 1000; + // Default to 14400 sec (4 hrs), clamp to 300 minimum, 86400 maximum (meaning active all of the time). + nActiveBeforeSB = clamp(GetArg("-activebeforesb", 14400), 300, 86400); // Run the scraper or subscriber housekeeping thread, but not both. The // subscriber housekeeping thread checks if the flag for the scraper thread diff --git a/src/gridcoin/quorum.cpp b/src/gridcoin/quorum.cpp index b18ab587e4..37c7b4ad71 100644 --- a/src/gridcoin/quorum.cpp +++ b/src/gridcoin/quorum.cpp @@ -573,7 +573,7 @@ class SuperblockValidator SuperblockValidator(const SuperblockPtr& superblock, size_t hint_bits = 32) : m_superblock(superblock) , m_quorum_hash(superblock->GetHash()) - , m_hint_shift(32 + std::max(0, std::min(32, 32 - hint_bits))) + , m_hint_shift(32 + clamp(32 - hint_bits, 0, 32)) { } diff --git a/src/gridcoin/staking/reward.cpp b/src/gridcoin/staking/reward.cpp index ff37d8863d..8c007f26e1 100644 --- a/src/gridcoin/staking/reward.cpp +++ b/src/gridcoin/staking/reward.cpp @@ -49,8 +49,7 @@ CAmount GRC::GetConstantBlockReward(const CBlockIndex* index) reward = atoi64(oCBReward.value); } - reward = std::max(reward, MIN_CBR); - reward = std::min(reward, MAX_CBR); + reward = clamp(reward, MIN_CBR, MAX_CBR); return reward; } diff --git a/src/miner.cpp b/src/miner.cpp index 19a10464d5..9e3d6fa474 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -266,7 +266,7 @@ bool CreateRestOfTheBlock(CBlock &block, CBlockIndex* pindexPrev) // Largest block you're willing to create: unsigned int nBlockMaxSize = GetArg("-blockmaxsize", MAX_BLOCK_SIZE_GEN/2); // Limit to between 1K and MAX_BLOCK_SIZE-1K for sanity: - nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize)); + nBlockMaxSize = clamp(nBlockMaxSize, 1000, MAX_BLOCK_SIZE - 1000); // How much of the block should be dedicated to high-priority transactions, // included regardless of the fees they pay diff --git a/src/test/gridcoin/superblock_tests.cpp b/src/test/gridcoin/superblock_tests.cpp index 3e4a9e386f..54ac1ff029 100644 --- a/src/test/gridcoin/superblock_tests.cpp +++ b/src/test/gridcoin/superblock_tests.cpp @@ -112,7 +112,7 @@ struct Legacy // Ensure we do not blow out the binary space (technically we can handle 0-65535) double magnitude_d = strtod(ExtractValue(entry, ",", 1).c_str(), NULL); // Changed to 65535 for the new NN. This will still be able to be successfully unpacked by any node. - magnitude_d = std::max(0.0, std::min(magnitude_d, 65535.0)); + magnitude_d = clamp(magnitude_d, 0.0, 65535.0); researcher.magnitude = htobe16(roundint(magnitude_d)); stream.write((const char*) &researcher, sizeof(BinaryResearcher));