main.cpp 167 KB
Newer Older
s_nakamoto's avatar
s_nakamoto committed
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2014 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
#include "net.h"
15
#include "pow.h"
16
17
#include "txdb.h"
#include "txmempool.h"
Pieter Wuille's avatar
Pieter Wuille committed
18
#include "ui_interface.h"
19
20
#include "util.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
40
//
// Global state
//

CCriticalSection cs_main;

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

52
/** Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) */
Gavin Andresen's avatar
Gavin Andresen committed
53
54
55
CFeeRate minRelayTxFee = CFeeRate(1000);

CTxMemPool mempool(::minRelayTxFee);
Gavin Andresen's avatar
Gavin Andresen committed
56

57
58
59
60
61
62
63
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
64

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

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

71
72
const string strMessageMagic = "Bitcoin Signed Message:\n";

73
74
// Internal stuff
namespace {
75

R E Broadley's avatar
R E Broadley committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    struct CBlockIndexWorkComparator
    {
        bool operator()(CBlockIndex *pa, CBlockIndex *pb) {
            // First sort by most total work, ...
            if (pa->nChainWork > pb->nChainWork) return false;
            if (pa->nChainWork < pb->nChainWork) return true;

            // ... then by earliest time received, ...
            if (pa->nSequenceId < pb->nSequenceId) return false;
            if (pa->nSequenceId > pb->nSequenceId) return true;

            // Use pointer address as tie breaker (should only happen with blocks
            // loaded from disk, as those all have id 0).
            if (pa < pb) return false;
            if (pa > pb) return true;

            // Identical blocks.
            return false;
        }
    };

    CBlockIndex *pindexBestInvalid;
98
99
100

    // The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS or better that are at least
    // as good as our current tip. Entries may be failed, though.
R E Broadley's avatar
R E Broadley committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid;

    CCriticalSection cs_LastBlockFile;
    CBlockFileInfo infoLastBlockFile;
    int nLastBlockFile = 0;

    // Every received block is assigned a unique and increasing identifier, so we
    // know which one to give priority in case of a fork.
    CCriticalSection cs_nBlockSequenceId;
    // Blocks loaded from disk are assigned id 0, so start the counter at 1.
    uint32_t nBlockSequenceId = 1;

    // Sources of received blocks, to be able to send them reject messages or ban
    // them, if processing happens afterwards. Protected by cs_main.
    map<uint256, NodeId> mapBlockSource;

    // Blocks that are in flight, and that are in the queue to be downloaded.
    // Protected by cs_main.
    struct QueuedBlock {
        uint256 hash;
        int64_t nTime;  // Time of "getdata" request in microseconds.
        int nQueuedBefore;  // Number of blocks in flight at the time of request.
    };
    map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
    map<uint256, pair<NodeId, list<uint256>::iterator> > mapBlocksToDownload;
126
127

} // anon namespace
s_nakamoto's avatar
s_nakamoto committed
128

Pieter Wuille's avatar
Pieter Wuille committed
129
130
131
132
133
//////////////////////////////////////////////////////////////////////////////
//
// dispatching functions
//

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

136
namespace {
137

138
struct CMainSignals {
139
140
    // Notifies listeners of updated transaction data (transaction, and optionally the block it is found in.
    boost::signals2::signal<void (const CTransaction &, const CBlock *)> SyncTransaction;
141
142
143
144
145
146
147
148
149
150
151
    // 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;
152
153

} // anon namespace
Pieter Wuille's avatar
Pieter Wuille committed
154

155
void RegisterWallet(CWalletInterface* pwalletIn) {
156
    g_signals.SyncTransaction.connect(boost::bind(&CWalletInterface::SyncTransaction, pwalletIn, _1, _2));
157
158
159
160
161
    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
162
163
}

164
165
166
167
168
169
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));
170
    g_signals.SyncTransaction.disconnect(boost::bind(&CWalletInterface::SyncTransaction, pwalletIn, _1, _2));
Pieter Wuille's avatar
Pieter Wuille committed
171
172
}

173
174
175
176
177
178
179
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
180
181
}

182
183
void SyncWithWallets(const CTransaction &tx, const CBlock *pblock) {
    g_signals.SyncTransaction(tx, pblock);
Pieter Wuille's avatar
Pieter Wuille committed
184
185
}

186
187
188
189
190
//////////////////////////////////////////////////////////////////////////////
//
// Registration of network node signals.
//

Pieter Wuille's avatar
Pieter Wuille committed
191
namespace {
192
193
194
195
196
197
198

struct CBlockReject {
    unsigned char chRejectCode;
    string strRejectReason;
    uint256 hashBlock;
};

Pieter Wuille's avatar
Pieter Wuille committed
199
200
201
202
203
// 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 {
204
    // Accumulated misbehaviour score for this peer.
Pieter Wuille's avatar
Pieter Wuille committed
205
    int nMisbehavior;
Pieter Wuille's avatar
Pieter Wuille committed
206
    // Whether this peer should be disconnected and banned (unless whitelisted).
Pieter Wuille's avatar
Pieter Wuille committed
207
    bool fShouldBan;
208
    // String name of this peer (debugging/logging purposes).
Pieter Wuille's avatar
Pieter Wuille committed
209
    std::string name;
210
211
    // List of asynchronously-determined block rejections to notify this peer about.
    std::vector<CBlockReject> rejects;
Pieter Wuille's avatar
Pieter Wuille committed
212
213
214
215
    // The best known block we know this peer has announced.
    CBlockIndex *pindexBestKnownBlock;
    // The hash of the last unknown block this peer has announced.
    uint256 hashLastUnknownBlock;
216
217
218
219
220
221
    list<QueuedBlock> vBlocksInFlight;
    int nBlocksInFlight;
    list<uint256> vBlocksToDownload;
    int nBlocksToDownload;
    int64_t nLastBlockReceive;
    int64_t nLastBlockProcess;
Pieter Wuille's avatar
Pieter Wuille committed
222
223
224
225

    CNodeState() {
        nMisbehavior = 0;
        fShouldBan = false;
Pieter Wuille's avatar
Pieter Wuille committed
226
227
        pindexBestKnownBlock = NULL;
        hashLastUnknownBlock = uint256(0);
228
229
230
231
        nBlocksToDownload = 0;
        nBlocksInFlight = 0;
        nLastBlockReceive = 0;
        nLastBlockProcess = 0;
Pieter Wuille's avatar
Pieter Wuille committed
232
233
234
    }
};

235
// Map maintaining per-node state. Requires cs_main.
Pieter Wuille's avatar
Pieter Wuille committed
236
237
238
239
240
241
242
243
244
245
246
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()
247
248
249
250
251
{
    LOCK(cs_main);
    return chainActive.Height();
}

Pieter Wuille's avatar
Pieter Wuille committed
252
253
254
255
256
257
258
259
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);
260
261
262
263
264
265
266
    CNodeState *state = State(nodeid);

    BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight)
        mapBlocksInFlight.erase(entry.hash);
    BOOST_FOREACH(const uint256& hash, state->vBlocksToDownload)
        mapBlocksToDownload.erase(hash);

Pieter Wuille's avatar
Pieter Wuille committed
267
268
    mapNodeState.erase(nodeid);
}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323

// Requires cs_main.
void MarkBlockAsReceived(const uint256 &hash, NodeId nodeFrom = -1) {
    map<uint256, pair<NodeId, list<uint256>::iterator> >::iterator itToDownload = mapBlocksToDownload.find(hash);
    if (itToDownload != mapBlocksToDownload.end()) {
        CNodeState *state = State(itToDownload->second.first);
        state->vBlocksToDownload.erase(itToDownload->second.second);
        state->nBlocksToDownload--;
        mapBlocksToDownload.erase(itToDownload);
    }

    map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
    if (itInFlight != mapBlocksInFlight.end()) {
        CNodeState *state = State(itInFlight->second.first);
        state->vBlocksInFlight.erase(itInFlight->second.second);
        state->nBlocksInFlight--;
        if (itInFlight->second.first == nodeFrom)
            state->nLastBlockReceive = GetTimeMicros();
        mapBlocksInFlight.erase(itInFlight);
    }
}

// Requires cs_main.
bool AddBlockToQueue(NodeId nodeid, const uint256 &hash) {
    if (mapBlocksToDownload.count(hash) || mapBlocksInFlight.count(hash))
        return false;

    CNodeState *state = State(nodeid);
    if (state == NULL)
        return false;

    list<uint256>::iterator it = state->vBlocksToDownload.insert(state->vBlocksToDownload.end(), hash);
    state->nBlocksToDownload++;
    if (state->nBlocksToDownload > 5000)
        Misbehaving(nodeid, 10);
    mapBlocksToDownload[hash] = std::make_pair(nodeid, it);
    return true;
}

// Requires cs_main.
void MarkBlockAsInFlight(NodeId nodeid, const uint256 &hash) {
    CNodeState *state = State(nodeid);
    assert(state != NULL);

    // Make sure it's not listed somewhere already.
    MarkBlockAsReceived(hash);

    QueuedBlock newentry = {hash, GetTimeMicros(), state->nBlocksInFlight};
    if (state->nBlocksInFlight == 0)
        state->nLastBlockReceive = newentry.nTime; // Reset when a first request is sent.
    list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(), newentry);
    state->nBlocksInFlight++;
    mapBlocksInFlight[hash] = std::make_pair(nodeid, it);
}

Pieter Wuille's avatar
Pieter Wuille committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/** Check whether the last unknown block a peer advertized is not yet known. */
void ProcessBlockAvailability(NodeId nodeid) {
    CNodeState *state = State(nodeid);
    assert(state != NULL);

    if (state->hashLastUnknownBlock != 0) {
        map<uint256, CBlockIndex*>::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
        if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
            if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
                state->pindexBestKnownBlock = itOld->second;
            state->hashLastUnknownBlock = uint256(0);
        }
    }
}

/** Update tracking information about which blocks a peer is assumed to have. */
void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
    CNodeState *state = State(nodeid);
    assert(state != NULL);

    ProcessBlockAvailability(nodeid);

    map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(hash);
    if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
        // An actually better block was announced.
        if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
            state->pindexBestKnownBlock = it->second;
    } else {
        // An unknown block was announced; just assume that the latest one is the best one.
        state->hashLastUnknownBlock = hash;
    }
}

357
} // anon namespace
Pieter Wuille's avatar
Pieter Wuille committed
358
359
360
361
362
363
364

bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
    LOCK(cs_main);
    CNodeState *state = State(nodeid);
    if (state == NULL)
        return false;
    stats.nMisbehavior = state->nMisbehavior;
Pieter Wuille's avatar
Pieter Wuille committed
365
    stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
Pieter Wuille's avatar
Pieter Wuille committed
366
367
368
    return true;
}

369
370
void RegisterNodeSignals(CNodeSignals& nodeSignals)
{
371
    nodeSignals.GetHeight.connect(&GetHeight);
372
373
    nodeSignals.ProcessMessages.connect(&ProcessMessages);
    nodeSignals.SendMessages.connect(&SendMessages);
Pieter Wuille's avatar
Pieter Wuille committed
374
375
    nodeSignals.InitializeNode.connect(&InitializeNode);
    nodeSignals.FinalizeNode.connect(&FinalizeNode);
376
}
Pieter Wuille's avatar
Pieter Wuille committed
377

378
379
void UnregisterNodeSignals(CNodeSignals& nodeSignals)
{
380
    nodeSignals.GetHeight.disconnect(&GetHeight);
381
382
    nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
    nodeSignals.SendMessages.disconnect(&SendMessages);
Pieter Wuille's avatar
Pieter Wuille committed
383
384
    nodeSignals.InitializeNode.disconnect(&InitializeNode);
    nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
385
}
Pieter Wuille's avatar
Pieter Wuille committed
386

387
388
//////////////////////////////////////////////////////////////////////////////
//
389
// CChain implementation
390
391
//

392
393
394
395
396
397
398
399
400
401
402
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;
403
404
}

405
CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
406
    int nStep = 1;
407
408
    std::vector<uint256> vHave;
    vHave.reserve(32);
409

410
411
412
413
414
415
416
417
418
    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);
Pieter Wuille's avatar
Pieter Wuille committed
419
420
        if (Contains(pindex)) {
            // Use O(1) CChain index if possible.
421
            pindex = (*this)[nHeight];
Pieter Wuille's avatar
Pieter Wuille committed
422
423
424
425
        } else {
            // Otherwise, use O(log n) skiplist.
            pindex = pindex->GetAncestor(nHeight);
        }
426
427
428
        if (vHave.size() > 10)
            nStep *= 2;
    }
Pieter Wuille's avatar
Pieter Wuille committed
429

430
    return CBlockLocator(vHave);
431
432
}

433
CBlockIndex *CChain::FindFork(const CBlockLocator &locator) const {
434
    // Find the first block the caller has in the main chain
435
    BOOST_FOREACH(const uint256& hash, locator.vHave) {
436
437
438
439
        std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
        if (mi != mapBlockIndex.end())
        {
            CBlockIndex* pindex = (*mi).second;
440
            if (Contains(pindex))
441
442
443
                return pindex;
        }
    }
444
    return Genesis();
445
446
}

447
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
448
449
    if (pindex->nHeight > Height())
        pindex = pindex->GetAncestor(Height());
450
451
452
453
454
    while (pindex && !Contains(pindex))
        pindex = pindex->pprev;
    return pindex;
}

455
CCoinsViewCache *pcoinsTip = NULL;
456
CBlockTreeDB *pblocktree = NULL;
Pieter Wuille's avatar
Pieter Wuille committed
457

s_nakamoto's avatar
s_nakamoto committed
458
459
460
461
462
//////////////////////////////////////////////////////////////////////////////
//
// mapOrphanTransactions
//

463
bool AddOrphanTx(const CTransaction& tx)
s_nakamoto's avatar
s_nakamoto committed
464
465
466
{
    uint256 hash = tx.GetHash();
    if (mapOrphanTransactions.count(hash))
467
468
469
470
471
472
473
474
475
        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:
476
477
    unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
    if (sz > 5000)
478
    {
479
        LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
480
481
        return false;
    }
482

483
    mapOrphanTransactions[hash] = tx;
484
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
485
        mapOrphanTransactionsByPrev[txin.prevout.hash].insert(hash);
486

487
    LogPrint("mempool", "stored orphan tx %s (mapsz %u)\n", hash.ToString(),
488
489
        mapOrphanTransactions.size());
    return true;
s_nakamoto's avatar
s_nakamoto committed
490
491
}

Pieter Wuille's avatar
Pieter Wuille committed
492
void static EraseOrphanTx(uint256 hash)
s_nakamoto's avatar
s_nakamoto committed
493
494
495
{
    if (!mapOrphanTransactions.count(hash))
        return;
496
    const CTransaction& tx = mapOrphanTransactions[hash];
497
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
s_nakamoto's avatar
s_nakamoto committed
498
    {
499
500
501
        mapOrphanTransactionsByPrev[txin.prevout.hash].erase(hash);
        if (mapOrphanTransactionsByPrev[txin.prevout.hash].empty())
            mapOrphanTransactionsByPrev.erase(txin.prevout.hash);
s_nakamoto's avatar
s_nakamoto committed
502
503
504
505
    }
    mapOrphanTransactions.erase(hash);
}

506
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
507
{
508
    unsigned int nEvicted = 0;
509
510
511
    while (mapOrphanTransactions.size() > nMaxOrphans)
    {
        // Evict a random orphan:
512
        uint256 randomhash = GetRandHash();
513
        map<uint256, CTransaction>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
514
515
516
517
518
519
520
        if (it == mapOrphanTransactions.end())
            it = mapOrphanTransactions.begin();
        EraseOrphanTx(it->first);
        ++nEvicted;
    }
    return nEvicted;
}
s_nakamoto's avatar
s_nakamoto committed
521
522
523
524
525
526
527







528
bool IsStandardTx(const CTransaction& tx, string& reason)
Gavin Andresen's avatar
Gavin Andresen committed
529
{
530
    AssertLockHeld(cs_main);
531
    if (tx.nVersion > CTransaction::CURRENT_VERSION || tx.nVersion < 1) {
532
        reason = "version";
533
        return false;
534
    }
535

536
537
538
    // 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)
539
    // Basically we don't want to propagate transactions that can't be included in
540
541
542
543
544
545
546
547
548
549
550
551
552
553
    // 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)) {
554
        reason = "non-final";
555
        return false;
556
    }
557

558
559
560
561
    // 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.
562
    unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
563
564
    if (sz >= MAX_STANDARD_TX_SIZE) {
        reason = "tx-size";
565
        return false;
566
    }
567

568
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
Gavin Andresen's avatar
Gavin Andresen committed
569
    {
570
571
        // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
        // keys. (remember the 520 byte limit on redeemScript size) That works
572
        // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
573
574
575
576
577
        // bytes of scriptSig, which we round off to 1650 bytes for some minor
        // future-proofing. That's also enough to spend a 20-of-20
        // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
        // considered standard)
        if (txin.scriptSig.size() > 1650) {
578
            reason = "scriptsig-size";
579
            return false;
580
581
582
        }
        if (!txin.scriptSig.IsPushOnly()) {
            reason = "scriptsig-not-pushonly";
583
            return false;
584
585
        }
        if (!txin.scriptSig.HasCanonicalPushes()) {
586
            reason = "scriptsig-non-canonical-push";
587
            return false;
588
        }
Gavin Andresen's avatar
Gavin Andresen committed
589
    }
590
591
592

    unsigned int nDataOut = 0;
    txnouttype whichType;
593
    BOOST_FOREACH(const CTxOut& txout, tx.vout) {
594
        if (!::IsStandard(txout.scriptPubKey, whichType)) {
595
            reason = "scriptpubkey";
596
            return false;
597
        }
598

599
600
        if (whichType == TX_NULL_DATA)
            nDataOut++;
601
602
603
604
        else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
            reason = "bare-multisig";
            return false;
        } else if (txout.IsDust(::minRelayTxFee)) {
605
            reason = "dust";
606
            return false;
607
        }
608
    }
609

610
611
    // only one OP_RETURN txout is permitted
    if (nDataOut > 1) {
612
        reason = "multi-op-return";
613
614
615
        return false;
    }

Gavin Andresen's avatar
Gavin Andresen committed
616
617
618
    return true;
}

619
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
620
{
621
    AssertLockHeld(cs_main);
622
623
624
625
    // Time based nLockTime implemented in 0.1.6
    if (tx.nLockTime == 0)
        return true;
    if (nBlockHeight == 0)
626
        nBlockHeight = chainActive.Height();
627
628
    if (nBlockTime == 0)
        nBlockTime = GetAdjustedTime();
629
    if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
630
631
632
633
634
635
636
        return true;
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
        if (!txin.IsFinal())
            return false;
    return true;
}

Gavin Andresen's avatar
Gavin Andresen committed
637
//
638
639
// Check transaction inputs to mitigate two
// potential denial-of-service attacks:
Gavin Andresen's avatar
Gavin Andresen committed
640
//
641
642
643
644
// 1. scriptSigs with extra data stuffed into them,
//    not consumed by scriptPubKey (or P2SH script)
// 2. P2SH scripts with a crazy number of expensive
//    CHECKSIG/CHECKMULTISIG operations
Gavin Andresen's avatar
Gavin Andresen committed
645
//
646
bool AreInputsStandard(const CTransaction& tx, CCoinsViewCache& mapInputs)
Gavin Andresen's avatar
Gavin Andresen committed
647
{
648
    if (tx.IsCoinBase())
649
        return true; // Coinbases don't use vin normally
650

651
    for (unsigned int i = 0; i < tx.vin.size(); i++)
Gavin Andresen's avatar
Gavin Andresen committed
652
    {
653
        const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]);
Gavin Andresen's avatar
Gavin Andresen committed
654
655

        vector<vector<unsigned char> > vSolutions;
656
657
        txnouttype whichType;
        // get the scriptPubKey corresponding to this input:
658
        const CScript& prevScript = prev.scriptPubKey;
659
        if (!Solver(prevScript, whichType, vSolutions))
660
            return false;
661
        int nArgsExpected = ScriptSigArgsExpected(whichType, vSolutions);
662
663
        if (nArgsExpected < 0)
            return false;
664
665
666
667

        // Transactions with extra stuff in their scriptSigs are
        // non-standard. Note that this EvalScript() call will
        // be quick, because if there are any operations
668
669
670
        // beside "push data" in the scriptSig
        // IsStandard() will have already returned false
        // and this method isn't called.
671
        vector<vector<unsigned char> > stack;
672
        if (!EvalScript(stack, tx.vin[i].scriptSig, tx, i, false, 0))
673
674
            return false;

