Browse Source
0.1075c82d4
Move coins.cpp and keystore.cpp to libbitcoin_common (Wladimir J. van der Laan)84ce18c
Remove unnecessary dependencies for bitcoin-cli (Wladimir J. van der Laan)14f888c
Move network-time related functions to timedata.cpp/h (Wladimir J. van der Laan)
Wladimir J. van der Laan
11 years ago
29 changed files with 353 additions and 173 deletions
@ -0,0 +1,93 @@
@@ -0,0 +1,93 @@
|
||||
// Copyright (c) 2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2014 The Bitcoin developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "chainparamsbase.h" |
||||
|
||||
#include "assert.h" |
||||
#include "util.h" |
||||
|
||||
#include <boost/assign/list_of.hpp> |
||||
|
||||
using namespace boost::assign; |
||||
|
||||
//
|
||||
// Main network
|
||||
//
|
||||
|
||||
class CBaseMainParams : public CBaseChainParams { |
||||
public: |
||||
CBaseMainParams() { |
||||
networkID = CBaseChainParams::MAIN; |
||||
nRPCPort = 8332; |
||||
} |
||||
}; |
||||
static CBaseMainParams mainParams; |
||||
|
||||
//
|
||||
// Testnet (v3)
|
||||
//
|
||||
class CBaseTestNetParams : public CBaseMainParams { |
||||
public: |
||||
CBaseTestNetParams() { |
||||
networkID = CBaseChainParams::TESTNET; |
||||
nRPCPort = 18332; |
||||
strDataDir = "testnet3"; |
||||
} |
||||
}; |
||||
static CBaseTestNetParams testNetParams; |
||||
|
||||
//
|
||||
// Regression test
|
||||
//
|
||||
class CBaseRegTestParams : public CBaseTestNetParams { |
||||
public: |
||||
CBaseRegTestParams() { |
||||
networkID = CBaseChainParams::REGTEST; |
||||
strDataDir = "regtest"; |
||||
} |
||||
}; |
||||
static CBaseRegTestParams regTestParams; |
||||
|
||||
static CBaseChainParams *pCurrentBaseParams = 0; |
||||
|
||||
const CBaseChainParams &BaseParams() { |
||||
assert(pCurrentBaseParams); |
||||
return *pCurrentBaseParams; |
||||
} |
||||
|
||||
void SelectBaseParams(CBaseChainParams::Network network) { |
||||
switch (network) { |
||||
case CBaseChainParams::MAIN: |
||||
pCurrentBaseParams = &mainParams; |
||||
break; |
||||
case CBaseChainParams::TESTNET: |
||||
pCurrentBaseParams = &testNetParams; |
||||
break; |
||||
case CBaseChainParams::REGTEST: |
||||
pCurrentBaseParams = ®TestParams; |
||||
break; |
||||
default: |
||||
assert(false && "Unimplemented network"); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
bool SelectBaseParamsFromCommandLine() { |
||||
bool fRegTest = GetBoolArg("-regtest", false); |
||||
bool fTestNet = GetBoolArg("-testnet", false); |
||||
|
||||
if (fTestNet && fRegTest) { |
||||
return false; |
||||
} |
||||
|
||||
if (fRegTest) { |
||||
SelectBaseParams(CBaseChainParams::REGTEST); |
||||
} else if (fTestNet) { |
||||
SelectBaseParams(CBaseChainParams::TESTNET); |
||||
} else { |
||||
SelectBaseParams(CBaseChainParams::MAIN); |
||||
} |
||||
return true; |
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2014 The Bitcoin developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_CHAIN_PARAMS_BASE_H |
||||
#define BITCOIN_CHAIN_PARAMS_BASE_H |
||||
|
||||
#include <vector> |
||||
#include <string> |
||||
|
||||
/**
|
||||
* CBaseChainParams defines the base parameters (shared between bitcoin-cli and bitcoind) |
||||
* of a given instance of the Bitcoin system. |
||||
*/ |
||||
class CBaseChainParams |
||||
{ |
||||
public: |
||||
enum Network { |
||||
MAIN, |
||||
TESTNET, |
||||
REGTEST, |
||||
|
||||
MAX_NETWORK_TYPES |
||||
}; |
||||
|
||||
const std::string& DataDir() const { return strDataDir; } |
||||
int RPCPort() const { return nRPCPort; } |
||||
Network NetworkID() const { return networkID; } |
||||
protected: |
||||
CBaseChainParams() {} |
||||
|
||||
int nRPCPort; |
||||
std::string strDataDir; |
||||
Network networkID; |
||||
}; |
||||
|
||||
/**
|
||||
* Return the currently selected parameters. This won't change after app startup |
||||
* outside of the unit tests. |
||||
*/ |
||||
const CBaseChainParams &BaseParams(); |
||||
|
||||
/** Sets the params returned by Params() to those for the given network. */ |
||||
void SelectBaseParams(CBaseChainParams::Network network); |
||||
|
||||
/**
|
||||
* Looks for -regtest or -testnet and then calls SelectParams as appropriate. |
||||
* Returns false if an invalid combination is given. |
||||
*/ |
||||
bool SelectBaseParamsFromCommandLine(); |
||||
|
||||
#endif |
@ -0,0 +1,91 @@
@@ -0,0 +1,91 @@
|
||||
// Copyright (c) 2014 The Bitcoin 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 "netbase.h" |
||||
#include "sync.h" |
||||
#include "ui_interface.h" |
||||
#include "util.h" |
||||
|
||||
#include <boost/foreach.hpp> |
||||
|
||||
using namespace std; |
||||
|
||||
static CCriticalSection cs_nTimeOffset; |
||||
static int64_t nTimeOffset = 0; |
||||
|
||||
//
|
||||
// "Never go to sea with two chronometers; take one or three."
|
||||
// Our three time sources are:
|
||||
// - System clock
|
||||
// - Median of other nodes clocks
|
||||
// - The user (asking the user to fix the system clock if the first two disagree)
|
||||
//
|
||||
//
|
||||
int64_t GetTimeOffset() |
||||
{ |
||||
LOCK(cs_nTimeOffset); |
||||
return nTimeOffset; |
||||
} |
||||
|
||||
int64_t GetAdjustedTime() |
||||
{ |
||||
return GetTime() + GetTimeOffset(); |
||||
} |
||||
|
||||
void AddTimeData(const CNetAddr& ip, int64_t nTime) |
||||
{ |
||||
int64_t nOffsetSample = nTime - GetTime(); |
||||
|
||||
LOCK(cs_nTimeOffset); |
||||
// Ignore duplicates
|
||||
static set<CNetAddr> setKnown; |
||||
if (!setKnown.insert(ip).second) |
||||
return; |
||||
|
||||
// Add data
|
||||
static CMedianFilter<int64_t> vTimeOffsets(200,0); |
||||
vTimeOffsets.input(nOffsetSample); |
||||
LogPrintf("Added time data, samples %d, offset %+d (%+d minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60); |
||||
if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) |
||||
{ |
||||
int64_t nMedian = vTimeOffsets.median(); |
||||
std::vector<int64_t> vSorted = vTimeOffsets.sorted(); |
||||
// Only let other nodes change our time by so much
|
||||
if (abs64(nMedian) < 70 * 60) |
||||
{ |
||||
nTimeOffset = nMedian; |
||||
} |
||||
else |
||||
{ |
||||
nTimeOffset = 0; |
||||
|
||||
static bool fDone; |
||||
if (!fDone) |
||||
{ |
||||
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
|
||||
bool fMatch = false; |
||||
BOOST_FOREACH(int64_t nOffset, vSorted) |
||||
if (nOffset != 0 && abs64(nOffset) < 5 * 60) |
||||
fMatch = true; |
||||
|
||||
if (!fMatch) |
||||
{ |
||||
fDone = true; |
||||
string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Bitcoin will not work properly."); |
||||
strMiscWarning = strMessage; |
||||
LogPrintf("*** %s\n", strMessage); |
||||
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING); |
||||
} |
||||
} |
||||
} |
||||
if (fDebug) { |
||||
BOOST_FOREACH(int64_t n, vSorted) |
||||
LogPrintf("%+d ", n); |
||||
LogPrintf("| "); |
||||
} |
||||
LogPrintf("nTimeOffset = %+d (%+d minutes)\n", nTimeOffset, nTimeOffset/60); |
||||
} |
||||
} |
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
// Copyright (c) 2014 The Bitcoin developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_TIMEDATA_H |
||||
#define BITCOIN_TIMEDATA_H |
||||
|
||||
#include <stdint.h> |
||||
|
||||
class CNetAddr; |
||||
|
||||
/* Functions to keep track of adjusted P2P time */ |
||||
int64_t GetTimeOffset(); |
||||
int64_t GetAdjustedTime(); |
||||
void AddTimeData(const CNetAddr& ip, int64_t nTime); |
||||
|
||||
#endif |
Loading…
Reference in new issue