Browse Source

Merge pull request #4748

ad49c25 Split up util.cpp/h (Wladimir J. van der Laan)
f841aa2 Move `COIN` and `CENT` to core.h (Wladimir J. van der Laan)
6e5fd00 Move `*Version()` functions to version.h/cpp (Wladimir J. van der Laan)
b4aa769 Move `S_I*` constants and `MSG_NOSIGNAL` to compat.h (Wladimir J. van der Laan)
af8297c Move functions in wallet.h to implementation file (Wladimir J. van der Laan)
651480c move functions in main and net to implementation files (Wladimir J. van der Laan)
610a8c0 Move SetThreadPriority implementation to util.cpp instead of the header (Wladimir J. van der Laan)
f780e65 Remove unused function `ByteReverse` from util.h (Wladimir J. van der Laan)
121d6ad Remove unused `alignup` function from util.h (Wladimir J. van der Laan)
d1e26d4 Move CMedianFilter to timedata.cpp (Wladimir J. van der Laan)
pull/1/head
Pieter Wuille 8 years ago
parent
commit
3da58b216b
No known key found for this signature in database
GPG Key ID: 8F653255C87992E0
  1. 6
      src/Makefile.am
  2. 1
      src/Makefile.test.include
  3. 1
      src/alert.cpp
  4. 2
      src/bitcoin-cli.cpp
  5. 1
      src/bitcoin-tx.cpp
  6. 1
      src/bitcoind.cpp
  7. 25
      src/compat.h
  8. 3
      src/core.h
  9. 1
      src/core_write.cpp
  10. 1
      src/crypter.cpp
  11. 2
      src/db.cpp
  12. 2
      src/init.cpp
  13. 1
      src/keystore.cpp
  14. 62
      src/main.cpp
  15. 64
      src/main.h
  16. 6
      src/miner.cpp
  17. 155
      src/net.cpp
  18. 161
      src/net.h
  19. 2
      src/netbase.cpp
  20. 1
      src/noui.cpp
  21. 1
      src/pow.cpp
  22. 1
      src/qt/bitcoin.cpp
  23. 1
      src/qt/bitcoingui.cpp
  24. 1
      src/qt/clientmodel.cpp
  25. 1
      src/qt/optionsdialog.cpp
  26. 1
      src/qt/paymentserver.cpp
  27. 1
      src/qt/transactiondesc.cpp
  28. 5
      src/qt/utilitydialog.cpp
  29. 4
      src/random.cpp
  30. 1
      src/rpcblockchain.cpp
  31. 2
      src/rpcdump.cpp
  32. 1
      src/rpcmining.cpp
  33. 4
      src/rpcprotocol.cpp
  34. 6
      src/rpcserver.cpp
  35. 3
      src/script.cpp
  36. 3
      src/script.h
  37. 1
      src/test/DoS_tests.cpp
  38. 1
      src/test/alert_tests.cpp
  39. 2
      src/test/allocator_tests.cpp
  40. 2
      src/test/base32_tests.cpp
  41. 2
      src/test/base64_tests.cpp
  42. 1
      src/test/checkblock_tests.cpp
  43. 2
      src/test/crypto_tests.cpp
  44. 2
      src/test/hash_tests.cpp
  45. 1
      src/test/test_bitcoin.cpp
  46. 38
      src/test/timedata_tests.cpp
  47. 28
      src/test/util_tests.cpp
  48. 6
      src/timedata.cpp
  49. 59
      src/timedata.h
  50. 1
      src/tinyformat.h
  51. 1
      src/txdb.cpp
  52. 1
      src/txmempool.cpp
  53. 3
      src/uint256.cpp
  54. 583
      src/util.cpp
  55. 300
      src/util.h
  56. 75
      src/utilmoneystr.cpp
  57. 19
      src/utilmoneystr.h
  58. 496
      src/utilstrencodings.cpp
  59. 97
      src/utilstrencodings.h
  60. 66
      src/utiltime.cpp
  61. 20
      src/utiltime.h
  62. 28
      src/version.cpp
  63. 4
      src/version.h
  64. 25
      src/wallet.cpp
  65. 24
      src/wallet.h
  66. 3
      src/walletdb.cpp

6
src/Makefile.am