Gavin Andresen's avatar
Gavin Andresen committed
675
676
        if (whichType == TX_SCRIPTHASH)
        {
677
            if (stack.empty())
Gavin Andresen's avatar
Gavin Andresen committed
678
                return false;
679
            CScript subscript(stack.back().begin(), stack.back().end());
680
681
            vector<vector<unsigned char> > vSolutions2;
            txnouttype whichType2;
682
683
684
685
686
687
688
689
690
691
692
693
694
695
            if (Solver(subscript, whichType2, vSolutions2))
            {
                int tmpExpected = ScriptSigArgsExpected(whichType2, vSolutions2);
                if (tmpExpected < 0)
                    return false;
                nArgsExpected += tmpExpected;
            }
            else
            {
                // Any other Script with less than 15 sigops OK:
                unsigned int sigops = subscript.GetSigOpCount(true);
                // ... extra data left on the stack after execution is OK, too:
                return (sigops <= MAX_P2SH_SIGOPS);
            }
Gavin Andresen's avatar
Gavin Andresen committed
696
        }
697

698
        if (stack.size() != (unsigned int)nArgsExpected)
699
            return false;
Gavin Andresen's avatar
Gavin Andresen committed
700
701
702
703
704
    }

    return true;
}

705
unsigned int GetLegacySigOpCount(const CTransaction& tx)
706
{
707
    unsigned int nSigOps = 0;
708
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
709
710
711
    {
        nSigOps += txin.scriptSig.GetSigOpCount(false);
    }
712
    BOOST_FOREACH(const CTxOut& txout, tx.vout)
713
714
715
716
717
    {
        nSigOps += txout.scriptPubKey.GetSigOpCount(false);
    }
    return nSigOps;
}
s_nakamoto's avatar
s_nakamoto committed
718

719
720
721
722
723
724
725
726
727
728
729
730
731
732
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
733
734
735

int CMerkleTx::SetMerkleBranch(const CBlock* pblock)
{
736
    AssertLockHeld(cs_main);
Pieter Wuille's avatar
Pieter Wuille committed
737
738
739
740
741
    CBlock blockTmp;

    if (pblock == NULL) {
        CCoins coins;
        if (pcoinsTip->GetCoins(GetHash(), coins)) {
742
            CBlockIndex *pindex = chainActive[coins.nHeight];
Pieter Wuille's avatar
Pieter Wuille committed
743
            if (pindex) {
744
                if (!ReadBlockFromDisk(blockTmp, pindex))
Pieter Wuille's avatar
Pieter Wuille committed
745
746
                    return 0;
                pblock = &blockTmp;
Pieter Wuille's avatar
Pieter Wuille committed
747
            }
s_nakamoto's avatar
s_nakamoto committed
748
        }
Pieter Wuille's avatar
Pieter Wuille committed
749
    }
s_nakamoto's avatar
s_nakamoto committed
750

Pieter Wuille's avatar
Pieter Wuille committed
751
    if (pblock) {
s_nakamoto's avatar
s_nakamoto committed
752
753
754
755
        // Update the tx's hashBlock
        hashBlock = pblock->GetHash();

        // Locate the transaction
756
        for (nIndex = 0; nIndex < (int)pblock->vtx.size(); nIndex++)
s_nakamoto's avatar
s_nakamoto committed
757
758
            if (pblock->vtx[nIndex] == *(CTransaction*)this)
                break;
759
        if (nIndex == (int)pblock->vtx.size())
s_nakamoto's avatar
s_nakamoto committed
760
761
762
        {
            vMerkleBranch.clear();
            nIndex = -1;
763
            LogPrintf("ERROR: SetMerkleBranch() : couldn't find tx in block\n");
s_nakamoto's avatar
s_nakamoto committed
764
765
766
767
768
769
770
771
772
773
774
775
            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;
776
    if (!pindex || !chainActive.Contains(pindex))
s_nakamoto's avatar
s_nakamoto committed
777
778
        return 0;

779
    return chainActive.Height() - pindex->nHeight + 1;
s_nakamoto's avatar
s_nakamoto committed
780
781
782
783
784
785
786
787
}







788
bool CheckTransaction(const CTransaction& tx, CValidationState &state)
789
790
{
    // Basic checks that don't depend on any context
791
    if (tx.vin.empty())
Gavin Andresen's avatar
Gavin Andresen committed
792
        return state.DoS(10, error("CheckTransaction() : vin empty"),
793
                         REJECT_INVALID, "bad-txns-vin-empty");
794
    if (tx.vout.empty())
Gavin Andresen's avatar
Gavin Andresen committed
795
        return state.DoS(10, error("CheckTransaction() : vout empty"),
796
                         REJECT_INVALID, "bad-txns-vout-empty");
797
    // Size limits
798
    if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
799
        return state.DoS(100, error("CheckTransaction() : size limits failed"),
800
                         REJECT_INVALID, "bad-txns-oversize");
801
802

    // Check for negative or overflow output values
803
    int64_t nValueOut = 0;
804
    BOOST_FOREACH(const CTxOut& txout, tx.vout)
805
806
    {
        if (txout.nValue < 0)
Gavin Andresen's avatar
Gavin Andresen committed
807
            return state.DoS(100, error("CheckTransaction() : txout.nValue negative"),
808
                             REJECT_INVALID, "bad-txns-vout-negative");
809
        if (txout.nValue > MAX_MONEY)
Gavin Andresen's avatar
Gavin Andresen committed
810
            return state.DoS(100, error("CheckTransaction() : txout.nValue too high"),
811
                             REJECT_INVALID, "bad-txns-vout-toolarge");
812
813
        nValueOut += txout.nValue;
        if (!MoneyRange(nValueOut))
814
            return state.DoS(100, error("CheckTransaction() : txout total out of range"),
815
                             REJECT_INVALID, "bad-txns-txouttotal-toolarge");
816
817
    }

818
819
    // Check for duplicate inputs
    set<COutPoint> vInOutPoints;
820
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
821
822
    {
        if (vInOutPoints.count(txin.prevout))
823
            return state.DoS(100, error("CheckTransaction() : duplicate inputs"),
824
                             REJECT_INVALID, "bad-txns-inputs-duplicate");
825
826
827
        vInOutPoints.insert(txin.prevout);
    }

828
    if (tx.IsCoinBase())
829
    {
830
        if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
Gavin Andresen's avatar
Gavin Andresen committed
831
            return state.DoS(100, error("CheckTransaction() : coinbase script size"),
832
                             REJECT_INVALID, "bad-cb-length");
833
834
835
    }
    else
    {
836
        BOOST_FOREACH(const CTxIn& txin, tx.vin)
837
            if (txin.prevout.IsNull())
Gavin Andresen's avatar
Gavin Andresen committed
838
                return state.DoS(10, error("CheckTransaction() : prevout is null"),
839
                                 REJECT_INVALID, "bad-txns-prevout-null");
840
841
842
843
844
    }

    return true;
}

845
int64_t GetMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree)
846
{
847
848
849
850
851
852
853
854
855
856
    {
        LOCK(mempool.cs);
        uint256 hash = tx.GetHash();
        double dPriorityDelta = 0;
        int64_t nFeeDelta = 0;
        mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
        if (dPriorityDelta > 0 || nFeeDelta > 0)
            return 0;
    }

Gavin Andresen's avatar
Gavin Andresen committed
857
    int64_t nMinFee = ::minRelayTxFee.GetFee(nBytes);
858
859
860

    if (fAllowFree)
    {
861
862
        // 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
863
864
        //   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.
865
        if (nBytes < (DEFAULT_BLOCK_PRIORITY_SIZE - 1000))
866
            nMinFee = 0;
867
868
869
870
871
872
873
    }

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

Pieter Wuille's avatar
Pieter Wuille committed
874

875
bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
876
                        bool* pfMissingInputs, bool fRejectInsaneFee)
s_nakamoto's avatar
s_nakamoto committed
877
{
878
    AssertLockHeld(cs_main);
s_nakamoto's avatar
s_nakamoto committed
879
880
881
    if (pfMissingInputs)
        *pfMissingInputs = false;

882
    if (!CheckTransaction(tx, state))
883
        return error("AcceptToMemoryPool: : CheckTransaction failed");
884

s_nakamoto's avatar
s_nakamoto committed
885
    // Coinbase is only valid in a block, not as a loose transaction
886
    if (tx.IsCoinBase())
Gavin Andresen's avatar
Gavin Andresen committed
887
888
        return state.DoS(100, error("AcceptToMemoryPool: : coinbase as individual tx"),
                         REJECT_INVALID, "coinbase");
s_nakamoto's avatar
s_nakamoto committed
889

890
    // Rather not work on nonstandard transactions (unless -testnet/-regtest)
891
    string reason;
jtimon's avatar
jtimon committed
892
    if (Params().RequireStandard() && !IsStandardTx(tx, reason))
Gavin Andresen's avatar
Gavin Andresen committed
893
        return state.DoS(0,
894
                         error("AcceptToMemoryPool : nonstandard transaction: %s", reason),
Gavin Andresen's avatar
Gavin Andresen committed
895
                         REJECT_NONSTANDARD, reason);
896

Pieter Wuille's avatar
Pieter Wuille committed
897
    // is it already in the memory pool?
898
    uint256 hash = tx.GetHash();
899
900
    if (pool.exists(hash))
        return false;
s_nakamoto's avatar
s_nakamoto committed
901
902

    // Check for conflicts with in-memory transactions
903
904
    {
    LOCK(pool.cs); // protect pool.mapNextTx
905
    for (unsigned int i = 0; i < tx.vin.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
906
    {
907
        COutPoint outpoint = tx.vin[i].prevout;
908
        if (pool.mapNextTx.count(outpoint))
s_nakamoto's avatar
s_nakamoto committed
909
        {
910
            // Disable replacement feature for now
911
            return false;
s_nakamoto's avatar
s_nakamoto committed
912
913
        }
    }
914
    }
s_nakamoto's avatar
s_nakamoto committed
915
916

    {
917
918
919
        CCoinsView dummy;
        CCoinsViewCache view(dummy);

920
        int64_t nValueIn = 0;
921
        {
922
923
        LOCK(pool.cs);
        CCoinsViewMemPool viewMemPool(*pcoinsTip, pool);
924
        view.SetBackend(viewMemPool);
Pieter Wuille's avatar
Pieter Wuille committed
925
926
927

        // do we already have it?
        if (view.HaveCoins(hash))
928
            return false;
Pieter Wuille's avatar
Pieter Wuille committed
929
930

        // do all inputs exist?
Pieter Wuille's avatar
Pieter Wuille committed
931
932
        // 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
933
934
935
936
937
938
        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
939
940
        }

Pieter Wuille's avatar
Pieter Wuille committed
941
        // are the actual inputs available?
942
        if (!view.HaveInputs(tx))
Gavin Andresen's avatar
Gavin Andresen committed
943
            return state.Invalid(error("AcceptToMemoryPool : inputs already spent"),
944
                                 REJECT_DUPLICATE, "bad-txns-inputs-spent");
945

946
947
948
        // Bring the best block into scope
        view.GetBestBlock();

949
950
        nValueIn = view.GetValueIn(tx);

951
952
953
        // 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
954

955
        // Check for non-standard pay-to-script-hash in inputs
jtimon's avatar
jtimon committed
956
        if (Params().RequireStandard() && !AreInputsStandard(tx, view))
957
            return error("AcceptToMemoryPool: : nonstandard transaction input");
Gavin Andresen's avatar
Gavin Andresen committed
958

959
960
961
962
        // 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.

963
964
965
966
967
968
        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();
969
970

        // Don't accept it if it can't get into a block
971
        int64_t txMinFee = GetMinRelayFee(tx, nSize, true);
972
        if (fLimitFree && nFees < txMinFee)
973
            return state.DoS(0, error("AcceptToMemoryPool : not enough fees %s, %d < %d",
974
                                      hash.ToString(), nFees, txMinFee),
Gavin Andresen's avatar
Gavin Andresen committed
975
                             REJECT_INSUFFICIENTFEE, "insufficient fee");
976

Gavin Andresen's avatar
Gavin Andresen committed
977
        // Continuously rate-limit free (really, very-low-fee)transactions
978
        // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
979
        // be annoying or make others' transactions take longer to confirm.
Gavin Andresen's avatar
Gavin Andresen committed
980
        if (fLimitFree && nFees < ::minRelayTxFee.GetFee(nSize))
981
        {
982
            static CCriticalSection csFreeLimiter;
983
            static double dFreeCount;
984
985
986
987
            static int64_t nLastTime;
            int64_t nNow = GetTime();

            LOCK(csFreeLimiter);
988

989
990
991
992
993
994
            // 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
            if (dFreeCount >= GetArg("-limitfreerelay", 15)*10*1000)
Gavin Andresen's avatar
Gavin Andresen committed
995
996
                return state.DoS(0, error("AcceptToMemoryPool : free transaction rejected by rate limiter"),
                                 REJECT_INSUFFICIENTFEE, "insufficient priority");
997
            LogPrint("mempool", "Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
998
            dFreeCount += nSize;
999
        }
1000

Gavin Andresen's avatar
Gavin Andresen committed
1001
        if (fRejectInsaneFee && nFees > ::minRelayTxFee.GetFee(nSize) * 10000)
1002
            return error("AcceptToMemoryPool: : insane fees %s, %d > %d",
1003
                         hash.ToString(),
Gavin Andresen's avatar
Gavin Andresen committed
1004
                         nFees, ::minRelayTxFee.GetFee(nSize) * 10000);
1005

1006
1007
        // Check against previous transactions
        // This is done last to help prevent CPU exhaustion denial-of-service attacks.
1008
        if (!CheckInputs(tx, state, view, true, STANDARD_SCRIPT_VERIFY_FLAGS))
1009
        {
1010
            return error("AcceptToMemoryPool: : ConnectInputs failed %s", hash.ToString());
1011
        }
1012
1013
        // Store transaction in memory
        pool.addUnchecked(hash, entry);
1014
1015
    }

1016
    g_signals.SyncTransaction(tx, NULL);
1017

1018
    return true;
1019
1020
}

1021

1022
int CMerkleTx::GetDepthInMainChainINTERNAL(CBlockIndex* &pindexRet) const
s_nakamoto's avatar
s_nakamoto committed
1023
1024
1025
{
    if (hashBlock == 0 || nIndex == -1)
        return 0;
1026
    AssertLockHeld(cs_main);
s_nakamoto's avatar
s_nakamoto committed
1027
1028
1029
1030
1031
1032

    // 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;
1033
    if (!pindex || !chainActive.Contains(pindex))
s_nakamoto's avatar
s_nakamoto committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
        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;
    }

1044
    pindexRet = pindex;
1045
    return chainActive.Height() - pindex->nHeight + 1;
s_nakamoto's avatar
s_nakamoto committed
1046
1047
}

1048
1049
int CMerkleTx::GetDepthInMainChain(CBlockIndex* &pindexRet) const
{
1050
    AssertLockHeld(cs_main);
1051
1052
1053
1054
1055
1056
    int nResult = GetDepthInMainChainINTERNAL(pindexRet);
    if (nResult == 0 && !mempool.exists(GetHash()))
        return -1; // Not in chain, not in mempool

    return nResult;
}
s_nakamoto's avatar
s_nakamoto committed
1057
1058
1059
1060
1061

int CMerkleTx::GetBlocksToMaturity() const
{
    if (!IsCoinBase())
        return 0;
1062
    return max(0, (COINBASE_MATURITY+1) - GetDepthInMainChain());
s_nakamoto's avatar
s_nakamoto committed
1063
1064
1065
}


Gavin Andresen's avatar
Gavin Andresen committed
1066
bool CMerkleTx::AcceptToMemoryPool(bool fLimitFree, bool fRejectInsaneFee)
s_nakamoto's avatar
s_nakamoto committed
1067
{
Pieter Wuille's avatar
Pieter Wuille committed
1068
    CValidationState state;
Gavin Andresen's avatar
Gavin Andresen committed
1069
    return ::AcceptToMemoryPool(mempool, state, *this, fLimitFree, NULL, fRejectInsaneFee);
s_nakamoto's avatar
s_nakamoto committed
1070
1071
1072
}


1073
// 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
1074
bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
1075
{
Pieter Wuille's avatar
Pieter Wuille committed
1076
    CBlockIndex *pindexSlow = NULL;
1077
1078
1079
    {
        LOCK(cs_main);
        {
1080
            if (mempool.lookup(hash, txOut))
1081
1082
1083
1084
            {
                return true;
            }
        }
Pieter Wuille's avatar
Pieter Wuille committed
1085

1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
        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) {
1096
                    return error("%s : Deserialize or I/O error - %s", __func__, e.what());
1097
1098
1099
                }
                hashBlock = header.GetHash();
                if (txOut.GetHash() != hash)
1100
                    return error("%s : txid mismatch", __func__);
1101
1102
1103
1104
                return true;
            }
        }

Pieter Wuille's avatar
Pieter Wuille committed
1105
1106
1107
        if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
            int nHeight = -1;
            {
1108
                CCoinsViewCache &view = *pcoinsTip;
Pieter Wuille's avatar
Pieter Wuille committed
1109
1110
1111
1112
1113
                CCoins coins;
                if (view.GetCoins(hash, coins))
                    nHeight = coins.nHeight;
            }
            if (nHeight > 0)
1114
                pindexSlow = chainActive[nHeight];
1115
1116
        }
    }
s_nakamoto's avatar
s_nakamoto committed
1117

Pieter Wuille's avatar
Pieter Wuille committed
1118
1119
    if (pindexSlow) {
        CBlock block;
1120
        if (ReadBlockFromDisk(block, pindexSlow)) {
Pieter Wuille's avatar
Pieter Wuille committed
1121
1122
1123
1124
1125
1126
1127
1128
1129
            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
1130

Pieter Wuille's avatar
Pieter Wuille committed
1131
1132
    return false;
}
s_nakamoto's avatar
s_nakamoto committed
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143






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

1144
1145
1146
1147
1148
bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos)
{
    // Open history file to append
    CAutoFile fileout = CAutoFile(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
    if (!fileout)
1149
        return error("WriteBlockToDisk : OpenBlockFile failed");
1150
1151
1152
1153
1154
1155
1156
1157

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

    // Write block
    long fileOutPos = ftell(fileout);
    if (fileOutPos < 0)
1158
        return error("WriteBlockToDisk : ftell failed");
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
    pos.nPos = (unsigned int)fileOutPos;
    fileout << block;

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

    return true;
}

1170
1171
1172
1173
1174
1175
1176
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)
1177
        return error("ReadBlockFromDisk : OpenBlockFile failed");
1178
1179
1180
1181
1182
1183

    // Read block
    try {
        filein >> block;
    }
    catch (std::exception &e) {
1184
        return error("%s : Deserialize or I/O error - %s", __func__, e.what());
1185
1186
1187
1188
    }

    // Check the header
    if (!CheckProofOfWork(block.GetHash(), block.nBits))
1189
        return error("ReadBlockFromDisk : Errors in block header");
1190
1191
1192
1193

    return true;
}

1194
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex)
s_nakamoto's avatar
s_nakamoto committed
1195
{
1196
    if (!ReadBlockFromDisk(block, pindex->GetBlockPos()))
s_nakamoto's avatar
s_nakamoto committed
1197
        return false;
1198
1199
    if (block.GetHash() != pindex->GetBlockHash())
        return error("ReadBlockFromDisk(CBlock&, CBlockIndex*) : GetHash() doesn't match index");
s_nakamoto's avatar
s_nakamoto committed
1200
1201
1202
    return true;
}

1203
uint256 static GetOrphanRoot(const uint256& hash)
s_nakamoto's avatar
s_nakamoto committed
1204
{
1205
1206
1207
1208
    map<uint256, COrphanBlock*>::iterator it = mapOrphanBlocks.find(hash);
    if (it == mapOrphanBlocks.end())
        return hash;

s_nakamoto's avatar
s_nakamoto committed
1209
    // Work back to the first block in the orphan chain
1210
1211
1212
1213
1214
1215
    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
1216
1217
}

1218
1219
1220
// Remove a random orphan block (which does not have any dependent orphans).
void static PruneOrphanBlocks()
{
1221
    if (mapOrphanBlocksByPrev.size() <= (size_t)std::max((int64_t)0, GetArg("-maxorphanblocks", DEFAULT_MAX_ORPHAN_BLOCKS)))
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
        return;

    // Pick a random orphan block.
    int pos = insecure_rand() % mapOrphanBlocksByPrev.size();
    std::multimap<uint256, COrphanBlock*>::iterator it = mapOrphanBlocksByPrev.begin();
    while (pos--) it++;

    // As long as this block has other orphans depending on it, move to one of those successors.
    do {
        std::multimap<uint256, COrphanBlock*>::iterator it2 = mapOrphanBlocksByPrev.find(it->second->hashBlock);
        if (it2 == mapOrphanBlocksByPrev.end())
            break;
        it = it2;
    } while(1);

    uint256 hash = it->second->hashBlock;
    delete it->second;
    mapOrphanBlocksByPrev.erase(it);
    mapOrphanBlocks.erase(hash);
}

