main.cpp 150 KB
Newer Older
s_nakamoto's avatar
s_nakamoto committed
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
super3's avatar
super3 committed
2
// Copyright (c) 2009-2013 The Bitcoin developers
s_nakamoto's avatar
s_nakamoto committed
3
// Distributed under the MIT/X11 software license, see the accompanying
Fordy's avatar
Fordy committed
4
5
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

6
#include "main.h"
7

8
#include "addrman.h"
9
#include "alert.h"
10
#include "chainparams.h"
11
#include "checkpoints.h"
12
#include "checkqueue.h"
13
#include "init.h"
14
15
16
#include "net.h"
#include "txdb.h"
#include "txmempool.h"
Pieter Wuille's avatar
Pieter Wuille committed
17
#include "ui_interface.h"
18
19
20
#include "util.h"

#include <inttypes.h>
Gavin Andresen's avatar
Gavin Andresen committed
21
#include <sstream>
22
23
24
25

#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
s_nakamoto's avatar
s_nakamoto committed
26

27
28
using namespace std;
using namespace boost;
s_nakamoto's avatar
s_nakamoto committed
29

30
31
32
33
#if defined(NDEBUG)
# error "Bitcoin cannot be compiled without assertions."
#endif

s_nakamoto's avatar
s_nakamoto committed
34
35
36
37
38
39
//
// Global state
//

CCriticalSection cs_main;

40
CTxMemPool mempool;
s_nakamoto's avatar
s_nakamoto committed
41
42

map<uint256, CBlockIndex*> mapBlockIndex;
43
CChain chainActive;
44
int64_t nTimeBestReceived = 0;
45
int nScriptCheckThreads = 0;
46
bool fImporting = false;
47
bool fReindex = false;
48
bool fBenchmark = false;
49
bool fTxIndex = false;
Pieter Wuille's avatar
Pieter Wuille committed
50
unsigned int nCoinCacheSize = 5000;
s_nakamoto's avatar
s_nakamoto committed
51

Gavin Andresen's avatar
Gavin Andresen committed
52
/** Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) */
53
int64_t CTransaction::nMinTxFee = 10000;  // Override with -mintxfee
Gavin Andresen's avatar
Gavin Andresen committed
54
/** Fees smaller than this (in satoshi) are considered zero fee (for relaying) */
55
int64_t CTransaction::nMinRelayTxFee = 10000;
Gavin Andresen's avatar
Gavin Andresen committed
56

57
static CMedianFilter<int> cPeerBlockCounts(8, 0); // Amount of blocks that other nodes claim to have
58

59
60
61
62
63
64
65
struct COrphanBlock {
    uint256 hashBlock;
    uint256 hashPrev;
    vector<unsigned char> vchBlock;
};
map<uint256, COrphanBlock*> mapOrphanBlocks;
multimap<uint256, COrphanBlock*> mapOrphanBlocksByPrev;
s_nakamoto's avatar
s_nakamoto committed
66

67
68
map<uint256, CTransaction> mapOrphanTransactions;
map<uint256, set<uint256> > mapOrphanTransactionsByPrev;
s_nakamoto's avatar
s_nakamoto committed
69

70
71
// Constant stuff for coinbase transactions we create:
CScript COINBASE_FLAGS;
s_nakamoto's avatar
s_nakamoto committed
72

73
74
const string strMessageMagic = "Bitcoin Signed Message:\n";

75
76
77
78
79
80
81
// Internal stuff
namespace {
struct CBlockIndexWorkComparator
{
    bool operator()(CBlockIndex *pa, CBlockIndex *pb) {
        if (pa->nChainWork > pb->nChainWork) return false;
        if (pa->nChainWork < pb->nChainWork) return true;
82

83
84
85
86
87
88
89
90
91
        if (pa->GetBlockHash() < pb->GetBlockHash()) return false;
        if (pa->GetBlockHash() > pb->GetBlockHash()) return true;

        return false; // identical blocks
    }
};

CBlockIndex *pindexBestInvalid;
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
92

93
94
95
96
CCriticalSection cs_LastBlockFile;
CBlockFileInfo infoLastBlockFile;
int nLastBlockFile = 0;
}
s_nakamoto's avatar
s_nakamoto committed
97

Pieter Wuille's avatar
Pieter Wuille committed
98
99
100
101
102
//////////////////////////////////////////////////////////////////////////////
//
// dispatching functions
//

Pieter Wuille's avatar
Pieter Wuille committed
103
104
// These functions dispatch to one or all registered wallets

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
namespace {
struct CMainSignals {
    // Notifies listeners of updated transaction data (passing hash, transaction, and optionally the block it is found in.
    boost::signals2::signal<void (const uint256 &, const CTransaction &, const CBlock *)> SyncTransaction;
    // Notifies listeners of an erased transaction (currently disabled, requires transaction replacement).
    boost::signals2::signal<void (const uint256 &)> EraseTransaction;
    // Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible).
    boost::signals2::signal<void (const uint256 &)> UpdatedTransaction;
    // Notifies listeners of a new active block chain.
    boost::signals2::signal<void (const CBlockLocator &)> SetBestChain;
    // Notifies listeners about an inventory item being seen on the network.
    boost::signals2::signal<void (const uint256 &)> Inventory;
    // Tells listeners to broadcast their data.
    boost::signals2::signal<void ()> Broadcast;
} g_signals;
Pieter Wuille's avatar
Pieter Wuille committed
120
121
}

122
123
124
125
126
127
128
void RegisterWallet(CWalletInterface* pwalletIn) {
    g_signals.SyncTransaction.connect(boost::bind(&CWalletInterface::SyncTransaction, pwalletIn, _1, _2, _3));
    g_signals.EraseTransaction.connect(boost::bind(&CWalletInterface::EraseFromWallet, pwalletIn, _1));
    g_signals.UpdatedTransaction.connect(boost::bind(&CWalletInterface::UpdatedTransaction, pwalletIn, _1));
    g_signals.SetBestChain.connect(boost::bind(&CWalletInterface::SetBestChain, pwalletIn, _1));
    g_signals.Inventory.connect(boost::bind(&CWalletInterface::Inventory, pwalletIn, _1));
    g_signals.Broadcast.connect(boost::bind(&CWalletInterface::ResendWalletTransactions, pwalletIn));
Pieter Wuille's avatar
Pieter Wuille committed
129
130
}

131
132
133
134
135
136
137
void UnregisterWallet(CWalletInterface* pwalletIn) {
    g_signals.Broadcast.disconnect(boost::bind(&CWalletInterface::ResendWalletTransactions, pwalletIn));
    g_signals.Inventory.disconnect(boost::bind(&CWalletInterface::Inventory, pwalletIn, _1));
    g_signals.SetBestChain.disconnect(boost::bind(&CWalletInterface::SetBestChain, pwalletIn, _1));
    g_signals.UpdatedTransaction.disconnect(boost::bind(&CWalletInterface::UpdatedTransaction, pwalletIn, _1));
    g_signals.EraseTransaction.disconnect(boost::bind(&CWalletInterface::EraseFromWallet, pwalletIn, _1));
    g_signals.SyncTransaction.disconnect(boost::bind(&CWalletInterface::SyncTransaction, pwalletIn, _1, _2, _3));
Pieter Wuille's avatar
Pieter Wuille committed
138
139
}

140
141
142
143
144
145
146
void UnregisterAllWallets() {
    g_signals.Broadcast.disconnect_all_slots();
    g_signals.Inventory.disconnect_all_slots();
    g_signals.SetBestChain.disconnect_all_slots();
    g_signals.UpdatedTransaction.disconnect_all_slots();
    g_signals.EraseTransaction.disconnect_all_slots();
    g_signals.SyncTransaction.disconnect_all_slots();
Pieter Wuille's avatar
Pieter Wuille committed
147
148
}

149
150
void SyncWithWallets(const uint256 &hash, const CTransaction &tx, const CBlock *pblock) {
    g_signals.SyncTransaction(hash, tx, pblock);
Pieter Wuille's avatar
Pieter Wuille committed
151
152
}

153
154
155
156
157
//////////////////////////////////////////////////////////////////////////////
//
// Registration of network node signals.
//

Pieter Wuille's avatar
Pieter Wuille committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
namespace {
// Maintain validation-specific state about nodes, protected by cs_main, instead
// by CNode's own locks. This simplifies asynchronous operation, where
// processing of incoming data is done after the ProcessMessage call returns,
// and we're no longer holding the node's locks.
struct CNodeState {
    int nMisbehavior;
    bool fShouldBan;
    std::string name;

    CNodeState() {
        nMisbehavior = 0;
        fShouldBan = false;
    }
};

map<NodeId, CNodeState> mapNodeState;

// Requires cs_main.
CNodeState *State(NodeId pnode) {
    map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
    if (it == mapNodeState.end())
        return NULL;
    return &it->second;
}

int GetHeight()
185
186
187
188
189
{
    LOCK(cs_main);
    return chainActive.Height();
}

Pieter Wuille's avatar
Pieter Wuille committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
void InitializeNode(NodeId nodeid, const CNode *pnode) {
    LOCK(cs_main);
    CNodeState &state = mapNodeState.insert(std::make_pair(nodeid, CNodeState())).first->second;
    state.name = pnode->addrName;
}

void FinalizeNode(NodeId nodeid) {
    LOCK(cs_main);
    mapNodeState.erase(nodeid);
}
}

bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
    LOCK(cs_main);
    CNodeState *state = State(nodeid);
    if (state == NULL)
        return false;
    stats.nMisbehavior = state->nMisbehavior;
    return true;
}

211
212
void RegisterNodeSignals(CNodeSignals& nodeSignals)
{
213
    nodeSignals.GetHeight.connect(&GetHeight);
214
215
    nodeSignals.ProcessMessages.connect(&ProcessMessages);
    nodeSignals.SendMessages.connect(&SendMessages);
Pieter Wuille's avatar
Pieter Wuille committed
216
217
    nodeSignals.InitializeNode.connect(&InitializeNode);
    nodeSignals.FinalizeNode.connect(&FinalizeNode);
218
}
Pieter Wuille's avatar
Pieter Wuille committed
219

220
221
void UnregisterNodeSignals(CNodeSignals& nodeSignals)
{
222
    nodeSignals.GetHeight.disconnect(&GetHeight);
223
224
    nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
    nodeSignals.SendMessages.disconnect(&SendMessages);
Pieter Wuille's avatar
Pieter Wuille committed
225
226
    nodeSignals.InitializeNode.disconnect(&InitializeNode);
    nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
227
}
Pieter Wuille's avatar
Pieter Wuille committed
228

229
230
//////////////////////////////////////////////////////////////////////////////
//
231
// CChain implementation
232
233
//

234
235
236
237
238
239
240
241
242
243
244
CBlockIndex *CChain::SetTip(CBlockIndex *pindex) {
    if (pindex == NULL) {
        vChain.clear();
        return NULL;
    }
    vChain.resize(pindex->nHeight + 1);
    while (pindex && vChain[pindex->nHeight] != pindex) {
        vChain[pindex->nHeight] = pindex;
        pindex = pindex->pprev;
    }
    return pindex;
245
246
}

247
CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
248
    int nStep = 1;
249
250
    std::vector<uint256> vHave;
    vHave.reserve(32);
251

252
253
254
255
256
257
258
259
260
261
262
    if (!pindex)
        pindex = Tip();
    while (pindex) {
        vHave.push_back(pindex->GetBlockHash());
        // Stop when we have added the genesis block.
        if (pindex->nHeight == 0)
            break;
        // Exponentially larger steps back, plus the genesis block.
        int nHeight = std::max(pindex->nHeight - nStep, 0);
        // In case pindex is not in this chain, iterate pindex->pprev to find blocks.
        while (pindex->nHeight > nHeight && !Contains(pindex))
263
            pindex = pindex->pprev;
264
265
266
        // If pindex is in this chain, use direct height-based access.
        if (pindex->nHeight > nHeight)
            pindex = (*this)[nHeight];
267
268
269
        if (vHave.size() > 10)
            nStep *= 2;
    }
Pieter Wuille's avatar
Pieter Wuille committed
270

271
    return CBlockLocator(vHave);
272
273
}

274
CBlockIndex *CChain::FindFork(const CBlockLocator &locator) const {
275
    // Find the first block the caller has in the main chain
276
    BOOST_FOREACH(const uint256& hash, locator.vHave) {
277
278
279
280
        std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
        if (mi != mapBlockIndex.end())
        {
            CBlockIndex* pindex = (*mi).second;
281
            if (Contains(pindex))
282
283
284
                return pindex;
        }
    }
285
    return Genesis();
286
287
}

288
CCoinsViewCache *pcoinsTip = NULL;
289
CBlockTreeDB *pblocktree = NULL;
Pieter Wuille's avatar
Pieter Wuille committed
290

s_nakamoto's avatar
s_nakamoto committed
291
292
293
294
295
//////////////////////////////////////////////////////////////////////////////
//
// mapOrphanTransactions
//

296
bool AddOrphanTx(const CTransaction& tx)
s_nakamoto's avatar
s_nakamoto committed
297
298
299
{
    uint256 hash = tx.GetHash();
    if (mapOrphanTransactions.count(hash))
300
301
302
303
304
305
306
307
308
        return false;

    // Ignore big transactions, to avoid a
    // send-big-orphans memory exhaustion attack. If a peer has a legitimate
    // large transaction with a missing parent then we assume
    // it will rebroadcast it later, after the parent transaction(s)
    // have been mined or received.
    // 10,000 orphans, each of which is at most 5,000 bytes big is
    // at most 500 megabytes of orphans:
309
310
    unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
    if (sz > 5000)
311
    {
312
        LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
313
314
        return false;
    }
315

316
    mapOrphanTransactions[hash] = tx;
317
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
318
        mapOrphanTransactionsByPrev[txin.prevout.hash].insert(hash);
319

320
    LogPrint("mempool", "stored orphan tx %s (mapsz %"PRIszu")\n", hash.ToString(),
321
322
        mapOrphanTransactions.size());
    return true;
s_nakamoto's avatar
s_nakamoto committed
323
324
}

Pieter Wuille's avatar
Pieter Wuille committed
325
void static EraseOrphanTx(uint256 hash)
s_nakamoto's avatar
s_nakamoto committed
326
327
328
{
    if (!mapOrphanTransactions.count(hash))
        return;
329
    const CTransaction& tx = mapOrphanTransactions[hash];
330
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
s_nakamoto's avatar
s_nakamoto committed
331
    {
332
333
334
        mapOrphanTransactionsByPrev[txin.prevout.hash].erase(hash);
        if (mapOrphanTransactionsByPrev[txin.prevout.hash].empty())
            mapOrphanTransactionsByPrev.erase(txin.prevout.hash);
s_nakamoto's avatar
s_nakamoto committed
335
336
337
338
    }
    mapOrphanTransactions.erase(hash);
}

339
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
340
{
341
    unsigned int nEvicted = 0;
342
343
344
    while (mapOrphanTransactions.size() > nMaxOrphans)
    {
        // Evict a random orphan:
345
        uint256 randomhash = GetRandHash();
346
        map<uint256, CTransaction>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
347
348
349
350
351
352
353
        if (it == mapOrphanTransactions.end())
            it = mapOrphanTransactions.begin();
        EraseOrphanTx(it->first);
        ++nEvicted;
    }
    return nEvicted;
}
s_nakamoto's avatar
s_nakamoto committed
354
355
356
357
358
359
360







361
bool IsStandardTx(const CTransaction& tx, string& reason)
Gavin Andresen's avatar
Gavin Andresen committed
362
{
363
    if (tx.nVersion > CTransaction::CURRENT_VERSION || tx.nVersion < 1) {
364
        reason = "version";
365
        return false;
366
    }
367

368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    // Treat non-final transactions as non-standard to prevent a specific type
    // of double-spend attack, as well as DoS attacks. (if the transaction
    // can't be mined, the attacker isn't expending resources broadcasting it)
    // Basically we don't want to propagate transactions that can't included in
    // the next block.
    //
    // However, IsFinalTx() is confusing... Without arguments, it uses
    // chainActive.Height() to evaluate nLockTime; when a block is accepted, chainActive.Height()
    // is set to the value of nHeight in the block. However, when IsFinalTx()
    // is called within CBlock::AcceptBlock(), the height of the block *being*
    // evaluated is what is used. Thus if we want to know if a transaction can
    // be part of the *next* block, we need to call IsFinalTx() with one more
    // than chainActive.Height().
    //
    // Timestamps on the other hand don't get any special treatment, because we
    // can't know what timestamp the next block will have, and there aren't
    // timestamp applications where it matters.
    if (!IsFinalTx(tx, chainActive.Height() + 1)) {
386
        reason = "non-final";
387
        return false;
388
    }
389

390
391
392
393
    // Extremely large transactions with lots of inputs can cost the network
    // almost as much to process as they cost the sender in fees, because
    // computing signature hashes is O(ninputs*txsize). Limiting transactions
    // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
394
    unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
395
396
    if (sz >= MAX_STANDARD_TX_SIZE) {
        reason = "tx-size";
397
        return false;
398
    }
399

400
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
Gavin Andresen's avatar
Gavin Andresen committed
401
    {
402
        // Biggest 'standard' txin is a 3-signature 3-of-3 CHECKMULTISIG
403
        // pay-to-script-hash, which is 3 ~80-byte signatures, 3
Gavin Andresen's avatar
Gavin Andresen committed
404
        // ~65-byte public keys, plus a few script ops.
405
406
        if (txin.scriptSig.size() > 500) {
            reason = "scriptsig-size";
407
            return false;
408
409
410
        }
        if (!txin.scriptSig.IsPushOnly()) {
            reason = "scriptsig-not-pushonly";
411
            return false;
412
        }
Gavin Andresen's avatar
Gavin Andresen committed
413
    }
414
415
416

    unsigned int nDataOut = 0;
    txnouttype whichType;
417
    BOOST_FOREACH(const CTxOut& txout, tx.vout) {
418
        if (!::IsStandard(txout.scriptPubKey, whichType)) {
419
            reason = "scriptpubkey";
420
            return false;
421
        }
422
423
424
        if (whichType == TX_NULL_DATA)
            nDataOut++;
        else if (txout.IsDust(CTransaction::nMinRelayTxFee)) {
425
            reason = "dust";
426
            return false;
427
        }
428
    }
429

430
431
432
433
434
435
    // only one OP_RETURN txout is permitted
    if (nDataOut > 1) {
        reason = "mucho-data";
        return false;
    }

Gavin Andresen's avatar
Gavin Andresen committed
436
437
438
    return true;
}

439
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
440
441
442
443
444
{
    // Time based nLockTime implemented in 0.1.6
    if (tx.nLockTime == 0)
        return true;
    if (nBlockHeight == 0)
445
        nBlockHeight = chainActive.Height();
446
447
    if (nBlockTime == 0)
        nBlockTime = GetAdjustedTime();
448
    if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
449
450
451
452
453
454
455
        return true;
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
        if (!txin.IsFinal())
            return false;
    return true;
}

Gavin Andresen's avatar
Gavin Andresen committed
456
457
//
// Check transaction inputs, and make sure any
458
// pay-to-script-hash transactions are evaluating IsStandard scripts
Gavin Andresen's avatar
Gavin Andresen committed
459
460
//
// Why bother? To avoid denial-of-service attacks; an attacker
461
462
463
// can submit a standard HASH... OP_EQUAL transaction,
// which will get accepted into blocks. The redemption
// script can be anything; an attacker could use a very
Gavin Andresen's avatar
Gavin Andresen committed
464
465
466
// expensive-to-check-upon-redemption script like:
//   DUP CHECKSIG DROP ... repeated 100 times... OP_1
//
467
bool AreInputsStandard(const CTransaction& tx, CCoinsViewCache& mapInputs)
Gavin Andresen's avatar
Gavin Andresen committed
468
{
469
    if (tx.IsCoinBase())
470
        return true; // Coinbases don't use vin normally
471

472
    for (unsigned int i = 0; i < tx.vin.size(); i++)
Gavin Andresen's avatar
Gavin Andresen committed
473
    {
474
        const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]);
Gavin Andresen's avatar
Gavin Andresen committed
475
476

        vector<vector<unsigned char> > vSolutions;
477
478
        txnouttype whichType;
        // get the scriptPubKey corresponding to this input:
479
        const CScript& prevScript = prev.scriptPubKey;
480
        if (!Solver(prevScript, whichType, vSolutions))
481
            return false;
482
        int nArgsExpected = ScriptSigArgsExpected(whichType, vSolutions);
483
484
        if (nArgsExpected < 0)
            return false;
485
486
487
488
489
490
491

        // Transactions with extra stuff in their scriptSigs are
        // non-standard. Note that this EvalScript() call will
        // be quick, because if there are any operations
        // beside "push data" in the scriptSig the
        // IsStandard() call returns false
        vector<vector<unsigned char> > stack;
492
        if (!EvalScript(stack, tx.vin[i].scriptSig, tx, i, false, 0))
493
494
            return false;

Gavin Andresen's avatar
Gavin Andresen committed
495
496
        if (whichType == TX_SCRIPTHASH)
        {
497
            if (stack.empty())
Gavin Andresen's avatar
Gavin Andresen committed
498
                return false;
499
            CScript subscript(stack.back().begin(), stack.back().end());
500
501
502
            vector<vector<unsigned char> > vSolutions2;
            txnouttype whichType2;
            if (!Solver(subscript, whichType2, vSolutions2))
503
                return false;
504
505
            if (whichType2 == TX_SCRIPTHASH)
                return false;
506
507
508
509
510
511

            int tmpExpected;
            tmpExpected = ScriptSigArgsExpected(whichType2, vSolutions2);
            if (tmpExpected < 0)
                return false;
            nArgsExpected += tmpExpected;
Gavin Andresen's avatar
Gavin Andresen committed
512
        }
513

514
        if (stack.size() != (unsigned int)nArgsExpected)
515
            return false;
Gavin Andresen's avatar
Gavin Andresen committed
516
517
518
519
520
    }

    return true;
}

521
unsigned int GetLegacySigOpCount(const CTransaction& tx)
522
{
523
    unsigned int nSigOps = 0;
524
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
525
526
527
    {
        nSigOps += txin.scriptSig.GetSigOpCount(false);
    }
528
    BOOST_FOREACH(const CTxOut& txout, tx.vout)
529
530
531
532
533
    {
        nSigOps += txout.scriptPubKey.GetSigOpCount(false);
    }
    return nSigOps;
}
s_nakamoto's avatar
s_nakamoto committed
534

535
536
537
538
539
540
541
542
543
544
545
546
547
548
unsigned int GetP2SHSigOpCount(const CTransaction& tx, CCoinsViewCache& inputs)
{
    if (tx.IsCoinBase())
        return 0;

    unsigned int nSigOps = 0;
    for (unsigned int i = 0; i < tx.vin.size(); i++)
    {
        const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
        if (prevout.scriptPubKey.IsPayToScriptHash())
            nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
    }
    return nSigOps;
}
s_nakamoto's avatar
s_nakamoto committed
549
550
551