@ -109,6 +109,9 @@ BITCOIN_CORE_H = \ @@ -109,6 +109,9 @@ BITCOIN_CORE_H = \
ui_interface.h \
uint256.h \
util.h \
utilstrencodings.h \
utilmoneystr.h \
utiltime.h \
version.h \
walletdb.h \
wallet.h \
@ -219,6 +222,9 @@ libbitcoin_util_a_SOURCES = \ @@ -219,6 +222,9 @@ libbitcoin_util_a_SOURCES = \
sync.cpp \
uint256.cpp \
util.cpp \
utilstrencodings.cpp \
utilmoneystr.cpp \
utiltime.cpp \
version.cpp \
$(BITCOIN_CORE_H)

1
src/Makefile.test.include

@ -61,6 +61,7 @@ BITCOIN_TESTS =\ @@ -61,6 +61,7 @@ BITCOIN_TESTS =\
test/sigopcount_tests.cpp \
test/skiplist_tests.cpp \
test/test_bitcoin.cpp \
test/timedata_tests.cpp \
test/transaction_tests.cpp \
test/uint256_tests.cpp \
test/univalue_tests.cpp \

1
src/alert.cpp

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/foreach.hpp>
#include <boost/thread.hpp>
using namespace std;

2
src/bitcoin-cli.cpp

@ -8,6 +8,8 @@ @@ -8,6 +8,8 @@
#include "rpcclient.h"
#include "rpcprotocol.h"
#include "chainparamsbase.h"
#include "utilstrencodings.h"
#include "version.h"
#include <boost/filesystem/operations.hpp>

1
src/bitcoin-tx.cpp

@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
#include "base58.h"
#include "util.h"
#include "utilmoneystr.h"
#include "core.h"
#include "main.h" // for MAX_BLOCK_SIZE
#include "keystore.h"

1
src/bitcoind.cpp

@ -12,6 +12,7 @@ @@ -12,6 +12,7 @@
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
/* Introduction text for doxygen: */

25
src/compat.h

@ -59,4 +59,29 @@ typedef u_int SOCKET; @@ -59,4 +59,29 @@ typedef u_int SOCKET;
#define SOCKET_ERROR -1
#endif
#ifdef WIN32
#ifndef S_IRUSR
#define S_IRUSR 0400
#define S_IWUSR 0200
#endif
#else
#define MAX_PATH 1024
#endif
// As Solaris does not have the MSG_NOSIGNAL flag for send(2) syscall, it is defined as 0
#if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
#define MSG_NOSIGNAL 0
#endif
#ifndef WIN32
// PRIO_MAX is not defined on Solaris
#ifndef PRIO_MAX
#define PRIO_MAX 20
#endif
#define THREAD_PRIORITY_LOWEST PRIO_MAX
#define THREAD_PRIORITY_BELOW_NORMAL 2
#define THREAD_PRIORITY_NORMAL 0
#define THREAD_PRIORITY_ABOVE_NORMAL (-2)
#endif
#endif // _BITCOIN_COMPAT_H

3
src/core.h

@ -14,6 +14,9 @@ @@ -14,6 +14,9 @@
class CTransaction;
static const int64_t COIN = 100000000;
static const int64_t CENT = 1000000;
/** No amount larger than this (in satoshi) is valid */
static const int64_t MAX_MONEY = 21000000 * COIN;
inline bool MoneyRange(int64_t nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }

1
src/core_write.cpp

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
#include "core.h"
#include "serialize.h"
#include "util.h"
#include "utilmoneystr.h"
#include "base58.h"
using namespace std;

1
src/crypter.cpp

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
#include "crypter.h"
#include "script.h"
#include "util.h"
#include <string>
#include <vector>

2
src/db.cpp

@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
#include "hash.h"
#include "protocol.h"
#include "util.h"
#include "utilstrencodings.h"
#include <stdint.h>
@ -17,6 +18,7 @@ @@ -17,6 +18,7 @@
#endif
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
#include <boost/version.hpp>
#include <openssl/rand.h>

2
src/init.cpp

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
#include "txdb.h"
#include "ui_interface.h"
#include "util.h"
#include "utilmoneystr.h"
#ifdef ENABLE_WALLET
#include "db.h"
#include "wallet.h"
@ -36,6 +37,7 @@ @@ -36,6 +37,7 @@
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/thread.hpp>
#include <openssl/crypto.h>
using namespace boost;

1
src/keystore.cpp

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
#include "crypter.h"
#include "key.h"
#include "script.h"
#include "util.h"
#include <boost/foreach.hpp>

62
src/main.cpp

