From 12240dca58c97cc7d5ec4b2c0308efaeb7512d75 Mon Sep 17 00:00:00 2001 From: random-zebra Date: Sun, 20 Dec 2020 19:37:40 +0100 Subject: [PATCH 1/3] [BUG] fix a few places improperly checking boost::get --- src/qt/pivx/settings/settingsbittoolwidget.cpp | 4 ++-- src/wallet/rpcdump.cpp | 10 +++++----- src/wallet/rpcwallet.cpp | 18 +++++++----------- src/wallet/wallet.cpp | 2 +- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/qt/pivx/settings/settingsbittoolwidget.cpp b/src/qt/pivx/settings/settingsbittoolwidget.cpp index 14e5ecbbc947..ea192449e0e4 100644 --- a/src/qt/pivx/settings/settingsbittoolwidget.cpp +++ b/src/qt/pivx/settings/settingsbittoolwidget.cpp @@ -150,7 +150,7 @@ void SettingsBitToolWidget::onEncryptKeyButtonENCClicked() return; } - CKeyID keyID = *boost::get(&dest); + const CKeyID* keyID = boost::get(&dest); if (!keyID) { //ui->addressIn_ENC->setValid(false); ui->statusLabel_ENC->setStyleSheet("QLabel { color: red; }"); @@ -166,7 +166,7 @@ void SettingsBitToolWidget::onEncryptKeyButtonENCClicked() } CKey key; - if (!pwalletMain->GetKey(keyID, key)) { + if (!pwalletMain->GetKey(*keyID, key)) { ui->statusLabel_ENC->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_ENC->setText(tr("Private key for the entered address is not available.")); return; diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 8e355f680c5a..2509f7c9db82 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -434,11 +434,11 @@ UniValue dumpprivkey(const JSONRPCRequest& request) CTxDestination dest = DecodeDestination(strAddress); if (!IsValidDestination(dest)) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address"); - CKeyID keyID = *boost::get(&dest); + const CKeyID* keyID = boost::get(&dest); if (!keyID) throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key"); CKey vchSecret; - if (!pwalletMain->GetKey(keyID, vchSecret)) + if (!pwalletMain->GetKey(*keyID, vchSecret)) throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known"); return EncodeSecret(vchSecret); } @@ -610,11 +610,11 @@ UniValue bip38encrypt(const JSONRPCRequest& request) CTxDestination address = DecodeDestination(strAddress); if (!IsValidDestination(address)) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address"); - CKeyID keyID = *boost::get(&address); + const CKeyID* keyID = boost::get(&address); if (!keyID) throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key"); CKey vchSecret; - if (!pwalletMain->GetKey(keyID, vchSecret)) + if (!pwalletMain->GetKey(*keyID, vchSecret)) throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known"); uint256 privKey = vchSecret.GetPrivKey_256(); @@ -944,4 +944,4 @@ UniValue exportsaplingkey(const JSONRPCRequest& request) throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private key for this shielded addr"); } return KeyIO::EncodeSpendingKey(libzcash::SpendingKey(sk.get())); -} \ No newline at end of file +} diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 1083904f7cc4..f20f4c781c89 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -679,11 +679,11 @@ UniValue delegatoradd(const JSONRPCRequest& request) const std::string strLabel = (request.params.size() > 1 ? request.params[1].get_str() : ""); - CKeyID keyID = boost::get(DecodeDestination(request.params[0].get_str())); + const CKeyID* keyID = boost::get(&dest); if (!keyID) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to get KeyID from PIVX address"); - return pwalletMain->SetAddressBook(keyID, strLabel, AddressBook::AddressBookPurpose::DELEGATOR); + return pwalletMain->SetAddressBook(*keyID, strLabel, AddressBook::AddressBookPurpose::DELEGATOR); } UniValue delegatorremove(const JSONRPCRequest& request) @@ -709,11 +709,11 @@ UniValue delegatorremove(const JSONRPCRequest& request) if (!IsValidDestination(dest) || isStakingAddress) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address"); - CKeyID keyID = *boost::get(&dest); + const CKeyID* keyID = boost::get(&dest); if (!keyID) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to get KeyID from PIVX address"); - if (!pwalletMain->HasAddressBook(keyID)) + if (!pwalletMain->HasAddressBook(*keyID)) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to get PIVX address from addressBook"); std::string label = ""; @@ -722,7 +722,7 @@ UniValue delegatorremove(const JSONRPCRequest& request) label = optAdd->name; } - return pwalletMain->SetAddressBook(keyID, label, AddressBook::AddressBookPurpose::DELEGABLE); + return pwalletMain->SetAddressBook(*keyID, label, AddressBook::AddressBookPurpose::DELEGABLE); } UniValue ListaddressesForPurpose(const std::string strPurpose) @@ -1103,8 +1103,6 @@ UniValue CreateColdStakeDelegation(const UniValue& params, CWalletTx& wtxNew, CR if (!IsValidDestination(dest) || isStaking) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX spending address"); ownerKey = *boost::get(&dest); - if (!ownerKey) - throw JSONRPCError(RPC_WALLET_ERROR, "Unable to get spend pubkey hash from owneraddress"); // Check that the owner address belongs to this wallet, or fForceExternalAddr is true bool fForceExternalAddr = params.size() > 3 && !params[3].isNull() ? params[3].get_bool() : false; if (!fForceExternalAddr && !pwalletMain->HaveKey(ownerKey)) { @@ -1119,8 +1117,6 @@ UniValue CreateColdStakeDelegation(const UniValue& params, CWalletTx& wtxNew, CR // Get new owner address from keypool CTxDestination ownerAddr = GetNewAddressFromLabel("delegated", NullUniValue); ownerKey = *boost::get(&ownerAddr); - if (!ownerKey) - throw JSONRPCError(RPC_WALLET_ERROR, "Unable to get spend pubkey hash from owneraddress"); ownerAddressStr = EncodeDestination(ownerAddr); } @@ -1838,12 +1834,12 @@ UniValue signmessage(const JSONRPCRequest& request) if (!IsValidDestination(dest)) throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address"); - CKeyID keyID = *boost::get(&dest); + const CKeyID* keyID = boost::get(&dest); if (!keyID) throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key"); CKey key; - if (!pwalletMain->GetKey(keyID, key)) + if (!pwalletMain->GetKey(*keyID, key)) throw JSONRPCError(RPC_WALLET_ERROR, "Private key not available"); CHashWriter ss(SER_GETHASH, 0); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 01dbb057cf1a..39dde2246c86 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -784,7 +784,7 @@ bool CWallet::GetVinAndKeysFromOutput(COutput out, CTxIn& txinRet, CPubKey& pubK CTxDestination address1; ExtractDestination(pubScript, address1, fColdStake); - CKeyID* keyID = boost::get(&address1); + const CKeyID* keyID = boost::get(&address1); if (!keyID) { LogPrintf("CWallet::GetVinAndKeysFromOutput -- Address does not refer to a key\n"); return false; From 86a106ebc5f6edaa6dc32fc113c363ace279d272 Mon Sep 17 00:00:00 2001 From: random-zebra Date: Sun, 20 Dec 2020 19:47:05 +0100 Subject: [PATCH 2/3] [Tests] Use arith_uint160 instead of uint160 in uint256_tests --- src/test/uint256_tests.cpp | 100 ++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp index f8aa5eedb53b..cfc3f9d4703d 100644 --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -23,14 +23,14 @@ const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b12115 const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256 const double R1Sdouble = 0.7096329412477836074; const uint256 R1L = uint256(std::vector(R1Array,R1Array+32)); -const uint160 R1S = uint160(std::vector(R1Array,R1Array+20)); +const arith_uint160 R1S = arith_uint160(std::vector(R1Array,R1Array+20)); const uint64_t R1LLow64 = 0x121156cfdb4a529cULL; const unsigned char R2Array[] = "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf" "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7"; const uint256 R2L = uint256(std::vector(R2Array,R2Array+32)); -const uint160 R2S = uint160(std::vector(R2Array,R2Array+20)); +const arith_uint160 R2S = arith_uint160(std::vector(R2Array,R2Array+20)); const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C"; @@ -38,22 +38,22 @@ const unsigned char ZeroArray[] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; const uint256 ZeroL = uint256(std::vector(ZeroArray,ZeroArray+32)); -const uint160 ZeroS = uint160(std::vector(ZeroArray,ZeroArray+20)); +const arith_uint160 ZeroS = arith_uint160(std::vector(ZeroArray,ZeroArray+20)); const unsigned char OneArray[] = "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; const uint256 OneL = uint256(std::vector(OneArray,OneArray+32)); -const uint160 OneS = uint160(std::vector(OneArray,OneArray+20)); +const arith_uint160 OneS = arith_uint160(std::vector(OneArray,OneArray+20)); const unsigned char MaxArray[] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; const uint256 MaxL = uint256(std::vector(MaxArray,MaxArray+32)); -const uint160 MaxS = uint160(std::vector(MaxArray,MaxArray+20)); +const arith_uint160 MaxS = arith_uint160(std::vector(MaxArray,MaxArray+20)); const uint256 HalfL = (OneL << 255); -const uint160 HalfS = (OneS << 159); +const arith_uint160 HalfS = (OneS << 159); std::string ArrayToString(const unsigned char A[], unsigned int width) { std::stringstream Stream; @@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality BOOST_CHECK(ZeroS != (OneS << i)); BOOST_CHECK((OneS << i) != ZeroS); BOOST_CHECK(R1S != (R1S ^ (OneS << i))); - BOOST_CHECK(((uint160(Tmp64) ^ (OneS << i) ) != Tmp64 )); + BOOST_CHECK(((arith_uint160(Tmp64) ^ (OneS << i) ) != Tmp64 )); } BOOST_CHECK(ZeroS == (OneS << 256)); @@ -125,30 +125,30 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality BOOST_CHECK(uint256(ZeroL) == ZeroL); BOOST_CHECK(uint256(OneL) == OneL); - BOOST_CHECK(uint160("0x"+R1S.ToString()) == R1S); - BOOST_CHECK(uint160("0x"+R2S.ToString()) == R2S); - BOOST_CHECK(uint160("0x"+ZeroS.ToString()) == ZeroS); - BOOST_CHECK(uint160("0x"+OneS.ToString()) == OneS); - BOOST_CHECK(uint160("0x"+MaxS.ToString()) == MaxS); - BOOST_CHECK(uint160(R1S.ToString()) == R1S); - BOOST_CHECK(uint160(" 0x"+R1S.ToString()+" ") == R1S); - BOOST_CHECK(uint160("") == ZeroS); - BOOST_CHECK(R1S == uint160(R1ArrayHex)); - - BOOST_CHECK(uint160(R1S) == R1S); - BOOST_CHECK((uint160(R1S^R2S)^R2S) == R1S); - BOOST_CHECK(uint160(ZeroS) == ZeroS); - BOOST_CHECK(uint160(OneS) == OneS); + BOOST_CHECK(arith_uint160("0x"+R1S.ToString()) == R1S); + BOOST_CHECK(arith_uint160("0x"+R2S.ToString()) == R2S); + BOOST_CHECK(arith_uint160("0x"+ZeroS.ToString()) == ZeroS); + BOOST_CHECK(arith_uint160("0x"+OneS.ToString()) == OneS); + BOOST_CHECK(arith_uint160("0x"+MaxS.ToString()) == MaxS); + BOOST_CHECK(arith_uint160(R1S.ToString()) == R1S); + BOOST_CHECK(arith_uint160(" 0x"+R1S.ToString()+" ") == R1S); + BOOST_CHECK(arith_uint160("") == ZeroS); + BOOST_CHECK(R1S == arith_uint160(R1ArrayHex)); + + BOOST_CHECK(arith_uint160(R1S) == R1S); + BOOST_CHECK((arith_uint160(R1S^R2S)^R2S) == R1S); + BOOST_CHECK(arith_uint160(ZeroS) == ZeroS); + BOOST_CHECK(arith_uint160(OneS) == OneS); // uint64_t constructor BOOST_CHECK( (R1L & uint256("0xffffffffffffffff")) == uint256(R1LLow64)); BOOST_CHECK(ZeroL.IsNull()); BOOST_CHECK(OneL == uint256(1)); BOOST_CHECK(uint256("0xffffffffffffffff") = uint256(0xffffffffffffffffULL)); - BOOST_CHECK( (R1S & uint160("0xffffffffffffffff")) == uint160(R1LLow64)); - BOOST_CHECK(ZeroS == uint160(0)); - BOOST_CHECK(OneS == uint160(1)); - BOOST_CHECK(uint160("0xffffffffffffffff") = uint160(0xffffffffffffffffULL)); + BOOST_CHECK( (R1S & arith_uint160("0xffffffffffffffff")) == arith_uint160(R1LLow64)); + BOOST_CHECK(ZeroS == arith_uint160(0)); + BOOST_CHECK(OneS == arith_uint160(1)); + BOOST_CHECK(arith_uint160("0xffffffffffffffff") = arith_uint160(0xffffffffffffffffULL)); // Assignment (from base_uint) uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL); @@ -156,7 +156,7 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L); tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L); tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL); - uint160 tmpS = ~ZeroS; BOOST_CHECK(tmpS == ~ZeroS); + arith_uint160 tmpS = ~ZeroS; BOOST_CHECK(tmpS == ~ZeroS); tmpS = ~OneS; BOOST_CHECK(tmpS == ~OneS); tmpS = ~R1S; BOOST_CHECK(tmpS == ~R1S); tmpS = ~R2S; BOOST_CHECK(tmpS == ~R2S); @@ -165,8 +165,8 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality // Wrong length must throw exception. BOOST_CHECK_THROW(uint256(std::vector(OneArray,OneArray+31)), uint_error); BOOST_CHECK_THROW(uint256(std::vector(OneArray,OneArray+20)), uint_error); - BOOST_CHECK_THROW(uint160(std::vector(OneArray,OneArray+32)), uint_error); - BOOST_CHECK_THROW(uint160(std::vector(OneArray,OneArray+19)), uint_error); + BOOST_CHECK_THROW(arith_uint160(std::vector(OneArray,OneArray+32)), uint_error); + BOOST_CHECK_THROW(arith_uint160(std::vector(OneArray,OneArray+19)), uint_error); } void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) @@ -242,11 +242,11 @@ BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>=" BOOST_CHECK((c1L << i) == (c2L << (i-128))); } - uint160 TmpS; + arith_uint160 TmpS; for (unsigned int i = 0; i < 160; ++i) { shiftArrayLeft(TmpArray, OneArray, 20, i); - BOOST_CHECK(uint160(std::vector(TmpArray,TmpArray+20)) == (OneS << i)); + BOOST_CHECK(arith_uint160(std::vector(TmpArray,TmpArray+20)) == (OneS << i)); TmpS = OneS; TmpS <<= i; BOOST_CHECK(TmpS == (OneS << i)); BOOST_CHECK((HalfS >> (159-i)) == (OneS << i)); @@ -254,27 +254,27 @@ BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>=" BOOST_CHECK(TmpS == (OneS << i)); shiftArrayLeft(TmpArray, R1Array, 20, i); - BOOST_CHECK(uint160(std::vector(TmpArray,TmpArray+20)) == (R1S << i)); + BOOST_CHECK(arith_uint160(std::vector(TmpArray,TmpArray+20)) == (R1S << i)); TmpS = R1S; TmpS <<= i; BOOST_CHECK(TmpS == (R1S << i)); shiftArrayRight(TmpArray, R1Array, 20, i); - BOOST_CHECK(uint160(std::vector(TmpArray,TmpArray+20)) == (R1S >> i)); + BOOST_CHECK(arith_uint160(std::vector(TmpArray,TmpArray+20)) == (R1S >> i)); TmpS = R1S; TmpS >>= i; BOOST_CHECK(TmpS == (R1S >> i)); shiftArrayLeft(TmpArray, MaxArray, 20, i); - BOOST_CHECK(uint160(std::vector(TmpArray,TmpArray+20)) == (MaxS << i)); + BOOST_CHECK(arith_uint160(std::vector(TmpArray,TmpArray+20)) == (MaxS << i)); TmpS = MaxS; TmpS <<= i; BOOST_CHECK(TmpS == (MaxS << i)); shiftArrayRight(TmpArray, MaxArray, 20, i); - BOOST_CHECK(uint160(std::vector(TmpArray,TmpArray+20)) == (MaxS >> i)); + BOOST_CHECK(arith_uint160(std::vector(TmpArray,TmpArray+20)) == (MaxS >> i)); TmpS = MaxS; TmpS >>= i; BOOST_CHECK(TmpS == (MaxS >> i)); } - uint160 c1S = uint160(0x0123456789abcdefULL); - uint160 c2S = c1S << 80; + arith_uint160 c1S = arith_uint160(0x0123456789abcdefULL); + arith_uint160 c2S = c1S << 80; for (unsigned int i = 0; i < 80; ++i) { BOOST_CHECK((c1S << i) == (c2S >> (80-i))); } @@ -300,7 +300,7 @@ BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ - unsigned char TmpArray[32]; for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; } BOOST_CHECK(uint256(std::vector(TmpArray,TmpArray+32)) == (~R1L)); - BOOST_CHECK(uint160(std::vector(TmpArray,TmpArray+20)) == (~R1S)); + BOOST_CHECK(arith_uint160(std::vector(TmpArray,TmpArray+20)) == (~R1S)); BOOST_CHECK(-ZeroL == ZeroL); BOOST_CHECK(-ZeroS == ZeroS); BOOST_CHECK(-R1L == (~R1L)+1); @@ -318,7 +318,7 @@ BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ - for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \ BOOST_CHECK(uint256(std::vector(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L)); \ for (unsigned int i = 0; i < 20; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \ - BOOST_CHECK(uint160(std::vector(TmpArray,TmpArray+20)) == (_A_##S _OP_ _B_##S)); + BOOST_CHECK(arith_uint160(std::vector(TmpArray,TmpArray+20)) == (_A_##S _OP_ _B_##S)); #define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \ TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); \ @@ -345,7 +345,7 @@ BOOST_AUTO_TEST_CASE( bitwiseOperators ) CHECKBITWISEOPERATOR(Max,R1,&) uint256 TmpL; - uint160 TmpS; + arith_uint160 TmpS; CHECKASSIGNMENTOPERATOR(R1,R2,|) CHECKASSIGNMENTOPERATOR(R1,R2,^) CHECKASSIGNMENTOPERATOR(R1,R2,&) @@ -364,13 +364,13 @@ BOOST_AUTO_TEST_CASE( bitwiseOperators ) uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL; TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | uint256(Tmp64))); - TmpS = R1S; TmpS |= Tmp64; BOOST_CHECK(TmpS == (R1S | uint160(Tmp64))); + TmpS = R1S; TmpS |= Tmp64; BOOST_CHECK(TmpS == (R1S | arith_uint160(Tmp64))); TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L); TmpS = R1S; TmpS |= 0; BOOST_CHECK(TmpS == R1S); TmpL ^= 0; BOOST_CHECK(TmpL == R1L); TmpS ^= 0; BOOST_CHECK(TmpS == R1S); TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ uint256(Tmp64))); - TmpS ^= Tmp64; BOOST_CHECK(TmpS == (R1S ^ uint160(Tmp64))); + TmpS ^= Tmp64; BOOST_CHECK(TmpS == (R1S ^ arith_uint160(Tmp64))); } BOOST_AUTO_TEST_CASE( comparison ) // <= >= < > @@ -385,7 +385,7 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < > BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL)); BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL)); } - uint160 TmpS; + arith_uint160 TmpS; for (unsigned int i = 0; i < 160; ++i) { TmpS= OneS<< i; BOOST_CHECK( TmpS >= ZeroS && TmpS > ZeroS && ZeroS < TmpS && ZeroS <= TmpS); @@ -441,8 +441,8 @@ BOOST_AUTO_TEST_CASE( plusMinus ) BOOST_CHECK(--TmpL == R1L-1); // 160-bit; copy-pasted - uint160 TmpS = 0; - BOOST_CHECK(R1S+R2S == uint160(R1LplusR2L)); + arith_uint160 TmpS = 0; + BOOST_CHECK(R1S+R2S == arith_uint160(R1LplusR2L)); TmpS += R1S; BOOST_CHECK(TmpS == R1S); TmpS += R2S; @@ -460,9 +460,9 @@ BOOST_AUTO_TEST_CASE( plusMinus ) BOOST_CHECK( TmpS++ == (MaxS>>i) ); BOOST_CHECK( TmpS == (HalfS >> (i-1))); } - BOOST_CHECK(uint160(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == uint160(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); - TmpS = uint160(0xbedc77e27940a7ULL); TmpS += 0xee8d836fce66fbULL; - BOOST_CHECK(TmpS == uint160(0xbedc77e27940a7ULL+0xee8d836fce66fbULL)); + BOOST_CHECK(arith_uint160(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint160(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); + TmpS = arith_uint160(0xbedc77e27940a7ULL); TmpS += 0xee8d836fce66fbULL; + BOOST_CHECK(TmpS == arith_uint160(0xbedc77e27940a7ULL+0xee8d836fce66fbULL)); TmpS -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpS == 0xbedc77e27940a7ULL); TmpS = R1S; BOOST_CHECK(++TmpS == R1S+1); @@ -538,8 +538,8 @@ BOOST_AUTO_TEST_CASE( divide ) BOOST_CHECK(MaxL / R2L == 1); BOOST_CHECK_THROW(R2L / ZeroL, uint_error); - uint160 D1S("D3C5EDCDEA54EB92679F0A4B4"); - uint160 D2S("13037"); + arith_uint160 D1S("D3C5EDCDEA54EB92679F0A4B4"); + arith_uint160 D2S("13037"); BOOST_CHECK((R1S / D1S).ToString() == "0000000000000000000000000db9af3beade6c02"); BOOST_CHECK((R1S / D2S).ToString() == "000098dfb6cc40ca592bf74366794f298ada205c"); BOOST_CHECK(R1S / OneS == R1S); @@ -614,7 +614,7 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G BOOST_CHECK(R2S.GetHex() == R2S.ToString()); BOOST_CHECK(OneS.GetHex() == OneS.ToString()); BOOST_CHECK(MaxS.GetHex() == MaxS.ToString()); - uint160 TmpS(R1S); + arith_uint160 TmpS(R1S); BOOST_CHECK(TmpS == R1S); TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S); TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == 0); From dea250cf92700a49658c6af503e8b8cce00687e7 Mon Sep 17 00:00:00 2001 From: random-zebra Date: Sun, 20 Dec 2020 19:50:35 +0100 Subject: [PATCH 3/3] [Core] Migrate uint160 (CKeyID/CScriptID) to opaque blobs --- src/blob_uint256.h | 11 ----------- src/uint256.h | 14 +++++++------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/blob_uint256.h b/src/blob_uint256.h index 51251fc421a1..e974c5442d55 100644 --- a/src/blob_uint256.h +++ b/src/blob_uint256.h @@ -113,17 +113,6 @@ class blob88 : public base_blob<88> { explicit blob88(const std::vector& vch) : base_blob<88>(vch) {} }; -/** 160-bit opaque blob. - * @note This type is called uint160 for historical reasons only. It is an opaque - * blob of 160 bits and has no integer operations. - */ -class blob_uint160 : public base_blob<160> { -public: - blob_uint160() {} - blob_uint160(const base_blob<160>& b) : base_blob<160>(b) {} - explicit blob_uint160(const std::vector& vch) : base_blob<160>(vch) {} -}; - /** 256-bit opaque blob. * @note This type is called uint256 for historical reasons only. It is an * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if diff --git a/src/uint256.h b/src/uint256.h index 5c690c79fd6b..564dd7a3751a 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -22,15 +22,15 @@ // with the blob_uint256 file. // -/** 160-bit unsigned big integer. */ -class uint160 : public base_uint<160> -{ +/** 160-bit opaque blob. + * @note This type is called uint160 for historical reasons only. It is an opaque + * blob of 160 bits and has no integer operations. + */ +class uint160 : public base_blob<160> { public: uint160() {} - uint160(const base_uint<160>& b) : base_uint<160>(b) {} - uint160(uint64_t b) : base_uint<160>(b) {} - explicit uint160(const std::string& str) : base_uint<160>(str) {} - explicit uint160(const std::vector& vch) : base_uint<160>(vch) {} + uint160(const base_blob<160>& b) : base_blob<160>(b) {} + explicit uint160(const std::vector& vch) : base_blob<160>(vch) {} }; /** 256-bit unsigned big integer. */