int CMerkleTx::SetMerkleBranch(const CBlock* pblock)
{
Pieter Wuille's avatar
Pieter Wuille committed
552
553
554
555
556
    CBlock blockTmp;

    if (pblock == NULL) {
        CCoins coins;
        if (pcoinsTip->GetCoins(GetHash(), coins)) {
557
            CBlockIndex *pindex = chainActive[coins.nHeight];
Pieter Wuille's avatar
Pieter Wuille committed
558
            if (pindex) {
559
                if (!ReadBlockFromDisk(blockTmp, pindex))
Pieter Wuille's avatar
Pieter Wuille committed
560
561
                    return 0;
                pblock = &blockTmp;
Pieter Wuille's avatar
Pieter Wuille committed
562
            }
s_nakamoto's avatar
s_nakamoto committed
563
        }
Pieter Wuille's avatar
Pieter Wuille committed
564
    }
s_nakamoto's avatar
s_nakamoto committed
565

Pieter Wuille's avatar
Pieter Wuille committed
566
    if (pblock) {
s_nakamoto's avatar
s_nakamoto committed
567
568
569
570
        // Update the tx's hashBlock
        hashBlock = pblock->GetHash();

        // Locate the transaction
571
        for (nIndex = 0; nIndex < (int)pblock->vtx.size(); nIndex++)
s_nakamoto's avatar
s_nakamoto committed
572
573
            if (pblock->vtx[nIndex] == *(CTransaction*)this)
                break;
574
        if (nIndex == (int)pblock->vtx.size())
s_nakamoto's avatar
s_nakamoto committed
575
576
577
        {
            vMerkleBranch.clear();
            nIndex = -1;
578
            LogPrintf("ERROR: SetMerkleBranch() : couldn't find tx in block\n");
s_nakamoto's avatar
s_nakamoto committed
579
580
581
582
583
584
585
586
587
588
589
590
            return 0;
        }

        // Fill in merkle branch
        vMerkleBranch = pblock->GetMerkleBranch(nIndex);
    }

    // Is the tx in a block that's in the main chain
    map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
    if (mi == mapBlockIndex.end())
        return 0;
    CBlockIndex* pindex = (*mi).second;
591
    if (!pindex || !chainActive.Contains(pindex))
s_nakamoto's avatar
s_nakamoto committed
592
593
        return 0;

594
    return chainActive.Height() - pindex->nHeight + 1;
s_nakamoto's avatar
s_nakamoto committed
595
596
597
598
599
600
601
602
}







603
bool CheckTransaction(const CTransaction& tx, CValidationState &state)
604
605
{
    // Basic checks that don't depend on any context
606
    if (tx.vin.empty())
Gavin Andresen's avatar
Gavin Andresen committed
607
608
        return state.DoS(10, error("CheckTransaction() : vin empty"),
                         REJECT_INVALID, "vin empty");
609
    if (tx.vout.empty())
Gavin Andresen's avatar
Gavin Andresen committed
610
611
        return state.DoS(10, error("CheckTransaction() : vout empty"),
                         REJECT_INVALID, "vout empty");
612
    // Size limits
613
    if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
614
        return state.DoS(100, error("CheckTransaction() : size limits failed"),
Gavin Andresen's avatar
Gavin Andresen committed
615
                         REJECT_INVALID, "oversize");
616
617

    // Check for negative or overflow output values
618
    int64_t nValueOut = 0;
619
    BOOST_FOREACH(const CTxOut& txout, tx.vout)
620
621
    {
        if (txout.nValue < 0)
Gavin Andresen's avatar
Gavin Andresen committed
622
623
            return state.DoS(100, error("CheckTransaction() : txout.nValue negative"),
                             REJECT_INVALID, "vout negative");
624
        if (txout.nValue > MAX_MONEY)
Gavin Andresen's avatar
Gavin Andresen committed
625
626
            return state.DoS(100, error("CheckTransaction() : txout.nValue too high"),
                             REJECT_INVALID, "vout too large");
627
628
        nValueOut += txout.nValue;
        if (!MoneyRange(nValueOut))
629
            return state.DoS(100, error("CheckTransaction() : txout total out of range"),
Gavin Andresen's avatar
Gavin Andresen committed
630
                             REJECT_INVALID, "txout total too large");
631
632
    }

633
634
    // Check for duplicate inputs
    set<COutPoint> vInOutPoints;
635
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
636
637
    {
        if (vInOutPoints.count(txin.prevout))
638
            return state.DoS(100, error("CheckTransaction() : duplicate inputs"),
Gavin Andresen's avatar
Gavin Andresen committed
639
                             REJECT_INVALID, "duplicate inputs");
640
641
642
        vInOutPoints.insert(txin.prevout);
    }

643
    if (tx.IsCoinBase())
644
    {
645
        if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
Gavin Andresen's avatar
Gavin Andresen committed
646
647
            return state.DoS(100, error("CheckTransaction() : coinbase script size"),
                             REJECT_INVALID, "coinbase script too large");
648
649
650
    }
    else
    {
651
        BOOST_FOREACH(const CTxIn& txin, tx.vin)
652
            if (txin.prevout.IsNull())
Gavin Andresen's avatar
Gavin Andresen committed
653
654
                return state.DoS(10, error("CheckTransaction() : prevout is null"),
                                 REJECT_INVALID, "prevout null");
655
656
657
658
659
    }

    return true;
}

660
int64_t GetMinFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree, enum GetMinFee_mode mode)
661
{
Gavin Andresen's avatar
Gavin Andresen committed
662
    // Base fee is either nMinTxFee or nMinRelayTxFee
663
    int64_t nBaseFee = (mode == GMF_RELAY) ? tx.nMinRelayTxFee : tx.nMinTxFee;
664

665
    int64_t nMinFee = (1 + (int64_t)nBytes / 1000) * nBaseFee;
666
667
668

    if (fAllowFree)
    {
669
670
        // There is a free transaction area in blocks created by most miners,
        // * If we are relaying we allow transactions up to DEFAULT_BLOCK_PRIORITY_SIZE - 1000
671
672
673
674
675
        //   to be considered to fall into this category. We don't want to encourage sending
        //   multiple transactions instead of one big transaction to avoid fees.
        // * If we are creating a transaction we allow transactions up to 1,000 bytes
        //   to be considered safe and assume they can likely make it into this section.
        if (nBytes < (mode == GMF_SEND ? 1000 : (DEFAULT_BLOCK_PRIORITY_SIZE - 1000)))
676
            nMinFee = 0;
677
678
    }

679
680
681
682
    // This code can be removed after enough miners have upgraded to version 0.9.
    // Until then, be safe when sending and require a fee if any output
    // is less than CENT:
    if (nMinFee < nBaseFee && mode == GMF_SEND)
683
    {
684
        BOOST_FOREACH(const CTxOut& txout, tx.vout)
685
686
687
688
689
690
691
692
693
            if (txout.nValue < CENT)
                nMinFee = nBaseFee;
    }

    if (!MoneyRange(nMinFee))
        nMinFee = MAX_MONEY;
    return nMinFee;
}

Pieter Wuille's avatar
Pieter Wuille committed
694

695
bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
696
                        bool* pfMissingInputs, bool fRejectInsaneFee)
s_nakamoto's avatar
s_nakamoto committed
697
698
699
700
{
    if (pfMissingInputs)
        *pfMissingInputs = false;

701
    if (!CheckTransaction(tx, state))
702
        return error("AcceptToMemoryPool: : CheckTransaction failed");
703

s_nakamoto's avatar
s_nakamoto committed
704
    // Coinbase is only valid in a block, not as a loose transaction
705
    if (tx.IsCoinBase())
Gavin Andresen's avatar
Gavin Andresen committed
706
707
        return state.DoS(100, error("AcceptToMemoryPool: : coinbase as individual tx"),
                         REJECT_INVALID, "coinbase");
s_nakamoto's avatar
s_nakamoto committed
708

709
    // Rather not work on nonstandard transactions (unless -testnet/-regtest)
710
    string reason;
711
    if (Params().NetworkID() == CChainParams::MAIN && !IsStandardTx(tx, reason))
Gavin Andresen's avatar
Gavin Andresen committed
712
        return state.DoS(0,
713
                         error("AcceptToMemoryPool : nonstandard transaction: %s", reason),
Gavin Andresen's avatar
Gavin Andresen committed
714
                         REJECT_NONSTANDARD, reason);
715

Pieter Wuille's avatar
Pieter Wuille committed
716
    // is it already in the memory pool?
717
    uint256 hash = tx.GetHash();
718
719
    if (pool.exists(hash))
        return false;
s_nakamoto's avatar
s_nakamoto committed
720
721

    // Check for conflicts with in-memory transactions
722
723
    {
    LOCK(pool.cs); // protect pool.mapNextTx
724
    for (unsigned int i = 0; i < tx.vin.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
725
    {
726
        COutPoint outpoint = tx.vin[i].prevout;
727
        if (pool.mapNextTx.count(outpoint))
s_nakamoto's avatar
s_nakamoto committed
728
729
730
731
732
        {
            // Disable replacement feature for now
            return false;
        }
    }
733
    }
s_nakamoto's avatar
s_nakamoto committed
734
735

    {
736
737
738
739
        CCoinsView dummy;
        CCoinsViewCache view(dummy);

        {
740
741
        LOCK(pool.cs);
        CCoinsViewMemPool viewMemPool(*pcoinsTip, pool);
742
        view.SetBackend(viewMemPool);
Pieter Wuille's avatar
Pieter Wuille committed
743
744
745

        // do we already have it?
        if (view.HaveCoins(hash))
746
            return false;
Pieter Wuille's avatar
Pieter Wuille committed
747
748

        // do all inputs exist?
Pieter Wuille's avatar
Pieter Wuille committed
749
750
        // Note that this does not check for the presence of actual outputs (see the next check for that),
        // only helps filling in pfMissingInputs (to determine missing vs spent).
Pieter Wuille's avatar
Pieter Wuille committed
751
752
753
754
755
756
        BOOST_FOREACH(const CTxIn txin, tx.vin) {
            if (!view.HaveCoins(txin.prevout.hash)) {
                if (pfMissingInputs)
                    *pfMissingInputs = true;
                return false;
            }
Gavin Andresen's avatar
Gavin Andresen committed
757
758
        }

Pieter Wuille's avatar
Pieter Wuille committed
759
        // are the actual inputs available?
760
        if (!view.HaveInputs(tx))
Gavin Andresen's avatar
Gavin Andresen committed
761
762
            return state.Invalid(error("AcceptToMemoryPool : inputs already spent"),
                                 REJECT_DUPLICATE, "inputs spent");
763

764
765
766
767
768
769
        // Bring the best block into scope
        view.GetBestBlock();

        // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
        view.SetBackend(dummy);
        }
Pieter Wuille's avatar
Pieter Wuille committed
770

771
        // Check for non-standard pay-to-script-hash in inputs
772
        if (Params().NetworkID() == CChainParams::MAIN && !AreInputsStandard(tx, view))
773
            return error("AcceptToMemoryPool: : nonstandard transaction input");
Gavin Andresen's avatar
Gavin Andresen committed
774

775
776
777
778
        // Note: if you modify this code to accept non-standard transactions, then
        // you should add code here to check that the transaction does a
        // reasonable number of ECDSA signature verifications.

779
780
781
782
783
784
785
        int64_t nValueIn = view.GetValueIn(tx);
        int64_t nValueOut = tx.GetValueOut();
        int64_t nFees = nValueIn-nValueOut;
        double dPriority = view.GetPriority(tx, chainActive.Height());

        CTxMemPoolEntry entry(tx, nFees, GetTime(), dPriority, chainActive.Height());
        unsigned int nSize = entry.GetTxSize();
786
787

        // Don't accept it if it can't get into a block
788
        int64_t txMinFee = GetMinFee(tx, nSize, true, GMF_RELAY);
789
        if (fLimitFree && nFees < txMinFee)
Gavin Andresen's avatar
Gavin Andresen committed
790
            return state.DoS(0, error("AcceptToMemoryPool : not enough fees %s, %"PRId64" < %"PRId64,
791
                                      hash.ToString(), nFees, txMinFee),
Gavin Andresen's avatar
Gavin Andresen committed
792
                             REJECT_INSUFFICIENTFEE, "insufficient fee");
793

794
        // Continuously rate-limit free transactions
795
        // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
796
        // be annoying or make others' transactions take longer to confirm.
Gavin Andresen's avatar
Gavin Andresen committed
797
        if (fLimitFree && nFees < CTransaction::nMinRelayTxFee)
798
        {
799
            static CCriticalSection csFreeLimiter;
800
            static double dFreeCount;
801
802
            static int64_t nLastTime;
            int64_t nNow = GetTime();
803

804
            LOCK(csFreeLimiter);
805
806
807
808
809
810

            // Use an exponentially decaying ~10-minute window:
            dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
            nLastTime = nNow;
            // -limitfreerelay unit is thousand-bytes-per-minute
            // At default rate it would take over a month to fill 1GB
811
            if (dFreeCount >= GetArg("-limitfreerelay", 15)*10*1000)
Gavin Andresen's avatar
Gavin Andresen committed
812
813
                return state.DoS(0, error("AcceptToMemoryPool : free transaction rejected by rate limiter"),
                                 REJECT_INSUFFICIENTFEE, "insufficient priority");
814
            LogPrint("mempool", "Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
815
            dFreeCount += nSize;
816
        }
817

818
        if (fRejectInsaneFee && nFees > CTransaction::nMinRelayTxFee * 10000)
819
            return error("AcceptToMemoryPool: : insane fees %s, %"PRId64" > %"PRId64,
820
                         hash.ToString(),
821
822
                         nFees, CTransaction::nMinRelayTxFee * 10000);

823
824
        // Check against previous transactions
        // This is done last to help prevent CPU exhaustion denial-of-service attacks.
825
        if (!CheckInputs(tx, state, view, true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC))
826
        {
827
            return error("AcceptToMemoryPool: : ConnectInputs failed %s", hash.ToString());
828
        }
829
830
        // Store transaction in memory
        pool.addUnchecked(hash, entry);
s_nakamoto's avatar
s_nakamoto committed
831
832
    }

833
    g_signals.SyncTransaction(hash, tx, NULL);
s_nakamoto's avatar
s_nakamoto committed
834
835
836
837

    return true;
}

838

839
int CMerkleTx::GetDepthInMainChain(CBlockIndex* &pindexRet) const
s_nakamoto's avatar
s_nakamoto committed
840
841
842
843
844
845
846
847
848
{
    if (hashBlock == 0 || nIndex == -1)
        return 0;

    // Find the block it claims to be in
    map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
    if (mi == mapBlockIndex.end())
        return 0;
    CBlockIndex* pindex = (*mi).second;
849
    if (!pindex || !chainActive.Contains(pindex))
s_nakamoto's avatar
s_nakamoto committed
850
851
852
853
854
855
856
857
858
859
        return 0;

    // Make sure the merkle branch connects to this block
    if (!fMerkleVerified)
    {
        if (CBlock::CheckMerkleBranch(GetHash(), vMerkleBranch, nIndex) != pindex->hashMerkleRoot)
            return 0;
        fMerkleVerified = true;
    }

860
    pindexRet = pindex;
861
    return chainActive.Height() - pindex->nHeight + 1;
s_nakamoto's avatar
s_nakamoto committed
862
863
864
865
866
867
868
}


int CMerkleTx::GetBlocksToMaturity() const
{
    if (!IsCoinBase())
        return 0;
869
    return max(0, (COINBASE_MATURITY+1) - GetDepthInMainChain());
s_nakamoto's avatar
s_nakamoto committed
870
871
872
}


873
bool CMerkleTx::AcceptToMemoryPool(bool fLimitFree)
s_nakamoto's avatar
s_nakamoto committed
874
{
Pieter Wuille's avatar
Pieter Wuille committed
875
    CValidationState state;
876
    return ::AcceptToMemoryPool(mempool, state, *this, fLimitFree, NULL);
s_nakamoto's avatar
s_nakamoto committed
877
878
879
}


880
// Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock
Pieter Wuille's avatar
Pieter Wuille committed
881
bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
882
{
Pieter Wuille's avatar
Pieter Wuille committed
883
    CBlockIndex *pindexSlow = NULL;
884
885
886
    {
        LOCK(cs_main);
        {
887
            if (mempool.lookup(hash, txOut))
888
889
890
891
            {
                return true;
            }
        }
Pieter Wuille's avatar
Pieter Wuille committed
892

893
894
895
896
897
898
899
900
901
902
        if (fTxIndex) {
            CDiskTxPos postx;
            if (pblocktree->ReadTxIndex(hash, postx)) {
                CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
                CBlockHeader header;
                try {
                    file >> header;
                    fseek(file, postx.nTxOffset, SEEK_CUR);
                    file >> txOut;
                } catch (std::exception &e) {
903
                    return error("%s : Deserialize or I/O error - %s", __PRETTY_FUNCTION__, e.what());
904
905
906
                }
                hashBlock = header.GetHash();
                if (txOut.GetHash() != hash)
907
                    return error("%s : txid mismatch", __PRETTY_FUNCTION__);
908
909
910
911
                return true;
            }
        }

Pieter Wuille's avatar
Pieter Wuille committed
912
913
914
        if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
            int nHeight = -1;
            {
915
                CCoinsViewCache &view = *pcoinsTip;
Pieter Wuille's avatar
Pieter Wuille committed
916
917
918
919
920
                CCoins coins;
                if (view.GetCoins(hash, coins))
                    nHeight = coins.nHeight;
            }
            if (nHeight > 0)
921
                pindexSlow = chainActive[nHeight];
922
923
        }
    }
s_nakamoto's avatar
s_nakamoto committed
924

Pieter Wuille's avatar
Pieter Wuille committed
925
926
    if (pindexSlow) {
        CBlock block;
927
        if (ReadBlockFromDisk(block, pindexSlow)) {
Pieter Wuille's avatar
Pieter Wuille committed
928
929
930
931
932
933
934
935
936
            BOOST_FOREACH(const CTransaction &tx, block.vtx) {
                if (tx.GetHash() == hash) {
                    txOut = tx;
                    hashBlock = pindexSlow->GetBlockHash();
                    return true;
                }
            }
        }
    }
s_nakamoto's avatar
s_nakamoto committed
937

Pieter Wuille's avatar
Pieter Wuille committed
938
939
    return false;
}
s_nakamoto's avatar
s_nakamoto committed
940
941
942
943
944
945
946
947
948
949
950






//////////////////////////////////////////////////////////////////////////////
//
// CBlock and CBlockIndex
//

951
952
953
954
955
bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos)
{
    // Open history file to append
    CAutoFile fileout = CAutoFile(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
    if (!fileout)
956
        return error("WriteBlockToDisk : OpenBlockFile failed");
957
958
959
960
961
962
963
964

    // Write index header
    unsigned int nSize = fileout.GetSerializeSize(block);
    fileout << FLATDATA(Params().MessageStart()) << nSize;

    // Write block
    long fileOutPos = ftell(fileout);
    if (fileOutPos < 0)
965
        return error("WriteBlockToDisk : ftell failed");
966
967
968
969
970
971
972
973
974
975
976
    pos.nPos = (unsigned int)fileOutPos;
    fileout << block;

    // Flush stdio buffers and commit to disk before returning
    fflush(fileout);
    if (!IsInitialBlockDownload())
        FileCommit(fileout);

    return true;
}

977
978
979
980
981
982
983
bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos)
{
    block.SetNull();

    // Open history file to read
    CAutoFile filein = CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
    if (!filein)
984
        return error("ReadBlockFromDisk : OpenBlockFile failed");
985
986
987
988
989
990

    // Read block
    try {
        filein >> block;
    }
    catch (std::exception &e) {
991
        return error("%s : Deserialize or I/O error - %s", __PRETTY_FUNCTION__, e.what());
992
993
994
995
    }

    // Check the header
    if (!CheckProofOfWork(block.GetHash(), block.nBits))
996
        return error("ReadBlockFromDisk : Errors in block header");
997
998
999
1000

    return true;
}

1001
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex)
s_nakamoto's avatar
s_nakamoto committed
1002
{
1003
    if (!ReadBlockFromDisk(block, pindex->GetBlockPos()))
s_nakamoto's avatar
s_nakamoto committed
1004
        return false;
1005
1006
    if (block.GetHash() != pindex->GetBlockHash())
        return error("ReadBlockFromDisk(CBlock&, CBlockIndex*) : GetHash() doesn't match index");
s_nakamoto's avatar
s_nakamoto committed
1007
1008
1009
    return true;
}

1010
uint256 static GetOrphanRoot(const uint256& hash)
s_nakamoto's avatar
s_nakamoto committed
1011
{
1012
1013
1014
1015
    map<uint256, COrphanBlock*>::iterator it = mapOrphanBlocks.find(hash);
    if (it == mapOrphanBlocks.end())
        return hash;

s_nakamoto's avatar
s_nakamoto committed
1016
    // Work back to the first block in the orphan chain
1017
1018
1019
1020
1021
1022
    do {
        map<uint256, COrphanBlock*>::iterator it2 = mapOrphanBlocks.find(it->second->hashPrev);
        if (it2 == mapOrphanBlocks.end())
            return it->first;
        it = it2;
    } while(true);
s_nakamoto's avatar
s_nakamoto committed
1023
1024
}

1025
int64_t GetBlockValue(int nHeight, int64_t nFees)
s_nakamoto's avatar
s_nakamoto committed
1026
{
1027
    int64_t nSubsidy = 50 * COIN;
s_nakamoto's avatar
s_nakamoto committed
1028

1029
1030
    // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
    nSubsidy >>= (nHeight / Params().SubsidyHalvingInterval());
s_nakamoto's avatar
s_nakamoto committed
1031
1032
1033
1034

    return nSubsidy + nFees;
}

1035
1036
1037
static const int64_t nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
static const int64_t nTargetSpacing = 10 * 60;
static const int64_t nInterval = nTargetTimespan / nTargetSpacing;
1038
1039
1040
1041
1042

//
// minimum amount of work that could possibly be required nTime after
// minimum work required was nBase
//
1043
unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime)
1044
{
1045
    const CBigNum &bnLimit = Params().ProofOfWorkLimit();
1046
1047
    // Testnet has min-difficulty blocks
    // after nTargetSpacing*2 time between blocks:
1048
1049
    if (TestNet() && nTime > nTargetSpacing*2)
        return bnLimit.GetCompact();
1050

1051
1052
    CBigNum bnResult;
    bnResult.SetCompact(nBase);
1053
    while (nTime > 0 && bnResult < bnLimit)
1054
1055
1056
1057
1058
1059
    {
        // Maximum 400% adjustment...
        bnResult *= 4;
        // ... in best-case exactly 4-times-normal target time
        nTime -= nTargetTimespan*4;
    }
1060
1061
    if (bnResult > bnLimit)
        bnResult = bnLimit;
1062
1063
1064
    return bnResult.GetCompact();
}

