diff options
-rw-r--r-- | contrib/debian/bitcoin-qt.desktop | 4 | ||||
-rw-r--r-- | contrib/debian/changelog | 7 | ||||
-rw-r--r-- | qa/rpc-tests/util.sh | 6 | ||||
-rwxr-xr-x | qa/rpc-tests/wallet.sh | 25 | ||||
-rw-r--r-- | src/Makefile.am | 5 | ||||
-rw-r--r-- | src/addrman.cpp | 8 | ||||
-rw-r--r-- | src/chainparams.cpp | 1 | ||||
-rw-r--r-- | src/init.cpp | 4 | ||||
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/qt/addresstablemodel.cpp | 37 | ||||
-rw-r--r-- | src/qt/bitcoingui.cpp | 7 | ||||
-rw-r--r-- | src/qt/coincontroldialog.cpp | 12 | ||||
-rw-r--r-- | src/qt/optionsmodel.cpp | 5 | ||||
-rw-r--r-- | src/qt/optionsmodel.h | 1 | ||||
-rw-r--r-- | src/qt/paymentserver.cpp | 1 | ||||
-rw-r--r-- | src/qt/sendcoinsdialog.cpp | 57 | ||||
-rw-r--r-- | src/qt/sendcoinsentry.cpp | 7 | ||||
-rw-r--r-- | src/qt/walletmodel.cpp | 5 | ||||
-rw-r--r-- | src/rpcdump.cpp | 2 | ||||
-rw-r--r-- | src/rpcmining.cpp | 5 | ||||
-rw-r--r-- | src/sync.cpp | 6 | ||||
-rw-r--r-- | src/sync.h | 5 | ||||
-rw-r--r-- | src/test/Makefile.am | 2 | ||||
-rw-r--r-- | src/test/uint160_tests.cpp | 20 | ||||
-rw-r--r-- | src/test/uint256_tests.cpp | 634 | ||||
-rw-r--r-- | src/txdb.cpp | 12 | ||||
-rw-r--r-- | src/ui_interface.h | 2 | ||||
-rw-r--r-- | src/uint256.h | 157 | ||||
-rw-r--r-- | src/wallet.cpp | 18 | ||||
-rw-r--r-- | src/wallet.h | 13 |
30 files changed, 784 insertions, 286 deletions
diff --git a/contrib/debian/bitcoin-qt.desktop b/contrib/debian/bitcoin-qt.desktop index 82bc80a734..61e1aca6ad 100644 --- a/contrib/debian/bitcoin-qt.desktop +++ b/contrib/debian/bitcoin-qt.desktop @@ -4,9 +4,9 @@ Name=Bitcoin Comment=Bitcoin P2P Cryptocurrency Comment[fr]=Bitcoin, monnaie virtuelle cryptographique pair à pair Comment[tr]=Bitcoin, eşten eşe kriptografik sanal para birimi -Exec=/usr/bin/bitcoin-qt %u +Exec=bitcoin-qt %u Terminal=false Type=Application -Icon=/usr/share/pixmaps/bitcoin128.png +Icon=bitcoin128 MimeType=x-scheme-handler/bitcoin; Categories=Office;Finance; diff --git a/contrib/debian/changelog b/contrib/debian/changelog index bd6b42dc51..95b9ad31a3 100644 --- a/contrib/debian/changelog +++ b/contrib/debian/changelog @@ -1,3 +1,10 @@ +bitcoin (0.8.6-precise1) precise; urgency=medium + + * New upstream release. + * Make .desktop paths non-fixed (suggested by prusnak@github) + + -- Matt Corallo <matt@bluematt.me> Fri, 13 Dec 2013 13:31:00 -0400 + bitcoin (0.8.5-precise1) precise; urgency=medium * New upstream release. diff --git a/qa/rpc-tests/util.sh b/qa/rpc-tests/util.sh index dc2a319970..e4e3953748 100644 --- a/qa/rpc-tests/util.sh +++ b/qa/rpc-tests/util.sh @@ -82,3 +82,9 @@ function CreateTxn1 { function SendRawTxn { $CLI $1 sendrawtransaction $2 } + +# Use: GetBlocks <datadir> +# returns number of blocks from getinfo +function GetBlocks { + ExtractKey blocks "$( $CLI $1 getinfo )" +} diff --git a/qa/rpc-tests/wallet.sh b/qa/rpc-tests/wallet.sh index dd511782d0..118809a265 100755 --- a/qa/rpc-tests/wallet.sh +++ b/qa/rpc-tests/wallet.sh @@ -37,12 +37,27 @@ B3PID=$! trap "kill -9 $B1PID $B2PID $B3PID; rm -rf $D" EXIT +# Wait until all three nodes are at the same block number +function WaitBlocks { + while : + do + sleep 1 + BLOCKS1=$( GetBlocks $B1ARGS ) + BLOCKS2=$( GetBlocks $B2ARGS ) + BLOCKS3=$( GetBlocks $B3ARGS ) + if (( $BLOCKS1 == $BLOCKS2 && $BLOCKS2 == $BLOCKS3 )) + then + break + fi + done +} + # 1 block, 50 XBT each == 50 XBT $CLI $B1ARGS setgenerate true 1 -sleep 1 # sleep is necessary to let block broadcast +WaitBlocks # 101 blocks, 1 mature == 50 XBT $CLI $B2ARGS setgenerate true 101 -sleep 1 +WaitBlocks CheckBalance $B1ARGS 50 CheckBalance $B2ARGS 50 @@ -56,11 +71,11 @@ Send $B1ARGS $B3ARGS 10 # Have B1 mine a new block, and mature it # to recover transaction fees $CLI $B1ARGS setgenerate true 1 -sleep 1 +WaitBlocks # Have B2 mine 100 blocks so B1's block is mature: $CLI $B2ARGS setgenerate true 100 -sleep 1 +WaitBlocks # B1 should end up with 100 XBT in block rewards plus fees, # minus the 21 XBT sent to B3: @@ -77,7 +92,7 @@ RAWTXID2=$(SendRawTxn $B2ARGS $RAW2) # Have B2 mine a block to confirm transactions: $CLI $B2ARGS setgenerate true 1 -sleep 1 # allow time for block to be relayed +WaitBlocks # Check balances after confirmation CheckBalance $B1ARGS 0 diff --git a/src/Makefile.am b/src/Makefile.am index 5ce1c8e1b6..85d7c3c9c6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -93,8 +93,7 @@ nodist_libbitcoin_common_a_SOURCES = $(top_srcdir)/src/obj/build.h # # bitcoind binary # -bitcoind_LDADD = libbitcoin_server.a libbitcoin_cli.a libbitcoin_common.a leveldb/libleveldb.a leveldb/libmemenv.a \ - $(BOOST_LIBS) +bitcoind_LDADD = libbitcoin_server.a libbitcoin_cli.a libbitcoin_common.a leveldb/libleveldb.a leveldb/libmemenv.a if ENABLE_WALLET bitcoind_LDADD += libbitcoin_wallet.a endif @@ -106,7 +105,7 @@ bitcoind_SOURCES += bitcoind-res.rc endif AM_CPPFLAGS += $(BDB_CPPFLAGS) -bitcoind_LDADD += $(BDB_LIBS) +bitcoind_LDADD += $(BOOST_LIBS) $(BDB_LIBS) # bitcoin-cli binary # bitcoin_cli_LDADD = libbitcoin_cli.a libbitcoin_common.a $(BOOST_LIBS) diff --git a/src/addrman.cpp b/src/addrman.cpp index 815da07c9b..46b4a94938 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -14,12 +14,12 @@ int CAddrInfo::GetTriedBucket(const std::vector<unsigned char> &nKey) const CDataStream ss1(SER_GETHASH, 0); std::vector<unsigned char> vchKey = GetKey(); ss1 << nKey << vchKey; - uint64_t hash1 = Hash(ss1.begin(), ss1.end()).Get64(); + uint64_t hash1 = Hash(ss1.begin(), ss1.end()).GetLow64(); CDataStream ss2(SER_GETHASH, 0); std::vector<unsigned char> vchGroupKey = GetGroup(); ss2 << nKey << vchGroupKey << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP); - uint64_t hash2 = Hash(ss2.begin(), ss2.end()).Get64(); + uint64_t hash2 = Hash(ss2.begin(), ss2.end()).GetLow64(); return hash2 % ADDRMAN_TRIED_BUCKET_COUNT; } @@ -29,11 +29,11 @@ int CAddrInfo::GetNewBucket(const std::vector<unsigned char> &nKey, const CNetAd std::vector<unsigned char> vchGroupKey = GetGroup(); std::vector<unsigned char> vchSourceGroupKey = src.GetGroup(); ss1 << nKey << vchGroupKey << vchSourceGroupKey; - uint64_t hash1 = Hash(ss1.begin(), ss1.end()).Get64(); + uint64_t hash1 = Hash(ss1.begin(), ss1.end()).GetLow64(); CDataStream ss2(SER_GETHASH, 0); ss2 << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP); - uint64_t hash2 = Hash(ss2.begin(), ss2.end()).Get64(); + uint64_t hash2 = Hash(ss2.begin(), ss2.end()).GetLow64(); return hash2 % ADDRMAN_NEW_BUCKET_COUNT; } diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 14da3860ad..d3d6b98d80 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -143,6 +143,7 @@ public: vSeeds.push_back(CDNSSeedData("bitcoin.sipa.be", "seed.bitcoin.sipa.be")); vSeeds.push_back(CDNSSeedData("bluematt.me", "dnsseed.bluematt.me")); vSeeds.push_back(CDNSSeedData("dashjr.org", "dnsseed.bitcoin.dashjr.org")); + vSeeds.push_back(CDNSSeedData("bitcoinstats.com", "seed.bitcoinstats.com")); vSeeds.push_back(CDNSSeedData("xf2.org", "bitseed.xf2.org")); base58Prefixes[PUBKEY_ADDRESS] = list_of(0); diff --git a/src/init.cpp b/src/init.cpp index 275985604e..20279bf733 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -164,13 +164,13 @@ void HandleSIGHUP(int) bool static InitError(const std::string &str) { - uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_ERROR); + uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_ERROR | CClientUIInterface::NOSHOWGUI); return false; } bool static InitWarning(const std::string &str) { - uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_WARNING); + uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_WARNING | CClientUIInterface::NOSHOWGUI); return true; } diff --git a/src/main.cpp b/src/main.cpp index e3f9fdd843..017a0768d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2235,7 +2235,7 @@ void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd) bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp) { - AssertLockHeld("cs_main"); + AssertLockHeld(cs_main); // Check for duplicate uint256 hash = pblock->GetHash(); diff --git a/src/qt/addresstablemodel.cpp b/src/qt/addresstablemodel.cpp index 5e7d8e6178..2987e5fdda 100644 --- a/src/qt/addresstablemodel.cpp +++ b/src/qt/addresstablemodel.cpp @@ -244,33 +244,34 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value, if(role == Qt::EditRole) { - switch(index.column()) + LOCK(wallet->cs_wallet); /* For SetAddressBook / DelAddressBook */ + CTxDestination curAddress = CBitcoinAddress(rec->address.toStdString()).Get(); + if(index.column() == Label) { - case Label: // Do nothing, if old label == new label if(rec->label == value.toString()) { editStatus = NO_CHANGES; return false; } - wallet->SetAddressBook(CBitcoinAddress(rec->address.toStdString()).Get(), value.toString().toStdString(), strPurpose); - break; - case Address: - // Do nothing, if old address == new address - if(CBitcoinAddress(rec->address.toStdString()) == CBitcoinAddress(value.toString().toStdString())) + wallet->SetAddressBook(curAddress, value.toString().toStdString(), strPurpose); + } else if(index.column() == Address) { + CTxDestination newAddress = CBitcoinAddress(value.toString().toStdString()).Get(); + // Refuse to set invalid address, set error status and return false + if(boost::get<CNoDestination>(&newAddress)) { - editStatus = NO_CHANGES; + editStatus = INVALID_ADDRESS; return false; } - // Refuse to set invalid address, set error status and return false - else if(!walletModel->validateAddress(value.toString())) + // Do nothing, if old address == new address + else if(newAddress == curAddress) { - editStatus = INVALID_ADDRESS; + editStatus = NO_CHANGES; return false; } // Check for duplicate addresses to prevent accidental deletion of addresses, if you try // to paste an existing address over another address (with a different label) - else if(wallet->mapAddressBook.count(CBitcoinAddress(value.toString().toStdString()).Get())) + else if(wallet->mapAddressBook.count(newAddress)) { editStatus = DUPLICATE_ADDRESS; return false; @@ -278,15 +279,11 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value, // Double-check that we're not overwriting a receiving address else if(rec->type == AddressTableEntry::Sending) { - { - LOCK(wallet->cs_wallet); - // Remove old entry - wallet->DelAddressBook(CBitcoinAddress(rec->address.toStdString()).Get()); - // Add new entry with new address - wallet->SetAddressBook(CBitcoinAddress(value.toString().toStdString()).Get(), rec->label.toStdString(), strPurpose); - } + // Remove old entry + wallet->DelAddressBook(curAddress); + // Add new entry with new address + wallet->SetAddressBook(newAddress, rec->label.toStdString(), strPurpose); } - break; } return true; } diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 2d0f51a3fb..6be5a64015 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -684,8 +684,11 @@ void BitcoinGUI::message(const QString &title, const QString &message, unsigned if (!(buttons = (QMessageBox::StandardButton)(style & CClientUIInterface::BTN_MASK))) buttons = QMessageBox::Ok; - // Ensure we get users attention - showNormalIfMinimized(); + // Ensure we get users attention, but only if main window is visible + // as we don't want to pop up the main window for messages that happen before + // initialization is finished. + if(!(style & CClientUIInterface::NOSHOWGUI)) + showNormalIfMinimized(); QMessageBox mBox((QMessageBox::Icon)nMBoxIcon, strTitle, message, buttons, this); int r = mBox.exec(); if (ret != NULL) diff --git a/src/qt/coincontroldialog.cpp b/src/qt/coincontroldialog.cpp index b4e6aeb1dd..e1a9140f45 100644 --- a/src/qt/coincontroldialog.cpp +++ b/src/qt/coincontroldialog.cpp @@ -449,7 +449,7 @@ void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog) } } - QString sPriorityLabel = ""; + QString sPriorityLabel = tr("none"); int64_t nAmount = 0; int64_t nPayFee = 0; int64_t nAfterFee = 0; @@ -593,10 +593,10 @@ void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog) l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change // turn labels "red" - l5->setStyleSheet((nBytes >= 1000) ? "color:red;" : ""); // Bytes >= 1000 - l6->setStyleSheet((!AllowFree(dPriority)) ? "color:red;" : ""); // Priority < "medium" - l7->setStyleSheet((fLowOutput) ? "color:red;" : ""); // Low Output = "yes" - l8->setStyleSheet((nChange > 0 && nChange < CENT) ? "color:red;" : ""); // Change < 0.01BTC + l5->setStyleSheet((nBytes >= 1000) ? "color:red;" : ""); // Bytes >= 1000 + l6->setStyleSheet((dPriority > 0 && !AllowFree(dPriority)) ? "color:red;" : ""); // Priority < "medium" + l7->setStyleSheet((fLowOutput) ? "color:red;" : ""); // Low Output = "yes" + l8->setStyleSheet((nChange > 0 && nChange < CENT) ? "color:red;" : ""); // Change < 0.01BTC // tool tips QString toolTip1 = tr("This label turns red, if the transaction size is greater than 1000 bytes.") + "<br /><br />"; @@ -604,7 +604,7 @@ void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog) toolTip1 += tr("Can vary +/- 1 byte per input."); QString toolTip2 = tr("Transactions with higher priority are more likely to get included into a block.") + "<br /><br />"; - toolTip2 += tr("This label turns red, if the priority is smaller than \"medium\"") + "<br /><br />"; + toolTip2 += tr("This label turns red, if the priority is smaller than \"medium\".") + "<br /><br />"; toolTip2 += tr("This means a fee of at least %1 per kB is required.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CTransaction::nMinTxFee)); QString toolTip3 = tr("This label turns red, if any recipient receives an amount smaller than %1.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CENT)) + "<br /><br />"; diff --git a/src/qt/optionsmodel.cpp b/src/qt/optionsmodel.cpp index 363f432d62..b64b0dff8b 100644 --- a/src/qt/optionsmodel.cpp +++ b/src/qt/optionsmodel.cpp @@ -307,11 +307,6 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in return successful; } -qint64 OptionsModel::getTransactionFee() -{ - return (qint64) nTransactionFee; -} - bool OptionsModel::getProxySettings(QString& proxyIP, quint16 &proxyPort) const { std::string proxy = GetArg("-proxy", ""); diff --git a/src/qt/optionsmodel.h b/src/qt/optionsmodel.h index 5bb563cc36..a50153c768 100644 --- a/src/qt/optionsmodel.h +++ b/src/qt/optionsmodel.h @@ -48,7 +48,6 @@ public: bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole); /* Explicit getters */ - qint64 getTransactionFee(); bool getMinimizeToTray() { return fMinimizeToTray; } bool getMinimizeOnClose() { return fMinimizeOnClose; } int getDisplayUnit() { return nDisplayUnit; } diff --git a/src/qt/paymentserver.cpp b/src/qt/paymentserver.cpp index ba5c06064f..cfa87c16ba 100644 --- a/src/qt/paymentserver.cpp +++ b/src/qt/paymentserver.cpp @@ -548,6 +548,7 @@ void PaymentServer::fetchPaymentACK(CWallet* wallet, SendCoinsRecipient recipien else { CPubKey newKey; if (wallet->GetKeyFromPool(newKey)) { + LOCK(wallet->cs_wallet); // SetAddressBook CKeyID keyID = newKey.GetID(); wallet->SetAddressBook(keyID, strAccount, "refund"); diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp index 4bf5c3c479..92be835c56 100644 --- a/src/qt/sendcoinsdialog.cpp +++ b/src/qt/sendcoinsdialog.cpp @@ -546,44 +546,45 @@ void SendCoinsDialog::coinControlChangeChecked(int state) // Coin Control: custom change address changed void SendCoinsDialog::coinControlChangeEdited(const QString& text) { - if (model) + if (model && model->getAddressTableModel()) { - CoinControlDialog::coinControl->destChange = CBitcoinAddress(text.toStdString()).Get(); + // Default to no change address until verified + CoinControlDialog::coinControl->destChange = CNoDestination(); + ui->labelCoinControlChangeLabel->setStyleSheet("QLabel{color:red;}"); + + CBitcoinAddress addr = CBitcoinAddress(text.toStdString()); - // label for the change address - ui->labelCoinControlChangeLabel->setStyleSheet("QLabel{color:black;}"); - if (text.isEmpty()) + if (text.isEmpty()) // Nothing entered + { ui->labelCoinControlChangeLabel->setText(""); - else if (!CBitcoinAddress(text.toStdString()).IsValid()) + } + else if (!addr.IsValid()) // Invalid address { - // invalid change address - CoinControlDialog::coinControl->destChange = CNoDestination(); - ui->lineEditCoinControlChange->setValid(false); - ui->labelCoinControlChangeLabel->setStyleSheet("QLabel{color:red;}"); ui->labelCoinControlChangeLabel->setText(tr("Warning: Invalid Bitcoin address")); } - else + else // Valid address { - QString associatedLabel = model->getAddressTableModel()->labelForAddress(text); - if (!associatedLabel.isEmpty()) - ui->labelCoinControlChangeLabel->setText(associatedLabel); - else + CPubKey pubkey; + CKeyID keyid; + addr.GetKeyID(keyid); + if (!model->getPubKey(keyid, pubkey)) // Unknown change address { - CPubKey pubkey; - CKeyID keyid; - CBitcoinAddress(text.toStdString()).GetKeyID(keyid); - if (model->getPubKey(keyid, pubkey)) - ui->labelCoinControlChangeLabel->setText(tr("(no label)")); + ui->lineEditCoinControlChange->setValid(false); + ui->labelCoinControlChangeLabel->setText(tr("Warning: Unknown change address")); + } + else // Known change address + { + ui->labelCoinControlChangeLabel->setStyleSheet("QLabel{color:black;}"); + + // Query label + QString associatedLabel = model->getAddressTableModel()->labelForAddress(text); + if (!associatedLabel.isEmpty()) + ui->labelCoinControlChangeLabel->setText(associatedLabel); else - { - // unknown change address - CoinControlDialog::coinControl->destChange = CNoDestination(); - - ui->lineEditCoinControlChange->setValid(false); - ui->labelCoinControlChangeLabel->setStyleSheet("QLabel{color:red;}"); - ui->labelCoinControlChangeLabel->setText(tr("Warning: Unknown change address")); - } + ui->labelCoinControlChangeLabel->setText(tr("(no label)")); + + CoinControlDialog::coinControl->destChange = addr.Get(); } } } diff --git a/src/qt/sendcoinsentry.cpp b/src/qt/sendcoinsentry.cpp index 4d28f68861..3f5d0cda3a 100644 --- a/src/qt/sendcoinsentry.cpp +++ b/src/qt/sendcoinsentry.cpp @@ -155,11 +155,12 @@ SendCoinsRecipient SendCoinsEntry::getValue() QWidget *SendCoinsEntry::setupTabChain(QWidget *prev) { QWidget::setTabOrder(prev, ui->payTo); - QWidget::setTabOrder(ui->payTo, ui->addressBookButton); + QWidget::setTabOrder(ui->payTo, ui->addAsLabel); + QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel); + QWidget::setTabOrder(w, ui->addressBookButton); QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton); QWidget::setTabOrder(ui->pasteButton, ui->deleteButton); - QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel); - return ui->payAmount->setupTabChain(ui->addAsLabel); + return ui->deleteButton; } void SendCoinsEntry::setValue(const SendCoinsRecipient &value) diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index f08342b83e..6f3e3b0aaf 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -502,6 +502,7 @@ void WalletModel::listCoins(std::map<QString, std::vector<COutput> >& mapCoins) std::vector<COutput> vCoins; wallet->AvailableCoins(vCoins); + LOCK(wallet->cs_wallet); // ListLockedCoins, mapWallet std::vector<COutPoint> vLockedCoins; wallet->ListLockedCoins(vLockedCoins); @@ -531,20 +532,24 @@ void WalletModel::listCoins(std::map<QString, std::vector<COutput> >& mapCoins) bool WalletModel::isLockedCoin(uint256 hash, unsigned int n) const { + LOCK(wallet->cs_wallet); return wallet->IsLockedCoin(hash, n); } void WalletModel::lockCoin(COutPoint& output) { + LOCK(wallet->cs_wallet); wallet->LockCoin(output); } void WalletModel::unlockCoin(COutPoint& output) { + LOCK(wallet->cs_wallet); wallet->UnlockCoin(output); } void WalletModel::listLockedCoins(std::vector<COutPoint>& vOutpts) { + LOCK(wallet->cs_wallet); wallet->ListLockedCoins(vOutpts); } diff --git a/src/rpcdump.cpp b/src/rpcdump.cpp index 92f4c2c6dd..c801b284cb 100644 --- a/src/rpcdump.cpp +++ b/src/rpcdump.cpp @@ -86,6 +86,8 @@ Value importprivkey(const Array& params, bool fHelp) + HelpExampleRpc("importprivkey", "\"mykey\", \"testing\", false") ); + EnsureWalletIsUnlocked(); + string strSecret = params[0].get_str(); string strLabel = ""; if (params.size() > 1) diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 1a8462fdea..564f0401f1 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -55,7 +55,10 @@ void ShutdownRPCMining() // or from the last difficulty change if 'lookup' is nonpositive. // If 'height' is nonnegative, compute the estimate at the time when a given block was found. Value GetNetworkHashPS(int lookup, int height) { - CBlockIndex *pb = chainActive[height]; + CBlockIndex *pb = chainActive.Tip(); + + if (height >= 0 && height < chainActive.Height()) + pb = chainActive[height]; if (pb == NULL || !pb->nHeight) return 0; diff --git a/src/sync.cpp b/src/sync.cpp index 9a20c87f88..b57d8c3c00 100644 --- a/src/sync.cpp +++ b/src/sync.cpp @@ -136,11 +136,11 @@ std::string LocksHeld() return result; } -void AssertLockHeld(std::string strName) +void AssertLockHeldInternal(const char *pszName, const char* pszFile, int nLine, void *cs) { BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)&i, *lockstack) - if (i.second.MutexName() == strName) return; - LogPrintf("Lock %s not held; locks held:\n%s", strName.c_str(), LocksHeld().c_str()); + if (i.first == cs) return; + LogPrintf("Lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str()); assert(0); } diff --git a/src/sync.h b/src/sync.h index c50abf81b6..077ed59b89 100644 --- a/src/sync.h +++ b/src/sync.h @@ -88,12 +88,13 @@ typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection; void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false); void LeaveCritical(); std::string LocksHeld(); -void AssertLockHeld(std::string strName); +void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void *cs); #else void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {} void static inline LeaveCritical() {} -void static inline AssertLockHeld(std::string) {} +void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void *cs) {} #endif +#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) #ifdef DEBUG_LOCKCONTENTION void PrintLockContention(const char* pszName, const char* pszFile, int nLine); diff --git a/src/test/Makefile.am b/src/test/Makefile.am index ccc8da1690..384616e897 100644 --- a/src/test/Makefile.am +++ b/src/test/Makefile.am @@ -34,7 +34,7 @@ test_bitcoin_SOURCES = alert_tests.cpp \ key_tests.cpp miner_tests.cpp mruset_tests.cpp multisig_tests.cpp \ netbase_tests.cpp pmt_tests.cpp rpc_tests.cpp script_P2SH_tests.cpp \ script_tests.cpp serialize_tests.cpp sigopcount_tests.cpp test_bitcoin.cpp \ - transaction_tests.cpp uint160_tests.cpp uint256_tests.cpp util_tests.cpp \ + transaction_tests.cpp uint256_tests.cpp util_tests.cpp \ sighash_tests.cpp $(JSON_TEST_FILES) $(RAW_TEST_FILES) if ENABLE_WALLET diff --git a/src/test/uint160_tests.cpp b/src/test/uint160_tests.cpp deleted file mode 100644 index 87418fabec..0000000000 --- a/src/test/uint160_tests.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "uint256.h" - -#include <stdint.h> - -#include <boost/test/unit_test.hpp> - -BOOST_AUTO_TEST_SUITE(uint160_tests) - -BOOST_AUTO_TEST_CASE(uint160_equality) -{ - uint160 num1 = 10; - uint160 num2 = 11; - BOOST_CHECK(num1+1 == num2); - - uint64_t num3 = 10; - BOOST_CHECK(num1 == num3); - BOOST_CHECK(num1+num2 == num3+num2); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp index 188635dcbd..368484fdff 100644 --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -1,29 +1,633 @@ +#include <boost/test/unit_test.hpp> +#include <stdint.h> +#include <sstream> +#include <iomanip> +#include <limits> +#include <cmath> #include "uint256.h" #include <string> +#include "version.h" -#include <stdint.h> +BOOST_AUTO_TEST_SUITE(uint256_tests) + +const unsigned char R1Array[] = + "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2" + "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"; +const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; +const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256 +const double R1Sdouble = 0.7096329412477836074; +const uint256 R1L = uint256(std::vector<unsigned char>(R1Array,R1Array+32)); +const uint160 R1S = uint160(std::vector<unsigned char>(R1Array,R1Array+20)); +const uint64_t R1LLow64 = 0x121156cfdb4a529cULL; -#include <boost/test/unit_test.hpp> +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<unsigned char>(R2Array,R2Array+32)); +const uint160 R2S = uint160(std::vector<unsigned char>(R2Array,R2Array+20)); -BOOST_AUTO_TEST_SUITE(uint256_tests) +const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C"; + +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<unsigned char>(ZeroArray,ZeroArray+32)); +const uint160 ZeroS = uint160(std::vector<unsigned char>(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<unsigned char>(OneArray,OneArray+32)); +const uint160 OneS = uint160(std::vector<unsigned char>(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<unsigned char>(MaxArray,MaxArray+32)); +const uint160 MaxS = uint160(std::vector<unsigned char>(MaxArray,MaxArray+20)); + +const uint256 HalfL = (OneL << 255); +const uint160 HalfS = (OneS << 159); +std::string ArrayToString(const unsigned char A[], unsigned int width) +{ + std::stringstream Stream; + Stream << std::hex; + for (unsigned int i = 0; i < width; ++i) + { + Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1]; + } + return Stream.str(); +} -BOOST_AUTO_TEST_CASE(uint256_equality) +BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality { - uint256 num1 = 10; - uint256 num2 = 11; - BOOST_CHECK(num1+1 == num2); + BOOST_CHECK(1 == 0+1); + // constructor uint256(vector<char>): + BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32)); + BOOST_CHECK(R1S.ToString() == ArrayToString(R1Array,20)); + BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32)); + BOOST_CHECK(R2S.ToString() == ArrayToString(R2Array,20)); + BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32)); + BOOST_CHECK(ZeroS.ToString() == ArrayToString(ZeroArray,20)); + BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32)); + BOOST_CHECK(OneS.ToString() == ArrayToString(OneArray,20)); + BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32)); + BOOST_CHECK(MaxS.ToString() == ArrayToString(MaxArray,20)); + BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32)); + BOOST_CHECK(OneS.ToString() != ArrayToString(ZeroArray,20)); + + // == and != + BOOST_CHECK(R1L != R2L && R1S != R2S); + BOOST_CHECK(ZeroL != OneL && ZeroS != OneS); + BOOST_CHECK(OneL != ZeroL && OneS != ZeroS); + BOOST_CHECK(MaxL != ZeroL && MaxS != ZeroS); + BOOST_CHECK(~MaxL == ZeroL && ~MaxS == ZeroS); + BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L); + BOOST_CHECK( ((R1S ^ R2S) ^ R1S) == R2S); + + uint64_t Tmp64 = 0xc4dab720d9c7acaaULL; + for (unsigned int i = 0; i < 256; ++i) + { + BOOST_CHECK(ZeroL != (OneL << i)); + BOOST_CHECK((OneL << i) != ZeroL); + BOOST_CHECK(R1L != (R1L ^ (OneL << i))); + BOOST_CHECK(((uint256(Tmp64) ^ (OneL << i) ) != Tmp64 )); + } + BOOST_CHECK(ZeroL == (OneL << 256)); + + for (unsigned int i = 0; i < 160; ++i) + { + 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(ZeroS == (OneS << 256)); + + // String Constructor and Copy Constructor + BOOST_CHECK(uint256("0x"+R1L.ToString()) == R1L); + BOOST_CHECK(uint256("0x"+R2L.ToString()) == R2L); + BOOST_CHECK(uint256("0x"+ZeroL.ToString()) == ZeroL); + BOOST_CHECK(uint256("0x"+OneL.ToString()) == OneL); + BOOST_CHECK(uint256("0x"+MaxL.ToString()) == MaxL); + BOOST_CHECK(uint256(R1L.ToString()) == R1L); + BOOST_CHECK(uint256(" 0x"+R1L.ToString()+" ") == R1L); + BOOST_CHECK(uint256("") == ZeroL); + BOOST_CHECK(R1L == uint256(R1ArrayHex)); + BOOST_CHECK(uint256(R1L) == R1L); + BOOST_CHECK((uint256(R1L^R2L)^R2L) == R1L); + 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); + + // uint64_t constructor + BOOST_CHECK( (R1L & uint256("0xffffffffffffffff")) == uint256(R1LLow64)); + BOOST_CHECK(ZeroL == uint256(0)); + 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)); + + // Assignment (from base_uint) + uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL); + tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL); + 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); + tmpS = ~OneS; BOOST_CHECK(tmpS == ~OneS); + tmpS = ~R1S; BOOST_CHECK(tmpS == ~R1S); + tmpS = ~R2S; BOOST_CHECK(tmpS == ~R2S); + tmpS = ~MaxS; BOOST_CHECK(tmpS == ~MaxS); - uint64_t num3 = 10; - BOOST_CHECK(num1 == num3); - BOOST_CHECK(num1+num2 == num3+num2); + // Wrong length must give 0 + BOOST_CHECK(uint256(std::vector<unsigned char>(OneArray,OneArray+31)) == 0); + BOOST_CHECK(uint256(std::vector<unsigned char>(OneArray,OneArray+20)) == 0); + BOOST_CHECK(uint160(std::vector<unsigned char>(OneArray,OneArray+32)) == 0); + BOOST_CHECK(uint160(std::vector<unsigned char>(OneArray,OneArray+19)) == 0); } -BOOST_AUTO_TEST_CASE(uint256_hex) +void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) { - std::string hexStr = "d35583ed493a5eee756931353144f558e6a9ab3ad6024a63ced7f10daf7faad9"; - uint256 num1; - num1.SetHex(hexStr); - BOOST_CHECK(num1.GetHex() == hexStr); + for (unsigned int T=0; T < arrayLength; ++T) + { + unsigned int F = (T+bitsToShift/8); + if (F < arrayLength) + to[T] = from[F] >> (bitsToShift%8); + else + to[T] = 0; + if (F + 1 < arrayLength) + to[T] |= from[(F+1)] << (8-bitsToShift%8); + } +} + +void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) +{ + for (unsigned int T=0; T < arrayLength; ++T) + { + if (T >= bitsToShift/8) + { + unsigned int F = T-bitsToShift/8; + to[T] = from[F] << (bitsToShift%8); + if (T >= bitsToShift/8+1) + to[T] |= from[F-1] >> (8-bitsToShift%8); + } + else { + to[T] = 0; + } + } +} + +BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>=" + unsigned char TmpArray[32]; + uint256 TmpL; + for (unsigned int i = 0; i < 256; ++i) + { + shiftArrayLeft(TmpArray, OneArray, 32, i); + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i)); + TmpL = OneL; TmpL <<= i; + BOOST_CHECK(TmpL == (OneL << i)); + BOOST_CHECK((HalfL >> (255-i)) == (OneL << i)); + TmpL = HalfL; TmpL >>= (255-i); + BOOST_CHECK(TmpL == (OneL << i)); + + shiftArrayLeft(TmpArray, R1Array, 32, i); + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i)); + TmpL = R1L; TmpL <<= i; + BOOST_CHECK(TmpL == (R1L << i)); + + shiftArrayRight(TmpArray, R1Array, 32, i); + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i)); + TmpL = R1L; TmpL >>= i; + BOOST_CHECK(TmpL == (R1L >> i)); + + shiftArrayLeft(TmpArray, MaxArray, 32, i); + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i)); + TmpL = MaxL; TmpL <<= i; + BOOST_CHECK(TmpL == (MaxL << i)); + + shiftArrayRight(TmpArray, MaxArray, 32, i); + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i)); + TmpL = MaxL; TmpL >>= i; + BOOST_CHECK(TmpL == (MaxL >> i)); + } + uint256 c1L = uint256(0x0123456789abcdefULL); + uint256 c2L = c1L << 128; + for (unsigned int i = 0; i < 128; ++i) { + BOOST_CHECK((c1L << i) == (c2L >> (128-i))); + } + for (unsigned int i = 128; i < 256; ++i) { + BOOST_CHECK((c1L << i) == (c2L << (i-128))); + } + + uint160 TmpS; + for (unsigned int i = 0; i < 160; ++i) + { + shiftArrayLeft(TmpArray, OneArray, 20, i); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (OneS << i)); + TmpS = OneS; TmpS <<= i; + BOOST_CHECK(TmpS == (OneS << i)); + BOOST_CHECK((HalfS >> (159-i)) == (OneS << i)); + TmpS = HalfS; TmpS >>= (159-i); + BOOST_CHECK(TmpS == (OneS << i)); + + shiftArrayLeft(TmpArray, R1Array, 20, i); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S << i)); + TmpS = R1S; TmpS <<= i; + BOOST_CHECK(TmpS == (R1S << i)); + + shiftArrayRight(TmpArray, R1Array, 20, i); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S >> i)); + TmpS = R1S; TmpS >>= i; + BOOST_CHECK(TmpS == (R1S >> i)); + + shiftArrayLeft(TmpArray, MaxArray, 20, i); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS << i)); + TmpS = MaxS; TmpS <<= i; + BOOST_CHECK(TmpS == (MaxS << i)); + + shiftArrayRight(TmpArray, MaxArray, 20, i); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS >> i)); + TmpS = MaxS; TmpS >>= i; + BOOST_CHECK(TmpS == (MaxS >> i)); + } + uint160 c1S = uint160(0x0123456789abcdefULL); + uint160 c2S = c1S << 80; + for (unsigned int i = 0; i < 80; ++i) { + BOOST_CHECK((c1S << i) == (c2S >> (80-i))); + } + for (unsigned int i = 80; i < 160; ++i) { + BOOST_CHECK((c1S << i) == (c2S << (i-80))); + } +} + +BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ - +{ + BOOST_CHECK(!ZeroL); BOOST_CHECK(!ZeroS); + BOOST_CHECK(!(!OneL));BOOST_CHECK(!(!OneS)); + for (unsigned int i = 0; i < 256; ++i) + BOOST_CHECK(!(!(OneL<<i))); + for (unsigned int i = 0; i < 160; ++i) + BOOST_CHECK(!(!(OneS<<i))); + BOOST_CHECK(!(!R1L));BOOST_CHECK(!(!R1S)); + BOOST_CHECK(!(!R2S));BOOST_CHECK(!(!R2S)); + BOOST_CHECK(!(!MaxL));BOOST_CHECK(!(!MaxS)); + + BOOST_CHECK(~ZeroL == MaxL); BOOST_CHECK(~ZeroS == MaxS); + + unsigned char TmpArray[32]; + for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; } + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L)); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (~R1S)); + + BOOST_CHECK(-ZeroL == ZeroL); BOOST_CHECK(-ZeroS == ZeroS); + BOOST_CHECK(-R1L == (~R1L)+1); + BOOST_CHECK(-R1S == (~R1S)+1); + for (unsigned int i = 0; i < 256; ++i) + BOOST_CHECK(-(OneL<<i) == (MaxL << i)); + for (unsigned int i = 0; i < 160; ++i) + BOOST_CHECK(-(OneS<<i) == (MaxS << i)); +} + + +// Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each +// element of Aarray and Barray, and then converting the result into a uint256. +#define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \ + for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \ + BOOST_CHECK(uint256(std::vector<unsigned char>(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<unsigned char>(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)); \ + TmpS = _A_##S; TmpS _OP_##= _B_##S; BOOST_CHECK(TmpS == (_A_##S _OP_ _B_##S)); + +BOOST_AUTO_TEST_CASE( bitwiseOperators ) +{ + unsigned char TmpArray[32]; + + CHECKBITWISEOPERATOR(R1,R2,|) + CHECKBITWISEOPERATOR(R1,R2,^) + CHECKBITWISEOPERATOR(R1,R2,&) + CHECKBITWISEOPERATOR(R1,Zero,|) + CHECKBITWISEOPERATOR(R1,Zero,^) + CHECKBITWISEOPERATOR(R1,Zero,&) + CHECKBITWISEOPERATOR(R1,Max,|) + CHECKBITWISEOPERATOR(R1,Max,^) + CHECKBITWISEOPERATOR(R1,Max,&) + CHECKBITWISEOPERATOR(Zero,R1,|) + CHECKBITWISEOPERATOR(Zero,R1,^) + CHECKBITWISEOPERATOR(Zero,R1,&) + CHECKBITWISEOPERATOR(Max,R1,|) + CHECKBITWISEOPERATOR(Max,R1,^) + CHECKBITWISEOPERATOR(Max,R1,&) + + uint256 TmpL; + uint160 TmpS; + CHECKASSIGNMENTOPERATOR(R1,R2,|) + CHECKASSIGNMENTOPERATOR(R1,R2,^) + CHECKASSIGNMENTOPERATOR(R1,R2,&) + CHECKASSIGNMENTOPERATOR(R1,Zero,|) + CHECKASSIGNMENTOPERATOR(R1,Zero,^) + CHECKASSIGNMENTOPERATOR(R1,Zero,&) + CHECKASSIGNMENTOPERATOR(R1,Max,|) + CHECKASSIGNMENTOPERATOR(R1,Max,^) + CHECKASSIGNMENTOPERATOR(R1,Max,&) + CHECKASSIGNMENTOPERATOR(Zero,R1,|) + CHECKASSIGNMENTOPERATOR(Zero,R1,^) + CHECKASSIGNMENTOPERATOR(Zero,R1,&) + CHECKASSIGNMENTOPERATOR(Max,R1,|) + CHECKASSIGNMENTOPERATOR(Max,R1,^) + CHECKASSIGNMENTOPERATOR(Max,R1,&) + + uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL; + TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | uint256(Tmp64))); + TmpS = R1S; TmpS |= Tmp64; BOOST_CHECK(TmpS == (R1S | 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))); +} + +BOOST_AUTO_TEST_CASE( comparison ) // <= >= < > +{ + uint256 TmpL; + for (unsigned int i = 0; i < 256; ++i) { + TmpL= OneL<< i; + BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL); + BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL); + TmpL |= R1L; + BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L)); + 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; + for (unsigned int i = 0; i < 160; ++i) { + TmpS= OneS<< i; + BOOST_CHECK( TmpS >= ZeroS && TmpS > ZeroS && ZeroS < TmpS && ZeroS <= TmpS); + BOOST_CHECK( TmpS >= 0 && TmpS > 0 && 0 < TmpS && 0 <= TmpS); + TmpS |= R1S; + BOOST_CHECK( TmpS >= R1S ); BOOST_CHECK( (TmpS == R1S) != (TmpS > R1S)); BOOST_CHECK( (TmpS == R1S) || !( TmpS <= R1S)); + BOOST_CHECK( R1S <= TmpS ); BOOST_CHECK( (R1S == TmpS) != (R1S < TmpS)); BOOST_CHECK( (TmpS == R1S) || !( R1S >= TmpS)); + BOOST_CHECK(! (TmpS < R1S)); BOOST_CHECK(! (R1S > TmpS)); + } +} + +BOOST_AUTO_TEST_CASE( plusMinus ) +{ + uint256 TmpL = 0; + BOOST_CHECK(R1L+R2L == uint256(R1LplusR2L)); + TmpL += R1L; + BOOST_CHECK(TmpL == R1L); + TmpL += R2L; + BOOST_CHECK(TmpL == R1L + R2L); + BOOST_CHECK(OneL+MaxL == ZeroL); + BOOST_CHECK(MaxL+OneL == ZeroL); + for (unsigned int i = 1; i < 256; ++i) { + BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) ); + BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) ); + TmpL = (MaxL>>i); TmpL += OneL; + BOOST_CHECK( TmpL == (HalfL >> (i-1)) ); + TmpL = (MaxL>>i); TmpL += 1; + BOOST_CHECK( TmpL == (HalfL >> (i-1)) ); + TmpL = (MaxL>>i); + BOOST_CHECK( TmpL++ == (MaxL>>i) ); + BOOST_CHECK( TmpL == (HalfL >> (i-1))); + } + BOOST_CHECK(uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); + TmpL = uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL; + BOOST_CHECK(TmpL == uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL)); + TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL); + TmpL = R1L; + BOOST_CHECK(++TmpL == R1L+1); + + BOOST_CHECK(R1L -(-R2L) == R1L+R2L); + BOOST_CHECK(R1L -(-OneL) == R1L+OneL); + BOOST_CHECK(R1L - OneL == R1L+(-OneL)); + for (unsigned int i = 1; i < 256; ++i) { + BOOST_CHECK((MaxL>>i) - (-OneL) == (HalfL >> (i-1))); + BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i)); + TmpL = (HalfL >> (i-1)); + BOOST_CHECK(TmpL-- == (HalfL >> (i-1))); + BOOST_CHECK(TmpL == (MaxL >> i)); + TmpL = (HalfL >> (i-1)); + BOOST_CHECK(--TmpL == (MaxL >> i)); + } + TmpL = R1L; + BOOST_CHECK(--TmpL == R1L-1); + + // 160-bit; copy-pasted + uint160 TmpS = 0; + BOOST_CHECK(R1S+R2S == uint160(R1LplusR2L)); + TmpS += R1S; + BOOST_CHECK(TmpS == R1S); + TmpS += R2S; + BOOST_CHECK(TmpS == R1S + R2S); + BOOST_CHECK(OneS+MaxS == ZeroS); + BOOST_CHECK(MaxS+OneS == ZeroS); + for (unsigned int i = 1; i < 160; ++i) { + BOOST_CHECK( (MaxS >> i) + OneS == (HalfS >> (i-1)) ); + BOOST_CHECK( OneS + (MaxS >> i) == (HalfS >> (i-1)) ); + TmpS = (MaxS>>i); TmpS += OneS; + BOOST_CHECK( TmpS == (HalfS >> (i-1)) ); + TmpS = (MaxS>>i); TmpS += 1; + BOOST_CHECK( TmpS == (HalfS >> (i-1)) ); + TmpS = (MaxS>>i); + 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)); + TmpS -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpS == 0xbedc77e27940a7ULL); + TmpS = R1S; + BOOST_CHECK(++TmpS == R1S+1); + + BOOST_CHECK(R1S -(-R2S) == R1S+R2S); + BOOST_CHECK(R1S -(-OneS) == R1S+OneS); + BOOST_CHECK(R1S - OneS == R1S+(-OneS)); + for (unsigned int i = 1; i < 160; ++i) { + BOOST_CHECK((MaxS>>i) - (-OneS) == (HalfS >> (i-1))); + BOOST_CHECK((HalfS >> (i-1)) - OneS == (MaxS>>i)); + TmpS = (HalfS >> (i-1)); + BOOST_CHECK(TmpS-- == (HalfS >> (i-1))); + BOOST_CHECK(TmpS == (MaxS >> i)); + TmpS = (HalfS >> (i-1)); + BOOST_CHECK(--TmpS == (MaxS >> i)); + } + TmpS = R1S; + BOOST_CHECK(--TmpS == R1S-1); + +} + +bool almostEqual(double d1, double d2) +{ + return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon(); +} + +BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize +{ + BOOST_CHECK(R1L.GetHex() == R1L.ToString()); + BOOST_CHECK(R2L.GetHex() == R2L.ToString()); + BOOST_CHECK(OneL.GetHex() == OneL.ToString()); + BOOST_CHECK(MaxL.GetHex() == MaxL.ToString()); + uint256 TmpL(R1L); + BOOST_CHECK(TmpL == R1L); + TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L); + TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0); + TmpL.SetHex(HalfL.ToString()); BOOST_CHECK(TmpL == HalfL); + + TmpL.SetHex(R1L.ToString()); + BOOST_CHECK(memcmp(R1L.begin(), R1Array, 32)==0); + BOOST_CHECK(memcmp(TmpL.begin(), R1Array, 32)==0); + BOOST_CHECK(memcmp(R2L.begin(), R2Array, 32)==0); + BOOST_CHECK(memcmp(ZeroL.begin(), ZeroArray, 32)==0); + BOOST_CHECK(memcmp(OneL.begin(), OneArray, 32)==0); + BOOST_CHECK(R1L.size() == 32); + BOOST_CHECK(R2L.size() == 32); + BOOST_CHECK(ZeroL.size() == 32); + BOOST_CHECK(MaxL.size() == 32); + BOOST_CHECK(R1L.begin() + 32 == R1L.end()); + BOOST_CHECK(R2L.begin() + 32 == R2L.end()); + BOOST_CHECK(OneL.begin() + 32 == OneL.end()); + BOOST_CHECK(MaxL.begin() + 32 == MaxL.end()); + BOOST_CHECK(TmpL.begin() + 32 == TmpL.end()); + BOOST_CHECK(R1L.GetLow64() == R1LLow64); + BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL); + BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL); + BOOST_CHECK(R1L.GetSerializeSize(0,PROTOCOL_VERSION) == 32); + BOOST_CHECK(ZeroL.GetSerializeSize(0,PROTOCOL_VERSION) == 32); + + std::stringstream ss; + R1L.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32)); + TmpL.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(R1L == TmpL); + ss.str(""); + ZeroL.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32)); + TmpL.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ZeroL == TmpL); + ss.str(""); + MaxL.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32)); + TmpL.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(MaxL == TmpL); + ss.str(""); + + BOOST_CHECK(R1S.GetHex() == R1S.ToString()); + BOOST_CHECK(R2S.GetHex() == R2S.ToString()); + BOOST_CHECK(OneS.GetHex() == OneS.ToString()); + BOOST_CHECK(MaxS.GetHex() == MaxS.ToString()); + uint160 TmpS(R1S); + BOOST_CHECK(TmpS == R1S); + TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S); + TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == 0); + TmpS.SetHex(HalfS.ToString()); BOOST_CHECK(TmpS == HalfS); + + TmpS.SetHex(R1S.ToString()); + BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20)==0); + BOOST_CHECK(memcmp(TmpS.begin(), R1Array, 20)==0); + BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20)==0); + BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20)==0); + BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20)==0); + BOOST_CHECK(R1S.size() == 20); + BOOST_CHECK(R2S.size() == 20); + BOOST_CHECK(ZeroS.size() == 20); + BOOST_CHECK(MaxS.size() == 20); + BOOST_CHECK(R1S.begin() + 20 == R1S.end()); + BOOST_CHECK(R2S.begin() + 20 == R2S.end()); + BOOST_CHECK(OneS.begin() + 20 == OneS.end()); + BOOST_CHECK(MaxS.begin() + 20 == MaxS.end()); + BOOST_CHECK(TmpS.begin() + 20 == TmpS.end()); + BOOST_CHECK(R1S.GetLow64() == R1LLow64); + BOOST_CHECK(HalfS.GetLow64() ==0x0000000000000000ULL); + BOOST_CHECK(OneS.GetLow64() ==0x0000000000000001ULL); + BOOST_CHECK(R1S.GetSerializeSize(0,PROTOCOL_VERSION) == 20); + BOOST_CHECK(ZeroS.GetSerializeSize(0,PROTOCOL_VERSION) == 20); + + R1S.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20)); + TmpS.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(R1S == TmpS); + ss.str(""); + ZeroS.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20)); + TmpS.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ZeroS == TmpS); + ss.str(""); + MaxS.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20)); + TmpS.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(MaxS == TmpS); + ss.str(""); + + for (unsigned int i = 0; i < 255; ++i) + { + BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i)); + if (i < 160) BOOST_CHECK((OneS << i).getdouble() == ldexp(1.0,i)); + } + BOOST_CHECK(ZeroL.getdouble() == 0.0); + BOOST_CHECK(ZeroS.getdouble() == 0.0); + for (int i = 256; i > 53; --i) + BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i))); + for (int i = 160; i > 53; --i) + BOOST_CHECK(almostEqual((R1S>>(160-i)).getdouble(), ldexp(R1Sdouble,i))); + uint64_t R1L64part = (R1L>>192).GetLow64(); + uint64_t R1S64part = (R1S>>96).GetLow64(); + for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly + { + BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i))); + BOOST_CHECK((R1S>>(160-i)).getdouble() == (double)(R1S64part >> (64-i))); + } +} + +BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage +{ + // ~R1L give a base_uint<256> + BOOST_CHECK((~~R1L >> 10) == (R1L >> 10)); BOOST_CHECK((~~R1S >> 10) == (R1S >> 10)); + BOOST_CHECK((~~R1L << 10) == (R1L << 10)); BOOST_CHECK((~~R1S << 10) == (R1S << 10)); + BOOST_CHECK(!(~~R1L < R1L)); BOOST_CHECK(!(~~R1S < R1S)); + BOOST_CHECK(~~R1L <= R1L); BOOST_CHECK(~~R1S <= R1S); + BOOST_CHECK(!(~~R1L > R1L)); BOOST_CHECK(!(~~R1S > R1S)); + BOOST_CHECK(~~R1L >= R1L); BOOST_CHECK(~~R1S >= R1S); + BOOST_CHECK(!(R1L < ~~R1L)); BOOST_CHECK(!(R1S < ~~R1S)); + BOOST_CHECK(R1L <= ~~R1L); BOOST_CHECK(R1S <= ~~R1S); + BOOST_CHECK(!(R1L > ~~R1L)); BOOST_CHECK(!(R1S > ~~R1S)); + BOOST_CHECK(R1L >= ~~R1L); BOOST_CHECK(R1S >= ~~R1S); + + BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L); + BOOST_CHECK(~~R1S + R2S == R1S + ~~R2S); + BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L); + BOOST_CHECK(~~R1S - R2S == R1S - ~~R2S); + BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L); + BOOST_CHECK(~R1S != R1S); BOOST_CHECK(R1S != ~R1S); + unsigned char TmpArray[32]; + CHECKBITWISEOPERATOR(~R1,R2,|) + CHECKBITWISEOPERATOR(~R1,R2,^) + CHECKBITWISEOPERATOR(~R1,R2,&) + CHECKBITWISEOPERATOR(R1,~R2,|) + CHECKBITWISEOPERATOR(R1,~R2,^) + CHECKBITWISEOPERATOR(R1,~R2,&) } BOOST_AUTO_TEST_SUITE_END() + diff --git a/src/txdb.cpp b/src/txdb.cpp index 99deb31404..6ed0d18785 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -26,8 +26,8 @@ void static BatchWriteHashBestChain(CLevelDBBatch &batch, const uint256 &hash) { CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe) { } -bool CCoinsViewDB::GetCoins(const uint256 &txid, CCoins &coins) { - return db.Read(make_pair('c', txid), coins); +bool CCoinsViewDB::GetCoins(const uint256 &txid, CCoins &coins) { + return db.Read(make_pair('c', txid), coins); } bool CCoinsViewDB::SetCoins(const uint256 &txid, const CCoins &coins) { @@ -37,7 +37,7 @@ bool CCoinsViewDB::SetCoins(const uint256 &txid, const CCoins &coins) { } bool CCoinsViewDB::HaveCoins(const uint256 &txid) { - return db.Exists(make_pair('c', txid)); + return db.Exists(make_pair('c', txid)); } uint256 CCoinsViewDB::GetBestBlock() { @@ -131,7 +131,7 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) { ssKey >> txhash; ss << txhash; ss << VARINT(coins.nVersion); - ss << (coins.fCoinBase ? 'c' : 'n'); + ss << (coins.fCoinBase ? 'c' : 'n'); ss << VARINT(coins.nHeight); stats.nTransactions++; for (unsigned int i=0; i<coins.vout.size(); i++) { @@ -148,7 +148,7 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) { } pcursor->Next(); } catch (std::exception &e) { - return error("%s() : deserialize error", __PRETTY_FUNCTION__); + return error("%s : Deserialize or I/O error - %s", __PRETTY_FUNCTION__, e.what()); } } delete pcursor; @@ -226,7 +226,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts() break; // if shutdown requested or finished loading block index } } catch (std::exception &e) { - return error("%s() : deserialize error", __PRETTY_FUNCTION__); + return error("%s : Deserialize or I/O error - %s", __PRETTY_FUNCTION__, e.what()); } } delete pcursor; diff --git a/src/ui_interface.h b/src/ui_interface.h index a14ed67814..677d097fa4 100644 --- a/src/ui_interface.h +++ b/src/ui_interface.h @@ -62,6 +62,8 @@ public: /** Force blocking, modal message box dialog (not just OS notification) */ MODAL = 0x10000000U, + /** Don't bring GUI to foreground. Use for messages during initialization */ + NOSHOWGUI = 0x20000000U, /** Predefined combinations for certain default usage cases */ MSG_INFORMATION = ICON_INFORMATION, diff --git a/src/uint256.h b/src/uint256.h index 7dbb3f83bb..ba903bc8fc 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -19,9 +19,6 @@ inline signed char HexDigit(char c) return p_util_hexdigit[(unsigned char)c]; } -inline int Testuint256AdHoc(std::vector<std::string> vArg); - - /** Base class without constructors for uint256 and uint160. * This makes the compiler let you use it in a union. */ @@ -203,7 +200,7 @@ public: { // prefix operator int i = 0; - while (--pn[i] == -1 && i < WIDTH-1) + while (--pn[i] == (uint32_t)-1 && i < WIDTH-1) i++; return *this; } @@ -370,9 +367,10 @@ public: return sizeof(pn); } - uint64_t Get64(int n=0) const + uint64_t GetLow64() const { - return pn[2*n] | (uint64_t)pn[2*n+1] << 32; + assert(WIDTH >= 2); + return pn[0] | (uint64_t)pn[1] << 32; } // unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const @@ -398,7 +396,6 @@ public: friend class uint160; friend class uint256; - friend inline int Testuint256AdHoc(std::vector<std::string> vArg); }; typedef base_uint<160> base_uint160; @@ -525,9 +522,6 @@ inline const uint160 operator-(const uint160& a, const uint160& b) { r - - - ////////////////////////////////////////////////////////////////////////////// // // uint256 @@ -638,147 +632,4 @@ inline const uint256 operator|(const uint256& a, const uint256& b) { return inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; } inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; } - - - - - - - - - -#ifdef TEST_UINT256 - -inline int Testuint256AdHoc(std::vector<std::string> vArg) -{ - uint256 g(0); - - - LogPrintf("%s\n", g.ToString().c_str()); - g--; LogPrintf("g--\n"); - LogPrintf("%s\n", g.ToString().c_str()); - g--; LogPrintf("g--\n"); - LogPrintf("%s\n", g.ToString().c_str()); - g++; LogPrintf("g++\n"); - LogPrintf("%s\n", g.ToString().c_str()); - g++; LogPrintf("g++\n"); - LogPrintf("%s\n", g.ToString().c_str()); - g++; LogPrintf("g++\n"); - LogPrintf("%s\n", g.ToString().c_str()); - g++; LogPrintf("g++\n"); - LogPrintf("%s\n", g.ToString().c_str()); - - - - uint256 a(7); - LogPrintf("a=7\n"); - LogPrintf("%s\n", a.ToString().c_str()); - - uint256 b; - LogPrintf("b undefined\n"); - LogPrintf("%s\n", b.ToString().c_str()); - int c = 3; - - a = c; - a.pn[3] = 15; - LogPrintf("%s\n", a.ToString().c_str()); - uint256 k(c); - - a = 5; - a.pn[3] = 15; - LogPrintf("%s\n", a.ToString().c_str()); - b = 1; - b <<= 52; - - a |= b; - - a ^= 0x500; - - LogPrintf("a %s\n", a.ToString().c_str()); - - a = a | b | (uint256)0x1000; - - - LogPrintf("a %s\n", a.ToString().c_str()); - LogPrintf("b %s\n", b.ToString().c_str()); - - a = 0xfffffffe; - a.pn[4] = 9; - - LogPrintf("%s\n", a.ToString().c_str()); - a++; - LogPrintf("%s\n", a.ToString().c_str()); - a++; - LogPrintf("%s\n", a.ToString().c_str()); - a++; - LogPrintf("%s\n", a.ToString().c_str()); - a++; - LogPrintf("%s\n", a.ToString().c_str()); - - a--; - LogPrintf("%s\n", a.ToString().c_str()); - a--; - LogPrintf("%s\n", a.ToString().c_str()); - a--; - LogPrintf("%s\n", a.ToString().c_str()); - uint256 d = a--; - LogPrintf("%s\n", d.ToString().c_str()); - LogPrintf("%s\n", a.ToString().c_str()); - a--; - LogPrintf("%s\n", a.ToString().c_str()); - a--; - LogPrintf("%s\n", a.ToString().c_str()); - - d = a; - - LogPrintf("%s\n", d.ToString().c_str()); - for (int i = uint256::WIDTH-1; i >= 0; i--) LogPrintf("%08x", d.pn[i]); LogPrintf("\n"); - - uint256 neg = d; - neg = ~neg; - LogPrintf("%s\n", neg.ToString().c_str()); - - - uint256 e = uint256("0xABCDEF123abcdef12345678909832180000011111111"); - LogPrintf("\n"); - LogPrintf("%s\n", e.ToString().c_str()); - - - LogPrintf("\n"); - uint256 x1 = uint256("0xABCDEF123abcdef12345678909832180000011111111"); - uint256 x2; - LogPrintf("%s\n", x1.ToString().c_str()); - for (int i = 0; i < 270; i += 4) - { - x2 = x1 << i; - LogPrintf("%s\n", x2.ToString().c_str()); - } - - LogPrintf("\n"); - LogPrintf("%s\n", x1.ToString().c_str()); - for (int i = 0; i < 270; i += 4) - { - x2 = x1; - x2 >>= i; - LogPrintf("%s\n", x2.ToString().c_str()); - } - - - for (int i = 0; i < 100; i++) - { - uint256 k = (~uint256(0) >> i); - LogPrintf("%s\n", k.ToString().c_str()); - } - - for (int i = 0; i < 100; i++) - { - uint256 k = (~uint256(0) << i); - LogPrintf("%s\n", k.ToString().c_str()); - } - - return (0); -} - -#endif - #endif diff --git a/src/wallet.cpp b/src/wallet.cpp index 76a83082ae..9065ba8483 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -35,6 +35,7 @@ struct CompareValueOnly CPubKey CWallet::GenerateNewKey() { + AssertLockHeld(cs_wallet); // mapKeyMetadata bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets RandAddSeedPerfmon(); @@ -60,6 +61,7 @@ CPubKey CWallet::GenerateNewKey() bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey) { + AssertLockHeld(cs_wallet); // mapKeyMetadata if (!CCryptoKeyStore::AddKeyPubKey(secret, pubkey)) return false; if (!fFileBacked) @@ -95,6 +97,7 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta) { + AssertLockHeld(cs_wallet); // mapKeyMetadata if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey)) nTimeFirstKey = meta.nCreateTime; @@ -202,6 +205,7 @@ public: bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit) { + AssertLockHeld(cs_wallet); // nWalletVersion if (nWalletVersion >= nVersion) return true; @@ -235,6 +239,7 @@ bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool CWallet::SetMaxVersion(int nVersion) { + AssertLockHeld(cs_wallet); // nWalletVersion, nWalletMaxVersion // cannot downgrade below current version if (nWalletVersion > nVersion) return false; @@ -327,6 +332,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase) int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb) { + AssertLockHeld(cs_wallet); // nOrderPosNext int64_t nRet = nOrderPosNext++; if (pwalletdb) { pwalletdb->WriteOrderPosNext(nOrderPosNext); @@ -338,6 +344,7 @@ int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb) CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount) { + AssertLockHeld(cs_wallet); // mapWallet CWalletDB walletdb(strWalletFile); // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap. @@ -1492,6 +1499,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet) { if (CDB::Rewrite(strWalletFile, "\x04pool")) { + LOCK(cs_wallet); setKeyPool.clear(); // Note: can't top-up keypool here, because wallet is locked. // User will be prompted to unlock wallet the next operation @@ -1509,6 +1517,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet) bool CWallet::SetAddressBook(const CTxDestination& address, const string& strName, const string& strPurpose) { + AssertLockHeld(cs_wallet); // mapAddressBook std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address); mapAddressBook[address].name = strName; if (!strPurpose.empty()) /* update purpose only if requested */ @@ -1525,6 +1534,7 @@ bool CWallet::SetAddressBook(const CTxDestination& address, const string& strNam bool CWallet::DelAddressBook(const CTxDestination& address) { + AssertLockHeld(cs_wallet); // mapAddressBook mapAddressBook.erase(address); NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), "", CT_DELETED); if (!fFileBacked) @@ -1738,6 +1748,7 @@ std::map<CTxDestination, int64_t> CWallet::GetAddressBalances() set< set<CTxDestination> > CWallet::GetAddressGroupings() { + AssertLockHeld(cs_wallet); // mapWallet set< set<CTxDestination> > groupings; set<CTxDestination> grouping; @@ -1830,6 +1841,7 @@ set< set<CTxDestination> > CWallet::GetAddressGroupings() set<CTxDestination> CWallet::GetAccountAddresses(string strAccount) const { + AssertLockHeld(cs_wallet); // mapWallet set<CTxDestination> result; BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& item, mapAddressBook) { @@ -1911,21 +1923,25 @@ void CWallet::UpdatedTransaction(const uint256 &hashTx) void CWallet::LockCoin(COutPoint& output) { + AssertLockHeld(cs_wallet); // setLockedCoins setLockedCoins.insert(output); } void CWallet::UnlockCoin(COutPoint& output) { + AssertLockHeld(cs_wallet); // setLockedCoins setLockedCoins.erase(output); } void CWallet::UnlockAllCoins() { + AssertLockHeld(cs_wallet); // setLockedCoins setLockedCoins.clear(); } bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const { + AssertLockHeld(cs_wallet); // setLockedCoins COutPoint outpt(hash, n); return (setLockedCoins.count(outpt) > 0); @@ -1933,6 +1949,7 @@ bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts) { + AssertLockHeld(cs_wallet); // setLockedCoins for (std::set<COutPoint>::iterator it = setLockedCoins.begin(); it != setLockedCoins.end(); it++) { COutPoint outpt = (*it); @@ -1941,6 +1958,7 @@ void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts) } void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const { + AssertLockHeld(cs_wallet); // mapKeyMetadata mapKeyBirth.clear(); // get birth times for keys with metadata diff --git a/src/wallet.h b/src/wallet.h index 99f6293b1e..e4452a3093 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -105,6 +105,11 @@ private: int64_t nLastResend; public: + /// Main wallet lock. + /// This lock protects all the fields added by CWallet + /// except for: + /// fFileBacked (immutable after instantiation) + /// strWalletFile (immutable after instantiation) mutable CCriticalSection cs_wallet; bool fFileBacked; @@ -154,10 +159,11 @@ public: int64_t nTimeFirstKey; // check whether we are allowed to upgrade (or already support) to the named feature - bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; } + bool CanSupportFeature(enum WalletFeature wf) { AssertLockHeld(cs_wallet); return nWalletMaxVersion >= wf; } void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl = NULL) const; bool SelectCoinsMinConf(int64_t nTargetValue, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const; + bool IsLockedCoin(uint256 hash, unsigned int n) const; void LockCoin(COutPoint& output); void UnlockCoin(COutPoint& output); @@ -174,7 +180,7 @@ public: // Load metadata (used by LoadWallet) bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata); - bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; } + bool LoadMinVersion(int nVersion) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; } // Adds an encrypted key to the store, and saves it to disk. bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret); @@ -323,6 +329,7 @@ public: unsigned int GetKeyPoolSize() { + AssertLockHeld(cs_wallet); // setKeyPool return setKeyPool.size(); } @@ -335,7 +342,7 @@ public: bool SetMaxVersion(int nVersion); // get the current wallet format (the oldest client version guaranteed to understand this wallet) - int GetVersion() { return nWalletVersion; } + int GetVersion() { AssertLockHeld(cs_wallet); return nWalletVersion; } /** Address book entry changed. * @note called with lock cs_wallet held. |