From 9b490f71b00227ceb9a32bb0b44a37f489a167a0 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 19 Sep 2011 12:40:23 +0200 Subject: [PATCH 01/10] put Q_OBJECT everywhere it should be (removes warnings when running lupdate) --- .gitignore | 1 + bitcoin-qt.pro | 17 ++++++++++------- src/qt/optionsdialog.cpp | 4 ++++ src/qt/overviewpage.cpp | 3 ++- src/qt/transactiondesc.h | 1 + 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index a5f96ba44c7..08dfd0016df 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ src/bitcoind ui_*.h *.qm moc_* +*.moc Makefile bitcoin-qt #resources cpp diff --git a/bitcoin-qt.pro b/bitcoin-qt.pro index 19d7814d398..dcd3d3d40ba 100644 --- a/bitcoin-qt.pro +++ b/bitcoin-qt.pro @@ -5,14 +5,7 @@ DEFINES += QT_GUI # DEFINES += SSL CONFIG += no_include_pwd -# for boost 1.37, add -mt to the boost libraries LIBS += -lssl -lcrypto -ldb_cxx -unix:!macx:LIBS += -lboost_system -lboost_filesystem -lboost_program_options -lboost_thread -macx:LIBS += -lboost_system-mt -lboost_filesystem-mt -lboost_program_options-mt -lboost_thread-mt -macx:DEFINES += __WXMAC_OSX__ MSG_NOSIGNAL=0 BOOST_FILESYSTEM_VERSION=3 -windows:LIBS += -lboost_system-mgw44-mt-1_43 -lboost_filesystem-mgw44-mt-1_43 -lboost_program_options-mgw44-mt-1_43 -lboost_thread-mgw44-mt-1_43 -lws2_32 -lgdi32 -windows:DEFINES += __WXMSW__ -windows:RC_FILE = src/qt/res/bitcoin-qt.rc # use: qmake "USE_UPNP=1" # miniupnpc (http://miniupnp.free.fr/files/) must be installed @@ -22,6 +15,7 @@ count(USE_UPNP, 1) { LIBS += -lminiupnpc } +# use: qmake "USE_DBUS=1" count(USE_DBUS, 1) { message(Building with DBUS (Freedesktop notifications) support) DEFINES += QT_DBUS @@ -176,6 +170,15 @@ TRANSLATIONS = src/qt/locale/bitcoin_nl.ts src/qt/locale/bitcoin_de.ts \ OTHER_FILES += \ README.rst +# Platform-specific libraries/includes +# for boost 1.37, add -mt to the boost libraries +unix:!macx:LIBS += -lboost_system -lboost_filesystem -lboost_program_options -lboost_thread +macx:LIBS += -lboost_system-mt -lboost_filesystem-mt -lboost_program_options-mt -lboost_thread-mt +macx:DEFINES += __WXMAC_OSX__ MSG_NOSIGNAL=0 BOOST_FILESYSTEM_VERSION=3 +windows:LIBS += -lboost_system-mgw44-mt-1_43 -lboost_filesystem-mgw44-mt-1_43 -lboost_program_options-mgw44-mt-1_43 -lboost_thread-mgw44-mt-1_43 -lws2_32 -lgdi32 +windows:DEFINES += __WXMSW__ +windows:RC_FILE = src/qt/res/bitcoin-qt.rc + # For use with MacPorts macx:INCLUDEPATH += /opt/local/include /opt/local/include/db48 macx:LIBS += -L/opt/local/lib -L/opt/local/lib/db48 diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp index 0eeb6f86a5a..7267e3d1036 100644 --- a/src/qt/optionsdialog.cpp +++ b/src/qt/optionsdialog.cpp @@ -23,6 +23,7 @@ /* First page of options */ class MainOptionsPage : public QWidget { + Q_OBJECT public: explicit MainOptionsPage(QWidget *parent=0); @@ -45,6 +46,7 @@ public slots: class DisplayOptionsPage : public QWidget { + Q_OBJECT public: explicit DisplayOptionsPage(QWidget *parent=0); @@ -58,6 +60,8 @@ public slots: }; +#include "optionsdialog.moc" + OptionsDialog::OptionsDialog(QWidget *parent): QDialog(parent), contents_widget(0), pages_widget(0), model(0), main_page(0), display_page(0) diff --git a/src/qt/overviewpage.cpp b/src/qt/overviewpage.cpp index 7bade9a755a..f84a79fe300 100644 --- a/src/qt/overviewpage.cpp +++ b/src/qt/overviewpage.cpp @@ -17,7 +17,7 @@ class TxViewDelegate : public QAbstractItemDelegate { - //Q_OBJECT + Q_OBJECT public: TxViewDelegate(): QAbstractItemDelegate(), unit(BitcoinUnits::BTC) { @@ -87,6 +87,7 @@ public: int unit; }; +#include "overviewpage.moc" OverviewPage::OverviewPage(QWidget *parent) : QWidget(parent), diff --git a/src/qt/transactiondesc.h b/src/qt/transactiondesc.h index 257b2cbb892..484bb1230ef 100644 --- a/src/qt/transactiondesc.h +++ b/src/qt/transactiondesc.h @@ -10,6 +10,7 @@ class CWalletTx; class TransactionDesc: public QObject { + Q_OBJECT public: // Provide human-readable extended HTML description of a transaction static QString toHTML(CWallet *wallet, CWalletTx &wtx); From b8afa21fad0e5d3e61deca9678d852efe8afc499 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Thu, 22 Sep 2011 19:02:01 +0200 Subject: [PATCH 02/10] show balance in sendcoins screen (issue #24) --- src/qt/forms/sendcoinsdialog.ui | 24 ++++++++++++++++++++++++ src/qt/sendcoinsdialog.cpp | 10 ++++++++++ src/qt/sendcoinsdialog.h | 1 + 3 files changed, 35 insertions(+) diff --git a/src/qt/forms/sendcoinsdialog.ui b/src/qt/forms/sendcoinsdialog.ui index 5b30d99e560..fb2fc99bfe8 100644 --- a/src/qt/forms/sendcoinsdialog.ui +++ b/src/qt/forms/sendcoinsdialog.ui @@ -75,6 +75,30 @@ + + + + 3 + + + + + Balance: + + + + + + + 123.456 BTC + + + Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse + + + + + diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp index 852d789805e..8954af47425 100644 --- a/src/qt/sendcoinsdialog.cpp +++ b/src/qt/sendcoinsdialog.cpp @@ -36,6 +36,9 @@ void SendCoinsDialog::setModel(WalletModel *model) entry->setModel(model); } } + + setBalance(model->getBalance(), model->getUnconfirmedBalance()); + connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64))); } SendCoinsDialog::~SendCoinsDialog() @@ -241,3 +244,10 @@ void SendCoinsDialog::handleURL(const QUrl *url) } pasteEntry(rv); } + +void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance) +{ + Q_UNUSED(unconfirmedBalance); + int unit = model->getOptionsModel()->getDisplayUnit(); + ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance)); +} diff --git a/src/qt/sendcoinsdialog.h b/src/qt/sendcoinsdialog.h index 9c56e51811e..a14f99e8b2f 100644 --- a/src/qt/sendcoinsdialog.h +++ b/src/qt/sendcoinsdialog.h @@ -37,6 +37,7 @@ public slots: void accept(); SendCoinsEntry *addEntry(); void updateRemoveEnabled(); + void setBalance(qint64 balance, qint64 unconfirmedBalance); private: Ui::SendCoinsDialog *ui; From 99cec89cb369beb8dce6c92db837bb5951f627d5 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Sat, 24 Sep 2011 11:43:58 +0200 Subject: [PATCH 03/10] make qmake build system more configurable (as suggested by lukejr) --- bitcoin-qt.pro | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/bitcoin-qt.pro b/bitcoin-qt.pro index dcd3d3d40ba..c7eb563bb41 100644 --- a/bitcoin-qt.pro +++ b/bitcoin-qt.pro @@ -5,7 +5,12 @@ DEFINES += QT_GUI # DEFINES += SSL CONFIG += no_include_pwd -LIBS += -lssl -lcrypto -ldb_cxx +# for boost 1.37, add -mt to the boost libraries +# use: qmake BOOST_LIB_SUFFIX=-mt + +# Dependency library locations can be customized with BOOST_INCLUDE_PATH, +# BOOST_LIB_PATH, BDB_INCLUDE_PATH, BDB_LIB_PATH +# OPENSSL_INCLUDE_PATH and OPENSSL_LIB_PATH respectively # use: qmake "USE_UPNP=1" # miniupnpc (http://miniupnp.free.fr/files/) must be installed @@ -170,19 +175,38 @@ TRANSLATIONS = src/qt/locale/bitcoin_nl.ts src/qt/locale/bitcoin_de.ts \ OTHER_FILES += \ README.rst -# Platform-specific libraries/includes -# for boost 1.37, add -mt to the boost libraries -unix:!macx:LIBS += -lboost_system -lboost_filesystem -lboost_program_options -lboost_thread -macx:LIBS += -lboost_system-mt -lboost_filesystem-mt -lboost_program_options-mt -lboost_thread-mt -macx:DEFINES += __WXMAC_OSX__ MSG_NOSIGNAL=0 BOOST_FILESYSTEM_VERSION=3 -windows:LIBS += -lboost_system-mgw44-mt-1_43 -lboost_filesystem-mgw44-mt-1_43 -lboost_program_options-mgw44-mt-1_43 -lboost_thread-mgw44-mt-1_43 -lws2_32 -lgdi32 +# platform specific defaults, if not overridden on command line +isEmpty(BOOST_LIB_SUFFIX) { + macx:BOOST_LIB_SUFFIX = -mt + windows:BOOST_LIB_SUFFIX = -mgw44-mt-1_43 +} + +isEmpty(BDB_LIB_PATH) { + macx:BDB_LIB_PATH = /opt/local/lib/db48 +} + +isEmpty(BDB_INCLUDE_PATH) { + macx:BDB_INCLUDE_PATH = /opt/local/include/db48 +} + +isEmpty(BOOST_LIB_PATH) { + macx:BOOST_LIB_PATH = /opt/local/lib +} + +isEmpty(BOOST_INCLUDE_PATH) { + macx:BOOST_INCLUDE_PATH = /opt/local/include +} + +windows:LIBS += -lws2_32 -lgdi32 windows:DEFINES += __WXMSW__ windows:RC_FILE = src/qt/res/bitcoin-qt.rc -# For use with MacPorts -macx:INCLUDEPATH += /opt/local/include /opt/local/include/db48 -macx:LIBS += -L/opt/local/lib -L/opt/local/lib/db48 - -# Additional Mac options +macx:DEFINES += __WXMAC_OSX__ MSG_NOSIGNAL=0 BOOST_FILESYSTEM_VERSION=3 macx:ICON = src/qt/res/icons/bitcoin.icns macx:TARGET = "Bitcoin Qt" + +# Set libraries and includes at end, to use platform-defined defaults if not overridden +INCLUDEPATH += $$BOOST_INCLUDE_PATH $$BDB_INCLUDE_PATH $$OPENSSL_INCLUDE_PATH +LIBS += $$join(BOOST_LIB_PATH,,-L,) $$join(BDB_LIB_PATH,,-L,) $$join(OPENSSL_LIB_PATH,,-L,) +LIBS += -lssl -lcrypto -ldb_cxx +LIBS += -lboost_system$$BOOST_LIB_SUFFIX -lboost_filesystem$$BOOST_LIB_SUFFIX -lboost_program_options$$BOOST_LIB_SUFFIX -lboost_thread$$BOOST_LIB_SUFFIX From 010c4fc0b5ca8ceccf15417b833bea4ce1016f43 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Sat, 24 Sep 2011 11:56:33 +0200 Subject: [PATCH 04/10] Change define to determine use of DBUS to USE_DBUS, to prevent overlap with Qt-defined QT_DBUS --- bitcoin-qt.pro | 2 +- src/qt/notificator.cpp | 12 ++++++------ src/qt/notificator.h | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bitcoin-qt.pro b/bitcoin-qt.pro index c7eb563bb41..3fff9ac908e 100644 --- a/bitcoin-qt.pro +++ b/bitcoin-qt.pro @@ -23,7 +23,7 @@ count(USE_UPNP, 1) { # use: qmake "USE_DBUS=1" count(USE_DBUS, 1) { message(Building with DBUS (Freedesktop notifications) support) - DEFINES += QT_DBUS + DEFINES += USE_DBUS QT += dbus } diff --git a/src/qt/notificator.cpp b/src/qt/notificator.cpp index 86ccfc8541c..cf0c0a39018 100644 --- a/src/qt/notificator.cpp +++ b/src/qt/notificator.cpp @@ -9,7 +9,7 @@ #include #include -#ifdef QT_DBUS +#ifdef USE_DBUS #include #include #endif @@ -23,7 +23,7 @@ Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon, programName(programName), mode(None), trayIcon(trayicon) -#ifdef QT_DBUS +#ifdef USE_DBUS ,interface(0) #endif { @@ -31,7 +31,7 @@ Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon, { mode = QSystemTray; } -#ifdef QT_DBUS +#ifdef USE_DBUS interface = new QDBusInterface("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications"); if(interface->isValid()) @@ -43,12 +43,12 @@ Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon, Notificator::~Notificator() { -#ifdef QT_DBUS +#ifdef USE_DBUS delete interface; #endif } -#ifdef QT_DBUS +#ifdef USE_DBUS // Loosely based on http://www.qtcentre.org/archive/index.php/t-25879.html class FreedesktopImage @@ -205,7 +205,7 @@ void Notificator::notify(Class cls, const QString &title, const QString &text, c { switch(mode) { -#ifdef QT_DBUS +#ifdef USE_DBUS case Freedesktop: notifyDBus(cls, title, text, icon, millisTimeout); break; diff --git a/src/qt/notificator.h b/src/qt/notificator.h index 13f6a908da7..4217f7e06fb 100644 --- a/src/qt/notificator.h +++ b/src/qt/notificator.h @@ -6,7 +6,7 @@ QT_BEGIN_NAMESPACE class QSystemTrayIcon; -#ifdef QT_DBUS +#ifdef USE_DBUS class QDBusInterface; #endif QT_END_NAMESPACE @@ -52,7 +52,7 @@ private: QString programName; Mode mode; QSystemTrayIcon *trayIcon; -#ifdef QT_DBUS +#ifdef USE_DBUS QDBusInterface *interface; void notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout); From 609acbf43d8d66102878e47a826c037edc270e46 Mon Sep 17 00:00:00 2001 From: Janne Pulkkinen Date: Tue, 27 Sep 2011 17:46:19 +0300 Subject: [PATCH 05/10] Send Coins page not cleared when changing tabs. Clear all button for clearing the entries --- src/qt/bitcoingui.cpp | 4 +++- src/qt/forms/sendcoinsdialog.ui | 24 ++++++++++++++++++++++-- src/qt/sendcoinsdialog.cpp | 1 + 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 3e6b5470063..c9a892fd1de 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -525,7 +525,9 @@ void BitcoinGUI::gotoSendCoinsPage() if(centralWidget->currentWidget() != sendCoinsPage) { // Clear the current contents if we arrived from another tab - sendCoinsPage->clear(); + // Not necessary especially if the user is jumping between Transactions + // and Send Coins pages. - Matoking + //sendCoinsPage->clear(); } centralWidget->setCurrentWidget(sendCoinsPage); diff --git a/src/qt/forms/sendcoinsdialog.ui b/src/qt/forms/sendcoinsdialog.ui index fb2fc99bfe8..f9dd02fef5b 100644 --- a/src/qt/forms/sendcoinsdialog.ui +++ b/src/qt/forms/sendcoinsdialog.ui @@ -25,7 +25,7 @@ 0 0 666 - 162 + 165 @@ -59,7 +59,7 @@ - 12 + 6 @@ -75,6 +75,26 @@ + + + + + 0 + 0 + + + + Clear all + + + + :/icons/remove:/icons/remove + + + 300 + + + diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp index 8954af47425..58eb5c21f79 100644 --- a/src/qt/sendcoinsdialog.cpp +++ b/src/qt/sendcoinsdialog.cpp @@ -22,6 +22,7 @@ SendCoinsDialog::SendCoinsDialog(QWidget *parent) : addEntry(); connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry())); + connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear())); } void SendCoinsDialog::setModel(WalletModel *model) From a1ef0d19b1c1dd30b49bba6b0036eea3414e6ef7 Mon Sep 17 00:00:00 2001 From: Janne Pulkkinen Date: Tue, 27 Sep 2011 19:20:24 +0300 Subject: [PATCH 06/10] Removing the if statement entirely - not needed. --- src/qt/bitcoingui.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index c9a892fd1de..be10b97c0f8 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -522,13 +522,6 @@ void BitcoinGUI::gotoReceiveCoinsPage() void BitcoinGUI::gotoSendCoinsPage() { sendCoinsAction->setChecked(true); - if(centralWidget->currentWidget() != sendCoinsPage) - { - // Clear the current contents if we arrived from another tab - // Not necessary especially if the user is jumping between Transactions - // and Send Coins pages. - Matoking - //sendCoinsPage->clear(); - } centralWidget->setCurrentWidget(sendCoinsPage); exportAction->setEnabled(false); From 59020408744664fde902406cf41de125859d461f Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Tue, 27 Sep 2011 20:47:51 +0200 Subject: [PATCH 07/10] translation handling improvements - automatically build binary translation files in qmake/make - roll translations into resource file and executable, to simply installation --- bitcoin-qt.pro | 15 +++++++++++++++ src/qt/bitcoin.cpp | 2 +- src/qt/bitcoin.qrc | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/bitcoin-qt.pro b/bitcoin-qt.pro index 3fff9ac908e..eefef5a1826 100644 --- a/bitcoin-qt.pro +++ b/bitcoin-qt.pro @@ -168,10 +168,25 @@ FORMS += \ src/qt/forms/askpassphrasedialog.ui CODECFORTR = UTF-8 + # for lrelease/lupdate TRANSLATIONS = src/qt/locale/bitcoin_nl.ts src/qt/locale/bitcoin_de.ts \ src/qt/locale/bitcoin_ru.ts +isEmpty(QMAKE_LRELEASE) { + win32:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]\lrelease.exe + else:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease +} +isEmpty(TS_DIR):TS_DIR = src/qt/locale +# automatically build translations, so they can be included in resource file +TSQM.name = lrelease ${QMAKE_FILE_IN} +TSQM.input = TRANSLATIONS +TSQM.output = $$TS_DIR/${QMAKE_FILE_BASE}.qm +TSQM.commands = $$QMAKE_LRELEASE ${QMAKE_FILE_IN} +TSQM.CONFIG = no_link +QMAKE_EXTRA_COMPILERS += TSQM +bPRE_TARGETDEPS += compiler_TSQM_make_all + OTHER_FILES += \ README.rst diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index daba512adc3..60a9074204b 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -120,7 +120,7 @@ int main(int argc, char *argv[]) // Load language file for system locale QString locale = QLocale::system().name(); QTranslator translator; - translator.load("bitcoin_"+locale); + translator.load(":/translations/"+locale); app.installTranslator(&translator); QSplashScreen splash(QPixmap(":/images/splash"), 0); diff --git a/src/qt/bitcoin.qrc b/src/qt/bitcoin.qrc index be0e4dce611..2985bb60efb 100644 --- a/src/qt/bitcoin.qrc +++ b/src/qt/bitcoin.qrc @@ -45,4 +45,9 @@ res/movies/update_spinner.mng + + locale/bitcoin_de.qm + locale/bitcoin_nl.qm + locale/bitcoin_ru.qm + From a8b95ce6ed5e84d34748ecdd0ff1db4d03377cf0 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 28 Sep 2011 21:35:58 +0200 Subject: [PATCH 08/10] use median filter for peer-reported reported number of blocks - fixes problem that one misconfigured or malicious node can mess up progress bar - implementation in src/util.h - testcase in src/test/util_tests.cpp --- src/main.cpp | 16 +++++---------- src/test/util_tests.cpp | 36 +++++++++++++++++++++++++++++++++ src/util.h | 44 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/test/util_tests.cpp diff --git a/src/main.cpp b/src/main.cpp index e732ddcf5d3..5c0dd7d7658 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,6 @@ uint256 hashGenesisBlock("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3 static CBigNum bnProofOfWorkLimit(~uint256(0) >> 32); const int nTotalBlocksEstimate = 140700; // Conservative estimate of total nr of blocks on main chain const int nInitialBlockThreshold = 120; // Regard blocks up until N-threshold as "initial download" -int nMaxBlocksOfPeers = 0; // Amount of blocks that other nodes claim to have CBlockIndex* pindexGenesisBlock = NULL; int nBestHeight = -1; CBigNum bnBestChainWork = 0; @@ -41,6 +40,8 @@ uint256 hashBestChain = 0; CBlockIndex* pindexBest = NULL; int64 nTimeBestReceived = 0; +CMedianFilter cPeerBlockCounts(5, 0); // Amount of blocks that other nodes claim to have + map mapOrphanBlocks; multimap mapOrphanBlocksByPrev; @@ -65,11 +66,6 @@ int fUseUPnP = false; #endif - - - - - ////////////////////////////////////////////////////////////////////////////// // // dispatching functions @@ -730,7 +726,7 @@ int GetTotalBlocksEstimate() // Return maximum amount of blocks that other nodes claim to have int GetNumBlocksOfPeers() { - return std::max(nMaxBlocksOfPeers, GetTotalBlocksEstimate()); + return std::max(cPeerBlockCounts.median(), GetTotalBlocksEstimate()); } bool IsInitialBlockDownload() @@ -1847,10 +1843,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) pfrom->fSuccessfullyConnected = true; printf("version message: version %d, blocks=%d\n", pfrom->nVersion, pfrom->nStartingHeight); - if(pfrom->nStartingHeight > nMaxBlocksOfPeers) - { - nMaxBlocksOfPeers = pfrom->nStartingHeight; - } + + cPeerBlockCounts.input(pfrom->nStartingHeight); } diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp new file mode 100644 index 00000000000..337d9012774 --- /dev/null +++ b/src/test/util_tests.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +#include "../util.h" + +using namespace std; + +BOOST_AUTO_TEST_SUITE(util_tests) + +BOOST_AUTO_TEST_CASE(util_MedianFilter) +{ + CMedianFilter filter(5, 15); + + BOOST_CHECK(filter.median() == 15); + + filter.input(20); // [15 20] + BOOST_CHECK(filter.median() == 17); + + filter.input(30); // [15 20 30] + BOOST_CHECK(filter.median() == 20); + + filter.input(3); // [3 15 20 30] + BOOST_CHECK(filter.median() == 17); + + filter.input(7); // [3 7 15 20 30] + BOOST_CHECK(filter.median() == 15); + + filter.input(18); // [3 7 18 20 30] + BOOST_CHECK(filter.median() == 18); + + filter.input(0); // [0 3 7 18 30] + BOOST_CHECK(filter.median() == 7); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/util.h b/src/util.h index fabcaf93035..290195cbfa0 100644 --- a/src/util.h +++ b/src/util.h @@ -565,6 +565,50 @@ inline uint160 Hash160(const std::vector& vch) } +// Median filter over a stream of values +// Returns the median of the last N numbers +template class CMedianFilter +{ +private: + std::vector vValues; + std::vector vSorted; + int nSize; +public: + CMedianFilter(int size, T initial_value): + nSize(size) + { + vValues.reserve(size); + vValues.push_back(initial_value); + vSorted = vValues; + } + + void input(T value) + { + if(vValues.size() == nSize) + { + vValues.erase(vValues.begin()); + } + vValues.push_back(value); + + vSorted.resize(vValues.size()); + std::copy(vValues.begin(), vValues.end(), vSorted.begin()); + std::sort(vSorted.begin(), vSorted.end()); + } + + T median() const + { + int size = vSorted.size(); + if(size & 1) // Odd number of elements + { + return vSorted[size/2]; + } + else // Even number of elements + { + return (vSorted[size/2-1] + vSorted[size/2]) / 2; + } + } +}; + From f33a43efdf2aacd3ada68a7027758877fa321169 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 28 Sep 2011 22:01:47 +0200 Subject: [PATCH 09/10] Update "other files" in Qt Creator with all documentation now that the projects merged --- bitcoin-qt.pro | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bitcoin-qt.pro b/bitcoin-qt.pro index 290d5d9dfb9..125b876752e 100644 --- a/bitcoin-qt.pro +++ b/bitcoin-qt.pro @@ -191,8 +191,9 @@ TSQM.CONFIG = no_link QMAKE_EXTRA_COMPILERS += TSQM bPRE_TARGETDEPS += compiler_TSQM_make_all +# "Other files" to show in Qt Creator OTHER_FILES += \ - README.rst + doc/*.rst doc/*.txt doc/README README.md # platform specific defaults, if not overridden on command line isEmpty(BOOST_LIB_SUFFIX) { From 20091df725e56376f6df0f71d19c63a34a5bf71d Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Thu, 29 Sep 2011 17:17:42 +0200 Subject: [PATCH 10/10] Add assertion size>0 to MedianFilter --- src/util.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/util.h b/src/util.h index 97d638c93e5..60e99431b14 100644 --- a/src/util.h +++ b/src/util.h @@ -599,6 +599,7 @@ public: T median() const { int size = vSorted.size(); + assert(size>0); if(size & 1) // Odd number of elements { return vSorted[size/2];