@ -24,6 +24,7 @@ @@ -24,6 +24,7 @@
#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/thread.hpp>
using namespace std;
using namespace boost;
@ -4655,7 +4656,68 @@ bool SendMessages(CNode* pto, bool fSendTrickle) @@ -4655,7 +4656,68 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
}
bool CBlockUndo::WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock)
{
// Open history file to append
CAutoFile fileout = CAutoFile(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
if (!fileout)
return error("CBlockUndo::WriteToDisk : OpenUndoFile failed");
// Write index header
unsigned int nSize = fileout.GetSerializeSize(*this);
fileout << FLATDATA(Params().MessageStart()) << nSize;
// Write undo data
long fileOutPos = ftell(fileout);
if (fileOutPos < 0)
return error("CBlockUndo::WriteToDisk : ftell failed");
pos.nPos = (unsigned int)fileOutPos;
fileout << *this;
// calculate & write checksum
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
hasher << hashBlock;
hasher << *this;
fileout << hasher.GetHash();
// Flush stdio buffers and commit to disk before returning
fflush(fileout);
if (!IsInitialBlockDownload())
FileCommit(fileout);
return true;
}
bool CBlockUndo::ReadFromDisk(const CDiskBlockPos &pos, const uint256 &hashBlock)
{
// Open history file to read
CAutoFile filein = CAutoFile(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
if (!filein)
return error("CBlockUndo::ReadFromDisk : OpenBlockFile failed");
// Read block
uint256 hashChecksum;
try {
filein >> *this;
filein >> hashChecksum;
}
catch (std::exception &e) {
return error("%s : Deserialize or I/O error - %s", __func__, e.what());
}
// Verify checksum
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
hasher << hashBlock;
hasher << *this;
if (hashChecksum != hasher.GetHash())
return error("CBlockUndo::ReadFromDisk : Checksum mismatch");
return true;
}
std::string CBlockFileInfo::ToString() const {
return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst).c_str(), DateTimeStrFormat("%Y-%m-%d", nTimeLast).c_str());
}

64
src/main.h