1065
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
s_nakamoto's avatar
s_nakamoto committed
1066
{
1067
    unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact();
s_nakamoto's avatar
s_nakamoto committed
1068
1069
1070

    // Genesis block
    if (pindexLast == NULL)
1071
        return nProofOfWorkLimit;
s_nakamoto's avatar
s_nakamoto committed
1072
1073
1074

    // Only change once per interval
    if ((pindexLast->nHeight+1) % nInterval != 0)
1075
    {
1076
        if (TestNet())
1077
        {
1078
            // Special difficulty rule for testnet:
1079
1080
            // If the new block's timestamp is more than 2* 10 minutes
            // then allow mining of a min-difficulty block.
1081
            if (pblock->nTime > pindexLast->nTime + nTargetSpacing*2)
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
                return nProofOfWorkLimit;
            else
            {
                // Return the last non-special-min-difficulty-rules-block
                const CBlockIndex* pindex = pindexLast;
                while (pindex->pprev && pindex->nHeight % nInterval != 0 && pindex->nBits == nProofOfWorkLimit)
                    pindex = pindex->pprev;
                return pindex->nBits;
            }
        }
s_nakamoto's avatar
s_nakamoto committed
1092
        return pindexLast->nBits;
1093
    }
s_nakamoto's avatar
s_nakamoto committed
1094
1095
1096
1097
1098
1099
1100
1101

    // Go back by what we want to be 14 days worth of blocks
    const CBlockIndex* pindexFirst = pindexLast;
    for (int i = 0; pindexFirst && i < nInterval-1; i++)
        pindexFirst = pindexFirst->pprev;
    assert(pindexFirst);

    // Limit adjustment step
1102
1103
    int64_t nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
    LogPrintf("  nActualTimespan = %"PRId64"  before bounds\n", nActualTimespan);
s_nakamoto's avatar
s_nakamoto committed
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
    if (nActualTimespan < nTargetTimespan/4)
        nActualTimespan = nTargetTimespan/4;
    if (nActualTimespan > nTargetTimespan*4)
        nActualTimespan = nTargetTimespan*4;

    // Retarget
    CBigNum bnNew;
    bnNew.SetCompact(pindexLast->nBits);
    bnNew *= nActualTimespan;
    bnNew /= nTargetTimespan;

1115
1116
    if (bnNew > Params().ProofOfWorkLimit())
        bnNew = Params().ProofOfWorkLimit();
s_nakamoto's avatar
s_nakamoto committed
1117
1118

    /// debug print
1119
    LogPrintf("GetNextWorkRequired RETARGET\n");
1120
    LogPrintf("nTargetTimespan = %"PRId64"    nActualTimespan = %"PRId64"\n", nTargetTimespan, nActualTimespan);
1121
1122
    LogPrintf("Before: %08x  %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString());
    LogPrintf("After:  %08x  %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString());
s_nakamoto's avatar
s_nakamoto committed
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132

    return bnNew.GetCompact();
}

bool CheckProofOfWork(uint256 hash, unsigned int nBits)
{
    CBigNum bnTarget;
    bnTarget.SetCompact(nBits);

    // Check range
1133
    if (bnTarget <= 0 || bnTarget > Params().ProofOfWorkLimit())
s_nakamoto's avatar
s_nakamoto committed
1134
1135
1136
1137
1138
1139
1140
1141
1142
        return error("CheckProofOfWork() : nBits below minimum work");

    // Check proof of work matches claimed amount
    if (hash > bnTarget.getuint256())
        return error("CheckProofOfWork() : hash doesn't match nBits");

    return true;
}

1143
// Return maximum amount of blocks that other nodes claim to have
1144
int GetNumBlocksOfPeers()
1145
{
1146
    return std::max(cPeerBlockCounts.median(), Checkpoints::GetTotalBlocksEstimate());
1147
1148
}

s_nakamoto's avatar
s_nakamoto committed
1149
1150
bool IsInitialBlockDownload()
{
1151
    if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate())
s_nakamoto's avatar
s_nakamoto committed
1152
        return true;
1153
    static int64_t nLastUpdate;
s_nakamoto's avatar
s_nakamoto committed
1154
    static CBlockIndex* pindexLastBest;
1155
    if (chainActive.Tip() != pindexLastBest)
s_nakamoto's avatar
s_nakamoto committed
1156
    {
1157
        pindexLastBest = chainActive.Tip();
s_nakamoto's avatar
s_nakamoto committed
1158
1159
1160
        nLastUpdate = GetTime();
    }
    return (GetTime() - nLastUpdate < 10 &&
1161
            chainActive.Tip()->GetBlockTime() < GetTime() - 24 * 60 * 60);
s_nakamoto's avatar
s_nakamoto committed
1162
1163
}

1164
bool fLargeWorkForkFound = false;
1165
bool fLargeWorkInvalidChainFound = false;
1166
1167
1168
1169
CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;

void CheckForkWarningConditions()
{
1170
1171
1172
1173
1174
    // Before we get past initial download, we cannot reliably alert about forks
    // (we assume we don't get stuck on a fork before the last checkpoint)
    if (IsInitialBlockDownload())
        return;

1175
1176
    // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
    // of our head, drop it
1177
    if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
1178
1179
        pindexBestForkTip = NULL;

1180
    if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6).getuint256()))
1181
    {
1182
1183
1184
1185
1186
        if (!fLargeWorkForkFound)
        {
            std::string strCmd = GetArg("-alertnotify", "");
            if (!strCmd.empty())
            {
1187
1188
                std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
                                      pindexBestForkBase->phashBlock->ToString() + std::string("'");
1189
1190
1191
1192
                boost::replace_all(strCmd, "%s", warning);
                boost::thread t(runCommand, strCmd); // thread runs free
            }
        }
1193
1194
        if (pindexBestForkTip)
        {
1195
            LogPrintf("CheckForkWarningConditions: Warning: Large valid fork found\n  forking the chain at height %d (%s)\n  lasting to height %d (%s).\nChain state database corruption likely.\n",
1196
1197
                   pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
                   pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1198
1199
1200
1201
            fLargeWorkForkFound = true;
        }
        else
        {
1202
            LogPrintf("CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n");
1203
1204
1205
1206
1207
            fLargeWorkInvalidChainFound = true;
        }
    }
    else
    {
1208
        fLargeWorkForkFound = false;
1209
1210
        fLargeWorkInvalidChainFound = false;
    }
1211
1212
1213
1214
1215
1216
}

void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
{
    // If we are on a fork that is sufficiently large, set a warning flag
    CBlockIndex* pfork = pindexNewForkTip;
1217
    CBlockIndex* plonger = chainActive.Tip();
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
    while (pfork && pfork != plonger)
    {
        while (plonger && plonger->nHeight > pfork->nHeight)
            plonger = plonger->pprev;
        if (pfork == plonger)
            break;
        pfork = pfork->pprev;
    }

    // We define a condition which we should warn the user about as a fork of at least 7 blocks
    // who's tip is within 72 blocks (+/- 12 hours if no one mines it) of ours
    // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
    // hash rate operating on the fork.
    // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
    // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
    // the 7-block condition and from this always have the most-likely-to-cause-warning fork
    if (pfork && (!pindexBestForkTip || (pindexBestForkTip && pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) &&
            pindexNewForkTip->nChainWork - pfork->nChainWork > (pfork->GetBlockWork() * 7).getuint256() &&
1236
            chainActive.Height() - pindexNewForkTip->nHeight < 72)
1237
1238
1239
1240
1241
1242
1243
1244
    {
        pindexBestForkTip = pindexNewForkTip;
        pindexBestForkBase = pfork;
    }

    CheckForkWarningConditions();
}

Pieter Wuille's avatar
Pieter Wuille committed
1245
void static InvalidChainFound(CBlockIndex* pindexNew)
s_nakamoto's avatar
s_nakamoto committed
1246
{
1247
    if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
s_nakamoto's avatar
s_nakamoto committed
1248
    {
1249
1250
1251
1252
1253
        pindexBestInvalid = pindexNew;
        // The current code doesn't actually read the BestInvalidWork entry in
        // the block database anymore, as it is derived from the flags in block
        // index entry. We only write it for backward compatibility.
        pblocktree->WriteBestInvalidWork(CBigNum(pindexBestInvalid->nChainWork));
1254
        uiInterface.NotifyBlocksChanged();
s_nakamoto's avatar
s_nakamoto committed
1255
    }
1256
    LogPrintf("InvalidChainFound: invalid block=%s  height=%d  log2_work=%.8g  date=%s\n",
1257
      pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
Pieter Wuille's avatar
Pieter Wuille committed
1258
      log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
1259
      pindexNew->GetBlockTime()));
1260
    LogPrintf("InvalidChainFound:  current best=%s  height=%d  log2_work=%.8g  date=%s\n",
1261
1262
      chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0),
      DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()));
1263
    CheckForkWarningConditions();
s_nakamoto's avatar
s_nakamoto committed
1264
1265
}

1266
1267
void static InvalidBlockFound(CBlockIndex *pindex) {
    pindex->nStatus |= BLOCK_FAILED_VALID;
1268
    pblocktree->WriteBlockIndex(CDiskBlockIndex(pindex));
1269
1270
    setBlockIndexValid.erase(pindex);
    InvalidChainFound(pindex);
1271
    if (chainActive.Next(pindex)) {
Pieter Wuille's avatar
Pieter Wuille committed
1272
1273
1274
        CValidationState stateDummy;
        ConnectBestBlock(stateDummy); // reorganise away from the failed block
    }
1275
1276
}

Pieter Wuille's avatar
Pieter Wuille committed
1277
bool ConnectBestBlock(CValidationState &state) {
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
    do {
        CBlockIndex *pindexNewBest;

        {
            std::set<CBlockIndex*,CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexValid.rbegin();
            if (it == setBlockIndexValid.rend())
                return true;
            pindexNewBest = *it;
        }

1288
        if (pindexNewBest == chainActive.Tip() || (chainActive.Tip() && pindexNewBest->nChainWork == chainActive.Tip()->nChainWork))
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
            return true; // nothing to do

        // check ancestry
        CBlockIndex *pindexTest = pindexNewBest;
        std::vector<CBlockIndex*> vAttach;
        do {
            if (pindexTest->nStatus & BLOCK_FAILED_MASK) {
                // mark descendants failed
                CBlockIndex *pindexFailed = pindexNewBest;
                while (pindexTest != pindexFailed) {
                    pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
                    setBlockIndexValid.erase(pindexFailed);
1301
                    pblocktree->WriteBlockIndex(CDiskBlockIndex(pindexFailed));
1302
1303
1304
1305
1306
1307
                    pindexFailed = pindexFailed->pprev;
                }
                InvalidChainFound(pindexNewBest);
                break;
            }

1308
            if (chainActive.Tip() == NULL || pindexTest->nChainWork > chainActive.Tip()->nChainWork)
1309
1310
                vAttach.push_back(pindexTest);

1311
            if (pindexTest->pprev == NULL || chainActive.Next(pindexTest)) {
1312
                reverse(vAttach.begin(), vAttach.end());
1313
                BOOST_FOREACH(CBlockIndex *pindexSwitch, vAttach) {
Gavin Andresen's avatar
Gavin Andresen committed
1314
                    boost::this_thread::interruption_point();
Pieter Wuille's avatar
Pieter Wuille committed
1315
1316
1317
1318
1319
1320
                    try {
                        if (!SetBestChain(state, pindexSwitch))
                            return false;
                    } catch(std::runtime_error &e) {
                        return state.Abort(_("System error: ") + e.what());
                    }
1321
                }
1322
1323
1324
1325
1326
1327
1328
                return true;
            }
            pindexTest = pindexTest->pprev;
        } while(true);
    } while(true);
}

1329
void UpdateTime(CBlockHeader& block, const CBlockIndex* pindexPrev)
1330
{
1331
    block.nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
1332
1333

    // Updating time can change work required on testnet:
1334
    if (TestNet())
1335
        block.nBits = GetNextWorkRequired(pindexPrev, &block);
1336
1337
}

s_nakamoto's avatar
s_nakamoto committed
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347










1348
void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight, const uint256 &txhash)
Pieter Wuille's avatar
Pieter Wuille committed
1349
{
1350
    bool ret;
Pieter Wuille's avatar
Pieter Wuille committed
1351
    // mark inputs spent
1352
1353
    if (!tx.IsCoinBase()) {
        BOOST_FOREACH(const CTxIn &txin, tx.vin) {
Pieter Wuille's avatar
Pieter Wuille committed
1354
            CCoins &coins = inputs.GetCoins(txin.prevout.hash);
Pieter Wuille's avatar
Pieter Wuille committed
1355
            CTxInUndo undo;
1356
1357
            ret = coins.Spend(txin.prevout, undo);
            assert(ret);
Pieter Wuille's avatar
Pieter Wuille committed
1358
1359
1360
1361
1362
            txundo.vprevout.push_back(undo);
        }
    }

    // add outputs
1363
1364
    ret = inputs.SetCoins(txhash, CCoins(tx, nHeight));
    assert(ret);
Pieter Wuille's avatar
Pieter Wuille committed
1365
1366
}

1367
1368
1369
bool CScriptCheck::operator()() const {
    const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
    if (!VerifyScript(scriptSig, scriptPubKey, *ptxTo, nIn, nFlags, nHashType))
1370
        return error("CScriptCheck() : %s VerifySignature failed", ptxTo->GetHash().ToString());
1371
1372
1373
    return true;
}

Pieter Wuille's avatar
Pieter Wuille committed
1374
1375
bool VerifySignature(const CCoins& txFrom, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType)
{
1376
    return CScriptCheck(txFrom, txTo, nIn, flags, nHashType)();
Pieter Wuille's avatar
Pieter Wuille committed
1377
1378
}

