1
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
2
// Copyright (c) 2009-2014 The Bitcoin developers
3
- // Distributed under the MIT/X11 software license, see the accompanying
3
+ // Distributed under the MIT software license, see the accompanying
4
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
6
6
#include " main.h"
@@ -33,9 +33,9 @@ using namespace std;
33
33
# error "Bitcoin cannot be compiled without assertions."
34
34
#endif
35
35
36
- //
37
- // Global state
38
- / /
36
+ /* *
37
+ * Global state
38
+ * /
39
39
40
40
CCriticalSection cs_main;
41
41
@@ -66,7 +66,7 @@ map<uint256, COrphanTx> mapOrphanTransactions;
66
66
map<uint256, set<uint256> > mapOrphanTransactionsByPrev;
67
67
void EraseOrphansFor (NodeId peer);
68
68
69
- // Constant stuff for coinbase transactions we create:
69
+ /* * Constant stuff for coinbase transactions we create: */
70
70
CScript COINBASE_FLAGS;
71
71
72
72
const string strMessageMagic = " Bitcoin Signed Message:\n " ;
@@ -97,44 +97,49 @@ namespace {
97
97
98
98
CBlockIndex *pindexBestInvalid;
99
99
100
- // The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS or better that are at least
101
- // as good as our current tip. Entries may be failed, though.
100
+ /* *
101
+ * The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS or better that are at least
102
+ * as good as our current tip. Entries may be failed, though.
103
+ */
102
104
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
103
- // Number of nodes with fSyncStarted.
105
+ /* * Number of nodes with fSyncStarted. */
104
106
int nSyncStarted = 0 ;
105
- // All pairs A->B, where A (or one if its ancestors) misses transactions, but B has transactions.
107
+ /* * All pairs A->B, where A (or one if its ancestors) misses transactions, but B has transactions. */
106
108
multimap<CBlockIndex*, CBlockIndex*> mapBlocksUnlinked;
107
109
108
110
CCriticalSection cs_LastBlockFile;
109
111
std::vector<CBlockFileInfo> vinfoBlockFile;
110
112
int nLastBlockFile = 0 ;
111
113
112
- // Every received block is assigned a unique and increasing identifier, so we
113
- // know which one to give priority in case of a fork.
114
+ /* *
115
+ * Every received block is assigned a unique and increasing identifier, so we
116
+ * know which one to give priority in case of a fork.
117
+ */
114
118
CCriticalSection cs_nBlockSequenceId;
115
- // Blocks loaded from disk are assigned id 0, so start the counter at 1.
119
+ /* * Blocks loaded from disk are assigned id 0, so start the counter at 1. */
116
120
uint32_t nBlockSequenceId = 1 ;
117
121
118
- // Sources of received blocks, to be able to send them reject messages or ban
119
- // them, if processing happens afterwards. Protected by cs_main.
122
+ /* *
123
+ * Sources of received blocks, to be able to send them reject messages or ban
124
+ * them, if processing happens afterwards. Protected by cs_main.
125
+ */
120
126
map<uint256, NodeId> mapBlockSource;
121
127
122
- // Blocks that are in flight, and that are in the queue to be downloaded.
123
- // Protected by cs_main.
128
+ /* * Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
124
129
struct QueuedBlock {
125
130
uint256 hash;
126
- CBlockIndex *pindex; // Optional.
127
- int64_t nTime; // Time of "getdata" request in microseconds.
131
+ CBlockIndex *pindex; // ! Optional.
132
+ int64_t nTime; // ! Time of "getdata" request in microseconds.
128
133
};
129
134
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
130
135
131
- // Number of preferrable block download peers.
136
+ /* * Number of preferable block download peers. */
132
137
int nPreferredDownload = 0 ;
133
138
134
- // Dirty block index entries.
139
+ /* * Dirty block index entries. */
135
140
set<CBlockIndex*> setDirtyBlockIndex;
136
141
137
- // Dirty block file entries.
142
+ /* * Dirty block file entries. */
138
143
set<int > setDirtyFileInfo;
139
144
} // anon namespace
140
145
@@ -148,19 +153,19 @@ namespace {
148
153
namespace {
149
154
150
155
struct CMainSignals {
151
- // Notifies listeners of updated transaction data (transaction, and optionally the block it is found in.
156
+ /* * Notifies listeners of updated transaction data (transaction, and optionally the block it is found in. */
152
157
boost::signals2::signal<void (const CTransaction &, const CBlock *)> SyncTransaction;
153
- // Notifies listeners of an erased transaction (currently disabled, requires transaction replacement).
158
+ /* * Notifies listeners of an erased transaction (currently disabled, requires transaction replacement). */
154
159
boost::signals2::signal<void (const uint256 &)> EraseTransaction;
155
- // Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible).
160
+ /* * Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible). */
156
161
boost::signals2::signal<void (const uint256 &)> UpdatedTransaction;
157
- // Notifies listeners of a new active block chain.
162
+ /* * Notifies listeners of a new active block chain. */
158
163
boost::signals2::signal<void (const CBlockLocator &)> SetBestChain;
159
- // Notifies listeners about an inventory item being seen on the network.
164
+ /* * Notifies listeners about an inventory item being seen on the network. */
160
165
boost::signals2::signal<void (const uint256 &)> Inventory;
161
- // Tells listeners to broadcast their data.
166
+ /* * Tells listeners to broadcast their data. */
162
167
boost::signals2::signal<void ()> Broadcast;
163
- // Notifies listeners of a block validation result
168
+ /* * Notifies listeners of a block validation result */
164
169
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
165
170
} g_signals;
166
171
@@ -213,32 +218,34 @@ struct CBlockReject {
213
218
uint256 hashBlock;
214
219
};
215
220
216
- // Maintain validation-specific state about nodes, protected by cs_main, instead
217
- // by CNode's own locks. This simplifies asynchronous operation, where
218
- // processing of incoming data is done after the ProcessMessage call returns,
219
- // and we're no longer holding the node's locks.
221
+ /* *
222
+ * Maintain validation-specific state about nodes, protected by cs_main, instead
223
+ * by CNode's own locks. This simplifies asynchronous operation, where
224
+ * processing of incoming data is done after the ProcessMessage call returns,
225
+ * and we're no longer holding the node's locks.
226
+ */
220
227
struct CNodeState {
221
- // Accumulated misbehaviour score for this peer.
228
+ // ! Accumulated misbehaviour score for this peer.
222
229
int nMisbehavior;
223
- // Whether this peer should be disconnected and banned (unless whitelisted).
230
+ // ! Whether this peer should be disconnected and banned (unless whitelisted).
224
231
bool fShouldBan ;
225
- // String name of this peer (debugging/logging purposes).
232
+ // ! String name of this peer (debugging/logging purposes).
226
233
std::string name;
227
- // List of asynchronously-determined block rejections to notify this peer about.
234
+ // ! List of asynchronously-determined block rejections to notify this peer about.
228
235
std::vector<CBlockReject> rejects;
229
- // The best known block we know this peer has announced.
236
+ // ! The best known block we know this peer has announced.
230
237
CBlockIndex *pindexBestKnownBlock;
231
- // The hash of the last unknown block this peer has announced.
238
+ // ! The hash of the last unknown block this peer has announced.
232
239
uint256 hashLastUnknownBlock;
233
- // The last full block we both have.
240
+ // ! The last full block we both have.
234
241
CBlockIndex *pindexLastCommonBlock;
235
- // Whether we've started headers synchronization with this peer.
242
+ // ! Whether we've started headers synchronization with this peer.
236
243
bool fSyncStarted ;
237
- // Since when we're stalling block download progress (in microseconds), or 0.
244
+ // ! Since when we're stalling block download progress (in microseconds), or 0.
238
245
int64_t nStallingSince;
239
246
list<QueuedBlock> vBlocksInFlight;
240
247
int nBlocksInFlight;
241
- // Whether we consider this a preferred download peer.
248
+ // ! Whether we consider this a preferred download peer.
242
249
bool fPreferredDownload ;
243
250
244
251
CNodeState () {
@@ -254,7 +261,7 @@ struct CNodeState {
254
261
}
255
262
};
256
263
257
- // Map maintaining per-node state. Requires cs_main.
264
+ /* * Map maintaining per-node state. Requires cs_main. */
258
265
map<NodeId, CNodeState> mapNodeState;
259
266
260
267
// Requires cs_main.
@@ -708,15 +715,15 @@ bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
708
715
return true ;
709
716
}
710
717
711
- //
712
- // Check transaction inputs to mitigate two
713
- // potential denial-of-service attacks:
714
- //
715
- // 1. scriptSigs with extra data stuffed into them,
716
- // not consumed by scriptPubKey (or P2SH script)
717
- // 2. P2SH scripts with a crazy number of expensive
718
- // CHECKSIG/CHECKMULTISIG operations
719
- / /
718
+ /* *
719
+ * Check transaction inputs to mitigate two
720
+ * potential denial-of-service attacks:
721
+ *
722
+ * 1. scriptSigs with extra data stuffed into them,
723
+ * not consumed by scriptPubKey (or P2SH script)
724
+ * 2. P2SH scripts with a crazy number of expensive
725
+ * CHECKSIG/CHECKMULTISIG operations
726
+ * /
720
727
bool AreInputsStandard (const CTransaction& tx, const CCoinsViewCache& mapInputs)
721
728
{
722
729
if (tx.IsCoinBase ())
@@ -1054,7 +1061,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
1054
1061
return true ;
1055
1062
}
1056
1063
1057
- // Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock
1064
+ /* * Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock */
1058
1065
bool GetTransaction (const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow )
1059
1066
{
1060
1067
CBlockIndex *pindexSlow = NULL ;
@@ -1818,7 +1825,7 @@ void FlushStateToDisk() {
1818
1825
FlushStateToDisk (state, FLUSH_STATE_ALWAYS);
1819
1826
}
1820
1827
1821
- // Update chainActive and related internal data structures.
1828
+ /* * Update chainActive and related internal data structures. */
1822
1829
void static UpdateTip (CBlockIndex *pindexNew) {
1823
1830
chainActive.SetTip (pindexNew);
1824
1831
@@ -1857,7 +1864,7 @@ void static UpdateTip(CBlockIndex *pindexNew) {
1857
1864
}
1858
1865
}
1859
1866
1860
- // Disconnect chainActive's tip.
1867
+ /* * Disconnect chainActive's tip. */
1861
1868
bool static DisconnectTip (CValidationState &state) {
1862
1869
CBlockIndex *pindexDelete = chainActive.Tip ();
1863
1870
assert (pindexDelete);
@@ -1904,8 +1911,10 @@ static int64_t nTimeFlush = 0;
1904
1911
static int64_t nTimeChainState = 0 ;
1905
1912
static int64_t nTimePostConnect = 0 ;
1906
1913
1907
- // Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
1908
- // corresponding to pindexNew, to bypass loading it again from disk.
1914
+ /* *
1915
+ * Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
1916
+ * corresponding to pindexNew, to bypass loading it again from disk.
1917
+ */
1909
1918
bool static ConnectTip (CValidationState &state, CBlockIndex *pindexNew, CBlock *pblock) {
1910
1919
assert (pindexNew->pprev == chainActive.Tip ());
1911
1920
mempool.check (pcoinsTip);
@@ -1965,8 +1974,10 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *
1965
1974
return true ;
1966
1975
}
1967
1976
1968
- // Return the tip of the chain with the most work in it, that isn't
1969
- // known to be invalid (it's however far from certain to be valid).
1977
+ /* *
1978
+ * Return the tip of the chain with the most work in it, that isn't
1979
+ * known to be invalid (it's however far from certain to be valid).
1980
+ */
1970
1981
static CBlockIndex* FindMostWorkChain () {
1971
1982
do {
1972
1983
CBlockIndex *pindexNew = NULL ;
@@ -2007,7 +2018,7 @@ static CBlockIndex* FindMostWorkChain() {
2007
2018
} while (true );
2008
2019
}
2009
2020
2010
- // Delete all entries in setBlockIndexCandidates that are worse than the current tip.
2021
+ /* * Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
2011
2022
static void PruneBlockIndexCandidates () {
2012
2023
// Note that we can't delete the current block itself, as we may need to return to it later in case a
2013
2024
// reorganization to a better block fails.
@@ -2019,8 +2030,10 @@ static void PruneBlockIndexCandidates() {
2019
2030
assert (!setBlockIndexCandidates.empty ());
2020
2031
}
2021
2032
2022
- // Try to make some progress towards making pindexMostWork the active block.
2023
- // pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
2033
+ /* *
2034
+ * Try to make some progress towards making pindexMostWork the active block.
2035
+ * pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
2036
+ */
2024
2037
static bool ActivateBestChainStep (CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) {
2025
2038
AssertLockHeld (cs_main);
2026
2039
bool fInvalidFound = false ;
@@ -2085,9 +2098,11 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
2085
2098
return true ;
2086
2099
}
2087
2100
2088
- // Make the best chain active, in multiple steps. The result is either failure
2089
- // or an activated best chain. pblock is either NULL or a pointer to a block
2090
- // that is already loaded (to avoid loading it again from disk).
2101
+ /* *
2102
+ * Make the best chain active, in multiple steps. The result is either failure
2103
+ * or an activated best chain. pblock is either NULL or a pointer to a block
2104
+ * that is already loaded (to avoid loading it again from disk).
2105
+ */
2091
2106
bool ActivateBestChain (CValidationState &state, CBlock *pblock) {
2092
2107
CBlockIndex *pindexNewTip = NULL ;
2093
2108
CBlockIndex *pindexMostWork = NULL ;
@@ -2236,7 +2251,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block)
2236
2251
return pindexNew;
2237
2252
}
2238
2253
2239
- // Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).
2254
+ /* * Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
2240
2255
bool ReceivedBlockTransactions (const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos)
2241
2256
{
2242
2257
pindexNew->nTx = block.vtx .size ();
@@ -2745,7 +2760,7 @@ uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::ve
2745
2760
} else {
2746
2761
// calculate left hash
2747
2762
uint256 left = CalcHash (height-1 , pos*2 , vTxid), right;
2748
- // calculate right hash if not beyong the end of the array - copy left hash otherwise1
2763
+ // calculate right hash if not beyond the end of the array - copy left hash otherwise1
2749
2764
if (pos*2 +1 < CalcTreeWidth (height-1 ))
2750
2765
right = CalcHash (height-1 , pos*2 +1 , vTxid);
2751
2766
else
0 commit comments