@ -312,64 +312,8 @@ public: @@ -312,64 +312,8 @@ public:
READWRITE(vtxundo);
)
bool WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock)
{
// Open history file to append
CAutoFile fileout = CAutoFile(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
if (!fileout)
return error("CBlockUndo::WriteToDisk : OpenUndoFile failed");
// Write index header
unsigned int nSize = fileout.GetSerializeSize(*this);
fileout << FLATDATA(Params().MessageStart()) << nSize;
// Write undo data
long fileOutPos = ftell(fileout);
if (fileOutPos < 0)
return error("CBlockUndo::WriteToDisk : ftell failed");
pos.nPos = (unsigned int)fileOutPos;
fileout << *this;
// calculate & write checksum
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
hasher << hashBlock;
hasher << *this;
fileout << hasher.GetHash();
// Flush stdio buffers and commit to disk before returning
fflush(fileout);
if (!IsInitialBlockDownload())
FileCommit(fileout);
return true;
}
bool ReadFromDisk(const CDiskBlockPos &pos, const uint256 &hashBlock)
{
// Open history file to read
CAutoFile filein = CAutoFile(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
if (!filein)
return error("CBlockUndo::ReadFromDisk : OpenBlockFile failed");
// Read block
uint256 hashChecksum;
try {
filein >> *this;
filein >> hashChecksum;
}
catch (std::exception &e) {
return error("%s : Deserialize or I/O error - %s", __func__, e.what());
}
// Verify checksum
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
hasher << hashBlock;
hasher << *this;
if (hashChecksum != hasher.GetHash())
return error("CBlockUndo::ReadFromDisk : Checksum mismatch");
return true;
}
bool WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock);
bool ReadFromDisk(const CDiskBlockPos &pos, const uint256 &hashBlock);
};
@ -625,9 +569,7 @@ public: @@ -625,9 +569,7 @@ public:
SetNull();
}
std::string ToString() const {
return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst).c_str(), DateTimeStrFormat("%Y-%m-%d", nTimeLast).c_str());
}
std::string ToString() const;
// update statistics (does not update nSize)
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {

6
src/miner.cpp

@ -3,8 +3,6 @@ @@ -3,8 +3,6 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <inttypes.h>
#include "miner.h"
#include "core.h"
@ -12,10 +10,14 @@ @@ -12,10 +10,14 @@
#include "main.h"
#include "net.h"
#include "pow.h"
#include "util.h"
#include "utilmoneystr.h"
#ifdef ENABLE_WALLET
#include "wallet.h"
#endif
#include <boost/thread.hpp>
using namespace std;
//////////////////////////////////////////////////////////////////////////////

155
src/net.cpp

@ -28,6 +28,7 @@ @@ -28,6 +28,7 @@
#endif
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
// Dump addresses to peers.dat every 15 minutes (900s)
#define DUMP_ADDRESSES_INTERVAL 900
@ -2037,3 +2038,157 @@ bool CAddrDB::Read(CAddrMan& addr) @@ -2037,3 +2038,157 @@ bool CAddrDB::Read(CAddrMan& addr)
return true;
}
unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); }
unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); }
CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) : ssSend(SER_NETWORK, INIT_PROTO_VERSION), setAddrKnown(5000)
{
nServices = 0;
hSocket = hSocketIn;
nRecvVersion = INIT_PROTO_VERSION;
nLastSend = 0;
nLastRecv = 0;
nSendBytes = 0;
nRecvBytes = 0;
nTimeConnected = GetTime();
addr = addrIn;
addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
nVersion = 0;
strSubVer = "";
fWhitelisted = false;
fOneShot = false;
fClient = false; // set by version message
fInbound = fInboundIn;
fNetworkNode = false;
fSuccessfullyConnected = false;
fDisconnect = false;
nRefCount = 0;
nSendSize = 0;
nSendOffset = 0;
hashContinue = 0;
pindexLastGetBlocksBegin = 0;
hashLastGetBlocksEnd = 0;
nStartingHeight = -1;
fStartSync = false;
fGetAddr = false;
fRelayTxes = false;
setInventoryKnown.max_size(SendBufferSize() / 1000);
pfilter = new CBloomFilter();
nPingNonceSent = 0;
nPingUsecStart = 0;
nPingUsecTime = 0;
fPingQueued = false;
{
LOCK(cs_nLastNodeId);
id = nLastNodeId++;
}
if (fLogIPs)
LogPrint("net", "Added connection to %s peer=%d\n", addrName, id);
else
LogPrint("net", "Added connection peer=%d\n", id);
// Be shy and don't send version until we hear
if (hSocket != INVALID_SOCKET && !fInbound)
PushVersion();
GetNodeSignals().InitializeNode(GetId(), this);
}
CNode::~CNode()
{
CloseSocket(hSocket);
if (pfilter)
delete pfilter;
GetNodeSignals().FinalizeNode(GetId());
}
void CNode::AskFor(const CInv& inv)
{
// We're using mapAskFor as a priority queue,
// the key is the earliest time the request can be sent
int64_t nRequestTime;
limitedmap<CInv, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv);
if (it != mapAlreadyAskedFor.end())
nRequestTime = it->second;
else
nRequestTime = 0;
LogPrint("net", "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000).c_str(), id);
// Make sure not to reuse time indexes to keep things in the same order
int64_t nNow = GetTimeMicros() - 1000000;
static int64_t nLastTime;
++nLastTime;
nNow = std::max(nNow, nLastTime);
nLastTime = nNow;
// Each retry is 2 minutes after the last
nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
if (it != mapAlreadyAskedFor.end())
mapAlreadyAskedFor.update(it, nRequestTime);
else
mapAlreadyAskedFor.insert(std::make_pair(inv, nRequestTime));
mapAskFor.insert(std::make_pair(nRequestTime, inv));
}
void CNode::BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
{
ENTER_CRITICAL_SECTION(cs_vSend);
assert(ssSend.size() == 0);
ssSend << CMessageHeader(pszCommand, 0);
LogPrint("net", "sending: %s ", pszCommand);
}
void CNode::AbortMessage() UNLOCK_FUNCTION(cs_vSend)
{
ssSend.clear();
LEAVE_CRITICAL_SECTION(cs_vSend);
LogPrint("net", "(aborted)\n");
}
void CNode::EndMessage() UNLOCK_FUNCTION(cs_vSend)
{
// The -*messagestest options are intentionally not documented in the help message,
// since they are only used during development to debug the networking code and are
// not intended for end-users.
if (mapArgs.count("-dropmessagestest") && GetRand(GetArg("-dropmessagestest", 2)) == 0)
{
LogPrint("net", "dropmessages DROPPING SEND MESSAGE\n");
AbortMessage();
return;
}
if (mapArgs.count("-fuzzmessagestest"))
Fuzz(GetArg("-fuzzmessagestest", 10));
if (ssSend.size() == 0)
return;
// Set the size
unsigned int nSize = ssSend.size() - CMessageHeader::HEADER_SIZE;
memcpy((char*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], &nSize, sizeof(nSize));
// Set the checksum
uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
unsigned int nChecksum = 0;
memcpy(&nChecksum, &hash, sizeof(nChecksum));
assert(ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum));
memcpy((char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], &nChecksum, sizeof(nChecksum));
LogPrint("net", "(%d bytes) peer=%d\n", nSize, id);
std::deque<CSerializeData>::iterator it = vSendMsg.insert(vSendMsg.end(), CSerializeData());
ssSend.GetAndClear(*it);
nSendSize += (*it).size();
// If write queue empty, attempt "optimistic write"
if (it == vSendMsg.begin())
SocketSendData(this);
LEAVE_CRITICAL_SECTION(cs_vSend);
}