1243
int64_t GetBlockValue(int nHeight, int64_t nFees)
s_nakamoto's avatar
s_nakamoto committed
1244
{
1245
    int64_t nSubsidy = 50 * COIN;
1246
1247
1248
1249
1250
    int halvings = nHeight / Params().SubsidyHalvingInterval();

    // Force block reward to zero when right shift is undefined.
    if (halvings >= 64)
        return nFees;
s_nakamoto's avatar
s_nakamoto committed
1251

1252
    // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
1253
    nSubsidy >>= halvings;
s_nakamoto's avatar
s_nakamoto committed
1254
1255
1256
1257
1258
1259

    return nSubsidy + nFees;
}

bool IsInitialBlockDownload()
{
1260
    LOCK(cs_main);
1261
    if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate())
s_nakamoto's avatar
s_nakamoto committed
1262
        return true;
1263
    static int64_t nLastUpdate;
s_nakamoto's avatar
s_nakamoto committed
1264
    static CBlockIndex* pindexLastBest;
1265
    if (chainActive.Tip() != pindexLastBest)
s_nakamoto's avatar
s_nakamoto committed
1266
    {
1267
        pindexLastBest = chainActive.Tip();
s_nakamoto's avatar
s_nakamoto committed
1268
1269
1270
        nLastUpdate = GetTime();
    }
    return (GetTime() - nLastUpdate < 10 &&
1271
            chainActive.Tip()->GetBlockTime() < GetTime() - 24 * 60 * 60);
s_nakamoto's avatar
s_nakamoto committed
1272
1273
}

1274
bool fLargeWorkForkFound = false;
1275
bool fLargeWorkInvalidChainFound = false;
1276
1277
1278
1279
CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;

void CheckForkWarningConditions()
{
1280
    AssertLockHeld(cs_main);
1281
1282
1283
1284
1285
    // 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;

1286
1287
    // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
    // of our head, drop it
1288
    if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
1289
1290
        pindexBestForkTip = NULL;

1291
    if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6)))
1292
    {
1293
1294
1295
1296
1297
        if (!fLargeWorkForkFound)
        {
            std::string strCmd = GetArg("-alertnotify", "");
            if (!strCmd.empty())
            {
1298
1299
                std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
                                      pindexBestForkBase->phashBlock->ToString() + std::string("'");
1300
1301
1302
1303
                boost::replace_all(strCmd, "%s", warning);
                boost::thread t(runCommand, strCmd); // thread runs free
            }
        }
1304
1305
        if (pindexBestForkTip)
        {
1306
            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",
1307
1308
                   pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
                   pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1309
1310
1311
1312
            fLargeWorkForkFound = true;
        }
        else
        {
1313
            LogPrintf("CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n");
1314
1315
1316
1317
1318
            fLargeWorkInvalidChainFound = true;
        }
    }
    else
    {
1319
        fLargeWorkForkFound = false;
1320
1321
        fLargeWorkInvalidChainFound = false;
    }
1322
1323
1324
1325
}

void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
{
1326
    AssertLockHeld(cs_main);
1327
1328
    // If we are on a fork that is sufficiently large, set a warning flag
    CBlockIndex* pfork = pindexNewForkTip;
1329
    CBlockIndex* plonger = chainActive.Tip();
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
    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)) &&
1347
            pindexNewForkTip->nChainWork - pfork->nChainWork > (pfork->GetBlockWork() * 7) &&
1348
            chainActive.Height() - pindexNewForkTip->nHeight < 72)
1349
1350
1351
1352
1353
1354
1355
1356
    {
        pindexBestForkTip = pindexNewForkTip;
        pindexBestForkBase = pfork;
    }

    CheckForkWarningConditions();
}

1357
// Requires cs_main.
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
void Misbehaving(NodeId pnode, int howmuch)
{
    if (howmuch == 0)
        return;

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

    state->nMisbehavior += howmuch;
Pieter Wuille's avatar
Pieter Wuille committed
1368
1369
    int banscore = GetArg("-banscore", 100);
    if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
1370
1371
1372
1373
1374
1375
1376
    {
        LogPrintf("Misbehaving: %s (%d -> %d) BAN THRESHOLD EXCEEDED\n", state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
        state->fShouldBan = true;
    } else
        LogPrintf("Misbehaving: %s (%d -> %d)\n", state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
}

Pieter Wuille's avatar
Pieter Wuille committed
1377
void static InvalidChainFound(CBlockIndex* pindexNew)
s_nakamoto's avatar
s_nakamoto committed
1378
{
1379
    if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
s_nakamoto's avatar
s_nakamoto committed
1380
    {
1381
        pindexBestInvalid = pindexNew;
1382
        uiInterface.NotifyBlocksChanged();
s_nakamoto's avatar
s_nakamoto committed
1383
    }
1384
    LogPrintf("InvalidChainFound: invalid block=%s  height=%d  log2_work=%.8g  date=%s\n",
1385
      pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
Pieter Wuille's avatar
Pieter Wuille committed
1386
      log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
1387
      pindexNew->GetBlockTime()));
1388
    LogPrintf("InvalidChainFound:  current best=%s  height=%d  log2_work=%.8g  date=%s\n",
1389
1390
      chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0),
      DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()));
1391
    CheckForkWarningConditions();
s_nakamoto's avatar
s_nakamoto committed
1392
1393
}

1394
1395
1396
1397
1398
1399
1400
1401
1402
void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
    int nDoS = 0;
    if (state.IsInvalid(nDoS)) {
        std::map<uint256, NodeId>::iterator it = mapBlockSource.find(pindex->GetBlockHash());
        if (it != mapBlockSource.end() && State(it->second)) {
            CBlockReject reject = {state.GetRejectCode(), state.GetRejectReason(), pindex->GetBlockHash()};
            State(it->second)->rejects.push_back(reject);
            if (nDoS > 0)
                Misbehaving(it->second, nDoS);
1403
        }
1404
1405
1406
1407
1408
1409
1410
    }
    if (!state.CorruptionPossible()) {
        pindex->nStatus |= BLOCK_FAILED_VALID;
        pblocktree->WriteBlockIndex(CDiskBlockIndex(pindex));
        setBlockIndexValid.erase(pindex);
        InvalidChainFound(pindex);
    }
1411
1412
}

1413
void UpdateTime(CBlockHeader& block, const CBlockIndex* pindexPrev)
1414
{
1415
    block.nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
1416
1417

    // Updating time can change work required on testnet:
1418
    if (Params().AllowMinDifficultyBlocks())
1419
        block.nBits = GetNextWorkRequired(pindexPrev, &block);
1420
1421
}

s_nakamoto's avatar
s_nakamoto committed
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431










1432
void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
Pieter Wuille's avatar
Pieter Wuille committed
1433
{
1434
    bool ret;
Pieter Wuille's avatar
Pieter Wuille committed
1435
    // mark inputs spent
1436
1437
    if (!tx.IsCoinBase()) {
        BOOST_FOREACH(const CTxIn &txin, tx.vin) {
Pieter Wuille's avatar
Pieter Wuille committed
1438
            CCoins &coins = inputs.GetCoins(txin.prevout.hash);
Pieter Wuille's avatar
Pieter Wuille committed
1439
            CTxInUndo undo;
1440
1441
            ret = coins.Spend(txin.prevout, undo);
            assert(ret);
Pieter Wuille's avatar
Pieter Wuille committed
1442
1443
1444
1445
1446
            txundo.vprevout.push_back(undo);
        }
    }

    // add outputs
1447
    ret = inputs.SetCoins(tx.GetHash(), CCoins(tx, nHeight));
1448
    assert(ret);
Pieter Wuille's avatar
Pieter Wuille committed
1449
1450
}

1451
1452
1453
bool CScriptCheck::operator()() const {
    const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
    if (!VerifyScript(scriptSig, scriptPubKey, *ptxTo, nIn, nFlags, nHashType))
1454
        return error("CScriptCheck() : %s VerifySignature failed", ptxTo->GetHash().ToString());
1455
1456
1457
    return true;
}

Pieter Wuille's avatar
Pieter Wuille committed
1458
1459
bool VerifySignature(const CCoins& txFrom, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType)
{
1460
    return CScriptCheck(txFrom, txTo, nIn, flags, nHashType)();
Pieter Wuille's avatar
Pieter Wuille committed
1461
1462
}

