aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp546
1 files changed, 384 insertions, 162 deletions
diff --git a/src/main.cpp b/src/main.cpp
index bc2fdb023..fbaf05dfc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -3,6 +3,7 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include "alert.h"
#include "checkpoints.h"
#include "db.h"
#include "net.h"
@@ -281,9 +282,12 @@ bool CTransaction::IsStandard() const
if (!txin.scriptSig.IsPushOnly())
return false;
}
- BOOST_FOREACH(const CTxOut& txout, vout)
+ BOOST_FOREACH(const CTxOut& txout, vout) {
if (!::IsStandard(txout.scriptPubKey))
return false;
+ if (txout.nValue == 0)
+ return false;
+ }
return true;
}
@@ -473,6 +477,55 @@ bool CTransaction::CheckTransaction() const
return true;
}
+int64 CTransaction::GetMinFee(unsigned int nBlockSize, bool fAllowFree,
+ enum GetMinFee_mode mode) const
+{
+ // Base fee is either MIN_TX_FEE or MIN_RELAY_TX_FEE
+ int64 nBaseFee = (mode == GMF_RELAY) ? MIN_RELAY_TX_FEE : MIN_TX_FEE;
+
+ unsigned int nBytes = ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
+ unsigned int nNewBlockSize = nBlockSize + nBytes;
+ int64 nMinFee = (1 + (int64)nBytes / 1000) * nBaseFee;
+
+ if (fAllowFree)
+ {
+ if (nBlockSize == 1)
+ {
+ // Transactions under 10K are free
+ // (about 4500 BTC if made of 50 BTC inputs)
+ if (nBytes < 10000)
+ nMinFee = 0;
+ }
+ else
+ {
+ // Free transaction area
+ if (nNewBlockSize < 27000)
+ nMinFee = 0;
+ }
+ }
+
+ // To limit dust spam, require MIN_TX_FEE/MIN_RELAY_TX_FEE if any output is less than 0.01
+ if (nMinFee < nBaseFee)
+ {
+ BOOST_FOREACH(const CTxOut& txout, vout)
+ if (txout.nValue < CENT)
+ nMinFee = nBaseFee;
+ }
+
+ // Raise the price as the block approaches full
+ if (nBlockSize != 1 && nNewBlockSize >= MAX_BLOCK_SIZE_GEN/2)
+ {
+ if (nNewBlockSize >= MAX_BLOCK_SIZE_GEN)
+ return MAX_MONEY;
+ nMinFee *= MAX_BLOCK_SIZE_GEN / (MAX_BLOCK_SIZE_GEN - nNewBlockSize);
+ }
+
+ if (!MoneyRange(nMinFee))
+ nMinFee = MAX_MONEY;
+ return nMinFee;
+}
+
+
bool CTxMemPool::accept(CTxDB& txdb, CTransaction &tx, bool fCheckInputs,
bool* pfMissingInputs)
{
@@ -559,12 +612,15 @@ bool CTxMemPool::accept(CTxDB& txdb, CTransaction &tx, bool fCheckInputs,
unsigned int nSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
// Don't accept it if it can't get into a block
- if (nFees < tx.GetMinFee(1000, true, GMF_RELAY))
- return error("CTxMemPool::accept() : not enough fees");
+ int64 txMinFee = tx.GetMinFee(1000, true, GMF_RELAY);
+ if (nFees < txMinFee)
+ return error("CTxMemPool::accept() : not enough fees %s, %"PRI64d" < %"PRI64d,
+ hash.ToString().c_str(),
+ nFees, txMinFee);
// Continuously rate-limit free transactions
// This mitigates 'penny-flooding' -- sending thousands of free transactions just to
- // be annoying or make other's transactions take longer to confirm.
+ // be annoying or make others' transactions take longer to confirm.
if (nFees < MIN_RELAY_TX_FEE)
{
static CCriticalSection cs;
@@ -653,6 +709,14 @@ bool CTxMemPool::remove(CTransaction &tx)
return true;
}
+void CTxMemPool::clear()
+{
+ LOCK(cs);
+ mapTx.clear();
+ mapNextTx.clear();
+ ++nTransactionsUpdated;
+}
+
void CTxMemPool::queryHashes(std::vector<uint256>& vtxid)
{
vtxid.clear();
@@ -802,6 +866,24 @@ bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock)
// CBlock and CBlockIndex
//
+static CBlockIndex* pblockindexFBBHLast;
+CBlockIndex* FindBlockByHeight(int nHeight)
+{
+ CBlockIndex *pblockindex;
+ if (nHeight < nBestHeight / 2)
+ pblockindex = pindexGenesisBlock;
+ else
+ pblockindex = pindexBest;
+ if (pblockindexFBBHLast && abs(nHeight - pblockindex->nHeight) > abs(nHeight - pblockindexFBBHLast->nHeight))
+ pblockindex = pblockindexFBBHLast;
+ while (pblockindex->nHeight > nHeight)
+ pblockindex = pblockindex->pprev;
+ while (pblockindex->nHeight < nHeight)
+ pblockindex = pblockindex->pnext;
+ pblockindexFBBHLast = pblockindex;
+ return pblockindex;
+}
+
bool CBlock::ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions)
{
if (!fReadTransactions)
@@ -979,7 +1061,7 @@ void static InvalidChainFound(CBlockIndex* pindexNew)
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainWork.ToString().c_str(),
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
if (pindexBest && bnBestInvalidWork > bnBestChainWork + pindexBest->GetBlockWork() * 6)
- printf("InvalidChainFound: WARNING: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.\n");
+ printf("InvalidChainFound: Warning: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.\n");
}
void CBlock::UpdateTime(const CBlockIndex* pindexPrev)
@@ -1030,7 +1112,7 @@ bool CTransaction::DisconnectInputs(CTxDB& txdb)
// Remove transaction from index
// This can fail if a duplicate of this transaction was in a chain that got
// reorganized away. This is only possible if this transaction was completely
- // spent, so erasing it would be a no-op anway.
+ // spent, so erasing it would be a no-op anyway.
txdb.EraseTxIndex(*this);
return true;
@@ -1093,7 +1175,7 @@ bool CTransaction::FetchInputs(CTxDB& txdb, const map<uint256, CTxIndex>& mapTes
}
}
- // Make sure all prevout.n's are valid:
+ // Make sure all prevout.n indexes are valid:
for (unsigned int i = 0; i < vin.size(); i++)
{
const COutPoint prevout = vin[i].prevout;
@@ -1316,10 +1398,10 @@ bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex)
return true;
}
-bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
+bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck)
{
// Check it again in case a previous version let a bad block in
- if (!CheckBlock())
+ if (!CheckBlock(!fJustCheck, !fJustCheck))
return false;
// Do not allow blocks that contain transactions which 'overwrite' older transactions,
@@ -1329,17 +1411,26 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
// being sent to another address.
// See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
// This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
- // already refuses previously-known transaction id's entirely.
- // This rule applies to all blocks whose timestamp is after March 15, 2012, 0:00 UTC.
- int64 nBIP30SwitchTime = 1331769600;
- bool fEnforceBIP30 = (pindex->nTime > nBIP30SwitchTime);
+ // already refuses previously-known transaction ids entirely.
+ // This rule was originally applied all blocks whose timestamp was after March 15, 2012, 0:00 UTC.
+ // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
+ // two in the chain that violate it. This prevents exploiting the issue against nodes in their
+ // initial block download.
+ bool fEnforceBIP30 = !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
+ (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
// BIP16 didn't become active until Apr 1 2012
int64 nBIP16SwitchTime = 1333238400;
bool fStrictPayToScriptHash = (pindex->nTime >= nBIP16SwitchTime);
//// issue here: it doesn't know the version
- unsigned int nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK, CLIENT_VERSION) - 1 + GetSizeOfCompactSize(vtx.size());
+ unsigned int nTxPos;
+ if (fJustCheck)
+ // FetchInputs treats CDiskTxPos(1,1,1) as a special "refer to memorypool" indicator
+ // Since we're just checking the block and not actually connecting it, it might not (and probably shouldn't) be on the disk to get the transaction from
+ nTxPos = 1;
+ else
+ nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK, CLIENT_VERSION) - 1 + GetSizeOfCompactSize(vtx.size());
map<uint256, CTxIndex> mapQueuedChanges;
int64 nFees = 0;
@@ -1362,7 +1453,8 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
return DoS(100, error("ConnectBlock() : too many sigops"));
CDiskTxPos posThisTx(pindex->nFile, pindex->nBlockPos, nTxPos);
- nTxPos += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
+ if (!fJustCheck)
+ nTxPos += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
MapPrevTx mapInputs;
if (!tx.IsCoinBase())
@@ -1390,6 +1482,12 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
mapQueuedChanges[hashTx] = CTxIndex(posThisTx, tx.vout.size());
}
+ if (vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
+ return false;
+
+ if (fJustCheck)
+ return true;
+
// Write queued txindex changes
for (map<uint256, CTxIndex>::iterator mi = mapQueuedChanges.begin(); mi != mapQueuedChanges.end(); ++mi)
{
@@ -1397,9 +1495,6 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
return error("ConnectBlock() : UpdateTxIndex failed");
}
- if (vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
- return false;
-
// Update block index on disk without changing it in memory.
// The memory index structure will be changed after the db commits.
if (pindex->pprev)
@@ -1585,7 +1680,7 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
return error("SetBestChain() : Reorganize failed");
}
- // Connect futher blocks
+ // Connect further blocks
BOOST_REVERSE_FOREACH(CBlockIndex *pindex, vpindexSecondary)
{
CBlock block;
@@ -1615,6 +1710,7 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
// New best block
hashBestChain = hash;
pindexBest = pindexNew;
+ pblockindexFBBHLast = NULL;
nBestHeight = pindexBest->nHeight;
bnBestChainWork = pindexNew->bnChainWork;
nTimeBestReceived = GetTime();
@@ -1638,7 +1734,7 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
printf("SetBestChain: %d of last 100 blocks above version %d\n", nUpgraded, CBlock::CURRENT_VERSION);
if (nUpgraded > 100/2)
// strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
- strMiscWarning = _("Warning: this version is obsolete, upgrade required");
+ strMiscWarning = _("Warning: This version is obsolete, upgrade required!");
}
std::string strCmd = GetArg("-blocknotify", "");
@@ -1703,7 +1799,7 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
-bool CBlock::CheckBlock() const
+bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot) const
{
// These are checks that are independent of context
// that can be verified before saving an orphan block.
@@ -1713,7 +1809,7 @@ bool CBlock::CheckBlock() const
return DoS(100, error("CheckBlock() : size limits failed"));
// Check proof of work matches claimed amount
- if (!CheckProofOfWork(GetHash(), nBits))
+ if (fCheckPOW && !CheckProofOfWork(GetHash(), nBits))
return DoS(50, error("CheckBlock() : proof of work failed"));
// Check timestamp
@@ -1750,8 +1846,8 @@ bool CBlock::CheckBlock() const
if (nSigOps > MAX_BLOCK_SIGOPS)
return DoS(100, error("CheckBlock() : out-of-bounds SigOpCount"));
- // Check merkleroot
- if (hashMerkleRoot != BuildMerkleTree())
+ // Check merkle root
+ if (fCheckMerkleRoot && hashMerkleRoot != BuildMerkleTree())
return DoS(100, error("CheckBlock() : hashMerkleRoot mismatch"));
return true;
@@ -1786,7 +1882,29 @@ bool CBlock::AcceptBlock()
// Check that the block chain matches the known block chain up to a checkpoint
if (!Checkpoints::CheckBlock(nHeight, hash))
- return DoS(100, error("AcceptBlock() : rejected by checkpoint lockin at %d", nHeight));
+ return DoS(100, error("AcceptBlock() : rejected by checkpoint lock-in at %d", nHeight));
+
+ // Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
+ if (nVersion < 2)
+ {
+ if ((!fTestNet && CBlockIndex::IsSuperMajority(2, pindexPrev, 950, 1000)) ||
+ (fTestNet && CBlockIndex::IsSuperMajority(2, pindexPrev, 75, 100)))
+ {
+ return error("AcceptBlock() : rejected nVersion=1 block");
+ }
+ }
+ // Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
+ if (nVersion >= 2)
+ {
+ // if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
+ if ((!fTestNet && CBlockIndex::IsSuperMajority(2, pindexPrev, 750, 1000)) ||
+ (fTestNet && CBlockIndex::IsSuperMajority(2, pindexPrev, 51, 100)))
+ {
+ CScript expect = CScript() << nHeight;
+ if (!std::equal(expect.begin(), expect.end(), vtx[0].vin[0].scriptSig.begin()))
+ return DoS(100, error("AcceptBlock() : block height mismatch in coinbase"));
+ }
+ }
// Write block to history file
if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK, CLIENT_VERSION)))
@@ -1811,6 +1929,18 @@ bool CBlock::AcceptBlock()
return true;
}
+bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired, unsigned int nToCheck)
+{
+ unsigned int nFound = 0;
+ for (unsigned int i = 0; i < nToCheck && nFound < nRequired && pstart != NULL; i++)
+ {
+ if (pstart->nVersion >= minVersion)
+ ++nFound;
+ pstart = pstart->pprev;
+ }
+ return (nFound >= nRequired);
+}
+
bool ProcessBlock(CNode* pfrom, CBlock* pblock)
{
// Check for duplicate
@@ -1848,17 +1978,20 @@ bool ProcessBlock(CNode* pfrom, CBlock* pblock)
}
- // If don't already have its previous block, shunt it off to holding area until we get it
+ // If we don't already have its previous block, shunt it off to holding area until we get it
if (!mapBlockIndex.count(pblock->hashPrevBlock))
{
printf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString().substr(0,20).c_str());
- CBlock* pblock2 = new CBlock(*pblock);
- mapOrphanBlocks.insert(make_pair(hash, pblock2));
- mapOrphanBlocksByPrev.insert(make_pair(pblock2->hashPrevBlock, pblock2));
- // Ask this guy to fill in what we're missing
- if (pfrom)
+ // Accept orphans as long as there is a node to request its parents from
+ if (pfrom) {
+ CBlock* pblock2 = new CBlock(*pblock);
+ mapOrphanBlocks.insert(make_pair(hash, pblock2));
+ mapOrphanBlocksByPrev.insert(make_pair(pblock2->hashPrevBlock, pblock2));
+
+ // Ask this guy to fill in what we're missing
pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(pblock2));
+ }
return true;
}
@@ -1904,7 +2037,7 @@ bool CheckDiskSpace(uint64 nAdditionalBytes)
if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
{
fShutdown = true;
- string strMessage = _("Warning: Disk space is low");
+ string strMessage = _("Warning: Disk space is low!");
strMiscWarning = strMessage;
printf("*** %s\n", strMessage.c_str());
uiInterface.ThreadSafeMessageBox(strMessage, "Bitcoin", CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION | CClientUIInterface::MODAL);
@@ -1914,11 +2047,17 @@ bool CheckDiskSpace(uint64 nAdditionalBytes)
return true;
}
+static filesystem::path BlockFilePath(unsigned int nFile)
+{
+ string strBlockFn = strprintf("blk%04u.dat", nFile);
+ return GetDataDir() / strBlockFn;
+}
+
FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode)
{
if ((nFile < 1) || (nFile == (unsigned int) -1))
return NULL;
- FILE* file = fopen((GetDataDir() / strprintf("blk%04d.dat", nFile)).string().c_str(), pszMode);
+ FILE* file = fopen(BlockFilePath(nFile).string().c_str(), pszMode);
if (!file)
return NULL;
if (nBlockPos != 0 && !strchr(pszMode, 'a') && !strchr(pszMode, 'w'))
@@ -1944,8 +2083,8 @@ FILE* AppendBlockFile(unsigned int& nFileRet)
return NULL;
if (fseek(file, 0, SEEK_END) != 0)
return NULL;
- // FAT32 filesize max 4GB, fseek and ftell max 2GB, so we must stay under 2GB
- if (ftell(file) < 0x7F000000 - MAX_SIZE)
+ // FAT32 file size max 4GB, fseek and ftell max 2GB, so we must stay under 2GB
+ if (ftell(file) < (long)(0x7F000000 - MAX_SIZE))
{
nFileRet = nCurrentBlockFile;
return file;
@@ -1959,10 +2098,10 @@ bool LoadBlockIndex(bool fAllowNew)
{
if (fTestNet)
{
- pchMessageStart[0] = 0xfa;
- pchMessageStart[1] = 0xbf;
- pchMessageStart[2] = 0xb5;
- pchMessageStart[3] = 0xda;
+ pchMessageStart[0] = 0x0b;
+ pchMessageStart[1] = 0x11;
+ pchMessageStart[2] = 0x09;
+ pchMessageStart[3] = 0x07;
hashGenesisBlock = uint256("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943");
}
@@ -2036,7 +2175,7 @@ bool LoadBlockIndex(bool fAllowNew)
void PrintBlockTree()
{
- // precompute tree structure
+ // pre-compute tree structure
map<CBlockIndex*, vector<CBlockIndex*> > mapNext;
for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
{
@@ -2089,7 +2228,7 @@ void PrintBlockTree()
PrintWallets(block);
- // put the main timechain first
+ // put the main time-chain first
vector<CBlockIndex*>& vNext = mapNext[pindex];
for (unsigned int i = 0; i < vNext.size(); i++)
{
@@ -2108,6 +2247,8 @@ void PrintBlockTree()
bool LoadExternalBlockFile(FILE* fileIn)
{
+ int64 nStart = GetTimeMillis();
+
int nLoaded = 0;
{
LOCK(cs_main);
@@ -2160,7 +2301,7 @@ bool LoadExternalBlockFile(FILE* fileIn)
__PRETTY_FUNCTION__);
}
}
- printf("Loaded %i blocks from external file\n", nLoaded);
+ printf("Loaded %i blocks from external file in %"PRI64d"ms\n", nLoaded, GetTimeMillis() - nStart);
return nLoaded > 0;
}
@@ -2177,8 +2318,8 @@ bool LoadExternalBlockFile(FILE* fileIn)
// CAlert
//
-map<uint256, CAlert> mapAlerts;
-CCriticalSection cs_mapAlerts;
+extern map<uint256, CAlert> mapAlerts;
+extern CCriticalSection cs_mapAlerts;
string GetWarnings(string strFor)
{
@@ -2199,7 +2340,7 @@ string GetWarnings(string strFor)
if (pindexBest && bnBestInvalidWork > bnBestChainWork + pindexBest->GetBlockWork() * 6)
{
nPriority = 2000;
- strStatusBar = strRPC = "WARNING: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.";
+ strStatusBar = strRPC = _("Warning: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.");
}
// Alerts
@@ -2224,69 +2365,6 @@ string GetWarnings(string strFor)
return "error";
}
-CAlert CAlert::getAlertByHash(const uint256 &hash)
-{
- CAlert retval;
- {
- LOCK(cs_mapAlerts);
- map<uint256, CAlert>::iterator mi = mapAlerts.find(hash);
- if(mi != mapAlerts.end())
- retval = mi->second;
- }
- return retval;
-}
-
-bool CAlert::ProcessAlert()
-{
- if (!CheckSignature())
- return false;
- if (!IsInEffect())
- return false;
-
- {
- LOCK(cs_mapAlerts);
- // Cancel previous alerts
- for (map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();)
- {
- const CAlert& alert = (*mi).second;
- if (Cancels(alert))
- {
- printf("cancelling alert %d\n", alert.nID);
- uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
- mapAlerts.erase(mi++);
- }
- else if (!alert.IsInEffect())
- {
- printf("expiring alert %d\n", alert.nID);
- uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
- mapAlerts.erase(mi++);
- }
- else
- mi++;
- }
-
- // Check if this alert has been cancelled
- BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
- {
- const CAlert& alert = item.second;
- if (alert.Cancels(*this))
- {
- printf("alert already cancelled by %d\n", alert.nID);
- return false;
- }
- }
-
- // Add to mapAlerts
- mapAlerts.insert(make_pair(GetHash(), *this));
- // Notify UI if it applies to me
- if(AppliesToMe())
- uiInterface.NotifyAlertChanged(GetHash(), CT_NEW);
- }
-
- printf("accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe());
- return true;
-}
-
@@ -2328,7 +2406,7 @@ bool static AlreadyHave(CTxDB& txdb, const CInv& inv)
// The message start string is designed to be unlikely to occur in normal data.
-// The characters are rarely used upper ascii, not valid as UTF-8, and produce
+// The characters are rarely used upper ASCII, not valid as UTF-8, and produce
// a large 4-byte int at any alignment.
unsigned char pchMessageStart[4] = { 0xf9, 0xbe, 0xb4, 0xd9 };
@@ -2543,7 +2621,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
{
vector<CInv> vInv;
vRecv >> vInv;
- if (vInv.size() > 50000)
+ if (vInv.size() > MAX_INV_SZ)
{
pfrom->Misbehaving(20);
return error("message inv size() = %d", vInv.size());
@@ -2578,7 +2656,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
// In case we are on a very long side-chain, it is possible that we already have
// the last block in an inv bundle sent in response to getblocks. Try to detect
// this situation and push another getblocks to continue.
- std::vector<CInv> vGetData(1,inv);
pfrom->PushGetBlocks(mapBlockIndex[inv.hash], uint256(0));
if (fDebug)
printf("force request: %s\n", inv.ToString().c_str());
@@ -2594,7 +2671,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
{
vector<CInv> vInv;
vRecv >> vInv;
- if (vInv.size() > 50000)
+ if (vInv.size() > MAX_INV_SZ)
{
pfrom->Misbehaving(20);
return error("message getdata size() = %d", vInv.size());
@@ -2636,11 +2713,24 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
else if (inv.IsKnownType())
{
// Send stream from relay memory
+ bool pushed = false;
{
LOCK(cs_mapRelay);
map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
- if (mi != mapRelay.end())
+ if (mi != mapRelay.end()) {
pfrom->PushMessage(inv.GetCommand(), (*mi).second);
+ pushed = true;
+ }
+ }
+ if (!pushed && inv.type == MSG_TX) {
+ LOCK(mempool.cs);
+ if (mempool.exists(inv.hash)) {
+ CTransaction tx = mempool.lookup(inv.hash);
+ CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
+ ss.reserve(1000);
+ ss << tx;
+ pfrom->PushMessage("tx", ss);
+ }
}
}
@@ -2815,6 +2905,22 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
}
+ else if (strCommand == "mempool")
+ {
+ std::vector<uint256> vtxid;
+ mempool.queryHashes(vtxid);
+ vector<CInv> vInv;
+ for (unsigned int i = 0; i < vtxid.size(); i++) {
+ CInv inv(MSG_TX, vtxid[i]);
+ vInv.push_back(inv);
+ if (i == (MAX_INV_SZ - 1))
+ break;
+ }
+ if (vInv.size() > 0)
+ pfrom->PushMessage("inv", vInv);
+ }
+
+
else if (strCommand == "checkorder")
{
uint256 hashReply;
@@ -2889,14 +2995,27 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
CAlert alert;
vRecv >> alert;
- if (alert.ProcessAlert())
+ uint256 alertHash = alert.GetHash();
+ if (pfrom->setKnown.count(alertHash) == 0)
{
- // Relay
- pfrom->setKnown.insert(alert.GetHash());
+ if (alert.ProcessAlert())
{
- LOCK(cs_vNodes);
- BOOST_FOREACH(CNode* pnode, vNodes)
- alert.RelayTo(pnode);
+ // Relay
+ pfrom->setKnown.insert(alertHash);
+ {
+ LOCK(cs_vNodes);
+ BOOST_FOREACH(CNode* pnode, vNodes)
+ alert.RelayTo(pnode);
+ }
+ }
+ else {
+ // Small DoS penalty so peers that send us lots of
+ // duplicate/expired/invalid-signature/whatever alerts
+ // eventually get banned.
+ // This isn't a Misbehaving(100) (immediate ban) because the
+ // peer might be an older or different implementation with
+ // a different signature key, etc.
+ pfrom->Misbehaving(10);
}
}
}
@@ -3011,12 +3130,12 @@ bool ProcessMessages(CNode* pfrom)
{
if (strstr(e.what(), "end of data"))
{
- // Allow exceptions from underlength message on vRecv
+ // Allow exceptions from under-length message on vRecv
printf("ProcessMessages(%s, %u bytes) : Exception '%s' caught, normally caused by a message being shorter than its stated length\n", strCommand.c_str(), nMessageSize, e.what());
}
else if (strstr(e.what(), "size too large"))
{
- // Allow exceptions from overlong size
+ // Allow exceptions from over-long size
printf("ProcessMessages(%s, %u bytes) : Exception '%s' caught\n", strCommand.c_str(), nMessageSize, e.what());
}
else
@@ -3265,9 +3384,9 @@ unsigned int static ScanHash_CryptoPP(char* pmidstate, char* pdata, char* phash1
unsigned int& nNonce = *(unsigned int*)(pdata + 12);
for (;;)
{
- // Crypto++ SHA-256
+ // Crypto++ SHA256
// Hash pdata using pmidstate as the starting state into
- // preformatted buffer phash1, then hash phash1 into phash
+ // pre-formatted buffer phash1, then hash phash1 into phash
nNonce++;
SHA256Transform(phash1, pdata, pmidstate);
SHA256Transform(phash, phash1, pSHA256InitState);
@@ -3293,16 +3412,18 @@ public:
CTransaction* ptx;
set<uint256> setDependsOn;
double dPriority;
+ double dFeePerKb;
COrphan(CTransaction* ptxIn)
{
ptx = ptxIn;
- dPriority = 0;
+ dPriority = dFeePerKb = 0;
}
void print() const
{
- printf("COrphan(hash=%s, dPriority=%.1f)\n", ptx->GetHash().ToString().substr(0,10).c_str(), dPriority);
+ printf("COrphan(hash=%s, dPriority=%.1f, dFeePerKb=%.1f)\n",
+ ptx->GetHash().ToString().substr(0,10).c_str(), dPriority, dFeePerKb);
BOOST_FOREACH(uint256 hash, setDependsOn)
printf(" setDependsOn %s\n", hash.ToString().substr(0,10).c_str());
}
@@ -3312,6 +3433,33 @@ public:
uint64 nLastBlockTx = 0;
uint64 nLastBlockSize = 0;
+// We want to sort transactions by priority and fee, so:
+typedef boost::tuple<double, double, CTransaction*> TxPriority;
+class TxPriorityCompare
+{
+ bool byFee;
+public:
+ TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
+ bool operator()(const TxPriority& a, const TxPriority& b)
+ {
+ if (byFee)
+ {
+ if (a.get<1>() == b.get<1>())
+ return a.get<0>() < b.get<0>();
+ return a.get<1>() < b.get<1>();
+ }
+ else
+ {
+ if (a.get<0>() == b.get<0>())
+ return a.get<1>() < b.get<1>();
+ return a.get<0>() < b.get<0>();
+ }
+ }
+};
+
+const char* pszDummy = "\0\0";
+CScript scriptDummy(std::vector<unsigned char>(pszDummy, pszDummy + sizeof(pszDummy)));
+
CBlock* CreateNewBlock(CReserveKey& reservekey)
{
CBlockIndex* pindexPrev = pindexBest;
@@ -3331,6 +3479,30 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
// Add our coinbase tx as first transaction
pblock->vtx.push_back(txNew);
+ // Largest block you're willing to create:
+ unsigned int nBlockMaxSize = GetArg("-blockmaxsize", MAX_BLOCK_SIZE_GEN/2);
+ // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
+ nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));
+
+ // How much of the block should be dedicated to high-priority transactions,
+ // included regardless of the fees they pay
+ unsigned int nBlockPrioritySize = GetArg("-blockprioritysize", 27000);
+ nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);
+
+ // Minimum block size you want to create; block will be filled with free transactions
+ // until there are no more or the block reaches this size:
+ unsigned int nBlockMinSize = GetArg("-blockminsize", 0);
+ nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize);
+
+ // Fee-per-kilobyte amount considered the same as "free"
+ // Be careful setting this: if you set it to zero then
+ // a transaction spammer can cheaply fill blocks using
+ // 1-satoshi-fee transactions. It should be set above the real
+ // cost to you of processing a transaction.
+ int64 nMinTxFee = MIN_TX_FEE;
+ if (mapArgs.count("-mintxfee"))
+ ParseMoney(mapArgs["-mintxfee"], nMinTxFee);
+
// Collect memory pool transactions into the block
int64 nFees = 0;
{
@@ -3340,7 +3512,10 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
// Priority order to process transactions
list<COrphan> vOrphan; // list memory doesn't move
map<uint256, vector<COrphan*> > mapDependers;
- multimap<double, CTransaction*> mapPriority;
+
+ // This vector will be sorted into a priority queue:
+ vector<TxPriority> vecPriority;
+ vecPriority.reserve(mempool.mapTx.size());
for (map<uint256, CTransaction>::iterator mi = mempool.mapTx.begin(); mi != mempool.mapTx.end(); ++mi)
{
CTransaction& tx = (*mi).second;
@@ -3349,6 +3524,8 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
COrphan* porphan = NULL;
double dPriority = 0;
+ int64 nTotalIn = 0;
+ bool fMissingInputs = false;
BOOST_FOREACH(const CTxIn& txin, tx.vin)
{
// Read prev transaction
@@ -3356,6 +3533,19 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
CTxIndex txindex;
if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
{
+ // This should never happen; all transactions in the memory
+ // pool should connect to either transactions in the chain
+ // or other transactions in the memory pool.
+ if (!mempool.mapTx.count(txin.prevout.hash))
+ {
+ printf("ERROR: mempool transaction missing input\n");
+ if (fDebug) assert("mempool transaction missing input" == 0);
+ fMissingInputs = true;
+ if (porphan)
+ vOrphan.pop_back();
+ break;
+ }
+
// Has to wait for dependencies
if (!porphan)
{
@@ -3365,34 +3555,33 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
}
mapDependers[txin.prevout.hash].push_back(porphan);
porphan->setDependsOn.insert(txin.prevout.hash);
+ nTotalIn += mempool.mapTx[txin.prevout.hash].vout[txin.prevout.n].nValue;
continue;
}
int64 nValueIn = txPrev.vout[txin.prevout.n].nValue;
+ nTotalIn += nValueIn;
- // Read block header
int nConf = txindex.GetDepthInMainChain();
-
dPriority += (double)nValueIn * nConf;
-
- if (fDebug && GetBoolArg("-printpriority"))
- printf("priority nValueIn=%-12"PRI64d" nConf=%-5d dPriority=%-20.1f\n", nValueIn, nConf, dPriority);
}
+ if (fMissingInputs) continue;
// Priority is sum(valuein * age) / txsize
- dPriority /= ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
+ unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
+ dPriority /= nTxSize;
- if (porphan)
- porphan->dPriority = dPriority;
- else
- mapPriority.insert(make_pair(-dPriority, &(*mi).second));
+ // This is a more accurate fee-per-kilobyte than is used by the client code, because the
+ // client code rounds up the size to the nearest 1K. That's good, because it gives an
+ // incentive to create smaller transactions.
+ double dFeePerKb = double(nTotalIn-tx.GetValueOut()) / (double(nTxSize)/1000.0);
- if (fDebug && GetBoolArg("-printpriority"))
+ if (porphan)
{
- printf("priority %-20.1f %s\n%s", dPriority, tx.GetHash().ToString().substr(0,10).c_str(), tx.ToString().c_str());
- if (porphan)
- porphan->print();
- printf("\n");
+ porphan->dPriority = dPriority;
+ porphan->dFeePerKb = dFeePerKb;
}
+ else
+ vecPriority.push_back(TxPriority(dPriority, dFeePerKb, &(*mi).second));
}
// Collect transactions into block
@@ -3400,16 +3589,24 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
uint64 nBlockSize = 1000;
uint64 nBlockTx = 0;
int nBlockSigOps = 100;
- while (!mapPriority.empty())
+ bool fSortedByFee = (nBlockPrioritySize <= 0);
+
+ TxPriorityCompare comparer(fSortedByFee);
+ std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
+
+ while (!vecPriority.empty())
{
- // Take highest priority transaction off priority queue
- double dPriority = -(*mapPriority.begin()).first;
- CTransaction& tx = *(*mapPriority.begin()).second;
- mapPriority.erase(mapPriority.begin());
+ // Take highest priority transaction off the priority queue:
+ double dPriority = vecPriority.front().get<0>();
+ double dFeePerKb = vecPriority.front().get<1>();
+ CTransaction& tx = *(vecPriority.front().get<2>());
+
+ std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
+ vecPriority.pop_back();
// Size limits
unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
- if (nBlockSize + nTxSize >= MAX_BLOCK_SIZE_GEN)
+ if (nBlockSize + nTxSize >= nBlockMaxSize)
continue;
// Legacy limits on sigOps:
@@ -3417,9 +3614,19 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
continue;
- // Transaction fee required depends on block size
- bool fAllowFree = (nBlockSize + nTxSize < 4000 || CTransaction::AllowFree(dPriority));
- int64 nMinFee = tx.GetMinFee(nBlockSize, fAllowFree, GMF_BLOCK);
+ // Skip free transactions if we're past the minimum block size:
+ if (fSortedByFee && (dFeePerKb < nMinTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
+ continue;
+
+ // Prioritize by fee once past the priority size or we run out of high-priority
+ // transactions:
+ if (!fSortedByFee &&
+ ((nBlockSize + nTxSize >= nBlockPrioritySize) || (dPriority < COIN * 144 / 250)))
+ {
+ fSortedByFee = true;
+ comparer = TxPriorityCompare(fSortedByFee);
+ std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
+ }
// Connecting shouldn't fail due to dependency on other memory pool transactions
// because we're already processing them in order of dependency
@@ -3430,8 +3637,6 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
continue;
int64 nTxFees = tx.GetValueIn(mapInputs)-tx.GetValueOut();
- if (nTxFees < nMinFee)
- continue;
nTxSigOps += tx.GetP2SHSigOpCount(mapInputs);
if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
@@ -3449,6 +3654,12 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
nBlockSigOps += nTxSigOps;
nFees += nTxFees;
+ if (fDebug && GetBoolArg("-printpriority"))
+ {
+ printf("priority %.1f feeperkb %.1f txid %s\n",
+ dPriority, dFeePerKb, tx.GetHash().ToString().c_str());
+ }
+
// Add transactions that depend on this one to the priority queue
uint256 hash = tx.GetHash();
if (mapDependers.count(hash))
@@ -3459,7 +3670,10 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
{
porphan->setDependsOn.erase(hash);
if (porphan->setDependsOn.empty())
- mapPriority.insert(make_pair(-porphan->dPriority, porphan->ptx));
+ {
+ vecPriority.push_back(TxPriority(porphan->dPriority, porphan->dFeePerKb, porphan->ptx));
+ std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
+ }
}
}
}
@@ -3469,16 +3683,22 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
nLastBlockSize = nBlockSize;
printf("CreateNewBlock(): total size %lu\n", nBlockSize);
- }
pblock->vtx[0].vout[0].nValue = GetBlockValue(pindexPrev->nHeight+1, nFees);
// Fill in header
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
- pblock->hashMerkleRoot = pblock->BuildMerkleTree();
pblock->UpdateTime(pindexPrev);
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock.get());
pblock->nNonce = 0;
+ pblock->vtx[0].vin[0].scriptSig = scriptDummy;
+ CBlockIndex indexDummy(1, 1, *pblock);
+ indexDummy.pprev = pindexPrev;
+ indexDummy.nHeight = pindexPrev->nHeight + 1;
+ if (!pblock->ConnectBlock(txdb, &indexDummy, true))
+ throw std::runtime_error("CreateNewBlock() : ConnectBlock failed");
+ }
+
return pblock.release();
}
@@ -3493,7 +3713,8 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int&
hashPrevBlock = pblock->hashPrevBlock;
}
++nExtraNonce;
- pblock->vtx[0].vin[0].scriptSig = (CScript() << pblock->nTime << CBigNum(nExtraNonce)) + COINBASE_FLAGS;
+ unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
+ pblock->vtx[0].vin[0].scriptSig = (CScript() << nHeight << CBigNum(nExtraNonce)) + COINBASE_FLAGS;
assert(pblock->vtx[0].vin[0].scriptSig.size() <= 100);
pblock->hashMerkleRoot = pblock->BuildMerkleTree();
@@ -3503,7 +3724,7 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int&
void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1)
{
//
- // Prebuild hash buffers
+ // Pre-build hash buffers
//
struct
{
@@ -3626,11 +3847,12 @@ void static BitcoinMiner(CWallet *pwallet)
return;
IncrementExtraNonce(pblock.get(), pindexPrev, nExtraNonce);
- printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size());
+ printf("Running BitcoinMiner with %d transactions in block (%u bytes)\n", pblock->vtx.size(),
+ ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
//
- // Prebuild hash buffers
+ // Pre-build hash buffers
//
char pmidstatebuf[32+16]; char* pmidstate = alignup<16>(pmidstatebuf);
char pdatabuf[128+16]; char* pdata = alignup<16>(pdatabuf);
@@ -3655,7 +3877,7 @@ void static BitcoinMiner(CWallet *pwallet)
unsigned int nHashesDone = 0;
unsigned int nNonceFound;
- // Crypto++ SHA-256
+ // Crypto++ SHA256
nNonceFound = ScanHash_CryptoPP(pmidstate, pdata + 64, phash1,
(char*)&hash, nHashesDone);
@@ -3779,8 +4001,8 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet)
printf("Starting %d BitcoinMiner threads\n", nAddThreads);
for (int i = 0; i < nAddThreads; i++)
{
- if (!CreateThread(ThreadBitcoinMiner, pwallet))
- printf("Error: CreateThread(ThreadBitcoinMiner) failed\n");
+ if (!NewThread(ThreadBitcoinMiner, pwallet))
+ printf("Error: NewThread(ThreadBitcoinMiner) failed\n");
Sleep(10);
}
}