161
src/net.h

@ -16,7 +16,7 @@ @@ -16,7 +16,7 @@
#include "random.h"
#include "sync.h"
#include "uint256.h"
#include "util.h"
#include "utilstrencodings.h"
#include <deque>
#include <stdint.h>
@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
#include <arpa/inet.h>
#endif
#include <boost/filesystem/path.hpp>
#include <boost/foreach.hpp>
#include <boost/signals2/signal.hpp>
@ -51,8 +52,8 @@ static const bool DEFAULT_UPNP = USE_UPNP; @@ -51,8 +52,8 @@ static const bool DEFAULT_UPNP = USE_UPNP;
static const bool DEFAULT_UPNP = false;
#endif
inline unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); }
inline unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); }
unsigned int ReceiveFloodSize();
unsigned int SendBufferSize();
void AddOneShot(std::string strDest);
bool RecvLine(SOCKET hSocket, std::string& strLine);
@ -300,70 +301,8 @@ public: @@ -300,70 +301,8 @@ public:
// Whether a ping is requested.
bool fPingQueued;
CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn = "", bool fInboundIn=false) : ssSend(SER_NETWORK, INIT_PROTO_VERSION), setAddrKnown(5000)
{
nServices = 0;
hSocket = hSocketIn;
nRecvVersion = INIT_PROTO_VERSION;
nLastSend = 0;
nLastRecv = 0;
nSendBytes = 0;
nRecvBytes = 0;
nTimeConnected = GetTime();
addr = addrIn;
addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
nVersion = 0;
strSubVer = "";
fWhitelisted = false;
fOneShot = false;
fClient = false; // set by version message
fInbound = fInboundIn;
fNetworkNode = false;
fSuccessfullyConnected = false;
fDisconnect = false;
nRefCount = 0;
nSendSize = 0;
nSendOffset = 0;
hashContinue = 0;
pindexLastGetBlocksBegin = 0;
hashLastGetBlocksEnd = 0;
nStartingHeight = -1;
fStartSync = false;
fGetAddr = false;
fRelayTxes = false;
setInventoryKnown.max_size(SendBufferSize() / 1000);
pfilter = new CBloomFilter();
nPingNonceSent = 0;
nPingUsecStart = 0;
nPingUsecTime = 0;
fPingQueued = false;
{
LOCK(cs_nLastNodeId);
id = nLastNodeId++;
}
if (fLogIPs)
LogPrint("net", "Added connection to %s peer=%d\n", addrName, id);
else
LogPrint("net", "Added connection peer=%d\n", id);
// Be shy and don't send version until we hear
if (hSocket != INVALID_SOCKET && !fInbound)
PushVersion();
GetNodeSignals().InitializeNode(GetId(), this);
}
~CNode()
{
CloseSocket(hSocket);
if (pfilter)
delete pfilter;
GetNodeSignals().FinalizeNode(GetId());
}
CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn = "", bool fInboundIn=false);
~CNode();
private:
// Network usage totals
@ -452,96 +391,16 @@ public: @@ -452,96 +391,16 @@ public:
}
}
void AskFor(const CInv& inv)
{
// We're using mapAskFor as a priority queue,
// the key is the earliest time the request can be sent
int64_t nRequestTime;
limitedmap<CInv, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv);
if (it != mapAlreadyAskedFor.end())
nRequestTime = it->second;
else
nRequestTime = 0;
LogPrint("net", "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000).c_str(), id);
// Make sure not to reuse time indexes to keep things in the same order
int64_t nNow = GetTimeMicros() - 1000000;
static int64_t nLastTime;
++nLastTime;
nNow = std::max(nNow, nLastTime);
nLastTime = nNow;
// Each retry is 2 minutes after the last
nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
if (it != mapAlreadyAskedFor.end())
mapAlreadyAskedFor.update(it, nRequestTime);
else
mapAlreadyAskedFor.insert(std::make_pair(inv, nRequestTime));
mapAskFor.insert(std::make_pair(nRequestTime, inv));
}
void AskFor(const CInv& inv);
// TODO: Document the postcondition of this function. Is cs_vSend locked?
void BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
{
ENTER_CRITICAL_SECTION(cs_vSend);
assert(ssSend.size() == 0);
ssSend << CMessageHeader(pszCommand, 0);
LogPrint("net", "sending: %s ", pszCommand);
}
void BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend);
// TODO: Document the precondition of this function. Is cs_vSend locked?
void AbortMessage() UNLOCK_FUNCTION(cs_vSend)
{
ssSend.clear();
LEAVE_CRITICAL_SECTION(cs_vSend);
LogPrint("net", "(aborted)\n");
}
void AbortMessage() UNLOCK_FUNCTION(cs_vSend);
// TODO: Document the precondition of this function. Is cs_vSend locked?
void EndMessage() UNLOCK_FUNCTION(cs_vSend)
{
// The -*messagestest options are intentionally not documented in the help message,
// since they are only used during development to debug the networking code and are
// not intended for end-users.
if (mapArgs.count("-dropmessagestest") && GetRand(GetArg("-dropmessagestest", 2)) == 0)
{
LogPrint("net", "dropmessages DROPPING SEND MESSAGE\n");
AbortMessage();
return;
}
if (mapArgs.count("-fuzzmessagestest"))
Fuzz(GetArg("-fuzzmessagestest", 10));
if (ssSend.size() == 0)
return;
// Set the size
unsigned int nSize = ssSend.size() - CMessageHeader::HEADER_SIZE;
memcpy((char*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], &nSize, sizeof(nSize));
// Set the checksum
uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
unsigned int nChecksum = 0;
memcpy(&nChecksum, &hash, sizeof(nChecksum));
assert(ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum));
memcpy((char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], &nChecksum, sizeof(nChecksum));
LogPrint("net", "(%d bytes) peer=%d\n", nSize, id);
std::deque<CSerializeData>::iterator it = vSendMsg.insert(vSendMsg.end(), CSerializeData());
ssSend.GetAndClear(*it);
nSendSize += (*it).size();
// If write queue empty, attempt "optimistic write"
if (it == vSendMsg.begin())
SocketSendData(this);
LEAVE_CRITICAL_SECTION(cs_vSend);
}
void EndMessage() UNLOCK_FUNCTION(cs_vSend);
void PushVersion();

