Skip to content

Commit 74540ca

Browse files
author
Sergey
committed
Change VersionInfo class to convert functions
1 parent b78e5f9 commit 74540ca

File tree

5 files changed

+42
-56
lines changed

5 files changed

+42
-56
lines changed

src/rpcmasternode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,6 @@ UniValue sentinelping(const UniValue& params, bool fHelp)
812812
);
813813
}
814814

815-
activeMasternode.UpdateSentinelPing(VersionInfo(params[0].get_str()));
815+
activeMasternode.UpdateSentinelPing(StringVersionToInt(params[0].get_str()));
816816
return true;
817817
}

src/test/rpc_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ BOOST_AUTO_TEST_CASE(rpc_sentinel_ping)
311311
{
312312
BOOST_CHECK_NO_THROW(CallRPC("sentinelping 1.0.2"));
313313
BOOST_CHECK_THROW(CallRPC("sentinelping"), runtime_error);
314-
BOOST_CHECK_THROW(CallRPC("sentinelping 2"), runtime_error);
314+
BOOST_CHECK_THROW(CallRPC("sentinelping 2"), bad_cast);
315315
}
316316

317317
BOOST_AUTO_TEST_SUITE_END()

src/test/util_tests.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -491,19 +491,17 @@ BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
491491

492492
BOOST_AUTO_TEST_CASE(version_info_helper)
493493
{
494-
BOOST_CHECK(VersionInfo("1.1.1") == 0x010101);
495-
BOOST_CHECK(VersionInfo(0x010101) == 0x010101);
496-
BOOST_CHECK(std::string(VersionInfo("1.1.1")) == "1.1.1");
497-
BOOST_CHECK(std::string(VersionInfo(0x010101)) == "1.1.1");
498-
499-
BOOST_CHECK_THROW(VersionInfo("1.1.hgdghfgf"), runtime_error);
500-
BOOST_CHECK_THROW(VersionInfo("1.1"), runtime_error);
501-
BOOST_CHECK_THROW(VersionInfo("1.1.1f"), runtime_error);
502-
BOOST_CHECK_THROW(VersionInfo("1.1.1000"), runtime_error);
503-
BOOST_CHECK_THROW(VersionInfo("10"), runtime_error);
504-
BOOST_CHECK_THROW(VersionInfo("1.1.1.1"), runtime_error);
505-
BOOST_CHECK_THROW(VersionInfo(0x01010101), runtime_error);
506-
BOOST_CHECK_THROW(VersionInfo(0), runtime_error);
494+
BOOST_CHECK(StringVersionToInt("1.1.1") == 0x010101);
495+
BOOST_CHECK(IntVersionToString(0x010101) == "1.1.1");
496+
497+
BOOST_CHECK_THROW(StringVersionToInt("1.1.hgdghfgf"), bad_cast);
498+
BOOST_CHECK_THROW(StringVersionToInt("1.1"), bad_cast);
499+
BOOST_CHECK_THROW(StringVersionToInt("1.1.1f"), bad_cast);
500+
BOOST_CHECK_THROW(StringVersionToInt("1.1.1000"), bad_cast);
501+
BOOST_CHECK_THROW(StringVersionToInt("10"), bad_cast);
502+
BOOST_CHECK_THROW(StringVersionToInt("1.1.1.1"), bad_cast);
503+
BOOST_CHECK_THROW(IntVersionToString(0x01010101), bad_cast);
504+
BOOST_CHECK_THROW(IntVersionToString(0), bad_cast);
507505
}
508506

509507
BOOST_AUTO_TEST_SUITE_END()

src/util.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -954,40 +954,33 @@ int GetNumCores()
954954
#endif
955955
}
956956

957-
VersionInfo::VersionInfo(uint32_t version)
958-
{
959-
if((version >> 24) > 0)
960-
throw std::runtime_error("Invalid version format");
961-
if(version == 0)
962-
throw std::runtime_error("Invalid version format");
963-
nVersion = version;
964-
}
965957

966-
VersionInfo::VersionInfo(const std::string& versionInfo)
958+
uint32_t StringVersionToInt(const std::string& strVersion)
967959
{
968960
std::vector<std::string> tokens;
969-
boost::split(tokens, versionInfo, boost::is_any_of("."));
961+
boost::split(tokens, strVersion, boost::is_any_of("."));
970962
if(tokens.size() != 3)
971-
throw std::runtime_error("Invalid version format");
972-
uint32_t version = 0;
963+
throw std::bad_cast();
964+
uint32_t nVersion = 0;
973965
for(unsigned idx = 0; idx < 3; idx++)
974966
{
975967
if(tokens[idx].length() == 0)
976-
throw std::runtime_error("Invalid version format");
977-
if(!std::none_of(tokens[idx].begin(), tokens[idx].end(),
978-
[](char c){return !std::isdigit(c);}))
979-
throw std::runtime_error("Invalid version format");
980-
uint32_t value = atol(tokens[idx].c_str());
968+
throw std::bad_cast();
969+
uint32_t value = boost::lexical_cast<uint32_t>(tokens[idx]);
981970
if(value > 255)
982-
throw std::runtime_error("Invalid version format");
983-
version <<= 8;
984-
version |= value;
971+
throw std::bad_cast();
972+
nVersion <<= 8;
973+
nVersion |= value;
985974
}
986-
nVersion = version;
975+
return nVersion;
987976
}
988977

989-
VersionInfo::operator std::string() const
978+
std::string IntVersionToString(uint32_t nVersion)
990979
{
980+
if((nVersion >> 24) > 0) // MSB is always 0
981+
throw std::bad_cast();
982+
if(nVersion == 0)
983+
throw std::bad_cast();
991984
std::array<std::string, 3> tokens;
992985
for(unsigned idx = 0; idx < 3; idx++)
993986
{
@@ -997,8 +990,3 @@ VersionInfo::operator std::string() const
997990
}
998991
return boost::join(tokens, ".");
999992
}
1000-
1001-
VersionInfo::operator uint32_t() const
1002-
{
1003-
return nVersion;
1004-
}

src/util.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,22 @@ template <typename Callable> void TraceThread(const char* name, Callable func)
272272
}
273273

274274

275-
/** Helper class to convert version strings of "x.x.x" format
276-
* to 4-byte unsigned integer and vice versa.
277-
* Most significant byte in integer is always 0.
275+
/**
276+
* @brief Converts version strings to 4-byte unsigned integer
277+
* @param strVersion version in "x.x.x" format (decimal digits only)
278+
* @return 4-byte unsigned integer, most significant byte is always 0
279+
* Throws std::bad_cast if format doesn\t match.
278280
*/
279-
class VersionInfo{
280-
uint32_t nVersion;
281-
public:
282-
VersionInfo(uint32_t version);
283-
VersionInfo(const std::string& version);
281+
uint32_t StringVersionToInt(const std::string& strVersion);
284282

285-
VersionInfo(const VersionInfo&) = default;
286-
VersionInfo(VersionInfo&&) = default;
287-
VersionInfo& operator=(const VersionInfo&) = default;
288283

289-
operator uint32_t() const;
290-
operator std::string() const;
291-
};
284+
/**
285+
* @brief Converts version as 4-byte unsigned integer to string
286+
* @param nVersion 4-byte unsigned integer, most significant byte is always 0
287+
* @return version string in "x.x.x" format (last 3 bytes as version parts)
288+
* Throws std::bad_cast if format doesn\t match.
289+
*/
290+
std::string IntVersionToString(uint32_t nVersion);
291+
292292

293293
#endif // BITCOIN_UTIL_H

0 commit comments

Comments
 (0)