1463
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
1464
{
1465
    if (!tx.IsCoinBase())
s_nakamoto's avatar
s_nakamoto committed
1466
    {
1467
        if (pvChecks)
1468
            pvChecks->reserve(tx.vin.size());
1469

Pieter Wuille's avatar
Pieter Wuille committed
1470
1471
        // 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.
1472
        if (!inputs.HaveInputs(tx))
1473
            return state.Invalid(error("CheckInputs() : %s inputs unavailable", tx.GetHash().ToString()));
Pieter Wuille's avatar
Pieter Wuille committed
1474

1475
1476
        // While checking, GetBestBlock() refers to the parent block.
        // This is also true for mempool checks.
1477
1478
        CBlockIndex *pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
        int nSpendHeight = pindexPrev->nHeight + 1;
1479
1480
        int64_t nValueIn = 0;
        int64_t nFees = 0;
1481
        for (unsigned int i = 0; i < tx.vin.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
1482
        {
1483
            const COutPoint &prevout = tx.vin[i].prevout;
Pieter Wuille's avatar
Pieter Wuille committed
1484
            const CCoins &coins = inputs.GetCoins(prevout.hash);
s_nakamoto's avatar
s_nakamoto committed
1485
1486

            // If prev is coinbase, check that it's matured
Pieter Wuille's avatar
Pieter Wuille committed
1487
            if (coins.IsCoinBase()) {
1488
                if (nSpendHeight - coins.nHeight < COINBASE_MATURITY)
Gavin Andresen's avatar
Gavin Andresen committed
1489
1490
                    return state.Invalid(
                        error("CheckInputs() : tried to spend coinbase at depth %d", nSpendHeight - coins.nHeight),
1491
                        REJECT_INVALID, "bad-txns-premature-spend-of-coinbase");
Pieter Wuille's avatar
Pieter Wuille committed
1492
            }
s_nakamoto's avatar
s_nakamoto committed
1493

1494
            // Check for negative or overflow input values
Pieter Wuille's avatar
Pieter Wuille committed
1495
1496
            nValueIn += coins.vout[prevout.n].nValue;
            if (!MoneyRange(coins.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
Gavin Andresen's avatar
Gavin Andresen committed
1497
                return state.DoS(100, error("CheckInputs() : txin values out of range"),
1498
                                 REJECT_INVALID, "bad-txns-inputvalues-outofrange");
1499
1500

        }
Pieter Wuille's avatar
Pieter Wuille committed
1501

1502
        if (nValueIn < tx.GetValueOut())
1503
            return state.DoS(100, error("CheckInputs() : %s value in < value out", tx.GetHash().ToString()),
1504
                             REJECT_INVALID, "bad-txns-in-belowout");
Pieter Wuille's avatar
Pieter Wuille committed
1505
1506

        // Tally transaction fees
1507
        int64_t nTxFee = nValueIn - tx.GetValueOut();
Pieter Wuille's avatar
Pieter Wuille committed
1508
        if (nTxFee < 0)
1509
            return state.DoS(100, error("CheckInputs() : %s nTxFee < 0", tx.GetHash().ToString()),
1510
                             REJECT_INVALID, "bad-txns-fee-negative");
Pieter Wuille's avatar
Pieter Wuille committed
1511
1512
        nFees += nTxFee;
        if (!MoneyRange(nFees))
Gavin Andresen's avatar
Gavin Andresen committed
1513
            return state.DoS(100, error("CheckInputs() : nFees out of range"),
1514
                             REJECT_INVALID, "bad-txns-fee-outofrange");
Pieter Wuille's avatar
Pieter Wuille committed
1515

1516
1517
1518
1519
        // 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
1520
        // Skip ECDSA signature verification when connecting blocks
1521
        // before the last block chain checkpoint. This is safe because block merkle hashes are
Pieter Wuille's avatar
Pieter Wuille committed
1522
        // still computed and checked, and any change will be caught at the next checkpoint.
1523
        if (fScriptChecks) {
1524
1525
            for (unsigned int i = 0; i < tx.vin.size(); i++) {
                const COutPoint &prevout = tx.vin[i].prevout;
Pieter Wuille's avatar
Pieter Wuille committed
1526
                const CCoins &coins = inputs.GetCoins(prevout.hash);
1527

1528
                // Verify signature
1529
                CScriptCheck check(coins, tx, i, flags, 0);
1530
1531
1532
                if (pvChecks) {
                    pvChecks->push_back(CScriptCheck());
                    check.swap(pvChecks->back());
1533
                } else if (!check()) {
1534
1535
1536
1537
1538
1539
1540
1541
1542
                    if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
                        // Check whether the failure was caused by a
                        // non-mandatory script verification check, such as
                        // non-standard DER encodings or non-null dummy
                        // arguments; if so, don't trigger DoS protection to
                        // avoid splitting the network between upgraded and
                        // non-upgraded nodes.
                        CScriptCheck check(coins, tx, i,
                                flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, 0);
1543
                        if (check())
1544
                            return state.Invalid(false, REJECT_NONSTANDARD, "non-mandatory-script-verify-flag");
1545
                    }
1546
1547
1548
1549
1550
1551
1552
1553
                    // Failures of other flags indicate a transaction that is
                    // invalid in new blocks, e.g. a invalid P2SH. We DoS ban
                    // such nodes as they are not following the protocol. That
                    // said during an upgrade careful thought should be taken
                    // as to the correct behavior - we may want to continue
                    // peering with non-upgraded nodes even after a soft-fork
                    // super-majority vote has passed.
                    return state.DoS(100,false, REJECT_INVALID, "mandatory-script-verify-flag-failed");
1554
                }
1555
            }
s_nakamoto's avatar
s_nakamoto committed
1556
1557
1558
1559
1560
1561
1562
1563
        }
    }

    return true;
}



1564
bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
s_nakamoto's avatar
s_nakamoto committed
1565
{
1566
    assert(pindex->GetBlockHash() == view.GetBestBlock());
s_nakamoto's avatar
s_nakamoto committed
1567

1568
1569
1570
1571
1572
    if (pfClean)
        *pfClean = false;

    bool fClean = true;

Pieter Wuille's avatar
Pieter Wuille committed
1573
    CBlockUndo blockUndo;
Pieter Wuille's avatar
Pieter Wuille committed
1574
1575
1576
1577
1578
    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
1579

1580
    if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
1581
        return error("DisconnectBlock() : block and undo data inconsistent");
Pieter Wuille's avatar
Pieter Wuille committed
1582
1583

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

1588
1589
1590
1591
1592
1593
        // 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;
1594
        outs.ClearUnspendable();
Pieter Wuille's avatar
Pieter Wuille committed
1595
1596

        CCoins outsBlock = CCoins(tx, pindex->nHeight);
1597
1598
1599
1600
1601
        // 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
1602
        if (outs != outsBlock)
1603
            fClean = fClean && error("DisconnectBlock() : added transaction mismatch? database corrupted");
Pieter Wuille's avatar
Pieter Wuille committed
1604
1605

        // remove outputs
Pieter Wuille's avatar
Pieter Wuille committed
1606
        outs = CCoins();
Pieter Wuille's avatar
Pieter Wuille committed
1607
1608
1609
1610

        // restore inputs
        if (i > 0) { // not coinbases
            const CTxUndo &txundo = blockUndo.vtxundo[i-1];
1611
1612
            if (txundo.vprevout.size() != tx.vin.size())
                return error("DisconnectBlock() : transaction and undo data inconsistent");
Pieter Wuille's avatar
Pieter Wuille committed
1613
1614
1615
1616
1617
            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
1618
1619
1620
1621
1622
                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
1623
1624
1625
1626
                    coins.fCoinBase = undo.fCoinBase;
                    coins.nHeight = undo.nHeight;
                    coins.nVersion = undo.nVersion;
                } else {
1627
1628
                    if (coins.IsPruned())
                        fClean = fClean && error("DisconnectBlock() : undo data adding output to missing transaction");
Pieter Wuille's avatar
Pieter Wuille committed
1629
1630
                }
                if (coins.IsAvailable(out.n))
1631
                    fClean = fClean && error("DisconnectBlock() : undo data overwriting existing output");
Pieter Wuille's avatar
Pieter Wuille committed
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
                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
1642
    view.SetBestBlock(pindex->pprev->GetBlockHash());
Pieter Wuille's avatar
Pieter Wuille committed
1643

1644
1645
1646
1647
1648
1649
    if (pfClean) {
        *pfClean = fClean;
        return true;
    } else {
        return fClean;
    }
s_nakamoto's avatar
s_nakamoto committed
1650
1651
}

1652
void static FlushBlockFile(bool fFinalize = false)
Pieter Wuille's avatar
Pieter Wuille committed
1653
1654
1655
{
    LOCK(cs_LastBlockFile);

1656
    CDiskBlockPos posOld(nLastBlockFile, 0);
Pieter Wuille's avatar
Pieter Wuille committed
1657
1658

    FILE *fileOld = OpenBlockFile(posOld);
1659
    if (fileOld) {
1660
1661
        if (fFinalize)
            TruncateFile(fileOld, infoLastBlockFile.nSize);
1662
1663
1664
        FileCommit(fileOld);
        fclose(fileOld);
    }
Pieter Wuille's avatar
Pieter Wuille committed
1665
1666

    fileOld = OpenUndoFile(posOld);
1667
    if (fileOld) {
1668
1669
        if (fFinalize)
            TruncateFile(fileOld, infoLastBlockFile.nUndoSize);
1670
1671
1672
        FileCommit(fileOld);
        fclose(fileOld);
    }
Pieter Wuille's avatar
Pieter Wuille committed
1673
1674
}

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

1677
1678
static CCheckQueue<CScriptCheck> scriptcheckqueue(128);

1679
void ThreadScriptCheck() {
1680
1681
1682
1683
    RenameThread("bitcoin-scriptch");
    scriptcheckqueue.Thread();
}

1684
1685
1686
1687
1688
1689
static int64_t nTimeVerify = 0;
static int64_t nTimeConnect = 0;
static int64_t nTimeIndex = 0;
static int64_t nTimeCallbacks = 0;
static int64_t nTimeTotal = 0;

1690
bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck)
s_nakamoto's avatar
s_nakamoto committed
1691
{
1692
    AssertLockHeld(cs_main);
s_nakamoto's avatar
s_nakamoto committed
1693
    // Check it again in case a previous version let a bad block in
1694
    if (!CheckBlock(block, state, !fJustCheck, !fJustCheck))
s_nakamoto's avatar
s_nakamoto committed
1695
1696
        return false;

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

1701
1702
    // Special case for the genesis block, skipping connection of its transactions
    // (its coinbase is unspendable)
1703
    if (block.GetHash() == Params().HashGenesisBlock()) {
1704
        view.SetBestBlock(pindex->GetBlockHash());
1705
1706
1707
        return true;
    }

1708
1709
    bool fScriptChecks = pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate();

1710
1711
1712
1713
1714
1715
1716
    // 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
1717
    // already refuses previously-known transaction ids entirely.
1718
1719
1720
1721
    // 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.
1722
1723
    bool fEnforceBIP30 = (!pindex->phashBlock) || // Enforce on CreateNewBlock invocations which don't have a hash.
                          !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
1724
                           (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
Pieter Wuille's avatar
Pieter Wuille committed
1725
    if (fEnforceBIP30) {
1726
1727
        BOOST_FOREACH(const CTransaction& tx, block.vtx) {
            const uint256& hash = tx.GetHash();
Pieter Wuille's avatar
Pieter Wuille committed
1728
            if (view.HaveCoins(hash) && !view.GetCoins(hash).IsPruned())
Gavin Andresen's avatar
Gavin Andresen committed
1729
                return state.DoS(100, error("ConnectBlock() : tried to overwrite transaction"),
1730
                                 REJECT_INVALID, "bad-txns-BIP30");
Pieter Wuille's avatar
Pieter Wuille committed
1731
1732
        }
    }
1733

1734
    // BIP16 didn't become active until Apr 1 2012
1735
    int64_t nBIP16SwitchTime = 1333238400;
jtimon's avatar
jtimon committed
1736
    bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime);
1737

1738
1739
1740
    unsigned int flags = SCRIPT_VERIFY_NOCACHE |
                         (fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE);

Pieter Wuille's avatar
Pieter Wuille committed
1741
1742
    CBlockUndo blockundo;

1743
1744
    CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);

1745
    int64_t nTimeStart = GetTimeMicros();
1746
    int64_t nFees = 0;
1747
    int nInputs = 0;
1748
    unsigned int nSigOps = 0;
1749
    CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
1750
    std::vector<std::pair<uint256, CDiskTxPos> > vPos;
1751
1752
    vPos.reserve(block.vtx.size());
    for (unsigned int i = 0; i < block.vtx.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
1753
    {
1754
        const CTransaction &tx = block.vtx[i];
Pieter Wuille's avatar
Pieter Wuille committed
1755

1756
        nInputs += tx.vin.size();
1757
        nSigOps += GetLegacySigOpCount(tx);
1758
        if (nSigOps > MAX_BLOCK_SIGOPS)
Gavin Andresen's avatar
Gavin Andresen committed
1759
            return state.DoS(100, error("ConnectBlock() : too many sigops"),
1760
                             REJECT_INVALID, "bad-blk-sigops");
1761

1762
1763
        if (!tx.IsCoinBase())
        {
1764
            if (!view.HaveInputs(tx))
Gavin Andresen's avatar
Gavin Andresen committed
1765
                return state.DoS(100, error("ConnectBlock() : inputs missing/spent"),
1766
                                 REJECT_INVALID, "bad-txns-inputs-missingorspent");
1767

1768
1769
1770
1771
1772
            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.
1773
                nSigOps += GetP2SHSigOpCount(tx, view);
1774
                if (nSigOps > MAX_BLOCK_SIGOPS)
Gavin Andresen's avatar
Gavin Andresen committed
1775
                    return state.DoS(100, error("ConnectBlock() : too many sigops"),
1776
                                     REJECT_INVALID, "bad-blk-sigops");
1777
            }
1778

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

1781
            std::vector<CScriptCheck> vChecks;
1782
            if (!CheckInputs(tx, state, view, fScriptChecks, flags, nScriptCheckThreads ? &vChecks : NULL))
1783
                return false;
1784
            control.Add(vChecks);
1785
1786
        }

Pieter Wuille's avatar
Pieter Wuille committed
1787
        CTxUndo txundo;
1788
        UpdateCoins(tx, state, view, txundo, pindex->nHeight);
Pieter Wuille's avatar
Pieter Wuille committed
1789
1790
        if (!tx.IsCoinBase())
            blockundo.vtxundo.push_back(txundo);
1791

1792
        vPos.push_back(std::make_pair(tx.GetHash(), pos));
1793
        pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
s_nakamoto's avatar
s_nakamoto committed
1794
    }
1795
1796
    int64_t nTime1 = GetTimeMicros(); nTimeConnect += nTime1 - nTimeStart;
    LogPrint("bench", "      - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime1 - nTimeStart), 0.001 * (nTime1 - nTimeStart) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime1 - nTimeStart) / (nInputs-1), nTimeConnect * 0.000001);
Gavin Andresen's avatar
Gavin Andresen committed
1797

1798
    if (block.vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
Gavin Andresen's avatar
Gavin Andresen committed
1799
        return state.DoS(100,
1800
                         error("ConnectBlock() : coinbase pays too much (actual=%d vs limit=%d)",
1801
                               block.vtx[0].GetValueOut(), GetBlockValue(pindex->nHeight, nFees)),
Philip Kaufmann's avatar
Philip Kaufmann committed
1802
                               REJECT_INVALID, "bad-cb-amount");
Pieter Wuille's avatar
Pieter Wuille committed
1803

1804
    if (!control.Wait())
Pieter Wuille's avatar
Pieter Wuille committed
1805
        return state.DoS(100, false);
1806
1807
    int64_t nTime2 = GetTimeMicros(); nTimeVerify += nTime2 - nTimeStart;
    LogPrint("bench", "    - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs]\n", nInputs - 1, 0.001 * (nTime2 - nTimeStart), nInputs <= 1 ? 0 : 0.001 * (nTime2 - nTimeStart) / (nInputs-1), nTimeVerify * 0.000001);
1808

1809
1810
1811
    if (fJustCheck)
        return true;

1812
1813
1814
1815
1816
    // Correct transaction counts.
    pindex->nTx = block.vtx.size();
    if (pindex->pprev)
        pindex->nChainTx = pindex->pprev->nChainTx + block.vtx.size();

Pieter Wuille's avatar
Pieter Wuille committed
1817
    // Write undo information to disk
1818
    if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS))
Pieter Wuille's avatar
Pieter Wuille committed
1819
    {
1820
1821
        if (pindex->GetUndoPos().IsNull()) {
            CDiskBlockPos pos;
Pieter Wuille's avatar
Pieter Wuille committed
1822
            if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
1823
                return error("ConnectBlock() : FindUndoPos failed");
Pieter Wuille's avatar
Pieter Wuille committed
1824
            if (!blockundo.WriteToDisk(pos, pindex->pprev->GetBlockHash()))
Pieter Wuille's avatar
Pieter Wuille committed
1825
                return state.Abort(_("Failed to write undo data"));
1826
1827
1828
1829
1830
1831

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

1832
        pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
1833

Pieter Wuille's avatar
Pieter Wuille committed
1834
        CDiskBlockIndex blockindex(pindex);
1835
        if (!pblocktree->WriteBlockIndex(blockindex))
Pieter Wuille's avatar
Pieter Wuille committed
1836
            return state.Abort(_("Failed to write block index"));
s_nakamoto's avatar
s_nakamoto committed
1837
1838
    }

1839
    if (fTxIndex)
Pieter Wuille's avatar
Pieter Wuille committed
1840
        if (!pblocktree->WriteTxIndex(vPos))
Pieter Wuille's avatar
Pieter Wuille committed
1841
            return state.Abort(_("Failed to write transaction index"));
1842

1843
    // add this block to the view's block chain
1844
1845
1846
    bool ret;
    ret = view.SetBestBlock(pindex->GetBlockHash());
    assert(ret);
Pieter Wuille's avatar
Pieter Wuille committed
1847

1848
1849
1850
    int64_t nTime3 = GetTimeMicros(); nTimeIndex += nTime3 - nTime2;
    LogPrint("bench", "    - Index writing: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeIndex * 0.000001);

s_nakamoto's avatar
s_nakamoto committed
1851
    // Watch for transactions paying to me
1852
1853
    BOOST_FOREACH(const CTransaction& tx, block.vtx)
        g_signals.SyncTransaction(tx, &block);
s_nakamoto's avatar
s_nakamoto committed
1854

1855
1856
1857
    // Watch for changes to the previous coinbase transaction.
    static uint256 hashPrevBestCoinBase;
    g_signals.UpdatedTransaction(hashPrevBestCoinBase);
1858
    hashPrevBestCoinBase = block.vtx[0].GetHash();
1859

1860
1861
1862
    int64_t nTime4 = GetTimeMicros(); nTimeCallbacks += nTime4 - nTime3;
    LogPrint("bench", "    - Callbacks: %.2fms [%.2fs]\n", 0.001 * (nTime4 - nTime3), nTimeCallbacks * 0.000001);

s_nakamoto's avatar
s_nakamoto committed
1863
1864
1865
    return true;
}

1866
// Update the on-disk chain state.
1867
bool static WriteChainState(CValidationState &state) {
1868
    static int64_t nLastWrite = 0;
1869
    if (pcoinsTip->GetCacheSize() > nCoinCacheSize || (!IsInitialBlockDownload() && GetTimeMicros() > nLastWrite + 600*1000000)) {
1870
1871
1872
1873
1874
1875
        // 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()))
1876
            return state.Error("out of disk space");
Pieter Wuille's avatar
Pieter Wuille committed
1877
        FlushBlockFile();
1878
        pblocktree->Sync();
Pieter Wuille's avatar
Pieter Wuille committed
1879
        if (!pcoinsTip->Flush())
Pieter Wuille's avatar
Pieter Wuille committed
1880
            return state.Abort(_("Failed to write to coin database"));
1881
        nLastWrite = GetTimeMicros();
Pieter Wuille's avatar
Pieter Wuille committed
1882
    }
1883
1884
    return true;
}
Pieter Wuille's avatar
Pieter Wuille committed
1885

1886
// Update chainActive and related internal data structures.
1887
void static UpdateTip(CBlockIndex *pindexNew) {
1888
    chainActive.SetTip(pindexNew);
s_nakamoto's avatar
s_nakamoto committed
1889

1890
    // Update best block in wallet (so we can detect restored wallets)
1891
1892
1893
    bool fIsInitialDownload = IsInitialBlockDownload();
    if ((chainActive.Height() % 20160) == 0 || (!fIsInitialDownload && (chainActive.Height() % 144) == 0))
        g_signals.SetBestChain(chainActive.GetLocator());
1894

s_nakamoto's avatar
s_nakamoto committed
1895
1896
    // New best block
    nTimeBestReceived = GetTime();
1897
    mempool.AddTransactionsUpdated(1);
1898

1899
1900
    LogPrintf("UpdateTip: new best=%s  height=%d  log2_work=%.8g  tx=%lu  date=%s progress=%f\n",
      chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
1901
      DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
1902
      Checkpoints::GuessVerificationProgress(chainActive.Tip()));
s_nakamoto's avatar
s_nakamoto committed
1903

1904
1905
    cvBlockChange.notify_all();

1906
1907
1908
1909
    // Check the version of the last 100 blocks to see if we need to upgrade:
    if (!fIsInitialDownload)
    {
        int nUpgraded = 0;
1910
        const CBlockIndex* pindex = chainActive.Tip();
1911
1912
1913
1914
1915
1916
1917
        for (int i = 0; i < 100 && pindex != NULL; i++)
        {
            if (pindex->nVersion > CBlock::CURRENT_VERSION)
                ++nUpgraded;
            pindex = pindex->pprev;
        }
        if (nUpgraded > 0)
1918
            LogPrintf("SetBestChain: %d of last 100 blocks above version %d\n", nUpgraded, (int)CBlock::CURRENT_VERSION);
1919
1920
        if (nUpgraded > 100/2)
            // strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
1921
            strMiscWarning = _("Warning: This version is obsolete, upgrade required!");
1922
    }
1923
}
1924

1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
// Disconnect chainActive's tip.
bool static DisconnectTip(CValidationState &state) {
    CBlockIndex *pindexDelete = chainActive.Tip();
    assert(pindexDelete);
    mempool.check(pcoinsTip);
    // Read block from disk.
    CBlock block;
    if (!ReadBlockFromDisk(block, pindexDelete))
        return state.Abort(_("Failed to read block"));
    // Apply the block atomically to the chain state.
    int64_t nStart = GetTimeMicros();
1936
    {
1937
1938
1939
1940
        CCoinsViewCache view(*pcoinsTip, true);
        if (!DisconnectBlock(block, state, pindexDelete, view))
            return error("DisconnectTip() : DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
        assert(view.Flush());
1941
    }
1942
    LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
1943
1944
1945
    // Write the chain state to disk, if necessary.
    if (!WriteChainState(state))
        return false;
Gavin Andresen's avatar
Gavin Andresen committed
1946
    // Resurrect mempool transactions from the disconnected block.
1947
1948
    BOOST_FOREACH(const CTransaction &tx, block.vtx) {
        // ignore validation errors in resurrected transactions
Gavin Andresen's avatar
Gavin Andresen committed
1949
        list<CTransaction> removed;
1950
        CValidationState stateDummy;
1951
1952
        if (!tx.IsCoinBase())
            if (!AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL))
Gavin Andresen's avatar
Gavin Andresen committed
1953
                mempool.remove(tx, removed, true);
1954
1955
1956
1957
    }
    mempool.check(pcoinsTip);
    // Update chainActive and related variables.
    UpdateTip(pindexDelete->pprev);
Gavin Andresen's avatar
Gavin Andresen committed
1958
1959
1960
    // Let wallets know transactions went from 1-confirmed to
    // 0-confirmed or conflicted:
    BOOST_FOREACH(const CTransaction &tx, block.vtx) {
1961
        SyncWithWallets(tx, NULL);
Gavin Andresen's avatar
Gavin Andresen committed
1962
    }
1963
    return true;
1964
}
1965

1966
1967
1968
1969
1970
1971
static int64_t nTimeReadFromDisk = 0;
static int64_t nTimeConnectTotal = 0;
static int64_t nTimeFlush = 0;
static int64_t nTimeChainState = 0;
static int64_t nTimePostConnect = 0;

1972
1973
1974
// Connect a new block to chainActive.
bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew) {
    assert(pindexNew->pprev == chainActive.Tip());
1975
    mempool.check(pcoinsTip);
1976
    // Read block from disk.
1977
    int64_t nTime1 = GetTimeMicros();
1978
1979
1980
1981
    CBlock block;
    if (!ReadBlockFromDisk(block, pindexNew))
        return state.Abort(_("Failed to read block"));
    // Apply the block atomically to the chain state.
1982
1983
1984
    int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
    int64_t nTime3;
    LogPrint("bench", "  - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
s_nakamoto's avatar
s_nakamoto committed
1985
    {
1986
1987
1988
1989
1990
1991
        CCoinsViewCache view(*pcoinsTip, true);
        CInv inv(MSG_BLOCK, pindexNew->GetBlockHash());
        if (!ConnectBlock(block, state, pindexNew, view)) {
            if (state.IsInvalid())
                InvalidBlockFound(pindexNew, state);
            return error("ConnectTip() : ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
1992
        }
1993
        mapBlockSource.erase(inv.hash);
1994
1995
        nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
        LogPrint("bench", "  - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001);
1996
        assert(view.Flush());
s_nakamoto's avatar
s_nakamoto committed
1997
    }
1998
1999
    int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
    LogPrint("bench", "  - Flush: %.2fms [%.2fs]\n", (nTime4 - nTime3) * 0.001, nTimeFlush * 0.000001);
2000
2001
2002
    // Write the chain state to disk, if necessary.
    if (!WriteChainState(state))
        return false;
2003
2004
    int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
    LogPrint("bench", "  - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
2005
    // Remove conflicting transactions from the mempool.
Gavin Andresen's avatar
Gavin Andresen committed
2006
    list<CTransaction> txConflicted;
2007
    mempool.removeForBlock(block.vtx, pindexNew->nHeight, txConflicted);
2008
2009
2010
    mempool.check(pcoinsTip);
    // Update chainActive & related variables.
    UpdateTip(pindexNew);
Gavin Andresen's avatar
Gavin Andresen committed
2011
2012
2013
    // Tell wallet about transactions that went from mempool
    // to conflicted:
    BOOST_FOREACH(const CTransaction &tx, txConflicted) {
2014
        SyncWithWallets(tx, NULL);
Gavin Andresen's avatar
Gavin Andresen committed
2015
2016
2017
    }
    // ... and about transactions that got confirmed:
    BOOST_FOREACH(const CTransaction &tx, block.vtx) {
2018
        SyncWithWallets(tx, &block);
Gavin Andresen's avatar
Gavin Andresen committed
2019
    }
2020
2021
2022
    int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
    LogPrint("bench", "  - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
    LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001);
s_nakamoto's avatar
s_nakamoto committed
2023
2024
2025
    return true;
}

2026
// Return the tip of the chain with the most work in it, that isn't
2027
// known to be invalid (it's however far from certain to be valid).
2028
static CBlockIndex* FindMostWorkChain() {
2029
    do {
2030
2031
        CBlockIndex *pindexNew = NULL;

2032
2033
2034
2035
        // Find the best candidate header.
        {
            std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexValid.rbegin();
            if (it == setBlockIndexValid.rend())
2036
                return NULL;
2037
2038
2039
2040
2041
2042
2043
2044
            pindexNew = *it;
        }

        // Check whether all blocks on the path between the currently active chain and the candidate are valid.
        // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
        CBlockIndex *pindexTest = pindexNew;
        bool fInvalidAncestor = false;
        while (pindexTest && !chainActive.Contains(pindexTest)) {
2045
            if (pindexTest->nStatus & BLOCK_FAILED_MASK) {
2046
2047
                // Candidate has an invalid ancestor, remove entire chain from the set.
                if (pindexBestInvalid == NULL || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
2048
2049
                    pindexBestInvalid = pindexNew;
                CBlockIndex *pindexFailed = pindexNew;
2050
2051
2052
2053
2054
                while (pindexTest != pindexFailed) {
                    pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
                    setBlockIndexValid.erase(pindexFailed);
                    pindexFailed = pindexFailed->pprev;
                }
2055
                setBlockIndexValid.erase(pindexTest);
2056
2057
                fInvalidAncestor = true;
                break;
Pieter Wuille's avatar
Pieter Wuille committed
2058
            }
2059
            pindexTest = pindexTest->pprev;
s_nakamoto's avatar
s_nakamoto committed
2060
        }
2061
2062
        if (!fInvalidAncestor)
            return pindexNew;
2063
2064
    } while(true);
}
s_nakamoto's avatar
s_nakamoto committed
2065

2066
2067
2068
// Try to make some progress towards making pindexMostWork the active block.
static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork) {
    AssertLockHeld(cs_main);
2069
    bool fInvalidFound = false;
2070
2071
    const CBlockIndex *pindexOldTip = chainActive.Tip();
    const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
s_nakamoto's avatar
s_nakamoto committed
2072

2073
2074
2075
2076
2077
    // Disconnect active blocks which are no longer in the best chain.
    while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
        if (!DisconnectTip(state))
            return false;
    }
2078

2079
2080
2081
2082
2083
2084
2085
    // Build list of new blocks to connect.
    std::vector<CBlockIndex*> vpindexToConnect;
    vpindexToConnect.reserve(pindexMostWork->nHeight - (pindexFork ? pindexFork->nHeight : -1));
    while (pindexMostWork && pindexMostWork != pindexFork) {
        vpindexToConnect.push_back(pindexMostWork);
        pindexMostWork = pindexMostWork->pprev;
    }
2086

2087
2088
2089
2090
2091
2092
2093
2094
    // Connect new blocks.
    BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
        if (!ConnectTip(state, pindexConnect)) {
            if (state.IsInvalid()) {
                // The block violates a consensus rule.
                if (!state.CorruptionPossible())
                    InvalidChainFound(vpindexToConnect.back());
                state = CValidationState();
2095
                fInvalidFound = true;
2096
2097
2098
2099
2100
2101
                break;
            } else {
                // A system error occurred (disk space, database error, ...).
                return false;
            }
        } else {
2102
2103
2104
2105
2106
2107
2108
2109
2110
            // Delete all entries in setBlockIndexValid that are worse than our new current block.
            // Note that we can't delete the current block itself, as we may need to return to it later in case a
            // reorganization to a better block fails.
            std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexValid.begin();
            while (setBlockIndexValid.value_comp()(*it, chainActive.Tip())) {
                setBlockIndexValid.erase(it++);
            }
            // Either the current tip or a successor of it we're working towards is left in setBlockIndexValid.
            assert(!setBlockIndexValid.empty());
2111
2112
2113
            if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
                // We're in a better position than we were. Return temporarily to release the lock.
                break;
2114
2115
            }
        }
2116
    }
s_nakamoto's avatar
s_nakamoto committed
2117

2118
2119
2120
2121
2122
2123
2124
2125
    // Callbacks/notifications for a new best chain.
    if (fInvalidFound)
        CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
    else
        CheckForkWarningConditions();

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

s_nakamoto's avatar
s_nakamoto committed
2127
2128
2129
    return true;
}

2130
bool ActivateBestChain(CValidationState &state) {
2131
2132
    CBlockIndex *pindexNewTip = NULL;
    CBlockIndex *pindexMostWork = NULL;
2133
2134
2135
    do {
        boost::this_thread::interruption_point();

2136
2137
2138
2139
        bool fInitialDownload;
        {
            LOCK(cs_main);
            pindexMostWork = FindMostWorkChain();
2140

2141
2142
2143
            // Whether we have anything to do at all.
            if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
                return true;
2144

2145
2146
            if (!ActivateBestChainStep(state, pindexMostWork))
                return false;
2147

2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
            pindexNewTip = chainActive.Tip();
            fInitialDownload = IsInitialBlockDownload();
        }
        // When we reach this point, we switched to a new tip (stored in pindexNewTip).

        // Notifications/callbacks that can run without cs_main
        if (!fInitialDownload) {
            uint256 hashNewTip = pindexNewTip->GetBlockHash();
            // Relay inventory, but don't relay old inventory during initial block download.
            int nBlockEstimate = Checkpoints::GetTotalBlocksEstimate();
            LOCK(cs_vNodes);
            BOOST_FOREACH(CNode* pnode, vNodes)
                if (chainActive.Height() > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate))
                    pnode->PushInventory(CInv(MSG_BLOCK, hashNewTip));

            std::string strCmd = GetArg("-blocknotify", "");
            if (!strCmd.empty()) {
                boost::replace_all(strCmd, "%s", hashNewTip.GetHex());
                boost::thread t(runCommand, strCmd); // thread runs free
            }
        }
        uiInterface.NotifyBlocksChanged();
    } while(pindexMostWork != chainActive.Tip());
2171
2172
2173

    return true;
}
2174
2175

CBlockIndex* AddToBlockIndex(CBlockHeader& block)
s_nakamoto's avatar
s_nakamoto committed
2176
2177
{
    // Check for duplicate
2178
    uint256 hash = block.GetHash();
2179
2180
2181
    std::map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(hash);
    if (it != mapBlockIndex.end())
        return it->second;
s_nakamoto's avatar
s_nakamoto committed
2182
2183

    // Construct new block index object
2184
    CBlockIndex* pindexNew = new CBlockIndex(block);
2185
    assert(pindexNew);
2186
2187
2188
2189
    {
         LOCK(cs_nBlockSequenceId);
         pindexNew->nSequenceId = nBlockSequenceId++;
    }
s_nakamoto's avatar
s_nakamoto committed
2190
2191
    map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
    pindexNew->phashBlock = &((*mi).first);
2192
    map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
s_nakamoto's avatar
s_nakamoto committed
2193
2194
2195
2196
    if (miPrev != mapBlockIndex.end())
    {
        pindexNew->pprev = (*miPrev).second;
        pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
2197
        pindexNew->BuildSkip();
s_nakamoto's avatar
s_nakamoto committed
2198
    }
2199
    pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + pindexNew->GetBlockWork();
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
    pindexNew->RaiseValidity(BLOCK_VALID_TREE);

    return pindexNew;
}

// Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).
bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos)
{
    pindexNew->nTx = block.vtx.size();
    if (pindexNew->pprev) {
        // Not the genesis block.
        if (pindexNew->pprev->nChainTx) {
            // This parent's block's total number transactions is known, so compute outs.
            pindexNew->nChainTx = pindexNew->pprev->nChainTx + pindexNew->nTx;
        } else {
            // The total number of transactions isn't known yet.
            // We will compute it when the block is connected.
            pindexNew->nChainTx = 0;
        }
    } else {
        // Genesis block.
        pindexNew->nChainTx = pindexNew->nTx;
    }
2223
2224
    pindexNew->nFile = pos.nFile;
    pindexNew->nDataPos = pos.nPos;
Pieter Wuille's avatar
Pieter Wuille committed
2225
    pindexNew->nUndoPos = 0;
2226
2227
2228
2229
    pindexNew->nStatus |= BLOCK_HAVE_DATA;

    if (pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS))
        setBlockIndexValid.insert(pindexNew);