1379
bool CheckInputs(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, std::vector<CScriptCheck> *pvChecks)
s_nakamoto's avatar
s_nakamoto committed
1380
{
1381
    if (!tx.IsCoinBase())
s_nakamoto's avatar
s_nakamoto committed
1382
    {
1383
        if (pvChecks)
1384
            pvChecks->reserve(tx.vin.size());
1385

Pieter Wuille's avatar
Pieter Wuille committed
1386
1387
        // This doesn't trigger the DoS code on purpose; if it did, it would make it easier
        // for an attacker to attempt to split the network.
1388
        if (!inputs.HaveInputs(tx))
1389
            return state.Invalid(error("CheckInputs() : %s inputs unavailable", tx.GetHash().ToString()));
Pieter Wuille's avatar
Pieter Wuille committed
1390

1391
1392
        // While checking, GetBestBlock() refers to the parent block.
        // This is also true for mempool checks.
1393
1394
        CBlockIndex *pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
        int nSpendHeight = pindexPrev->nHeight + 1;
1395
1396
        int64_t nValueIn = 0;
        int64_t nFees = 0;
1397
        for (unsigned int i = 0; i < tx.vin.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
1398
        {
1399
            const COutPoint &prevout = tx.vin[i].prevout;
Pieter Wuille's avatar
Pieter Wuille committed
1400
            const CCoins &coins = inputs.GetCoins(prevout.hash);
s_nakamoto's avatar
s_nakamoto committed
1401
1402

            // If prev is coinbase, check that it's matured
Pieter Wuille's avatar
Pieter Wuille committed
1403
            if (coins.IsCoinBase()) {
1404
                if (nSpendHeight - coins.nHeight < COINBASE_MATURITY)
Gavin Andresen's avatar
Gavin Andresen committed
1405
1406
1407
                    return state.Invalid(
                        error("CheckInputs() : tried to spend coinbase at depth %d", nSpendHeight - coins.nHeight),
                        REJECT_INVALID, "premature spend of coinbase");
Pieter Wuille's avatar
Pieter Wuille committed
1408
            }
s_nakamoto's avatar
s_nakamoto committed
1409

1410
            // Check for negative or overflow input values
Pieter Wuille's avatar
Pieter Wuille committed
1411
1412
            nValueIn += coins.vout[prevout.n].nValue;
            if (!MoneyRange(coins.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
Gavin Andresen's avatar
Gavin Andresen committed
1413
1414
                return state.DoS(100, error("CheckInputs() : txin values out of range"),
                                 REJECT_INVALID, "input values out of range");
1415
1416

        }
Pieter Wuille's avatar
Pieter Wuille committed
1417

1418
        if (nValueIn < tx.GetValueOut())
1419
            return state.DoS(100, error("CheckInputs() : %s value in < value out", tx.GetHash().ToString()),
Gavin Andresen's avatar
Gavin Andresen committed
1420
                             REJECT_INVALID, "in < out");
Pieter Wuille's avatar
Pieter Wuille committed
1421
1422

        // Tally transaction fees
1423
        int64_t nTxFee = nValueIn - tx.GetValueOut();
Pieter Wuille's avatar
Pieter Wuille committed
1424
        if (nTxFee < 0)
1425
            return state.DoS(100, error("CheckInputs() : %s nTxFee < 0", tx.GetHash().ToString()),
Gavin Andresen's avatar
Gavin Andresen committed
1426
                             REJECT_INVALID, "fee < 0");
Pieter Wuille's avatar
Pieter Wuille committed
1427
1428
        nFees += nTxFee;
        if (!MoneyRange(nFees))
Gavin Andresen's avatar
Gavin Andresen committed
1429
1430
            return state.DoS(100, error("CheckInputs() : nFees out of range"),
                             REJECT_INVALID, "fee out of range");
Pieter Wuille's avatar
Pieter Wuille committed
1431

1432
1433
1434
1435
        // The first loop above does all the inexpensive checks.
        // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
        // Helps prevent CPU exhaustion attacks.

Pieter Wuille's avatar
Pieter Wuille committed
1436
        // Skip ECDSA signature verification when connecting blocks
1437
        // before the last block chain checkpoint. This is safe because block merkle hashes are
Pieter Wuille's avatar
Pieter Wuille committed
1438
        // still computed and checked, and any change will be caught at the next checkpoint.
1439
        if (fScriptChecks) {
1440
1441
            for (unsigned int i = 0; i < tx.vin.size(); i++) {
                const COutPoint &prevout = tx.vin[i].prevout;
Pieter Wuille's avatar
Pieter Wuille committed
1442
                const CCoins &coins = inputs.GetCoins(prevout.hash);
1443

1444
                // Verify signature
1445
                CScriptCheck check(coins, tx, i, flags, 0);
1446
1447
1448
                if (pvChecks) {
                    pvChecks->push_back(CScriptCheck());
                    check.swap(pvChecks->back());
1449
1450
1451
1452
                } else if (!check()) {
                    if (flags & SCRIPT_VERIFY_STRICTENC) {
                        // For now, check whether the failure was caused by non-canonical
                        // encodings or not; if so, don't trigger DoS protection.
1453
                        CScriptCheck check(coins, tx, i, flags & (~SCRIPT_VERIFY_STRICTENC), 0);
1454
                        if (check())
Gavin Andresen's avatar
Gavin Andresen committed
1455
                            return state.Invalid(false, REJECT_NONSTANDARD, "non-canonical");
1456
                    }
Gavin Andresen's avatar
Gavin Andresen committed
1457
                    return state.DoS(100,false, REJECT_NONSTANDARD, "non-canonical");
1458
                }
1459
            }
s_nakamoto's avatar
s_nakamoto committed
1460
1461
1462
1463
1464
1465
1466
1467
        }
    }

    return true;
}



1468
bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
s_nakamoto's avatar
s_nakamoto committed
1469
{
1470
    assert(pindex->GetBlockHash() == view.GetBestBlock());
s_nakamoto's avatar
s_nakamoto committed
1471

1472
1473
1474
1475
1476
    if (pfClean)
        *pfClean = false;

    bool fClean = true;

Pieter Wuille's avatar
Pieter Wuille committed
1477
    CBlockUndo blockUndo;
Pieter Wuille's avatar
Pieter Wuille committed
1478
1479
1480
1481
1482
    CDiskBlockPos pos = pindex->GetUndoPos();
    if (pos.IsNull())
        return error("DisconnectBlock() : no undo data available");
    if (!blockUndo.ReadFromDisk(pos, pindex->pprev->GetBlockHash()))
        return error("DisconnectBlock() : failure reading undo data");
s_nakamoto's avatar
s_nakamoto committed
1483

1484
    if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
1485
        return error("DisconnectBlock() : block and undo data inconsistent");
Pieter Wuille's avatar
Pieter Wuille committed
1486
1487

    // undo transactions in reverse order
1488
1489
    for (int i = block.vtx.size() - 1; i >= 0; i--) {
        const CTransaction &tx = block.vtx[i];
Pieter Wuille's avatar
Pieter Wuille committed
1490
1491
        uint256 hash = tx.GetHash();

1492
1493
1494
1495
1496
1497
        // Check that all outputs are available and match the outputs in the block itself
        // exactly. Note that transactions with only provably unspendable outputs won't
        // have outputs available even in the block itself, so we handle that case
        // specially with outsEmpty.
        CCoins outsEmpty;
        CCoins &outs = view.HaveCoins(hash) ? view.GetCoins(hash) : outsEmpty;
1498
        outs.ClearUnspendable();
Pieter Wuille's avatar
Pieter Wuille committed
1499
1500

        CCoins outsBlock = CCoins(tx, pindex->nHeight);
1501
1502
1503
1504
1505
        // The CCoins serialization does not serialize negative numbers.
        // No network rules currently depend on the version here, so an inconsistency is harmless
        // but it must be corrected before txout nversion ever influences a network rule.
        if (outsBlock.nVersion < 0)
            outs.nVersion = outsBlock.nVersion;
Pieter Wuille's avatar
Pieter Wuille committed
1506
        if (outs != outsBlock)
1507
            fClean = fClean && error("DisconnectBlock() : added transaction mismatch? database corrupted");
Pieter Wuille's avatar
Pieter Wuille committed
1508
1509

        // remove outputs
Pieter Wuille's avatar
Pieter Wuille committed
1510
        outs = CCoins();
Pieter Wuille's avatar
Pieter Wuille committed
1511
1512
1513
1514

        // restore inputs
        if (i > 0) { // not coinbases
            const CTxUndo &txundo = blockUndo.vtxundo[i-1];
1515
1516
            if (txundo.vprevout.size() != tx.vin.size())
                return error("DisconnectBlock() : transaction and undo data inconsistent");
Pieter Wuille's avatar
Pieter Wuille committed
1517
1518
1519
1520
1521
            for (unsigned int j = tx.vin.size(); j-- > 0;) {
                const COutPoint &out = tx.vin[j].prevout;
                const CTxInUndo &undo = txundo.vprevout[j];
                CCoins coins;
                view.GetCoins(out.hash, coins); // this can fail if the prevout was already entirely spent
1522
1523
1524
1525
1526
                if (undo.nHeight != 0) {
                    // undo data contains height: this is the last output of the prevout tx being spent
                    if (!coins.IsPruned())
                        fClean = fClean && error("DisconnectBlock() : undo data overwriting existing transaction");
                    coins = CCoins();
Pieter Wuille's avatar
Pieter Wuille committed
1527
1528
1529
1530
                    coins.fCoinBase = undo.fCoinBase;
                    coins.nHeight = undo.nHeight;
                    coins.nVersion = undo.nVersion;
                } else {
1531
1532
                    if (coins.IsPruned())
                        fClean = fClean && error("DisconnectBlock() : undo data adding output to missing transaction");
Pieter Wuille's avatar
Pieter Wuille committed
1533
1534
                }
                if (coins.IsAvailable(out.n))
1535
                    fClean = fClean && error("DisconnectBlock() : undo data overwriting existing output");
Pieter Wuille's avatar
Pieter Wuille committed
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
                if (coins.vout.size() < out.n+1)
                    coins.vout.resize(out.n+1);
                coins.vout[out.n] = undo.txout;
                if (!view.SetCoins(out.hash, coins))
                    return error("DisconnectBlock() : cannot restore coin inputs");
            }
        }
    }

    // move best block pointer to prevout block
1546
    view.SetBestBlock(pindex->pprev->GetBlockHash());
Pieter Wuille's avatar
Pieter Wuille committed
1547

1548
1549
1550
1551
1552
1553
    if (pfClean) {
        *pfClean = fClean;
        return true;
    } else {
        return fClean;
    }
s_nakamoto's avatar
s_nakamoto committed
1554
1555
}

1556
void static FlushBlockFile(bool fFinalize = false)
Pieter Wuille's avatar
Pieter Wuille committed
1557
1558
1559
{
    LOCK(cs_LastBlockFile);

1560
    CDiskBlockPos posOld(nLastBlockFile, 0);
Pieter Wuille's avatar
Pieter Wuille committed
1561
1562

    FILE *fileOld = OpenBlockFile(posOld);
1563
    if (fileOld) {
1564
1565
        if (fFinalize)
            TruncateFile(fileOld, infoLastBlockFile.nSize);
1566
1567
1568
        FileCommit(fileOld);
        fclose(fileOld);
    }
Pieter Wuille's avatar
Pieter Wuille committed
1569
1570

    fileOld = OpenUndoFile(posOld);
1571
    if (fileOld) {
1572
1573
        if (fFinalize)
            TruncateFile(fileOld, infoLastBlockFile.nUndoSize);
1574
1575
1576
        FileCommit(fileOld);
        fclose(fileOld);
    }
Pieter Wuille's avatar
Pieter Wuille committed
1577
1578
}

Pieter Wuille's avatar
Pieter Wuille committed
1579
bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
Pieter Wuille's avatar
Pieter Wuille committed
1580

1581
1582
static CCheckQueue<CScriptCheck> scriptcheckqueue(128);

1583
void ThreadScriptCheck() {
1584
1585
1586
1587
    RenameThread("bitcoin-scriptch");
    scriptcheckqueue.Thread();
}

1588
bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck)
s_nakamoto's avatar
s_nakamoto committed
1589
1590
{
    // Check it again in case a previous version let a bad block in
1591
    if (!CheckBlock(block, state, !fJustCheck, !fJustCheck))
s_nakamoto's avatar
s_nakamoto committed
1592
1593
        return false;

Pieter Wuille's avatar
Pieter Wuille committed
1594
    // verify that the view's current state corresponds to the previous block
1595
1596
    uint256 hashPrevBlock = pindex->pprev == NULL ? uint256(0) : pindex->pprev->GetBlockHash();
    assert(hashPrevBlock == view.GetBestBlock());
Pieter Wuille's avatar
Pieter Wuille committed
1597

1598
1599
    // Special case for the genesis block, skipping connection of its transactions
    // (its coinbase is unspendable)
1600
    if (block.GetHash() == Params().HashGenesisBlock()) {
1601
        view.SetBestBlock(pindex->GetBlockHash());
1602
1603
1604
        return true;
    }

1605
1606
    bool fScriptChecks = pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate();

1607
1608
1609
1610
1611
1612
1613
    // Do not allow blocks that contain transactions which 'overwrite' older transactions,
    // unless those are already completely spent.
    // If such overwrites are allowed, coinbases and transactions depending upon those
    // can be duplicated to remove the ability to spend the first instance -- even after
    // being sent to another address.
    // See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
    // This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
1614
    // already refuses previously-known transaction ids entirely.
1615
1616
1617
1618
    // This rule was originally applied all blocks whose timestamp was after March 15, 2012, 0:00 UTC.
    // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
    // two in the chain that violate it. This prevents exploiting the issue against nodes in their
    // initial block download.
1619
1620
    bool fEnforceBIP30 = (!pindex->phashBlock) || // Enforce on CreateNewBlock invocations which don't have a hash.
                          !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
1621
                           (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
Pieter Wuille's avatar
Pieter Wuille committed
1622
    if (fEnforceBIP30) {
1623
1624
        for (unsigned int i = 0; i < block.vtx.size(); i++) {
            uint256 hash = block.GetTxHash(i);
Pieter Wuille's avatar
Pieter Wuille committed
1625
            if (view.HaveCoins(hash) && !view.GetCoins(hash).IsPruned())
Gavin Andresen's avatar
Gavin Andresen committed
1626
1627
                return state.DoS(100, error("ConnectBlock() : tried to overwrite transaction"),
                                 REJECT_INVALID, "BIP30");
Pieter Wuille's avatar
Pieter Wuille committed
1628
1629
        }
    }
1630

1631
    // BIP16 didn't become active until Apr 1 2012
1632
    int64_t nBIP16SwitchTime = 1333238400;
1633
    bool fStrictPayToScriptHash = (pindex->nTime >= nBIP16SwitchTime);
1634

1635
1636
1637
    unsigned int flags = SCRIPT_VERIFY_NOCACHE |
                         (fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE);

Pieter Wuille's avatar
Pieter Wuille committed
1638
1639
    CBlockUndo blockundo;

1640
1641
    CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);

1642
1643
    int64_t nStart = GetTimeMicros();
    int64_t nFees = 0;
1644
    int nInputs = 0;
1645
    unsigned int nSigOps = 0;
1646
    CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
1647
    std::vector<std::pair<uint256, CDiskTxPos> > vPos;
1648
1649
    vPos.reserve(block.vtx.size());
    for (unsigned int i = 0; i < block.vtx.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
1650
    {
1651
        const CTransaction &tx = block.vtx[i];
Pieter Wuille's avatar
Pieter Wuille committed
1652

1653
        nInputs += tx.vin.size();
1654
        nSigOps += GetLegacySigOpCount(tx);
1655
        if (nSigOps > MAX_BLOCK_SIGOPS)
Gavin Andresen's avatar
Gavin Andresen committed
1656
1657
            return state.DoS(100, error("ConnectBlock() : too many sigops"),
                             REJECT_INVALID, "too many sigops");
1658

1659
1660
        if (!tx.IsCoinBase())
        {
1661
            if (!view.HaveInputs(tx))
Gavin Andresen's avatar
Gavin Andresen committed
1662
1663
                return state.DoS(100, error("ConnectBlock() : inputs missing/spent"),
                                 REJECT_INVALID, "inputs missing/spent");
1664

1665
1666
1667
1668
1669
            if (fStrictPayToScriptHash)
            {
                // Add in sigops done by pay-to-script-hash inputs;
                // this is to prevent a "rogue miner" from creating
                // an incredibly-expensive-to-validate block.
1670
                nSigOps += GetP2SHSigOpCount(tx, view);
1671
                if (nSigOps > MAX_BLOCK_SIGOPS)
Gavin Andresen's avatar
Gavin Andresen committed
1672
1673
                    return state.DoS(100, error("ConnectBlock() : too many sigops"),
                                     REJECT_INVALID, "too many sigops");
1674
            }
1675

1676
            nFees += view.GetValueIn(tx)-tx.GetValueOut();
Pieter Wuille's avatar
Pieter Wuille committed
1677

1678
            std::vector<CScriptCheck> vChecks;
1679
            if (!CheckInputs(tx, state, view, fScriptChecks, flags, nScriptCheckThreads ? &vChecks : NULL))
1680
                return false;
1681
            control.Add(vChecks);
1682
1683
        }

Pieter Wuille's avatar
Pieter Wuille committed
1684
        CTxUndo txundo;
1685
        UpdateCoins(tx, state, view, txundo, pindex->nHeight, block.GetTxHash(i));
Pieter Wuille's avatar
Pieter Wuille committed
1686
1687
        if (!tx.IsCoinBase())
            blockundo.vtxundo.push_back(txundo);
1688

1689
        vPos.push_back(std::make_pair(block.GetTxHash(i), pos));
1690
        pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
s_nakamoto's avatar
s_nakamoto committed
1691
    }
1692
    int64_t nTime = GetTimeMicros() - nStart;
1693
    if (fBenchmark)
1694
        LogPrintf("- Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin)\n", (unsigned)block.vtx.size(), 0.001 * nTime, 0.001 * nTime / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * nTime / (nInputs-1));
Gavin Andresen's avatar
Gavin Andresen committed
1695

1696
    if (block.vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
Gavin Andresen's avatar
Gavin Andresen committed
1697
1698
        return state.DoS(100,
                         error("ConnectBlock() : coinbase pays too much (actual=%"PRId64" vs limit=%"PRId64")",
1699
                               block.vtx[0].GetValueOut(), GetBlockValue(pindex->nHeight, nFees)),
Gavin Andresen's avatar
Gavin Andresen committed
1700
                         REJECT_INVALID, "coinbase too large");
Pieter Wuille's avatar
Pieter Wuille committed
1701

1702
    if (!control.Wait())
Pieter Wuille's avatar
Pieter Wuille committed
1703
        return state.DoS(100, false);
1704
    int64_t nTime2 = GetTimeMicros() - nStart;
1705
    if (fBenchmark)
1706
        LogPrintf("- Verify %u txins: %.2fms (%.3fms/txin)\n", nInputs - 1, 0.001 * nTime2, nInputs <= 1 ? 0 : 0.001 * nTime2 / (nInputs-1));
1707

1708
1709
1710
    if (fJustCheck)
        return true;

Pieter Wuille's avatar
Pieter Wuille committed
1711
    // Write undo information to disk
1712
    if (pindex->GetUndoPos().IsNull() || (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS)
Pieter Wuille's avatar
Pieter Wuille committed
1713
    {
1714
1715
        if (pindex->GetUndoPos().IsNull()) {
            CDiskBlockPos pos;
Pieter Wuille's avatar
Pieter Wuille committed
1716
            if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
1717
                return error("ConnectBlock() : FindUndoPos failed");
Pieter Wuille's avatar
Pieter Wuille committed
1718
            if (!blockundo.WriteToDisk(pos, pindex->pprev->GetBlockHash()))
Pieter Wuille's avatar
Pieter Wuille committed
1719
                return state.Abort(_("Failed to write undo data"));
1720
1721
1722
1723
1724
1725
1726
1727

            // update nUndoPos in block index
            pindex->nUndoPos = pos.nPos;
            pindex->nStatus |= BLOCK_HAVE_UNDO;
        }

        pindex->nStatus = (pindex->nStatus & ~BLOCK_VALID_MASK) | BLOCK_VALID_SCRIPTS;

Pieter Wuille's avatar
Pieter Wuille committed
1728
        CDiskBlockIndex blockindex(pindex);
1729
        if (!pblocktree->WriteBlockIndex(blockindex))
Pieter Wuille's avatar
Pieter Wuille committed
1730
            return state.Abort(_("Failed to write block index"));
s_nakamoto's avatar
s_nakamoto committed
1731
1732
    }

1733
    if (fTxIndex)
Pieter Wuille's avatar
Pieter Wuille committed
1734
        if (!pblocktree->WriteTxIndex(vPos))
Pieter Wuille's avatar
Pieter Wuille committed
1735
            return state.Abort(_("Failed to write transaction index"));
1736

1737
    // add this block to the view's block chain
1738
1739
1740
    bool ret;
    ret = view.SetBestBlock(pindex->GetBlockHash());
    assert(ret);
Pieter Wuille's avatar
Pieter Wuille committed
1741

s_nakamoto's avatar
s_nakamoto committed
1742
    // Watch for transactions paying to me
1743
    for (unsigned int i = 0; i < block.vtx.size(); i++)
1744
        g_signals.SyncTransaction(block.GetTxHash(i), block.vtx[i], &block);
s_nakamoto's avatar
s_nakamoto committed
1745
1746
1747
1748

    return true;
}

Pieter Wuille's avatar
Pieter Wuille committed
1749
bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew)
s_nakamoto's avatar
s_nakamoto committed
1750
{
1751
    mempool.check(pcoinsTip);
Pieter Wuille's avatar
Pieter Wuille committed
1752

Pieter Wuille's avatar
Pieter Wuille committed
1753
1754
1755
    // All modifications to the coin state will be done in this cache.
    // Only when all have succeeded, we push it to pcoinsTip.
    CCoinsViewCache view(*pcoinsTip, true);
Pieter Wuille's avatar
Pieter Wuille committed
1756
1757

    // Find the fork (typically, there is none)
1758
1759
1760
    std::map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(view.GetBestBlock());
    CBlockIndex* ptip = (it != mapBlockIndex.end()) ? it->second : NULL;
    CBlockIndex* pfork = ptip;
s_nakamoto's avatar
s_nakamoto committed
1761
    CBlockIndex* plonger = pindexNew;
1762
    while (pfork && pfork != plonger)
s_nakamoto's avatar
s_nakamoto committed
1763
    {
1764
1765
1766
1767
        while (plonger->nHeight > pfork->nHeight) {
            plonger = plonger->pprev;
            assert(plonger != NULL);
        }
s_nakamoto's avatar
s_nakamoto committed
1768
1769
        if (pfork == plonger)
            break;
1770
1771
        pfork = pfork->pprev;
        assert(pfork != NULL);
s_nakamoto's avatar
s_nakamoto committed
1772
1773
    }

Pieter Wuille's avatar
Pieter Wuille committed
1774
    // List of what to disconnect (typically nothing)
s_nakamoto's avatar
s_nakamoto committed
1775
    vector<CBlockIndex*> vDisconnect;
1776
    for (CBlockIndex* pindex = ptip; pindex != pfork; pindex = pindex->pprev)
s_nakamoto's avatar
s_nakamoto committed
1777
1778
        vDisconnect.push_back(pindex);

Pieter Wuille's avatar
Pieter Wuille committed
1779
    // List of what to connect (typically only pindexNew)
s_nakamoto's avatar
s_nakamoto committed
1780
1781
1782
1783
1784
    vector<CBlockIndex*> vConnect;
    for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev)
        vConnect.push_back(pindex);
    reverse(vConnect.begin(), vConnect.end());

Pieter Wuille's avatar
Pieter Wuille committed
1785
    if (vDisconnect.size() > 0) {
1786
1787
        LogPrintf("REORGANIZE: Disconnect %"PRIszu" blocks; %s...\n", vDisconnect.size(), pfork->GetBlockHash().ToString());
        LogPrintf("REORGANIZE: Connect %"PRIszu" blocks; ...%s\n", vConnect.size(), pindexNew->GetBlockHash().ToString());
Pieter Wuille's avatar
Pieter Wuille committed
1788
    }
1789

s_nakamoto's avatar
s_nakamoto committed
1790
    // Disconnect shorter branch
1791
    list<CTransaction> vResurrect;
Pieter Wuille's avatar
Pieter Wuille committed
1792
    BOOST_FOREACH(CBlockIndex* pindex, vDisconnect) {
s_nakamoto's avatar
s_nakamoto committed
1793
        CBlock block;
1794
        if (!ReadBlockFromDisk(block, pindex))
Pieter Wuille's avatar
Pieter Wuille committed
1795
            return state.Abort(_("Failed to read block"));
1796
        int64_t nStart = GetTimeMicros();
1797
        if (!DisconnectBlock(block, state, pindex, view))
1798
            return error("SetBestBlock() : DisconnectBlock %s failed", pindex->GetBlockHash().ToString());
1799
        if (fBenchmark)
1800
            LogPrintf("- Disconnect: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
s_nakamoto's avatar
s_nakamoto committed
1801

1802
1803
1804
        // Queue memory transactions to resurrect.
        // We only do this for blocks after the last checkpoint (reorganisation before that
        // point should only happen with -reindex/-loadblock, or a misbehaving peer.
1805
        BOOST_REVERSE_FOREACH(const CTransaction& tx, block.vtx)
1806
            if (!tx.IsCoinBase() && pindex->nHeight > Checkpoints::GetTotalBlocksEstimate())
1807
                vResurrect.push_front(tx);
s_nakamoto's avatar
s_nakamoto committed
1808
1809
1810
1811
    }

    // Connect longer branch
    vector<CTransaction> vDelete;
Pieter Wuille's avatar
Pieter Wuille committed
1812
    BOOST_FOREACH(CBlockIndex *pindex, vConnect) {
s_nakamoto's avatar
s_nakamoto committed
1813
        CBlock block;
1814
        if (!ReadBlockFromDisk(block, pindex))
Pieter Wuille's avatar
Pieter Wuille committed
1815
            return state.Abort(_("Failed to read block"));
1816
        int64_t nStart = GetTimeMicros();
1817
        if (!ConnectBlock(block, state, pindex, view)) {
Pieter Wuille's avatar
Pieter Wuille committed
1818
1819
1820
1821
            if (state.IsInvalid()) {
                InvalidChainFound(pindexNew);
                InvalidBlockFound(pindex);
            }
1822
            return error("SetBestBlock() : ConnectBlock %s failed", pindex->GetBlockHash().ToString());
s_nakamoto's avatar
s_nakamoto committed
1823
        }
1824
        if (fBenchmark)
1825
            LogPrintf("- Connect: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
s_nakamoto's avatar
s_nakamoto committed
1826
1827

        // Queue memory transactions to delete
1828
        BOOST_FOREACH(const CTransaction& tx, block.vtx)
s_nakamoto's avatar
s_nakamoto committed
1829
1830
1831
            vDelete.push_back(tx);
    }

Pieter Wuille's avatar
Pieter Wuille committed
1832
    // Flush changes to global coin state
1833
    int64_t nStart = GetTimeMicros();
1834
    int nModified = view.GetCacheSize();
1835
1836
1837
    bool ret;
    ret = view.Flush();
    assert(ret);
1838
    int64_t nTime = GetTimeMicros() - nStart;
1839
    if (fBenchmark)
1840
        LogPrintf("- Flush %i transactions: %.2fms (%.4fms/tx)\n", nModified, 0.001 * nTime, 0.001 * nTime / nModified);
Pieter Wuille's avatar
Pieter Wuille committed
1841

1842
    // Make sure it's successfully written to disk before changing memory structure
1843
    bool fIsInitialDownload = IsInitialBlockDownload();
Pieter Wuille's avatar
Pieter Wuille committed
1844
    if (!fIsInitialDownload || pcoinsTip->GetCacheSize() > nCoinCacheSize) {
1845
1846
1847
1848
1849
1850
1851
        // Typical CCoins structures on disk are around 100 bytes in size.
        // Pushing a new one to the database can cause it to be written
        // twice (once in the log, and once in the tables). This is already
        // an overestimation, as most will delete an existing entry or
        // overwrite one. Still, use a conservative safety factor of 2.
        if (!CheckDiskSpace(100 * 2 * 2 * pcoinsTip->GetCacheSize()))
            return state.Error();
Pieter Wuille's avatar
Pieter Wuille committed
1852
        FlushBlockFile();
1853
        pblocktree->Sync();
Pieter Wuille's avatar
Pieter Wuille committed
1854
        if (!pcoinsTip->Flush())
Pieter Wuille's avatar
Pieter Wuille committed
1855
            return state.Abort(_("Failed to write to coin database"));
Pieter Wuille's avatar
Pieter Wuille committed
1856
    }
Pieter Wuille's avatar
Pieter Wuille committed
1857
1858
1859

    // At this point, all changes have been done to the database.
    // Proceed by updating the memory structures.
s_nakamoto's avatar
s_nakamoto committed
1860

1861
    // Register new best chain
1862
    chainActive.SetTip(pindexNew);
s_nakamoto's avatar
s_nakamoto committed
1863
1864

    // Resurrect memory transactions that were in the disconnected branch
Pieter Wuille's avatar
Pieter Wuille committed
1865
1866
1867
    BOOST_FOREACH(CTransaction& tx, vResurrect) {
        // ignore validation errors in resurrected transactions
        CValidationState stateDummy;
1868
        if (!AcceptToMemoryPool(mempool,stateDummy, tx, false, NULL))
1869
            mempool.remove(tx, true);
Pieter Wuille's avatar
Pieter Wuille committed
1870
    }
s_nakamoto's avatar
s_nakamoto committed
1871
1872

    // Delete redundant memory transactions that are in the connected branch
1873
    BOOST_FOREACH(CTransaction& tx, vDelete) {
1874
        mempool.remove(tx);
1875
1876
        mempool.removeConflicts(tx);
    }
s_nakamoto's avatar
s_nakamoto committed
1877

1878
    mempool.check(pcoinsTip);
Pieter Wuille's avatar
Pieter Wuille committed
1879

1880
    // Update best block in wallet (so we can detect restored wallets)
1881
    if ((pindexNew->nHeight % 20160) == 0 || (!fIsInitialDownload && (pindexNew->nHeight % 144) == 0))
1882
        g_signals.SetBestChain(chainActive.GetLocator(pindexNew));
1883

s_nakamoto's avatar
s_nakamoto committed
1884
1885
    // New best block
    nTimeBestReceived = GetTime();
1886
    mempool.AddTransactionsUpdated(1);
1887
    LogPrintf("SetBestChain: new best=%s  height=%d  log2_work=%.8g  tx=%lu  date=%s progress=%f\n",
1888
1889
      chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)pindexNew->nChainTx,
      DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
1890
      Checkpoints::GuessVerificationProgress(chainActive.Tip()));
s_nakamoto's avatar
s_nakamoto committed
1891

1892
1893
1894
1895
    // Check the version of the last 100 blocks to see if we need to upgrade:
    if (!fIsInitialDownload)
    {
        int nUpgraded = 0;
1896
        const CBlockIndex* pindex = chainActive.Tip();
1897
1898
1899
1900
1901
1902
1903
        for (int i = 0; i < 100 && pindex != NULL; i++)
        {
            if (pindex->nVersion > CBlock::CURRENT_VERSION)
                ++nUpgraded;
            pindex = pindex->pprev;
        }
        if (nUpgraded > 0)
1904
            LogPrintf("SetBestChain: %d of last 100 blocks above version %d\n", nUpgraded, (int)CBlock::CURRENT_VERSION);
1905
1906
        if (nUpgraded > 100/2)
            // strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
1907
            strMiscWarning = _("Warning: This version is obsolete, upgrade required!");
1908
1909
    }

1910
1911
1912
1913
    std::string strCmd = GetArg("-blocknotify", "");

    if (!fIsInitialDownload && !strCmd.empty())
    {
1914
        boost::replace_all(strCmd, "%s", chainActive.Tip()->GetBlockHash().GetHex());
1915
1916
1917
        boost::thread t(runCommand, strCmd); // thread runs free
    }

s_nakamoto's avatar
s_nakamoto committed
1918
1919
1920
1921
    return true;
}


1922
bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos& pos)
s_nakamoto's avatar
s_nakamoto committed
1923
1924
{
    // Check for duplicate
1925
    uint256 hash = block.GetHash();
s_nakamoto's avatar
s_nakamoto committed
1926
    if (mapBlockIndex.count(hash))
1927
        return state.Invalid(error("AddToBlockIndex() : %s already exists", hash.ToString()));
s_nakamoto's avatar
s_nakamoto committed
1928
1929

    // Construct new block index object
1930
    CBlockIndex* pindexNew = new CBlockIndex(block);
1931
    assert(pindexNew);
s_nakamoto's avatar
s_nakamoto committed
1932
1933
    map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
    pindexNew->phashBlock = &((*mi).first);
1934
    map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
s_nakamoto's avatar
s_nakamoto committed
1935
1936
1937
1938
1939
    if (miPrev != mapBlockIndex.end())
    {
        pindexNew->pprev = (*miPrev).second;
        pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
    }
1940
    pindexNew->nTx = block.vtx.size();
Pieter Wuille's avatar
Pieter Wuille committed
1941
    pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + pindexNew->GetBlockWork().getuint256();
1942
1943
1944
    pindexNew->nChainTx = (pindexNew->pprev ? pindexNew->pprev->nChainTx : 0) + pindexNew->nTx;
    pindexNew->nFile = pos.nFile;
    pindexNew->nDataPos = pos.nPos;
Pieter Wuille's avatar
Pieter Wuille committed
1945
    pindexNew->nUndoPos = 0;
1946
1947
    pindexNew->nStatus = BLOCK_VALID_TRANSACTIONS | BLOCK_HAVE_DATA;
    setBlockIndexValid.insert(pindexNew);
s_nakamoto's avatar
s_nakamoto committed
1948

Pieter Wuille's avatar
Pieter Wuille committed
1949
    if (!pblocktree->WriteBlockIndex(CDiskBlockIndex(pindexNew)))
Pieter Wuille's avatar
Pieter Wuille committed
1950
        return state.Abort(_("Failed to write block index"));
s_nakamoto's avatar
s_nakamoto committed
1951

1952
    // New best?
Pieter Wuille's avatar
Pieter Wuille committed
1953
    if (!ConnectBestBlock(state))
1954
        return false;
s_nakamoto's avatar
s_nakamoto committed
1955

1956
    if (pindexNew == chainActive.Tip())
s_nakamoto's avatar
s_nakamoto committed
1957
    {
1958
1959
        // Clear fork warning if its no longer applicable
        CheckForkWarningConditions();
s_nakamoto's avatar
s_nakamoto committed
1960
1961
        // Notify UI to display prev block's coinbase if it was ours
        static uint256 hashPrevBestCoinBase;
1962
        g_signals.UpdatedTransaction(hashPrevBestCoinBase);
1963
        hashPrevBestCoinBase = block.GetTxHash(0);
1964
1965
    } else
        CheckForkWarningConditionsOnNewFork(pindexNew);
s_nakamoto's avatar
s_nakamoto committed
1966

Pieter Wuille's avatar
Pieter Wuille committed
1967
    if (!pblocktree->Flush())
Pieter Wuille's avatar
Pieter Wuille committed
1968
        return state.Abort(_("Failed to sync block index"));
1969

1970
    uiInterface.NotifyBlocksChanged();
s_nakamoto's avatar
s_nakamoto committed
1971
1972
1973
1974
    return true;
}


1975
bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
Pieter Wuille's avatar
Pieter Wuille committed
1976
1977
1978
1979
1980
{
    bool fUpdatedLast = false;

    LOCK(cs_LastBlockFile);

1981
1982
1983
1984
1985
    if (fKnown) {
        if (nLastBlockFile != pos.nFile) {
            nLastBlockFile = pos.nFile;
            infoLastBlockFile.SetNull();
            pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile);
1986
            fUpdatedLast = true;
1987
1988
1989
        }
    } else {
        while (infoLastBlockFile.nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
1990
            LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, infoLastBlockFile.ToString());
1991
            FlushBlockFile(true);
1992
1993
1994
1995
1996
1997
1998
            nLastBlockFile++;
            infoLastBlockFile.SetNull();
            pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile); // check whether data for the new file somehow already exist; can fail just fine
            fUpdatedLast = true;
        }
        pos.nFile = nLastBlockFile;
        pos.nPos = infoLastBlockFile.nSize;
Pieter Wuille's avatar
Pieter Wuille committed
1999
2000
2001
2002
2003
    }

    infoLastBlockFile.nSize += nAddSize;
    infoLastBlockFile.AddBlock(nHeight, nTime);

2004
2005
2006
2007
    if (!fKnown) {
        unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
        unsigned int nNewChunks = (infoLastBlockFile.nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
        if (nNewChunks > nOldChunks) {
2008
2009
2010
            if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
                FILE *file = OpenBlockFile(pos);
                if (file) {
2011
                    LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
2012
2013
2014
                    AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
                    fclose(file);
                }
2015
            }
2016
            else
2017
                return state.Error();
2018
2019
2020
        }
    }

2021
    if (!pblocktree->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
Pieter Wuille's avatar
Pieter Wuille committed
2022
        return state.Abort(_("Failed to write file info"));
Pieter Wuille's avatar
Pieter Wuille committed
2023
    if (fUpdatedLast)
2024
        pblocktree->WriteLastBlockFile(nLastBlockFile);
Pieter Wuille's avatar
Pieter Wuille committed
2025
2026
2027
2028

    return true;
}

Pieter Wuille's avatar
Pieter Wuille committed
2029
bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
Pieter Wuille's avatar
Pieter Wuille committed
2030
2031
2032
2033
2034
{
    pos.nFile = nFile;

    LOCK(cs_LastBlockFile);

2035
    unsigned int nNewSize;
Pieter Wuille's avatar
Pieter Wuille committed
2036
2037
    if (nFile == nLastBlockFile) {
        pos.nPos = infoLastBlockFile.nUndoSize;
2038
        nNewSize = (infoLastBlockFile.nUndoSize += nAddSize);
2039
        if (!pblocktree->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
Pieter Wuille's avatar
Pieter Wuille committed
2040
            return state.Abort(_("Failed to write block info"));
2041
2042
    } else {
        CBlockFileInfo info;
2043
        if (!pblocktree->ReadBlockFileInfo(nFile, info))
Pieter Wuille's avatar
Pieter Wuille committed
2044
            return state.Abort(_("Failed to read block info"));
2045
2046
        pos.nPos = info.nUndoSize;
        nNewSize = (info.nUndoSize += nAddSize);
2047
        if (!pblocktree->WriteBlockFileInfo(nFile, info))
Pieter Wuille's avatar
Pieter Wuille committed
2048
            return state.Abort(_("Failed to write block info"));
2049
2050
2051
2052
2053
    }

    unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
    unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
    if (nNewChunks > nOldChunks) {
2054
2055
2056
        if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
            FILE *file = OpenUndoFile(pos);
            if (file) {
2057
                LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
2058
2059
2060
                AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
                fclose(file);
            }
2061
        }
2062
        else
2063
            return state.Error();
Pieter Wuille's avatar
Pieter Wuille committed
2064
2065
2066
2067
2068
    }

    return true;
}

s_nakamoto's avatar
s_nakamoto committed
2069

2070
bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot)
s_nakamoto's avatar
s_nakamoto committed
2071
2072
2073
2074
2075
{
    // These are checks that are independent of context
    // that can be verified before saving an orphan block.

    // Size limits
2076
    if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_SIZE || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
Gavin Andresen's avatar
Gavin Andresen committed
2077
2078
        return state.DoS(100, error("CheckBlock() : size limits failed"),
                         REJECT_INVALID, "block size too large");
s_nakamoto's avatar
s_nakamoto committed
2079

2080
    // Check proof of work matches claimed amount
2081
    if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits))
