diff options
Diffstat (limited to 'src/main.h')
| -rw-r--r-- | src/main.h | 1637 |
1 files changed, 1637 insertions, 0 deletions
diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..c458412 --- /dev/null +++ b/src/main.h | |||
| @@ -0,0 +1,1637 @@ | |||
| 1 | // Copyright (c) 2009 Satoshi Nakamoto | ||
| 2 | // Distributed under the MIT/X11 software license, see the accompanying | ||
| 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. | ||
| 4 | |||
| 5 | class COutPoint; | ||
| 6 | class CInPoint; | ||
| 7 | class CDiskTxPos; | ||
| 8 | class CCoinBase; | ||
| 9 | class CTxIn; | ||
| 10 | class CTxOut; | ||
| 11 | class CTransaction; | ||
| 12 | class CBlock; | ||
| 13 | class CBlockIndex; | ||
| 14 | class CWalletTx; | ||
| 15 | class CKeyItem; | ||
| 16 | |||
| 17 | // Ceiling on transactions held waiting for a parent that has not arrived. | ||
| 18 | // They cost a peer nothing to send and are never validated, only stored. | ||
| 19 | static const unsigned int MAX_ORPHAN_TRANSACTIONS = 100; | ||
| 20 | // Blocks held waiting for a parent that has not arrived. Orphan transactions | ||
| 21 | // have had a ceiling since 0.1.0 and orphan blocks never did: one whose parent | ||
| 22 | // never turns up stays in memory for the life of the process, and a peer can | ||
| 23 | // send an unlimited number of them by inventing the previous-block hash. They | ||
| 24 | // are not free either -- each holds a whole block. A production node was | ||
| 25 | // seeing 1287 of them against 4117 blocks processed. | ||
| 26 | static const unsigned int MAX_ORPHAN_BLOCKS = 200; | ||
| 27 | // Signature operations allowed in one block. Size alone does not bound what a | ||
| 28 | // block costs to validate: every OP_CHECKSIG is an elliptic-curve verification, | ||
| 29 | // so a block within the size limit could still hold millions of them and take | ||
| 30 | // the network minutes of CPU to reject. Same value Bitcoin settled on. | ||
| 31 | static const unsigned int MAX_BLOCK_SIGOPS = 20000; | ||
| 32 | // Consensus block-size ceiling. MAX_SIZE is the serializer's generic safety | ||
| 33 | // limit (32 MB), not a block rule. Bitcoin 0.1.0 predated the later 1 MB block | ||
| 34 | // cap, so this tree inherited no real block-size consensus limit. | ||
| 35 | static const unsigned int MAX_BLOCK_SIZE = 1000000; | ||
| 36 | static const int64 COIN = 100000000; | ||
| 37 | // Total Bitflash emission: identical to Bitcoin (21 million). | ||
| 38 | // MAX_MONEY guards against the value overflow bug (CVE-2010-5139), which in | ||
| 39 | // 2010 allowed creating 184 billion coins in a single transaction. | ||
| 40 | static const int64 MAX_MONEY = 21000000 * COIN; | ||
| 41 | inline bool MoneyRange(int64 nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } | ||
| 42 | static const int64 CENT = 1000000; | ||
| 43 | static const int COINBASE_MATURITY = 100; | ||
| 44 | |||
| 45 | // Bitflash difficulty floor. The PoW is RandomX (memory-hard), ~1000x slower | ||
| 46 | // per hash than SHA-256d, so the target is much easier than Bitcoin's to let a | ||
| 47 | // CPU network start with little hashrate. Difficulty rises on its own (retarget | ||
| 48 | // toward 10-min blocks) as miners join. This does NOT affect emission | ||
| 49 | // (21 million, halving every 210,000 blocks). Tunable. | ||
| 50 | static const CBigNum bnProofOfWorkLimit(~uint256(0) >> 12); | ||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | extern CCriticalSection cs_main; | ||
| 58 | extern map<uint256, CBlockIndex*> mapBlockIndex; | ||
| 59 | extern uint256 hashGenesisBlock; | ||
| 60 | extern uint256 hashGenesisMerkleRoot; | ||
| 61 | extern unsigned int GENESIS_NONCE; | ||
| 62 | extern CBlockIndex* pindexGenesisBlock; | ||
| 63 | extern int nBestHeight; | ||
| 64 | extern uint256 hashBestChain; | ||
| 65 | extern int nCheckBlocksOnLoad; | ||
| 66 | extern CBlockIndex* pindexBadChainFork; | ||
| 67 | extern CBlockIndex* pindexBest; | ||
| 68 | extern unsigned int nTransactionsUpdated; | ||
| 69 | extern string strSetDataDir; | ||
| 70 | extern int nDropMessagesTest; | ||
| 71 | // Mining mode -- remembered in wallet.dat, but the command line always wins. | ||
| 72 | // See LoadWallet() in db.cpp for why the precedence has to be explicit. | ||
| 73 | // MINE_RELAY (0): default. Node runs/syncs, never mines, no pool server. | ||
| 74 | // MINE_SOLO (1): mine to own wallet, no pool server | ||
| 75 | // MINE_OPERATOR (2): run pool server, mine to own wallet, distribute to miners | ||
| 76 | // MINE_PARTICIPANT (3): connect to external pool, submit shares, receive payouts | ||
| 77 | #define MINE_RELAY 0 | ||
| 78 | #define MINE_SOLO 1 | ||
| 79 | #define MINE_OPERATOR 2 | ||
| 80 | #define MINE_PARTICIPANT 3 | ||
| 81 | extern int nMineMode; | ||
| 82 | // Set by the startup parser when a flag chose the mode. LoadWallet leaves the | ||
| 83 | // stored mode alone when this is true, so a flag is never silently overruled by | ||
| 84 | // something a previous session wrote. | ||
| 85 | extern bool fMineModeFromCommandLine; | ||
| 86 | extern string strParticipantPool; // participant: pool .btf address | ||
| 87 | extern string strPoolName; // operator: announced pool name | ||
| 88 | extern string strPoolDashboardUrl; // operator: optional dashboard URL | ||
| 89 | extern double dPoolFeePercent; // operator: announced fee percent | ||
| 90 | extern bool fSoloMineTest; // /solomine: mine without requiring a peer (local test) | ||
| 91 | // Threads to hash with; 0 = decide from the hardware. Set by /genproclimit. | ||
| 92 | extern int nMinerThreads; | ||
| 93 | // Resolves the automatic case into a real count. | ||
| 94 | int MinerThreadCount(); | ||
| 95 | extern bool gPoolServerRunning; // GUI/startup state | ||
| 96 | extern volatile bool gPoolRunning; // runtime pool server loop flag (rpc.cpp) | ||
| 97 | |||
| 98 | struct PendingPayoutView | ||
| 99 | { | ||
| 100 | int matureAtHeight; | ||
| 101 | int recipients; | ||
| 102 | int64 totalAmount; | ||
| 103 | }; | ||
| 104 | |||
| 105 | struct PoolWorkerStatView | ||
| 106 | { | ||
| 107 | std::string address; | ||
| 108 | std::string worker; | ||
| 109 | uint64 totalShares; | ||
| 110 | uint64 roundShares; | ||
| 111 | int64 lastSeen; | ||
| 112 | double hashRate; // estimated H/s, derived from share difficulty + submit rate | ||
| 113 | }; | ||
| 114 | |||
| 115 | // Settings | ||
| 116 | extern int fGenerateBitcoins; | ||
| 117 | extern int64 nTransactionFee; | ||
| 118 | extern CAddress addrIncoming; | ||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | string GetAppDir(); | ||
| 127 | FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode="rb"); | ||
| 128 | FILE* AppendBlockFile(unsigned int& nFileRet); | ||
| 129 | bool AddKey(const CKey& key); | ||
| 130 | vector<unsigned char> GenerateNewKey(); | ||
| 131 | |||
| 132 | // --- Wallet encryption ---------------------------------------------------- | ||
| 133 | // | ||
| 134 | // Plain wallets keep private keys in mapKeys. Encrypted wallets keep public | ||
| 135 | // keys visible so balances can still be recognized while locked, and decrypt | ||
| 136 | // private keys only after an unlock passphrase has supplied the master key. | ||
| 137 | typedef vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial; | ||
| 138 | extern map<vector<unsigned char>, vector<unsigned char> > mapCryptedKeys; | ||
| 139 | extern map<unsigned int, CWalletMasterKey> mapMasterKeys; | ||
| 140 | extern unsigned int nWalletMasterKeyMaxID; | ||
| 141 | extern CKeyingMaterial vWalletMasterKey; | ||
| 142 | extern vector<unsigned char> vchCryptedHDMaster; | ||
| 143 | extern vector<unsigned char> vchCryptedHDChainCode; | ||
| 144 | extern bool fWalletEncrypted; | ||
| 145 | extern bool fWalletLocked; | ||
| 146 | bool IsWalletEncrypted(); | ||
| 147 | bool IsWalletLocked(); | ||
| 148 | void LockWallet(); | ||
| 149 | bool UnlockWallet(const string& strPassphrase, string& strErrorRet); | ||
| 150 | bool DeriveWalletPassphraseKey(const string& strPassphrase, | ||
| 151 | const vector<unsigned char>& vchSalt, | ||
| 152 | unsigned int nDeriveIterations, | ||
| 153 | CKeyingMaterial& vchKeyRet, | ||
| 154 | vector<unsigned char>& vchIVRet); | ||
| 155 | bool EncryptSecret(const CKeyingMaterial& vchKey, | ||
| 156 | const vector<unsigned char>& vchPlaintext, | ||
| 157 | const vector<unsigned char>& vchIV, | ||
| 158 | vector<unsigned char>& vchCiphertextRet); | ||
| 159 | bool DecryptSecret(const CKeyingMaterial& vchKey, | ||
| 160 | const vector<unsigned char>& vchCiphertext, | ||
| 161 | const vector<unsigned char>& vchIV, | ||
| 162 | vector<unsigned char>& vchPlaintextRet); | ||
| 163 | vector<unsigned char> WalletKeyIV(const vector<unsigned char>& vchPubKey); | ||
| 164 | vector<unsigned char> WalletSecretIV(const string& strLabel); | ||
| 165 | bool AddCryptedKey(const vector<unsigned char>& vchPubKey, | ||
| 166 | const vector<unsigned char>& vchCryptedSecret); | ||
| 167 | bool WalletCanSpendKey(const vector<unsigned char>& vchPubKey); | ||
| 168 | bool GetWalletPrivKey(const vector<unsigned char>& vchPubKey, | ||
| 169 | CPrivKey& vchPrivKeyRet, | ||
| 170 | string& strErrorRet); | ||
| 171 | |||
| 172 | // --- Key pool ------------------------------------------------------------- | ||
| 173 | // | ||
| 174 | // A backup of this wallet only ever contained the keys that existed the moment | ||
| 175 | // it was taken. Mine a block or take a new address afterwards and the coin | ||
| 176 | // lands on a key the backup has never heard of -- it is on the chain, and the | ||
| 177 | // file you carefully put away cannot spend it. The LEIA-ME shipped with a | ||
| 178 | // warning about this, which is an admission, not a fix. | ||
| 179 | // | ||
| 180 | // Keys are now generated a hundred at a time and handed out one by one, so a | ||
| 181 | // backup covers the next hundred addresses this node will use. It is the same | ||
| 182 | // answer Bitcoin arrived at, and it is not deterministic derivation (#47) -- | ||
| 183 | // it just buys back the distance between a backup and the next mistake. | ||
| 184 | static const int KEYPOOL_SIZE = 100; | ||
| 185 | extern CCriticalSection cs_keyPool; | ||
| 186 | extern map<int64, vector<unsigned char> > mapKeyPool; | ||
| 187 | |||
| 188 | // --- Deterministic wallet (BIP32) ----------------------------------------- | ||
| 189 | // | ||
| 190 | // When a seed is present today, the key pool is derived from it by the legacy | ||
| 191 | // Bitflash path -- m/index', hardened, index increasing -- instead of being | ||
| 192 | // made of random keys. That is what lets a recovery phrase bring a wallet back: | ||
| 193 | // the same twelve words reproduce the same keys in the same order. | ||
| 194 | // | ||
| 195 | // BIP44 uses a different path family and separate receive/change counters. | ||
| 196 | // The schema fields below let new wallets opt into that without making old | ||
| 197 | // m/index' coins disappear. | ||
| 198 | // | ||
| 199 | // A wallet without a seed keeps working exactly as before. Existing seeded | ||
| 200 | // wallets keep their recorded schema. A new phrase uses BIP44, but it is still | ||
| 201 | // created only when the user asks for a phrase and confirms they have written | ||
| 202 | // it down, because a phrase generated silently is a backup nobody has. | ||
| 203 | // | ||
| 204 | // Empty until a seed exists. | ||
| 205 | extern vector<unsigned char> vchHDMaster; // 32-byte master private key (IL) | ||
| 206 | extern vector<unsigned char> vchHDChainCode; // 32 bytes (IR) | ||
| 207 | extern unsigned int nHDNext; // next child index to derive | ||
| 208 | static const int HD_SCHEMA_NONE = 0; | ||
| 209 | static const int HD_SCHEMA_LEGACY = 1; // m/index' | ||
| 210 | static const int HD_SCHEMA_BIP44 = 2; // m/44'/coin_type'/account'/change/index | ||
| 211 | static const unsigned int HD_BIP44_PURPOSE = 44; | ||
| 212 | static const unsigned int HD_BIP44_ACCOUNT = 0; | ||
| 213 | static const unsigned int HD_BIP44_CHAIN_RECEIVE = 0; | ||
| 214 | static const unsigned int HD_BIP44_CHAIN_CHANGE = 1; | ||
| 215 | // Provisional until Bitflash has an official SLIP-0044 assignment. | ||
| 216 | // Proposed registry row: 4346950 | BITFLASH | Bitflash. | ||
| 217 | static const unsigned int HD_BIP44_COIN_TYPE_BITFLASH_PROVISIONAL = 4346950; | ||
| 218 | extern int nHDKeySchema; | ||
| 219 | extern unsigned int nHDReceiveNext; // BIP44 external chain | ||
| 220 | extern unsigned int nHDChangeNext; // BIP44 internal chain | ||
| 221 | extern unsigned int nHDCoinType; // BIP44 coin_type' | ||
| 222 | inline bool HaveHDSeed() { return vchHDMaster.size() == 32 && vchHDChainCode.size() == 32; } | ||
| 223 | string HDKeySchemaName(int nSchema); | ||
| 224 | std::vector<unsigned int> HDLegacyPath(unsigned int nIndex); | ||
| 225 | std::vector<unsigned int> HDBIP44Path(unsigned int nCoinType, | ||
| 226 | unsigned int nAccount, | ||
| 227 | unsigned int nChain, | ||
| 228 | unsigned int nIndex); | ||
| 229 | |||
| 230 | // Install a seed derived from a mnemonic, replacing any existing one. The | ||
| 231 | // default receiving key is derived immediately, so the next visible address is | ||
| 232 | // covered by the phrase. Writes to wallet.dat. Returns false and leaves the | ||
| 233 | // wallet untouched if the phrase is not valid. | ||
| 234 | bool SetHDSeedFromMnemonic(const string& strMnemonic, string& strErrorRet); | ||
| 235 | |||
| 236 | // Derive the receiving child at nIndex for the wallet's active schema and | ||
| 237 | // return it as a key. Legacy seeded wallets use m/index'. BIP44 wallets use | ||
| 238 | // m/44'/coin_type'/0'/0/index. Used by the key pool and restore, which needs | ||
| 239 | // to run ahead of the pool. | ||
| 240 | bool DeriveHDKey(unsigned int nIndex, CKey& keyRet, string& strErrorRet); | ||
| 241 | bool DeriveHDChangeKey(unsigned int nIndex, CKey& keyRet, string& strErrorRet); | ||
| 242 | |||
| 243 | // Fill the pool back up to KEYPOOL_SIZE. Every key it creates is written to | ||
| 244 | // wallet.dat before it is offered to anybody. | ||
| 245 | void TopUpKeyPool(); | ||
| 246 | |||
| 247 | // Hand out the oldest unused key. Falls back to generating one on the spot if | ||
| 248 | // the pool cannot be filled, which is exactly what this code did before the | ||
| 249 | // pool existed -- a wallet that cannot pre-generate should still be able to | ||
| 250 | // mine. | ||
| 251 | vector<unsigned char> GetKeyFromPool(); | ||
| 252 | bool AddToWallet(const CWalletTx& wtxIn); | ||
| 253 | void ReacceptWalletTransactions(); | ||
| 254 | // Walk the chain from pindexStart forward, adding every transaction that pays | ||
| 255 | // a key this wallet holds. Returns how many were added or updated. Needed by | ||
| 256 | // anything that puts a key into the wallet after the fact -- an import, a | ||
| 257 | // restored backup, and later a recovery phrase. | ||
| 258 | bool CanScanWalletTransactions(string& strErrorRet); | ||
| 259 | int ScanForWalletTransactions(CBlockIndex* pindexStart); | ||
| 260 | // Returns how many wallet transactions the chain corrected. | ||
| 261 | int RescanSpentFlags(); | ||
| 262 | void RelayWalletTransactions(); | ||
| 263 | bool LoadBlockIndex(bool fAllowNew=true); | ||
| 264 | void PrintBlockTree(); | ||
| 265 | void AddOrphanTx(const CDataStream& vMsg); | ||
| 266 | void EraseOrphanTx(uint256 hash); | ||
| 267 | void LimitOrphanTx(unsigned int nMaxOrphans); | ||
| 268 | // Same ceiling idea for orphan blocks, which never had one. | ||
| 269 | void LimitOrphanBlocks(unsigned int nMaxOrphans); | ||
| 270 | // nThreadId is 1..N, only for logging: it makes each miner's lines | ||
| 271 | // distinguishable, which the dedup filter would otherwise collapse into one. | ||
| 272 | bool BitcoinMiner(int nThreadId = 1); | ||
| 273 | void ThreadRPCServer(void* parg); // rpc.cpp -- .btf pool server | ||
| 274 | void GetParticipantMiningStats(uint64& sharesSent, uint64& sharesAccepted, double& hashRate); | ||
| 275 | void SetParticipantMiningStatus(const std::string& status); | ||
| 276 | std::string GetParticipantMiningStatus(); | ||
| 277 | void GetPoolOperatorStats(int& authorizedMiners, int& blocksFound, uint64& roundShares, double& totalHashRate); | ||
| 278 | void GetPoolWorkerStats(std::vector<PoolWorkerStatView>& out); | ||
| 279 | void GetPendingPayouts(std::vector<PendingPayoutView>& out); | ||
| 280 | bool ProcessMessages(CNode* pfrom); | ||
| 281 | bool ProcessBlock(CNode* pfrom, CBlock* pblock); | ||
| 282 | unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast); | ||
| 283 | extern CCriticalSection cs_mapTransactions; | ||
| 284 | bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv); | ||
| 285 | bool SendMessages(CNode* pto); | ||
| 286 | int64 GetBalance(); | ||
| 287 | struct WalletRecoveryAudit | ||
| 288 | { | ||
| 289 | bool fHaveSeed; | ||
| 290 | bool fDeriveComplete; | ||
| 291 | int nSchema; | ||
| 292 | unsigned int nDerivedKnown; | ||
| 293 | unsigned int nReceiveNext; | ||
| 294 | unsigned int nChangeNext; | ||
| 295 | unsigned int nCoinType; | ||
| 296 | int nRecoverableTx; | ||
| 297 | int nLegacyTx; | ||
| 298 | int nRecoverableImmatureTx; | ||
| 299 | int nLegacyImmatureTx; | ||
| 300 | int64 nRecoverableCredit; | ||
| 301 | int64 nLegacyCredit; | ||
| 302 | int64 nRecoverableImmatureCredit; | ||
| 303 | int64 nLegacyImmatureCredit; | ||
| 304 | string strDeriveError; | ||
| 305 | |||
| 306 | WalletRecoveryAudit() | ||
| 307 | { | ||
| 308 | fHaveSeed = false; | ||
| 309 | fDeriveComplete = true; | ||
| 310 | nSchema = HD_SCHEMA_NONE; | ||
| 311 | nDerivedKnown = 0; | ||
| 312 | nReceiveNext = 0; | ||
| 313 | nChangeNext = 0; | ||
| 314 | nCoinType = HD_BIP44_COIN_TYPE_BITFLASH_PROVISIONAL; | ||
| 315 | nRecoverableTx = 0; | ||
| 316 | nLegacyTx = 0; | ||
| 317 | nRecoverableImmatureTx = 0; | ||
| 318 | nLegacyImmatureTx = 0; | ||
| 319 | nRecoverableCredit = 0; | ||
| 320 | nLegacyCredit = 0; | ||
| 321 | nRecoverableImmatureCredit = 0; | ||
| 322 | nLegacyImmatureCredit = 0; | ||
| 323 | } | ||
| 324 | }; | ||
| 325 | WalletRecoveryAudit GetWalletRecoveryAudit(); | ||
| 326 | bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& txNew, int64& nFeeRequiredRet); | ||
| 327 | bool CommitTransactionSpent(const CWalletTx& wtxNew); | ||
| 328 | bool SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew); | ||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | |||
| 337 | |||
| 338 | |||
| 339 | |||
| 340 | class CDiskTxPos | ||
| 341 | { | ||
| 342 | public: | ||
| 343 | unsigned int nFile; | ||
| 344 | unsigned int nBlockPos; | ||
| 345 | unsigned int nTxPos; | ||
| 346 | |||
| 347 | CDiskTxPos() | ||
| 348 | { | ||
| 349 | SetNull(); | ||
| 350 | } | ||
| 351 | |||
| 352 | CDiskTxPos(unsigned int nFileIn, unsigned int nBlockPosIn, unsigned int nTxPosIn) | ||
| 353 | { | ||
| 354 | nFile = nFileIn; | ||
| 355 | nBlockPos = nBlockPosIn; | ||
| 356 | nTxPos = nTxPosIn; | ||
| 357 | } | ||
| 358 | |||
| 359 | IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); ) | ||
| 360 | void SetNull() { nFile = -1; nBlockPos = 0; nTxPos = 0; } | ||
| 361 | bool IsNull() const { return (nFile == -1); } | ||
| 362 | |||
| 363 | friend bool operator==(const CDiskTxPos& a, const CDiskTxPos& b) | ||
| 364 | { | ||
| 365 | return (a.nFile == b.nFile && | ||
| 366 | a.nBlockPos == b.nBlockPos && | ||
| 367 | a.nTxPos == b.nTxPos); | ||
| 368 | } | ||
| 369 | |||
| 370 | friend bool operator!=(const CDiskTxPos& a, const CDiskTxPos& b) | ||
| 371 | { | ||
| 372 | return !(a == b); | ||
| 373 | } | ||
| 374 | |||
| 375 | string ToString() const | ||
| 376 | { | ||
| 377 | if (IsNull()) | ||
| 378 | return strprintf("null"); | ||
| 379 | else | ||
| 380 | return strprintf("(nFile=%d, nBlockPos=%d, nTxPos=%d)", nFile, nBlockPos, nTxPos); | ||
| 381 | } | ||
| 382 | |||
| 383 | void print() const | ||
| 384 | { | ||
| 385 | printf("%s", ToString().c_str()); | ||
| 386 | } | ||
| 387 | }; | ||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | class CInPoint | ||
| 393 | { | ||
| 394 | public: | ||
| 395 | CTransaction* ptx; | ||
| 396 | unsigned int n; | ||
| 397 | |||
| 398 | CInPoint() { SetNull(); } | ||
| 399 | CInPoint(CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; } | ||
| 400 | void SetNull() { ptx = NULL; n = -1; } | ||
| 401 | bool IsNull() const { return (ptx == NULL && n == -1); } | ||
| 402 | }; | ||
| 403 | |||
| 404 | |||
| 405 | |||
| 406 | |||
| 407 | class COutPoint | ||
| 408 | { | ||
| 409 | public: | ||
| 410 | uint256 hash; | ||
| 411 | unsigned int n; | ||
| 412 | |||
| 413 | COutPoint() { SetNull(); } | ||
| 414 | COutPoint(uint256 hashIn, unsigned int nIn) { hash = hashIn; n = nIn; } | ||
| 415 | IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); ) | ||
| 416 | void SetNull() { hash = 0; n = -1; } | ||
| 417 | bool IsNull() const { return (hash == 0 && n == -1); } | ||
| 418 | |||
| 419 | friend bool operator<(const COutPoint& a, const COutPoint& b) | ||
| 420 | { | ||
| 421 | return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n)); | ||
| 422 | } | ||
| 423 | |||
| 424 | friend bool operator==(const COutPoint& a, const COutPoint& b) | ||
| 425 | { | ||
| 426 | return (a.hash == b.hash && a.n == b.n); | ||
| 427 | } | ||
| 428 | |||
| 429 | friend bool operator!=(const COutPoint& a, const COutPoint& b) | ||
| 430 | { | ||
| 431 | return !(a == b); | ||
| 432 | } | ||
| 433 | |||
| 434 | string ToString() const | ||
| 435 | { | ||
| 436 | return strprintf("COutPoint(%s, %d)", hash.ToString().substr(0,6).c_str(), n); | ||
| 437 | } | ||
| 438 | |||
| 439 | void print() const | ||
| 440 | { | ||
| 441 | printf("%s\n", ToString().c_str()); | ||
| 442 | } | ||
| 443 | }; | ||
| 444 | |||
| 445 | |||
| 446 | |||
| 447 | |||
| 448 | // | ||
| 449 | // An input of a transaction. It contains the location of the previous | ||
| 450 | // transaction's output that it claims and a signature that matches the | ||
| 451 | // output's public key. | ||
| 452 | // | ||
| 453 | class CTxIn | ||
| 454 | { | ||
| 455 | public: | ||
| 456 | COutPoint prevout; | ||
| 457 | CScript scriptSig; | ||
| 458 | unsigned int nSequence; | ||
| 459 | |||
| 460 | CTxIn() | ||
| 461 | { | ||
| 462 | nSequence = UINT_MAX; | ||
| 463 | } | ||
| 464 | |||
| 465 | explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=UINT_MAX) | ||
| 466 | { | ||
| 467 | prevout = prevoutIn; | ||
| 468 | scriptSig = scriptSigIn; | ||
| 469 | nSequence = nSequenceIn; | ||
| 470 | } | ||
| 471 | |||
| 472 | CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=UINT_MAX) | ||
| 473 | { | ||
| 474 | prevout = COutPoint(hashPrevTx, nOut); | ||
| 475 | scriptSig = scriptSigIn; | ||
| 476 | nSequence = nSequenceIn; | ||
| 477 | } | ||
| 478 | |||
| 479 | IMPLEMENT_SERIALIZE | ||
| 480 | ( | ||
| 481 | READWRITE(prevout); | ||
| 482 | READWRITE(scriptSig); | ||
| 483 | READWRITE(nSequence); | ||
| 484 | ) | ||
| 485 | |||
| 486 | bool IsFinal() const | ||
| 487 | { | ||
| 488 | return (nSequence == UINT_MAX); | ||
| 489 | } | ||
| 490 | |||
| 491 | friend bool operator==(const CTxIn& a, const CTxIn& b) | ||
| 492 | { | ||
| 493 | return (a.prevout == b.prevout && | ||
| 494 | a.scriptSig == b.scriptSig && | ||
| 495 | a.nSequence == b.nSequence); | ||
| 496 | } | ||
| 497 | |||
| 498 | friend bool operator!=(const CTxIn& a, const CTxIn& b) | ||
| 499 | { | ||
| 500 | return !(a == b); | ||
| 501 | } | ||
| 502 | |||
| 503 | string ToString() const | ||
| 504 | { | ||
| 505 | string str; | ||
| 506 | str += strprintf("CTxIn("); | ||
| 507 | str += prevout.ToString(); | ||
| 508 | if (prevout.IsNull()) | ||
| 509 | str += strprintf(", coinbase %s", HexStr(scriptSig.begin(), scriptSig.end(), false).c_str()); | ||
| 510 | else | ||
| 511 | str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24).c_str()); | ||
| 512 | if (nSequence != UINT_MAX) | ||
| 513 | str += strprintf(", nSequence=%u", nSequence); | ||
| 514 | str += ")"; | ||
| 515 | return str; | ||
| 516 | } | ||
| 517 | |||
| 518 | void print() const | ||
| 519 | { | ||
| 520 | printf("%s\n", ToString().c_str()); | ||
| 521 | } | ||
| 522 | |||
| 523 | bool IsMine() const; | ||
| 524 | int64 GetDebit() const; | ||
| 525 | }; | ||
| 526 | |||
| 527 | |||
| 528 | |||
| 529 | |||
| 530 | // | ||
| 531 | // An output of a transaction. It contains the public key that the next input | ||
| 532 | // must be able to sign with to claim it. | ||
| 533 | // | ||
| 534 | class CTxOut | ||
| 535 | { | ||
| 536 | public: | ||
| 537 | int64 nValue; | ||
| 538 | CScript scriptPubKey; | ||
| 539 | |||
| 540 | public: | ||
| 541 | CTxOut() | ||
| 542 | { | ||
| 543 | SetNull(); | ||
| 544 | } | ||
| 545 | |||
| 546 | CTxOut(int64 nValueIn, CScript scriptPubKeyIn) | ||
| 547 | { | ||
| 548 | nValue = nValueIn; | ||
| 549 | scriptPubKey = scriptPubKeyIn; | ||
| 550 | } | ||
| 551 | |||
| 552 | IMPLEMENT_SERIALIZE | ||
| 553 | ( | ||
| 554 | READWRITE(nValue); | ||
| 555 | READWRITE(scriptPubKey); | ||
| 556 | ) | ||
| 557 | |||
| 558 | void SetNull() | ||
| 559 | { | ||
| 560 | nValue = -1; | ||
| 561 | scriptPubKey.clear(); | ||
| 562 | } | ||
| 563 | |||
| 564 | bool IsNull() | ||
| 565 | { | ||
| 566 | return (nValue == -1); | ||
| 567 | } | ||
| 568 | |||
| 569 | uint256 GetHash() const | ||
| 570 | { | ||
| 571 | return SerializeHash(*this); | ||
| 572 | } | ||
| 573 | |||
| 574 | bool IsMine() const | ||
| 575 | { | ||
| 576 | return ::IsMine(scriptPubKey); | ||
| 577 | } | ||
| 578 | |||
| 579 | int64 GetCredit() const | ||
| 580 | { | ||
| 581 | if (IsMine()) | ||
| 582 | return nValue; | ||
| 583 | return 0; | ||
| 584 | } | ||
| 585 | |||
| 586 | friend bool operator==(const CTxOut& a, const CTxOut& b) | ||
| 587 | { | ||
| 588 | return (a.nValue == b.nValue && | ||
| 589 | a.scriptPubKey == b.scriptPubKey); | ||
| 590 | } | ||
| 591 | |||
| 592 | friend bool operator!=(const CTxOut& a, const CTxOut& b) | ||
| 593 | { | ||
| 594 | return !(a == b); | ||
| 595 | } | ||
| 596 | |||
| 597 | string ToString() const | ||
| 598 | { | ||
| 599 | if (scriptPubKey.size() < 6) | ||
| 600 | return "CTxOut(error)"; | ||
| 601 | return strprintf("CTxOut(nValue=%lld.%08lld, scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,24).c_str()); | ||
| 602 | } | ||
| 603 | |||
| 604 | void print() const | ||
| 605 | { | ||
| 606 | printf("%s\n", ToString().c_str()); | ||
| 607 | } | ||
| 608 | }; | ||
| 609 | |||
| 610 | |||
| 611 | |||
| 612 | |||
| 613 | // | ||
| 614 | // The basic transaction that is broadcasted on the network and contained in | ||
| 615 | // blocks. A transaction can contain multiple inputs and outputs. | ||
| 616 | // | ||
| 617 | class CTransaction | ||
| 618 | { | ||
| 619 | public: | ||
| 620 | int nVersion; | ||
| 621 | vector<CTxIn> vin; | ||
| 622 | vector<CTxOut> vout; | ||
| 623 | int nLockTime; | ||
| 624 | |||
| 625 | |||
| 626 | CTransaction() | ||
| 627 | { | ||
| 628 | SetNull(); | ||
| 629 | } | ||
| 630 | |||
| 631 | IMPLEMENT_SERIALIZE | ||
| 632 | ( | ||
| 633 | READWRITE(this->nVersion); | ||
| 634 | nVersion = this->nVersion; | ||
| 635 | READWRITE(vin); | ||
| 636 | READWRITE(vout); | ||
| 637 | READWRITE(nLockTime); | ||
| 638 | ) | ||
| 639 | |||
| 640 | void SetNull() | ||
| 641 | { | ||
| 642 | nVersion = 1; | ||
| 643 | vin.clear(); | ||
| 644 | vout.clear(); | ||
| 645 | nLockTime = 0; | ||
| 646 | } | ||
| 647 | |||
| 648 | bool IsNull() const | ||
| 649 | { | ||
| 650 | return (vin.empty() && vout.empty()); | ||
| 651 | } | ||
| 652 | |||
| 653 | uint256 GetHash() const | ||
| 654 | { | ||
| 655 | return SerializeHash(*this); | ||
| 656 | } | ||
| 657 | |||
| 658 | bool IsFinal() const | ||
| 659 | { | ||
| 660 | if (nLockTime == 0 || nLockTime < nBestHeight) | ||
| 661 | return true; | ||
| 662 | foreach(const CTxIn& txin, vin) | ||
| 663 | if (!txin.IsFinal()) | ||
| 664 | return false; | ||
| 665 | return true; | ||
| 666 | } | ||
| 667 | |||
| 668 | bool IsNewerThan(const CTransaction& old) const | ||
| 669 | { | ||
| 670 | if (vin.size() != old.vin.size()) | ||
| 671 | return false; | ||
| 672 | for (int i = 0; i < vin.size(); i++) | ||
| 673 | if (vin[i].prevout != old.vin[i].prevout) | ||
| 674 | return false; | ||
| 675 | |||
| 676 | bool fNewer = false; | ||
| 677 | unsigned int nLowest = UINT_MAX; | ||
| 678 | for (int i = 0; i < vin.size(); i++) | ||
| 679 | { | ||
| 680 | if (vin[i].nSequence != old.vin[i].nSequence) | ||
| 681 | { | ||
| 682 | if (vin[i].nSequence <= nLowest) | ||
| 683 | { | ||
| 684 | fNewer = false; | ||
| 685 | nLowest = vin[i].nSequence; | ||
| 686 | } | ||
| 687 | if (old.vin[i].nSequence < nLowest) | ||
| 688 | { | ||
| 689 | fNewer = true; | ||
| 690 | nLowest = old.vin[i].nSequence; | ||
| 691 | } | ||
| 692 | } | ||
| 693 | } | ||
| 694 | return fNewer; | ||
| 695 | } | ||
| 696 | |||
| 697 | bool IsCoinBase() const | ||
| 698 | { | ||
| 699 | return (vin.size() == 1 && vin[0].prevout.IsNull()); | ||
| 700 | } | ||
| 701 | |||
| 702 | unsigned int GetSigOpCount() const | ||
| 703 | { | ||
| 704 | unsigned int n = 0; | ||
| 705 | foreach(const CTxIn& txin, vin) | ||
| 706 | n += txin.scriptSig.GetSigOpCount(); | ||
| 707 | foreach(const CTxOut& txout, vout) | ||
| 708 | n += txout.scriptPubKey.GetSigOpCount(); | ||
| 709 | return n; | ||
| 710 | } | ||
| 711 | |||
| 712 | bool CheckTransaction() const | ||
| 713 | { | ||
| 714 | // Basic checks that don't depend on any context | ||
| 715 | if (vin.empty() || vout.empty()) | ||
| 716 | return error("CTransaction::CheckTransaction() : vin or vout empty"); | ||
| 717 | |||
| 718 | // Check for negative or overflow output values (CVE-2010-5139) | ||
| 719 | int64 nValueOut = 0; | ||
| 720 | foreach(const CTxOut& txout, vout) | ||
| 721 | { | ||
| 722 | if (txout.nValue < 0) | ||
| 723 | return error("CTransaction::CheckTransaction() : txout.nValue negative"); | ||
| 724 | if (txout.nValue > MAX_MONEY) | ||
| 725 | return error("CTransaction::CheckTransaction() : txout.nValue too high"); | ||
| 726 | nValueOut += txout.nValue; | ||
| 727 | if (!MoneyRange(nValueOut)) | ||
| 728 | return error("CTransaction::CheckTransaction() : txout total out of range"); | ||
| 729 | } | ||
| 730 | |||
| 731 | if (IsCoinBase()) | ||
| 732 | { | ||
| 733 | if (vin[0].scriptSig.size() < 2 || vin[0].scriptSig.size() > 100) | ||
| 734 | return error("CTransaction::CheckTransaction() : coinbase script size"); | ||
| 735 | } | ||
| 736 | else | ||
| 737 | { | ||
| 738 | foreach(const CTxIn& txin, vin) | ||
| 739 | if (txin.prevout.IsNull()) | ||
| 740 | return error("CTransaction::CheckTransaction() : prevout is null"); | ||
| 741 | } | ||
| 742 | |||
| 743 | // Check for duplicate inputs (a tx spending the same output twice) | ||
| 744 | set<COutPoint> vInOutPoints; | ||
| 745 | foreach(const CTxIn& txin, vin) | ||
| 746 | { | ||
| 747 | if (vInOutPoints.count(txin.prevout)) | ||
| 748 | return error("CTransaction::CheckTransaction() : duplicate inputs"); | ||
| 749 | vInOutPoints.insert(txin.prevout); | ||
| 750 | } | ||
| 751 | |||
| 752 | return true; | ||
| 753 | } | ||
| 754 | |||
| 755 | bool IsMine() const | ||
| 756 | { | ||
| 757 | foreach(const CTxOut& txout, vout) | ||
| 758 | if (txout.IsMine()) | ||
| 759 | return true; | ||
| 760 | return false; | ||
| 761 | } | ||
| 762 | |||
| 763 | int64 GetDebit() const | ||
| 764 | { | ||
| 765 | int64 nDebit = 0; | ||
| 766 | foreach(const CTxIn& txin, vin) | ||
| 767 | nDebit += txin.GetDebit(); | ||
| 768 | return nDebit; | ||
| 769 | } | ||
| 770 | |||
| 771 | int64 GetCredit() const | ||
| 772 | { | ||
| 773 | int64 nCredit = 0; | ||
| 774 | foreach(const CTxOut& txout, vout) | ||
| 775 | nCredit += txout.GetCredit(); | ||
| 776 | return nCredit; | ||
| 777 | } | ||
| 778 | |||
| 779 | int64 GetValueOut() const | ||
| 780 | { | ||
| 781 | int64 nValueOut = 0; | ||
| 782 | foreach(const CTxOut& txout, vout) | ||
| 783 | { | ||
| 784 | if (txout.nValue < 0) | ||
| 785 | throw runtime_error("CTransaction::GetValueOut() : negative value"); | ||
| 786 | if (txout.nValue > MAX_MONEY) | ||
| 787 | throw runtime_error("CTransaction::GetValueOut() : value too high"); | ||
| 788 | nValueOut += txout.nValue; | ||
| 789 | if (!MoneyRange(nValueOut)) | ||
| 790 | throw runtime_error("CTransaction::GetValueOut() : total out of range"); | ||
| 791 | } | ||
| 792 | return nValueOut; | ||
| 793 | } | ||
| 794 | |||
| 795 | int64 GetMinFee(bool fDiscount=false) const | ||
| 796 | { | ||
| 797 | unsigned int nBytes = ::GetSerializeSize(*this, SER_NETWORK); | ||
| 798 | if (fDiscount && nBytes < 10000) | ||
| 799 | return 0; | ||
| 800 | return (1 + (int64)nBytes / 1000) * CENT; | ||
| 801 | } | ||
| 802 | |||
| 803 | |||
| 804 | |||
| 805 | bool ReadFromDisk(CDiskTxPos pos, FILE** pfileRet=NULL) | ||
| 806 | { | ||
| 807 | CAutoFile filein = OpenBlockFile(pos.nFile, 0, pfileRet ? "rb+" : "rb"); | ||
| 808 | if (!filein) | ||
| 809 | return error("CTransaction::ReadFromDisk() : OpenBlockFile failed"); | ||
| 810 | |||
| 811 | // Read transaction | ||
| 812 | if (fseek(filein, pos.nTxPos, SEEK_SET) != 0) | ||
| 813 | return error("CTransaction::ReadFromDisk() : fseek failed"); | ||
| 814 | filein >> *this; | ||
| 815 | |||
| 816 | // Return file pointer | ||
| 817 | if (pfileRet) | ||
| 818 | { | ||
| 819 | if (fseek(filein, pos.nTxPos, SEEK_SET) != 0) | ||
| 820 | return error("CTransaction::ReadFromDisk() : second fseek failed"); | ||
| 821 | *pfileRet = filein.release(); | ||
| 822 | } | ||
| 823 | return true; | ||
| 824 | } | ||
| 825 | |||
| 826 | |||
| 827 | friend bool operator==(const CTransaction& a, const CTransaction& b) | ||
| 828 | { | ||
| 829 | return (a.nVersion == b.nVersion && | ||
| 830 | a.vin == b.vin && | ||
| 831 | a.vout == b.vout && | ||
| 832 | a.nLockTime == b.nLockTime); | ||
| 833 | } | ||
| 834 | |||
| 835 | friend bool operator!=(const CTransaction& a, const CTransaction& b) | ||
| 836 | { | ||
| 837 | return !(a == b); | ||
| 838 | } | ||
| 839 | |||
| 840 | |||
| 841 | string ToString() const | ||
| 842 | { | ||
| 843 | string str; | ||
| 844 | str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%d, vout.size=%d, nLockTime=%u)\n", | ||
| 845 | GetHash().ToString().substr(0,6).c_str(), | ||
| 846 | nVersion, | ||
| 847 | (int)vin.size(), | ||
| 848 | (int)vout.size(), | ||
| 849 | nLockTime); | ||
| 850 | for (int i = 0; i < vin.size(); i++) | ||
| 851 | str += " " + vin[i].ToString() + "\n"; | ||
| 852 | for (int i = 0; i < vout.size(); i++) | ||
| 853 | str += " " + vout[i].ToString() + "\n"; | ||
| 854 | return str; | ||
| 855 | } | ||
| 856 | |||
| 857 | void print() const | ||
| 858 | { | ||
| 859 | printf("%s", ToString().c_str()); | ||
| 860 | } | ||
| 861 | |||
| 862 | |||
| 863 | |||
| 864 | bool DisconnectInputs(CTxDB& txdb); | ||
| 865 | bool ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx, int nHeight, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee=0); | ||
| 866 | bool ClientConnectInputs(); | ||
| 867 | |||
| 868 | bool AcceptTransaction(CTxDB& txdb, bool fCheckInputs=true, bool* pfMissingInputs=NULL); | ||
| 869 | |||
| 870 | bool AcceptTransaction(bool fCheckInputs=true, bool* pfMissingInputs=NULL) | ||
| 871 | { | ||
| 872 | CTxDB txdb("r"); | ||
| 873 | return AcceptTransaction(txdb, fCheckInputs, pfMissingInputs); | ||
| 874 | } | ||
| 875 | |||
| 876 | protected: | ||
| 877 | bool AddToMemoryPool(); | ||
| 878 | public: | ||
| 879 | bool RemoveFromMemoryPool(); | ||
| 880 | }; | ||
| 881 | |||
| 882 | |||
| 883 | |||
| 884 | |||
| 885 | |||
| 886 | // | ||
| 887 | // A transaction with a merkle branch linking it to the block chain | ||
| 888 | // | ||
| 889 | class CMerkleTx : public CTransaction | ||
| 890 | { | ||
| 891 | public: | ||
| 892 | uint256 hashBlock; | ||
| 893 | vector<uint256> vMerkleBranch; | ||
| 894 | int nIndex; | ||
| 895 | |||
| 896 | // memory only | ||
| 897 | mutable bool fMerkleVerified; | ||
| 898 | |||
| 899 | |||
| 900 | CMerkleTx() | ||
| 901 | { | ||
| 902 | Init(); | ||
| 903 | } | ||
| 904 | |||
| 905 | CMerkleTx(const CTransaction& txIn) : CTransaction(txIn) | ||
| 906 | { | ||
| 907 | Init(); | ||
| 908 | } | ||
| 909 | |||
| 910 | void Init() | ||
| 911 | { | ||
| 912 | hashBlock = 0; | ||
| 913 | nIndex = -1; | ||
| 914 | fMerkleVerified = false; | ||
| 915 | } | ||
| 916 | |||
| 917 | int64 GetCredit() const | ||
| 918 | { | ||
| 919 | // Must wait until coinbase is safely deep enough in the chain before valuing it | ||
| 920 | if (IsCoinBase() && GetBlocksToMaturity() > 0) | ||
| 921 | return 0; | ||
| 922 | return CTransaction::GetCredit(); | ||
| 923 | } | ||
| 924 | |||
| 925 | IMPLEMENT_SERIALIZE | ||
| 926 | ( | ||
| 927 | nSerSize += SerReadWrite(s, *(CTransaction*)this, nType, nVersion, ser_action); | ||
| 928 | nVersion = this->nVersion; | ||
| 929 | READWRITE(hashBlock); | ||
| 930 | READWRITE(vMerkleBranch); | ||
| 931 | READWRITE(nIndex); | ||
| 932 | ) | ||
| 933 | |||
| 934 | |||
| 935 | int SetMerkleBranch(const CBlock* pblock=NULL); | ||
| 936 | int GetDepthInMainChain() const; | ||
| 937 | bool IsInMainChain() const { return GetDepthInMainChain() > 0; } | ||
| 938 | int GetBlocksToMaturity() const; | ||
| 939 | bool AcceptTransaction(CTxDB& txdb, bool fCheckInputs=true); | ||
| 940 | bool AcceptTransaction() { CTxDB txdb("r"); return AcceptTransaction(txdb); } | ||
| 941 | }; | ||
| 942 | |||
| 943 | |||
| 944 | |||
| 945 | |||
| 946 | // | ||
| 947 | // A transaction with a bunch of additional info that only the owner cares | ||
| 948 | // about. It includes any unrecorded transactions needed to link it back | ||
| 949 | // to the block chain. | ||
| 950 | // | ||
| 951 | class CWalletTx : public CMerkleTx | ||
| 952 | { | ||
| 953 | public: | ||
| 954 | vector<CMerkleTx> vtxPrev; | ||
| 955 | map<string, string> mapValue; | ||
| 956 | vector<pair<string, string> > vOrderForm; | ||
| 957 | unsigned int fTimeReceivedIsTxTime; | ||
| 958 | unsigned int nTimeReceived; // time received by this node | ||
| 959 | char fFromMe; | ||
| 960 | char fSpent; | ||
| 961 | //// probably need to sign the order info so know it came from payer | ||
| 962 | |||
| 963 | // memory only | ||
| 964 | mutable unsigned int nTimeDisplayed; | ||
| 965 | |||
| 966 | |||
| 967 | CWalletTx() | ||
| 968 | { | ||
| 969 | Init(); | ||
| 970 | } | ||
| 971 | |||
| 972 | CWalletTx(const CMerkleTx& txIn) : CMerkleTx(txIn) | ||
| 973 | { | ||
| 974 | Init(); | ||
| 975 | } | ||
| 976 | |||
| 977 | CWalletTx(const CTransaction& txIn) : CMerkleTx(txIn) | ||
| 978 | { | ||
| 979 | Init(); | ||
| 980 | } | ||
| 981 | |||
| 982 | void Init() | ||
| 983 | { | ||
| 984 | fTimeReceivedIsTxTime = false; | ||
| 985 | nTimeReceived = 0; | ||
| 986 | fFromMe = false; | ||
| 987 | fSpent = false; | ||
| 988 | nTimeDisplayed = 0; | ||
| 989 | } | ||
| 990 | |||
| 991 | IMPLEMENT_SERIALIZE | ||
| 992 | ( | ||
| 993 | nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion, ser_action); | ||
| 994 | nVersion = this->nVersion; | ||
| 995 | READWRITE(vtxPrev); | ||
| 996 | READWRITE(mapValue); | ||
| 997 | READWRITE(vOrderForm); | ||
| 998 | READWRITE(fTimeReceivedIsTxTime); | ||
| 999 | READWRITE(nTimeReceived); | ||
| 1000 | READWRITE(fFromMe); | ||
| 1001 | READWRITE(fSpent); | ||
| 1002 | ) | ||
| 1003 | |||
| 1004 | bool WriteToDisk() | ||
| 1005 | { | ||
| 1006 | return CWalletDB().WriteTx(GetHash(), *this); | ||
| 1007 | } | ||
| 1008 | |||
| 1009 | |||
| 1010 | int64 GetTxTime() const; | ||
| 1011 | |||
| 1012 | void AddSupportingTransactions(CTxDB& txdb); | ||
| 1013 | |||
| 1014 | bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true); | ||
| 1015 | bool AcceptWalletTransaction() { CTxDB txdb("r"); return AcceptWalletTransaction(txdb); } | ||
| 1016 | |||
| 1017 | void RelayWalletTransaction(CTxDB& txdb); | ||
| 1018 | void RelayWalletTransaction() { CTxDB txdb("r"); RelayWalletTransaction(txdb); } | ||
| 1019 | }; | ||
| 1020 | |||
| 1021 | |||
| 1022 | |||
| 1023 | |||
| 1024 | // | ||
| 1025 | // A txdb record that contains the disk location of a transaction and the | ||
| 1026 | // locations of transactions that spend its outputs. vSpent is really only | ||
| 1027 | // used as a flag, but having the location is very helpful for debugging. | ||
| 1028 | // | ||
| 1029 | class CTxIndex | ||
| 1030 | { | ||
| 1031 | public: | ||
| 1032 | CDiskTxPos pos; | ||
| 1033 | vector<CDiskTxPos> vSpent; | ||
| 1034 | |||
| 1035 | CTxIndex() | ||
| 1036 | { | ||
| 1037 | SetNull(); | ||
| 1038 | } | ||
| 1039 | |||
| 1040 | CTxIndex(const CDiskTxPos& posIn, unsigned int nOutputs) | ||
| 1041 | { | ||
| 1042 | pos = posIn; | ||
| 1043 | vSpent.resize(nOutputs); | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | IMPLEMENT_SERIALIZE | ||
| 1047 | ( | ||
| 1048 | if (!(nType & SER_GETHASH)) | ||
| 1049 | READWRITE(nVersion); | ||
| 1050 | READWRITE(pos); | ||
| 1051 | READWRITE(vSpent); | ||
| 1052 | ) | ||
| 1053 | |||
| 1054 | void SetNull() | ||
| 1055 | { | ||
| 1056 | pos.SetNull(); | ||
| 1057 | vSpent.clear(); | ||
| 1058 | } | ||
| 1059 | |||
| 1060 | bool IsNull() | ||
| 1061 | { | ||
| 1062 | return pos.IsNull(); | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | friend bool operator==(const CTxIndex& a, const CTxIndex& b) | ||
| 1066 | { | ||
| 1067 | if (a.pos != b.pos || a.vSpent.size() != b.vSpent.size()) | ||
| 1068 | return false; | ||
| 1069 | for (int i = 0; i < a.vSpent.size(); i++) | ||
| 1070 | if (a.vSpent[i] != b.vSpent[i]) | ||
| 1071 | return false; | ||
| 1072 | return true; | ||
| 1073 | } | ||
| 1074 | |||
| 1075 | friend bool operator!=(const CTxIndex& a, const CTxIndex& b) | ||
| 1076 | { | ||
| 1077 | return !(a == b); | ||
| 1078 | } | ||
| 1079 | }; | ||
| 1080 | |||
| 1081 | |||
| 1082 | |||
| 1083 | |||
| 1084 | |||
| 1085 | // | ||
| 1086 | // Nodes collect new transactions into a block, hash them into a hash tree, | ||
| 1087 | // and scan through nonce values to make the block's hash satisfy proof-of-work | ||
| 1088 | // requirements. When they solve the proof-of-work, they broadcast the block | ||
| 1089 | // to everyone and the block is added to the block chain. The first transaction | ||
| 1090 | // in the block is a special one that creates a new coin owned by the creator | ||
| 1091 | // of the block. | ||
| 1092 | // | ||
| 1093 | // Blocks are appended to blk0001.dat files on disk. Their location on disk | ||
| 1094 | // is indexed by CBlockIndex objects in memory. | ||
| 1095 | // | ||
| 1096 | class CBlock | ||
| 1097 | { | ||
| 1098 | public: | ||
| 1099 | // header | ||
| 1100 | int nVersion; | ||
| 1101 | uint256 hashPrevBlock; | ||
| 1102 | uint256 hashMerkleRoot; | ||
| 1103 | unsigned int nTime; | ||
| 1104 | unsigned int nBits; | ||
| 1105 | unsigned int nNonce; | ||
| 1106 | |||
| 1107 | // network and disk | ||
| 1108 | vector<CTransaction> vtx; | ||
| 1109 | |||
| 1110 | // memory only | ||
| 1111 | mutable vector<uint256> vMerkleTree; | ||
| 1112 | |||
| 1113 | |||
| 1114 | CBlock() | ||
| 1115 | { | ||
| 1116 | SetNull(); | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | IMPLEMENT_SERIALIZE | ||
| 1120 | ( | ||
| 1121 | READWRITE(this->nVersion); | ||
| 1122 | nVersion = this->nVersion; | ||
| 1123 | READWRITE(hashPrevBlock); | ||
| 1124 | READWRITE(hashMerkleRoot); | ||
| 1125 | READWRITE(nTime); | ||
| 1126 | READWRITE(nBits); | ||
| 1127 | READWRITE(nNonce); | ||
| 1128 | |||
| 1129 | // ConnectBlock depends on vtx being last so it can calculate offset | ||
| 1130 | if (!(nType & (SER_GETHASH|SER_BLOCKHEADERONLY))) | ||
| 1131 | READWRITE(vtx); | ||
| 1132 | else if (fRead) | ||
| 1133 | const_cast<CBlock*>(this)->vtx.clear(); | ||
| 1134 | ) | ||
| 1135 | |||
| 1136 | void SetNull() | ||
| 1137 | { | ||
| 1138 | nVersion = 1; | ||
| 1139 | hashPrevBlock = 0; | ||
| 1140 | hashMerkleRoot = 0; | ||
| 1141 | nTime = 0; | ||
| 1142 | nBits = 0; | ||
| 1143 | nNonce = 0; | ||
| 1144 | vtx.clear(); | ||
| 1145 | vMerkleTree.clear(); | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | bool IsNull() const | ||
| 1149 | { | ||
| 1150 | return (nBits == 0); | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | uint256 GetHash() const | ||
| 1154 | { | ||
| 1155 | // Block IDENTITY hash (SHA-256d), used for indexing. | ||
| 1156 | return Hash(BEGIN(nVersion), END(nNonce)); | ||
| 1157 | } | ||
| 1158 | |||
| 1159 | uint256 GetPoWHash() const | ||
| 1160 | { | ||
| 1161 | // Memory-hard PROOF-OF-WORK hash (RandomX) over the 80-byte header. | ||
| 1162 | // This is the one that must be <= target (nBits). | ||
| 1163 | return RandomXPoWHash((const void*)BEGIN(nVersion), END(nNonce) - BEGIN(nVersion)); | ||
| 1164 | } | ||
| 1165 | |||
| 1166 | bool CheckSizeLimits() const | ||
| 1167 | { | ||
| 1168 | // The middle test compares a transaction *count* against a *byte* | ||
| 1169 | // limit. It is inherited from 0.1.0, where the same line compared | ||
| 1170 | // vtx.size() against MAX_SIZE, and it is inert: the smallest possible | ||
| 1171 | // transaction serializes to more than sixty bytes, so a block holding | ||
| 1172 | // over a million of them cannot also weigh under a megabyte. | ||
| 1173 | // | ||
| 1174 | // Kept rather than removed. This is consensus code, and a rewrite that | ||
| 1175 | // reads better is still a rewrite that can change what the network | ||
| 1176 | // accepts. Written down so the next reader does not have to derive it, | ||
| 1177 | // or "fix" it into something that does. | ||
| 1178 | return (!vtx.empty() && | ||
| 1179 | vtx.size() <= MAX_BLOCK_SIZE && | ||
| 1180 | ::GetSerializeSize(*this, SER_DISK) <= MAX_BLOCK_SIZE); | ||
| 1181 | } | ||
| 1182 | |||
| 1183 | |||
| 1184 | uint256 BuildMerkleTree() const | ||
| 1185 | { | ||
| 1186 | vMerkleTree.clear(); | ||
| 1187 | foreach(const CTransaction& tx, vtx) | ||
| 1188 | vMerkleTree.push_back(tx.GetHash()); | ||
| 1189 | int j = 0; | ||
| 1190 | for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) | ||
| 1191 | { | ||
| 1192 | for (int i = 0; i < nSize; i += 2) | ||
| 1193 | { | ||
| 1194 | int i2 = min(i+1, nSize-1); | ||
| 1195 | vMerkleTree.push_back(Hash(BEGIN(vMerkleTree[j+i]), END(vMerkleTree[j+i]), | ||
| 1196 | BEGIN(vMerkleTree[j+i2]), END(vMerkleTree[j+i2]))); | ||
| 1197 | } | ||
| 1198 | j += nSize; | ||
| 1199 | } | ||
| 1200 | return (vMerkleTree.empty() ? 0 : vMerkleTree.back()); | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | vector<uint256> GetMerkleBranch(int nIndex) const | ||
| 1204 | { | ||
| 1205 | if (vMerkleTree.empty()) | ||
| 1206 | BuildMerkleTree(); | ||
| 1207 | vector<uint256> vMerkleBranch; | ||
| 1208 | int j = 0; | ||
| 1209 | for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) | ||
| 1210 | { | ||
| 1211 | int i = min(nIndex^1, nSize-1); | ||
| 1212 | vMerkleBranch.push_back(vMerkleTree[j+i]); | ||
| 1213 | nIndex >>= 1; | ||
| 1214 | j += nSize; | ||
| 1215 | } | ||
| 1216 | return vMerkleBranch; | ||
| 1217 | } | ||
| 1218 | |||
| 1219 | static uint256 CheckMerkleBranch(uint256 hash, const vector<uint256>& vMerkleBranch, int nIndex) | ||
| 1220 | { | ||
| 1221 | if (nIndex == -1) | ||
| 1222 | return 0; | ||
| 1223 | foreach(const uint256& otherside, vMerkleBranch) | ||
| 1224 | { | ||
| 1225 | if (nIndex & 1) | ||
| 1226 | hash = Hash(BEGIN(otherside), END(otherside), BEGIN(hash), END(hash)); | ||
| 1227 | else | ||
| 1228 | hash = Hash(BEGIN(hash), END(hash), BEGIN(otherside), END(otherside)); | ||
| 1229 | nIndex >>= 1; | ||
| 1230 | } | ||
| 1231 | return hash; | ||
| 1232 | } | ||
| 1233 | |||
| 1234 | |||
| 1235 | bool WriteToDisk(bool fWriteTransactions, unsigned int& nFileRet, unsigned int& nBlockPosRet) | ||
| 1236 | { | ||
| 1237 | // Open history file to append | ||
| 1238 | CAutoFile fileout = AppendBlockFile(nFileRet); | ||
| 1239 | if (!fileout) | ||
| 1240 | return error("CBlock::WriteToDisk() : AppendBlockFile failed"); | ||
| 1241 | if (!fWriteTransactions) | ||
| 1242 | fileout.nType |= SER_BLOCKHEADERONLY; | ||
| 1243 | |||
| 1244 | // Write index header | ||
| 1245 | unsigned int nSize = fileout.GetSerializeSize(*this); | ||
| 1246 | fileout << FLATDATA(pchMessageStart) << nSize; | ||
| 1247 | |||
| 1248 | // Write block | ||
| 1249 | nBlockPosRet = ftell(fileout); | ||
| 1250 | if (nBlockPosRet == -1) | ||
| 1251 | return error("CBlock::WriteToDisk() : ftell failed"); | ||
| 1252 | fileout << *this; | ||
| 1253 | |||
| 1254 | return true; | ||
| 1255 | } | ||
| 1256 | |||
| 1257 | bool ReadFromDisk(unsigned int nFile, unsigned int nBlockPos, bool fReadTransactions) | ||
| 1258 | { | ||
| 1259 | SetNull(); | ||
| 1260 | |||
| 1261 | // Open history file to read | ||
| 1262 | CAutoFile filein = OpenBlockFile(nFile, nBlockPos, "rb"); | ||
| 1263 | if (!filein) | ||
| 1264 | return error("CBlock::ReadFromDisk() : OpenBlockFile failed"); | ||
| 1265 | if (!fReadTransactions) | ||
| 1266 | filein.nType |= SER_BLOCKHEADERONLY; | ||
| 1267 | |||
| 1268 | // Read block | ||
| 1269 | filein >> *this; | ||
| 1270 | |||
| 1271 | // Check the header | ||
| 1272 | if (CBigNum().SetCompact(nBits) > bnProofOfWorkLimit) | ||
| 1273 | return error("CBlock::ReadFromDisk() : nBits errors in block header"); | ||
| 1274 | // NOTE: the proof of work is RandomX (GetPoWHash), validated once by | ||
| 1275 | // CheckBlock when the block is accepted. Re-checking it on every disk | ||
| 1276 | // read would be prohibitively slow, and checking the SHA-256d GetHash() | ||
| 1277 | // here (a leftover from Bitcoin) is simply wrong for a RandomX chain. | ||
| 1278 | |||
| 1279 | return true; | ||
| 1280 | } | ||
| 1281 | |||
| 1282 | |||
| 1283 | |||
| 1284 | void print() const | ||
| 1285 | { | ||
| 1286 | printf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%d)\n", | ||
| 1287 | GetHash().ToString().substr(0,14).c_str(), | ||
| 1288 | nVersion, | ||
| 1289 | hashPrevBlock.ToString().substr(0,14).c_str(), | ||
| 1290 | hashMerkleRoot.ToString().substr(0,6).c_str(), | ||
| 1291 | nTime, nBits, nNonce, | ||
| 1292 | (int)vtx.size()); | ||
| 1293 | for (int i = 0; i < vtx.size(); i++) | ||
| 1294 | { | ||
| 1295 | printf(" "); | ||
| 1296 | vtx[i].print(); | ||
| 1297 | } | ||
| 1298 | printf(" vMerkleTree: "); | ||
| 1299 | for (int i = 0; i < vMerkleTree.size(); i++) | ||
| 1300 | printf("%s ", vMerkleTree[i].ToString().substr(0,6).c_str()); | ||
| 1301 | printf("\n"); | ||
| 1302 | } | ||
| 1303 | |||
| 1304 | |||
| 1305 | int64 GetBlockValue(int nHeight, int64 nFees) const; | ||
| 1306 | bool DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex); | ||
| 1307 | bool ConnectBlock(CTxDB& txdb, CBlockIndex* pindex); | ||
| 1308 | bool ReadFromDisk(const CBlockIndex* blockindex, bool fReadTransactions); | ||
| 1309 | bool AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos); | ||
| 1310 | bool CheckBlock() const; | ||
| 1311 | bool AcceptBlock(); | ||
| 1312 | }; | ||
| 1313 | |||
| 1314 | |||
| 1315 | |||
| 1316 | |||
| 1317 | |||
| 1318 | |||
| 1319 | // | ||
| 1320 | // The block chain is a tree shaped structure starting with the | ||
| 1321 | // genesis block at the root, with each block potentially having multiple | ||
| 1322 | // candidates to be the next block. pprev and pnext link a path through the | ||
| 1323 | // main/longest chain. A blockindex may have multiple pprev pointing back | ||
| 1324 | // to it, but pnext will only point forward to the longest branch, or will | ||
| 1325 | // be null if the block is not part of the longest chain. | ||
| 1326 | // | ||
| 1327 | class CBlockIndex | ||
| 1328 | { | ||
| 1329 | public: | ||
| 1330 | const uint256* phashBlock; | ||
| 1331 | CBlockIndex* pprev; | ||
| 1332 | CBlockIndex* pnext; | ||
| 1333 | unsigned int nFile; | ||
| 1334 | unsigned int nBlockPos; | ||
| 1335 | int nHeight; | ||
| 1336 | |||
| 1337 | // block header | ||
| 1338 | int nVersion; | ||
| 1339 | uint256 hashMerkleRoot; | ||
| 1340 | unsigned int nTime; | ||
| 1341 | unsigned int nBits; | ||
| 1342 | unsigned int nNonce; | ||
| 1343 | |||
| 1344 | |||
| 1345 | CBlockIndex() | ||
| 1346 | { | ||
| 1347 | phashBlock = NULL; | ||
| 1348 | pprev = NULL; | ||
| 1349 | pnext = NULL; | ||
| 1350 | nFile = 0; | ||
| 1351 | nBlockPos = 0; | ||
| 1352 | nHeight = 0; | ||
| 1353 | |||
| 1354 | nVersion = 0; | ||
| 1355 | hashMerkleRoot = 0; | ||
| 1356 | nTime = 0; | ||
| 1357 | nBits = 0; | ||
| 1358 | nNonce = 0; | ||
| 1359 | } | ||
| 1360 | |||
| 1361 | CBlockIndex(unsigned int nFileIn, unsigned int nBlockPosIn, CBlock& block) | ||
| 1362 | { | ||
| 1363 | phashBlock = NULL; | ||
| 1364 | pprev = NULL; | ||
| 1365 | pnext = NULL; | ||
| 1366 | nFile = nFileIn; | ||
| 1367 | nBlockPos = nBlockPosIn; | ||
| 1368 | nHeight = 0; | ||
| 1369 | |||
| 1370 | nVersion = block.nVersion; | ||
| 1371 | hashMerkleRoot = block.hashMerkleRoot; | ||
| 1372 | nTime = block.nTime; | ||
| 1373 | nBits = block.nBits; | ||
| 1374 | nNonce = block.nNonce; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | uint256 GetBlockHash() const | ||
| 1378 | { | ||
| 1379 | return *phashBlock; | ||
| 1380 | } | ||
| 1381 | |||
| 1382 | bool IsInMainChain() const | ||
| 1383 | { | ||
| 1384 | return (pnext || this == pindexBest); | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | bool EraseBlockFromDisk() | ||
| 1388 | { | ||
| 1389 | // Open history file | ||
| 1390 | CAutoFile fileout = OpenBlockFile(nFile, nBlockPos, "rb+"); | ||
| 1391 | if (!fileout) | ||
| 1392 | return false; | ||
| 1393 | |||
| 1394 | // Overwrite with empty null block | ||
| 1395 | CBlock block; | ||
| 1396 | block.SetNull(); | ||
| 1397 | fileout << block; | ||
| 1398 | |||
| 1399 | return true; | ||
| 1400 | } | ||
| 1401 | |||
| 1402 | enum { nMedianTimeSpan=11 }; | ||
| 1403 | |||
| 1404 | int64 GetMedianTimePast() const | ||
| 1405 | { | ||
| 1406 | unsigned int pmedian[nMedianTimeSpan]; | ||
| 1407 | unsigned int* pbegin = &pmedian[nMedianTimeSpan]; | ||
| 1408 | unsigned int* pend = &pmedian[nMedianTimeSpan]; | ||
| 1409 | |||
| 1410 | const CBlockIndex* pindex = this; | ||
| 1411 | for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev) | ||
| 1412 | *(--pbegin) = pindex->nTime; | ||
| 1413 | |||
| 1414 | sort(pbegin, pend); | ||
| 1415 | return pbegin[(pend - pbegin)/2]; | ||
| 1416 | } | ||
| 1417 | |||
| 1418 | int64 GetMedianTime() const | ||
| 1419 | { | ||
| 1420 | const CBlockIndex* pindex = this; | ||
| 1421 | for (int i = 0; i < nMedianTimeSpan/2; i++) | ||
| 1422 | { | ||
| 1423 | if (!pindex->pnext) | ||
| 1424 | return nTime; | ||
| 1425 | pindex = pindex->pnext; | ||
| 1426 | } | ||
| 1427 | return pindex->GetMedianTimePast(); | ||
| 1428 | } | ||
| 1429 | |||
| 1430 | |||
| 1431 | |||
| 1432 | string ToString() const | ||
| 1433 | { | ||
| 1434 | // pprev/pnext are pointers: %08x truncated them to 32 bits on a 64-bit | ||
| 1435 | // build, so the two fields this line exists to show were unreliable. | ||
| 1436 | return strprintf("CBlockIndex(nprev=%p, pnext=%p, nFile=%d, nBlockPos=%-6d nHeight=%d, merkle=%s, hashBlock=%s)", | ||
| 1437 | (void*)pprev, (void*)pnext, nFile, nBlockPos, nHeight, | ||
| 1438 | hashMerkleRoot.ToString().substr(0,6).c_str(), | ||
| 1439 | GetBlockHash().ToString().substr(0,14).c_str()); | ||
| 1440 | } | ||
| 1441 | |||
| 1442 | void print() const | ||
| 1443 | { | ||
| 1444 | printf("%s\n", ToString().c_str()); | ||
| 1445 | } | ||
| 1446 | }; | ||
| 1447 | |||
| 1448 | |||
| 1449 | |||
| 1450 | // | ||
| 1451 | // Used to marshal pointers into hashes for db storage. | ||
| 1452 | // | ||
| 1453 | class CDiskBlockIndex : public CBlockIndex | ||
| 1454 | { | ||
| 1455 | public: | ||
| 1456 | uint256 hashPrev; | ||
| 1457 | uint256 hashNext; | ||
| 1458 | |||
| 1459 | CDiskBlockIndex() | ||
| 1460 | { | ||
| 1461 | hashPrev = 0; | ||
| 1462 | hashNext = 0; | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex) | ||
| 1466 | { | ||
| 1467 | hashPrev = (pprev ? pprev->GetBlockHash() : 0); | ||
| 1468 | hashNext = (pnext ? pnext->GetBlockHash() : 0); | ||
| 1469 | } | ||
| 1470 | |||
| 1471 | IMPLEMENT_SERIALIZE | ||
| 1472 | ( | ||
| 1473 | if (!(nType & SER_GETHASH)) | ||
| 1474 | READWRITE(nVersion); | ||
| 1475 | |||
| 1476 | READWRITE(hashNext); | ||
| 1477 | READWRITE(nFile); | ||
| 1478 | READWRITE(nBlockPos); | ||
| 1479 | READWRITE(nHeight); | ||
| 1480 | |||
| 1481 | // block header | ||
| 1482 | READWRITE(this->nVersion); | ||
| 1483 | READWRITE(hashPrev); | ||
| 1484 | READWRITE(hashMerkleRoot); | ||
| 1485 | READWRITE(nTime); | ||
| 1486 | READWRITE(nBits); | ||
| 1487 | READWRITE(nNonce); | ||
| 1488 | ) | ||
| 1489 | |||
| 1490 | uint256 GetBlockHash() const | ||
| 1491 | { | ||
| 1492 | CBlock block; | ||
| 1493 | block.nVersion = nVersion; | ||
| 1494 | block.hashPrevBlock = hashPrev; | ||
| 1495 | block.hashMerkleRoot = hashMerkleRoot; | ||
| 1496 | block.nTime = nTime; | ||
| 1497 | block.nBits = nBits; | ||
| 1498 | block.nNonce = nNonce; | ||
| 1499 | return block.GetHash(); | ||
| 1500 | } | ||
| 1501 | |||
| 1502 | |||
| 1503 | string ToString() const | ||
| 1504 | { | ||
| 1505 | string str = "CDiskBlockIndex("; | ||
| 1506 | str += CBlockIndex::ToString(); | ||
| 1507 | str += strprintf("\n hashBlock=%s, hashPrev=%s, hashNext=%s)", | ||
| 1508 | GetBlockHash().ToString().c_str(), | ||
| 1509 | hashPrev.ToString().substr(0,14).c_str(), | ||
| 1510 | hashNext.ToString().substr(0,14).c_str()); | ||
| 1511 | return str; | ||
| 1512 | } | ||
| 1513 | |||
| 1514 | void print() const | ||
| 1515 | { | ||
| 1516 | printf("%s\n", ToString().c_str()); | ||
| 1517 | } | ||
| 1518 | }; | ||
| 1519 | |||
| 1520 | |||
| 1521 | |||
| 1522 | |||
| 1523 | |||
| 1524 | |||
| 1525 | |||
| 1526 | |||
| 1527 | // | ||
| 1528 | // Describes a place in the block chain to another node such that if the | ||
| 1529 | // other node doesn't have the same branch, it can find a recent common trunk. | ||
| 1530 | // The further back it is, the further before the fork it may be. | ||
| 1531 | // | ||
| 1532 | class CBlockLocator | ||
| 1533 | { | ||
| 1534 | protected: | ||
| 1535 | vector<uint256> vHave; | ||
| 1536 | public: | ||
| 1537 | |||
| 1538 | CBlockLocator() | ||
| 1539 | { | ||
| 1540 | } | ||
| 1541 | |||
| 1542 | explicit CBlockLocator(const CBlockIndex* pindex) | ||
| 1543 | { | ||
| 1544 | Set(pindex); | ||
| 1545 | } | ||
| 1546 | |||
| 1547 | explicit CBlockLocator(uint256 hashBlock) | ||
| 1548 | { | ||
| 1549 | map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock); | ||
| 1550 | if (mi != mapBlockIndex.end()) | ||
| 1551 | Set((*mi).second); | ||
| 1552 | } | ||
| 1553 | |||
| 1554 | IMPLEMENT_SERIALIZE | ||
| 1555 | ( | ||
| 1556 | if (!(nType & SER_GETHASH)) | ||
| 1557 | READWRITE(nVersion); | ||
| 1558 | READWRITE(vHave); | ||
| 1559 | ) | ||
| 1560 | |||
| 1561 | void Set(const CBlockIndex* pindex) | ||
| 1562 | { | ||
| 1563 | vHave.clear(); | ||
| 1564 | int nStep = 1; | ||
| 1565 | while (pindex) | ||
| 1566 | { | ||
| 1567 | vHave.push_back(pindex->GetBlockHash()); | ||
| 1568 | |||
| 1569 | // Exponentially larger steps back | ||
| 1570 | for (int i = 0; pindex && i < nStep; i++) | ||
| 1571 | pindex = pindex->pprev; | ||
| 1572 | if (vHave.size() > 10) | ||
| 1573 | nStep *= 2; | ||
| 1574 | } | ||
| 1575 | vHave.push_back(hashGenesisBlock); | ||
| 1576 | } | ||
| 1577 | |||
| 1578 | CBlockIndex* GetBlockIndex() | ||
| 1579 | { | ||
| 1580 | // Find the first block the caller has in the main chain | ||
| 1581 | foreach(const uint256& hash, vHave) | ||
| 1582 | { | ||
| 1583 | map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash); | ||
| 1584 | if (mi != mapBlockIndex.end()) | ||
| 1585 | { | ||
| 1586 | CBlockIndex* pindex = (*mi).second; | ||
| 1587 | if (pindex->IsInMainChain()) | ||
| 1588 | return pindex; | ||
| 1589 | } | ||
| 1590 | } | ||
| 1591 | return pindexGenesisBlock; | ||
| 1592 | } | ||
| 1593 | |||
| 1594 | uint256 GetBlockHash() | ||
| 1595 | { | ||
| 1596 | // Find the first block the caller has in the main chain | ||
| 1597 | foreach(const uint256& hash, vHave) | ||
| 1598 | { | ||
| 1599 | map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash); | ||
| 1600 | if (mi != mapBlockIndex.end()) | ||
| 1601 | { | ||
| 1602 | CBlockIndex* pindex = (*mi).second; | ||
| 1603 | if (pindex->IsInMainChain()) | ||
| 1604 | return hash; | ||
| 1605 | } | ||
| 1606 | } | ||
| 1607 | return hashGenesisBlock; | ||
| 1608 | } | ||
| 1609 | |||
| 1610 | int GetHeight() | ||
| 1611 | { | ||
| 1612 | CBlockIndex* pindex = GetBlockIndex(); | ||
| 1613 | if (!pindex) | ||
| 1614 | return 0; | ||
| 1615 | return pindex->nHeight; | ||
| 1616 | } | ||
| 1617 | }; | ||
| 1618 | |||
| 1619 | |||
| 1620 | |||
| 1621 | |||
| 1622 | |||
| 1623 | |||
| 1624 | |||
| 1625 | |||
| 1626 | |||
| 1627 | |||
| 1628 | |||
| 1629 | |||
| 1630 | extern map<uint256, CTransaction> mapTransactions; | ||
| 1631 | extern map<uint256, CWalletTx> mapWallet; | ||
| 1632 | extern vector<pair<uint256, bool> > vWalletUpdated; | ||
| 1633 | extern CCriticalSection cs_mapWallet; | ||
| 1634 | extern map<vector<unsigned char>, CPrivKey> mapKeys; | ||
| 1635 | extern map<uint160, vector<unsigned char> > mapPubKeys; | ||
| 1636 | extern CCriticalSection cs_mapKeys; | ||
| 1637 | extern CKey keyUser; | ||