s_nakamoto's avatar
s_nakamoto committed
2230

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

2234
    return true;
s_nakamoto's avatar
s_nakamoto committed
2235
2236
}

2237
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
2238
2239
2240
2241
2242
{
    bool fUpdatedLast = false;

    LOCK(cs_LastBlockFile);

2243
2244
2245
2246
2247
    if (fKnown) {
        if (nLastBlockFile != pos.nFile) {
            nLastBlockFile = pos.nFile;
            infoLastBlockFile.SetNull();
            pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile);
2248
            fUpdatedLast = true;
2249
2250
2251
        }
    } else {
        while (infoLastBlockFile.nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
2252
            LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, infoLastBlockFile.ToString());
2253
            FlushBlockFile(true);
2254
2255
2256
2257
2258
2259
2260
            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
2261
2262
2263
2264
2265
    }

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

2266
2267
2268
2269
    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) {
2270
2271
2272
            if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
                FILE *file = OpenBlockFile(pos);
                if (file) {
2273
                    LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
2274
2275
2276
                    AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
                    fclose(file);
                }
2277
            }
2278
            else
2279
                return state.Error("out of disk space");
2280
2281
2282
        }
    }

2283
    if (!pblocktree->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
Pieter Wuille's avatar
Pieter Wuille committed
2284
        return state.Abort(_("Failed to write file info"));
Pieter Wuille's avatar
Pieter Wuille committed
2285
    if (fUpdatedLast)
2286
        pblocktree->WriteLastBlockFile(nLastBlockFile);
Pieter Wuille's avatar
Pieter Wuille committed
2287
2288
2289
2290

    return true;
}

Pieter Wuille's avatar
Pieter Wuille committed
2291
bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
Pieter Wuille's avatar
Pieter Wuille committed
2292
2293
2294
2295
2296
{
    pos.nFile = nFile;

    LOCK(cs_LastBlockFile);

2297
    unsigned int nNewSize;
Pieter Wuille's avatar
Pieter Wuille committed
2298
2299
    if (nFile == nLastBlockFile) {
        pos.nPos = infoLastBlockFile.nUndoSize;
2300
        nNewSize = (infoLastBlockFile.nUndoSize += nAddSize);
2301
        if (!pblocktree->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
Pieter Wuille's avatar
Pieter Wuille committed
2302
            return state.Abort(_("Failed to write block info"));
2303
2304
    } else {
        CBlockFileInfo info;
2305
        if (!pblocktree->ReadBlockFileInfo(nFile, info))
Pieter Wuille's avatar
Pieter Wuille committed
2306
            return state.Abort(_("Failed to read block info"));
2307
2308
        pos.nPos = info.nUndoSize;
        nNewSize = (info.nUndoSize += nAddSize);
2309
        if (!pblocktree->WriteBlockFileInfo(nFile, info))
Pieter Wuille's avatar
Pieter Wuille committed
2310
            return state.Abort(_("Failed to write block info"));
2311
2312
2313
2314
2315
    }

    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) {
2316
2317
2318
        if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
            FILE *file = OpenUndoFile(pos);
            if (file) {
2319
                LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
2320
2321
2322
                AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
                fclose(file);
            }
2323
        }
2324
        else
2325
            return state.Error("out of disk space");
Pieter Wuille's avatar
Pieter Wuille committed
2326
2327
2328
2329
2330
    }

    return true;
}

2331
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW)
s_nakamoto's avatar
s_nakamoto committed
2332
{
2333
    // Check proof of work matches claimed amount
2334
    if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits))
2335
        return state.DoS(50, error("CheckBlockHeader() : proof of work failed"),
2336
                         REJECT_INVALID, "high-hash");
2337

s_nakamoto's avatar
s_nakamoto committed
2338
    // Check timestamp
2339
    if (block.GetBlockTime() > GetAdjustedTime() + 2 * 60 * 60)
2340
        return state.Invalid(error("CheckBlockHeader() : block timestamp too far in the future"),
2341
                             REJECT_INVALID, "time-too-new");
s_nakamoto's avatar
s_nakamoto committed
2342

2343
2344
2345
    return true;
}

2346
bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot)
s_nakamoto's avatar
s_nakamoto committed
2347
2348
2349
2350
{
    // These are checks that are independent of context
    // that can be verified before saving an orphan block.

2351
2352
2353
    if (!CheckBlockHeader(block, state, fCheckPOW))
        return false;

s_nakamoto's avatar
s_nakamoto committed
2354
    // Size limits
2355
    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
2356
        return state.DoS(100, error("CheckBlock() : size limits failed"),
2357
                         REJECT_INVALID, "bad-blk-length");
s_nakamoto's avatar
s_nakamoto committed
2358
2359

    // First transaction must be coinbase, the rest must not be
2360
    if (block.vtx.empty() || !block.vtx[0].IsCoinBase())
Gavin Andresen's avatar
Gavin Andresen committed
2361
        return state.DoS(100, error("CheckBlock() : first tx is not coinbase"),
2362
                         REJECT_INVALID, "bad-cb-missing");
2363
2364
    for (unsigned int i = 1; i < block.vtx.size(); i++)
        if (block.vtx[i].IsCoinBase())
Gavin Andresen's avatar
Gavin Andresen committed
2365
            return state.DoS(100, error("CheckBlock() : more than one coinbase"),
2366
                             REJECT_INVALID, "bad-cb-multiple");
s_nakamoto's avatar
s_nakamoto committed
2367
2368

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

2373
2374
2375
    // Check for duplicate txids. This is caught by ConnectInputs(),
    // but catching it earlier avoids a potential DoS attack:
    set<uint256> uniqueTx;
2376
2377
    BOOST_FOREACH(const CTransaction &tx, block.vtx) {
        uniqueTx.insert(tx.GetHash());
2378
    }
2379
    if (uniqueTx.size() != block.vtx.size())
Gavin Andresen's avatar
Gavin Andresen committed
2380
        return state.DoS(100, error("CheckBlock() : duplicate transaction"),
2381
                         REJECT_INVALID, "bad-txns-duplicate", true);
2382

2383
    unsigned int nSigOps = 0;
2384
    BOOST_FOREACH(const CTransaction& tx, block.vtx)
Gavin Andresen's avatar
Gavin Andresen committed
2385
    {
2386
        nSigOps += GetLegacySigOpCount(tx);
Gavin Andresen's avatar
Gavin Andresen committed
2387
2388
    }
    if (nSigOps > MAX_BLOCK_SIGOPS)
Gavin Andresen's avatar
Gavin Andresen committed
2389
        return state.DoS(100, error("CheckBlock() : out-of-bounds SigOpCount"),
2390
                         REJECT_INVALID, "bad-blk-sigops", true);
s_nakamoto's avatar
s_nakamoto committed
2391

2392
    // Check merkle root
2393
    if (fCheckMerkleRoot && block.hashMerkleRoot != block.BuildMerkleTree())
Gavin Andresen's avatar
Gavin Andresen committed
2394
        return state.DoS(100, error("CheckBlock() : hashMerkleRoot mismatch"),
2395
                         REJECT_INVALID, "bad-txnmrklroot", true);
s_nakamoto's avatar
s_nakamoto committed
2396
2397
2398
2399

    return true;
}

2400
bool AcceptBlockHeader(CBlockHeader& block, CValidationState& state, CBlockIndex** ppindex)
s_nakamoto's avatar
s_nakamoto committed
2401
{
2402
    AssertLockHeld(cs_main);
s_nakamoto's avatar
s_nakamoto committed
2403
    // Check for duplicate
2404
    uint256 hash = block.GetHash();
2405
2406
2407
2408
2409
2410
2411
    std::map<uint256, CBlockIndex*>::iterator miSelf = mapBlockIndex.find(hash);
    CBlockIndex *pindex = NULL;
    if (miSelf != mapBlockIndex.end()) {
        pindex = miSelf->second;
        if (pindex->nStatus & BLOCK_FAILED_MASK)
            return state.Invalid(error("AcceptBlock() : block is marked invalid"), 0, "duplicate");
    }
s_nakamoto's avatar
s_nakamoto committed
2412

2413
2414
2415
2416
    CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(mapBlockIndex);
    if (pcheckpoint && block.hashPrevBlock != (chainActive.Tip() ? chainActive.Tip()->GetBlockHash() : uint256(0)))
    {
        // Extra checks to prevent "fill up memory by spamming with bogus blocks"
jtimon's avatar
jtimon committed
2417
        int64_t deltaTime = block.GetBlockTime() - pcheckpoint->GetBlockTime();
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
        if (deltaTime < 0)
        {
            return state.DoS(100, error("CheckBlockHeader() : block with timestamp before last checkpoint"),
                             REJECT_CHECKPOINT, "time-too-old");
        }
        bool fOverflow = false;
        uint256 bnNewBlock;
        bnNewBlock.SetCompact(block.nBits, NULL, &fOverflow);
        uint256 bnRequired;
        bnRequired.SetCompact(ComputeMinWork(pcheckpoint->nBits, deltaTime));
        if (fOverflow || bnNewBlock > bnRequired)
        {
            return state.DoS(100, error("CheckBlockHeader() : block with too little proof-of-work"),
                             REJECT_INVALID, "bad-diffbits");
        }
    }

s_nakamoto's avatar
s_nakamoto committed
2435
    // Get prev block index
2436
2437
    CBlockIndex* pindexPrev = NULL;
    int nHeight = 0;
2438
    if (hash != Params().HashGenesisBlock()) {
2439
        map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
2440
        if (mi == mapBlockIndex.end())
2441
            return state.DoS(10, error("AcceptBlock() : prev block not found"), 0, "bad-prevblk");
2442
2443
2444
2445
        pindexPrev = (*mi).second;
        nHeight = pindexPrev->nHeight+1;

        // Check proof of work
2446
        if (block.nBits != GetNextWorkRequired(pindexPrev, &block))
Gavin Andresen's avatar
Gavin Andresen committed
2447
            return state.DoS(100, error("AcceptBlock() : incorrect proof of work"),
2448
                             REJECT_INVALID, "bad-diffbits");
2449
2450

        // Check timestamp against prev
2451
        if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
Gavin Andresen's avatar
Gavin Andresen committed
2452
            return state.Invalid(error("AcceptBlock() : block's timestamp is too early"),
2453
                                 REJECT_INVALID, "time-too-old");
2454
2455
2456

        // 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
2457
2458
            return state.DoS(100, error("AcceptBlock() : rejected by checkpoint lock-in at %d", nHeight),
                             REJECT_CHECKPOINT, "checkpoint mismatch");
2459

2460
2461
2462
2463
2464
        // Don't accept any forks from the main chain prior to last checkpoint
        CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(mapBlockIndex);
        if (pcheckpoint && nHeight < pcheckpoint->nHeight)
            return state.DoS(100, error("AcceptBlock() : forked chain older than last checkpoint (height %d)", nHeight));

2465
        // Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
2466
2467
        if (block.nVersion < 2 && 
            CBlockIndex::IsSuperMajority(2, pindexPrev, Params().RejectBlockOutdatedMajority()))
2468
        {
2469
2470
            return state.Invalid(error("AcceptBlock() : rejected nVersion=1 block"),
                                 REJECT_OBSOLETE, "bad-version");
2471
        }
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
    }

    if (pindex == NULL)
        pindex = AddToBlockIndex(block);

    if (ppindex)
        *ppindex = pindex;

    return true;
}

bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, CDiskBlockPos* dbp)
{
    AssertLockHeld(cs_main);

    CBlockIndex *&pindex = *ppindex;

    if (!AcceptBlockHeader(block, state, &pindex))
        return false;

    if (!CheckBlock(block, state)) {
2493
        if (state.IsInvalid() && !state.CorruptionPossible()) {
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
            pindex->nStatus |= BLOCK_FAILED_VALID;
        }
        return false;
    }

    int nHeight = pindex->nHeight;

    // Check that all transactions are finalized
    BOOST_FOREACH(const CTransaction& tx, block.vtx)
        if (!IsFinalTx(tx, nHeight, block.GetBlockTime())) {
            pindex->nStatus |= BLOCK_FAILED_VALID;
            return state.DoS(10, error("AcceptBlock() : contains a non-final transaction"),
                             REJECT_INVALID, "bad-txns-nonfinal");
        }

    // Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
2510
2511
2512
    // if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
    if (block.nVersion >= 2 && 
        CBlockIndex::IsSuperMajority(2, pindex->pprev, Params().EnforceBlockUpgradeMajority()))
2513
    {
2514
2515
2516
2517
2518
        CScript expect = CScript() << nHeight;
        if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
            !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) {
            pindex->nStatus |= BLOCK_FAILED_VALID;
            return state.DoS(100, error("AcceptBlock() : block height mismatch in coinbase"), REJECT_INVALID, "bad-cb-height");
2519
2520
2521
        }
    }

s_nakamoto's avatar
s_nakamoto committed
2522
    // Write block to history file
Pieter Wuille's avatar
Pieter Wuille committed
2523
    try {
2524
        unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
Pieter Wuille's avatar
Pieter Wuille committed
2525
2526
2527
        CDiskBlockPos blockPos;
        if (dbp != NULL)
            blockPos = *dbp;
jtimon's avatar
jtimon committed
2528
        if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != NULL))
Pieter Wuille's avatar
Pieter Wuille committed
2529
2530
            return error("AcceptBlock() : FindBlockPos failed");
        if (dbp == NULL)
2531
            if (!WriteBlockToDisk(block, blockPos))
Pieter Wuille's avatar
Pieter Wuille committed
2532
                return state.Abort(_("Failed to write block"));
2533
2534
        if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
            return error("AcceptBlock() : ReceivedBlockTransactions failed");
Pieter Wuille's avatar
Pieter Wuille committed
2535
2536
2537
    } catch(std::runtime_error &e) {
        return state.Abort(_("System error: ") + e.what());
    }
s_nakamoto's avatar
s_nakamoto committed
2538
2539
2540
2541

    return true;
}

2542
bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired)
2543
{
2544
    unsigned int nToCheck = Params().ToCheckBlockUpgradeMajority();
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
    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);
}

2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
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
2595
2596
2597
2598
2599
2600
2601
2602
2603
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
int static inline InvertLowestOne(int n) { return n & (n - 1); }

/** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
int static inline GetSkipHeight(int height) {
    if (height < 2)
        return 0;

    // Determine which height to jump back to. Any number strictly lower than height is acceptable,
    // but the following expression seems to perform well in simulations (max 110 steps to go back
    // up to 2**18 blocks).
    return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
}

CBlockIndex* CBlockIndex::GetAncestor(int height)
{
    if (height > nHeight || height < 0)
        return NULL;

    CBlockIndex* pindexWalk = this;
    int heightWalk = nHeight;
    while (heightWalk > height) {
        int heightSkip = GetSkipHeight(heightWalk);
        int heightSkipPrev = GetSkipHeight(heightWalk - 1);
        if (heightSkip == height ||
            (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
                                      heightSkipPrev >= height))) {
            // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
            pindexWalk = pindexWalk->pskip;
            heightWalk = heightSkip;
        } else {
            pindexWalk = pindexWalk->pprev;
            heightWalk--;
        }
    }
    return pindexWalk;
}

const CBlockIndex* CBlockIndex::GetAncestor(int height) const
{
    return const_cast<CBlockIndex*>(this)->GetAncestor(height);
}

void CBlockIndex::BuildSkip()
{
    if (pprev)
        pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
}

2604
2605
void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)
{
2606
    AssertLockHeld(cs_main);
2607
2608
2609
2610
2611
2612
    // Filter out duplicate requests
    if (pindexBegin == pnode->pindexLastGetBlocksBegin && hashEnd == pnode->hashLastGetBlocksEnd)
        return;
    pnode->pindexLastGetBlocksBegin = pindexBegin;
    pnode->hashLastGetBlocksEnd = hashEnd;

2613
    pnode->PushMessage("getblocks", chainActive.GetLocator(pindexBegin), hashEnd);
2614
2615
}

Pieter Wuille's avatar
Pieter Wuille committed
2616
bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
s_nakamoto's avatar
s_nakamoto committed
2617
2618
2619
{
    // Check for duplicate
    uint256 hash = pblock->GetHash();
2620
2621
2622

    {
    LOCK(cs_main);
s_nakamoto's avatar
s_nakamoto committed
2623
    if (mapBlockIndex.count(hash))
2624
        return state.Invalid(error("ProcessBlock() : already have block %d %s", mapBlockIndex[hash]->nHeight, hash.ToString()), 0, "duplicate");
s_nakamoto's avatar
s_nakamoto committed
2625
    if (mapOrphanBlocks.count(hash))
2626
        return state.Invalid(error("ProcessBlock() : already have block (orphan) %s", hash.ToString()), 0, "duplicate");
s_nakamoto's avatar
s_nakamoto committed
2627
2628

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

2632
2633
2634
    // If we don't already have its previous block (with full data), shunt it off to holding area until we get it
    std::map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(pblock->hashPrevBlock);
    if (pblock->hashPrevBlock != 0 && (it == mapBlockIndex.end() || !(it->second->nStatus & BLOCK_HAVE_DATA)))
s_nakamoto's avatar
s_nakamoto committed
2635
    {
2636
        LogPrintf("ProcessBlock: ORPHAN BLOCK %lu, prev=%s\n", (unsigned long)mapOrphanBlocks.size(), pblock->hashPrevBlock.ToString());
s_nakamoto's avatar
s_nakamoto committed
2637

2638
2639
        // Accept orphans as long as there is a node to request its parents from
        if (pfrom) {
2640
            PruneOrphanBlocks();
2641
2642
2643
2644
2645
2646
2647
2648
            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;
2649
            mapOrphanBlocks.insert(make_pair(hash, pblock2));
2650
            mapOrphanBlocksByPrev.insert(make_pair(pblock2->hashPrev, pblock2));
2651
2652

            // Ask this guy to fill in what we're missing
2653
            PushGetBlocks(pfrom, chainActive.Tip(), GetOrphanRoot(hash));
2654
        }
s_nakamoto's avatar
s_nakamoto committed
2655
2656
2657
2658
        return true;
    }

    // Store to disk
2659
2660
2661
    CBlockIndex *pindex = NULL;
    bool ret = AcceptBlock(*pblock, state, &pindex, dbp);
    if (!ret)
s_nakamoto's avatar
s_nakamoto committed
2662
2663
2664
2665
2666
        return error("ProcessBlock() : AcceptBlock FAILED");

    // Recursively process any orphan blocks that depended on this one
    vector<uint256> vWorkQueue;
    vWorkQueue.push_back(hash);
2667
    for (unsigned int i = 0; i < vWorkQueue.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
2668
2669
    {
        uint256 hashPrev = vWorkQueue[i];
2670
        for (multimap<uint256, COrphanBlock*>::iterator mi = mapOrphanBlocksByPrev.lower_bound(hashPrev);
s_nakamoto's avatar
s_nakamoto committed
2671
2672
2673
             mi != mapOrphanBlocksByPrev.upper_bound(hashPrev);
             ++mi)
        {
2674
2675
2676
2677
2678
2679
            CBlock block;
            {
                CDataStream ss(mi->second->vchBlock, SER_DISK, CLIENT_VERSION);
                ss >> block;
            }
            block.BuildMerkleTree();
2680
2681
            // 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;
2682
2683
            CBlockIndex *pindexChild = NULL;
            if (AcceptBlock(block, stateDummy, &pindexChild))
2684
2685
2686
                vWorkQueue.push_back(mi->second->hashBlock);
            mapOrphanBlocks.erase(mi->second->hashBlock);
            delete mi->second;
s_nakamoto's avatar
s_nakamoto committed
2687
2688
2689
2690
        }
        mapOrphanBlocksByPrev.erase(hashPrev);
    }

2691
2692
2693
2694
2695
    }

    if (!ActivateBestChain(state))
        return error("ProcessBlock() : ActivateBestChain failed");

s_nakamoto's avatar
s_nakamoto committed
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
    return true;
}








2706
2707
2708
2709
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter& filter)
{
    header = block.GetBlockHeader();

2710
2711
2712
2713
2714
2715
2716
    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++)
2717
    {
2718
2719
        const uint256& hash = block.vtx[i].GetHash();
        if (filter.IsRelevantAndUpdate(block.vtx[i]))
2720
        {
2721
2722
            vMatch.push_back(true);
            vMatchedTxn.push_back(make_pair(i, hash));
2723
        }
2724
2725
2726
        else
            vMatch.push_back(false);
        vHashes.push_back(hash);
2727
    }
2728
2729

    txn = CPartialMerkleTree(vHashes, vMatch);
2730
2731
2732
2733
2734
2735
2736
2737
2738
}








Pieter Wuille's avatar
Pieter Wuille committed
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
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;
}







2859
2860
bool AbortNode(const std::string &strMessage) {
    strMiscWarning = strMessage;
2861
    LogPrintf("*** %s\n", strMessage);
2862
    uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_ERROR);
2863
2864
2865
    StartShutdown();
    return false;
}
Pieter Wuille's avatar
Pieter Wuille committed
2866

2867
bool CheckDiskSpace(uint64_t nAdditionalBytes)
s_nakamoto's avatar
s_nakamoto committed
2868
{
2869
    uint64_t nFreeBytesAvailable = filesystem::space(GetDataDir()).available;
s_nakamoto's avatar
s_nakamoto committed
2870

2871
2872
    // Check for nMinDiskSpace bytes (currently 50MB)
    if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
2873
2874
        return AbortNode(_("Error: Disk space is low!"));

s_nakamoto's avatar
s_nakamoto committed
2875
2876
2877
    return true;
}

Pieter Wuille's avatar
Pieter Wuille committed
2878
FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
2879
{
Pieter Wuille's avatar
Pieter Wuille committed
2880
    if (pos.IsNull())
s_nakamoto's avatar
s_nakamoto committed
2881
        return NULL;
Pieter Wuille's avatar
Pieter Wuille committed
2882
2883
2884
2885
2886
    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
2887
    if (!file) {
2888
        LogPrintf("Unable to open file %s\n", path.string());
s_nakamoto's avatar
s_nakamoto committed
2889
        return NULL;
Pieter Wuille's avatar
Pieter Wuille committed
2890
    }
Pieter Wuille's avatar
Pieter Wuille committed
2891
2892
    if (pos.nPos) {
        if (fseek(file, pos.nPos, SEEK_SET)) {
2893
            LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
Pieter Wuille's avatar
Pieter Wuille committed
2894
2895
2896
2897
            fclose(file);
            return NULL;
        }
    }
s_nakamoto's avatar
s_nakamoto committed
2898
2899
2900
    return file;
}

Pieter Wuille's avatar
Pieter Wuille committed
2901
2902
2903
2904
FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
    return OpenDiskFile(pos, "blk", fReadOnly);
}

2905
FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
Pieter Wuille's avatar
Pieter Wuille committed
2906
2907
2908
    return OpenDiskFile(pos, "rev", fReadOnly);
}

2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
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
2934
    boost::this_thread::interruption_point();
2935

Pieter Wuille's avatar
Pieter Wuille committed
2936
    // Calculate nChainWork
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
    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;
2948
        pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + pindex->GetBlockWork();
2949
        pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
2950
        if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS))
2951
            setBlockIndexValid.insert(pindex);
2952
2953
        if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
            pindexBestInvalid = pindex;
2954
2955
        if (pindex->pprev)
            pindex->BuildSkip();
2956
2957
2958
2959
    }

    // Load block file info
    pblocktree->ReadLastBlockFile(nLastBlockFile);
2960
    LogPrintf("LoadBlockIndexDB(): last block file = %i\n", nLastBlockFile);
2961
    if (pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
2962
        LogPrintf("LoadBlockIndexDB(): last block file info: %s\n", infoLastBlockFile.ToString());
2963

2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
    // Check presence of blk files
    LogPrintf("Checking all blk files are present...\n");
    set<int> setBlkDataFiles;
    BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
    {
        CBlockIndex* pindex = item.second;
        if (pindex->nStatus & BLOCK_HAVE_DATA) {
            setBlkDataFiles.insert(pindex->nFile);
        }
    }
    for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
    {
        CDiskBlockPos pos(*it, 0);
        if (!CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION)) {
            return false;
        }
    }

2982
2983
2984
2985
2986
    // Check whether we need to continue reindexing
    bool fReindexing = false;
    pblocktree->ReadReindexing(fReindexing);
    fReindex |= fReindexing;

2987
2988
    // Check whether we have a transaction index
    pblocktree->ReadFlag("txindex", fTxIndex);
2989
    LogPrintf("LoadBlockIndexDB(): transaction index %s\n", fTxIndex ? "enabled" : "disabled");
2990

2991
    // Load pointer to end of best chain
2992
2993
    std::map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
    if (it == mapBlockIndex.end())
2994
        return true;
2995
    chainActive.SetTip(it->second);
2996
    LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s progress=%f\n",
2997
        chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
2998
2999
        DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
        Checkpoints::GuessVerificationProgress(chainActive.Tip()));
3000

Pieter Wuille's avatar
Pieter Wuille committed
3001
3002
3003
    return true;
}

Cozz Lovan's avatar
Cozz Lovan committed
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
CVerifyDB::CVerifyDB()
{
    uiInterface.ShowProgress(_("Verifying blocks..."), 0);
}

CVerifyDB::~CVerifyDB()
{
    uiInterface.ShowProgress("", 100);
}

bool CVerifyDB::VerifyDB(int nCheckLevel, int nCheckDepth)
3015
{
3016
    LOCK(cs_main);
3017
    if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
Pieter Wuille's avatar
Pieter Wuille committed
3018
3019
        return true;

3020
    // Verify blocks in the best chain
3021
    if (nCheckDepth <= 0)
3022
        nCheckDepth = 1000000000; // suffices until the year 19000
3023
3024
    if (nCheckDepth > chainActive.Height())
        nCheckDepth = chainActive.Height();
Pieter Wuille's avatar
Pieter Wuille committed
3025
    nCheckLevel = std::max(0, std::min(4, nCheckLevel));
3026
    LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
Pieter Wuille's avatar
Pieter Wuille committed
3027
    CCoinsViewCache coins(*pcoinsTip, true);
3028
    CBlockIndex* pindexState = chainActive.Tip();
Pieter Wuille's avatar
Pieter Wuille committed
3029
3030
    CBlockIndex* pindexFailure = NULL;
    int nGoodTransactions = 0;
Pieter Wuille's avatar
Pieter Wuille committed
3031
    CValidationState state;
3032
    for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
3033
    {
Gavin Andresen's avatar
Gavin Andresen committed
3034
        boost::this_thread::interruption_point();
Cozz Lovan's avatar
Cozz Lovan committed
3035
        uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100)))));
3036
        if (pindex->nHeight < chainActive.Height()-nCheckDepth)
3037
3038
            break;
        CBlock block;
Pieter Wuille's avatar
Pieter Wuille committed
3039
        // check level 0: read from disk
3040
        if (!ReadBlockFromDisk(block, pindex))
3041
            return error("VerifyDB() : *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3042
        // check level 1: verify block validity
3043
        if (nCheckLevel >= 1 && !CheckBlock(block, state))
3044
            return error("VerifyDB() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
Pieter Wuille's avatar
Pieter Wuille committed
3045
3046
3047
3048
3049
3050
        // 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()))
3051
                    return error("VerifyDB() : *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
Pieter Wuille's avatar
Pieter Wuille committed
3052
3053
3054
3055
3056
            }
        }
        // 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;
3057
            if (!DisconnectBlock(block, state, pindex, coins, &fClean))
3058
                return error("VerifyDB() : *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
Pieter Wuille's avatar
Pieter Wuille committed
3059
3060
3061
3062
3063
3064
            pindexState = pindex->pprev;
            if (!fClean) {
                nGoodTransactions = 0;
                pindexFailure = pindex;
            } else
                nGoodTransactions += block.vtx.size();
3065
3066
        }
    }
Pieter Wuille's avatar
Pieter Wuille committed
3067
    if (pindexFailure)
3068
        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
3069
3070
3071
3072

    // check level 4: try reconnecting blocks
    if (nCheckLevel >= 4) {
        CBlockIndex *pindex = pindexState;
3073
        while (pindex != chainActive.Tip()) {
Gavin Andresen's avatar
Gavin Andresen committed
3074
            boost::this_thread::interruption_point();
Cozz Lovan's avatar
Cozz Lovan committed
3075
            uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))));
3076
            pindex = chainActive.Next(pindex);
3077
            CBlock block;
3078
            if (!ReadBlockFromDisk(block, pindex))
3079
                return error("VerifyDB() : *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3080
            if (!ConnectBlock(block, state, pindex, coins))
3081
                return error("VerifyDB() : *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
Pieter Wuille's avatar
Pieter Wuille committed
3082
        }
3083
3084
    }

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

3087
3088
3089
    return true;
}

3090
3091
3092
3093
void UnloadBlockIndex()
{
    mapBlockIndex.clear();
    setBlockIndexValid.clear();
3094
    chainActive.SetTip(NULL);
3095
    pindexBestInvalid = NULL;
3096
3097
}

3098
bool LoadBlockIndex()
s_nakamoto's avatar
s_nakamoto committed
3099
{
3100
    // Load block index from databases
3101
    if (!fReindex && !LoadBlockIndexDB())
s_nakamoto's avatar
s_nakamoto committed
3102
        return false;
3103
3104
    return true;
}
3105
3106


3107
bool InitBlockIndex() {
3108
    LOCK(cs_main);
3109
    // Check whether we're already initialized
3110
    if (chainActive.Genesis() != NULL)
3111
3112
3113
3114
3115
        return true;

    // Use the provided setting for -txindex in the new database
    fTxIndex = GetBoolArg("-txindex", false);
    pblocktree->WriteFlag("txindex", fTxIndex);
3116
    LogPrintf("Initializing databases...\n");
3117
3118
3119
3120

    // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
    if (!fReindex) {
        try {
3121
3122
            CBlock &block = const_cast<CBlock&>(Params().GenesisBlock());
            // Start new block file
3123
3124
3125
            unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
            CDiskBlockPos blockPos;
            CValidationState state;
jtimon's avatar
jtimon committed
3126
            if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
3127
                return error("LoadBlockIndex() : FindBlockPos failed");
3128
            if (!WriteBlockToDisk(block, blockPos))
3129
                return error("LoadBlockIndex() : writing genesis block to disk failed");
3130
3131
            CBlockIndex *pindex = AddToBlockIndex(block);
            if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
3132
                return error("LoadBlockIndex() : genesis block not accepted");
3133
3134
            if (!ActivateBestChain(state))
                return error("LoadBlockIndex() : genesis block cannot be activated");
3135
3136
3137
        } catch(std::runtime_error &e) {
            return error("LoadBlockIndex() : failed to initialize block database: %s", e.what());
        }
s_nakamoto's avatar
s_nakamoto committed
3138
3139
3140
3141
3142
3143
3144
3145
3146
    }

    return true;
}



void PrintBlockTree()
{
3147
    AssertLockHeld(cs_main);
3148
    // pre-compute tree structure
s_nakamoto's avatar
s_nakamoto committed
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
    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;
3160
    vStack.push_back(make_pair(0, chainActive.Genesis()));
s_nakamoto's avatar
s_nakamoto committed
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172

    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++)
3173
3174
                LogPrintf("| ");
            LogPrintf("|\\\n");
s_nakamoto's avatar
s_nakamoto committed
3175
3176
3177
3178
        }
        else if (nCol < nPrevCol)
        {
            for (int i = 0; i < nCol; i++)
3179
3180
                LogPrintf("| ");
            LogPrintf("|\n");
Pieter Wuille's avatar
Pieter Wuille committed
3181
       }
s_nakamoto's avatar
s_nakamoto committed
3182
3183
3184
3185
        nPrevCol = nCol;

        // print columns
        for (int i = 0; i < nCol; i++)
3186
            LogPrintf("| ");
s_nakamoto's avatar
s_nakamoto committed
3187
3188
3189

        // print item
        CBlock block;
3190
        ReadBlockFromDisk(block, pindex);
3191
        LogPrintf("%d (blk%05u.dat:0x%x)  %s  tx %u\n",
s_nakamoto's avatar
s_nakamoto committed
3192
            pindex->nHeight,
Pieter Wuille's avatar
Pieter Wuille committed
3193
            pindex->GetBlockPos().nFile, pindex->GetBlockPos().nPos,
3194
            DateTimeStrFormat("%Y-%m-%d %H:%M:%S", block.GetBlockTime()),
s_nakamoto's avatar
s_nakamoto committed
3195
3196
            block.vtx.size());

3197
        // put the main time-chain first
s_nakamoto's avatar
s_nakamoto committed
3198
        vector<CBlockIndex*>& vNext = mapNext[pindex];
3199
        for (unsigned int i = 0; i < vNext.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
3200
        {
3201
            if (chainActive.Next(vNext[i]))
s_nakamoto's avatar
s_nakamoto committed
3202
3203
3204
3205
3206
3207
3208
            {
                swap(vNext[0], vNext[i]);
                break;
            }
        }

        // iterate children
3209
        for (unsigned int i = 0; i < vNext.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
3210
3211
3212
3213
            vStack.push_back(make_pair(nCol+i, vNext[i]));
    }
}

3214
bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
3215
{
3216
    int64_t nStart = GetTimeMillis();
3217

3218
    int nLoaded = 0;
Pieter Wuille's avatar
Pieter Wuille committed
3219
    try {
3220
        CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION);
3221
        uint64_t nStartByte = 0;
3222
3223
3224
3225
3226
3227
3228
3229
        if (dbp) {
            // (try to) skip already indexed part
            CBlockFileInfo info;
            if (pblocktree->ReadBlockFileInfo(dbp->nFile, info)) {
                nStartByte = info.nSize;
                blkdat.Seek(info.nSize);
            }
        }
3230
        uint64_t nRewind = blkdat.GetPos();
3231
3232
3233
        while (blkdat.good() && !blkdat.eof()) {
            boost::this_thread::interruption_point();

3234
3235
3236
            blkdat.SetPos(nRewind);
            nRewind++; // start one byte further next time, in case of failure
            blkdat.SetLimit(); // remove former limit
3237
            unsigned int nSize = 0;
3238
3239
            try {
                // locate a header
3240
                unsigned char buf[MESSAGE_START_SIZE];
3241
                blkdat.FindByte(Params().MessageStart()[0]);
3242
3243
                nRewind = blkdat.GetPos()+1;
                blkdat >> FLATDATA(buf);
3244
                if (memcmp(buf, Params().MessageStart(), MESSAGE_START_SIZE))
3245
3246
                    continue;
                // read size
3247
                blkdat >> nSize;
3248
3249
                if (nSize < 80 || nSize > MAX_BLOCK_SIZE)
                    continue;
3250
3251
3252
3253
3254
            } catch (std::exception &e) {
                // no valid block header found; don't complain
                break;
            }
            try {
3255
                // read block
3256
                uint64_t nBlockPos = blkdat.GetPos();
3257
                blkdat.SetLimit(nBlockPos + nSize);
3258
3259
3260
                CBlock block;
                blkdat >> block;
                nRewind = blkdat.GetPos();
3261
3262
3263
3264
3265

                // process block
                if (nBlockPos >= nStartByte) {
                    if (dbp)
                        dbp->nPos = nBlockPos;
Pieter Wuille's avatar
Pieter Wuille committed
3266
3267
                    CValidationState state;
                    if (ProcessBlock(state, NULL, &block, dbp))
3268
                        nLoaded++;
Pieter Wuille's avatar
Pieter Wuille committed
3269
3270
                    if (state.IsError())
                        break;
3271
                }
3272
            } catch (std::exception &e) {
3273
                LogPrintf("%s : Deserialize or I/O error - %s", __func__, e.what());
3274
3275
            }
        }
3276
        fclose(fileIn);
Pieter Wuille's avatar
Pieter Wuille committed
3277
3278
    } catch(std::runtime_error &e) {
        AbortNode(_("Error: system error: ") + e.what());
3279
    }
3280
    if (nLoaded > 0)
3281
        LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
3282
3283
    return nLoaded > 0;
}
s_nakamoto's avatar
s_nakamoto committed
3284

3285

s_nakamoto's avatar
s_nakamoto committed
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303








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

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

3305
    if (GetBoolArg("-testsafemode", false))
s_nakamoto's avatar
s_nakamoto committed
3306
3307
        strRPC = "test";

3308
3309
3310
    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
3311
3312
3313
3314
3315
3316
3317
    // Misc warnings like out of disk space and clock is wrong
    if (strMiscWarning != "")
    {
        nPriority = 1000;
        strStatusBar = strMiscWarning;
    }

3318
    if (fLargeWorkForkFound)
s_nakamoto's avatar
s_nakamoto committed
3319
3320
    {
        nPriority = 2000;
3321
3322
3323
        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
3324
3325
    {
        nPriority = 2000;
3326
        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
3327
3328
3329
3330
    }

    // Alerts
    {
3331
        LOCK(cs_mapAlerts);
3332
        BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
s_nakamoto's avatar
s_nakamoto committed
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
        {
            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;
3347
    assert(!"GetWarnings() : invalid parameter");
s_nakamoto's avatar
s_nakamoto committed
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
    return "error";
}








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


3364
bool static AlreadyHave(const CInv& inv)
s_nakamoto's avatar
s_nakamoto committed
3365
3366
3367
{
    switch (inv.type)
    {
Jeff Garzik's avatar
Jeff Garzik committed
3368
3369
    case MSG_TX:
        {
Pieter Wuille's avatar
Pieter Wuille committed
3370
            bool txInMap = false;
3371
            txInMap = mempool.exists(inv.hash);
Pieter Wuille's avatar
Pieter Wuille committed
3372
            return txInMap || mapOrphanTransactions.count(inv.hash) ||
3373
                pcoinsTip->HaveCoins(inv.hash);
Jeff Garzik's avatar
Jeff Garzik committed
3374
3375
3376
3377
        }
    case MSG_BLOCK:
        return mapBlockIndex.count(inv.hash) ||
               mapOrphanBlocks.count(inv.hash);
s_nakamoto's avatar
s_nakamoto committed
3378
3379
3380
3381
3382
3383
    }
    // Don't know what it is, just say we already got one
    return true;
}


3384
3385
3386
3387
3388
3389
void static ProcessGetData(CNode* pfrom)
{
    std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();

    vector<CInv> vNotFound;

3390
3391
    LOCK(cs_main);

3392
3393
3394
3395
3396
3397
3398
    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
3399
            boost::this_thread::interruption_point();
3400
3401
3402
3403
            it++;

            if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
            {
3404
                bool send = false;
3405
3406
3407
                map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(inv.hash);
                if (mi != mapBlockIndex.end())
                {
3408
3409
3410
3411
3412
                    // If the requested block is at a height below our last
                    // checkpoint, only serve it if it's in the checkpointed chain
                    int nHeight = mi->second->nHeight;
                    CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(mapBlockIndex);
                    if (pcheckpoint && nHeight < pcheckpoint->nHeight) {
Philip Kaufmann's avatar
Philip Kaufmann committed
3413
3414
3415
3416
3417
3418
                        if (!chainActive.Contains(mi->second))
                        {
                            LogPrintf("ProcessGetData(): ignoring request for old block that isn't in the main chain\n");
                        } else {
                            send = true;
                        }
3419
                    } else {
Philip Kaufmann's avatar
Philip Kaufmann committed
3420
                        send = true;
3421
3422
3423
3424
3425
                    }
                }
                if (send)
                {
                    // Send block from disk
3426
                    CBlock block;
3427
3428
                    if (!ReadBlockFromDisk(block, (*mi).second))
                        assert(!"cannot load block from disk");
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
                    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;
3460
                        vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
                        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) {
3479
3480
                    CTransaction tx;
                    if (mempool.lookup(inv.hash, tx)) {
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
                        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.
3494
            g_signals.Inventory(inv.hash);
3495

3496
3497
            if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
                break;
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
        }
    }

    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);
    }
}

3515
bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived)
s_nakamoto's avatar
s_nakamoto committed
3516
3517
{
    RandAddSeedPerfmon();
3518
    LogPrint("net", "received: %s (%u bytes) peer=%d\n", strCommand, vRecv.size(), pfrom->id);
s_nakamoto's avatar
s_nakamoto committed
3519
3520
    if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
    {
3521
        LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
s_nakamoto's avatar
s_nakamoto committed
3522
3523
3524
        return true;
    }

Pieter Wuille's avatar
Pieter Wuille committed
3525
3526
3527
3528
    {
        LOCK(cs_main);
        State(pfrom->GetId())->nLastBlockProcess = GetTimeMicros();
    }
s_nakamoto's avatar
s_nakamoto committed
3529
3530
3531
3532
3533
3534
3535



    if (strCommand == "version")
    {
        // Each connection can only send one version message
        if (pfrom->nVersion != 0)
3536
        {
Gavin Andresen's avatar
Gavin Andresen committed
3537
            pfrom->PushMessage("reject", strCommand, REJECT_DUPLICATE, string("Duplicate version message"));
Pieter Wuille's avatar
Pieter Wuille committed
3538
            Misbehaving(pfrom->GetId(), 1);
s_nakamoto's avatar
s_nakamoto committed
3539
            return false;
3540
        }
s_nakamoto's avatar
s_nakamoto committed
3541

3542
        int64_t nTime;
s_nakamoto's avatar
s_nakamoto committed
3543
3544
        CAddress addrMe;
        CAddress addrFrom;
3545
        uint64_t nNonce = 1;
s_nakamoto's avatar
s_nakamoto committed
3546
        vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
3547
        if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
Pieter Wuille's avatar
Pieter Wuille committed
3548
        {
3549
            // disconnect from peers older than this proto version
3550
            LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->id, pfrom->nVersion);
Gavin Andresen's avatar
Gavin Andresen committed
3551
3552
            pfrom->PushMessage("reject", strCommand, REJECT_OBSOLETE,
                               strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION));
Pieter Wuille's avatar
Pieter Wuille committed
3553
3554
3555
3556
            pfrom->fDisconnect = true;
            return false;
        }

s_nakamoto's avatar
s_nakamoto committed
3557
3558
        if (pfrom->nVersion == 10300)
            pfrom->nVersion = 300;
Pieter Wuille's avatar
Pieter Wuille committed
3559
        if (!vRecv.empty())
s_nakamoto's avatar
s_nakamoto committed
3560
            vRecv >> addrFrom >> nNonce;
Mike Hearn's avatar
Mike Hearn committed
3561
        if (!vRecv.empty()) {
s_nakamoto's avatar
s_nakamoto committed
3562
            vRecv >> pfrom->strSubVer;
Mike Hearn's avatar
Mike Hearn committed
3563
3564
            pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer);
        }
Pieter Wuille's avatar
Pieter Wuille committed
3565
        if (!vRecv.empty())
s_nakamoto's avatar
s_nakamoto committed
3566
            vRecv >> pfrom->nStartingHeight;
3567
3568
3569
3570
        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
3571

3572
3573
3574
3575
3576
3577
        if (pfrom->fInbound && addrMe.IsRoutable())
        {
            pfrom->addrLocal = addrMe;
            SeenLocal(addrMe);
        }

s_nakamoto's avatar
s_nakamoto committed
3578
3579
3580
        // Disconnect if we connected to ourself
        if (nNonce == nLocalHostNonce && nNonce > 1)
        {
3581
            LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
s_nakamoto's avatar
s_nakamoto committed
3582
3583
3584
3585
            pfrom->fDisconnect = true;
            return true;
        }

Gavin Andresen's avatar
Gavin Andresen committed
3586
3587
3588
3589
        // Be shy and don't send version until we hear
        if (pfrom->fInbound)
            pfrom->PushVersion();

s_nakamoto's avatar
s_nakamoto committed
3590
3591
3592
3593
        pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);


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

s_nakamoto's avatar
s_nakamoto committed
3597
3598
3599
        if (!pfrom->fInbound)
        {
            // Advertise our address
3600
            if (fListen && !IsInitialBlockDownload())
s_nakamoto's avatar
s_nakamoto committed
3601
            {
3602
3603
3604
                CAddress addr = GetLocalAddress(&pfrom->addr);
                if (addr.IsRoutable())
                    pfrom->PushAddress(addr);
s_nakamoto's avatar
s_nakamoto committed
3605
3606
3607
            }

            // Get recent addresses
3608
            if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
s_nakamoto's avatar
s_nakamoto committed
3609
3610
3611
3612
            {
                pfrom->PushMessage("getaddr");
                pfrom->fGetAddr = true;
            }
3613
3614
3615
3616
3617
3618
3619
            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
3620
3621
        }

s_nakamoto's avatar
s_nakamoto committed
3622
        // Relay alerts
3623
3624
        {
            LOCK(cs_mapAlerts);
3625
            BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
s_nakamoto's avatar
s_nakamoto committed
3626
                item.second.RelayTo(pfrom);
3627
        }
s_nakamoto's avatar
s_nakamoto committed
3628
3629
3630

        pfrom->fSuccessfullyConnected = true;

3631
        LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d\n", pfrom->cleanSubVer, pfrom->nVersion, pfrom->nStartingHeight, addrMe.ToString(), pfrom->id);
3632

3633
        AddTimeData(pfrom->addr, nTime);
s_nakamoto's avatar
s_nakamoto committed
3634
3635
3636
3637
3638
3639
    }


    else if (pfrom->nVersion == 0)
    {
        // Must have a version message before anything else
Pieter Wuille's avatar
Pieter Wuille committed
3640
        Misbehaving(pfrom->GetId(), 1);
s_nakamoto's avatar
s_nakamoto committed
3641
3642
3643
3644
3645
3646
        return false;
    }


    else if (strCommand == "verack")
    {
3647
        pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
s_nakamoto's avatar
s_nakamoto committed
3648
3649
3650
3651
3652
3653
3654
    }


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

        // Don't want addr from older versions unless seeding
3657
        if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
s_nakamoto's avatar
s_nakamoto committed
3658
3659
            return true;
        if (vAddr.size() > 1000)
3660
        {
Pieter Wuille's avatar
Pieter Wuille committed
3661
            Misbehaving(pfrom->GetId(), 20);
3662
            return error("message addr size() = %u", vAddr.size());
3663
        }
s_nakamoto's avatar
s_nakamoto committed
3664
3665

        // Store the new addresses
3666
        vector<CAddress> vAddrOk;
3667
3668
        int64_t nNow = GetAdjustedTime();
        int64_t nSince = nNow - 10 * 60;
3669
        BOOST_FOREACH(CAddress& addr, vAddr)
s_nakamoto's avatar
s_nakamoto committed
3670
        {
Gavin Andresen's avatar
Gavin Andresen committed
3671
3672
            boost::this_thread::interruption_point();

s_nakamoto's avatar
s_nakamoto committed
3673
3674
            if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
                addr.nTime = nNow - 5 * 24 * 60 * 60;
s_nakamoto's avatar
s_nakamoto committed
3675
            pfrom->AddAddressKnown(addr);
3676
            bool fReachable = IsReachable(addr);
s_nakamoto's avatar
s_nakamoto committed
3677
            if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
s_nakamoto's avatar
s_nakamoto committed
3678
3679
3680
            {
                // Relay to a limited number of other nodes
                {
3681
                    LOCK(cs_vNodes);
3682
3683
                    // 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
3684
3685
                    static uint256 hashSalt;
                    if (hashSalt == 0)
3686
                        hashSalt = GetRandHash();
3687
                    uint64_t hashAddr = addr.GetHash();
Pieter Wuille's avatar
Pieter Wuille committed
3688
                    uint256 hashRand = hashSalt ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60));
3689
                    hashRand = Hash(BEGIN(hashRand), END(hashRand));
s_nakamoto's avatar
s_nakamoto committed
3690
                    multimap<uint256, CNode*> mapMix;
3691
                    BOOST_FOREACH(CNode* pnode, vNodes)
3692
                    {
3693
                        if (pnode->nVersion < CADDR_TIME_VERSION)
s_nakamoto's avatar
s_nakamoto committed
3694
                            continue;
3695
3696
3697
3698
3699
3700
                        unsigned int nPointer;
                        memcpy(&nPointer, &pnode, sizeof(nPointer));
                        uint256 hashKey = hashRand ^ nPointer;
                        hashKey = Hash(BEGIN(hashKey), END(hashKey));
                        mapMix.insert(make_pair(hashKey, pnode));
                    }
3701
                    int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
s_nakamoto's avatar
s_nakamoto committed
3702
3703
3704
3705
                    for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
                        ((*mi).second)->PushAddress(addr);
                }
            }
3706
3707
3708
            // Do not store addresses outside our network
            if (fReachable)
                vAddrOk.push_back(addr);
s_nakamoto's avatar
s_nakamoto committed
3709
        }
3710
        addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
s_nakamoto's avatar
s_nakamoto committed
3711
3712
        if (vAddr.size() < 1000)
            pfrom->fGetAddr = false;
3713
3714
        if (pfrom->fOneShot)
            pfrom->fDisconnect = true;
s_nakamoto's avatar
s_nakamoto committed
3715
3716
3717
3718
3719
3720
3721
    }


    else if (strCommand == "inv")
    {
        vector<CInv> vInv;
        vRecv >> vInv;
3722
        if (vInv.size() > MAX_INV_SZ)
3723
        {
Pieter Wuille's avatar
Pieter Wuille committed
3724
            Misbehaving(pfrom->GetId(), 20);
3725
            return error("message inv size() = %u", vInv.size());
3726
        }
s_nakamoto's avatar
s_nakamoto committed
3727

3728
3729
        LOCK(cs_main);

3730
        for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
s_nakamoto's avatar
s_nakamoto committed
3731
        {
3732
3733
            const CInv &inv = vInv[nInv];

Gavin Andresen's avatar
Gavin Andresen committed
3734
            boost::this_thread::interruption_point();
s_nakamoto's avatar
s_nakamoto committed
3735
3736
            pfrom->AddInventoryKnown(inv);

3737
            bool fAlreadyHave = AlreadyHave(inv);
3738
            LogPrint("net", "got inv: %s  %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->id);
s_nakamoto's avatar
s_nakamoto committed
3739

3740
            if (!fAlreadyHave) {
3741
3742
3743
3744
3745
3746
                if (!fImporting && !fReindex) {
                    if (inv.type == MSG_BLOCK)
                        AddBlockToQueue(pfrom->GetId(), inv.hash);
                    else
                        pfrom->AskFor(inv);
                }
3747
            } else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash)) {
3748
                PushGetBlocks(pfrom, chainActive.Tip(), GetOrphanRoot(inv.hash));
3749
            }
s_nakamoto's avatar
s_nakamoto committed
3750

Pieter Wuille's avatar
Pieter Wuille committed
3751
3752
3753
            if (inv.type == MSG_BLOCK)
                UpdateBlockAvailability(pfrom->GetId(), inv.hash);

s_nakamoto's avatar
s_nakamoto committed
3754
            // Track requests for our stuff
3755
            g_signals.Inventory(inv.hash);
s_nakamoto's avatar
s_nakamoto committed
3756
3757
3758
3759
3760
3761
3762
3763
        }
    }


    else if (strCommand == "getdata")
    {
        vector<CInv> vInv;
        vRecv >> vInv;
3764
        if (vInv.size() > MAX_INV_SZ)
3765
        {
Pieter Wuille's avatar
Pieter Wuille committed
3766
            Misbehaving(pfrom->GetId(), 20);
3767
            return error("message getdata size() = %u", vInv.size());
3768
        }
s_nakamoto's avatar
s_nakamoto committed
3769

3770
        if (fDebug || (vInv.size() != 1))
3771
            LogPrint("net", "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->id);
3772

3773
        if ((fDebug && vInv.size() > 0) || (vInv.size() == 1))
3774
            LogPrint("net", "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->id);
s_nakamoto's avatar
s_nakamoto committed
3775

3776
3777
        pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
        ProcessGetData(pfrom);
s_nakamoto's avatar
s_nakamoto committed
3778
3779
3780
3781
3782
3783
3784
3785
3786
    }


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

3787
3788
        LOCK(cs_main);

3789
        // Find the last block the caller has in the main chain
3790
        CBlockIndex* pindex = chainActive.FindFork(locator);
s_nakamoto's avatar
s_nakamoto committed
3791
3792
3793

        // Send the rest of the chain
        if (pindex)
3794
            pindex = chainActive.Next(pindex);
3795
        int nLimit = 500;
3796
        LogPrint("net", "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop==uint256(0) ? "end" : hashStop.ToString(), nLimit, pfrom->id);
3797
        for (; pindex; pindex = chainActive.Next(pindex))
s_nakamoto's avatar
s_nakamoto committed
3798
3799
3800
        {
            if (pindex->GetBlockHash() == hashStop)
            {
3801
                LogPrint("net", "  getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
s_nakamoto's avatar
s_nakamoto committed
3802
3803
3804
                break;
            }
            pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
3805
            if (--nLimit <= 0)
s_nakamoto's avatar
s_nakamoto committed
3806
3807
3808
            {
                // When this block is requested, we'll send an inv that'll make them
                // getblocks the next batch of inventory.
3809
                LogPrint("net", "  getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
s_nakamoto's avatar
s_nakamoto committed
3810
3811
3812
3813
3814
3815
3816
                pfrom->hashContinue = pindex->GetBlockHash();
                break;
            }
        }
    }


3817
3818
3819
3820
3821
3822
    else if (strCommand == "getheaders")
    {
        CBlockLocator locator;
        uint256 hashStop;
        vRecv >> locator >> hashStop;

3823
3824
        LOCK(cs_main);

3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
        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
3837
            pindex = chainActive.FindFork(locator);
3838
            if (pindex)
3839
                pindex = chainActive.Next(pindex);
3840
3841
        }

3842
        // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
3843
        vector<CBlock> vHeaders;
3844
        int nLimit = 2000;
3845
        LogPrint("net", "getheaders %d to %s\n", (pindex ? pindex->nHeight : -1), hashStop.ToString());
3846
        for (; pindex; pindex = chainActive.Next(pindex))
3847
3848
3849
3850
3851
3852
3853
3854
3855
        {
            vHeaders.push_back(pindex->GetBlockHeader());
            if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
                break;
        }
        pfrom->PushMessage("headers", vHeaders);
    }


s_nakamoto's avatar
s_nakamoto committed
3856
3857
3858
    else if (strCommand == "tx")
    {
        vector<uint256> vWorkQueue;
3859
        vector<uint256> vEraseQueue;
s_nakamoto's avatar
s_nakamoto committed
3860
3861
3862
3863
3864
3865
        CTransaction tx;
        vRecv >> tx;

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

3866
3867
        LOCK(cs_main);

s_nakamoto's avatar
s_nakamoto committed
3868
        bool fMissingInputs = false;
Pieter Wuille's avatar
Pieter Wuille committed
3869
        CValidationState state;
3870
        if (AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs))
s_nakamoto's avatar
s_nakamoto committed
3871
        {
3872
            mempool.check(pcoinsTip);
3873
            RelayTransaction(tx);
s_nakamoto's avatar
s_nakamoto committed
3874
3875
            mapAlreadyAskedFor.erase(inv);
            vWorkQueue.push_back(inv.hash);
3876
            vEraseQueue.push_back(inv.hash);
s_nakamoto's avatar
s_nakamoto committed
3877

3878

3879
3880
            LogPrint("mempool", "AcceptToMemoryPool: peer=%d %s : accepted %s (poolsz %u)\n",
                pfrom->id, pfrom->cleanSubVer,
3881
                tx.GetHash().ToString(),
3882
3883
                mempool.mapTx.size());

s_nakamoto's avatar
s_nakamoto committed
3884
            // Recursively process any orphan transactions that depended on this one
3885
            for (unsigned int i = 0; i < vWorkQueue.size(); i++)
s_nakamoto's avatar
s_nakamoto committed
3886
3887
            {
                uint256 hashPrev = vWorkQueue[i];
3888
                for (set<uint256>::iterator mi = mapOrphanTransactionsByPrev[hashPrev].begin();
3889
                     mi != mapOrphanTransactionsByPrev[hashPrev].end();
s_nakamoto's avatar
s_nakamoto committed
3890
3891
                     ++mi)
                {
3892
3893
                    const uint256& orphanHash = *mi;
                    const CTransaction& orphanTx = mapOrphanTransactions[orphanHash];
3894
                    bool fMissingInputs2 = false;
3895
3896
3897
                    // 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)
3898
                    CValidationState stateDummy;
s_nakamoto's avatar
s_nakamoto committed
3899

3900
                    if (AcceptToMemoryPool(mempool, stateDummy, orphanTx, true, &fMissingInputs2))
s_nakamoto's avatar
s_nakamoto committed
3901
                    {
3902
                        LogPrint("mempool", "   accepted orphan tx %s\n", orphanHash.ToString());
3903
                        RelayTransaction(orphanTx);
3904
3905
3906
                        mapAlreadyAskedFor.erase(CInv(MSG_TX, orphanHash));
                        vWorkQueue.push_back(orphanHash);
                        vEraseQueue.push_back(orphanHash);
3907
3908
3909
                    }
                    else if (!fMissingInputs2)
                    {
3910
                        // invalid or too-little-fee orphan
3911
                        vEraseQueue.push_back(orphanHash);
3912
                        LogPrint("mempool", "   removed orphan tx %s\n", orphanHash.ToString());
s_nakamoto's avatar
s_nakamoto committed
3913
                    }
3914
                    mempool.check(pcoinsTip);
s_nakamoto's avatar
s_nakamoto committed
3915
3916
3917
                }
            }

3918
            BOOST_FOREACH(uint256 hash, vEraseQueue)
s_nakamoto's avatar
s_nakamoto committed
3919
3920
3921
3922
                EraseOrphanTx(hash);
        }
        else if (fMissingInputs)
        {
3923
            AddOrphanTx(tx);
3924
3925

            // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
3926
            unsigned int nEvicted = LimitOrphanTxSize(MAX_ORPHAN_TRANSACTIONS);
3927
            if (nEvicted > 0)
3928
                LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", nEvicted);
Pieter Wuille's avatar
Pieter Wuille committed
3929
3930
3931
3932
3933
        } else if (pfrom->fWhitelisted) {
            // Always relay transactions received from whitelisted peers, even
            // if they are already in the mempool (allowing the node to function
            // as a gateway for nodes hidden behind it).
            RelayTransaction(tx);
s_nakamoto's avatar
s_nakamoto committed
3934
        }
3935
        int nDoS = 0;
3936
        if (state.IsInvalid(nDoS))
Philip Kaufmann's avatar
Philip Kaufmann committed
3937
        {
3938
3939
            LogPrint("mempool", "%s from peer=%d %s was not accepted into the memory pool: %s\n", tx.GetHash().ToString(),
                pfrom->id, pfrom->cleanSubVer,
3940
                state.GetRejectReason());
Gavin Andresen's avatar
Gavin Andresen committed
3941
3942
            pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
                               state.GetRejectReason(), inv.hash);
3943
            if (nDoS > 0)
Pieter Wuille's avatar
Pieter Wuille committed
3944
                Misbehaving(pfrom->GetId(), nDoS);
Gavin Andresen's avatar
Gavin Andresen committed
3945
        }
s_nakamoto's avatar
s_nakamoto committed
3946
3947
3948
    }


3949
    else if (strCommand == "block" && !fImporting && !fReindex) // Ignore blocks received while importing
s_nakamoto's avatar
s_nakamoto committed
3950
    {
3951
3952
        CBlock block;
        vRecv >> block;
s_nakamoto's avatar
s_nakamoto committed
3953

3954
        LogPrint("net", "received block %s peer=%d\n", block.GetHash().ToString(), pfrom->id);
3955
        // block.print();
s_nakamoto's avatar
s_nakamoto committed
3956

3957
        CInv inv(MSG_BLOCK, block.GetHash());
s_nakamoto's avatar
s_nakamoto committed
3958
3959
        pfrom->AddInventoryKnown(inv);

3960
3961
3962
3963
3964
3965
        {
            LOCK(cs_main);
            // Remember who we got this block from.
            mapBlockSource[inv.hash] = pfrom->GetId();
            MarkBlockAsReceived(inv.hash, pfrom->GetId());
        }
3966

Pieter Wuille's avatar
Pieter Wuille committed
3967
        CValidationState state;
3968
        ProcessBlock(state, pfrom, &block);
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
        int nDoS;
        if (state.IsInvalid(nDoS)) {
            pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
                               state.GetRejectReason(), inv.hash);
            if (nDoS > 0) {
                LOCK(cs_main);
                Misbehaving(pfrom->GetId(), nDoS);
            }
        }

s_nakamoto's avatar
s_nakamoto committed
3979
3980
3981
3982
3983
3984
    }


    else if (strCommand == "getaddr")
    {
        pfrom->vAddrToSend.clear();
3985
3986
3987
        vector<CAddress> vAddr = addrman.GetAddr();
        BOOST_FOREACH(const CAddress &addr, vAddr)
            pfrom->PushAddress(addr);
s_nakamoto's avatar
s_nakamoto committed
3988
3989
3990
    }


3991
3992
    else if (strCommand == "mempool")
    {
3993
        LOCK2(cs_main, pfrom->cs_filter);
3994

3995
3996
3997
        std::vector<uint256> vtxid;
        mempool.queryHashes(vtxid);
        vector<CInv> vInv;
Matt Corallo's avatar
Matt Corallo committed
3998
3999
        BOOST_FOREACH(uint256& hash, vtxid) {
            CInv inv(MSG_TX, hash);
4000
4001
4002
            CTransaction tx;
            bool fInMemPool = mempool.lookup(hash, tx);
            if (!fInMemPool) continue; // another thread removed since queryHashes, maybe...
4003
            if ((pfrom->pfilter && pfrom->pfilter->IsRelevantAndUpdate(tx)) ||
Matt Corallo's avatar
Matt Corallo committed
4004
4005
               (!pfrom->pfilter))
                vInv.push_back(inv);
4006
4007
4008
4009
            if (vInv.size() == MAX_INV_SZ) {
                pfrom->PushMessage("inv", vInv);
                vInv.clear();
            }
4010
4011
4012
4013
4014
4015
        }
        if (vInv.size() > 0)
            pfrom->PushMessage("inv", vInv);
    }


s_nakamoto's avatar
s_nakamoto committed
4016
4017
    else if (strCommand == "ping")
    {
Jeff Garzik's avatar
Jeff Garzik committed
4018
4019
        if (pfrom->nVersion > BIP0031_VERSION)
        {
4020
            uint64_t nonce = 0;
Jeff Garzik's avatar
Jeff Garzik committed
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
            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
4035
4036
4037
    }


Josh Lehan's avatar
Josh Lehan committed
4038
4039
    else if (strCommand == "pong")
    {
4040
        int64_t pingUsecEnd = nTimeReceived;
4041
        uint64_t nonce = 0;
Josh Lehan's avatar
Josh Lehan committed
4042
4043
4044
        size_t nAvail = vRecv.in_avail();
        bool bPingFinished = false;
        std::string sProblem;
4045

Josh Lehan's avatar
Josh Lehan committed
4046
4047
        if (nAvail >= sizeof(nonce)) {
            vRecv >> nonce;
4048

Josh Lehan's avatar
Josh Lehan committed
4049
4050
4051
4052
4053
            // 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;
4054
                    int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
Josh Lehan's avatar
Josh Lehan committed
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
                    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";
        }
4079

Josh Lehan's avatar
Josh Lehan committed
4080
        if (!(sProblem.empty())) {
4081
4082
            LogPrint("net", "pong peer=%d %s: %s, %x expected, %x received, %u bytes\n",
                pfrom->id,
4083
4084
                pfrom->cleanSubVer,
                sProblem,
4085
4086
4087
                pfrom->nPingNonceSent,
                nonce,
                nAvail);
Josh Lehan's avatar
Josh Lehan committed
4088
4089
4090
4091
4092
        }
        if (bPingFinished) {
            pfrom->nPingNonceSent = 0;
        }
    }
4093
4094


s_nakamoto's avatar
s_nakamoto committed
4095
4096
4097
4098
4099
    else if (strCommand == "alert")
    {
        CAlert alert;
        vRecv >> alert;

Gavin Andresen's avatar
Gavin Andresen committed
4100
4101
        uint256 alertHash = alert.GetHash();
        if (pfrom->setKnown.count(alertHash) == 0)
s_nakamoto's avatar
s_nakamoto committed
4102
        {
Gavin Andresen's avatar
Gavin Andresen committed
4103
            if (alert.ProcessAlert())
4104
            {
Gavin Andresen's avatar
Gavin Andresen committed
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
                // 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
4120
                Misbehaving(pfrom->GetId(), 10);
4121
            }
s_nakamoto's avatar
s_nakamoto committed
4122
4123
4124
4125
        }
    }


4126
4127
4128
4129
4130
4131
4132
    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
4133
            Misbehaving(pfrom->GetId(), 100);
4134
4135
4136
4137
4138
        else
        {
            LOCK(pfrom->cs_filter);
            delete pfrom->pfilter;
            pfrom->pfilter = new CBloomFilter(filter);
4139
            pfrom->pfilter->UpdateEmptyFull();
4140
        }
4141
        pfrom->fRelayTxes = true;
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
    }


    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
4152
        if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE)
4153
        {
Pieter Wuille's avatar
Pieter Wuille committed
4154
            Misbehaving(pfrom->GetId(), 100);
4155
4156
4157
4158
4159
        } else {
            LOCK(pfrom->cs_filter);
            if (pfrom->pfilter)
                pfrom->pfilter->insert(vData);
            else
Pieter Wuille's avatar
Pieter Wuille committed
4160
                Misbehaving(pfrom->GetId(), 100);
4161
4162
4163
4164
4165
4166
4167
4168
        }
    }


    else if (strCommand == "filterclear")
    {
        LOCK(pfrom->cs_filter);
        delete pfrom->pfilter;
4169
        pfrom->pfilter = new CBloomFilter();
4170
        pfrom->fRelayTxes = true;
4171
4172
4173
    }


Gavin Andresen's avatar
Gavin Andresen committed
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
    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);
4193
            LogPrint("net", "Reject %s\n", SanitizeString(s));
Gavin Andresen's avatar
Gavin Andresen committed
4194
4195
4196
        }
    }

s_nakamoto's avatar
s_nakamoto committed
4197
4198
4199
    else
    {
        // Ignore unknown commands for extensibility
4200
        LogPrint("net", "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->id);
s_nakamoto's avatar
s_nakamoto committed
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
    }


    // 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;
}

4213
// requires LOCK(cs_vRecvMsg)
4214
4215
4216
bool ProcessMessages(CNode* pfrom)
{
    //if (fDebug)
4217
    //    LogPrintf("ProcessMessages(%u messages)\n", pfrom->vRecvMsg.size());
s_nakamoto's avatar
s_nakamoto committed
4218

4219
4220
4221
4222
4223
4224
4225
4226
    //
    // Message format
    //  (4) message start
    //  (12) command
    //  (4) size
    //  (4) checksum
    //  (x) data
    //
4227
    bool fOk = true;
s_nakamoto's avatar
s_nakamoto committed
4228

4229
4230
    if (!pfrom->vRecvGetData.empty())
        ProcessGetData(pfrom);
4231

4232
4233
    // this maintains the order of responses
    if (!pfrom->vRecvGetData.empty()) return fOk;
4234

4235
    std::deque<CNetMessage>::iterator it = pfrom->vRecvMsg.begin();
4236
    while (!pfrom->fDisconnect && it != pfrom->vRecvMsg.end()) {
4237
        // Don't bother if send buffer is too full to respond anyway
4238
        if (pfrom->nSendSize >= SendBufferSize())
4239
4240
            break;

4241
4242
        // get next message
        CNetMessage& msg = *it;
4243
4244

        //if (fDebug)
4245
        //    LogPrintf("ProcessMessages(message %u msgsz, %u bytes, complete:%s)\n",
4246
4247
4248
        //            msg.hdr.nMessageSize, msg.vRecv.size(),
        //            msg.complete() ? "Y" : "N");

4249
        // end, if an incomplete message is found
4250
        if (!msg.complete())
4251
            break;
4252

4253
4254
4255
        // at this point, any failure means we can delete the current message
        it++;

4256
        // Scan for message start
4257
        if (memcmp(msg.hdr.pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0) {
4258
            LogPrintf("\n\nPROCESSMESSAGE: INVALID MESSAGESTART\n\n");
4259
4260
            fOk = false;
            break;
4261
        }
s_nakamoto's avatar
s_nakamoto committed
4262

4263
        // Read header
4264
        CMessageHeader& hdr = msg.hdr;
4265
4266
        if (!hdr.IsValid())
        {
4267
            LogPrintf("\n\nPROCESSMESSAGE: ERRORS IN HEADER %s\n\n\n", hdr.GetCommand());
4268
4269
4270
4271
4272
4273
4274
4275
            continue;
        }
        string strCommand = hdr.GetCommand();

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

        // Checksum
4276
        CDataStream& vRecv = msg.vRecv;
Pieter Wuille's avatar
Pieter Wuille committed
4277
4278
4279
4280
        uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
        unsigned int nChecksum = 0;
        memcpy(&nChecksum, &hash, sizeof(nChecksum));
        if (nChecksum != hdr.nChecksum)
4281
        {
4282
            LogPrintf("ProcessMessages(%s, %u bytes) : CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n",
4283
               strCommand, nMessageSize, nChecksum, hdr.nChecksum);
Pieter Wuille's avatar
Pieter Wuille committed
4284
            continue;
4285
4286
4287
4288
4289
4290
        }

        // Process message
        bool fRet = false;
        try
        {
4291
            fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime);
Gavin Andresen's avatar
Gavin Andresen committed
4292
            boost::this_thread::interruption_point();
4293
4294
4295
        }
        catch (std::ios_base::failure& e)
        {
Gavin Andresen's avatar
Gavin Andresen committed
4296
            pfrom->PushMessage("reject", strCommand, REJECT_MALFORMED, string("error parsing message"));
4297
4298
            if (strstr(e.what(), "end of data"))
            {
4299
                // Allow exceptions from under-length message on vRecv
4300
                LogPrintf("ProcessMessages(%s, %u bytes) : Exception '%s' caught, normally caused by a message being shorter than its stated length\n", strCommand, nMessageSize, e.what());
4301
4302
4303
            }
            else if (strstr(e.what(), "size too large"))
            {
4304
                // Allow exceptions from over-long size
4305
                LogPrintf("ProcessMessages(%s, %u bytes) : Exception '%s' caught\n", strCommand, nMessageSize, e.what());
4306
4307
4308
            }
            else
            {
4309
                PrintExceptionContinue(&e, "ProcessMessages()");
4310
4311
            }
        }
Gavin Andresen's avatar
Gavin Andresen committed
4312
4313
4314
        catch (boost::thread_interrupted) {
            throw;
        }
4315
        catch (std::exception& e) {
4316
            PrintExceptionContinue(&e, "ProcessMessages()");
4317
        } catch (...) {
4318
            PrintExceptionContinue(NULL, "ProcessMessages()");
4319
4320
4321
        }

        if (!fRet)
4322
            LogPrintf("ProcessMessage(%s, %u bytes) FAILED peer=%d\n", strCommand, nMessageSize, pfrom->id);
4323

4324
        break;
4325
4326
    }

4327
4328
4329
4330
    // In case the connection got shut down, its receive buffer was wiped
    if (!pfrom->fDisconnect)
        pfrom->vRecvMsg.erase(pfrom->vRecvMsg.begin(), it);

4331
    return fOk;
4332
}
s_nakamoto's avatar
s_nakamoto committed
4333
4334
4335
4336


bool SendMessages(CNode* pto, bool fSendTrickle)
{
4337
    {
s_nakamoto's avatar
s_nakamoto committed
4338
4339
4340
4341
        // Don't send anything until we get their version message
        if (pto->nVersion == 0)
            return true;

Josh Lehan's avatar
Josh Lehan committed
4342
4343
4344
4345
4346
4347
4348
4349
        //
        // Message: ping
        //
        bool pingSend = false;
        if (pto->fPingQueued) {
            // RPC ping request by user
            pingSend = true;
        }
4350
4351
        if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
            // Ping automatically sent as a latency probe & keepalive.
Josh Lehan's avatar
Josh Lehan committed
4352
4353
4354
            pingSend = true;
        }
        if (pingSend) {
4355
            uint64_t nonce = 0;
Josh Lehan's avatar
Josh Lehan committed
4356
            while (nonce == 0) {
4357
                GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
Josh Lehan's avatar
Josh Lehan committed
4358
4359
            }
            pto->fPingQueued = false;
4360
            pto->nPingUsecStart = GetTimeMicros();
Josh Lehan's avatar
Josh Lehan committed
4361
            if (pto->nVersion > BIP0031_VERSION) {
4362
                pto->nPingNonceSent = nonce;
Pieter Wuille's avatar
Pieter Wuille committed
4363
                pto->PushMessage("ping", nonce);
Josh Lehan's avatar
Josh Lehan committed
4364
            } else {
4365
4366
                // Peer is too old to support ping command with nonce, pong will never arrive.
                pto->nPingNonceSent = 0;
Jeff Garzik's avatar
Jeff Garzik committed
4367
                pto->PushMessage("ping");
Josh Lehan's avatar
Josh Lehan committed
4368
            }
Jeff Garzik's avatar
Jeff Garzik committed
4369
        }
s_nakamoto's avatar
s_nakamoto committed
4370

4371
4372
4373
4374
        TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
        if (!lockMain)
            return true;

s_nakamoto's avatar
s_nakamoto committed
4375
        // Address refresh broadcast
4376
        static int64_t nLastRebroadcast;
4377
        if (!IsInitialBlockDownload() && (GetTime() - nLastRebroadcast > 24 * 60 * 60))
s_nakamoto's avatar
s_nakamoto committed
4378
4379
        {
            {
4380
                LOCK(cs_vNodes);
4381
                BOOST_FOREACH(CNode* pnode, vNodes)
s_nakamoto's avatar
s_nakamoto committed
4382
4383
                {
                    // Periodically clear setAddrKnown to allow refresh broadcasts
4384
4385
                    if (nLastRebroadcast)
                        pnode->setAddrKnown.clear();
s_nakamoto's avatar
s_nakamoto committed
4386
4387

                    // Rebroadcast our address
4388
                    if (fListen)
s_nakamoto's avatar
s_nakamoto committed
4389
                    {
4390
4391
4392
                        CAddress addr = GetLocalAddress(&pnode->addr);
                        if (addr.IsRoutable())
                            pnode->PushAddress(addr);
s_nakamoto's avatar
s_nakamoto committed
4393
                    }
s_nakamoto's avatar
s_nakamoto committed
4394
4395
                }
            }
4396
            nLastRebroadcast = GetTime();
s_nakamoto's avatar
s_nakamoto committed
4397
4398
4399
4400
4401
4402
4403
4404
4405
        }

        //
        // Message: addr
        //
        if (fSendTrickle)
        {
            vector<CAddress> vAddr;
            vAddr.reserve(pto->vAddrToSend.size());
4406
            BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
s_nakamoto's avatar
s_nakamoto committed
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
            {
                // 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);
        }

4425
4426
        CNodeState &state = *State(pto->GetId());
        if (state.fShouldBan) {
Pieter Wuille's avatar
Pieter Wuille committed
4427
4428
            if (pto->fWhitelisted)
                LogPrintf("Warning: not punishing whitelisted peer %s!\n", pto->addr.ToString());
Pieter Wuille's avatar
Pieter Wuille committed
4429
4430
            else {
                pto->fDisconnect = true;
Pieter Wuille's avatar
Pieter Wuille committed
4431
4432
4433
4434
                if (pto->addr.IsLocal())
                    LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
                else
                    CNode::Ban(pto->addr);
Pieter Wuille's avatar
Pieter Wuille committed
4435
            }
4436
            state.fShouldBan = false;
Pieter Wuille's avatar
Pieter Wuille committed
4437
4438
        }

4439
4440
4441
4442
        BOOST_FOREACH(const CBlockReject& reject, state.rejects)
            pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock);
        state.rejects.clear();

4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
        // 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())
        {
4454
            g_signals.Broadcast();
4455
        }
s_nakamoto's avatar
s_nakamoto committed
4456
4457
4458
4459
4460
4461
4462

        //
        // Message: inventory
        //
        vector<CInv> vInv;
        vector<CInv> vInvWait;
        {
4463
            LOCK(pto->cs_inventory);
s_nakamoto's avatar
s_nakamoto committed
4464
4465
            vInv.reserve(pto->vInventoryToSend.size());
            vInvWait.reserve(pto->vInventoryToSend.size());
4466
            BOOST_FOREACH(const CInv& inv, pto->vInventoryToSend)
s_nakamoto's avatar
s_nakamoto committed
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
            {
                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)
4477
                        hashSalt = GetRandHash();
s_nakamoto's avatar
s_nakamoto committed
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
                    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);


4506
4507
4508
4509
4510
        // Detect stalled peers. Require that blocks are in flight, we haven't
        // received a (requested) block in one minute, and that all blocks are
        // in flight for over two minutes, since we first had a chance to
        // process an incoming block.
        int64_t nNow = GetTimeMicros();
4511
4512
        if (!pto->fDisconnect && state.nBlocksInFlight &&
            state.nLastBlockReceive < state.nLastBlockProcess - BLOCK_DOWNLOAD_TIMEOUT*1000000 &&
4513
4514
4515
4516
4517
            state.vBlocksInFlight.front().nTime < state.nLastBlockProcess - 2*BLOCK_DOWNLOAD_TIMEOUT*1000000) {
            LogPrintf("Peer %s is stalling block download, disconnecting\n", state.name.c_str());
            pto->fDisconnect = true;
        }

Pieter Wuille's avatar
Pieter Wuille committed
4518
4519
4520
        // Update knowledge of peer's block availability.
        ProcessBlockAvailability(pto->GetId());

s_nakamoto's avatar
s_nakamoto committed
4521
        //
4522
        // Message: getdata (blocks)
s_nakamoto's avatar
s_nakamoto committed
4523
4524
        //
        vector<CInv> vGetData;
4525
4526
4527
4528
        while (!pto->fDisconnect && state.nBlocksToDownload && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
            uint256 hash = state.vBlocksToDownload.front();
            vGetData.push_back(CInv(MSG_BLOCK, hash));
            MarkBlockAsInFlight(pto->GetId(), hash);
4529
            LogPrint("net", "Requesting block %s peer=%d\n", hash.ToString(), pto->id);
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
            if (vGetData.size() >= 1000)
            {
                pto->PushMessage("getdata", vGetData);
                vGetData.clear();
            }
        }

        //
        // Message: getdata (non-blocks)
        //
        while (!pto->fDisconnect && !pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
s_nakamoto's avatar
s_nakamoto committed
4541
4542
        {
            const CInv& inv = (*pto->mapAskFor.begin()).second;
4543
            if (!AlreadyHave(inv))
s_nakamoto's avatar
s_nakamoto committed
4544
            {
4545
                if (fDebug)
4546
                    LogPrint("net", "Requesting %s peer=%d\n", inv.ToString(), pto->id);
s_nakamoto's avatar
s_nakamoto committed
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
                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;
}






4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
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
4580
        std::map<uint256, COrphanBlock*>::iterator it2 = mapOrphanBlocks.begin();
4581
4582
4583
4584
4585
4586
4587
4588
        for (; it2 != mapOrphanBlocks.end(); it2++)
            delete (*it2).second;
        mapOrphanBlocks.clear();

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