Gavin Andresen's avatar
Gavin Andresen committed
2082
2083
        return state.DoS(50, error("CheckBlock() : proof of work failed"),
                         REJECT_INVALID, "invalid pow");
2084

s_nakamoto's avatar
s_nakamoto committed
2085
    // Check timestamp
2086
    if (block.GetBlockTime() > GetAdjustedTime() + 2 * 60 * 60)
Gavin Andresen's avatar
Gavin Andresen committed
2087
2088
        return state.Invalid(error("CheckBlock() : block timestamp too far in the future"),
                             REJECT_INVALID, "time in future");
s_nakamoto's avatar
s_nakamoto committed
2089
2090

    // First transaction must be coinbase, the rest must not be
2091
    if (block.vtx.empty() || !block.vtx[0].IsCoinBase())
Gavin Andresen's avatar
Gavin Andresen committed
2092
2093
        return state.DoS(100, error("CheckBlock() : first tx is not coinbase"),
                         REJECT_INVALID, "no coinbase");
2094
2095
    for (unsigned int i = 1; i < block.vtx.size(); i++)
        if (block.vtx[i].IsCoinBase())
Gavin Andresen's avatar
Gavin Andresen committed
2096
2097
            return state.DoS(100, error("CheckBlock() : more than one coinbase"),
                             REJECT_INVALID, "duplicate coinbase");
s_nakamoto's avatar
s_nakamoto committed
2098
2099

    // Check transactions
2100
    BOOST_FOREACH(const CTransaction& tx, block.vtx)
2101
        if (!CheckTransaction(tx, state))
Pieter Wuille's avatar
Pieter Wuille committed
2102
            return error("CheckBlock() : CheckTransaction failed");
s_nakamoto's avatar
s_nakamoto committed
2103

Pieter Wuille's avatar
Pieter Wuille committed
2104
2105
2106
    // Build the merkle tree already. We need it anyway later, and it makes the
    // block cache the transaction hashes, which means they don't need to be
    // recalculated many times during this block's validation.
2107
    block.BuildMerkleTree();
Pieter Wuille's avatar
Pieter Wuille committed
2108

2109
2110
2111
    // Check for duplicate txids. This is caught by ConnectInputs(),
    // but catching it earlier avoids a potential DoS attack:
    set<uint256> uniqueTx;
2112
2113
    for (unsigned int i = 0; i < block.vtx.size(); i++) {
        uniqueTx.insert(block.GetTxHash(i));
2114
    }
2115
    if (uniqueTx.size() != block.vtx.size())
Gavin Andresen's avatar
Gavin Andresen committed
2116
2117
        return state.DoS(100, error("CheckBlock() : duplicate transaction"),
                         REJECT_INVALID, "duplicate transaction", true);
2118

2119
    unsigned int nSigOps = 0;
2120
    BOOST_FOREACH(const CTransaction& tx, block.vtx)
Gavin Andresen's avatar
Gavin Andresen committed
2121
    {
2122
        nSigOps += GetLegacySigOpCount(tx);
Gavin Andresen's avatar
Gavin Andresen committed
2123
2124
    }
    if (nSigOps > MAX_BLOCK_SIGOPS)
Gavin Andresen's avatar
Gavin Andresen committed
2125
2126
        return state.DoS(100, error("CheckBlock() : out-of-bounds SigOpCount"),
                         REJECT_INVALID, "sig op count", true);
s_nakamoto's avatar
s_nakamoto committed
2127

2128
    // Check merkle root
2129
    if (fCheckMerkleRoot && block.hashMerkleRoot != block.vMerkleTree.back())
Gavin Andresen's avatar
Gavin Andresen committed
2130
2131
        return state.DoS(100, error("CheckBlock() : hashMerkleRoot mismatch"),
                         REJECT_INVALID, "bad merkle root", true);
s_nakamoto's avatar
s_nakamoto committed
2132
2133
2134
2135

    return true;
}

2136
bool AcceptBlock(CBlock& block, CValidationState& state, CDiskBlockPos* dbp)
s_nakamoto's avatar
s_nakamoto committed
2137
2138
{
    // Check for duplicate
2139
    uint256 hash = block.GetHash();
s_nakamoto's avatar
s_nakamoto committed
2140
    if (mapBlockIndex.count(hash))
Pieter Wuille's avatar
Pieter Wuille committed
2141
        return state.Invalid(error("AcceptBlock() : block already in mapBlockIndex"));
s_nakamoto's avatar
s_nakamoto committed
2142
2143

    // Get prev block index
2144
2145
    CBlockIndex* pindexPrev = NULL;
    int nHeight = 0;
2146
    if (hash != Params().HashGenesisBlock()) {
2147
        map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
2148
        if (mi == mapBlockIndex.end())
Pieter Wuille's avatar
Pieter Wuille committed
2149
            return state.DoS(10, error("AcceptBlock() : prev block not found"));
2150
2151
2152
2153
        pindexPrev = (*mi).second;
        nHeight = pindexPrev->nHeight+1;

        // Check proof of work
2154
        if (block.nBits != GetNextWorkRequired(pindexPrev, &block))
Gavin Andresen's avatar
Gavin Andresen committed
2155
2156
            return state.DoS(100, error("AcceptBlock() : incorrect proof of work"),
                             REJECT_INVALID, "bad pow");
2157
2158

        // Check timestamp against prev
2159
        if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
Gavin Andresen's avatar
Gavin Andresen committed
2160
2161
            return state.Invalid(error("AcceptBlock() : block's timestamp is too early"),
                                 REJECT_INVALID, "timestamp too early");
2162
2163

        // Check that all transactions are finalized
2164
2165
        BOOST_FOREACH(const CTransaction& tx, block.vtx)
            if (!IsFinalTx(tx, nHeight, block.GetBlockTime()))
Gavin Andresen's avatar
Gavin Andresen committed
2166
2167
                return state.DoS(10, error("AcceptBlock() : contains a non-final transaction"),
                                 REJECT_INVALID, "non-final tx");
2168
2169
2170

        // Check that the block chain matches the known block chain up to a checkpoint
        if (!Checkpoints::CheckBlock(nHeight, hash))
Gavin Andresen's avatar
Gavin Andresen committed
2171
2172
            return state.DoS(100, error("AcceptBlock() : rejected by checkpoint lock-in at %d", nHeight),
                             REJECT_CHECKPOINT, "checkpoint mismatch");
2173
2174

        // Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
2175
        if (block.nVersion < 2)
2176
        {
2177
2178
            if ((!TestNet() && CBlockIndex::IsSuperMajority(2, pindexPrev, 950, 1000)) ||
                (TestNet() && CBlockIndex::IsSuperMajority(2, pindexPrev, 75, 100)))
2179
            {
Gavin Andresen's avatar
Gavin Andresen committed
2180
2181
                return state.Invalid(error("AcceptBlock() : rejected nVersion=1 block"),
                                     REJECT_OBSOLETE, "version 1 blocks obsolete");
2182
            }
2183
        }
2184
        // Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
2185
        if (block.nVersion >= 2)
2186
        {
2187
            // if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
2188
2189
            if ((!TestNet() && CBlockIndex::IsSuperMajority(2, pindexPrev, 750, 1000)) ||
                (TestNet() && CBlockIndex::IsSuperMajority(2, pindexPrev, 51, 100)))
2190
2191
            {
                CScript expect = CScript() << nHeight;
Pieter Wuille's avatar
Pieter Wuille committed
2192
2193
                if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
                    !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin()))
Gavin Andresen's avatar
Gavin Andresen committed
2194
2195
                    return state.DoS(100, error("AcceptBlock() : block height mismatch in coinbase"),
                                     REJECT_INVALID, "height incorrect in coinbase");
2196
            }
2197
2198
2199
        }
    }

s_nakamoto's avatar
s_nakamoto committed
2200
    // Write block to history file
Pieter Wuille's avatar
Pieter Wuille committed
2201
    try {
2202
        unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
Pieter Wuille's avatar
Pieter Wuille committed
2203
2204
2205
        CDiskBlockPos blockPos;
        if (dbp != NULL)
            blockPos = *dbp;
2206
        if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.nTime, dbp != NULL))
Pieter Wuille's avatar
Pieter Wuille committed
2207
2208
            return error("AcceptBlock() : FindBlockPos failed");
        if (dbp == NULL)
2209
            if (!WriteBlockToDisk(block, blockPos))
Pieter Wuille's avatar
Pieter Wuille committed
2210
                return state.Abort(_("Failed to write block"));
2211
        if (!AddToBlockIndex(block, state, blockPos))
Pieter Wuille's avatar
Pieter Wuille committed
2212
2213
2214
2215
            return error("AcceptBlock() : AddToBlockIndex failed");
    } catch(std::runtime_error &e) {
        return state.Abort(_("System error: ") + e.what());
    }
s_nakamoto's avatar
s_nakamoto committed
2216
2217

    // Relay inventory, but don't relay old inventory during initial block download
2218
    int nBlockEstimate = Checkpoints::GetTotalBlocksEstimate();
2219
    if (chainActive.Tip()->GetBlockHash() == hash)
2220
2221
2222
    {
        LOCK(cs_vNodes);
        BOOST_FOREACH(CNode* pnode, vNodes)
2223
            if (chainActive.Height() > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate))
2224
2225
                pnode->PushInventory(CInv(MSG_BLOCK, hash));
    }
s_nakamoto's avatar
s_nakamoto committed
2226
2227
2228
2229

    return true;
}

2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired, unsigned int nToCheck)
{
    unsigned int nFound = 0;
    for (unsigned int i = 0; i < nToCheck && nFound < nRequired && pstart != NULL; i++)
    {
        if (pstart->nVersion >= minVersion)
            ++nFound;
        pstart = pstart->pprev;
    }
    return (nFound >= nRequired);
}

2242
int64_t CBlockIndex::GetMedianTime() const
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
{
    const CBlockIndex* pindex = this;
    for (int i = 0; i < nMedianTimeSpan/2; i++)
    {
        if (!chainActive.Next(pindex))
            return GetBlockTime();
        pindex = chainActive.Next(pindex);
    }
    return pindex->GetMedianTimePast();
}

2254
2255
2256
2257
2258
2259
2260
2261
void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)
{
    // Filter out duplicate requests
    if (pindexBegin == pnode->pindexLastGetBlocksBegin && hashEnd == pnode->hashLastGetBlocksEnd)
        return;
    pnode->pindexLastGetBlocksBegin = pindexBegin;
    pnode->hashLastGetBlocksEnd = hashEnd;

2262
    pnode->PushMessage("getblocks", chainActive.GetLocator(pindexBegin), hashEnd);
2263
2264
}

Pieter Wuille's avatar
Pieter Wuille committed
2265
bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
s_nakamoto's avatar
s_nakamoto committed
2266
{
2267
    AssertLockHeld(cs_main);
2268

s_nakamoto's avatar
s_nakamoto committed
2269
2270
2271
    // Check for duplicate
    uint256 hash = pblock->GetHash();
    if (mapBlockIndex.count(hash))
2272
        return state.Invalid(error("ProcessBlock() : already have block %d %s", mapBlockIndex[hash]->nHeight, hash.ToString()));
s_nakamoto's avatar
s_nakamoto committed
2273
    if (mapOrphanBlocks.count(hash))
2274
        return state.Invalid(error("ProcessBlock() : already have block (orphan) %s", hash.ToString()));
s_nakamoto's avatar
s_nakamoto committed
2275
2276

    // Preliminary checks
2277
    if (!CheckBlock(*pblock, state))
s_nakamoto's avatar
s_nakamoto committed
2278
2279
        return error("ProcessBlock() : CheckBlock FAILED");

2280
    CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(mapBlockIndex);
2281
    if (pcheckpoint && pblock->hashPrevBlock != (chainActive.Tip() ? chainActive.Tip()->GetBlockHash() : uint256(0)))
2282
2283
    {
        // Extra checks to prevent "fill up memory by spamming with bogus blocks"
2284
        int64_t deltaTime = pblock->GetBlockTime() - pcheckpoint->nTime;
2285
2286
        if (deltaTime < 0)
        {
Gavin Andresen's avatar
Gavin Andresen committed
2287
2288
            return state.DoS(100, error("ProcessBlock() : block with timestamp before last checkpoint"),
                             REJECT_CHECKPOINT, "timestamp before checkpoint");
2289
2290
2291
2292
2293
2294
2295
        }
        CBigNum bnNewBlock;
        bnNewBlock.SetCompact(pblock->nBits);
        CBigNum bnRequired;
        bnRequired.SetCompact(ComputeMinWork(pcheckpoint->nBits, deltaTime));
        if (bnNewBlock > bnRequired)
        {
Gavin Andresen's avatar
Gavin Andresen committed
2296
2297
            return state.DoS(100, error("ProcessBlock() : block with too little proof-of-work"),
                             REJECT_INVALID, "invalid pow");
2298
2299
2300
2301
        }
    }


2302
    // If we don't already have its previous block, shunt it off to holding area until we get it
2303
    if (pblock->hashPrevBlock != 0 && !mapBlockIndex.count(pblock->hashPrevBlock))
s_nakamoto's avatar
s_nakamoto committed
2304
    {
2305
        LogPrintf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString());
s_nakamoto's avatar
s_nakamoto committed
2306

2307
2308
        // Accept orphans as long as there is a node to request its parents from
        if (pfrom) {
2309
2310
2311
2312
2313
2314
2315
2316
            COrphanBlock* pblock2 = new COrphanBlock();
            {
                CDataStream ss(SER_DISK, CLIENT_VERSION);
                ss << *pblock;
                pblock2->vchBlock = std::vector<unsigned char>(ss.begin(), ss.end());
            }
            pblock2->hashBlock = hash;
            pblock2->hashPrev = pblock->hashPrevBlock;
2317
            mapOrphanBlocks.insert(make_pair(hash, pblock2));
2318
            mapOrphanBlocksByPrev.insert(make_pair(pblock2->hashPrev, pblock2));
2319
2320

            // Ask this guy to fill in what we're missing
2321
            PushGetBlocks(pfrom, chainActive.Tip(), GetOrphanRoot(hash));
2322
        }
s_nakamoto's avatar
s_nakamoto committed
2323
2324
2325
2326
        return true;
    }

    // Store to disk
2327
    if (!AcceptBlock(*pblock, state, dbp))
s_nakamoto's avatar
s_nakamoto committed
2328
2329
2330
2331
2332
        return error("ProcessBlock() : AcceptBlock FAILED");

    // Recursively process any orphan blocks that depended on this one
    vector<uint256> vWorkQueue;
    vWorkQueue.push_back(hash);
2333
    for (unsigned int i = 0; i < vWorkQueue.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
2334
2335
    {
        uint256 hashPrev = vWorkQueue[i];
2336
        for (multimap<uint256, COrphanBlock*>::iterator mi = mapOrphanBlocksByPrev.lower_bound(hashPrev);
s_nakamoto's avatar
s_nakamoto committed
2337
2338
2339
             mi != mapOrphanBlocksByPrev.upper_bound(hashPrev);
             ++mi)
        {
2340
2341
2342
2343
2344
2345
            CBlock block;
            {
                CDataStream ss(mi->second->vchBlock, SER_DISK, CLIENT_VERSION);
                ss >> block;
            }
            block.BuildMerkleTree();
2346
2347
            // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan resolution (that is, feeding people an invalid block based on LegitBlockX in order to get anyone relaying LegitBlockX banned)
            CValidationState stateDummy;
2348
2349
2350
2351
            if (AcceptBlock(block, stateDummy))
                vWorkQueue.push_back(mi->second->hashBlock);
            mapOrphanBlocks.erase(mi->second->hashBlock);
            delete mi->second;
s_nakamoto's avatar
s_nakamoto committed
2352
2353
2354
2355
        }
        mapOrphanBlocksByPrev.erase(hashPrev);
    }

2356
    LogPrintf("ProcessBlock: ACCEPTED\n");
s_nakamoto's avatar
s_nakamoto committed
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
    return true;
}








2367
2368
2369
2370
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter& filter)
{
    header = block.GetBlockHeader();

2371
2372
2373
2374
2375
2376
2377
    vector<bool> vMatch;
    vector<uint256> vHashes;

    vMatch.reserve(block.vtx.size());
    vHashes.reserve(block.vtx.size());

    for (unsigned int i = 0; i < block.vtx.size(); i++)
2378
2379
2380
2381
    {
        uint256 hash = block.vtx[i].GetHash();
        if (filter.IsRelevantAndUpdate(block.vtx[i], hash))
        {
2382
2383
            vMatch.push_back(true);
            vMatchedTxn.push_back(make_pair(i, hash));
2384
        }
2385
2386
2387
        else
            vMatch.push_back(false);
        vHashes.push_back(hash);
2388
    }
2389
2390

    txn = CPartialMerkleTree(vHashes, vMatch);
2391
2392
2393
2394
2395
2396
2397
2398
2399
}








Pieter Wuille's avatar
Pieter Wuille committed
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid) {
    if (height == 0) {
        // hash at height 0 is the txids themself
        return vTxid[pos];
    } else {
        // calculate left hash
        uint256 left = CalcHash(height-1, pos*2, vTxid), right;
        // calculate right hash if not beyong the end of the array - copy left hash otherwise1
        if (pos*2+1 < CalcTreeWidth(height-1))
            right = CalcHash(height-1, pos*2+1, vTxid);
        else
            right = left;
        // combine subhashes
        return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
    }
}

void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) {
    // determine whether this node is the parent of at least one matched txid
    bool fParentOfMatch = false;
    for (unsigned int p = pos << height; p < (pos+1) << height && p < nTransactions; p++)
        fParentOfMatch |= vMatch[p];
    // store as flag bit
    vBits.push_back(fParentOfMatch);
    if (height==0 || !fParentOfMatch) {
        // if at height 0, or nothing interesting below, store hash and stop
        vHash.push_back(CalcHash(height, pos, vTxid));
    } else {
        // otherwise, don't store any hash, but descend into the subtrees
        TraverseAndBuild(height-1, pos*2, vTxid, vMatch);
        if (pos*2+1 < CalcTreeWidth(height-1))
            TraverseAndBuild(height-1, pos*2+1, vTxid, vMatch);
    }
}

uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch) {
    if (nBitsUsed >= vBits.size()) {
        // overflowed the bits array - failure
        fBad = true;
        return 0;
    }
    bool fParentOfMatch = vBits[nBitsUsed++];
    if (height==0 || !fParentOfMatch) {
        // if at height 0, or nothing interesting below, use stored hash and do not descend
        if (nHashUsed >= vHash.size()) {
            // overflowed the hash array - failure
            fBad = true;
            return 0;
        }
        const uint256 &hash = vHash[nHashUsed++];
        if (height==0 && fParentOfMatch) // in case of height 0, we have a matched txid
            vMatch.push_back(hash);
        return hash;
    } else {
        // otherwise, descend into the subtrees to extract matched txids and hashes
        uint256 left = TraverseAndExtract(height-1, pos*2, nBitsUsed, nHashUsed, vMatch), right;
        if (pos*2+1 < CalcTreeWidth(height-1))
            right = TraverseAndExtract(height-1, pos*2+1, nBitsUsed, nHashUsed, vMatch);
        else
            right = left;
        // and combine them before returning
        return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
    }
}

CPartialMerkleTree::CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) : nTransactions(vTxid.size()), fBad(false) {
    // reset state
    vBits.clear();
    vHash.clear();

    // calculate height of tree
    int nHeight = 0;
    while (CalcTreeWidth(nHeight) > 1)
        nHeight++;

    // traverse the partial tree
    TraverseAndBuild(nHeight, 0, vTxid, vMatch);
}

CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {}

uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch) {
    vMatch.clear();
    // An empty set will not work
    if (nTransactions == 0)
        return 0;
    // check for excessively high numbers of transactions
    if (nTransactions > MAX_BLOCK_SIZE / 60) // 60 is the lower bound for the size of a serialized CTransaction
        return 0;
    // there can never be more hashes provided than one for every txid
    if (vHash.size() > nTransactions)
        return 0;
    // there must be at least one bit per node in the partial tree, and at least one node per hash
    if (vBits.size() < vHash.size())
        return 0;
    // calculate height of tree
    int nHeight = 0;
    while (CalcTreeWidth(nHeight) > 1)
        nHeight++;
    // traverse the partial tree
    unsigned int nBitsUsed = 0, nHashUsed = 0;
    uint256 hashMerkleRoot = TraverseAndExtract(nHeight, 0, nBitsUsed, nHashUsed, vMatch);
    // verify that no problems occured during the tree traversal
    if (fBad)
        return 0;
    // verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
    if ((nBitsUsed+7)/8 != (vBits.size()+7)/8)
        return 0;
    // verify that all hashes were consumed
    if (nHashUsed != vHash.size())
        return 0;
    return hashMerkleRoot;
}







2520
2521
bool AbortNode(const std::string &strMessage) {
    strMiscWarning = strMessage;
2522
    LogPrintf("*** %s\n", strMessage);
2523
    uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_ERROR);
2524
2525
2526
    StartShutdown();
    return false;
}
Pieter Wuille's avatar
Pieter Wuille committed
2527

2528
bool CheckDiskSpace(uint64_t nAdditionalBytes)
s_nakamoto's avatar
s_nakamoto committed
2529
{
2530
    uint64_t nFreeBytesAvailable = filesystem::space(GetDataDir()).available;
s_nakamoto's avatar
s_nakamoto committed
2531

2532
2533
    // Check for nMinDiskSpace bytes (currently 50MB)
    if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
2534
2535
        return AbortNode(_("Error: Disk space is low!"));

s_nakamoto's avatar
s_nakamoto committed
2536
2537
2538
    return true;
}

Pieter Wuille's avatar
Pieter Wuille committed
2539
FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
2540
{
Pieter Wuille's avatar
Pieter Wuille committed
2541
    if (pos.IsNull())
s_nakamoto's avatar
s_nakamoto committed
2542
        return NULL;
Pieter Wuille's avatar
Pieter Wuille committed
2543
2544
2545
2546
2547
    boost::filesystem::path path = GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
    boost::filesystem::create_directories(path.parent_path());
    FILE* file = fopen(path.string().c_str(), "rb+");
    if (!file && !fReadOnly)
        file = fopen(path.string().c_str(), "wb+");
Pieter Wuille's avatar
Pieter Wuille committed
2548
    if (!file) {
2549
        LogPrintf("Unable to open file %s\n", path.string());
s_nakamoto's avatar
s_nakamoto committed
2550
        return NULL;
Pieter Wuille's avatar
Pieter Wuille committed
2551
    }
Pieter Wuille's avatar
Pieter Wuille committed
2552
2553
    if (pos.nPos) {
        if (fseek(file, pos.nPos, SEEK_SET)) {
2554
            LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
Pieter Wuille's avatar
Pieter Wuille committed
2555
2556
2557
2558
            fclose(file);
            return NULL;
        }
    }
s_nakamoto's avatar
s_nakamoto committed
2559
2560
2561
    return file;
}

Pieter Wuille's avatar
Pieter Wuille committed
2562
2563
2564
2565
FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
    return OpenDiskFile(pos, "blk", fReadOnly);
}

2566
FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
Pieter Wuille's avatar
Pieter Wuille committed
2567
2568
2569
    return OpenDiskFile(pos, "rev", fReadOnly);
}

2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
CBlockIndex * InsertBlockIndex(uint256 hash)
{
    if (hash == 0)
        return NULL;

    // Return existing
    map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
    if (mi != mapBlockIndex.end())
        return (*mi).second;

    // Create new
    CBlockIndex* pindexNew = new CBlockIndex();
    if (!pindexNew)
        throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
    mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
    pindexNew->phashBlock = &((*mi).first);

    return pindexNew;
}

bool static LoadBlockIndexDB()
{
    if (!pblocktree->LoadBlockIndexGuts())
        return false;

Gavin Andresen's avatar
Gavin Andresen committed
2595
    boost::this_thread::interruption_point();
2596

Pieter Wuille's avatar
Pieter Wuille committed
2597
    // Calculate nChainWork
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
    vector<pair<int, CBlockIndex*> > vSortedByHeight;
    vSortedByHeight.reserve(mapBlockIndex.size());
    BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
    {
        CBlockIndex* pindex = item.second;
        vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
    }
    sort(vSortedByHeight.begin(), vSortedByHeight.end());
    BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
    {
        CBlockIndex* pindex = item.second;
Pieter Wuille's avatar
Pieter Wuille committed
2609
        pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + pindex->GetBlockWork().getuint256();
2610
2611
2612
        pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
        if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS && !(pindex->nStatus & BLOCK_FAILED_MASK))
            setBlockIndexValid.insert(pindex);
2613
2614
        if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
            pindexBestInvalid = pindex;
2615
2616
2617
2618
    }

    // Load block file info
    pblocktree->ReadLastBlockFile(nLastBlockFile);
2619
    LogPrintf("LoadBlockIndexDB(): last block file = %i\n", nLastBlockFile);
2620
    if (pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
2621
        LogPrintf("LoadBlockIndexDB(): last block file info: %s\n", infoLastBlockFile.ToString());
2622

2623
2624
2625
2626
2627
    // Check whether we need to continue reindexing
    bool fReindexing = false;
    pblocktree->ReadReindexing(fReindexing);
    fReindex |= fReindexing;

2628
2629
    // Check whether we have a transaction index
    pblocktree->ReadFlag("txindex", fTxIndex);
2630
    LogPrintf("LoadBlockIndexDB(): transaction index %s\n", fTxIndex ? "enabled" : "disabled");
2631

2632
    // Load pointer to end of best chain
2633
2634
    std::map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
    if (it == mapBlockIndex.end())
2635
        return true;
2636
    chainActive.SetTip(it->second);
2637
    LogPrintf("LoadBlockIndexDB(): hashBestChain=%s  height=%d date=%s\n",
2638
2639
        chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
        DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()));
2640

Pieter Wuille's avatar
Pieter Wuille committed
2641
2642
2643
    return true;
}

