File indexing completed on 2020-06-25 15:55:20
0001
0002
0003
0004
0005 #ifndef BITCOIN_MAIN_H
0006 #define BITCOIN_MAIN_H
0007
0008 #include "bignum.h"
0009 #include "net.h"
0010 #include "key.h"
0011 #include "script.h"
0012 #include "db.h"
0013
0014 #include <list>
0015
0016 class CBlock;
0017 class CBlockIndex;
0018 class CWalletTx;
0019 class CWallet;
0020 class CKeyItem;
0021 class CReserveKey;
0022 class CWalletDB;
0023
0024 class CAddress;
0025 class CInv;
0026 class CRequestTracker;
0027 class CNode;
0028 class CBlockIndex;
0029
0030 static const unsigned int MAX_BLOCK_SIZE = 1000000;
0031 static const unsigned int MAX_BLOCK_SIZE_GEN = MAX_BLOCK_SIZE/2;
0032 static const int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
0033 static const int MAX_ORPHAN_TRANSACTIONS = MAX_BLOCK_SIZE/100;
0034 static const int64 COIN = 100000000;
0035 static const int64 CENT = 1000000;
0036 static const int64 MIN_TX_FEE = 50000;
0037 static const int64 MIN_RELAY_TX_FEE = 10000;
0038 static const int64 MAX_MONEY = 21000000 * COIN;
0039 inline bool MoneyRange(int64 nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
0040 static const int COINBASE_MATURITY = 100;
0041
0042 static const int LOCKTIME_THRESHOLD = 500000000;
0043
0044
0045
0046
0047
0048
0049 extern CCriticalSection cs_main;
0050 extern std::map<uint256, CBlockIndex*> mapBlockIndex;
0051 extern uint256 hashGenesisBlock;
0052 extern CBlockIndex* pindexGenesisBlock;
0053 extern int nBestHeight;
0054 extern CBigNum bnBestChainWork;
0055 extern CBigNum bnBestInvalidWork;
0056 extern uint256 hashBestChain;
0057 extern CBlockIndex* pindexBest;
0058 extern unsigned int nTransactionsUpdated;
0059 extern double dHashesPerSec;
0060 extern int64 nHPSTimerStart;
0061 extern int64 nTimeBestReceived;
0062 extern CCriticalSection cs_setpwalletRegistered;
0063 extern std::set<CWallet*> setpwalletRegistered;
0064
0065
0066 extern int fGenerateBitcoins;
0067 extern int64 nTransactionFee;
0068 extern int fLimitProcessors;
0069 extern int nLimitProcessors;
0070 extern int fMinimizeToTray;
0071 extern int fMinimizeOnClose;
0072
0073
0074
0075
0076
0077 class CReserveKey;
0078 class CTxDB;
0079 class CTxIndex;
0080
0081 void RegisterWallet(CWallet* pwalletIn);
0082 void UnregisterWallet(CWallet* pwalletIn);
0083 bool ProcessBlock(CNode* pfrom, CBlock* pblock);
0084 bool CheckDiskSpace(uint64 nAdditionalBytes=0);
0085 FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode="rb");
0086 FILE* AppendBlockFile(unsigned int& nFileRet);
0087 bool LoadBlockIndex(bool fAllowNew=true);
0088 void PrintBlockTree();
0089 bool ProcessMessages(CNode* pfrom);
0090 bool SendMessages(CNode* pto, bool fSendTrickle);
0091 void GenerateBitcoins(bool fGenerate, CWallet* pwallet);
0092 CBlock* CreateNewBlock(CReserveKey& reservekey);
0093 void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
0094 void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1);
0095 bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey);
0096 bool CheckProofOfWork(uint256 hash, unsigned int nBits);
0097 unsigned int ComputeMinWork(unsigned int nBase, int64 nTime);
0098 int GetNumBlocksOfPeers();
0099 bool IsInitialBlockDownload();
0100 std::string GetWarnings(std::string strFor);
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);
0114
0115 template<typename T>
0116 bool WriteSetting(const std::string& strKey, const T& value)
0117 {
0118 bool fOk = false;
0119 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
0120 {
0121 std::string strWalletFile;
0122 if (!GetWalletFile(pwallet, strWalletFile))
0123 continue;
0124 fOk |= CWalletDB(strWalletFile).WriteSetting(strKey, value);
0125 }
0126 return fOk;
0127 }
0128
0129
0130 class CDiskTxPos
0131 {
0132 public:
0133 unsigned int nFile;
0134 unsigned int nBlockPos;
0135 unsigned int nTxPos;
0136
0137 CDiskTxPos()
0138 {
0139 SetNull();
0140 }
0141
0142 CDiskTxPos(unsigned int nFileIn, unsigned int nBlockPosIn, unsigned int nTxPosIn)
0143 {
0144 nFile = nFileIn;
0145 nBlockPos = nBlockPosIn;
0146 nTxPos = nTxPosIn;
0147 }
0148
0149 IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
0150 void SetNull() { nFile = -1; nBlockPos = 0; nTxPos = 0; }
0151 bool IsNull() const { return (nFile == -1); }
0152
0153 friend bool operator==(const CDiskTxPos& a, const CDiskTxPos& b)
0154 {
0155 return (a.nFile == b.nFile &&
0156 a.nBlockPos == b.nBlockPos &&
0157 a.nTxPos == b.nTxPos);
0158 }
0159
0160 friend bool operator!=(const CDiskTxPos& a, const CDiskTxPos& b)
0161 {
0162 return !(a == b);
0163 }
0164
0165 std::string ToString() const
0166 {
0167 if (IsNull())
0168 return strprintf("null");
0169 else
0170 return strprintf("(nFile=%d, nBlockPos=%d, nTxPos=%d)", nFile, nBlockPos, nTxPos);
0171 }
0172
0173 void print() const
0174 {
0175 printf("%s", ToString().c_str());
0176 }
0177 };
0178
0179
0180
0181
0182 class CInPoint
0183 {
0184 public:
0185 CTransaction* ptx;
0186 unsigned int n;
0187
0188 CInPoint() { SetNull(); }
0189 CInPoint(CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; }
0190 void SetNull() { ptx = NULL; n = -1; }
0191 bool IsNull() const { return (ptx == NULL && n == -1); }
0192 };
0193
0194
0195
0196
0197 class COutPoint
0198 {
0199 public:
0200 uint256 hash;
0201 unsigned int n;
0202
0203 COutPoint() { SetNull(); }
0204 COutPoint(uint256 hashIn, unsigned int nIn) { hash = hashIn; n = nIn; }
0205 IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
0206 void SetNull() { hash = 0; n = -1; }
0207 bool IsNull() const { return (hash == 0 && n == -1); }
0208
0209 friend bool operator<(const COutPoint& a, const COutPoint& b)
0210 {
0211 return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
0212 }
0213
0214 friend bool operator==(const COutPoint& a, const COutPoint& b)
0215 {
0216 return (a.hash == b.hash && a.n == b.n);
0217 }
0218
0219 friend bool operator!=(const COutPoint& a, const COutPoint& b)
0220 {
0221 return !(a == b);
0222 }
0223
0224 std::string ToString() const
0225 {
0226 return strprintf("COutPoint(%s, %d)", hash.ToString().substr(0,10).c_str(), n);
0227 }
0228
0229 void print() const
0230 {
0231 printf("%s\n", ToString().c_str());
0232 }
0233 };
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243 class CTxIn
0244 {
0245 public:
0246 COutPoint prevout;
0247 CScript scriptSig;
0248 unsigned int nSequence;
0249
0250 CTxIn()
0251 {
0252 nSequence = UINT_MAX;
0253 }
0254
0255 explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=UINT_MAX)
0256 {
0257 prevout = prevoutIn;
0258 scriptSig = scriptSigIn;
0259 nSequence = nSequenceIn;
0260 }
0261
0262 CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=UINT_MAX)
0263 {
0264 prevout = COutPoint(hashPrevTx, nOut);
0265 scriptSig = scriptSigIn;
0266 nSequence = nSequenceIn;
0267 }
0268
0269 IMPLEMENT_SERIALIZE
0270 (
0271 READWRITE(prevout);
0272 READWRITE(scriptSig);
0273 READWRITE(nSequence);
0274 )
0275
0276 bool IsFinal() const
0277 {
0278 return (nSequence == UINT_MAX);
0279 }
0280
0281 friend bool operator==(const CTxIn& a, const CTxIn& b)
0282 {
0283 return (a.prevout == b.prevout &&
0284 a.scriptSig == b.scriptSig &&
0285 a.nSequence == b.nSequence);
0286 }
0287
0288 friend bool operator!=(const CTxIn& a, const CTxIn& b)
0289 {
0290 return !(a == b);
0291 }
0292
0293 std::string ToString() const
0294 {
0295 std::string str;
0296 str += strprintf("CTxIn(");
0297 str += prevout.ToString();
0298 if (prevout.IsNull())
0299 str += strprintf(", coinbase %s", HexStr(scriptSig).c_str());
0300 else
0301 str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24).c_str());
0302 if (nSequence != UINT_MAX)
0303 str += strprintf(", nSequence=%u", nSequence);
0304 str += ")";
0305 return str;
0306 }
0307
0308 void print() const
0309 {
0310 printf("%s\n", ToString().c_str());
0311 }
0312 };
0313
0314
0315
0316
0317
0318
0319
0320
0321 class CTxOut
0322 {
0323 public:
0324 int64 nValue;
0325 CScript scriptPubKey;
0326
0327 CTxOut()
0328 {
0329 SetNull();
0330 }
0331
0332 CTxOut(int64 nValueIn, CScript scriptPubKeyIn)
0333 {
0334 nValue = nValueIn;
0335 scriptPubKey = scriptPubKeyIn;
0336 }
0337
0338 IMPLEMENT_SERIALIZE
0339 (
0340 READWRITE(nValue);
0341 READWRITE(scriptPubKey);
0342 )
0343
0344 void SetNull()
0345 {
0346 nValue = -1;
0347 scriptPubKey.clear();
0348 }
0349
0350 bool IsNull()
0351 {
0352 return (nValue == -1);
0353 }
0354
0355 uint256 GetHash() const
0356 {
0357 return SerializeHash(*this);
0358 }
0359
0360 friend bool operator==(const CTxOut& a, const CTxOut& b)
0361 {
0362 return (a.nValue == b.nValue &&
0363 a.scriptPubKey == b.scriptPubKey);
0364 }
0365
0366 friend bool operator!=(const CTxOut& a, const CTxOut& b)
0367 {
0368 return !(a == b);
0369 }
0370
0371 std::string ToString() const
0372 {
0373 if (scriptPubKey.size() < 6)
0374 return "CTxOut(error)";
0375 return strprintf("CTxOut(nValue=%"PRI64d".%08"PRI64d", scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30).c_str());
0376 }
0377
0378 void print() const
0379 {
0380 printf("%s\n", ToString().c_str());
0381 }
0382 };
0383
0384
0385
0386
0387
0388
0389
0390
0391 class CTransaction
0392 {
0393 public:
0394 int nVersion;
0395 std::vector<CTxIn> vin;
0396 std::vector<CTxOut> vout;
0397 unsigned int nLockTime;
0398
0399
0400 mutable int nDoS;
0401 bool DoS(int nDoSIn, bool fIn) const { nDoS += nDoSIn; return fIn; }
0402
0403 CTransaction()
0404 {
0405 SetNull();
0406 }
0407
0408 IMPLEMENT_SERIALIZE
0409 (
0410 READWRITE(this->nVersion);
0411 nVersion = this->nVersion;
0412 READWRITE(vin);
0413 READWRITE(vout);
0414 READWRITE(nLockTime);
0415 )
0416
0417 void SetNull()
0418 {
0419 nVersion = 1;
0420 vin.clear();
0421 vout.clear();
0422 nLockTime = 0;
0423 nDoS = 0;
0424 }
0425
0426 bool IsNull() const
0427 {
0428 return (vin.empty() && vout.empty());
0429 }
0430
0431 uint256 GetHash() const
0432 {
0433 return SerializeHash(*this);
0434 }
0435
0436 bool IsFinal(int nBlockHeight=0, int64 nBlockTime=0) const
0437 {
0438
0439 if (nLockTime == 0)
0440 return true;
0441 if (nBlockHeight == 0)
0442 nBlockHeight = nBestHeight;
0443 if (nBlockTime == 0)
0444 nBlockTime = GetAdjustedTime();
0445 if ((int64)nLockTime < (nLockTime < LOCKTIME_THRESHOLD ? (int64)nBlockHeight : nBlockTime))
0446 return true;
0447 BOOST_FOREACH(const CTxIn& txin, vin)
0448 if (!txin.IsFinal())
0449 return false;
0450 return true;
0451 }
0452
0453 bool IsNewerThan(const CTransaction& old) const
0454 {
0455 if (vin.size() != old.vin.size())
0456 return false;
0457 for (int i = 0; i < vin.size(); i++)
0458 if (vin[i].prevout != old.vin[i].prevout)
0459 return false;
0460
0461 bool fNewer = false;
0462 unsigned int nLowest = UINT_MAX;
0463 for (int i = 0; i < vin.size(); i++)
0464 {
0465 if (vin[i].nSequence != old.vin[i].nSequence)
0466 {
0467 if (vin[i].nSequence <= nLowest)
0468 {
0469 fNewer = false;
0470 nLowest = vin[i].nSequence;
0471 }
0472 if (old.vin[i].nSequence < nLowest)
0473 {
0474 fNewer = true;
0475 nLowest = old.vin[i].nSequence;
0476 }
0477 }
0478 }
0479 return fNewer;
0480 }
0481
0482 bool IsCoinBase() const
0483 {
0484 return (vin.size() == 1 && vin[0].prevout.IsNull());
0485 }
0486
0487 int GetSigOpCount() const
0488 {
0489 int n = 0;
0490 BOOST_FOREACH(const CTxIn& txin, vin)
0491 n += txin.scriptSig.GetSigOpCount();
0492 BOOST_FOREACH(const CTxOut& txout, vout)
0493 n += txout.scriptPubKey.GetSigOpCount();
0494 return n;
0495 }
0496
0497 bool IsStandard() const
0498 {
0499 BOOST_FOREACH(const CTxIn& txin, vin)
0500 if (!txin.scriptSig.IsPushOnly())
0501 return error("nonstandard txin: %s", txin.scriptSig.ToString().c_str());
0502 BOOST_FOREACH(const CTxOut& txout, vout)
0503 if (!::IsStandard(txout.scriptPubKey))
0504 return error("nonstandard txout: %s", txout.scriptPubKey.ToString().c_str());
0505 return true;
0506 }
0507
0508 int64 GetValueOut() const
0509 {
0510 int64 nValueOut = 0;
0511 BOOST_FOREACH(const CTxOut& txout, vout)
0512 {
0513 nValueOut += txout.nValue;
0514 if (!MoneyRange(txout.nValue) || !MoneyRange(nValueOut))
0515 throw std::runtime_error("CTransaction::GetValueOut() : value out of range");
0516 }
0517 return nValueOut;
0518 }
0519
0520 static bool AllowFree(double dPriority)
0521 {
0522
0523
0524 return dPriority > COIN * 144 / 250;
0525 }
0526
0527 int64 GetMinFee(unsigned int nBlockSize=1, bool fAllowFree=true, bool fForRelay=false) const
0528 {
0529
0530 int64 nBaseFee = fForRelay ? MIN_RELAY_TX_FEE : MIN_TX_FEE;
0531
0532 unsigned int nBytes = ::GetSerializeSize(*this, SER_NETWORK);
0533 unsigned int nNewBlockSize = nBlockSize + nBytes;
0534 int64 nMinFee = (1 + (int64)nBytes / 1000) * nBaseFee;
0535
0536 if (fAllowFree)
0537 {
0538 if (nBlockSize == 1)
0539 {
0540
0541
0542 if (nBytes < 10000)
0543 nMinFee = 0;
0544 }
0545 else
0546 {
0547
0548 if (nNewBlockSize < 27000)
0549 nMinFee = 0;
0550 }
0551 }
0552
0553
0554 if (nMinFee < nBaseFee)
0555 BOOST_FOREACH(const CTxOut& txout, vout)
0556 if (txout.nValue < CENT)
0557 nMinFee = nBaseFee;
0558
0559
0560 if (nBlockSize != 1 && nNewBlockSize >= MAX_BLOCK_SIZE_GEN/2)
0561 {
0562 if (nNewBlockSize >= MAX_BLOCK_SIZE_GEN)
0563 return MAX_MONEY;
0564 nMinFee *= MAX_BLOCK_SIZE_GEN / (MAX_BLOCK_SIZE_GEN - nNewBlockSize);
0565 }
0566
0567 if (!MoneyRange(nMinFee))
0568 nMinFee = MAX_MONEY;
0569 return nMinFee;
0570 }
0571
0572
0573 bool ReadFromDisk(CDiskTxPos pos, FILE** pfileRet=NULL)
0574 {
0575 CAutoFile filein = OpenBlockFile(pos.nFile, 0, pfileRet ? "rb+" : "rb");
0576 if (!filein)
0577 return error("CTransaction::ReadFromDisk() : OpenBlockFile failed");
0578
0579
0580 if (fseek(filein, pos.nTxPos, SEEK_SET) != 0)
0581 return error("CTransaction::ReadFromDisk() : fseek failed");
0582 filein >> *this;
0583
0584
0585 if (pfileRet)
0586 {
0587 if (fseek(filein, pos.nTxPos, SEEK_SET) != 0)
0588 return error("CTransaction::ReadFromDisk() : second fseek failed");
0589 *pfileRet = filein.release();
0590 }
0591 return true;
0592 }
0593
0594 friend bool operator==(const CTransaction& a, const CTransaction& b)
0595 {
0596 return (a.nVersion == b.nVersion &&
0597 a.vin == b.vin &&
0598 a.vout == b.vout &&
0599 a.nLockTime == b.nLockTime);
0600 }
0601
0602 friend bool operator!=(const CTransaction& a, const CTransaction& b)
0603 {
0604 return !(a == b);
0605 }
0606
0607
0608 std::string ToString() const
0609 {
0610 std::string str;
0611 str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%d, vout.size=%d, nLockTime=%d)\n",
0612 GetHash().ToString().substr(0,10).c_str(),
0613 nVersion,
0614 vin.size(),
0615 vout.size(),
0616 nLockTime);
0617 for (int i = 0; i < vin.size(); i++)
0618 str += " " + vin[i].ToString() + "\n";
0619 for (int i = 0; i < vout.size(); i++)
0620 str += " " + vout[i].ToString() + "\n";
0621 return str;
0622 }
0623
0624 void print() const
0625 {
0626 printf("%s", ToString().c_str());
0627 }
0628
0629
0630 bool ReadFromDisk(CTxDB& txdb, COutPoint prevout, CTxIndex& txindexRet);
0631 bool ReadFromDisk(CTxDB& txdb, COutPoint prevout);
0632 bool ReadFromDisk(COutPoint prevout);
0633 bool DisconnectInputs(CTxDB& txdb);
0634 bool ConnectInputs(CTxDB& txdb, std::map<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx,
0635 CBlockIndex* pindexBlock, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee,
0636 bool& fInvalid);
0637 bool ClientConnectInputs();
0638 bool CheckTransaction() const;
0639 bool AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs=true, bool* pfMissingInputs=NULL);
0640 bool AcceptToMemoryPool(bool fCheckInputs=true, bool* pfMissingInputs=NULL);
0641 protected:
0642 bool AddToMemoryPoolUnchecked();
0643 public:
0644 bool RemoveFromMemoryPool();
0645 };
0646
0647
0648
0649
0650
0651
0652
0653
0654 class CMerkleTx : public CTransaction
0655 {
0656 public:
0657 uint256 hashBlock;
0658 std::vector<uint256> vMerkleBranch;
0659 int nIndex;
0660
0661
0662 mutable char fMerkleVerified;
0663
0664
0665 CMerkleTx()
0666 {
0667 Init();
0668 }
0669
0670 CMerkleTx(const CTransaction& txIn) : CTransaction(txIn)
0671 {
0672 Init();
0673 }
0674
0675 void Init()
0676 {
0677 hashBlock = 0;
0678 nIndex = -1;
0679 f