Skip to content

Commit 14f888c

Browse files
committed
Move network-time related functions to timedata.cpp/h
The network time-offset-mangement functions from util.cpp are moved to timedata.(cpp|h). This breaks the dependency of util on netbase.
1 parent 208bf5b commit 14f888c

13 files changed

+118
-81
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ BITCOIN_CORE_H = \
8080
serialize.h \
8181
sync.h \
8282
threadsafety.h \
83+
timedata.h \
8384
tinyformat.h \
8485
txdb.h \
8586
txmempool.h \
@@ -129,6 +130,7 @@ libbitcoin_server_a_SOURCES = \
129130
rpcnet.cpp \
130131
rpcrawtransaction.cpp \
131132
rpcserver.cpp \
133+
timedata.cpp \
132134
txdb.cpp \
133135
txmempool.cpp \
134136
$(JSON_H) \

src/addrman.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "netbase.h"
99
#include "protocol.h"
1010
#include "sync.h"
11+
#include "timedata.h"
1112
#include "util.h"
1213

1314
#include <map>

src/alert.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "chainparams.h"
99
#include "key.h"
1010
#include "net.h"
11+
#include "timedata.h"
1112
#include "ui_interface.h"
1213
#include "util.h"
1314

src/qt/transactiondesc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "main.h"
1313
#include "paymentserver.h"
1414
#include "transactionrecord.h"
15+
#include "timedata.h"
1516
#include "ui_interface.h"
1617
#include "wallet.h"
1718

src/qt/transactionrecord.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "transactionrecord.h"
66

77
#include "base58.h"
8+
#include "timedata.h"
89
#include "wallet.h"
910

1011
#include <stdint.h>

src/rpcmisc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "net.h"
1010
#include "netbase.h"
1111
#include "rpcserver.h"
12+
#include "timedata.h"
1213
#include "util.h"
1314
#ifdef ENABLE_WALLET
1415
#include "wallet.h"

src/rpcnet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "netbase.h"
1010
#include "protocol.h"
1111
#include "sync.h"
12+
#include "timedata.h"
1213
#include "util.h"
1314

1415
#include <boost/foreach.hpp>

src/rpcwallet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "init.h"
99
#include "net.h"
1010
#include "netbase.h"
11+
#include "timedata.h"
1112
#include "util.h"
1213
#include "wallet.h"
1314
#include "walletdb.h"

src/timedata.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) 2014 The Bitcoin developers
2+
// Distributed under the MIT/X11 software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "timedata.h"
6+
7+
#include "netbase.h"
8+
#include "sync.h"
9+
#include "ui_interface.h"
10+
#include "util.h"
11+
12+
#include <boost/foreach.hpp>
13+
14+
using namespace std;
15+
16+
static CCriticalSection cs_nTimeOffset;
17+
static int64_t nTimeOffset = 0;
18+
19+
//
20+
// "Never go to sea with two chronometers; take one or three."
21+
// Our three time sources are:
22+
// - System clock
23+
// - Median of other nodes clocks
24+
// - The user (asking the user to fix the system clock if the first two disagree)
25+
//
26+
//
27+
int64_t GetTimeOffset()
28+
{
29+
LOCK(cs_nTimeOffset);
30+
return nTimeOffset;
31+
}
32+
33+
int64_t GetAdjustedTime()
34+
{
35+
return GetTime() + GetTimeOffset();
36+
}
37+
38+
void AddTimeData(const CNetAddr& ip, int64_t nTime)
39+
{
40+
int64_t nOffsetSample = nTime - GetTime();
41+
42+
LOCK(cs_nTimeOffset);
43+
// Ignore duplicates
44+
static set<CNetAddr> setKnown;
45+
if (!setKnown.insert(ip).second)
46+
return;
47+
48+
// Add data
49+
static CMedianFilter<int64_t> vTimeOffsets(200,0);
50+
vTimeOffsets.input(nOffsetSample);
51+
LogPrintf("Added time data, samples %d, offset %+d (%+d minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
52+
if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
53+
{
54+
int64_t nMedian = vTimeOffsets.median();
55+
std::vector<int64_t> vSorted = vTimeOffsets.sorted();
56+
// Only let other nodes change our time by so much
57+
if (abs64(nMedian) < 70 * 60)
58+
{
59+
nTimeOffset = nMedian;
60+
}
61+
else
62+
{
63+
nTimeOffset = 0;
64+
65+
static bool fDone;
66+
if (!fDone)
67+
{
68+
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
69+
bool fMatch = false;
70+
BOOST_FOREACH(int64_t nOffset, vSorted)
71+
if (nOffset != 0 && abs64(nOffset) < 5 * 60)
72+
fMatch = true;
73+
74+
if (!fMatch)
75+
{
76+
fDone = true;
77+
string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Bitcoin will not work properly.");
78+
strMiscWarning = strMessage;
79+
LogPrintf("*** %s\n", strMessage);
80+
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
81+
}
82+
}
83+
}
84+
if (fDebug) {
85+
BOOST_FOREACH(int64_t n, vSorted)
86+
LogPrintf("%+d ", n);
87+
LogPrintf("| ");
88+
}
89+
LogPrintf("nTimeOffset = %+d (%+d minutes)\n", nTimeOffset, nTimeOffset/60);
90+
}
91+
}

src/timedata.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2014 The Bitcoin developers
2+
// Distributed under the MIT/X11 software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_TIMEDATA_H
6+
#define BITCOIN_TIMEDATA_H
7+
8+
#include <stdint.h>
9+
10+
class CNetAddr;
11+
12+
/* Functions to keep track of adjusted P2P time */
13+
int64_t GetTimeOffset();
14+
int64_t GetAdjustedTime();
15+
void AddTimeData(const CNetAddr& ip, int64_t nTime);
16+
17+
#endif

0 commit comments

Comments
 (0)