2644
2645
bool VerifyDB(int nCheckLevel, int nCheckDepth)
{
2646
    if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
Pieter Wuille's avatar
Pieter Wuille committed
2647
2648
        return true;

2649
    // Verify blocks in the best chain
2650
    if (nCheckDepth <= 0)
2651
        nCheckDepth = 1000000000; // suffices until the year 19000
2652
2653
    if (nCheckDepth > chainActive.Height())
        nCheckDepth = chainActive.Height();
Pieter Wuille's avatar
Pieter Wuille committed
2654
    nCheckLevel = std::max(0, std::min(4, nCheckLevel));
2655
    LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
Pieter Wuille's avatar
Pieter Wuille committed
2656
    CCoinsViewCache coins(*pcoinsTip, true);
2657
    CBlockIndex* pindexState = chainActive.Tip();
Pieter Wuille's avatar
Pieter Wuille committed
2658
2659
    CBlockIndex* pindexFailure = NULL;
    int nGoodTransactions = 0;
Pieter Wuille's avatar
Pieter Wuille committed
2660
    CValidationState state;
2661
    for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
2662
    {
Gavin Andresen's avatar
Gavin Andresen committed
2663
        boost::this_thread::interruption_point();
2664
        if (pindex->nHeight < chainActive.Height()-nCheckDepth)
2665
2666
            break;
        CBlock block;
Pieter Wuille's avatar
Pieter Wuille committed
2667
        // check level 0: read from disk
2668
        if (!ReadBlockFromDisk(block, pindex))
2669
            return error("VerifyDB() : *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
2670
        // check level 1: verify block validity
2671
        if (nCheckLevel >= 1 && !CheckBlock(block, state))
2672
            return error("VerifyDB() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
Pieter Wuille's avatar
Pieter Wuille committed
2673
2674
2675
2676
2677
2678
        // check level 2: verify undo validity
        if (nCheckLevel >= 2 && pindex) {
            CBlockUndo undo;
            CDiskBlockPos pos = pindex->GetUndoPos();
            if (!pos.IsNull()) {
                if (!undo.ReadFromDisk(pos, pindex->pprev->GetBlockHash()))
2679
                    return error("VerifyDB() : *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
Pieter Wuille's avatar
Pieter Wuille committed
2680
2681
2682
2683
2684
            }
        }
        // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
        if (nCheckLevel >= 3 && pindex == pindexState && (coins.GetCacheSize() + pcoinsTip->GetCacheSize()) <= 2*nCoinCacheSize + 32000) {
            bool fClean = true;
2685
            if (!DisconnectBlock(block, state, pindex, coins, &fClean))
2686
                return error("VerifyDB() : *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
Pieter Wuille's avatar
Pieter Wuille committed
2687
2688
2689
2690
2691
2692
            pindexState = pindex->pprev;
            if (!fClean) {
                nGoodTransactions = 0;
                pindexFailure = pindex;
            } else
                nGoodTransactions += block.vtx.size();
2693
2694
        }
    }
Pieter Wuille's avatar
Pieter Wuille committed
2695
    if (pindexFailure)
2696
        return error("VerifyDB() : *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
Pieter Wuille's avatar
Pieter Wuille committed
2697
2698
2699
2700

    // check level 4: try reconnecting blocks
    if (nCheckLevel >= 4) {
        CBlockIndex *pindex = pindexState;
2701
        while (pindex != chainActive.Tip()) {
Gavin Andresen's avatar
Gavin Andresen committed
2702
            boost::this_thread::interruption_point();
2703
            pindex = chainActive.Next(pindex);
2704
            CBlock block;
2705
            if (!ReadBlockFromDisk(block, pindex))
2706
                return error("VerifyDB() : *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
2707
            if (!ConnectBlock(block, state, pindex, coins))
2708
                return error("VerifyDB() : *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
Pieter Wuille's avatar
Pieter Wuille committed
2709
        }
2710
2711
    }

2712
    LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
Pieter Wuille's avatar
Pieter Wuille committed
2713

2714
2715
2716
    return true;
}

2717
2718
2719
2720
void UnloadBlockIndex()
{
    mapBlockIndex.clear();
    setBlockIndexValid.clear();
2721
    chainActive.SetTip(NULL);
2722
    pindexBestInvalid = NULL;
2723
2724
}

2725
bool LoadBlockIndex()
s_nakamoto's avatar
s_nakamoto committed
2726
{
2727
    // Load block index from databases
2728
    if (!fReindex && !LoadBlockIndexDB())
s_nakamoto's avatar
s_nakamoto committed
2729
        return false;
2730
2731
    return true;
}
2732
2733


2734
2735
bool InitBlockIndex() {
    // Check whether we're already initialized
2736
    if (chainActive.Genesis() != NULL)
2737
2738
2739
2740
2741
        return true;

    // Use the provided setting for -txindex in the new database
    fTxIndex = GetBoolArg("-txindex", false);
    pblocktree->WriteFlag("txindex", fTxIndex);
2742
    LogPrintf("Initializing databases...\n");
2743
2744
2745
2746

    // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
    if (!fReindex) {
        try {
2747
2748
            CBlock &block = const_cast<CBlock&>(Params().GenesisBlock());
            // Start new block file
2749
2750
2751
2752
            unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
            CDiskBlockPos blockPos;
            CValidationState state;
            if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.nTime))
2753
                return error("LoadBlockIndex() : FindBlockPos failed");
2754
            if (!WriteBlockToDisk(block, blockPos))
2755
                return error("LoadBlockIndex() : writing genesis block to disk failed");
2756
            if (!AddToBlockIndex(block, state, blockPos))
2757
2758
2759
2760
                return error("LoadBlockIndex() : genesis block not accepted");
        } catch(std::runtime_error &e) {
            return error("LoadBlockIndex() : failed to initialize block database: %s", e.what());
        }
s_nakamoto's avatar
s_nakamoto committed
2761
2762
2763
2764
2765
2766
2767
2768
2769
    }

    return true;
}



void PrintBlockTree()
{
2770
    // pre-compute tree structure
s_nakamoto's avatar
s_nakamoto committed
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
    map<CBlockIndex*, vector<CBlockIndex*> > mapNext;
    for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
    {
        CBlockIndex* pindex = (*mi).second;
        mapNext[pindex->pprev].push_back(pindex);
        // test
        //while (rand() % 3 == 0)
        //    mapNext[pindex->pprev].push_back(pindex);
    }

    vector<pair<int, CBlockIndex*> > vStack;
2782
    vStack.push_back(make_pair(0, chainActive.Genesis()));
s_nakamoto's avatar
s_nakamoto committed
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794

    int nPrevCol = 0;
    while (!vStack.empty())
    {
        int nCol = vStack.back().first;
        CBlockIndex* pindex = vStack.back().second;
        vStack.pop_back();

        // print split or gap
        if (nCol > nPrevCol)
        {
            for (int i = 0; i < nCol-1; i++)
2795
2796
                LogPrintf("| ");
            LogPrintf("|\\\n");
s_nakamoto's avatar
s_nakamoto committed
2797
2798
2799
2800
        }
        else if (nCol < nPrevCol)
        {
            for (int i = 0; i < nCol; i++)
2801
2802
                LogPrintf("| ");
            LogPrintf("|\n");
Pieter Wuille's avatar
Pieter Wuille committed
2803
       }
s_nakamoto's avatar
s_nakamoto committed
2804
2805
2806
2807
        nPrevCol = nCol;

        // print columns
        for (int i = 0; i < nCol; i++)
2808
            LogPrintf("| ");
s_nakamoto's avatar
s_nakamoto committed
2809
2810
2811

        // print item
        CBlock block;
2812
        ReadBlockFromDisk(block, pindex);
2813
        LogPrintf("%d (blk%05u.dat:0x%x)  %s  tx %"PRIszu"",
s_nakamoto's avatar
s_nakamoto committed
2814
            pindex->nHeight,
Pieter Wuille's avatar
Pieter Wuille committed
2815
            pindex->GetBlockPos().nFile, pindex->GetBlockPos().nPos,
2816
            DateTimeStrFormat("%Y-%m-%d %H:%M:%S", block.GetBlockTime()),
s_nakamoto's avatar
s_nakamoto committed
2817
2818
            block.vtx.size());

2819
        // put the main time-chain first
s_nakamoto's avatar
s_nakamoto committed
2820
        vector<CBlockIndex*>& vNext = mapNext[pindex];
2821
        for (unsigned int i = 0; i < vNext.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
2822
        {
2823
            if (chainActive.Next(vNext[i]))
s_nakamoto's avatar
s_nakamoto committed
2824
2825
2826
2827
2828
2829
2830
            {
                swap(vNext[0], vNext[i]);
                break;
            }
        }

        // iterate children
2831
        for (unsigned int i = 0; i < vNext.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
2832
2833
2834
2835
            vStack.push_back(make_pair(nCol+i, vNext[i]));
    }
}

2836
bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
2837
{
2838
    int64_t nStart = GetTimeMillis();
2839

2840
    int nLoaded = 0;
Pieter Wuille's avatar
Pieter Wuille committed
2841
    try {
2842
        CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION);
2843
        uint64_t nStartByte = 0;
2844
2845
2846
2847
2848
2849
2850
2851
        if (dbp) {
            // (try to) skip already indexed part
            CBlockFileInfo info;
            if (pblocktree->ReadBlockFileInfo(dbp->nFile, info)) {
                nStartByte = info.nSize;
                blkdat.Seek(info.nSize);
            }
        }
2852
        uint64_t nRewind = blkdat.GetPos();
2853
2854
2855
        while (blkdat.good() && !blkdat.eof()) {
            boost::this_thread::interruption_point();

2856
2857
2858
            blkdat.SetPos(nRewind);
            nRewind++; // start one byte further next time, in case of failure
            blkdat.SetLimit(); // remove former limit
2859
            unsigned int nSize = 0;
2860
2861
2862
            try {
                // locate a header
                unsigned char buf[4];
2863
                blkdat.FindByte(Params().MessageStart()[0]);
2864
2865
                nRewind = blkdat.GetPos()+1;
                blkdat >> FLATDATA(buf);
2866
                if (memcmp(buf, Params().MessageStart(), 4))
2867
2868
                    continue;
                // read size
2869
                blkdat >> nSize;
2870
2871
                if (nSize < 80 || nSize > MAX_BLOCK_SIZE)
                    continue;
2872
2873
2874
2875
2876
            } catch (std::exception &e) {
                // no valid block header found; don't complain
                break;
            }
            try {
2877
                // read block
2878
                uint64_t nBlockPos = blkdat.GetPos();
2879
                blkdat.SetLimit(nBlockPos + nSize);
2880
2881
2882
                CBlock block;
                blkdat >> block;
                nRewind = blkdat.GetPos();
2883
2884
2885

                // process block
                if (nBlockPos >= nStartByte) {
2886
                    LOCK(cs_main);
2887
2888
                    if (dbp)
                        dbp->nPos = nBlockPos;
Pieter Wuille's avatar
Pieter Wuille committed
2889
2890
                    CValidationState state;
                    if (ProcessBlock(state, NULL, &block, dbp))
2891
                        nLoaded++;
Pieter Wuille's avatar
Pieter Wuille committed
2892
2893
                    if (state.IsError())
                        break;
2894
                }
2895
            } catch (std::exception &e) {
2896
                LogPrintf("%s : Deserialize or I/O error - %s", __PRETTY_FUNCTION__, e.what());
2897
2898
            }
        }
2899
        fclose(fileIn);
Pieter Wuille's avatar
Pieter Wuille committed
2900
2901
    } catch(std::runtime_error &e) {
        AbortNode(_("Error: system error: ") + e.what());
2902
    }
2903
    if (nLoaded > 0)
2904
        LogPrintf("Loaded %i blocks from external file in %"PRId64"ms\n", nLoaded, GetTimeMillis() - nStart);
2905
2906
    return nLoaded > 0;
}
s_nakamoto's avatar
s_nakamoto committed
2907

2908

s_nakamoto's avatar
s_nakamoto committed
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926








//////////////////////////////////////////////////////////////////////////////
//
// CAlert
//

string GetWarnings(string strFor)
{
    int nPriority = 0;
    string strStatusBar;
    string strRPC;
2927

2928
    if (GetBoolArg("-testsafemode", false))
s_nakamoto's avatar
s_nakamoto committed
2929
2930
        strRPC = "test";

2931
2932
2933
    if (!CLIENT_VERSION_IS_RELEASE)
        strStatusBar = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");

s_nakamoto's avatar
s_nakamoto committed
2934
2935
2936
2937
2938
2939
2940
    // Misc warnings like out of disk space and clock is wrong
    if (strMiscWarning != "")
    {
        nPriority = 1000;
        strStatusBar = strMiscWarning;
    }

2941
    if (fLargeWorkForkFound)
s_nakamoto's avatar
s_nakamoto committed
2942
2943
    {
        nPriority = 2000;
2944
2945
2946
        strStatusBar = strRPC = _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
    }
    else if (fLargeWorkInvalidChainFound)
s_nakamoto's avatar
s_nakamoto committed
2947
2948
    {
        nPriority = 2000;
2949
        strStatusBar = strRPC = _("Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade.");
s_nakamoto's avatar
s_nakamoto committed
2950
2951
2952
2953
    }

    // Alerts
    {
2954
        LOCK(cs_mapAlerts);
2955
        BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
s_nakamoto's avatar
s_nakamoto committed
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
        {
            const CAlert& alert = item.second;
            if (alert.AppliesToMe() && alert.nPriority > nPriority)
            {
                nPriority = alert.nPriority;
                strStatusBar = alert.strStatusBar;
            }
        }
    }

    if (strFor == "statusbar")
        return strStatusBar;
    else if (strFor == "rpc")
        return strRPC;
2970
    assert(!"GetWarnings() : invalid parameter");
s_nakamoto's avatar
s_nakamoto committed
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
    return "error";
}








//////////////////////////////////////////////////////////////////////////////
//
// Messages
//


2987
bool static AlreadyHave(const CInv& inv)
s_nakamoto's avatar
s_nakamoto committed
2988
2989
2990
{
    switch (inv.type)
    {
Jeff Garzik's avatar
Jeff Garzik committed
2991
2992
    case MSG_TX:
        {
Pieter Wuille's avatar
Pieter Wuille committed
2993
            bool txInMap = false;
2994
            txInMap = mempool.exists(inv.hash);
Pieter Wuille's avatar
Pieter Wuille committed
2995
            return txInMap || mapOrphanTransactions.count(inv.hash) ||
2996
                pcoinsTip->HaveCoins(inv.hash);
Jeff Garzik's avatar
Jeff Garzik committed
2997
2998
2999
3000
        }
    case MSG_BLOCK:
        return mapBlockIndex.count(inv.hash) ||
               mapOrphanBlocks.count(inv.hash);
s_nakamoto's avatar
s_nakamoto committed
3001
3002
3003
3004
3005
3006
    }
    // Don't know what it is, just say we already got one
    return true;
}


Pieter Wuille's avatar
Pieter Wuille committed
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
void Misbehaving(NodeId pnode, int howmuch)
{
    if (howmuch == 0)
        return;

    CNodeState *state = State(pnode);
    if (state == NULL)
        return;

    state->nMisbehavior += howmuch;
    if (state->nMisbehavior >= GetArg("-banscore", 100))
    {
3019
        LogPrintf("Misbehaving: %s (%d -> %d) BAN THRESHOLD EXCEEDED\n", state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
Pieter Wuille's avatar
Pieter Wuille committed
3020
3021
        state->fShouldBan = true;
    } else
3022
        LogPrintf("Misbehaving: %s (%d -> %d)\n", state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
Pieter Wuille's avatar
Pieter Wuille committed
3023
}
s_nakamoto's avatar
s_nakamoto committed
3024

3025
3026
3027
3028
3029
3030
void static ProcessGetData(CNode* pfrom)
{
    std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();

    vector<CInv> vNotFound;

3031
3032
    LOCK(cs_main);

3033
3034
3035
3036
3037
3038
3039
    while (it != pfrom->vRecvGetData.end()) {
        // Don't bother if send buffer is too full to respond anyway
        if (pfrom->nSendSize >= SendBufferSize())
            break;

        const CInv &inv = *it;
        {
Gavin Andresen's avatar
Gavin Andresen committed
3040
            boost::this_thread::interruption_point();
3041
3042
3043
3044
3045
3046
3047
3048
3049
            it++;

            if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
            {
                // Send block from disk
                map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(inv.hash);
                if (mi != mapBlockIndex.end())
                {
                    CBlock block;
3050
                    ReadBlockFromDisk(block, (*mi).second);
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
                    if (inv.type == MSG_BLOCK)
                        pfrom->PushMessage("block", block);
                    else // MSG_FILTERED_BLOCK)
                    {
                        LOCK(pfrom->cs_filter);
                        if (pfrom->pfilter)
                        {
                            CMerkleBlock merkleBlock(block, *pfrom->pfilter);
                            pfrom->PushMessage("merkleblock", merkleBlock);
                            // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
                            // This avoids hurting performance by pointlessly requiring a round-trip
                            // Note that there is currently no way for a node to request any single transactions we didnt send here -
                            // they must either disconnect and retry or request the full block.
                            // Thus, the protocol spec specified allows for us to provide duplicate txn here,
                            // however we MUST always provide at least what the remote peer needs
                            typedef std::pair<unsigned int, uint256> PairType;
                            BOOST_FOREACH(PairType& pair, merkleBlock.vMatchedTxn)
                                if (!pfrom->setInventoryKnown.count(CInv(MSG_TX, pair.second)))
                                    pfrom->PushMessage("tx", block.vtx[pair.first]);
                        }
                        // else
                            // no response
                    }

                    // Trigger them to send a getblocks request for the next batch of inventory
                    if (inv.hash == pfrom->hashContinue)
                    {
                        // Bypass PushInventory, this must send even if redundant,
                        // and we want it right after the last block so they don't
                        // wait for other stuff first.
                        vector<CInv> vInv;
3082
                        vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
                        pfrom->PushMessage("inv", vInv);
                        pfrom->hashContinue = 0;
                    }
                }
            }
            else if (inv.IsKnownType())
            {
                // Send stream from relay memory
                bool pushed = false;
                {
                    LOCK(cs_mapRelay);
                    map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
                    if (mi != mapRelay.end()) {
                        pfrom->PushMessage(inv.GetCommand(), (*mi).second);
                        pushed = true;
                    }
                }
                if (!pushed && inv.type == MSG_TX) {
3101
3102
                    CTransaction tx;
                    if (mempool.lookup(inv.hash, tx)) {
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
                        CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
                        ss.reserve(1000);
                        ss << tx;
                        pfrom->PushMessage("tx", ss);
                        pushed = true;
                    }
                }
                if (!pushed) {
                    vNotFound.push_back(inv);
                }
            }

            // Track requests for our stuff.
3116
            g_signals.Inventory(inv.hash);
3117

3118
3119
            if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
                break;
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
        }
    }

    pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);

    if (!vNotFound.empty()) {
        // Let the peer know that we didn't find what it asked for, so it doesn't
        // have to wait around forever. Currently only SPV clients actually care
        // about this message: it's needed when they are recursively walking the
        // dependencies of relevant unconfirmed transactions. SPV clients want to
        // do that because they want to know about (and store and rebroadcast and
        // risk analyze) the dependencies of transactions relevant to them, without
        // having to download the entire memory pool.
        pfrom->PushMessage("notfound", vNotFound);
    }
}

Pieter Wuille's avatar
Pieter Wuille committed
3137
bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
s_nakamoto's avatar
s_nakamoto committed
3138
3139
{
    RandAddSeedPerfmon();
3140
    LogPrint("net", "received: %s (%"PRIszu" bytes)\n", strCommand, vRecv.size());
s_nakamoto's avatar
s_nakamoto committed
3141
3142
    if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
    {
3143
        LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
s_nakamoto's avatar
s_nakamoto committed
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
        return true;
    }





    if (strCommand == "version")
    {
        // Each connection can only send one version message
        if (pfrom->nVersion != 0)
3155
        {
Gavin Andresen's avatar
Gavin Andresen committed
3156
            pfrom->PushMessage("reject", strCommand, REJECT_DUPLICATE, string("Duplicate version message"));
Pieter Wuille's avatar
Pieter Wuille committed
3157
            Misbehaving(pfrom->GetId(), 1);
s_nakamoto's avatar
s_nakamoto committed
3158
            return false;
3159
        }
s_nakamoto's avatar
s_nakamoto committed
3160

3161
        int64_t nTime;
s_nakamoto's avatar
s_nakamoto committed
3162
3163
        CAddress addrMe;
        CAddress addrFrom;
3164
        uint64_t nNonce = 1;
s_nakamoto's avatar
s_nakamoto committed
3165
        vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
3166
        if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
Pieter Wuille's avatar
Pieter Wuille committed
3167
        {
3168
            // disconnect from peers older than this proto version
3169
            LogPrintf("partner %s using obsolete version %i; disconnecting\n", pfrom->addr.ToString(), pfrom->nVersion);
Gavin Andresen's avatar
Gavin Andresen committed
3170
3171
            pfrom->PushMessage("reject", strCommand, REJECT_OBSOLETE,
                               strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION));
Pieter Wuille's avatar
Pieter Wuille committed
3172
3173
3174
3175
            pfrom->fDisconnect = true;
            return false;
        }

s_nakamoto's avatar
s_nakamoto committed
3176
3177
        if (pfrom->nVersion == 10300)
            pfrom->nVersion = 300;
Pieter Wuille's avatar
Pieter Wuille committed
3178
        if (!vRecv.empty())
s_nakamoto's avatar
s_nakamoto committed
3179
            vRecv >> addrFrom >> nNonce;
Mike Hearn's avatar
Mike Hearn committed
3180
        if (!vRecv.empty()) {
s_nakamoto's avatar
s_nakamoto committed
3181
            vRecv >> pfrom->strSubVer;
Mike Hearn's avatar
Mike Hearn committed
3182
3183
            pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer);
        }
Pieter Wuille's avatar
Pieter Wuille committed
3184
        if (!vRecv.empty())
s_nakamoto's avatar
s_nakamoto committed
3185
            vRecv >> pfrom->nStartingHeight;
3186
3187
3188
3189
        if (!vRecv.empty())
            vRecv >> pfrom->fRelayTxes; // set to true after we get the first filter* message
        else
            pfrom->fRelayTxes = true;
s_nakamoto's avatar
s_nakamoto committed
3190

3191
3192
3193
3194
3195
3196
        if (pfrom->fInbound && addrMe.IsRoutable())
        {
            pfrom->addrLocal = addrMe;
            SeenLocal(addrMe);
        }

s_nakamoto's avatar
s_nakamoto committed
3197
3198
3199
        // Disconnect if we connected to ourself
        if (nNonce == nLocalHostNonce && nNonce > 1)
        {
3200
            LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
s_nakamoto's avatar
s_nakamoto committed
3201
3202
3203
3204
            pfrom->fDisconnect = true;
            return true;
        }

Gavin Andresen's avatar
Gavin Andresen committed
3205
3206
3207
3208
        // Be shy and don't send version until we hear
        if (pfrom->fInbound)
            pfrom->PushVersion();

s_nakamoto's avatar
s_nakamoto committed
3209
3210
3211
3212
        pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);


        // Change version
Pieter Wuille's avatar
Pieter Wuille committed
3213
        pfrom->PushMessage("verack");
3214
        pfrom->ssSend.SetVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
s_nakamoto's avatar
s_nakamoto committed
3215

s_nakamoto's avatar
s_nakamoto committed
3216
3217
3218
        if (!pfrom->fInbound)
        {
            // Advertise our address
Pieter Wuille's avatar
Pieter Wuille committed
3219
            if (!fNoListen && !IsInitialBlockDownload())
s_nakamoto's avatar
s_nakamoto committed
3220
            {
3221
3222
3223
                CAddress addr = GetLocalAddress(&pfrom->addr);
                if (addr.IsRoutable())
                    pfrom->PushAddress(addr);
s_nakamoto's avatar
s_nakamoto committed
3224
3225
3226
            }

            // Get recent addresses
3227
            if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
s_nakamoto's avatar
s_nakamoto committed
3228
3229
3230
3231
            {
                pfrom->PushMessage("getaddr");
                pfrom->fGetAddr = true;
            }
3232
3233
3234
3235
3236
3237
3238
            addrman.Good(pfrom->addr);
        } else {
            if (((CNetAddr)pfrom->addr) == (CNetAddr)addrFrom)
            {
                addrman.Add(addrFrom, addrFrom);
                addrman.Good(addrFrom);
            }
s_nakamoto's avatar
s_nakamoto committed
3239
3240
        }

s_nakamoto's avatar
s_nakamoto committed
3241
        // Relay alerts
3242
3243
        {
            LOCK(cs_mapAlerts);
3244
            BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
s_nakamoto's avatar
s_nakamoto committed
3245
                item.second.RelayTo(pfrom);
3246
        }
s_nakamoto's avatar
s_nakamoto committed
3247
3248
3249

        pfrom->fSuccessfullyConnected = true;

3250
        LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", pfrom->cleanSubVer, pfrom->nVersion, pfrom->nStartingHeight, addrMe.ToString(), addrFrom.ToString(), pfrom->addr.ToString());
3251

3252
        AddTimeData(pfrom->addr, nTime);
3253
3254

        LOCK(cs_main);
3255
        cPeerBlockCounts.input(pfrom->nStartingHeight);
s_nakamoto's avatar
s_nakamoto committed
3256
3257
3258
3259
3260
3261
    }


    else if (pfrom->nVersion == 0)
    {
        // Must have a version message before anything else
Pieter Wuille's avatar
Pieter Wuille committed
3262
        Misbehaving(pfrom->GetId(), 1);
s_nakamoto's avatar
s_nakamoto committed
3263
3264
3265
3266
3267
3268
        return false;
    }


    else if (strCommand == "verack")
    {
3269
        pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
s_nakamoto's avatar
s_nakamoto committed
3270
3271
3272
3273
3274
3275
3276
    }


    else if (strCommand == "addr")
    {
        vector<CAddress> vAddr;
        vRecv >> vAddr;
s_nakamoto's avatar
s_nakamoto committed
3277
3278

        // Don't want addr from older versions unless seeding
3279
        if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
s_nakamoto's avatar
s_nakamoto committed
3280
3281
            return true;
        if (vAddr.size() > 1000)
3282
        {
Pieter Wuille's avatar
Pieter Wuille committed
3283
            Misbehaving(pfrom->GetId(), 20);
3284
            return error("message addr size() = %"PRIszu"", vAddr.size());
3285
        }
s_nakamoto's avatar
s_nakamoto committed
3286
3287

        // Store the new addresses
3288
        vector<CAddress> vAddrOk;
3289
3290
        int64_t nNow = GetAdjustedTime();
        int64_t nSince = nNow - 10 * 60;
3291
        BOOST_FOREACH(CAddress& addr, vAddr)
s_nakamoto's avatar
s_nakamoto committed
3292
        {
Gavin Andresen's avatar
Gavin Andresen committed
3293
3294
            boost::this_thread::interruption_point();

s_nakamoto's avatar
s_nakamoto committed
3295
3296
            if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
                addr.nTime = nNow - 5 * 24 * 60 * 60;
s_nakamoto's avatar
s_nakamoto committed
3297
            pfrom->AddAddressKnown(addr);
3298
            bool fReachable = IsReachable(addr);
s_nakamoto's avatar
s_nakamoto committed
3299
            if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
s_nakamoto's avatar
s_nakamoto committed
3300
3301
3302
            {
                // Relay to a limited number of other nodes
                {
3303
                    LOCK(cs_vNodes);
3304
3305
                    // Use deterministic randomness to send to the same nodes for 24 hours
                    // at a time so the setAddrKnowns of the chosen nodes prevent repeats
s_nakamoto's avatar
s_nakamoto committed
3306
3307
                    static uint256 hashSalt;
                    if (hashSalt == 0)
3308
                        hashSalt = GetRandHash();
3309
                    uint64_t hashAddr = addr.GetHash();
Pieter Wuille's avatar
Pieter Wuille committed
3310
                    uint256 hashRand = hashSalt ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60));
3311
                    hashRand = Hash(BEGIN(hashRand), END(hashRand));
s_nakamoto's avatar
s_nakamoto committed
3312
                    multimap<uint256, CNode*> mapMix;
3313
                    BOOST_FOREACH(CNode* pnode, vNodes)
3314
                    {
3315
                        if (pnode->nVersion < CADDR_TIME_VERSION)
s_nakamoto's avatar
s_nakamoto committed
3316
                            continue;
3317
3318
3319
3320
3321
3322
                        unsigned int nPointer;
                        memcpy(&nPointer, &pnode, sizeof(nPointer));
                        uint256 hashKey = hashRand ^ nPointer;
                        hashKey = Hash(BEGIN(hashKey), END(hashKey));
                        mapMix.insert(make_pair(hashKey, pnode));
                    }
3323
                    int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
s_nakamoto's avatar
s_nakamoto committed
3324
3325
3326
3327
                    for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
                        ((*mi).second)->PushAddress(addr);
                }
            }
3328
3329
3330
            // Do not store addresses outside our network
            if (fReachable)
                vAddrOk.push_back(addr);
s_nakamoto's avatar
s_nakamoto committed
3331
        }
3332
        addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
s_nakamoto's avatar
s_nakamoto committed
3333
3334
        if (vAddr.size() < 1000)
            pfrom->fGetAddr = false;
3335
3336
        if (pfrom->fOneShot)
            pfrom->fDisconnect = true;
s_nakamoto's avatar
s_nakamoto committed
3337
3338
3339
3340
3341
3342
3343
    }


    else if (strCommand == "inv")
    {
        vector<CInv> vInv;
        vRecv >> vInv;
3344
        if (vInv.size() > MAX_INV_SZ)
3345
        {
Pieter Wuille's avatar
Pieter Wuille committed
3346
            Misbehaving(pfrom->GetId(), 20);
3347
            return error("message inv size() = %"PRIszu"", vInv.size());
3348
        }
s_nakamoto's avatar
s_nakamoto committed
3349

3350
3351
3352
        // find last block in inv vector
        unsigned int nLastBlock = (unsigned int)(-1);
        for (unsigned int nInv = 0; nInv < vInv.size(); nInv++) {
3353
            if (vInv[vInv.size() - 1 - nInv].type == MSG_BLOCK) {
3354
                nLastBlock = vInv.size() - 1 - nInv;
3355
3356
                break;
            }
3357
        }
3358
3359
3360

        LOCK(cs_main);

3361
        for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
s_nakamoto's avatar
s_nakamoto committed
3362
        {
3363
3364
            const CInv &inv = vInv[nInv];

Gavin Andresen's avatar
Gavin Andresen committed
3365
            boost::this_thread::interruption_point();
s_nakamoto's avatar
s_nakamoto committed
3366
3367
            pfrom->AddInventoryKnown(inv);

3368
            bool fAlreadyHave = AlreadyHave(inv);
3369
            LogPrint("net", "  got inventory: %s  %s\n", inv.ToString(), fAlreadyHave ? "have" : "new");
s_nakamoto's avatar
s_nakamoto committed
3370

3371
            if (!fAlreadyHave) {
3372
                if (!fImporting && !fReindex)
3373
3374
                    pfrom->AskFor(inv);
            } else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash)) {
3375
                PushGetBlocks(pfrom, chainActive.Tip(), GetOrphanRoot(inv.hash));
3376
3377
3378
3379
            } else if (nInv == nLastBlock) {
                // In case we are on a very long side-chain, it is possible that we already have
                // the last block in an inv bundle sent in response to getblocks. Try to detect
                // this situation and push another getblocks to continue.
3380
                PushGetBlocks(pfrom, mapBlockIndex[inv.hash], uint256(0));
3381
                if (fDebug)
3382
                    LogPrintf("force request: %s\n", inv.ToString());
3383
            }
s_nakamoto's avatar
s_nakamoto committed
3384
3385

            // Track requests for our stuff
3386
            g_signals.Inventory(inv.hash);
s_nakamoto's avatar
s_nakamoto committed
3387
3388
3389
3390
3391
3392
3393
3394
        }
    }


    else if (strCommand == "getdata")
    {
        vector<CInv> vInv;
        vRecv >> vInv;
3395
        if (vInv.size() > MAX_INV_SZ)
3396
        {
Pieter Wuille's avatar
Pieter Wuille committed
3397
            Misbehaving(pfrom->GetId(), 20);
3398
            return error("message getdata size() = %"PRIszu"", vInv.size());
3399
        }
s_nakamoto's avatar
s_nakamoto committed
3400

3401
        if (fDebug || (vInv.size() != 1))
3402
            LogPrint("net", "received getdata (%"PRIszu" invsz)\n", vInv.size());
3403

3404
        if ((fDebug && vInv.size() > 0) || (vInv.size() == 1))
3405
            LogPrint("net", "received getdata for: %s\n", vInv[0].ToString());
s_nakamoto's avatar
s_nakamoto committed
3406

3407
3408
        pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
        ProcessGetData(pfrom);
s_nakamoto's avatar
s_nakamoto committed
3409
3410
3411
3412
3413
3414
3415
3416
3417
    }


    else if (strCommand == "getblocks")
    {
        CBlockLocator locator;
        uint256 hashStop;
        vRecv >> locator >> hashStop;

3418
3419
        LOCK(cs_main);

3420
        // Find the last block the caller has in the main chain
3421
        CBlockIndex* pindex = chainActive.FindFork(locator);
s_nakamoto's avatar
s_nakamoto committed
3422
3423
3424

        // Send the rest of the chain
        if (pindex)
3425
            pindex = chainActive.Next(pindex);
3426
        int nLimit = 500;
3427
        LogPrint("net", "getblocks %d to %s limit %d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString(), nLimit);
3428
        for (; pindex; pindex = chainActive.Next(pindex))
s_nakamoto's avatar
s_nakamoto committed
3429
3430
3431
        {
            if (pindex->GetBlockHash() == hashStop)
            {
3432
                LogPrint("net", "  getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
s_nakamoto's avatar
s_nakamoto committed
3433
3434
3435
                break;
            }
            pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
3436
            if (--nLimit <= 0)
s_nakamoto's avatar
s_nakamoto committed
3437
3438
3439
            {
                // When this block is requested, we'll send an inv that'll make them
                // getblocks the next batch of inventory.
3440
                LogPrint("net", "  getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
s_nakamoto's avatar
s_nakamoto committed
3441
3442
3443
3444
3445
3446
3447
                pfrom->hashContinue = pindex->GetBlockHash();
                break;
            }
        }
    }


3448
3449
3450
3451
3452
3453
    else if (strCommand == "getheaders")
    {
        CBlockLocator locator;
        uint256 hashStop;
        vRecv >> locator >> hashStop;

3454
3455
        LOCK(cs_main);

3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
        CBlockIndex* pindex = NULL;
        if (locator.IsNull())
        {
            // If locator is null, return the hashStop block
            map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashStop);
            if (mi == mapBlockIndex.end())
                return true;
            pindex = (*mi).second;
        }
        else
        {
            // Find the last block the caller has in the main chain
3468
            pindex = chainActive.FindFork(locator);
3469
            if (pindex)
3470
                pindex = chainActive.Next(pindex);
3471
3472
        }

3473
        // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
3474
        vector<CBlock> vHeaders;
3475
        int nLimit = 2000;
3476
        LogPrint("net", "getheaders %d to %s\n", (pindex ? pindex->nHeight : -1), hashStop.ToString());
3477
        for (; pindex; pindex = chainActive.Next(pindex))
3478
3479
3480
3481
3482
3483
3484
3485
3486
        {
            vHeaders.push_back(pindex->GetBlockHeader());
            if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
                break;
        }
        pfrom->PushMessage("headers", vHeaders);
    }


s_nakamoto's avatar
s_nakamoto committed
3487
3488
3489
    else if (strCommand == "tx")
    {
        vector<uint256> vWorkQueue;
3490
        vector<uint256> vEraseQueue;
s_nakamoto's avatar
s_nakamoto committed
3491
3492
3493
3494
3495
3496
        CTransaction tx;
        vRecv >> tx;

        CInv inv(MSG_TX, tx.GetHash());
        pfrom->AddInventoryKnown(inv);

3497
3498
        LOCK(cs_main);

s_nakamoto's avatar
s_nakamoto committed
3499
        bool fMissingInputs = false;
Pieter Wuille's avatar
Pieter Wuille committed
3500
        CValidationState state;
3501
        if (AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs))
s_nakamoto's avatar
s_nakamoto committed
3502
        {
3503
            mempool.check(pcoinsTip);
3504
            RelayTransaction(tx, inv.hash);
s_nakamoto's avatar
s_nakamoto committed
3505
3506
            mapAlreadyAskedFor.erase(inv);
            vWorkQueue.push_back(inv.hash);
3507
            vEraseQueue.push_back(inv.hash);
s_nakamoto's avatar
s_nakamoto committed
3508

3509
3510

            LogPrint("mempool", "AcceptToMemoryPool: %s %s : accepted %s (poolsz %"PRIszu")\n",
3511
3512
                pfrom->addr.ToString(), pfrom->cleanSubVer,
                tx.GetHash().ToString(),
3513
3514
                mempool.mapTx.size());

s_nakamoto's avatar
s_nakamoto committed
3515
            // Recursively process any orphan transactions that depended on this one
3516
            for (unsigned int i = 0; i < vWorkQueue.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
3517
3518
            {
                uint256 hashPrev = vWorkQueue[i];
3519
                for (set<uint256>::iterator mi = mapOrphanTransactionsByPrev[hashPrev].begin();
3520
                     mi != mapOrphanTransactionsByPrev[hashPrev].end();
s_nakamoto's avatar
s_nakamoto committed
3521
3522
                     ++mi)
                {
3523
3524
                    const uint256& orphanHash = *mi;
                    const CTransaction& orphanTx = mapOrphanTransactions[orphanHash];
3525
                    bool fMissingInputs2 = false;
3526
3527
3528
                    // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
                    // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
                    // anyone relaying LegitTxX banned)
3529
                    CValidationState stateDummy;
s_nakamoto's avatar
s_nakamoto committed
3530

3531
                    if (AcceptToMemoryPool(mempool, stateDummy, orphanTx, true, &fMissingInputs2))
s_nakamoto's avatar
s_nakamoto committed
3532
                    {
3533
                        LogPrint("mempool", "   accepted orphan tx %s\n", orphanHash.ToString());
3534
3535
3536
3537
                        RelayTransaction(orphanTx, orphanHash);
                        mapAlreadyAskedFor.erase(CInv(MSG_TX, orphanHash));
                        vWorkQueue.push_back(orphanHash);
                        vEraseQueue.push_back(orphanHash);
3538
3539
3540
                    }
                    else if (!fMissingInputs2)
                    {
3541
                        // invalid or too-little-fee orphan
3542
                        vEraseQueue.push_back(orphanHash);
3543
                        LogPrint("mempool", "   removed orphan tx %s\n", orphanHash.ToString());
s_nakamoto's avatar
s_nakamoto committed
3544
                    }
3545
                    mempool.check(pcoinsTip);
s_nakamoto's avatar
s_nakamoto committed
3546
3547
3548
                }
            }

3549
            BOOST_FOREACH(uint256 hash, vEraseQueue)
s_nakamoto's avatar
s_nakamoto committed
3550
3551
3552
3553
                EraseOrphanTx(hash);
        }
        else if (fMissingInputs)
        {
3554
            AddOrphanTx(tx);
3555
3556

            // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
3557
            unsigned int nEvicted = LimitOrphanTxSize(MAX_ORPHAN_TRANSACTIONS);
3558
            if (nEvicted > 0)
3559
                LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", nEvicted);
s_nakamoto's avatar
s_nakamoto committed
3560
        }
3561
        int nDoS = 0;
3562
        if (state.IsInvalid(nDoS))
3563
        { 
3564
3565
3566
            LogPrint("mempool", "%s from %s %s was not accepted into the memory pool: %s\n", tx.GetHash().ToString(),
                pfrom->addr.ToString(), pfrom->cleanSubVer,
                state.GetRejectReason());
Gavin Andresen's avatar
Gavin Andresen committed
3567
3568
            pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
                               state.GetRejectReason(), inv.hash);
3569
            if (nDoS > 0)
Pieter Wuille's avatar
Pieter Wuille committed
3570
                Misbehaving(pfrom->GetId(), nDoS);
Gavin Andresen's avatar
Gavin Andresen committed
3571
        }
s_nakamoto's avatar
s_nakamoto committed
3572
3573
3574
    }


3575
    else if (strCommand == "block" && !fImporting && !fReindex) // Ignore blocks received while importing
s_nakamoto's avatar
s_nakamoto committed
3576
    {
3577
3578
        CBlock block;
        vRecv >> block;
s_nakamoto's avatar
s_nakamoto committed
3579

3580
        LogPrint("net", "received block %s\n", block.GetHash().ToString());
3581
        // block.print();
s_nakamoto's avatar
s_nakamoto committed
3582

3583
        CInv inv(MSG_BLOCK, block.GetHash());
s_nakamoto's avatar
s_nakamoto committed
3584
3585
        pfrom->AddInventoryKnown(inv);

3586
3587
        LOCK(cs_main);

Pieter Wuille's avatar
Pieter Wuille committed
3588
        CValidationState state;
3589
        if (ProcessBlock(state, pfrom, &block) || state.CorruptionPossible())
s_nakamoto's avatar
s_nakamoto committed
3590
            mapAlreadyAskedFor.erase(inv);
3591
        int nDoS = 0;
3592
        if (state.IsInvalid(nDoS))
Gavin Andresen's avatar
Gavin Andresen committed
3593
3594
3595
        {
            pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
                               state.GetRejectReason(), inv.hash);
3596
            if (nDoS > 0)
Pieter Wuille's avatar
Pieter Wuille committed
3597
                Misbehaving(pfrom->GetId(), nDoS);
Gavin Andresen's avatar
Gavin Andresen committed
3598
        }
s_nakamoto's avatar
s_nakamoto committed
3599
3600
3601
3602
3603
3604
    }


    else if (strCommand == "getaddr")
    {
        pfrom->vAddrToSend.clear();
3605
3606
3607
        vector<CAddress> vAddr = addrman.GetAddr();
        BOOST_FOREACH(const CAddress &addr, vAddr)
            pfrom->PushAddress(addr);
s_nakamoto's avatar
s_nakamoto committed
3608
3609
3610
    }


3611
3612
    else if (strCommand == "mempool")
    {
3613
        LOCK2(cs_main, pfrom->cs_filter);
3614

3615
3616
3617
        std::vector<uint256> vtxid;
        mempool.queryHashes(vtxid);
        vector<CInv> vInv;
Matt Corallo's avatar
Matt Corallo committed
3618
3619
        BOOST_FOREACH(uint256& hash, vtxid) {
            CInv inv(MSG_TX, hash);
3620
3621
3622
3623
            CTransaction tx;
            bool fInMemPool = mempool.lookup(hash, tx);
            if (!fInMemPool) continue; // another thread removed since queryHashes, maybe...
            if ((pfrom->pfilter && pfrom->pfilter->IsRelevantAndUpdate(tx, hash)) ||
Matt Corallo's avatar
Matt Corallo committed
3624
3625
               (!pfrom->pfilter))
                vInv.push_back(inv);
3626
3627
3628
3629
            if (vInv.size() == MAX_INV_SZ) {
                pfrom->PushMessage("inv", vInv);
                vInv.clear();
            }
3630
3631
3632
3633
3634
3635
        }
        if (vInv.size() > 0)
            pfrom->PushMessage("inv", vInv);
    }


s_nakamoto's avatar
s_nakamoto committed
3636
3637
    else if (strCommand == "ping")
    {
Jeff Garzik's avatar
Jeff Garzik committed
3638
3639
        if (pfrom->nVersion > BIP0031_VERSION)
        {
3640
            uint64_t nonce = 0;
Jeff Garzik's avatar
Jeff Garzik committed
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
            vRecv >> nonce;
            // Echo the message back with the nonce. This allows for two useful features:
            //
            // 1) A remote node can quickly check if the connection is operational
            // 2) Remote nodes can measure the latency of the network thread. If this node
            //    is overloaded it won't respond to pings quickly and the remote node can
            //    avoid sending us more work, like chain download requests.
            //
            // The nonce stops the remote getting confused between different pings: without
            // it, if the remote node sends a ping once per second and this node takes 5
            // seconds to respond to each, the 5th ping the remote sends would appear to
            // return very quickly.
            pfrom->PushMessage("pong", nonce);
        }
s_nakamoto's avatar
s_nakamoto committed
3655
3656
3657
    }


Josh Lehan's avatar
Josh Lehan committed
3658
3659
    else if (strCommand == "pong")
    {
3660
3661
        int64_t pingUsecEnd = GetTimeMicros();
        uint64_t nonce = 0;
Josh Lehan's avatar
Josh Lehan committed
3662
3663
3664
        size_t nAvail = vRecv.in_avail();
        bool bPingFinished = false;
        std::string sProblem;
3665

Josh Lehan's avatar
Josh Lehan committed
3666
3667
        if (nAvail >= sizeof(nonce)) {
            vRecv >> nonce;
3668

Josh Lehan's avatar
Josh Lehan committed
3669
3670
3671
3672
3673
            // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
            if (pfrom->nPingNonceSent != 0) {
                if (nonce == pfrom->nPingNonceSent) {
                    // Matching pong received, this ping is no longer outstanding
                    bPingFinished = true;
3674
                    int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
Josh Lehan's avatar
Josh Lehan committed
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
                    if (pingUsecTime > 0) {
                        // Successful ping time measurement, replace previous
                        pfrom->nPingUsecTime = pingUsecTime;
                    } else {
                        // This should never happen
                        sProblem = "Timing mishap";
                    }
                } else {
                    // Nonce mismatches are normal when pings are overlapping
                    sProblem = "Nonce mismatch";
                    if (nonce == 0) {
                        // This is most likely a bug in another implementation somewhere, cancel this ping
                        bPingFinished = true;
                        sProblem = "Nonce zero";
                    }
                }
            } else {
                sProblem = "Unsolicited pong without ping";
            }
        } else {
            // This is most likely a bug in another implementation somewhere, cancel this ping
            bPingFinished = true;
            sProblem = "Short payload";
        }
3699

Josh Lehan's avatar
Josh Lehan committed
3700
        if (!(sProblem.empty())) {
3701
            LogPrint("net", "pong %s %s: %s, %"PRIx64" expected, %"PRIx64" received, %"PRIszu" bytes\n",
3702
3703
3704
                pfrom->addr.ToString(),
                pfrom->cleanSubVer,
                sProblem,
3705
3706
3707
                pfrom->nPingNonceSent,
                nonce,
                nAvail);
Josh Lehan's avatar
Josh Lehan committed
3708
3709
3710
3711
3712
        }
        if (bPingFinished) {
            pfrom->nPingNonceSent = 0;
        }
    }
3713
3714


s_nakamoto's avatar
s_nakamoto committed
3715
3716
3717
3718
3719
    else if (strCommand == "alert")
    {
        CAlert alert;
        vRecv >> alert;

Gavin Andresen's avatar
Gavin Andresen committed
3720
3721
        uint256 alertHash = alert.GetHash();
        if (pfrom->setKnown.count(alertHash) == 0)
s_nakamoto's avatar
s_nakamoto committed
3722
        {
Gavin Andresen's avatar
Gavin Andresen committed
3723
            if (alert.ProcessAlert())
3724
            {
Gavin Andresen's avatar
Gavin Andresen committed
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
                // Relay
                pfrom->setKnown.insert(alertHash);
                {
                    LOCK(cs_vNodes);
                    BOOST_FOREACH(CNode* pnode, vNodes)
                        alert.RelayTo(pnode);
                }
            }
            else {
                // Small DoS penalty so peers that send us lots of
                // duplicate/expired/invalid-signature/whatever alerts
                // eventually get banned.
                // This isn't a Misbehaving(100) (immediate ban) because the
                // peer might be an older or different implementation with
                // a different signature key, etc.
Pieter Wuille's avatar
Pieter Wuille committed
3740
                Misbehaving(pfrom->GetId(), 10);
3741
            }
s_nakamoto's avatar
s_nakamoto committed
3742
3743
3744
3745
        }
    }


3746
3747
3748
3749
3750
3751
3752
    else if (strCommand == "filterload")
    {
        CBloomFilter filter;
        vRecv >> filter;

        if (!filter.IsWithinSizeConstraints())
            // There is no excuse for sending a too-large filter
Pieter Wuille's avatar
Pieter Wuille committed
3753
            Misbehaving(pfrom->GetId(), 100);
3754
3755
3756
3757
3758
        else
        {
            LOCK(pfrom->cs_filter);
            delete pfrom->pfilter;
            pfrom->pfilter = new CBloomFilter(filter);
3759
            pfrom->pfilter->UpdateEmptyFull();
3760
        }
3761
        pfrom->fRelayTxes = true;
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
    }


    else if (strCommand == "filteradd")
    {
        vector<unsigned char> vData;
        vRecv >> vData;

        // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
        // and thus, the maximum size any matched object can have) in a filteradd message
3772
        if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE)
3773
        {
Pieter Wuille's avatar
Pieter Wuille committed
3774
            Misbehaving(pfrom->GetId(), 100);
3775
3776
3777
3778
3779
        } else {
            LOCK(pfrom->cs_filter);
            if (pfrom->pfilter)
                pfrom->pfilter->insert(vData);
            else
Pieter Wuille's avatar
Pieter Wuille committed
3780
                Misbehaving(pfrom->GetId(), 100);
3781
3782
3783
3784
3785
3786
3787
3788
        }
    }


    else if (strCommand == "filterclear")
    {
        LOCK(pfrom->cs_filter);
        delete pfrom->pfilter;
3789
        pfrom->pfilter = new CBloomFilter();
3790
        pfrom->fRelayTxes = true;
3791
3792
3793
    }


