annotate src/main.cpp @ 718:3ce286fc9dc1 draft

Limit response to getblocks to half of output buffer size Introduce SendBufferSize() and ReceiveBufferSize(), and limit the blocks sent as response to the "getblocks" message to half of the active send buffer size.
author Pieter Wuille <pieter.wuille@gmail.com>
date Thu, 30 Jun 2011 23:29:44 +0200
parents 0213b284ff93
children 7d6b511630d6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2 // Distributed under the MIT/X11 software license, see the accompanying
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
4 #include "headers.h"
605
1ae81ad9b0e5 Only include db.h when we have to.
Jordan Lewis <jordanthelewis@gmail.com>
parents: 595
diff changeset
5 #include "db.h"
607
92e329774c81 Only include net.h when we have to
Jordan Lewis <jordanthelewis@gmail.com>
parents: 605
diff changeset
6 #include "net.h"
608
1dbd290981a9 Only include init.h when we have to
Jordan Lewis <jordanthelewis@gmail.com>
parents: 607
diff changeset
7 #include "init.h"
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
8 #include "cryptopp/sha.h"
695
9609c7af94ba Fix missing includes needed for Boost 1.46.
Shane Wegner <shane-github@csy.ca>
parents: 690
diff changeset
9 #include <boost/filesystem.hpp>
611
503d51554676 Only include certain boost headers if necessary.
Jordan Lewis <jordanthelewis@gmail.com>
parents: 608
diff changeset
10 #include <boost/filesystem/fstream.hpp>
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
11
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
12 using namespace std;
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
13 using namespace boost;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
14
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
15 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
16 // Global state
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
17 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
18
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
19 CCriticalSection cs_setpwalletRegistered;
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
20 set<CWallet*> setpwalletRegistered;
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
21
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
22 CCriticalSection cs_main;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
23
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
24 CCriticalSection cs_mapPubKeys;
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
25 map<uint160, vector<unsigned char> > mapPubKeys;
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
26
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
27 map<uint256, CTransaction> mapTransactions;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
28 CCriticalSection cs_mapTransactions;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
29 unsigned int nTransactionsUpdated = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
30 map<COutPoint, CInPoint> mapNextTx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
31
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
32 map<uint256, CBlockIndex*> mapBlockIndex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
33 uint256 hashGenesisBlock("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
34 CBigNum bnProofOfWorkLimit(~uint256(0) >> 32);
702
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
35 const int nTotalBlocksEstimate = 131000; // Conservative estimate of total nr of blocks on main chain
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
36 const int nInitialBlockThreshold = 10000; // Regard blocks up until N-threshold as "initial download"
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
37 CBlockIndex* pindexGenesisBlock = NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
38 int nBestHeight = -1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
39 CBigNum bnBestChainWork = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
40 CBigNum bnBestInvalidWork = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
41 uint256 hashBestChain = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
42 CBlockIndex* pindexBest = NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
43 int64 nTimeBestReceived = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
44
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
45 map<uint256, CBlock*> mapOrphanBlocks;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
46 multimap<uint256, CBlock*> mapOrphanBlocksByPrev;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
47
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
48 map<uint256, CDataStream*> mapOrphanTransactions;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
49 multimap<uint256, CDataStream*> mapOrphanTransactionsByPrev;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
50
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
51
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
52 double dHashesPerSec;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
53 int64 nHPSTimerStart;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
54
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
55 // Settings
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
56 int fGenerateBitcoins = false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
57 int64 nTransactionFee = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
58 CAddress addrIncoming;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
59 int fLimitProcessors = false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
60 int nLimitProcessors = 1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
61 int fMinimizeToTray = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
62 int fMinimizeOnClose = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
63 #if USE_UPNP
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
64 int fUseUPnP = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
65 #else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
66 int fUseUPnP = false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
67 #endif
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
68
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
69
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
70
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
71
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
72
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
73
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
74
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
75 //////////////////////////////////////////////////////////////////////////////
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
76 //
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
77 // dispatching functions
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
78 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
79
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
80 void RegisterWallet(CWallet* pwalletIn)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
81 {
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
82 CRITICAL_BLOCK(cs_setpwalletRegistered)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
83 {
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
84 setpwalletRegistered.insert(pwalletIn);
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
85 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
86 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
87
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
88 void UnregisterWallet(CWallet* pwalletIn)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
89 {
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
90 CRITICAL_BLOCK(cs_setpwalletRegistered)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
91 {
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
92 setpwalletRegistered.erase(pwalletIn);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
93 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
94 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
95
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
96 bool static IsFromMe(CTransaction& tx)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
97 {
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
98 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
99 if (pwallet->IsFromMe(tx))
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
100 return true;
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
101 return false;
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
102 }
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
103
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
104 bool static GetTransaction(const uint256& hashTx, CWalletTx& wtx)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
105 {
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
106 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
107 if (pwallet->GetTransaction(hashTx,wtx))
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
108 return true;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
109 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
110 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
111
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
112 void static EraseFromWallets(uint256 hash)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
113 {
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
114 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
115 pwallet->EraseFromWallet(hash);
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
116 }
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
117
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
118 void static SyncWithWallets(const CTransaction& tx, const CBlock* pblock = NULL, bool fUpdate = false)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
119 {
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
120 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
121 pwallet->AddToWalletIfInvolvingMe(tx, pblock, fUpdate);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
122 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
123
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
124 void static SetBestChain(const CBlockLocator& loc)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
125 {
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
126 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
127 pwallet->SetBestChain(loc);
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
128 }
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
129
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
130 void static UpdatedTransaction(const uint256& hashTx)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
131 {
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
132 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
133 pwallet->UpdatedTransaction(hashTx);
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
134 }
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
135
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
136 void static PrintWallets(const CBlock& block)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
137 {
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
138 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
139 pwallet->PrintWallet(block);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
140 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
141
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
142 void static Inventory(const uint256& hash)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
143 {
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
144 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
145 pwallet->Inventory(hash);
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
146 }
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
147
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
148 void static ResendWalletTransactions()
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
149 {
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
150 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
151 pwallet->ResendWalletTransactions();
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
152 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
153
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
154
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
155
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
156
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
157
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
158
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
159
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
160 //////////////////////////////////////////////////////////////////////////////
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
161 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
162 // mapOrphanTransactions
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
163 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
164
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
165 void static AddOrphanTx(const CDataStream& vMsg)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
166 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
167 CTransaction tx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
168 CDataStream(vMsg) >> tx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
169 uint256 hash = tx.GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
170 if (mapOrphanTransactions.count(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
171 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
172 CDataStream* pvMsg = mapOrphanTransactions[hash] = new CDataStream(vMsg);
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
173 BOOST_FOREACH(const CTxIn& txin, tx.vin)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
174 mapOrphanTransactionsByPrev.insert(make_pair(txin.prevout.hash, pvMsg));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
175 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
176
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
177 void static EraseOrphanTx(uint256 hash)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
178 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
179 if (!mapOrphanTransactions.count(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
180 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
181 const CDataStream* pvMsg = mapOrphanTransactions[hash];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
182 CTransaction tx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
183 CDataStream(*pvMsg) >> tx;
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
184 BOOST_FOREACH(const CTxIn& txin, tx.vin)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
185 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
186 for (multimap<uint256, CDataStream*>::iterator mi = mapOrphanTransactionsByPrev.lower_bound(txin.prevout.hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
187 mi != mapOrphanTransactionsByPrev.upper_bound(txin.prevout.hash);)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
188 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
189 if ((*mi).second == pvMsg)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
190 mapOrphanTransactionsByPrev.erase(mi++);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
191 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
192 mi++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
193 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
194 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
195 delete pvMsg;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
196 mapOrphanTransactions.erase(hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
197 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
198
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
199
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
200
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
201
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
202
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
203
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
204
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
205
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
206 //////////////////////////////////////////////////////////////////////////////
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
207 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
208 // CTransaction and CTxIndex
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
209 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
210
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
211 bool CTransaction::ReadFromDisk(CTxDB& txdb, COutPoint prevout, CTxIndex& txindexRet)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
212 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
213 SetNull();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
214 if (!txdb.ReadTxIndex(prevout.hash, txindexRet))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
215 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
216 if (!ReadFromDisk(txindexRet.pos))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
217 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
218 if (prevout.n >= vout.size())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
219 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
220 SetNull();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
221 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
222 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
223 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
224 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
225
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
226 bool CTransaction::ReadFromDisk(CTxDB& txdb, COutPoint prevout)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
227 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
228 CTxIndex txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
229 return ReadFromDisk(txdb, prevout, txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
230 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
231
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
232 bool CTransaction::ReadFromDisk(COutPoint prevout)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
233 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
234 CTxDB txdb("r");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
235 CTxIndex txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
236 return ReadFromDisk(txdb, prevout, txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
237 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
238
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
239
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
240
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
241 int CMerkleTx::SetMerkleBranch(const CBlock* pblock)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
242 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
243 if (fClient)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
244 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
245 if (hashBlock == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
246 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
247 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
248 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
249 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
250 CBlock blockTmp;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
251 if (pblock == NULL)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
252 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
253 // Load the block this tx is in
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
254 CTxIndex txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
255 if (!CTxDB("r").ReadTxIndex(GetHash(), txindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
256 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
257 if (!blockTmp.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
258 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
259 pblock = &blockTmp;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
260 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
261
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
262 // Update the tx's hashBlock
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
263 hashBlock = pblock->GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
264
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
265 // Locate the transaction
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
266 for (nIndex = 0; nIndex < pblock->vtx.size(); nIndex++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
267 if (pblock->vtx[nIndex] == *(CTransaction*)this)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
268 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
269 if (nIndex == pblock->vtx.size())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
270 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
271 vMerkleBranch.clear();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
272 nIndex = -1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
273 printf("ERROR: SetMerkleBranch() : couldn't find tx in block\n");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
274 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
275 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
276
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
277 // Fill in merkle branch
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
278 vMerkleBranch = pblock->GetMerkleBranch(nIndex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
279 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
280
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
281 // Is the tx in a block that's in the main chain
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
282 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
283 if (mi == mapBlockIndex.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
284 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
285 CBlockIndex* pindex = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
286 if (!pindex || !pindex->IsInMainChain())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
287 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
288
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
289 return pindexBest->nHeight - pindex->nHeight + 1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
290 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
291
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
292
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
293
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
294
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
295
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
296
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
297
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
298 bool CTransaction::CheckTransaction() const
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
299 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
300 // Basic checks that don't depend on any context
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
301 if (vin.empty() || vout.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
302 return error("CTransaction::CheckTransaction() : vin or vout empty");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
303
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
304 // Size limits
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
305 if (::GetSerializeSize(*this, SER_NETWORK) > MAX_BLOCK_SIZE)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
306 return error("CTransaction::CheckTransaction() : size limits failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
307
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
308 // Check for negative or overflow output values
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
309 int64 nValueOut = 0;
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
310 BOOST_FOREACH(const CTxOut& txout, vout)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
311 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
312 if (txout.nValue < 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
313 return error("CTransaction::CheckTransaction() : txout.nValue negative");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
314 if (txout.nValue > MAX_MONEY)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
315 return error("CTransaction::CheckTransaction() : txout.nValue too high");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
316 nValueOut += txout.nValue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
317 if (!MoneyRange(nValueOut))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
318 return error("CTransaction::CheckTransaction() : txout total out of range");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
319 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
320
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
321 if (IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
322 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
323 if (vin[0].scriptSig.size() < 2 || vin[0].scriptSig.size() > 100)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
324 return error("CTransaction::CheckTransaction() : coinbase script size");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
325 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
326 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
327 {
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
328 BOOST_FOREACH(const CTxIn& txin, vin)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
329 if (txin.prevout.IsNull())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
330 return error("CTransaction::CheckTransaction() : prevout is null");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
331 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
332
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
333 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
334 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
335
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
336 bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMissingInputs)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
337 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
338 if (pfMissingInputs)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
339 *pfMissingInputs = false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
340
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
341 if (!CheckTransaction())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
342 return error("AcceptToMemoryPool() : CheckTransaction failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
343
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
344 // Coinbase is only valid in a block, not as a loose transaction
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
345 if (IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
346 return error("AcceptToMemoryPool() : coinbase as individual tx");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
347
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
348 // To help v0.1.5 clients who would see it as a negative number
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
349 if ((int64)nLockTime > INT_MAX)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
350 return error("AcceptToMemoryPool() : not accepting nLockTime beyond 2038 yet");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
351
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
352 // Safety limits
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
353 unsigned int nSize = ::GetSerializeSize(*this, SER_NETWORK);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
354 // Checking ECDSA signatures is a CPU bottleneck, so to avoid denial-of-service
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
355 // attacks disallow transactions with more than one SigOp per 34 bytes.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
356 // 34 bytes because a TxOut is:
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
357 // 20-byte address + 8 byte bitcoin amount + 5 bytes of ops + 1 byte script length
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
358 if (GetSigOpCount() > nSize / 34 || nSize < 100)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
359 return error("AcceptToMemoryPool() : nonstandard transaction");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
360
576
8ec6d5e778e5 Manual merge of jaromil's source tree reorg commit.
Jeff Garzik <jeff@garzik.org>
parents: 575
diff changeset
361 // Rather not work on nonstandard transactions (unless -testnet)
8ec6d5e778e5 Manual merge of jaromil's source tree reorg commit.
Jeff Garzik <jeff@garzik.org>
parents: 575
diff changeset
362 if (!fTestNet && !IsStandard())
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
363 return error("AcceptToMemoryPool() : nonstandard transaction type");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
364
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
365 // Do we already have it?
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
366 uint256 hash = GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
367 CRITICAL_BLOCK(cs_mapTransactions)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
368 if (mapTransactions.count(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
369 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
370 if (fCheckInputs)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
371 if (txdb.ContainsTx(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
372 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
373
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
374 // Check for conflicts with in-memory transactions
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
375 CTransaction* ptxOld = NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
376 for (int i = 0; i < vin.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
377 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
378 COutPoint outpoint = vin[i].prevout;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
379 if (mapNextTx.count(outpoint))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
380 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
381 // Disable replacement feature for now
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
382 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
383
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
384 // Allow replacing with a newer version of the same transaction
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
385 if (i != 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
386 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
387 ptxOld = mapNextTx[outpoint].ptx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
388 if (ptxOld->IsFinal())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
389 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
390 if (!IsNewerThan(*ptxOld))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
391 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
392 for (int i = 0; i < vin.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
393 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
394 COutPoint outpoint = vin[i].prevout;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
395 if (!mapNextTx.count(outpoint) || mapNextTx[outpoint].ptx != ptxOld)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
396 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
397 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
398 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
399 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
400 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
401
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
402 if (fCheckInputs)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
403 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
404 // Check against previous transactions
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
405 map<uint256, CTxIndex> mapUnused;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
406 int64 nFees = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
407 if (!ConnectInputs(txdb, mapUnused, CDiskTxPos(1,1,1), pindexBest, nFees, false, false))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
408 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
409 if (pfMissingInputs)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
410 *pfMissingInputs = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
411 return error("AcceptToMemoryPool() : ConnectInputs failed %s", hash.ToString().substr(0,10).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
412 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
413
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
414 // Don't accept it if it can't get into a block
654
a8a8e3a6f08a bugfix: accept free transactions
Pieter Wuille <pieter.wuille@gmail.com>
parents: 638
diff changeset
415 if (nFees < GetMinFee(1000, true, true))
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
416 return error("AcceptToMemoryPool() : not enough fees");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
417
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
418 // Continuously rate-limit free transactions
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
419 // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
420 // be annoying or make other's transactions take longer to confirm.
638
6046cfa5296f Separate required fee for relaying and creation
Pieter Wuille <pieter.wuille@gmail.com>
parents: 595
diff changeset
421 if (nFees < MIN_RELAY_TX_FEE)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
422 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
423 static CCriticalSection cs;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
424 static double dFreeCount;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
425 static int64 nLastTime;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
426 int64 nNow = GetTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
427
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
428 CRITICAL_BLOCK(cs)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
429 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
430 // Use an exponentially decaying ~10-minute window:
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
431 dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
432 nLastTime = nNow;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
433 // -limitfreerelay unit is thousand-bytes-per-minute
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
434 // At default rate it would take over a month to fill 1GB
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
435 if (dFreeCount > GetArg("-limitfreerelay", 15)*10*1000 && !IsFromMe(*this))
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
436 return error("AcceptToMemoryPool() : free transaction rejected by rate limiter");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
437 if (fDebug)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
438 printf("Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
439 dFreeCount += nSize;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
440 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
441 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
442 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
443
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
444 // Store transaction in memory
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
445 CRITICAL_BLOCK(cs_mapTransactions)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
446 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
447 if (ptxOld)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
448 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
449 printf("AcceptToMemoryPool() : replacing tx %s with new version\n", ptxOld->GetHash().ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
450 ptxOld->RemoveFromMemoryPool();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
451 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
452 AddToMemoryPoolUnchecked();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
453 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
454
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
455 ///// are we sure this is ok when loading transactions or restoring block txes
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
456 // If updated, erase old tx from wallet
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
457 if (ptxOld)
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
458 EraseFromWallets(ptxOld->GetHash());
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
459
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
460 printf("AcceptToMemoryPool(): accepted %s\n", hash.ToString().substr(0,10).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
461 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
462 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
463
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
464 bool CTransaction::AcceptToMemoryPool(bool fCheckInputs, bool* pfMissingInputs)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
465 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
466 CTxDB txdb("r");
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
467 return AcceptToMemoryPool(txdb, fCheckInputs, pfMissingInputs);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
468 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
469
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
470 bool CTransaction::AddToMemoryPoolUnchecked()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
471 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
472 // Add to memory pool without checking anything. Don't call this directly,
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
473 // call AcceptToMemoryPool to properly check the transaction first.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
474 CRITICAL_BLOCK(cs_mapTransactions)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
475 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
476 uint256 hash = GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
477 mapTransactions[hash] = *this;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
478 for (int i = 0; i < vin.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
479 mapNextTx[vin[i].prevout] = CInPoint(&mapTransactions[hash], i);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
480 nTransactionsUpdated++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
481 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
482 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
483 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
484
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
485
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
486 bool CTransaction::RemoveFromMemoryPool()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
487 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
488 // Remove transaction from memory pool
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
489 CRITICAL_BLOCK(cs_mapTransactions)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
490 {
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
491 BOOST_FOREACH(const CTxIn& txin, vin)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
492 mapNextTx.erase(txin.prevout);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
493 mapTransactions.erase(GetHash());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
494 nTransactionsUpdated++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
495 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
496 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
497 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
498
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
499
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
500
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
501
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
502
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
503
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
504 int CMerkleTx::GetDepthInMainChain(int& nHeightRet) const
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
505 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
506 if (hashBlock == 0 || nIndex == -1)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
507 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
508
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
509 // Find the block it claims to be in
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
510 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
511 if (mi == mapBlockIndex.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
512 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
513 CBlockIndex* pindex = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
514 if (!pindex || !pindex->IsInMainChain())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
515 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
516
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
517 // Make sure the merkle branch connects to this block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
518 if (!fMerkleVerified)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
519 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
520 if (CBlock::CheckMerkleBranch(GetHash(), vMerkleBranch, nIndex) != pindex->hashMerkleRoot)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
521 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
522 fMerkleVerified = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
523 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
524
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
525 nHeightRet = pindex->nHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
526 return pindexBest->nHeight - pindex->nHeight + 1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
527 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
528
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
529
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
530 int CMerkleTx::GetBlocksToMaturity() const
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
531 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
532 if (!IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
533 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
534 return max(0, (COINBASE_MATURITY+20) - GetDepthInMainChain());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
535 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
536
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
537
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
538 bool CMerkleTx::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
539 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
540 if (fClient)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
541 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
542 if (!IsInMainChain() && !ClientConnectInputs())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
543 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
544 return CTransaction::AcceptToMemoryPool(txdb, false);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
545 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
546 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
547 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
548 return CTransaction::AcceptToMemoryPool(txdb, fCheckInputs);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
549 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
550 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
551
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
552 bool CMerkleTx::AcceptToMemoryPool()
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
553 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
554 CTxDB txdb("r");
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
555 return AcceptToMemoryPool(txdb);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
556 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
557
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
558
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
559
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
560 bool CWalletTx::AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
561 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
562 CRITICAL_BLOCK(cs_mapTransactions)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
563 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
564 // Add previous supporting transactions first
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
565 BOOST_FOREACH(CMerkleTx& tx, vtxPrev)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
566 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
567 if (!tx.IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
568 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
569 uint256 hash = tx.GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
570 if (!mapTransactions.count(hash) && !txdb.ContainsTx(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
571 tx.AcceptToMemoryPool(txdb, fCheckInputs);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
572 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
573 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
574 return AcceptToMemoryPool(txdb, fCheckInputs);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
575 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
576 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
577 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
578
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
579 bool CWalletTx::AcceptWalletTransaction()
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
580 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
581 CTxDB txdb("r");
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
582 return AcceptWalletTransaction(txdb);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
583 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
584
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
585 int CTxIndex::GetDepthInMainChain() const
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
586 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
587 // Read block header
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
588 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
589 if (!block.ReadFromDisk(pos.nFile, pos.nBlockPos, false))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
590 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
591 // Find the block in the index
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
592 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(block.GetHash());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
593 if (mi == mapBlockIndex.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
594 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
595 CBlockIndex* pindex = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
596 if (!pindex || !pindex->IsInMainChain())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
597 return 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
598 return 1 + nBestHeight - pindex->nHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
599 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
600
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
601
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
602
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
603
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
604
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
605
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
606
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
607
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
608
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
609
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
610 //////////////////////////////////////////////////////////////////////////////
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
611 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
612 // CBlock and CBlockIndex
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
613 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
614
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
615 bool CBlock::ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
616 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
617 if (!fReadTransactions)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
618 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
619 *this = pindex->GetBlockHeader();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
620 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
621 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
622 if (!ReadFromDisk(pindex->nFile, pindex->nBlockPos, fReadTransactions))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
623 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
624 if (GetHash() != pindex->GetBlockHash())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
625 return error("CBlock::ReadFromDisk() : GetHash() doesn't match index");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
626 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
627 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
628
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
629 uint256 static GetOrphanRoot(const CBlock* pblock)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
630 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
631 // Work back to the first block in the orphan chain
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
632 while (mapOrphanBlocks.count(pblock->hashPrevBlock))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
633 pblock = mapOrphanBlocks[pblock->hashPrevBlock];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
634 return pblock->GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
635 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
636
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
637 int64 static GetBlockValue(int nHeight, int64 nFees)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
638 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
639 int64 nSubsidy = 50 * COIN;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
640
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
641 // Subsidy is cut in half every 4 years
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
642 nSubsidy >>= (nHeight / 210000);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
643
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
644 return nSubsidy + nFees;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
645 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
646
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
647 unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
648 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
649 const int64 nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
650 const int64 nTargetSpacing = 10 * 60;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
651 const int64 nInterval = nTargetTimespan / nTargetSpacing;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
652
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
653 // Genesis block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
654 if (pindexLast == NULL)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
655 return bnProofOfWorkLimit.GetCompact();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
656
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
657 // Only change once per interval
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
658 if ((pindexLast->nHeight+1) % nInterval != 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
659 return pindexLast->nBits;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
660
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
661 // Go back by what we want to be 14 days worth of blocks
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
662 const CBlockIndex* pindexFirst = pindexLast;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
663 for (int i = 0; pindexFirst && i < nInterval-1; i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
664 pindexFirst = pindexFirst->pprev;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
665 assert(pindexFirst);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
666
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
667 // Limit adjustment step
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
668 int64 nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
669 printf(" nActualTimespan = %"PRI64d" before bounds\n", nActualTimespan);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
670 if (nActualTimespan < nTargetTimespan/4)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
671 nActualTimespan = nTargetTimespan/4;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
672 if (nActualTimespan > nTargetTimespan*4)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
673 nActualTimespan = nTargetTimespan*4;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
674
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
675 // Retarget
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
676 CBigNum bnNew;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
677 bnNew.SetCompact(pindexLast->nBits);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
678 bnNew *= nActualTimespan;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
679 bnNew /= nTargetTimespan;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
680
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
681 if (bnNew > bnProofOfWorkLimit)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
682 bnNew = bnProofOfWorkLimit;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
683
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
684 /// debug print
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
685 printf("GetNextWorkRequired RETARGET\n");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
686 printf("nTargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n", nTargetTimespan, nActualTimespan);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
687 printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
688 printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
689
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
690 return bnNew.GetCompact();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
691 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
692
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
693 bool CheckProofOfWork(uint256 hash, unsigned int nBits)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
694 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
695 CBigNum bnTarget;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
696 bnTarget.SetCompact(nBits);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
697
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
698 // Check range
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
699 if (bnTarget <= 0 || bnTarget > bnProofOfWorkLimit)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
700 return error("CheckProofOfWork() : nBits below minimum work");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
701
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
702 // Check proof of work matches claimed amount
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
703 if (hash > bnTarget.getuint256())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
704 return error("CheckProofOfWork() : hash doesn't match nBits");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
705
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
706 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
707 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
708
702
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
709 // Return conservative estimate of total number of blocks, 0 if unknown
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
710 int GetTotalBlocksEstimate()
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
711 {
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
712 if(fTestNet)
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
713 {
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
714 return 0;
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
715 }
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
716 else
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
717 {
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
718 return nTotalBlocksEstimate;
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
719 }
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
720 }
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
721
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
722 bool IsInitialBlockDownload()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
723 {
702
ddf3fdf690a5 add GetTotalBlocksEstimate() function, move magic number to constant
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 688
diff changeset
724 if (pindexBest == NULL || nBestHeight < (GetTotalBlocksEstimate()-nInitialBlockThreshold))
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
725 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
726 static int64 nLastUpdate;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
727 static CBlockIndex* pindexLastBest;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
728 if (pindexBest != pindexLastBest)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
729 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
730 pindexLastBest = pindexBest;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
731 nLastUpdate = GetTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
732 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
733 return (GetTime() - nLastUpdate < 10 &&
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
734 pindexBest->GetBlockTime() < GetTime() - 24 * 60 * 60);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
735 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
736
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
737 void static InvalidChainFound(CBlockIndex* pindexNew)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
738 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
739 if (pindexNew->bnChainWork > bnBestInvalidWork)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
740 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
741 bnBestInvalidWork = pindexNew->bnChainWork;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
742 CTxDB().WriteBestInvalidWork(bnBestInvalidWork);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
743 MainFrameRepaint();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
744 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
745 printf("InvalidChainFound: invalid block=%s height=%d work=%s\n", pindexNew->GetBlockHash().ToString().substr(0,20).c_str(), pindexNew->nHeight, pindexNew->bnChainWork.ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
746 printf("InvalidChainFound: current best=%s height=%d work=%s\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainWork.ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
747 if (pindexBest && bnBestInvalidWork > bnBestChainWork + pindexBest->GetBlockWork() * 6)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
748 printf("InvalidChainFound: WARNING: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.\n");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
749 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
750
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
751
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
752
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
753
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
754
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
755
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
756
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
757
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
758
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
759
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
760
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
761 bool CTransaction::DisconnectInputs(CTxDB& txdb)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
762 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
763 // Relinquish previous transactions' spent pointers
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
764 if (!IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
765 {
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
766 BOOST_FOREACH(const CTxIn& txin, vin)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
767 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
768 COutPoint prevout = txin.prevout;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
769
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
770 // Get prev txindex from disk
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
771 CTxIndex txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
772 if (!txdb.ReadTxIndex(prevout.hash, txindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
773 return error("DisconnectInputs() : ReadTxIndex failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
774
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
775 if (prevout.n >= txindex.vSpent.size())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
776 return error("DisconnectInputs() : prevout.n out of range");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
777
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
778 // Mark outpoint as not spent
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
779 txindex.vSpent[prevout.n].SetNull();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
780
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
781 // Write back
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
782 if (!txdb.UpdateTxIndex(prevout.hash, txindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
783 return error("DisconnectInputs() : UpdateTxIndex failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
784 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
785 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
786
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
787 // Remove transaction from index
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
788 if (!txdb.EraseTxIndex(*this))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
789 return error("DisconnectInputs() : EraseTxPos failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
790
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
791 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
792 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
793
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
794
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
795 bool CTransaction::ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx,
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
796 CBlockIndex* pindexBlock, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
797 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
798 // Take over previous transactions' spent pointers
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
799 if (!IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
800 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
801 int64 nValueIn = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
802 for (int i = 0; i < vin.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
803 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
804 COutPoint prevout = vin[i].prevout;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
805
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
806 // Read txindex
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
807 CTxIndex txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
808 bool fFound = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
809 if (fMiner && mapTestPool.count(prevout.hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
810 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
811 // Get txindex from current proposed changes
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
812 txindex = mapTestPool[prevout.hash];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
813 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
814 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
815 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
816 // Read txindex from txdb
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
817 fFound = txdb.ReadTxIndex(prevout.hash, txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
818 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
819 if (!fFound && (fBlock || fMiner))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
820 return fMiner ? false : error("ConnectInputs() : %s prev tx %s index entry not found", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
821
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
822 // Read txPrev
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
823 CTransaction txPrev;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
824 if (!fFound || txindex.pos == CDiskTxPos(1,1,1))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
825 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
826 // Get prev tx from single transactions in memory
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
827 CRITICAL_BLOCK(cs_mapTransactions)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
828 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
829 if (!mapTransactions.count(prevout.hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
830 return error("ConnectInputs() : %s mapTransactions prev not found %s", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
831 txPrev = mapTransactions[prevout.hash];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
832 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
833 if (!fFound)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
834 txindex.vSpent.resize(txPrev.vout.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
835 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
836 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
837 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
838 // Get prev tx from disk
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
839 if (!txPrev.ReadFromDisk(txindex.pos))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
840 return error("ConnectInputs() : %s ReadFromDisk prev tx %s failed", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
841 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
842
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
843 if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
844 return error("ConnectInputs() : %s prevout.n out of range %d %d %d prev tx %s\n%s", GetHash().ToString().substr(0,10).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size(), prevout.hash.ToString().substr(0,10).c_str(), txPrev.ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
845
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
846 // If prev is coinbase, check that it's matured
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
847 if (txPrev.IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
848 for (CBlockIndex* pindex = pindexBlock; pindex && pindexBlock->nHeight - pindex->nHeight < COINBASE_MATURITY; pindex = pindex->pprev)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
849 if (pindex->nBlockPos == txindex.pos.nBlockPos && pindex->nFile == txindex.pos.nFile)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
850 return error("ConnectInputs() : tried to spend coinbase at depth %d", pindexBlock->nHeight - pindex->nHeight);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
851
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
852 // Verify signature
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
853 if (!VerifySignature(txPrev, *this, i))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
854 return error("ConnectInputs() : %s VerifySignature failed", GetHash().ToString().substr(0,10).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
855
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
856 // Check for conflicts
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
857 if (!txindex.vSpent[prevout.n].IsNull())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
858 return fMiner ? false : error("ConnectInputs() : %s prev tx already used at %s", GetHash().ToString().substr(0,10).c_str(), txindex.vSpent[prevout.n].ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
859
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
860 // Check for negative or overflow input values
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
861 nValueIn += txPrev.vout[prevout.n].nValue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
862 if (!MoneyRange(txPrev.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
863 return error("ConnectInputs() : txin values out of range");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
864
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
865 // Mark outpoints as spent
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
866 txindex.vSpent[prevout.n] = posThisTx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
867
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
868 // Write back
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
869 if (fBlock)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
870 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
871 if (!txdb.UpdateTxIndex(prevout.hash, txindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
872 return error("ConnectInputs() : UpdateTxIndex failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
873 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
874 else if (fMiner)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
875 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
876 mapTestPool[prevout.hash] = txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
877 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
878 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
879
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
880 if (nValueIn < GetValueOut())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
881 return error("ConnectInputs() : %s value in < value out", GetHash().ToString().substr(0,10).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
882
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
883 // Tally transaction fees
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
884 int64 nTxFee = nValueIn - GetValueOut();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
885 if (nTxFee < 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
886 return error("ConnectInputs() : %s nTxFee < 0", GetHash().ToString().substr(0,10).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
887 if (nTxFee < nMinFee)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
888 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
889 nFees += nTxFee;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
890 if (!MoneyRange(nFees))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
891 return error("ConnectInputs() : nFees out of range");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
892 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
893
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
894 if (fBlock)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
895 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
896 // Add transaction to disk index
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
897 if (!txdb.AddTxIndex(*this, posThisTx, pindexBlock->nHeight))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
898 return error("ConnectInputs() : AddTxPos failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
899 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
900 else if (fMiner)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
901 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
902 // Add transaction to test pool
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
903 mapTestPool[GetHash()] = CTxIndex(CDiskTxPos(1,1,1), vout.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
904 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
905
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
906 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
907 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
908
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
909
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
910 bool CTransaction::ClientConnectInputs()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
911 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
912 if (IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
913 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
914
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
915 // Take over previous transactions' spent pointers
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
916 CRITICAL_BLOCK(cs_mapTransactions)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
917 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
918 int64 nValueIn = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
919 for (int i = 0; i < vin.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
920 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
921 // Get prev tx from single transactions in memory
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
922 COutPoint prevout = vin[i].prevout;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
923 if (!mapTransactions.count(prevout.hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
924 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
925 CTransaction& txPrev = mapTransactions[prevout.hash];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
926
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
927 if (prevout.n >= txPrev.vout.size())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
928 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
929
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
930 // Verify signature
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
931 if (!VerifySignature(txPrev, *this, i))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
932 return error("ConnectInputs() : VerifySignature failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
933
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
934 ///// this is redundant with the mapNextTx stuff, not sure which I want to get rid of
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
935 ///// this has to go away now that posNext is gone
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
936 // // Check for conflicts
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
937 // if (!txPrev.vout[prevout.n].posNext.IsNull())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
938 // return error("ConnectInputs() : prev tx already used");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
939 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
940 // // Flag outpoints as used
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
941 // txPrev.vout[prevout.n].posNext = posThisTx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
942
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
943 nValueIn += txPrev.vout[prevout.n].nValue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
944
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
945 if (!MoneyRange(txPrev.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
946 return error("ClientConnectInputs() : txin values out of range");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
947 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
948 if (GetValueOut() > nValueIn)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
949 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
950 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
951
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
952 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
953 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
954
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
955
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
956
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
957
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
958 bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
959 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
960 // Disconnect in reverse order
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
961 for (int i = vtx.size()-1; i >= 0; i--)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
962 if (!vtx[i].DisconnectInputs(txdb))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
963 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
964
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
965 // Update block index on disk without changing it in memory.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
966 // The memory index structure will be changed after the db commits.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
967 if (pindex->pprev)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
968 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
969 CDiskBlockIndex blockindexPrev(pindex->pprev);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
970 blockindexPrev.hashNext = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
971 if (!txdb.WriteBlockIndex(blockindexPrev))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
972 return error("DisconnectBlock() : WriteBlockIndex failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
973 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
974
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
975 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
976 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
977
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
978 bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
979 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
980 // Check it again in case a previous version let a bad block in
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
981 if (!CheckBlock())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
982 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
983
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
984 //// issue here: it doesn't know the version
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
985 unsigned int nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK) - 1 + GetSizeOfCompactSize(vtx.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
986
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
987 map<uint256, CTxIndex> mapUnused;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
988 int64 nFees = 0;
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
989 BOOST_FOREACH(CTransaction& tx, vtx)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
990 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
991 CDiskTxPos posThisTx(pindex->nFile, pindex->nBlockPos, nTxPos);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
992 nTxPos += ::GetSerializeSize(tx, SER_DISK);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
993
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
994 if (!tx.ConnectInputs(txdb, mapUnused, posThisTx, pindex, nFees, true, false))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
995 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
996 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
997
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
998 if (vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
999 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1000
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1001 // Update block index on disk without changing it in memory.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1002 // The memory index structure will be changed after the db commits.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1003 if (pindex->pprev)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1004 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1005 CDiskBlockIndex blockindexPrev(pindex->pprev);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1006 blockindexPrev.hashNext = pindex->GetBlockHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1007 if (!txdb.WriteBlockIndex(blockindexPrev))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1008 return error("ConnectBlock() : WriteBlockIndex failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1009 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1010
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1011 // Watch for transactions paying to me
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1012 BOOST_FOREACH(CTransaction& tx, vtx)
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1013 SyncWithWallets(tx, this, true);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1014
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1015 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1016 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1017
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1018 bool static Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1019 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1020 printf("REORGANIZE\n");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1021
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1022 // Find the fork
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1023 CBlockIndex* pfork = pindexBest;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1024 CBlockIndex* plonger = pindexNew;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1025 while (pfork != plonger)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1026 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1027 while (plonger->nHeight > pfork->nHeight)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1028 if (!(plonger = plonger->pprev))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1029 return error("Reorganize() : plonger->pprev is null");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1030 if (pfork == plonger)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1031 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1032 if (!(pfork = pfork->pprev))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1033 return error("Reorganize() : pfork->pprev is null");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1034 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1035
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1036 // List of what to disconnect
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1037 vector<CBlockIndex*> vDisconnect;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1038 for (CBlockIndex* pindex = pindexBest; pindex != pfork; pindex = pindex->pprev)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1039 vDisconnect.push_back(pindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1040
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1041 // List of what to connect
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1042 vector<CBlockIndex*> vConnect;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1043 for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1044 vConnect.push_back(pindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1045 reverse(vConnect.begin(), vConnect.end());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1046
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1047 // Disconnect shorter branch
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1048 vector<CTransaction> vResurrect;
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1049 BOOST_FOREACH(CBlockIndex* pindex, vDisconnect)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1050 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1051 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1052 if (!block.ReadFromDisk(pindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1053 return error("Reorganize() : ReadFromDisk for disconnect failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1054 if (!block.DisconnectBlock(txdb, pindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1055 return error("Reorganize() : DisconnectBlock failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1056
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1057 // Queue memory transactions to resurrect
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1058 BOOST_FOREACH(const CTransaction& tx, block.vtx)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1059 if (!tx.IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1060 vResurrect.push_back(tx);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1061 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1062
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1063 // Connect longer branch
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1064 vector<CTransaction> vDelete;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1065 for (int i = 0; i < vConnect.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1066 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1067 CBlockIndex* pindex = vConnect[i];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1068 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1069 if (!block.ReadFromDisk(pindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1070 return error("Reorganize() : ReadFromDisk for connect failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1071 if (!block.ConnectBlock(txdb, pindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1072 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1073 // Invalid block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1074 txdb.TxnAbort();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1075 return error("Reorganize() : ConnectBlock failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1076 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1077
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1078 // Queue memory transactions to delete
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1079 BOOST_FOREACH(const CTransaction& tx, block.vtx)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1080 vDelete.push_back(tx);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1081 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1082 if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash()))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1083 return error("Reorganize() : WriteHashBestChain failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1084
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1085 // Make sure it's successfully written to disk before changing memory structure
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1086 if (!txdb.TxnCommit())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1087 return error("Reorganize() : TxnCommit failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1088
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1089 // Disconnect shorter branch
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1090 BOOST_FOREACH(CBlockIndex* pindex, vDisconnect)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1091 if (pindex->pprev)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1092 pindex->pprev->pnext = NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1093
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1094 // Connect longer branch
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1095 BOOST_FOREACH(CBlockIndex* pindex, vConnect)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1096 if (pindex->pprev)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1097 pindex->pprev->pnext = pindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1098
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1099 // Resurrect memory transactions that were in the disconnected branch
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1100 BOOST_FOREACH(CTransaction& tx, vResurrect)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1101 tx.AcceptToMemoryPool(txdb, false);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1102
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1103 // Delete redundant memory transactions that are in the connected branch
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1104 BOOST_FOREACH(CTransaction& tx, vDelete)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1105 tx.RemoveFromMemoryPool();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1106
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1107 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1108 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1109
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1110
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1111 bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1112 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1113 uint256 hash = GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1114
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1115 txdb.TxnBegin();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1116 if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1117 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1118 txdb.WriteHashBestChain(hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1119 if (!txdb.TxnCommit())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1120 return error("SetBestChain() : TxnCommit failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1121 pindexGenesisBlock = pindexNew;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1122 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1123 else if (hashPrevBlock == hashBestChain)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1124 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1125 // Adding to current best branch
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1126 if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1127 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1128 txdb.TxnAbort();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1129 InvalidChainFound(pindexNew);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1130 return error("SetBestChain() : ConnectBlock failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1131 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1132 if (!txdb.TxnCommit())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1133 return error("SetBestChain() : TxnCommit failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1134
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1135 // Add to current best branch
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1136 pindexNew->pprev->pnext = pindexNew;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1137
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1138 // Delete redundant memory transactions
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1139 BOOST_FOREACH(CTransaction& tx, vtx)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1140 tx.RemoveFromMemoryPool();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1141 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1142 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1143 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1144 // New best branch
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1145 if (!Reorganize(txdb, pindexNew))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1146 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1147 txdb.TxnAbort();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1148 InvalidChainFound(pindexNew);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1149 return error("SetBestChain() : Reorganize failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1150 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1151 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1152
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1153 // Update best block in wallet (so we can detect restored wallets)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1154 if (!IsInitialBlockDownload())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1155 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1156 const CBlockLocator locator(pindexNew);
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1157 ::SetBestChain(locator);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1158 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1159
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1160 // New best block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1161 hashBestChain = hash;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1162 pindexBest = pindexNew;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1163 nBestHeight = pindexBest->nHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1164 bnBestChainWork = pindexNew->bnChainWork;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1165 nTimeBestReceived = GetTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1166 nTransactionsUpdated++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1167 printf("SetBestChain: new best=%s height=%d work=%s\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainWork.ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1168
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1169 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1170 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1171
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1172
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1173 bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1174 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1175 // Check for duplicate
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1176 uint256 hash = GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1177 if (mapBlockIndex.count(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1178 return error("AddToBlockIndex() : %s already exists", hash.ToString().substr(0,20).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1179
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1180 // Construct new block index object
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1181 CBlockIndex* pindexNew = new CBlockIndex(nFile, nBlockPos, *this);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1182 if (!pindexNew)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1183 return error("AddToBlockIndex() : new CBlockIndex failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1184 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1185 pindexNew->phashBlock = &((*mi).first);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1186 map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(hashPrevBlock);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1187 if (miPrev != mapBlockIndex.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1188 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1189 pindexNew->pprev = (*miPrev).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1190 pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1191 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1192 pindexNew->bnChainWork = (pindexNew->pprev ? pindexNew->pprev->bnChainWork : 0) + pindexNew->GetBlockWork();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1193
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1194 CTxDB txdb;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1195 txdb.TxnBegin();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1196 txdb.WriteBlockIndex(CDiskBlockIndex(pindexNew));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1197 if (!txdb.TxnCommit())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1198 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1199
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1200 // New best
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1201 if (pindexNew->bnChainWork > bnBestChainWork)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1202 if (!SetBestChain(txdb, pindexNew))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1203 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1204
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1205 txdb.Close();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1206
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1207 if (pindexNew == pindexBest)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1208 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1209 // Notify UI to display prev block's coinbase if it was ours
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1210 static uint256 hashPrevBestCoinBase;
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1211 UpdatedTransaction(hashPrevBestCoinBase);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1212 hashPrevBestCoinBase = vtx[0].GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1213 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1214
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1215 MainFrameRepaint();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1216 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1217 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1218
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1219
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1220
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1221
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1222 bool CBlock::CheckBlock() const
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1223 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1224 // These are checks that are independent of context
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1225 // that can be verified before saving an orphan block.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1226
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1227 // Size limits
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1228 if (vtx.empty() || vtx.size() > MAX_BLOCK_SIZE || ::GetSerializeSize(*this, SER_NETWORK) > MAX_BLOCK_SIZE)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1229 return error("CheckBlock() : size limits failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1230
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1231 // Check proof of work matches claimed amount
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1232 if (!CheckProofOfWork(GetHash(), nBits))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1233 return error("CheckBlock() : proof of work failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1234
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1235 // Check timestamp
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1236 if (GetBlockTime() > GetAdjustedTime() + 2 * 60 * 60)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1237 return error("CheckBlock() : block timestamp too far in the future");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1238
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1239 // First transaction must be coinbase, the rest must not be
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1240 if (vtx.empty() || !vtx[0].IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1241 return error("CheckBlock() : first tx is not coinbase");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1242 for (int i = 1; i < vtx.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1243 if (vtx[i].IsCoinBase())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1244 return error("CheckBlock() : more than one coinbase");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1245
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1246 // Check transactions
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1247 BOOST_FOREACH(const CTransaction& tx, vtx)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1248 if (!tx.CheckTransaction())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1249 return error("CheckBlock() : CheckTransaction failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1250
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1251 // Check that it's not full of nonstandard transactions
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1252 if (GetSigOpCount() > MAX_BLOCK_SIGOPS)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1253 return error("CheckBlock() : too many nonstandard transactions");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1254
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1255 // Check merkleroot
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1256 if (hashMerkleRoot != BuildMerkleTree())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1257 return error("CheckBlock() : hashMerkleRoot mismatch");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1258
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1259 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1260 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1261
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1262 bool CBlock::AcceptBlock()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1263 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1264 // Check for duplicate
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1265 uint256 hash = GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1266 if (mapBlockIndex.count(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1267 return error("AcceptBlock() : block already in mapBlockIndex");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1268
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1269 // Get prev block index
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1270 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashPrevBlock);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1271 if (mi == mapBlockIndex.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1272 return error("AcceptBlock() : prev block not found");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1273 CBlockIndex* pindexPrev = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1274 int nHeight = pindexPrev->nHeight+1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1275
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1276 // Check proof of work
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1277 if (nBits != GetNextWorkRequired(pindexPrev))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1278 return error("AcceptBlock() : incorrect proof of work");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1279
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1280 // Check timestamp against prev
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1281 if (GetBlockTime() <= pindexPrev->GetMedianTimePast())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1282 return error("AcceptBlock() : block's timestamp is too early");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1283
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1284 // Check that all transactions are finalized
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1285 BOOST_FOREACH(const CTransaction& tx, vtx)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1286 if (!tx.IsFinal(nHeight, GetBlockTime()))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1287 return error("AcceptBlock() : contains a non-final transaction");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1288
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1289 // Check that the block chain matches the known block chain up to a checkpoint
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1290 if (!fTestNet)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1291 if ((nHeight == 11111 && hash != uint256("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d")) ||
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1292 (nHeight == 33333 && hash != uint256("0x000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6")) ||
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1293 (nHeight == 68555 && hash != uint256("0x00000000001e1b4903550a0b96e9a9405c8a95f387162e4944e8d9fbe501cd6a")) ||
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1294 (nHeight == 70567 && hash != uint256("0x00000000006a49b14bcf27462068f1264c961f11fa2e0eddd2be0791e1d4124a")) ||
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1295 (nHeight == 74000 && hash != uint256("0x0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20")) ||
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1296 (nHeight == 105000 && hash != uint256("0x00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97")) ||
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1297 (nHeight == 118000 && hash != uint256("0x000000000000774a7f8a7a12dc906ddb9e17e75d684f15e00f8767f9e8f36553")))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1298 return error("AcceptBlock() : rejected by checkpoint lockin at %d", nHeight);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1299
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1300 // Write block to history file
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1301 if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK)))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1302 return error("AcceptBlock() : out of disk space");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1303 unsigned int nFile = -1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1304 unsigned int nBlockPos = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1305 if (!WriteToDisk(nFile, nBlockPos))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1306 return error("AcceptBlock() : WriteToDisk failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1307 if (!AddToBlockIndex(nFile, nBlockPos))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1308 return error("AcceptBlock() : AddToBlockIndex failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1309
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1310 // Relay inventory, but don't relay old inventory during initial block download
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1311 if (hashBestChain == hash)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1312 CRITICAL_BLOCK(cs_vNodes)
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1313 BOOST_FOREACH(CNode* pnode, vNodes)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1314 if (nBestHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 118000))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1315 pnode->PushInventory(CInv(MSG_BLOCK, hash));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1316
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1317 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1318 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1319
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1320 bool static ProcessBlock(CNode* pfrom, CBlock* pblock)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1321 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1322 // Check for duplicate
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1323 uint256 hash = pblock->GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1324 if (mapBlockIndex.count(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1325 return error("ProcessBlock() : already have block %d %s", mapBlockIndex[hash]->nHeight, hash.ToString().substr(0,20).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1326 if (mapOrphanBlocks.count(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1327 return error("ProcessBlock() : already have block (orphan) %s", hash.ToString().substr(0,20).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1328
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1329 // Preliminary checks
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1330 if (!pblock->CheckBlock())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1331 return error("ProcessBlock() : CheckBlock FAILED");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1332
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1333 // If don't already have its previous block, shunt it off to holding area until we get it
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1334 if (!mapBlockIndex.count(pblock->hashPrevBlock))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1335 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1336 printf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString().substr(0,20).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1337 CBlock* pblock2 = new CBlock(*pblock);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1338 mapOrphanBlocks.insert(make_pair(hash, pblock2));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1339 mapOrphanBlocksByPrev.insert(make_pair(pblock2->hashPrevBlock, pblock2));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1340
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1341 // Ask this guy to fill in what we're missing
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1342 if (pfrom)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1343 pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(pblock2));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1344 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1345 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1346
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1347 // Store to disk
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1348 if (!pblock->AcceptBlock())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1349 return error("ProcessBlock() : AcceptBlock FAILED");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1350
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1351 // Recursively process any orphan blocks that depended on this one
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1352 vector<uint256> vWorkQueue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1353 vWorkQueue.push_back(hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1354 for (int i = 0; i < vWorkQueue.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1355 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1356 uint256 hashPrev = vWorkQueue[i];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1357 for (multimap<uint256, CBlock*>::iterator mi = mapOrphanBlocksByPrev.lower_bound(hashPrev);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1358 mi != mapOrphanBlocksByPrev.upper_bound(hashPrev);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1359 ++mi)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1360 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1361 CBlock* pblockOrphan = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1362 if (pblockOrphan->AcceptBlock())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1363 vWorkQueue.push_back(pblockOrphan->GetHash());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1364 mapOrphanBlocks.erase(pblockOrphan->GetHash());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1365 delete pblockOrphan;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1366 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1367 mapOrphanBlocksByPrev.erase(hashPrev);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1368 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1369
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1370 printf("ProcessBlock: ACCEPTED\n");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1371 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1372 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1373
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1374
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1375
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1376
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1377
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1378
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1379
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1380
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1381 template<typename Stream>
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1382 bool static ScanMessageStart(Stream& s)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1383 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1384 // Scan ahead to the next pchMessageStart, which should normally be immediately
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1385 // at the file pointer. Leaves file pointer at end of pchMessageStart.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1386 s.clear(0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1387 short prevmask = s.exceptions(0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1388 const char* p = BEGIN(pchMessageStart);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1389 try
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1390 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1391 loop
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1392 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1393 char c;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1394 s.read(&c, 1);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1395 if (s.fail())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1396 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1397 s.clear(0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1398 s.exceptions(prevmask);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1399 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1400 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1401 if (*p != c)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1402 p = BEGIN(pchMessageStart);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1403 if (*p == c)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1404 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1405 if (++p == END(pchMessageStart))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1406 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1407 s.clear(0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1408 s.exceptions(prevmask);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1409 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1410 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1411 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1412 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1413 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1414 catch (...)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1415 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1416 s.clear(0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1417 s.exceptions(prevmask);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1418 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1419 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1420 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1421
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1422 bool CheckDiskSpace(uint64 nAdditionalBytes)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1423 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1424 uint64 nFreeBytesAvailable = filesystem::space(GetDataDir()).available;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1425
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1426 // Check for 15MB because database could create another 10MB log file at any time
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1427 if (nFreeBytesAvailable < (uint64)15000000 + nAdditionalBytes)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1428 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1429 fShutdown = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1430 string strMessage = _("Warning: Disk space is low ");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1431 strMiscWarning = strMessage;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1432 printf("*** %s\n", strMessage.c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1433 ThreadSafeMessageBox(strMessage, "Bitcoin", wxOK | wxICON_EXCLAMATION);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1434 CreateThread(Shutdown, NULL);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1435 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1436 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1437 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1438 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1439
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1440 FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1441 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1442 if (nFile == -1)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1443 return NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1444 FILE* file = fopen(strprintf("%s/blk%04d.dat", GetDataDir().c_str(), nFile).c_str(), pszMode);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1445 if (!file)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1446 return NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1447 if (nBlockPos != 0 && !strchr(pszMode, 'a') && !strchr(pszMode, 'w'))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1448 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1449 if (fseek(file, nBlockPos, SEEK_SET) != 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1450 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1451 fclose(file);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1452 return NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1453 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1454 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1455 return file;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1456 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1457
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1458 static unsigned int nCurrentBlockFile = 1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1459
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1460 FILE* AppendBlockFile(unsigned int& nFileRet)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1461 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1462 nFileRet = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1463 loop
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1464 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1465 FILE* file = OpenBlockFile(nCurrentBlockFile, 0, "ab");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1466 if (!file)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1467 return NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1468 if (fseek(file, 0, SEEK_END) != 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1469 return NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1470 // FAT32 filesize max 4GB, fseek and ftell max 2GB, so we must stay under 2GB
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1471 if (ftell(file) < 0x7F000000 - MAX_SIZE)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1472 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1473 nFileRet = nCurrentBlockFile;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1474 return file;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1475 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1476 fclose(file);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1477 nCurrentBlockFile++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1478 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1479 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1480
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1481 bool LoadBlockIndex(bool fAllowNew)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1482 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1483 if (fTestNet)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1484 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1485 hashGenesisBlock = uint256("0x00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1486 bnProofOfWorkLimit = CBigNum(~uint256(0) >> 28);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1487 pchMessageStart[0] = 0xfa;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1488 pchMessageStart[1] = 0xbf;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1489 pchMessageStart[2] = 0xb5;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1490 pchMessageStart[3] = 0xda;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1491 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1492
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1493 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1494 // Load block index
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1495 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1496 CTxDB txdb("cr");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1497 if (!txdb.LoadBlockIndex())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1498 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1499 txdb.Close();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1500
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1501 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1502 // Init with genesis block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1503 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1504 if (mapBlockIndex.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1505 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1506 if (!fAllowNew)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1507 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1508
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1509 // Genesis Block:
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1510 // CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1511 // CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1512 // CTxIn(COutPoint(000000, -1), coinbase 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1513 // CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1514 // vMerkleTree: 4a5e1e
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1515
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1516 // Genesis block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1517 const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1518 CTransaction txNew;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1519 txNew.vin.resize(1);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1520 txNew.vout.resize(1);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1521 txNew.vin[0].scriptSig = CScript() << 486604799 << CBigNum(4) << vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1522 txNew.vout[0].nValue = 50 * COIN;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1523 txNew.vout[0].scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1524 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1525 block.vtx.push_back(txNew);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1526 block.hashPrevBlock = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1527 block.hashMerkleRoot = block.BuildMerkleTree();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1528 block.nVersion = 1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1529 block.nTime = 1231006505;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1530 block.nBits = 0x1d00ffff;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1531 block.nNonce = 2083236893;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1532
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1533 if (fTestNet)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1534 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1535 block.nTime = 1296688602;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1536 block.nBits = 0x1d07fff8;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1537 block.nNonce = 384568319;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1538 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1539
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1540 //// debug print
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1541 printf("%s\n", block.GetHash().ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1542 printf("%s\n", hashGenesisBlock.ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1543 printf("%s\n", block.hashMerkleRoot.ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1544 assert(block.hashMerkleRoot == uint256("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1545 block.print();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1546 assert(block.GetHash() == hashGenesisBlock);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1547
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1548 // Start new block file
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1549 unsigned int nFile;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1550 unsigned int nBlockPos;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1551 if (!block.WriteToDisk(nFile, nBlockPos))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1552 return error("LoadBlockIndex() : writing genesis block to disk failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1553 if (!block.AddToBlockIndex(nFile, nBlockPos))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1554 return error("LoadBlockIndex() : genesis block not accepted");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1555 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1556
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1557 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1558 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1559
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1560
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1561
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1562 void PrintBlockTree()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1563 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1564 // precompute tree structure
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1565 map<CBlockIndex*, vector<CBlockIndex*> > mapNext;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1566 for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1567 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1568 CBlockIndex* pindex = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1569 mapNext[pindex->pprev].push_back(pindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1570 // test
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1571 //while (rand() % 3 == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1572 // mapNext[pindex->pprev].push_back(pindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1573 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1574
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1575 vector<pair<int, CBlockIndex*> > vStack;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1576 vStack.push_back(make_pair(0, pindexGenesisBlock));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1577
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1578 int nPrevCol = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1579 while (!vStack.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1580 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1581 int nCol = vStack.back().first;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1582 CBlockIndex* pindex = vStack.back().second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1583 vStack.pop_back();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1584
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1585 // print split or gap
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1586 if (nCol > nPrevCol)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1587 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1588 for (int i = 0; i < nCol-1; i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1589 printf("| ");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1590 printf("|\\\n");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1591 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1592 else if (nCol < nPrevCol)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1593 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1594 for (int i = 0; i < nCol; i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1595 printf("| ");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1596 printf("|\n");
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1597 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1598 nPrevCol = nCol;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1599
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1600 // print columns
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1601 for (int i = 0; i < nCol; i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1602 printf("| ");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1603
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1604 // print item
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1605 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1606 block.ReadFromDisk(pindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1607 printf("%d (%u,%u) %s %s tx %d",
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1608 pindex->nHeight,
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1609 pindex->nFile,
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1610 pindex->nBlockPos,
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1611 block.GetHash().ToString().substr(0,20).c_str(),
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1612 DateTimeStrFormat("%x %H:%M:%S", block.GetBlockTime()).c_str(),
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1613 block.vtx.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1614
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1615 PrintWallets(block);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1616
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1617 // put the main timechain first
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1618 vector<CBlockIndex*>& vNext = mapNext[pindex];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1619 for (int i = 0; i < vNext.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1620 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1621 if (vNext[i]->pnext)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1622 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1623 swap(vNext[0], vNext[i]);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1624 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1625 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1626 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1627
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1628 // iterate children
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1629 for (int i = 0; i < vNext.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1630 vStack.push_back(make_pair(nCol+i, vNext[i]));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1631 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1632 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1633
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1634
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1635
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1636
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1637
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1638
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1639
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1640
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1641
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1642
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1643 //////////////////////////////////////////////////////////////////////////////
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1644 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1645 // CAlert
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1646 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1647
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1648 map<uint256, CAlert> mapAlerts;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1649 CCriticalSection cs_mapAlerts;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1650
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1651 string GetWarnings(string strFor)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1652 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1653 int nPriority = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1654 string strStatusBar;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1655 string strRPC;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1656 if (GetBoolArg("-testsafemode"))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1657 strRPC = "test";
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1658
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1659 // Misc warnings like out of disk space and clock is wrong
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1660 if (strMiscWarning != "")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1661 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1662 nPriority = 1000;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1663 strStatusBar = strMiscWarning;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1664 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1665
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1666 // Longer invalid proof-of-work chain
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1667 if (pindexBest && bnBestInvalidWork > bnBestChainWork + pindexBest->GetBlockWork() * 6)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1668 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1669 nPriority = 2000;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1670 strStatusBar = strRPC = "WARNING: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.";
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1671 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1672
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1673 // Alerts
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1674 CRITICAL_BLOCK(cs_mapAlerts)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1675 {
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1676 BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1677 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1678 const CAlert& alert = item.second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1679 if (alert.AppliesToMe() && alert.nPriority > nPriority)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1680 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1681 nPriority = alert.nPriority;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1682 strStatusBar = alert.strStatusBar;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1683 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1684 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1685 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1686
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1687 if (strFor == "statusbar")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1688 return strStatusBar;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1689 else if (strFor == "rpc")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1690 return strRPC;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1691 assert(("GetWarnings() : invalid parameter", false));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1692 return "error";
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1693 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1694
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1695 bool CAlert::ProcessAlert()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1696 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1697 if (!CheckSignature())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1698 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1699 if (!IsInEffect())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1700 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1701
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1702 CRITICAL_BLOCK(cs_mapAlerts)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1703 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1704 // Cancel previous alerts
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1705 for (map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1706 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1707 const CAlert& alert = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1708 if (Cancels(alert))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1709 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1710 printf("cancelling alert %d\n", alert.nID);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1711 mapAlerts.erase(mi++);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1712 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1713 else if (!alert.IsInEffect())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1714 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1715 printf("expiring alert %d\n", alert.nID);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1716 mapAlerts.erase(mi++);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1717 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1718 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1719 mi++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1720 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1721
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1722 // Check if this alert has been cancelled
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1723 BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1724 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1725 const CAlert& alert = item.second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1726 if (alert.Cancels(*this))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1727 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1728 printf("alert already cancelled by %d\n", alert.nID);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1729 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1730 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1731 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1732
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1733 // Add to mapAlerts
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1734 mapAlerts.insert(make_pair(GetHash(), *this));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1735 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1736
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1737 printf("accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1738 MainFrameRepaint();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1739 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1740 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1741
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1742
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1743
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1744
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1745
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1746
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1747
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1748
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1749 //////////////////////////////////////////////////////////////////////////////
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1750 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1751 // Messages
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1752 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1753
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1754
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1755 bool static AlreadyHave(CTxDB& txdb, const CInv& inv)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1756 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1757 switch (inv.type)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1758 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1759 case MSG_TX: return mapTransactions.count(inv.hash) || mapOrphanTransactions.count(inv.hash) || txdb.ContainsTx(inv.hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1760 case MSG_BLOCK: return mapBlockIndex.count(inv.hash) || mapOrphanBlocks.count(inv.hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1761 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1762 // Don't know what it is, just say we already got one
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1763 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1764 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1765
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1766
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1767
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1768
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1769 // The message start string is designed to be unlikely to occur in normal data.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1770 // The characters are rarely used upper ascii, not valid as UTF-8, and produce
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1771 // a large 4-byte int at any alignment.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1772 char pchMessageStart[4] = { 0xf9, 0xbe, 0xb4, 0xd9 };
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1773
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1774
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1775 bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1776 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1777 static map<unsigned int, vector<unsigned char> > mapReuseKey;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1778 RandAddSeedPerfmon();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1779 if (fDebug)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1780 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1781 printf("received: %s (%d bytes)\n", strCommand.c_str(), vRecv.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1782 if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1783 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1784 printf("dropmessagestest DROPPING RECV MESSAGE\n");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1785 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1786 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1787
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1788
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1789
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1790
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1791
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1792 if (strCommand == "version")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1793 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1794 // Each connection can only send one version message
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1795 if (pfrom->nVersion != 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1796 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1797
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1798 int64 nTime;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1799 CAddress addrMe;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1800 CAddress addrFrom;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1801 uint64 nNonce = 1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1802 vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1803 if (pfrom->nVersion == 10300)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1804 pfrom->nVersion = 300;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1805 if (pfrom->nVersion >= 106 && !vRecv.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1806 vRecv >> addrFrom >> nNonce;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1807 if (pfrom->nVersion >= 106 && !vRecv.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1808 vRecv >> pfrom->strSubVer;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1809 if (pfrom->nVersion >= 209 && !vRecv.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1810 vRecv >> pfrom->nStartingHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1811
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1812 if (pfrom->nVersion == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1813 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1814
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1815 // Disconnect if we connected to ourself
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1816 if (nNonce == nLocalHostNonce && nNonce > 1)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1817 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1818 printf("connected to self at %s, disconnecting\n", pfrom->addr.ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1819 pfrom->fDisconnect = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1820 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1821 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1822
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1823 // Be shy and don't send version until we hear
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1824 if (pfrom->fInbound)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1825 pfrom->PushVersion();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1826
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1827 pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1828
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1829 AddTimeData(pfrom->addr.ip, nTime);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1830
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1831 // Change version
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1832 if (pfrom->nVersion >= 209)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1833 pfrom->PushMessage("verack");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1834 pfrom->vSend.SetVersion(min(pfrom->nVersion, VERSION));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1835 if (pfrom->nVersion < 209)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1836 pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1837
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1838 if (!pfrom->fInbound)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1839 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1840 // Advertise our address
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1841 if (addrLocalHost.IsRoutable() && !fUseProxy)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1842 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1843 CAddress addr(addrLocalHost);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1844 addr.nTime = GetAdjustedTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1845 pfrom->PushAddress(addr);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1846 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1847
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1848 // Get recent addresses
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1849 if (pfrom->nVersion >= 31402 || mapAddresses.size() < 1000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1850 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1851 pfrom->PushMessage("getaddr");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1852 pfrom->fGetAddr = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1853 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1854 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1855
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1856 // Ask the first connected node for block updates
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1857 static int nAskedForBlocks;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1858 if (!pfrom->fClient && (nAskedForBlocks < 1 || vNodes.size() <= 1))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1859 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1860 nAskedForBlocks++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1861 pfrom->PushGetBlocks(pindexBest, uint256(0));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1862 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1863
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1864 // Relay alerts
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1865 CRITICAL_BLOCK(cs_mapAlerts)
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1866 BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1867 item.second.RelayTo(pfrom);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1868
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1869 pfrom->fSuccessfullyConnected = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1870
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1871 printf("version message: version %d, blocks=%d\n", pfrom->nVersion, pfrom->nStartingHeight);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1872 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1873
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1874
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1875 else if (pfrom->nVersion == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1876 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1877 // Must have a version message before anything else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1878 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1879 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1880
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1881
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1882 else if (strCommand == "verack")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1883 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1884 pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1885 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1886
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1887
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1888 else if (strCommand == "addr")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1889 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1890 vector<CAddress> vAddr;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1891 vRecv >> vAddr;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1892
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1893 // Don't want addr from older versions unless seeding
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1894 if (pfrom->nVersion < 209)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1895 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1896 if (pfrom->nVersion < 31402 && mapAddresses.size() > 1000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1897 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1898 if (vAddr.size() > 1000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1899 return error("message addr size() = %d", vAddr.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1900
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1901 // Store the new addresses
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1902 int64 nNow = GetAdjustedTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1903 int64 nSince = nNow - 10 * 60;
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1904 BOOST_FOREACH(CAddress& addr, vAddr)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1905 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1906 if (fShutdown)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1907 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1908 // ignore IPv6 for now, since it isn't implemented anyway
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1909 if (!addr.IsIPv4())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1910 continue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1911 if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1912 addr.nTime = nNow - 5 * 24 * 60 * 60;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1913 AddAddress(addr, 2 * 60 * 60);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1914 pfrom->AddAddressKnown(addr);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1915 if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1916 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1917 // Relay to a limited number of other nodes
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1918 CRITICAL_BLOCK(cs_vNodes)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1919 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1920 // Use deterministic randomness to send to the same nodes for 24 hours
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1921 // at a time so the setAddrKnowns of the chosen nodes prevent repeats
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1922 static uint256 hashSalt;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1923 if (hashSalt == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1924 RAND_bytes((unsigned char*)&hashSalt, sizeof(hashSalt));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1925 uint256 hashRand = hashSalt ^ (((int64)addr.ip)<<32) ^ ((GetTime()+addr.ip)/(24*60*60));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1926 hashRand = Hash(BEGIN(hashRand), END(hashRand));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1927 multimap<uint256, CNode*> mapMix;
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1928 BOOST_FOREACH(CNode* pnode, vNodes)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1929 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1930 if (pnode->nVersion < 31402)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1931 continue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1932 unsigned int nPointer;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1933 memcpy(&nPointer, &pnode, sizeof(nPointer));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1934 uint256 hashKey = hashRand ^ nPointer;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1935 hashKey = Hash(BEGIN(hashKey), END(hashKey));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1936 mapMix.insert(make_pair(hashKey, pnode));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1937 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1938 int nRelayNodes = 2;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1939 for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1940 ((*mi).second)->PushAddress(addr);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1941 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1942 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1943 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1944 if (vAddr.size() < 1000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1945 pfrom->fGetAddr = false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1946 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1947
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1948
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1949 else if (strCommand == "inv")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1950 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1951 vector<CInv> vInv;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1952 vRecv >> vInv;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1953 if (vInv.size() > 50000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1954 return error("message inv size() = %d", vInv.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1955
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1956 CTxDB txdb("r");
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1957 BOOST_FOREACH(const CInv& inv, vInv)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1958 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1959 if (fShutdown)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1960 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1961 pfrom->AddInventoryKnown(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1962
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1963 bool fAlreadyHave = AlreadyHave(txdb, inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1964 printf(" got inventory: %s %s\n", inv.ToString().c_str(), fAlreadyHave ? "have" : "new");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1965
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1966 if (!fAlreadyHave)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1967 pfrom->AskFor(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1968 else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1969 pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[inv.hash]));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1970
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1971 // Track requests for our stuff
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
1972 Inventory(inv.hash);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1973 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1974 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1975
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1976
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1977 else if (strCommand == "getdata")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1978 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1979 vector<CInv> vInv;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1980 vRecv >> vInv;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1981 if (vInv.size() > 50000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1982 return error("message getdata size() = %d", vInv.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1983
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
1984 BOOST_FOREACH(const CInv& inv, vInv)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1985 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1986 if (fShutdown)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1987 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1988 printf("received getdata for: %s\n", inv.ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1989
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1990 if (inv.type == MSG_BLOCK)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1991 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1992 // Send block from disk
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1993 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(inv.hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1994 if (mi != mapBlockIndex.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1995 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1996 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1997 block.ReadFromDisk((*mi).second);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1998 pfrom->PushMessage("block", block);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1999
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2000 // Trigger them to send a getblocks request for the next batch of inventory
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2001 if (inv.hash == pfrom->hashContinue)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2002 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2003 // Bypass PushInventory, this must send even if redundant,
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2004 // and we want it right after the last block so they don't
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2005 // wait for other stuff first.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2006 vector<CInv> vInv;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2007 vInv.push_back(CInv(MSG_BLOCK, hashBestChain));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2008 pfrom->PushMessage("inv", vInv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2009 pfrom->hashContinue = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2010 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2011 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2012 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2013 else if (inv.IsKnownType())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2014 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2015 // Send stream from relay memory
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2016 CRITICAL_BLOCK(cs_mapRelay)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2017 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2018 map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2019 if (mi != mapRelay.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2020 pfrom->PushMessage(inv.GetCommand(), (*mi).second);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2021 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2022 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2023
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2024 // Track requests for our stuff
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2025 Inventory(inv.hash);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2026 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2027 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2028
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2029
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2030 else if (strCommand == "getblocks")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2031 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2032 CBlockLocator locator;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2033 uint256 hashStop;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2034 vRecv >> locator >> hashStop;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2035
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2036 // Find the last block the caller has in the main chain
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2037 CBlockIndex* pindex = locator.GetBlockIndex();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2038
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2039 // Send the rest of the chain
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2040 if (pindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2041 pindex = pindex->pnext;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2042 int nLimit = 500 + locator.GetDistanceBack();
718
3ce286fc9dc1 Limit response to getblocks to half of output buffer size
Pieter Wuille <pieter.wuille@gmail.com>
parents: 703
diff changeset
2043 unsigned int nBytes = 0;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2044 printf("getblocks %d to %s limit %d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString().substr(0,20).c_str(), nLimit);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2045 for (; pindex; pindex = pindex->pnext)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2046 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2047 if (pindex->GetBlockHash() == hashStop)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2048 {
718
3ce286fc9dc1 Limit response to getblocks to half of output buffer size
Pieter Wuille <pieter.wuille@gmail.com>
parents: 703
diff changeset
2049 printf(" getblocks stopping at %d %s (%u bytes)\n", pindex->nHeight, pindex->GetBlockHash().ToString().substr(0,20).c_str(), nBytes);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2050 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2051 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2052 pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
718
3ce286fc9dc1 Limit response to getblocks to half of output buffer size
Pieter Wuille <pieter.wuille@gmail.com>
parents: 703
diff changeset
2053 CBlock block;
3ce286fc9dc1 Limit response to getblocks to half of output buffer size
Pieter Wuille <pieter.wuille@gmail.com>
parents: 703
diff changeset
2054 block.ReadFromDisk(pindex, true);
3ce286fc9dc1 Limit response to getblocks to half of output buffer size
Pieter Wuille <pieter.wuille@gmail.com>
parents: 703
diff changeset
2055 nBytes += block.GetSerializeSize(SER_NETWORK);
3ce286fc9dc1 Limit response to getblocks to half of output buffer size
Pieter Wuille <pieter.wuille@gmail.com>
parents: 703
diff changeset
2056 if (--nLimit <= 0 || nBytes >= SendBufferSize()/2)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2057 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2058 // When this block is requested, we'll send an inv that'll make them
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2059 // getblocks the next batch of inventory.
718
3ce286fc9dc1 Limit response to getblocks to half of output buffer size
Pieter Wuille <pieter.wuille@gmail.com>
parents: 703
diff changeset
2060 printf(" getblocks stopping at limit %d %s (%u bytes)\n", pindex->nHeight, pindex->GetBlockHash().ToString().substr(0,20).c_str(), nBytes);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2061 pfrom->hashContinue = pindex->GetBlockHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2062 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2063 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2064 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2065 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2066
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2067
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2068 else if (strCommand == "getheaders")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2069 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2070 CBlockLocator locator;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2071 uint256 hashStop;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2072 vRecv >> locator >> hashStop;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2073
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2074 CBlockIndex* pindex = NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2075 if (locator.IsNull())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2076 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2077 // If locator is null, return the hashStop block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2078 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashStop);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2079 if (mi == mapBlockIndex.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2080 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2081 pindex = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2082 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2083 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2084 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2085 // Find the last block the caller has in the main chain
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2086 pindex = locator.GetBlockIndex();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2087 if (pindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2088 pindex = pindex->pnext;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2089 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2090
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2091 vector<CBlock> vHeaders;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2092 int nLimit = 2000 + locator.GetDistanceBack();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2093 printf("getheaders %d to %s limit %d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString().substr(0,20).c_str(), nLimit);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2094 for (; pindex; pindex = pindex->pnext)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2095 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2096 vHeaders.push_back(pindex->GetBlockHeader());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2097 if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2098 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2099 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2100 pfrom->PushMessage("headers", vHeaders);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2101 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2102
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2103
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2104 else if (strCommand == "tx")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2105 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2106 vector<uint256> vWorkQueue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2107 CDataStream vMsg(vRecv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2108 CTransaction tx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2109 vRecv >> tx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2110
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2111 CInv inv(MSG_TX, tx.GetHash());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2112 pfrom->AddInventoryKnown(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2113
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2114 bool fMissingInputs = false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2115 if (tx.AcceptToMemoryPool(true, &fMissingInputs))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2116 {
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2117 SyncWithWallets(tx, NULL, true);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2118 RelayMessage(inv, vMsg);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2119 mapAlreadyAskedFor.erase(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2120 vWorkQueue.push_back(inv.hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2121
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2122 // Recursively process any orphan transactions that depended on this one
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2123 for (int i = 0; i < vWorkQueue.size(); i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2124 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2125 uint256 hashPrev = vWorkQueue[i];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2126 for (multimap<uint256, CDataStream*>::iterator mi = mapOrphanTransactionsByPrev.lower_bound(hashPrev);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2127 mi != mapOrphanTransactionsByPrev.upper_bound(hashPrev);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2128 ++mi)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2129 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2130 const CDataStream& vMsg = *((*mi).second);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2131 CTransaction tx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2132 CDataStream(vMsg) >> tx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2133 CInv inv(MSG_TX, tx.GetHash());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2134
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2135 if (tx.AcceptToMemoryPool(true))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2136 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2137 printf(" accepted orphan tx %s\n", inv.hash.ToString().substr(0,10).c_str());
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2138 SyncWithWallets(tx, NULL, true);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2139 RelayMessage(inv, vMsg);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2140 mapAlreadyAskedFor.erase(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2141 vWorkQueue.push_back(inv.hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2142 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2143 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2144 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2145
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
2146 BOOST_FOREACH(uint256 hash, vWorkQueue)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2147 EraseOrphanTx(hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2148 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2149 else if (fMissingInputs)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2150 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2151 printf("storing orphan tx %s\n", inv.hash.ToString().substr(0,10).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2152 AddOrphanTx(vMsg);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2153 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2154 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2155
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2156
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2157 else if (strCommand == "block")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2158 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2159 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2160 vRecv >> block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2161
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2162 printf("received block %s\n", block.GetHash().ToString().substr(0,20).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2163 // block.print();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2164
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2165 CInv inv(MSG_BLOCK, block.GetHash());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2166 pfrom->AddInventoryKnown(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2167
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2168 if (ProcessBlock(pfrom, &block))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2169 mapAlreadyAskedFor.erase(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2170 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2171
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2172
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2173 else if (strCommand == "getaddr")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2174 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2175 // Nodes rebroadcast an addr every 24 hours
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2176 pfrom->vAddrToSend.clear();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2177 int64 nSince = GetAdjustedTime() - 3 * 60 * 60; // in the last 3 hours
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2178 CRITICAL_BLOCK(cs_mapAddresses)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2179 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2180 unsigned int nCount = 0;
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
2181 BOOST_FOREACH(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapAddresses)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2182 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2183 const CAddress& addr = item.second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2184 if (addr.nTime > nSince)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2185 nCount++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2186 }
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
2187 BOOST_FOREACH(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapAddresses)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2188 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2189 const CAddress& addr = item.second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2190 if (addr.nTime > nSince && GetRand(nCount) < 2500)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2191 pfrom->PushAddress(addr);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2192 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2193 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2194 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2195
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2196
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2197 else if (strCommand == "checkorder")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2198 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2199 uint256 hashReply;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2200 vRecv >> hashReply;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2201
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2202 if (!GetBoolArg("-allowreceivebyip"))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2203 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2204 pfrom->PushMessage("reply", hashReply, (int)2, string(""));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2205 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2206 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2207
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2208 CWalletTx order;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2209 vRecv >> order;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2210
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2211 /// we have a chance to check the order here
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2212
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2213 // Keep giving the same key to the same ip until they use it
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2214 if (!mapReuseKey.count(pfrom->addr.ip))
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2215 mapReuseKey[pfrom->addr.ip] = pwalletMain->GetKeyFromKeyPool();
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2216
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2217 // Send back approval of order and pubkey to use
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2218 CScript scriptPubKey;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2219 scriptPubKey << mapReuseKey[pfrom->addr.ip] << OP_CHECKSIG;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2220 pfrom->PushMessage("reply", hashReply, (int)0, scriptPubKey);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2221 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2222
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2223
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2224 else if (strCommand == "reply")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2225 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2226 uint256 hashReply;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2227 vRecv >> hashReply;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2228
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2229 CRequestTracker tracker;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2230 CRITICAL_BLOCK(pfrom->cs_mapRequests)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2231 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2232 map<uint256, CRequestTracker>::iterator mi = pfrom->mapRequests.find(hashReply);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2233 if (mi != pfrom->mapRequests.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2234 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2235 tracker = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2236 pfrom->mapRequests.erase(mi);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2237 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2238 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2239 if (!tracker.IsNull())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2240 tracker.fn(tracker.param1, vRecv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2241 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2242
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2243
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2244 else if (strCommand == "ping")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2245 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2246 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2247
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2248
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2249 else if (strCommand == "alert")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2250 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2251 CAlert alert;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2252 vRecv >> alert;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2253
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2254 if (alert.ProcessAlert())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2255 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2256 // Relay
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2257 pfrom->setKnown.insert(alert.GetHash());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2258 CRITICAL_BLOCK(cs_vNodes)
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
2259 BOOST_FOREACH(CNode* pnode, vNodes)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2260 alert.RelayTo(pnode);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2261 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2262 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2263
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2264
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2265 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2266 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2267 // Ignore unknown commands for extensibility
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2268 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2269
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2270
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2271 // Update the last seen time for this node's address
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2272 if (pfrom->fNetworkNode)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2273 if (strCommand == "version" || strCommand == "addr" || strCommand == "inv" || strCommand == "getdata" || strCommand == "ping")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2274 AddressCurrentlyConnected(pfrom->addr);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2275
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2276
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2277 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2278 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2279
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2280 bool ProcessMessages(CNode* pfrom)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2281 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2282 CDataStream& vRecv = pfrom->vRecv;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2283 if (vRecv.empty())
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2284 return true;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2285 //if (fDebug)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2286 // printf("ProcessMessages(%u bytes)\n", vRecv.size());
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2287
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2288 //
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2289 // Message format
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2290 // (4) message start
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2291 // (12) command
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2292 // (4) size
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2293 // (4) checksum
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2294 // (x) data
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2295 //
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2296
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2297 loop
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2298 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2299 // Scan for message start
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2300 CDataStream::iterator pstart = search(vRecv.begin(), vRecv.end(), BEGIN(pchMessageStart), END(pchMessageStart));
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2301 int nHeaderSize = vRecv.GetSerializeSize(CMessageHeader());
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2302 if (vRecv.end() - pstart < nHeaderSize)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2303 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2304 if (vRecv.size() > nHeaderSize)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2305 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2306 printf("\n\nPROCESSMESSAGE MESSAGESTART NOT FOUND\n\n");
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2307 vRecv.erase(vRecv.begin(), vRecv.end() - nHeaderSize);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2308 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2309 break;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2310 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2311 if (pstart - vRecv.begin() > 0)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2312 printf("\n\nPROCESSMESSAGE SKIPPED %d BYTES\n\n", pstart - vRecv.begin());
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2313 vRecv.erase(vRecv.begin(), pstart);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2314
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2315 // Read header
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2316 vector<char> vHeaderSave(vRecv.begin(), vRecv.begin() + nHeaderSize);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2317 CMessageHeader hdr;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2318 vRecv >> hdr;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2319 if (!hdr.IsValid())
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2320 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2321 printf("\n\nPROCESSMESSAGE: ERRORS IN HEADER %s\n\n\n", hdr.GetCommand().c_str());
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2322 continue;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2323 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2324 string strCommand = hdr.GetCommand();
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2325
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2326 // Message size
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2327 unsigned int nMessageSize = hdr.nMessageSize;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2328 if (nMessageSize > MAX_SIZE)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2329 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2330 printf("ProcessMessage(%s, %u bytes) : nMessageSize > MAX_SIZE\n", strCommand.c_str(), nMessageSize);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2331 continue;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2332 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2333 if (nMessageSize > vRecv.size())
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2334 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2335 // Rewind and wait for rest of message
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2336 vRecv.insert(vRecv.begin(), vHeaderSave.begin(), vHeaderSave.end());
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2337 break;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2338 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2339
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2340 // Checksum
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2341 if (vRecv.GetVersion() >= 209)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2342 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2343 uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2344 unsigned int nChecksum = 0;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2345 memcpy(&nChecksum, &hash, sizeof(nChecksum));
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2346 if (nChecksum != hdr.nChecksum)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2347 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2348 printf("ProcessMessage(%s, %u bytes) : CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n",
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2349 strCommand.c_str(), nMessageSize, nChecksum, hdr.nChecksum);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2350 continue;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2351 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2352 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2353
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2354 // Copy message to its own buffer
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2355 CDataStream vMsg(vRecv.begin(), vRecv.begin() + nMessageSize, vRecv.nType, vRecv.nVersion);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2356 vRecv.ignore(nMessageSize);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2357
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2358 // Process message
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2359 bool fRet = false;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2360 try
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2361 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2362 CRITICAL_BLOCK(cs_main)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2363 fRet = ProcessMessage(pfrom, strCommand, vMsg);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2364 if (fShutdown)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2365 return true;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2366 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2367 catch (std::ios_base::failure& e)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2368 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2369 if (strstr(e.what(), "end of data"))
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2370 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2371 // Allow exceptions from underlength message on vRecv
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2372 printf("ProcessMessage(%s, %u bytes) : Exception '%s' caught, normally caused by a message being shorter than its stated length\n", strCommand.c_str(), nMessageSize, e.what());
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2373 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2374 else if (strstr(e.what(), "size too large"))
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2375 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2376 // Allow exceptions from overlong size
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2377 printf("ProcessMessage(%s, %u bytes) : Exception '%s' caught\n", strCommand.c_str(), nMessageSize, e.what());
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2378 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2379 else
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2380 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2381 PrintExceptionContinue(&e, "ProcessMessage()");
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2382 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2383 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2384 catch (std::exception& e) {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2385 PrintExceptionContinue(&e, "ProcessMessage()");
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2386 } catch (...) {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2387 PrintExceptionContinue(NULL, "ProcessMessage()");
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2388 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2389
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2390 if (!fRet)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2391 printf("ProcessMessage(%s, %u bytes) FAILED\n", strCommand.c_str(), nMessageSize);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2392 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2393
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2394 vRecv.Compact();
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2395 return true;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
2396 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2397
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2398
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2399 bool SendMessages(CNode* pto, bool fSendTrickle)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2400 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2401 CRITICAL_BLOCK(cs_main)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2402 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2403 // Don't send anything until we get their version message
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2404 if (pto->nVersion == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2405 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2406
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2407 // Keep-alive ping
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2408 if (pto->nLastSend && GetTime() - pto->nLastSend > 30 * 60 && pto->vSend.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2409 pto->PushMessage("ping");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2410
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2411 // Resend wallet transactions that haven't gotten in a block yet
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2412 ResendWalletTransactions();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2413
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2414 // Address refresh broadcast
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2415 static int64 nLastRebroadcast;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2416 if (GetTime() - nLastRebroadcast > 24 * 60 * 60)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2417 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2418 nLastRebroadcast = GetTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2419 CRITICAL_BLOCK(cs_vNodes)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2420 {
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
2421 BOOST_FOREACH(CNode* pnode, vNodes)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2422 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2423 // Periodically clear setAddrKnown to allow refresh broadcasts
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2424 pnode->setAddrKnown.clear();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2425
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2426 // Rebroadcast our address
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2427 if (addrLocalHost.IsRoutable() && !fUseProxy)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2428 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2429 CAddress addr(addrLocalHost);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2430 addr.nTime = GetAdjustedTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2431 pnode->PushAddress(addr);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2432 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2433 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2434 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2435 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2436
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2437 // Clear out old addresses periodically so it's not too much work at once
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2438 static int64 nLastClear;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2439 if (nLastClear == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2440 nLastClear = GetTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2441 if (GetTime() - nLastClear > 10 * 60 && vNodes.size() >= 3)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2442 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2443 nLastClear = GetTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2444 CRITICAL_BLOCK(cs_mapAddresses)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2445 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2446 CAddrDB addrdb;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2447 int64 nSince = GetAdjustedTime() - 14 * 24 * 60 * 60;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2448 for (map<vector<unsigned char>, CAddress>::iterator mi = mapAddresses.begin();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2449 mi != mapAddresses.end();)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2450 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2451 const CAddress& addr = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2452 if (addr.nTime < nSince)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2453 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2454 if (mapAddresses.size() < 1000 || GetTime() > nLastClear + 20)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2455 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2456 addrdb.EraseAddress(addr);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2457 mapAddresses.erase(mi++);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2458 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2459 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2460 mi++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2461 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2462 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2463 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2464
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2465
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2466 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2467 // Message: addr
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2468 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2469 if (fSendTrickle)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2470 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2471 vector<CAddress> vAddr;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2472 vAddr.reserve(pto->vAddrToSend.size());
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
2473 BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2474 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2475 // returns true if wasn't already contained in the set
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2476 if (pto->setAddrKnown.insert(addr).second)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2477 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2478 vAddr.push_back(addr);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2479 // receiver rejects addr messages larger than 1000
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2480 if (vAddr.size() >= 1000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2481 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2482 pto->PushMessage("addr", vAddr);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2483 vAddr.clear();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2484 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2485 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2486 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2487 pto->vAddrToSend.clear();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2488 if (!vAddr.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2489 pto->PushMessage("addr", vAddr);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2490 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2491
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2492
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2493 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2494 // Message: inventory
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2495 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2496 vector<CInv> vInv;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2497 vector<CInv> vInvWait;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2498 CRITICAL_BLOCK(pto->cs_inventory)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2499 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2500 vInv.reserve(pto->vInventoryToSend.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2501 vInvWait.reserve(pto->vInventoryToSend.size());
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
2502 BOOST_FOREACH(const CInv& inv, pto->vInventoryToSend)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2503 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2504 if (pto->setInventoryKnown.count(inv))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2505 continue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2506
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2507 // trickle out tx inv to protect privacy
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2508 if (inv.type == MSG_TX && !fSendTrickle)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2509 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2510 // 1/4 of tx invs blast to all immediately
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2511 static uint256 hashSalt;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2512 if (hashSalt == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2513 RAND_bytes((unsigned char*)&hashSalt, sizeof(hashSalt));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2514 uint256 hashRand = inv.hash ^ hashSalt;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2515 hashRand = Hash(BEGIN(hashRand), END(hashRand));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2516 bool fTrickleWait = ((hashRand & 3) != 0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2517
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2518 // always trickle our own transactions
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2519 if (!fTrickleWait)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2520 {
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2521 CWalletTx wtx;
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2522 if (GetTransaction(inv.hash, wtx))
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2523 if (wtx.fFromMe)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2524 fTrickleWait = true;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2525 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2526
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2527 if (fTrickleWait)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2528 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2529 vInvWait.push_back(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2530 continue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2531 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2532 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2533
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2534 // returns true if wasn't already contained in the set
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2535 if (pto->setInventoryKnown.insert(inv).second)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2536 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2537 vInv.push_back(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2538 if (vInv.size() >= 1000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2539 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2540 pto->PushMessage("inv", vInv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2541 vInv.clear();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2542 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2543 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2544 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2545 pto->vInventoryToSend = vInvWait;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2546 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2547 if (!vInv.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2548 pto->PushMessage("inv", vInv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2549
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2550
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2551 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2552 // Message: getdata
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2553 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2554 vector<CInv> vGetData;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2555 int64 nNow = GetTime() * 1000000;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2556 CTxDB txdb("r");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2557 while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2558 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2559 const CInv& inv = (*pto->mapAskFor.begin()).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2560 if (!AlreadyHave(txdb, inv))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2561 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2562 printf("sending getdata: %s\n", inv.ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2563 vGetData.push_back(inv);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2564 if (vGetData.size() >= 1000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2565 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2566 pto->PushMessage("getdata", vGetData);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2567 vGetData.clear();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2568 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2569 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2570 pto->mapAskFor.erase(pto->mapAskFor.begin());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2571 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2572 if (!vGetData.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2573 pto->PushMessage("getdata", vGetData);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2574
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2575 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2576 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2577 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2578
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2579
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2580
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2581
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2582
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2583
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2584
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2585
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2586
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2587
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2588
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2589
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2590
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2591
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2592 //////////////////////////////////////////////////////////////////////////////
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2593 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2594 // BitcoinMiner
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2595 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2596
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2597 int static FormatHashBlocks(void* pbuffer, unsigned int len)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2598 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2599 unsigned char* pdata = (unsigned char*)pbuffer;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2600 unsigned int blocks = 1 + ((len + 8) / 64);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2601 unsigned char* pend = pdata + 64 * blocks;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2602 memset(pdata + len, 0, 64 * blocks - len);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2603 pdata[len] = 0x80;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2604 unsigned int bits = len * 8;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2605 pend[-1] = (bits >> 0) & 0xff;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2606 pend[-2] = (bits >> 8) & 0xff;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2607 pend[-3] = (bits >> 16) & 0xff;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2608 pend[-4] = (bits >> 24) & 0xff;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2609 return blocks;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2610 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2611
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2612 using CryptoPP::ByteReverse;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2613
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2614 static const unsigned int pSHA256InitState[8] =
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2615 {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2616
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2617 inline void SHA256Transform(void* pstate, void* pinput, const void* pinit)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2618 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2619 memcpy(pstate, pinit, 32);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2620 CryptoPP::SHA256::Transform((CryptoPP::word32*)pstate, (CryptoPP::word32*)pinput);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2621 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2622
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2623 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2624 // ScanHash scans nonces looking for a hash with at least some zero bits.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2625 // It operates on big endian data. Caller does the byte reversing.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2626 // All input buffers are 16-byte aligned. nNonce is usually preserved
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2627 // between calls, but periodically or if nNonce is 0xffff0000 or above,
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2628 // the block is rebuilt and nNonce starts over at zero.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2629 //
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2630 unsigned int static ScanHash_CryptoPP(char* pmidstate, char* pdata, char* phash1, char* phash, unsigned int& nHashesDone)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2631 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2632 unsigned int& nNonce = *(unsigned int*)(pdata + 12);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2633 for (;;)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2634 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2635 // Crypto++ SHA-256
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2636 // Hash pdata using pmidstate as the starting state into
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2637 // preformatted buffer phash1, then hash phash1 into phash
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2638 nNonce++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2639 SHA256Transform(phash1, pdata, pmidstate);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2640 SHA256Transform(phash, phash1, pSHA256InitState);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2641
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2642 // Return the nonce if the hash has at least some zero bits,
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2643 // caller will check if it has enough to reach the target
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2644 if (((unsigned short*)phash)[14] == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2645 return nNonce;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2646
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2647 // If nothing found after trying for a while, return -1
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2648 if ((nNonce & 0xffff) == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2649 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2650 nHashesDone = 0xffff+1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2651 return -1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2652 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2653 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2654 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2655
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2656
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2657 class COrphan
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2658 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2659 public:
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2660 CTransaction* ptx;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2661 set<uint256> setDependsOn;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2662 double dPriority;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2663
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2664 COrphan(CTransaction* ptxIn)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2665 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2666 ptx = ptxIn;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2667 dPriority = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2668 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2669
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2670 void print() const
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2671 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2672 printf("COrphan(hash=%s, dPriority=%.1f)\n", ptx->GetHash().ToString().substr(0,10).c_str(), dPriority);
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
2673 BOOST_FOREACH(uint256 hash, setDependsOn)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2674 printf(" setDependsOn %s\n", hash.ToString().substr(0,10).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2675 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2676 };
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2677
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2678
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2679 CBlock* CreateNewBlock(CReserveKey& reservekey)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2680 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2681 CBlockIndex* pindexPrev = pindexBest;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2682
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2683 // Create new block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2684 auto_ptr<CBlock> pblock(new CBlock());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2685 if (!pblock.get())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2686 return NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2687
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2688 // Create coinbase tx
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2689 CTransaction txNew;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2690 txNew.vin.resize(1);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2691 txNew.vin[0].prevout.SetNull();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2692 txNew.vout.resize(1);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2693 txNew.vout[0].scriptPubKey << reservekey.GetReservedKey() << OP_CHECKSIG;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2694
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2695 // Add our coinbase tx as first transaction
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2696 pblock->vtx.push_back(txNew);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2697
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2698 // Collect memory pool transactions into the block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2699 int64 nFees = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2700 CRITICAL_BLOCK(cs_main)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2701 CRITICAL_BLOCK(cs_mapTransactions)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2702 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2703 CTxDB txdb("r");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2704
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2705 // Priority order to process transactions
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2706 list<COrphan> vOrphan; // list memory doesn't move
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2707 map<uint256, vector<COrphan*> > mapDependers;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2708 multimap<double, CTransaction*> mapPriority;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2709 for (map<uint256, CTransaction>::iterator mi = mapTransactions.begin(); mi != mapTransactions.end(); ++mi)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2710 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2711 CTransaction& tx = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2712 if (tx.IsCoinBase() || !tx.IsFinal())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2713 continue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2714
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2715 COrphan* porphan = NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2716 double dPriority = 0;
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
2717 BOOST_FOREACH(const CTxIn& txin, tx.vin)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2718 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2719 // Read prev transaction
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2720 CTransaction txPrev;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2721 CTxIndex txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2722 if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2723 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2724 // Has to wait for dependencies
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2725 if (!porphan)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2726 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2727 // Use list for automatic deletion
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2728 vOrphan.push_back(COrphan(&tx));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2729 porphan = &vOrphan.back();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2730 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2731 mapDependers[txin.prevout.hash].push_back(porphan);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2732 porphan->setDependsOn.insert(txin.prevout.hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2733 continue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2734 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2735 int64 nValueIn = txPrev.vout[txin.prevout.n].nValue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2736
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2737 // Read block header
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2738 int nConf = txindex.GetDepthInMainChain();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2739
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2740 dPriority += (double)nValueIn * nConf;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2741
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2742 if (fDebug && GetBoolArg("-printpriority"))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2743 printf("priority nValueIn=%-12I64d nConf=%-5d dPriority=%-20.1f\n", nValueIn, nConf, dPriority);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2744 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2745
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2746 // Priority is sum(valuein * age) / txsize
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2747 dPriority /= ::GetSerializeSize(tx, SER_NETWORK);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2748
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2749 if (porphan)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2750 porphan->dPriority = dPriority;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2751 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2752 mapPriority.insert(make_pair(-dPriority, &(*mi).second));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2753
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2754 if (fDebug && GetBoolArg("-printpriority"))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2755 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2756 printf("priority %-20.1f %s\n%s", dPriority, tx.GetHash().ToString().substr(0,10).c_str(), tx.ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2757 if (porphan)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2758 porphan->print();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2759 printf("\n");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2760 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2761 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2762
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2763 // Collect transactions into block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2764 map<uint256, CTxIndex> mapTestPool;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2765 uint64 nBlockSize = 1000;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2766 int nBlockSigOps = 100;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2767 while (!mapPriority.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2768 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2769 // Take highest priority transaction off priority queue
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2770 double dPriority = -(*mapPriority.begin()).first;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2771 CTransaction& tx = *(*mapPriority.begin()).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2772 mapPriority.erase(mapPriority.begin());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2773
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2774 // Size limits
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2775 unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2776 if (nBlockSize + nTxSize >= MAX_BLOCK_SIZE_GEN)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2777 continue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2778 int nTxSigOps = tx.GetSigOpCount();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2779 if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2780 continue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2781
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2782 // Transaction fee required depends on block size
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2783 bool fAllowFree = (nBlockSize + nTxSize < 4000 || CTransaction::AllowFree(dPriority));
638
6046cfa5296f Separate required fee for relaying and creation
Pieter Wuille <pieter.wuille@gmail.com>
parents: 595
diff changeset
2784 int64 nMinFee = tx.GetMinFee(nBlockSize, fAllowFree, true);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2785
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2786 // Connecting shouldn't fail due to dependency on other memory pool transactions
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2787 // because we're already processing them in order of dependency
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2788 map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2789 if (!tx.ConnectInputs(txdb, mapTestPoolTmp, CDiskTxPos(1,1,1), pindexPrev, nFees, false, true, nMinFee))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2790 continue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2791 swap(mapTestPool, mapTestPoolTmp);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2792
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2793 // Added
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2794 pblock->vtx.push_back(tx);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2795 nBlockSize += nTxSize;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2796 nBlockSigOps += nTxSigOps;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2797
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2798 // Add transactions that depend on this one to the priority queue
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2799 uint256 hash = tx.GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2800 if (mapDependers.count(hash))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2801 {
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 588
diff changeset
2802 BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2803 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2804 if (!porphan->setDependsOn.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2805 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2806 porphan->setDependsOn.erase(hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2807 if (porphan->setDependsOn.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2808 mapPriority.insert(make_pair(-porphan->dPriority, porphan->ptx));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2809 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2810 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2811 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2812 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2813 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2814 pblock->vtx[0].vout[0].nValue = GetBlockValue(pindexPrev->nHeight+1, nFees);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2815
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2816 // Fill in header
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2817 pblock->hashPrevBlock = pindexPrev->GetBlockHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2818 pblock->hashMerkleRoot = pblock->BuildMerkleTree();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2819 pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2820 pblock->nBits = GetNextWorkRequired(pindexPrev);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2821 pblock->nNonce = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2822
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2823 return pblock.release();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2824 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2825
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2826
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2827 void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce, int64& nPrevTime)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2828 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2829 // Update nExtraNonce
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2830 int64 nNow = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2831 if (++nExtraNonce >= 0x7f && nNow > nPrevTime+1)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2832 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2833 nExtraNonce = 1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2834 nPrevTime = nNow;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2835 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2836 pblock->vtx[0].vin[0].scriptSig = CScript() << pblock->nBits << CBigNum(nExtraNonce);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2837 pblock->hashMerkleRoot = pblock->BuildMerkleTree();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2838 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2839
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2840
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2841 void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2842 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2843 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2844 // Prebuild hash buffers
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2845 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2846 struct
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2847 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2848 struct unnamed2
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2849 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2850 int nVersion;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2851 uint256 hashPrevBlock;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2852 uint256 hashMerkleRoot;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2853 unsigned int nTime;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2854 unsigned int nBits;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2855 unsigned int nNonce;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2856 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2857 block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2858 unsigned char pchPadding0[64];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2859 uint256 hash1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2860 unsigned char pchPadding1[64];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2861 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2862 tmp;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2863 memset(&tmp, 0, sizeof(tmp));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2864
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2865 tmp.block.nVersion = pblock->nVersion;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2866 tmp.block.hashPrevBlock = pblock->hashPrevBlock;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2867 tmp.block.hashMerkleRoot = pblock->hashMerkleRoot;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2868 tmp.block.nTime = pblock->nTime;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2869 tmp.block.nBits = pblock->nBits;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2870 tmp.block.nNonce = pblock->nNonce;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2871
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2872 FormatHashBlocks(&tmp.block, sizeof(tmp.block));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2873 FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2874
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2875 // Byte swap all the input buffer
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2876 for (int i = 0; i < sizeof(tmp)/4; i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2877 ((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2878
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2879 // Precalc the first half of the first hash, which stays constant
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2880 SHA256Transform(pmidstate, &tmp.block, pSHA256InitState);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2881
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2882 memcpy(pdata, &tmp.block, 128);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2883 memcpy(phash1, &tmp.hash1, 64);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2884 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2885
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2886
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2887 bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2888 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2889 uint256 hash = pblock->GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2890 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2891
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2892 if (hash > hashTarget)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2893 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2894
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2895 //// debug print
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2896 printf("BitcoinMiner:\n");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2897 printf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2898 pblock->print();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2899 printf("%s ", DateTimeStrFormat("%x %H:%M", GetTime()).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2900 printf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2901
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2902 // Found a solution
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2903 CRITICAL_BLOCK(cs_main)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2904 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2905 if (pblock->hashPrevBlock != hashBestChain)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2906 return error("BitcoinMiner : generated block is stale");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2907
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2908 // Remove key from key pool
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2909 reservekey.KeepKey();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2910
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2911 // Track how many getdata requests this block gets
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2912 CRITICAL_BLOCK(wallet.cs_mapRequestCount)
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2913 wallet.mapRequestCount[pblock->GetHash()] = 0;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2914
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2915 // Process this block the same as if we had received it from another node
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2916 if (!ProcessBlock(NULL, pblock))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2917 return error("BitcoinMiner : ProcessBlock, block not accepted");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2918 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2919
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2920 Sleep(2000);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2921 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2922 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2923
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2924 void static ThreadBitcoinMiner(void* parg);
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2925
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2926 void static BitcoinMiner(CWallet *pwallet)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2927 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2928 printf("BitcoinMiner started\n");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2929 SetThreadPriority(THREAD_PRIORITY_LOWEST);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2930
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2931 // Each thread has its own key and counter
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
2932 CReserveKey reservekey(pwallet);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2933 unsigned int nExtraNonce = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2934 int64 nPrevTime = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2935
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2936 while (fGenerateBitcoins)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2937 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2938 if (AffinityBugWorkaround(ThreadBitcoinMiner))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2939 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2940 if (fShutdown)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2941 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2942 while (vNodes.empty() || IsInitialBlockDownload())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2943 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2944 Sleep(1000);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2945 if (fShutdown)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2946 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2947 if (!fGenerateBitcoins)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2948 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2949 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2950
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2951
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2952 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2953 // Create new block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2954 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2955 unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2956 CBlockIndex* pindexPrev = pindexBest;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2957
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2958 auto_ptr<CBlock> pblock(CreateNewBlock(reservekey));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2959 if (!pblock.get())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2960 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2961 IncrementExtraNonce(pblock.get(), pindexPrev, nExtraNonce, nPrevTime);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2962
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2963 printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2964
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2965
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2966 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2967 // Prebuild hash buffers
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2968 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2969 char pmidstatebuf[32+16]; char* pmidstate = alignup<16>(pmidstatebuf);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2970 char pdatabuf[128+16]; char* pdata = alignup<16>(pdatabuf);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2971 char phash1buf[64+16]; char* phash1 = alignup<16>(phash1buf);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2972
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2973 FormatHashBuffers(pblock.get(), pmidstate, pdata, phash1);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2974
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2975 unsigned int& nBlockTime = *(unsigned int*)(pdata + 64 + 4);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2976 unsigned int& nBlockNonce = *(unsigned int*)(pdata + 64 + 12);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2977
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2978
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2979 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2980 // Search
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2981 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2982 int64 nStart = GetTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2983 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2984 uint256 hashbuf[2];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2985 uint256& hash = *alignup<16>(hashbuf);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2986 loop
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2987 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2988 unsigned int nHashesDone = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2989 unsigned int nNonceFound;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2990
576
8ec6d5e778e5 Manual merge of jaromil's source tree reorg commit.
Jeff Garzik <jeff@garzik.org>
parents: 575
diff changeset
2991 // Crypto++ SHA-256
8ec6d5e778e5 Manual merge of jaromil's source tree reorg commit.
Jeff Garzik <jeff@garzik.org>
parents: 575
diff changeset
2992 nNonceFound = ScanHash_CryptoPP(pmidstate, pdata + 64, phash1,
8ec6d5e778e5 Manual merge of jaromil's source tree reorg commit.
Jeff Garzik <jeff@garzik.org>
parents: 575
diff changeset
2993 (char*)&hash, nHashesDone);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2994
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2995 // Check if something found
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2996 if (nNonceFound != -1)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2997 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2998 for (int i = 0; i < sizeof(hash)/4; i++)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
2999 ((unsigned int*)&hash)[i] = ByteReverse(((unsigned int*)&hash)[i]);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3000
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3001 if (hash <= hashTarget)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3002 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3003 // Found a solution
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3004 pblock->nNonce = ByteReverse(nNonceFound);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3005 assert(hash == pblock->GetHash());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3006
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3007 SetThreadPriority(THREAD_PRIORITY_NORMAL);
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
3008 CheckWork(pblock.get(), *pwalletMain, reservekey);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3009 SetThreadPriority(THREAD_PRIORITY_LOWEST);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3010 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3011 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3012 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3013
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3014 // Meter hashes/sec
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3015 static int64 nHashCounter;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3016 if (nHPSTimerStart == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3017 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3018 nHPSTimerStart = GetTimeMillis();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3019 nHashCounter = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3020 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3021 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3022 nHashCounter += nHashesDone;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3023 if (GetTimeMillis() - nHPSTimerStart > 4000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3024 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3025 static CCriticalSection cs;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3026 CRITICAL_BLOCK(cs)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3027 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3028 if (GetTimeMillis() - nHPSTimerStart > 4000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3029 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3030 dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3031 nHPSTimerStart = GetTimeMillis();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3032 nHashCounter = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3033 string strStatus = strprintf(" %.0f khash/s", dHashesPerSec/1000.0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3034 UIThreadCall(boost::bind(CalledSetStatusBar, strStatus, 0));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3035 static int64 nLogTime;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3036 if (GetTime() - nLogTime > 30 * 60)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3037 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3038 nLogTime = GetTime();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3039 printf("%s ", DateTimeStrFormat("%x %H:%M", GetTime()).c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3040 printf("hashmeter %3d CPUs %6.0f khash/s\n", vnThreadsRunning[3], dHashesPerSec/1000.0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3041 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3042 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3043 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3044 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3045
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3046 // Check for stop or if block needs to be rebuilt
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3047 if (fShutdown)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3048 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3049 if (!fGenerateBitcoins)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3050 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3051 if (fLimitProcessors && vnThreadsRunning[3] > nLimitProcessors)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3052 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3053 if (vNodes.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3054 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3055 if (nBlockNonce >= 0xffff0000)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3056 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3057 if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3058 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3059 if (pindexPrev != pindexBest)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3060 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3061
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3062 // Update nTime every few seconds
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3063 pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3064 nBlockTime = ByteReverse(pblock->nTime);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3065 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3066 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3067 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3068
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
3069 void static ThreadBitcoinMiner(void* parg)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3070 {
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
3071 CWallet* pwallet = (CWallet*)parg;
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3072 try
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3073 {
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3074 vnThreadsRunning[3]++;
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
3075 BitcoinMiner(pwallet);
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3076 vnThreadsRunning[3]--;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3077 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3078 catch (std::exception& e) {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3079 vnThreadsRunning[3]--;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3080 PrintException(&e, "ThreadBitcoinMiner()");
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3081 } catch (...) {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3082 vnThreadsRunning[3]--;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3083 PrintException(NULL, "ThreadBitcoinMiner()");
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3084 }
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3085 UIThreadCall(boost::bind(CalledSetStatusBar, "", 0));
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3086 nHPSTimerStart = 0;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3087 if (vnThreadsRunning[3] == 0)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3088 dHashesPerSec = 0;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3089 printf("ThreadBitcoinMiner exiting, %d threads remaining\n", vnThreadsRunning[3]);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3090 }
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3091
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3092
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
3093 void GenerateBitcoins(bool fGenerate, CWallet* pwallet)
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3094 {
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3095 if (fGenerateBitcoins != fGenerate)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3096 {
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3097 fGenerateBitcoins = fGenerate;
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
3098 WriteSetting("fGenerateBitcoins", fGenerateBitcoins);
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3099 MainFrameRepaint();
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3100 }
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3101 if (fGenerateBitcoins)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3102 {
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3103 int nProcessors = boost::thread::hardware_concurrency();
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3104 printf("%d processors\n", nProcessors);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3105 if (nProcessors < 1)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3106 nProcessors = 1;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3107 if (fLimitProcessors && nProcessors > nLimitProcessors)
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3108 nProcessors = nLimitProcessors;
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3109 int nAddThreads = nProcessors - vnThreadsRunning[3];
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3110 printf("Starting %d BitcoinMiner threads\n", nAddThreads);
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3111 for (int i = 0; i < nAddThreads; i++)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3112 {
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
3113 if (!CreateThread(ThreadBitcoinMiner, pwallet))
689
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3114 printf("Error: CreateThread(ThreadBitcoinMiner) failed\n");
9df222fa1ef1 move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 688
diff changeset
3115 Sleep(10);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3116 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3117 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3118 }