2
src/netbase.cpp

@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
#include "sync.h"
#include "uint256.h"
#include "util.h"
#include "utilstrencodings.h"
#ifdef HAVE_GETADDRINFO_A
#include <netdb.h>
@ -27,6 +28,7 @@ @@ -27,6 +28,7 @@
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
#include <boost/thread.hpp>
#if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
#define MSG_NOSIGNAL 0

1
src/noui.cpp

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
#include "ui_interface.h"
#include "util.h"
#include <cstdio>
#include <stdint.h>
#include <string>

1
src/pow.cpp

@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
#include "core.h"
#include "main.h"
#include "uint256.h"
#include "util.h"
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
{

1
src/qt/bitcoin.cpp

@ -33,6 +33,7 @@ @@ -33,6 +33,7 @@
#include <stdint.h>
#include <boost/filesystem/operations.hpp>
#include <boost/thread.hpp>
#include <QApplication>
#include <QDebug>
#include <QLibraryInfo>

1
src/qt/bitcoingui.cpp

@ -24,6 +24,7 @@ @@ -24,6 +24,7 @@
#endif
#include "init.h"
#include "util.h"
#include "ui_interface.h"
#include <iostream>

1
src/qt/clientmodel.cpp

@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
#include "main.h"
#include "net.h"
#include "ui_interface.h"
#include "util.h"
#include <stdint.h>

1
src/qt/optionsdialog.cpp

@ -21,6 +21,7 @@ @@ -21,6 +21,7 @@
#include "wallet.h" // for CWallet::minTxFee
#endif
#include <boost/thread.hpp>
#include <QDir>
#include <QIntValidator>
#include <QLocale>

1
src/qt/paymentserver.cpp

@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
#include "base58.h"
#include "ui_interface.h"
#include "util.h"
#include "wallet.h"
#include <cstdlib>

1
src/qt/transactiondesc.cpp

@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
#include "transactionrecord.h"
#include "timedata.h"
#include "ui_interface.h"
#include "util.h"
#include "wallet.h"
#include <stdint.h>

5
src/qt/utilitydialog.cpp

@ -10,9 +10,10 @@ @@ -10,9 +10,10 @@
#include "clientmodel.h"
#include "guiutil.h"
#include "clientversion.h"
#include "init.h"
#include "util.h"
#include "version.h"
#include <stdio.h>
#include <QLabel>
#include <QRegExp>

4
src/random.cpp

@ -8,12 +8,14 @@ @@ -8,12 +8,14 @@
#ifdef WIN32
#include "compat.h" // for Windows API
#endif
#include "serialize.h" // for begin_ptr(vec)
#include "util.h" // for LogPrint()
#include "utilstrencodings.h" // for GetTime()
#ifndef WIN32
#include <sys/time.h>
#endif
#include <limits>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/rand.h>

1
src/rpcblockchain.cpp

@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
#include "main.h"
#include "rpcserver.h"
#include "sync.h"
#include "util.h"
#include <stdint.h>

2
src/rpcdump.cpp

@ -7,6 +7,8 @@ @@ -7,6 +7,8 @@
#include "init.h"
#include "main.h"
#include "sync.h"
#include "utiltime.h"
#include "util.h"
#include "wallet.h"
#include <fstream>

1
src/rpcmining.cpp

@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
#include "miner.h"
#include "pow.h"
#include "core_io.h"
#include "util.h"
#ifdef ENABLE_WALLET
#include "db.h"
#include "wallet.h"

4
src/rpcprotocol.cpp

@ -6,6 +6,10 @@ @@ -6,6 +6,10 @@
#include "rpcprotocol.h"
#include "util.h"
#include "tinyformat.h"
#include "utilstrencodings.h"
#include "utiltime.h"
#include "version.h"
#include <stdint.h>

6
src/rpcserver.cpp

@ -23,6 +23,7 @@ @@ -23,6 +23,7 @@
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include "json/json_spirit_writer_template.h"
using namespace boost;
@ -82,6 +83,11 @@ void RPCTypeCheck(const Object& o, @@ -82,6 +83,11 @@ void RPCTypeCheck(const Object& o,
}
}
static inline int64_t roundint64(double d)
{
return (int64_t)(d > 0 ? d + 0.5 : d - 0.5);
}
int64_t AmountFromValue(const Value& value)
{
double dAmount = value.get_real();

3
src/script.cpp

@ -18,8 +18,9 @@ @@ -18,8 +18,9 @@
#include "util.h"
#include <boost/foreach.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/thread.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <boost/tuple/tuple.hpp>
using namespace std;
using namespace boost;

3
src/script.h

@ -7,7 +7,8 @@ @@ -7,7 +7,8 @@
#define H_BITCOIN_SCRIPT
#include "key.h"
#include "util.h"
#include "utilstrencodings.h"
#include "tinyformat.h"
#include <stdexcept>
#include <stdint.h>

1
src/test/DoS_tests.cpp

@ -14,6 +14,7 @@ @@ -14,6 +14,7 @@
#include "pow.h"
#include "script.h"
#include "serialize.h"
#include "util.h"
#include <stdint.h>

1
src/test/alert_tests.cpp

@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
#include "serialize.h"
#include "util.h"
#include "utilstrencodings.h"
#include "version.h"
#include <fstream>

2
src/test/allocator_tests.cpp

@ -4,6 +4,8 @@ @@ -4,6 +4,8 @@
#include "util.h"
#include "allocators.h"
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(allocator_tests)

2
src/test/base32_tests.cpp

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "util.h"
#include "utilstrencodings.h"
#include <boost/test/unit_test.hpp>

2
src/test/base64_tests.cpp

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "util.h"
#include "utilstrencodings.h"
#include <boost/test/unit_test.hpp>

1
src/test/checkblock_tests.cpp

@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
#include "main.h"
#include "utiltime.h"
#include <cstdio>

2
src/test/crypto_tests.cpp

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
#include "crypto/sha1.h"
#include "crypto/sha2.h"
#include "random.h"
#include "util.h"
#include "utilstrencodings.h"
#include <vector>

2
src/test/hash_tests.cpp

@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "hash.h"
#include "util.h"
#include "utilstrencodings.h"
#include <vector>

1
src/test/test_bitcoin.cpp

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/thread.hpp>
CClientUIInterface uiInterface;
CWallet* pwalletMain;

38
src/test/timedata_tests.cpp

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
// Copyright (c) 2011-2014 The Bitcoin Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//
#include "timedata.h"
#include <boost/test/unit_test.hpp>
using namespace std;
BOOST_AUTO_TEST_SUITE(timedata_tests)
BOOST_AUTO_TEST_CASE(util_MedianFilter)
{
CMedianFilter<int> filter(5, 15);
BOOST_CHECK_EQUAL(filter.median(), 15);
filter.input(20); // [15 20]
BOOST_CHECK_EQUAL(filter.median(), 17);
filter.input(30); // [15 20 30]
BOOST_CHECK_EQUAL(filter.median(), 20);
filter.input(3); // [3 15 20 30]
BOOST_CHECK_EQUAL(filter.median(), 17);
filter.input(7); // [3 7 15 20 30]
BOOST_CHECK_EQUAL(filter.median(), 15);
filter.input(18); // [3 7 18 20 30]
BOOST_CHECK_EQUAL(filter.median(), 18);
filter.input(0); // [0 3 7 18 30]
BOOST_CHECK_EQUAL(filter.median(), 7);
}
BOOST_AUTO_TEST_SUITE_END()

28
src/test/util_tests.cpp

@ -4,8 +4,11 @@ @@ -4,8 +4,11 @@
#include "util.h"
#include "core.h"
#include "random.h"
#include "sync.h"
#include "utilstrencodings.h"
#include "utilmoneystr.h"
#include <stdint.h>
#include <vector>
@ -36,31 +39,6 @@ BOOST_AUTO_TEST_CASE(util_criticalsection) @@ -36,31 +39,6 @@ BOOST_AUTO_TEST_CASE(util_criticalsection)
} while(0);
}
BOOST_AUTO_TEST_CASE(util_MedianFilter)
{
CMedianFilter<int> filter(5, 15);
BOOST_CHECK_EQUAL(filter.median(), 15);
filter.input(20); // [15 20]
BOOST_CHECK_EQUAL(filter.median(), 17);
filter.input(30); // [15 20 30]
BOOST_CHECK_EQUAL(filter.median(), 20);
filter.input(3); // [3 15 20 30]
BOOST_CHECK_EQUAL(filter.median(), 17);
filter.input(7); // [3 7 15 20 30]
BOOST_CHECK_EQUAL(filter.median(), 15);
filter.input(18); // [3 7 18 20 30]
BOOST_CHECK_EQUAL(filter.median(), 18);
filter.input(0); // [0 3 7 18 30]
BOOST_CHECK_EQUAL(filter.median(), 7);
}
static const unsigned char ParseHex_expected[65] = {
0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,

6
src/timedata.cpp

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
#include "sync.h"
#include "ui_interface.h"
#include "util.h"
#include "utilstrencodings.h"
#include <boost/foreach.hpp>
@ -35,6 +36,11 @@ int64_t GetAdjustedTime() @@ -35,6 +36,11 @@ int64_t GetAdjustedTime()
return GetTime() + GetTimeOffset();
}
static int64_t abs64(int64_t n)
{
return (n >= 0 ? n : -n);
}
void AddTimeData(const CNetAddr& ip, int64_t nTime)
{
int64_t nOffsetSample = nTime - GetTime();

59
src/timedata.h

@ -6,9 +6,68 @@ @@ -6,9 +6,68 @@
#define BITCOIN_TIMEDATA_H
#include <stdint.h>
#include <vector>
#include <algorithm>
#include <assert.h>
class CNetAddr;
/** Median filter over a stream of values.
* Returns the median of the last N numbers
*/
template <typename T> class CMedianFilter
{
private:
std::vector<T> vValues;
std::vector<T> vSorted;
unsigned int nSize;
public:
CMedianFilter(unsigned 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();
assert(size>0);
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;
}
}
int size() const
{
return vValues.size();
}
std::vector<T> sorted () const
{
return vSorted;
}
};
/* Functions to keep track of adjusted P2P time */
int64_t GetTimeOffset();
int64_t GetAdjustedTime();

1
src/tinyformat.h

@ -121,6 +121,7 @@ namespace tfm = tinyformat; @@ -121,6 +121,7 @@ namespace tfm = tinyformat;
#include <cassert>
#include <iostream>
#include <sstream>
#include <stdexcept>
#ifndef TINYFORMAT_ERROR
# define TINYFORMAT_ERROR(reason) assert(0 && reason)

1
src/txdb.cpp

@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
#include "pow.h"
#include "uint256.h"
#include <boost/thread.hpp>
#include <stdint.h>
using namespace std;

1
src/txmempool.cpp

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
#include "core.h"
#include "txmempool.h"
#include "util.h"
#include <boost/circular_buffer.hpp>