Gavin Andresen's avatar
Gavin Andresen committed
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
    else if (strCommand == "reject")
    {
        if (fDebug)
        {
            string strMsg; unsigned char ccode; string strReason;
            vRecv >> strMsg >> ccode >> strReason;

            ostringstream ss;
            ss << strMsg << " code " << itostr(ccode) << ": " << strReason;

            if (strMsg == "block" || strMsg == "tx")
            {
                uint256 hash;
                vRecv >> hash;
                ss << ": hash " << hash.ToString();
            }
            // Truncate to reasonable length and sanitize before printing:
            string s = ss.str();
            if (s.size() > 111) s.erase(111, string::npos);
3813
            LogPrint("net", "Reject %s\n", SanitizeString(s));
Gavin Andresen's avatar
Gavin Andresen committed
3814
3815
3816
        }
    }

s_nakamoto's avatar
s_nakamoto committed
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
    else
    {
        // Ignore unknown commands for extensibility
    }


    // Update the last seen time for this node's address
    if (pfrom->fNetworkNode)
        if (strCommand == "version" || strCommand == "addr" || strCommand == "inv" || strCommand == "getdata" || strCommand == "ping")
            AddressCurrentlyConnected(pfrom->addr);


    return true;
}

3832
// requires LOCK(cs_vRecvMsg)
3833
3834
3835
bool ProcessMessages(CNode* pfrom)
{
    //if (fDebug)
3836
    //    LogPrintf("ProcessMessages(%"PRIszu" messages)\n", pfrom->vRecvMsg.size());
s_nakamoto's avatar
s_nakamoto committed
3837

3838
3839
3840
3841
3842
3843
3844
3845
    //
    // Message format
    //  (4) message start
    //  (12) command
    //  (4) size
    //  (4) checksum
    //  (x) data
    //
3846
    bool fOk = true;
s_nakamoto's avatar
s_nakamoto committed
3847

3848
3849
    if (!pfrom->vRecvGetData.empty())
        ProcessGetData(pfrom);
3850

3851
3852
    // this maintains the order of responses
    if (!pfrom->vRecvGetData.empty()) return fOk;
3853

3854
    std::deque<CNetMessage>::iterator it = pfrom->vRecvMsg.begin();
3855
    while (!pfrom->fDisconnect && it != pfrom->vRecvMsg.end()) {
3856
        // Don't bother if send buffer is too full to respond anyway
3857
        if (pfrom->nSendSize >= SendBufferSize())
3858
3859
            break;

3860
3861
        // get next message
        CNetMessage& msg = *it;
3862
3863

        //if (fDebug)
3864
        //    LogPrintf("ProcessMessages(message %u msgsz, %"PRIszu" bytes, complete:%s)\n",
3865
3866
3867
        //            msg.hdr.nMessageSize, msg.vRecv.size(),
        //            msg.complete() ? "Y" : "N");

3868
        // end, if an incomplete message is found
3869
        if (!msg.complete())
3870
            break;
3871

3872
3873
3874
        // at this point, any failure means we can delete the current message
        it++;

3875
        // Scan for message start
3876
        if (memcmp(msg.hdr.pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0) {
3877
            LogPrintf("\n\nPROCESSMESSAGE: INVALID MESSAGESTART\n\n");
3878
3879
            fOk = false;
            break;
3880
        }
s_nakamoto's avatar
s_nakamoto committed
3881

3882
        // Read header
3883
        CMessageHeader& hdr = msg.hdr;
3884
3885
        if (!hdr.IsValid())
        {
3886
            LogPrintf("\n\nPROCESSMESSAGE: ERRORS IN HEADER %s\n\n\n", hdr.GetCommand());
3887
3888
3889
3890
3891
3892
3893
3894
            continue;
        }
        string strCommand = hdr.GetCommand();

        // Message size
        unsigned int nMessageSize = hdr.nMessageSize;

        // Checksum
3895
        CDataStream& vRecv = msg.vRecv;
Pieter Wuille's avatar
Pieter Wuille committed
3896
3897
3898
3899
        uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
        unsigned int nChecksum = 0;
        memcpy(&nChecksum, &hash, sizeof(nChecksum));
        if (nChecksum != hdr.nChecksum)
3900
        {
3901
            LogPrintf("ProcessMessages(%s, %u bytes) : CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n",
3902
               strCommand, nMessageSize, nChecksum, hdr.nChecksum);
Pieter Wuille's avatar
Pieter Wuille committed
3903
            continue;
3904
3905
3906
3907
3908
3909
        }

        // Process message
        bool fRet = false;
        try
        {
3910
            fRet = ProcessMessage(pfrom, strCommand, vRecv);
Gavin Andresen's avatar
Gavin Andresen committed
3911
            boost::this_thread::interruption_point();
3912
3913
3914
        }
        catch (std::ios_base::failure& e)
        {
Gavin Andresen's avatar
Gavin Andresen committed
3915
            pfrom->PushMessage("reject", strCommand, REJECT_MALFORMED, string("error parsing message"));
3916
3917
            if (strstr(e.what(), "end of data"))
            {
3918
                // Allow exceptions from under-length message on vRecv
3919
                LogPrintf("ProcessMessages(%s, %u bytes) : Exception '%s' caught, normally caused by a message being shorter than its stated length\n", strCommand, nMessageSize, e.what());
3920
3921
3922
            }
            else if (strstr(e.what(), "size too large"))
            {
3923
                // Allow exceptions from over-long size
3924
                LogPrintf("ProcessMessages(%s, %u bytes) : Exception '%s' caught\n", strCommand, nMessageSize, e.what());
3925
3926
3927
            }
            else
            {
3928
                PrintExceptionContinue(&e, "ProcessMessages()");
3929
3930
            }
        }
Gavin Andresen's avatar
Gavin Andresen committed
3931
3932
3933
        catch (boost::thread_interrupted) {
            throw;
        }
3934
        catch (std::exception& e) {
3935
            PrintExceptionContinue(&e, "ProcessMessages()");
3936
        } catch (...) {
3937
            PrintExceptionContinue(NULL, "ProcessMessages()");
3938
3939
3940
        }

        if (!fRet)
3941
            LogPrintf("ProcessMessage(%s, %u bytes) FAILED\n", strCommand, nMessageSize);
3942

3943
        break;
3944
3945
    }

3946
3947
3948
3949
    // In case the connection got shut down, its receive buffer was wiped
    if (!pfrom->fDisconnect)
        pfrom->vRecvMsg.erase(pfrom->vRecvMsg.begin(), it);

3950
    return fOk;
3951
}
s_nakamoto's avatar
s_nakamoto committed
3952
3953
3954
3955


bool SendMessages(CNode* pto, bool fSendTrickle)
{
3956
    {
s_nakamoto's avatar
s_nakamoto committed
3957
3958
3959
3960
        // Don't send anything until we get their version message
        if (pto->nVersion == 0)
            return true;

Josh Lehan's avatar
Josh Lehan committed
3961
3962
3963
3964
3965
3966
3967
3968
        //
        // Message: ping
        //
        bool pingSend = false;
        if (pto->fPingQueued) {
            // RPC ping request by user
            pingSend = true;
        }
3969
        if (pto->nLastSend && GetTime() - pto->nLastSend > 30 * 60 && pto->vSendMsg.empty()) {
Josh Lehan's avatar
Josh Lehan committed
3970
3971
3972
3973
            // Ping automatically sent as a keepalive
            pingSend = true;
        }
        if (pingSend) {
3974
            uint64_t nonce = 0;
Josh Lehan's avatar
Josh Lehan committed
3975
3976
3977
3978
3979
3980
3981
3982
            while (nonce == 0) {
                RAND_bytes((unsigned char*)&nonce, sizeof(nonce));
            }
            pto->nPingNonceSent = nonce;
            pto->fPingQueued = false;
            if (pto->nVersion > BIP0031_VERSION) {
                // Take timestamp as close as possible before transmitting ping
                pto->nPingUsecStart = GetTimeMicros();
Pieter Wuille's avatar
Pieter Wuille committed
3983
                pto->PushMessage("ping", nonce);
Josh Lehan's avatar
Josh Lehan committed
3984
3985
3986
            } else {
                // Peer is too old to support ping command with nonce, pong will never arrive, disable timing
                pto->nPingUsecStart = 0;
Jeff Garzik's avatar
Jeff Garzik committed
3987
                pto->PushMessage("ping");
Josh Lehan's avatar
Josh Lehan committed
3988
            }
Jeff Garzik's avatar
Jeff Garzik committed
3989
        }
s_nakamoto's avatar
s_nakamoto committed
3990
3991

        // Address refresh broadcast
3992
        static int64_t nLastRebroadcast;
3993
        if (!IsInitialBlockDownload() && (GetTime() - nLastRebroadcast > 24 * 60 * 60))
s_nakamoto's avatar
s_nakamoto committed
3994
3995
        {
            {
3996
                LOCK(cs_vNodes);
3997
                BOOST_FOREACH(CNode* pnode, vNodes)
s_nakamoto's avatar
s_nakamoto committed
3998
3999
                {
                    // Periodically clear setAddrKnown to allow refresh broadcasts
4000
4001
                    if (nLastRebroadcast)
                        pnode->setAddrKnown.clear();
s_nakamoto's avatar
s_nakamoto committed
4002
4003

                    // Rebroadcast our address
Pieter Wuille's avatar
Pieter Wuille committed
4004
                    if (!fNoListen)
s_nakamoto's avatar
s_nakamoto committed
4005
                    {
4006
4007
4008
                        CAddress addr = GetLocalAddress(&pnode->addr);
                        if (addr.IsRoutable())
                            pnode->PushAddress(addr);
s_nakamoto's avatar
s_nakamoto committed
4009
                    }
s_nakamoto's avatar
s_nakamoto committed
4010
4011
                }
            }
4012
            nLastRebroadcast = GetTime();
s_nakamoto's avatar
s_nakamoto committed
4013
4014
4015
4016
4017
4018
4019
4020
4021
        }

        //
        // Message: addr
        //
        if (fSendTrickle)
        {
            vector<CAddress> vAddr;
            vAddr.reserve(pto->vAddrToSend.size());
4022
            BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
s_nakamoto's avatar
s_nakamoto committed
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
            {
                // returns true if wasn't already contained in the set
                if (pto->setAddrKnown.insert(addr).second)
                {
                    vAddr.push_back(addr);
                    // receiver rejects addr messages larger than 1000
                    if (vAddr.size() >= 1000)
                    {
                        pto->PushMessage("addr", vAddr);
                        vAddr.clear();
                    }
                }
            }
            pto->vAddrToSend.clear();
            if (!vAddr.empty())
                pto->PushMessage("addr", vAddr);
        }

4041
4042
4043
4044
        TRY_LOCK(cs_main, lockMain);
        if (!lockMain)
            return true;

Pieter Wuille's avatar
Pieter Wuille committed
4045
4046
        if (State(pto->GetId())->fShouldBan) {
            if (pto->addr.IsLocal())
4047
                LogPrintf("Warning: not banning local node %s!\n", pto->addr.ToString());
Pieter Wuille's avatar
Pieter Wuille committed
4048
4049
4050
4051
4052
4053
4054
            else {
                pto->fDisconnect = true;
                CNode::Ban(pto->addr);
            }
            State(pto->GetId())->fShouldBan = false;
        }

4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
        // Start block sync
        if (pto->fStartSync && !fImporting && !fReindex) {
            pto->fStartSync = false;
            PushGetBlocks(pto, chainActive.Tip(), uint256(0));
        }

        // Resend wallet transactions that haven't gotten in a block yet
        // Except during reindex, importing and IBD, when old wallet
        // transactions become unconfirmed and spams other nodes.
        if (!fReindex && !fImporting && !IsInitialBlockDownload())
        {
4066
            g_signals.Broadcast();
4067
        }
s_nakamoto's avatar
s_nakamoto committed
4068
4069
4070
4071
4072
4073
4074

        //
        // Message: inventory
        //
        vector<CInv> vInv;
        vector<CInv> vInvWait;
        {
4075
            LOCK(pto->cs_inventory);
s_nakamoto's avatar
s_nakamoto committed
4076
4077
            vInv.reserve(pto->vInventoryToSend.size());
            vInvWait.reserve(pto->vInventoryToSend.size());
4078
            BOOST_FOREACH(const CInv& inv, pto->vInventoryToSend)
s_nakamoto's avatar
s_nakamoto committed
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
            {
                if (pto->setInventoryKnown.count(inv))
                    continue;

                // trickle out tx inv to protect privacy
                if (inv.type == MSG_TX && !fSendTrickle)
                {
                    // 1/4 of tx invs blast to all immediately
                    static uint256 hashSalt;
                    if (hashSalt == 0)
4089
                        hashSalt = GetRandHash();
s_nakamoto's avatar
s_nakamoto committed
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
                    uint256 hashRand = inv.hash ^ hashSalt;
                    hashRand = Hash(BEGIN(hashRand), END(hashRand));
                    bool fTrickleWait = ((hashRand & 3) != 0);

                    if (fTrickleWait)
                    {
                        vInvWait.push_back(inv);
                        continue;
                    }
                }

                // returns true if wasn't already contained in the set
                if (pto->setInventoryKnown.insert(inv).second)
                {
                    vInv.push_back(inv);
                    if (vInv.size() >= 1000)
                    {
                        pto->PushMessage("inv", vInv);
                        vInv.clear();
                    }
                }
            }
            pto->vInventoryToSend = vInvWait;
        }
        if (!vInv.empty())
            pto->PushMessage("inv", vInv);


        //
        // Message: getdata
        //
        vector<CInv> vGetData;
4122
        int64_t nNow = GetTime() * 1000000;
s_nakamoto's avatar
s_nakamoto committed
4123
4124
4125
        while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
        {
            const CInv& inv = (*pto->mapAskFor.begin()).second;
4126
            if (!AlreadyHave(inv))
s_nakamoto's avatar
s_nakamoto committed
4127
            {
4128
                if (fDebug)
4129
                    LogPrint("net", "sending getdata: %s\n", inv.ToString());
s_nakamoto's avatar
s_nakamoto committed
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
                vGetData.push_back(inv);
                if (vGetData.size() >= 1000)
                {
                    pto->PushMessage("getdata", vGetData);
                    vGetData.clear();
                }
            }
            pto->mapAskFor.erase(pto->mapAskFor.begin());
        }
        if (!vGetData.empty())
            pto->PushMessage("getdata", vGetData);

    }
    return true;
}






4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
class CMainCleanup
{
public:
    CMainCleanup() {}
    ~CMainCleanup() {
        // block headers
        std::map<uint256, CBlockIndex*>::iterator it1 = mapBlockIndex.begin();
        for (; it1 != mapBlockIndex.end(); it1++)
            delete (*it1).second;
        mapBlockIndex.clear();

        // orphan blocks
4163
        std::map<uint256, COrphanBlock*>::iterator it2 = mapOrphanBlocks.begin();
4164
4165
4166
4167
4168
4169
4170
4171
        for (; it2 != mapOrphanBlocks.end(); it2++)
            delete (*it2).second;
        mapOrphanBlocks.clear();

        // orphan transactions
        mapOrphanTransactions.clear();
    }
} instance_of_cmaincleanup;