INCLUDEPATH += src/leveldb/include src/leveldb/helpers
LIBS += $$PWD/src/leveldb/libleveldb.a $$PWD/src/leveldb/libmemenv.a
-SOURCES += src/txdb-leveldb.cpp
!win32 {
# we use QMAKE_CXXFLAGS_RELEASE even without RELEASE=1 because we use RELEASE to indicate linking preferences not -O preferences
genleveldb.commands = cd $$PWD/src/leveldb && CC=$$QMAKE_CC CXX=$$QMAKE_CXX $(MAKE) OPT=\"$$QMAKE_CXXFLAGS $$QMAKE_CXXFLAGS_RELEASE\" libleveldb.a libmemenv.a
src/net.h \
src/key.h \
src/db.h \
- src/txdb.h \
src/walletdb.h \
src/script.h \
src/init.h \
src/checkpoints.cpp \
src/addrman.cpp \
src/db.cpp \
+ src/leveldb.cpp \
src/walletdb.cpp \
src/qt/clientmodel.cpp \
src/qt/guiutil.cpp \
#include "checkpoints.h"
-#include "txdb.h"
+#include "db.h"
#include "main.h"
#include "uint256.h"
static MapCheckpoints mapCheckpoints =
boost::assign::map_list_of
( 0, std::make_pair(hashGenesisBlock, 1360105017) )
- ( 9690, std::make_pair(uint256("0x00000000026561450859c46868099e0df6068a538f038cb18988fd8d47dcdaf5"), 1362791423) )
+/* ( 9690, std::make_pair(uint256("0x00000000026561450859c46868099e0df6068a538f038cb18988fd8d47dcdaf5"), 1362791423) )
( 13560, std::make_pair(uint256("0xa1591a0fcbf11f282d671581edb9f0aadcd06fee69761081e0a3245914c13729"), 1364674052) )
( 37092, std::make_pair(uint256("0x0000000000a38c2f98556f46793b453e92d8fab2d31c0b93fd08bcf78e56099d"), 1376677203) )
( 44200, std::make_pair(uint256("0xc9bda7232a18b9c1f5ff974a9e5566b2d1879ceb8fc0e9e61fba9038a25b8447"), 1380145962) )
( 65000, std::make_pair(uint256("0xfb2b51a2fd65062c98a7a6053cde46aeaefebb95ba2f680e85a29ee25b1dcf05"), 1388526385) )
- ;
+*/ ;
// TestNet has no checkpoints
static MapCheckpoints mapCheckpointsTestnet =
bool WriteSyncCheckpoint(const uint256& hashCheckpoint)
{
- CTxDB txdb;
- txdb.TxnBegin();
- if (!txdb.WriteSyncCheckpoint(hashCheckpoint))
+ CChainDB chaindb;
+ chaindb.TxnBegin();
+ if (!chaindb.WriteSyncCheckpoint(hashCheckpoint))
{
- txdb.TxnAbort();
+ chaindb.TxnAbort();
return error("WriteSyncCheckpoint(): failed to write to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
}
- if (!txdb.TxnCommit())
+ if (!chaindb.TxnCommit())
return error("WriteSyncCheckpoint(): failed to commit to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
Checkpoints::hashSyncCheckpoint = hashCheckpoint;
return false;
}
- CTxDB txdb;
+ CChainDB chaindb;
CBlockIndex* pindexCheckpoint = mapBlockIndex[hashPendingCheckpoint];
if (!pindexCheckpoint->IsInMainChain())
{
CBlock block;
if (!block.ReadFromDisk(pindexCheckpoint))
return error("AcceptPendingSyncCheckpoint: ReadFromDisk failed for sync checkpoint %s", hashPendingCheckpoint.ToString().c_str());
- if (!block.SetBestChain(txdb, pindexCheckpoint))
+ if (!block.SetBestChain(pindexCheckpoint))
{
hashInvalidCheckpoint = hashPendingCheckpoint;
return error("AcceptPendingSyncCheckpoint: SetBestChain failed for sync checkpoint %s", hashPendingCheckpoint.ToString().c_str());
{
// checkpoint block accepted but not yet in main chain
printf("ResetSyncCheckpoint: SetBestChain to hardened checkpoint %s\n", hash.ToString().c_str());
- CTxDB txdb;
+ CChainDB chaindb;
CBlock block;
if (!block.ReadFromDisk(mapBlockIndex[hash]))
return error("ResetSyncCheckpoint: ReadFromDisk failed for hardened checkpoint %s", hash.ToString().c_str());
- if (!block.SetBestChain(txdb, mapBlockIndex[hash]))
+ if (!block.SetBestChain(mapBlockIndex[hash]))
{
return error("ResetSyncCheckpoint: SetBestChain failed for hardened checkpoint %s", hash.ToString().c_str());
}
if (!Checkpoints::ValidateSyncCheckpoint(hashCheckpoint))
return false;
- CTxDB txdb;
+ CChainDB chaindb;
CBlockIndex* pindexCheckpoint = mapBlockIndex[hashCheckpoint];
if (!pindexCheckpoint->IsInMainChain())
{
CBlock block;
if (!block.ReadFromDisk(pindexCheckpoint))
return error("ProcessSyncCheckpoint: ReadFromDisk failed for sync checkpoint %s", hashCheckpoint.ToString().c_str());
- if (!block.SetBestChain(txdb, pindexCheckpoint))
+ if (!block.SetBestChain(pindexCheckpoint))
{
Checkpoints::hashInvalidCheckpoint = hashCheckpoint;
return error("ProcessSyncCheckpoint: SetBestChain failed for sync checkpoint %s", hashCheckpoint.ToString().c_str());
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "db.h"
-#include "net.h"
+#include "kernel.h"
+#include "checkpoints.h"
#include "util.h"
#include "main.h"
-#include "ui_interface.h"
+#include <boost/version.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
unsigned int nWalletDBUpdated;
+
+
//
// CDB
//
if (ret != 0)
printf("EnvShutdown exception: %s (%d)\n", DbEnv::strerror(ret), ret);
if (!fMockDb)
- DbEnv(0).remove(strPath.c_str(), 0);
+ DbEnv(0).remove(GetDataDir().string().c_str(), 0);
}
CDBEnv::CDBEnv() : dbenv(DB_CXX_NO_EXCEPTIONS)
{
- fDbEnvInit = false;
- fMockDb = false;
}
CDBEnv::~CDBEnv()
pathEnv = pathEnv_;
filesystem::path pathDataDir = pathEnv;
- strPath = pathDataDir.string();
filesystem::path pathLogDir = pathDataDir / "database";
filesystem::create_directory(pathLogDir);
filesystem::path pathErrorFile = pathDataDir / "db.log";
dbenv.set_cachesize(nDbCache / 1024, (nDbCache % 1024)*1048576, 1);
dbenv.set_lg_bsize(1048576);
dbenv.set_lg_max(10485760);
-
- // Bugfix: Bump lk_max_locks default to 537000, to safely handle reorgs with up to 5 blocks reversed
- // dbenv.set_lk_max_locks(10000);
- dbenv.set_lk_max_locks(537000);
-
+ dbenv.set_lk_max_locks(10000);
dbenv.set_lk_max_objects(10000);
dbenv.set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug
dbenv.set_flags(DB_AUTO_COMMIT, 1);
#ifdef DB_LOG_AUTO_REMOVE
dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1);
#endif
- int ret = dbenv.open(strPath.c_str(),
+ int ret = dbenv.open(pathDataDir.string().c_str(),
DB_CREATE |
DB_INIT_LOCK |
DB_INIT_LOG |
fDbEnvInit = true;
fMockDb = false;
-
return true;
}
ret = pdb->open(NULL, // Txn pointer
fMockDb ? NULL : pszFile, // Filename
- "main", // Logical db name
+ fMockDb ? pszFile : "main", // Logical db name
DB_BTREE, // Database type
nFlags, // Flags
0);
static bool IsChainFile(std::string strFile)
{
- if (strFile == "blkindex.dat")
+ if (strFile == "coins.dat" || strFile == "chain.dat")
return true;
return false;
}
+
+
+
+
+//
+// CChainDB and CCoinsDB
+//
+
+bool CCoinsDB::HaveCoins(uint256 hash) {
+ assert(!fClient);
+ return Exists(make_pair('c', hash));
+}
+
+bool CCoinsDB::ReadCoins(uint256 hash, CCoins &coins) {
+ assert(!fClient);
+ return Read(make_pair('c', hash), coins);
+}
+
+bool CCoinsDB::WriteCoins(uint256 hash, const CCoins &coins) {
+ assert(!fClient);
+ if (coins.IsPruned())
+ return Erase(make_pair('c', hash));
+ else
+ return Write(make_pair('c', hash), coins);
+}
+
+bool CChainDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
+{
+ return Write(make_pair('b', blockindex.GetBlockHash()), blockindex);
+}
+
+bool CCoinsDB::ReadHashBestChain(uint256& hashBestChain)
+{
+ return Read('B', hashBestChain);
+}
+
+bool CCoinsDB::WriteHashBestChain(uint256 hashBestChain)
+{
+ return Write('B', hashBestChain);
+}
+
+bool CChainDB::ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust)
+{
+ return Read('I', bnBestInvalidTrust);
+}
+
+bool CChainDB::WriteBestInvalidTrust(CBigNum bnBestInvalidTrust)
+{
+ return Write('I', bnBestInvalidTrust);
+}
+
+bool CChainDB::WriteBlockFileInfo(int nFile, const CBlockFileInfo &info) {
+ return Write(make_pair('f', nFile), info);
+}
+
+bool CChainDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
+ return Read(make_pair('f', nFile), info);
+}
+
+bool CChainDB::WriteLastBlockFile(int nFile) {
+ return Write('l', nFile);
+}
+
+bool CChainDB::ReadLastBlockFile(int &nFile) {
+ return Read('l', nFile);
+}
+
+bool CChainDB::ReadSyncCheckpoint(uint256& hashCheckpoint)
+{
+ return Read('H', hashCheckpoint);
+}
+
+bool CChainDB::WriteSyncCheckpoint(uint256 hashCheckpoint)
+{
+ return Write('H', hashCheckpoint);
+}
+
+bool CChainDB::ReadCheckpointPubKey(string& strPubKey)
+{
+ return Read('K', strPubKey);
+}
+
+bool CChainDB::WriteCheckpointPubKey(const string& strPubKey)
+{
+ return Write('K', strPubKey);
+}
+
+CBlockIndex static * InsertBlockIndex(uint256 hash)
+{
+ if (hash == 0)
+ return NULL;
+
+ // Return existing
+ map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
+ if (mi != mapBlockIndex.end())
+ return (*mi).second;
+
+ // Create new
+ CBlockIndex* pindexNew = new CBlockIndex();
+ if (!pindexNew)
+ throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
+ mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
+ pindexNew->phashBlock = &((*mi).first);
+
+ return pindexNew;
+}
+
+bool LoadBlockIndex(CCoinsDB &coindb, CChainDB &chaindb)
+{
+ if (!chaindb.LoadBlockIndexGuts())
+ return false;
+
+ if (fRequestShutdown)
+ return true;
+
+ // Calculate nChainTrust
+ vector<pair<int, CBlockIndex*> > vSortedByHeight;
+ vSortedByHeight.reserve(mapBlockIndex.size());
+ BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
+ {
+ CBlockIndex* pindex = item.second;
+ vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
+ }
+ sort(vSortedByHeight.begin(), vSortedByHeight.end());
+ BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
+ {
+ CBlockIndex* pindex = item.second;
+ pindex->nChainTrust = (pindex->pprev ? pindex->pprev->nChainTrust : 0) + pindex->GetBlockTrust();
+ // NovaCoin: calculate stake modifier checksum
+ pindex->nStakeModifierChecksum = GetStakeModifierChecksum(pindex);
+ if (!CheckStakeModifierCheckpoints(pindex->nHeight, pindex->nStakeModifierChecksum))
+ return error("CTxDB::LoadBlockIndex() : Failed stake modifier checkpoint height=%d, modifier=0x%016"PRI64x, pindex->nHeight, pindex->nStakeModifier);
+ }
+
+ // Load block file info
+ chaindb.ReadLastBlockFile(nLastBlockFile);
+ printf("LoadBlockIndex(): last block file = %i\n", nLastBlockFile);
+ if (chaindb.ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
+ printf("LoadBlockIndex(): last block file: %s\n", infoLastBlockFile.ToString().c_str());
+
+ // Load hashBestChain pointer to end of best chain
+ if (!coindb.ReadHashBestChain(hashBestChain))
+ {
+ if (pindexGenesisBlock == NULL)
+ return true;
+ return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
+ }
+ std::map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(hashBestChain);
+ if (it == mapBlockIndex.end()) {
+ return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
+ } else {
+ // set 'next' pointers in best chain
+ CBlockIndex *pindex = it->second;
+ while(pindex != NULL && pindex->pprev != NULL) {
+ CBlockIndex *pindexPrev = pindex->pprev;
+ pindexPrev->pnext = pindex;
+ pindex = pindexPrev;
+ }
+ pindexBest = it->second;
+ nBestHeight = pindexBest->nHeight;
+ nBestChainTrust = pindexBest->nChainTrust;
+ }
+ printf("LoadBlockIndex(): hashBestChain=%s height=%d date=%s\n",
+ hashBestChain.ToString().substr(0,20).c_str(), nBestHeight,
+ DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
+
+ // Load sync-checkpoint
+ if (!chaindb.ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint))
+ return error("CTxDB::LoadBlockIndex() : hashSyncCheckpoint not loaded");
+ printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());
+
+ // Load bnBestInvalidTrust, OK if it doesn't exist
+ CBigNum bnBestInvalidTrust;
+ chaindb.ReadBestInvalidTrust(bnBestInvalidTrust);
+ nBestInvalidTrust = bnBestInvalidTrust.getuint256();
+
+ // Verify blocks in the best chain
+ int nCheckLevel = GetArg("-checklevel", 1);
+ int nCheckDepth = GetArg( "-checkblocks", 2500);
+ if (nCheckDepth == 0)
+ nCheckDepth = 1000000000; // suffices until the year 19000
+ if (nCheckDepth > nBestHeight)
+ nCheckDepth = nBestHeight;
+ printf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
+ CBlockIndex* pindexFork = NULL;
+ for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
+ {
+ if (fRequestShutdown || pindex->nHeight < nBestHeight-nCheckDepth)
+ break;
+ CBlock block;
+ if (!block.ReadFromDisk(pindex))
+ return error("LoadBlockIndex() : block.ReadFromDisk failed");
+ // check level 1: verify block validity
+ if (nCheckLevel>0 && !block.CheckBlock())
+ {
+ printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
+ pindexFork = pindex->pprev;
+ }
+ // TODO: stronger verifications
+ }
+ if (pindexFork && !fRequestShutdown)
+ {
+ // TODO: reorg back
+ return error("LoadBlockIndex(): chain database corrupted");
+ }
+
+ return true;
+}
+
+
+
+bool CChainDB::LoadBlockIndexGuts()
+{
+ // Get database cursor
+ Dbc* pcursor = GetCursor();
+ if (!pcursor)
+ return false;
+
+ // Load mapBlockIndex
+ unsigned int fFlags = DB_SET_RANGE;
+ while (true)
+ {
+ // Read next record
+ CDataStream ssKey(SER_DISK, CLIENT_VERSION);
+ if (fFlags == DB_SET_RANGE)
+ ssKey << make_pair('b', uint256(0));
+ CDataStream ssValue(SER_DISK, CLIENT_VERSION);
+ int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
+ fFlags = DB_NEXT;
+ if (ret == DB_NOTFOUND)
+ break;
+ else if (ret != 0)
+ return false;
+
+ // Unserialize
+
+ try {
+ char chType;
+ ssKey >> chType;
+ if (chType == 'b' && !fRequestShutdown)
+ {
+ CDiskBlockIndex diskindex;
+ ssValue >> diskindex;
+
+ uint256 blockHash = diskindex.GetBlockHash();
+
+ // Construct block index object
+ CBlockIndex* pindexNew = InsertBlockIndex(blockHash);
+ pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
+ pindexNew->nHeight = diskindex.nHeight;
+ pindexNew->pos = diskindex.pos;
+ pindexNew->nUndoPos = diskindex.nUndoPos;
+ pindexNew->nMint = diskindex.nMint;
+ pindexNew->nMoneySupply = diskindex.nMoneySupply;
+ pindexNew->nFlags = diskindex.nFlags;
+ pindexNew->nStakeModifier = diskindex.nStakeModifier;
+ pindexNew->prevoutStake = diskindex.prevoutStake;
+ pindexNew->nStakeTime = diskindex.nStakeTime;
+ pindexNew->hashProofOfStake = diskindex.hashProofOfStake;
+ pindexNew->nVersion = diskindex.nVersion;
+ pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
+ pindexNew->nTime = diskindex.nTime;
+ pindexNew->nBits = diskindex.nBits;
+ pindexNew->nNonce = diskindex.nNonce;
+
+ // Watch for genesis block
+ if (pindexGenesisBlock == NULL && blockHash == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet))
+ pindexGenesisBlock = pindexNew;
+
+ if (!pindexNew->CheckIndex())
+ return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
+
+ // Build setStakeSeen
+ if (pindexNew->IsProofOfStake())
+ setStakeSeen.insert(make_pair(pindexNew->prevoutStake, pindexNew->nStakeTime));
+ }
+ else
+ {
+ break; // if shutdown requested or finished loading block index
+ }
+ } // try
+ catch (std::exception &e) {
+ return error("%s() : deserialize error", __PRETTY_FUNCTION__);
+ }
+ }
+ pcursor->close();
+
+ return true;
+}
+
+
+
+
+
//
// CAddrDB
//
if (hashIn != hashTmp)
return error("CAddrman::Read() : checksum mismatch; data corrupted");
+ // de-serialize address data
unsigned char pchMsgTmp[4];
try {
- // de-serialize file header (pchMessageStart magic number) and
ssPeers >> FLATDATA(pchMsgTmp);
-
- // verify the network matches ours
- if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
- return error("CAddrman::Read() : invalid network magic number");
-
- // de-serialize address data into one CAddrMan object
ssPeers >> addr;
}
catch (std::exception &e) {
return error("CAddrman::Read() : I/O error or stream data corrupted");
}
+ // finally, verify the network matches ours
+ if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
+ return error("CAddrman::Read() : invalid network magic number");
+
return true;
}
class CAddrMan;
class CBlockLocator;
class CDiskBlockIndex;
-class CDiskTxPos;
class CMasterKey;
class COutPoint;
-class CTxIndex;
class CWallet;
class CWalletTx;
bool fDbEnvInit;
bool fMockDb;
boost::filesystem::path pathEnv;
- std::string strPath;
void EnvShutdown();
};
+
+
+
+
+
+/** Access to the transaction database (coins.dat) */
+class CCoinsDB : public CDB
+{
+public:
+ CCoinsDB(const char* pszMode="r+") : CDB("coins.dat", pszMode) { }
+private:
+ CCoinsDB(const CCoinsDB&);
+ void operator=(const CCoinsDB&);
+public:
+ bool ReadCoins(uint256 hash, CCoins &coins);
+ bool WriteCoins(uint256 hash, const CCoins& coins);
+ bool HaveCoins(uint256 hash);
+ bool ReadHashBestChain(uint256& hashBestChain);
+ bool WriteHashBestChain(uint256 hashBestChain);
+};
+
+/** Access to the block database (chain.dat) */
+class CChainDB : public CDB
+{
+public:
+ CChainDB(const char* pszMode="r+") : CDB("chain.dat", pszMode) { }
+private:
+ CChainDB(const CChainDB&);
+ void operator=(const CChainDB&);
+public:
+ bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
+ bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
+ bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust);
+ bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
+ bool WriteBlockFileInfo(int nFile, const CBlockFileInfo &fileinfo);
+ bool ReadLastBlockFile(int &nFile);
+ bool WriteLastBlockFile(int nFile);
+ bool ReadSyncCheckpoint(uint256& hashCheckpoint);
+ bool WriteSyncCheckpoint(uint256 hashCheckpoint);
+ bool ReadCheckpointPubKey(std::string& strPubKey);
+ bool WriteCheckpointPubKey(const std::string& strPubKey);
+ bool LoadBlockIndexGuts();
+};
+
+
+bool LoadBlockIndex(CCoinsDB &coinsdb, CChainDB &chaindb);
+
+
/** Access to the (IP) address database (peers.dat) */
class CAddrDB
{
// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#include "txdb.h"
+#include "db.h"
#include "walletdb.h"
#include "bitcoinrpc.h"
#include "net.h"
{
fShutdown = true;
nTransactionsUpdated++;
-// CTxDB().Close();
bitdb.Flush(false);
StopNode();
bitdb.Flush(true);
return InitError(msg);
}
- if (GetBoolArg("-loadblockindextest"))
- {
- CTxDB txdb("r");
- txdb.LoadBlockIndex();
- PrintBlockTree();
- return false;
- }
-
uiInterface.InitMessage(_("Loading block index..."));
printf("Loading block index...\n");
nStart = GetTimeMillis();
#include <boost/assign/list_of.hpp>
#include "kernel.h"
-#include "txdb.h"
+#include "db.h"
using namespace std;
ss << nTimeBlockFrom << nTxPrevOffset << txPrev.nTime << prevout.n << nTimeTx;
hashProofOfStake = Hash(ss.begin(), ss.end());
+
if (fPrintProofOfStake)
{
printf("CheckStakeKernelHash() : using modifier 0x%016"PRI64x" at height=%d timestamp=%s for block from height=%d timestamp=%s\n",
// Kernel (input 0) must match the stake hash target per coin age (nBits)
const CTxIn& txin = tx.vin[0];
- // First try finding the previous transaction in database
- CTxDB txdb("r");
+ unsigned nTxPos;
+
CTransaction txPrev;
- CTxIndex txindex;
- if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
- return tx.DoS(1, error("CheckProofOfStake() : INFO: read txPrev failed")); // previous transaction not in main chain, may occur during initial download
+ CCoins coins;
+ CCoinsDB coindb("r");
+ CCoinsViewDB view(coindb);
- // Verify signature
- if (!VerifySignature(txPrev, tx, 0, true, 0))
- return tx.DoS(100, error("CheckProofOfStake() : VerifySignature failed on coinstake %s", tx.GetHash().ToString().c_str()));
+ if (!view.GetCoins(txin.prevout.hash, coins))
+ return tx.DoS(1, error("CheckProofOfStake() : INFO: read coins for txPrev failed")); // previous transaction not in main chain, may occur during initial download
+
+ CBlockIndex* pindex = FindBlockByHeight(coins.nHeight);
- // Read block header
+ // Read block and scan it to find txPrev
CBlock block;
- if (!block.ReadFromDisk(txindex.pos.blockPos, false))
+ if (block.ReadFromDisk(pindex)) {
+ nTxPos = GetSerializeSize(CBlock(), SER_DISK, CLIENT_VERSION) - (2 * GetSizeOfCompactSize(0)) + GetSizeOfCompactSize(block.vtx.size());
+ BOOST_FOREACH(const CTransaction &tx, block.vtx) {
+ if (tx.GetHash() == txin.prevout.hash) {
+ txPrev = tx;
+ break;
+ }
+ nTxPos += tx.GetSerializeSize(SER_DISK, CLIENT_VERSION);
+ }
+ }
+ else
return fDebug? error("CheckProofOfStake() : read block failed") : false; // unable to read block of previous transaction
- if (!CheckStakeKernelHash(nBits, block, txindex.pos.nTxPos, txPrev, txin.prevout, tx.nTime, hashProofOfStake, targetProofOfStake, fDebug))
+ // Verify signature
+ if (!VerifySignature(coins, tx, 0, true, false, 0))
+ return tx.DoS(100, error("CheckProofOfStake() : VerifySignature failed on coinstake %s", tx.GetHash().ToString().c_str()));
+
+ if (!CheckStakeKernelHash(nBits, block, nTxPos, txPrev, txin.prevout, tx.nTime, hashProofOfStake, targetProofOfStake, fDebug))
return tx.DoS(1, error("CheckProofOfStake() : INFO: check kernel failed on coinstake %s, hashProof=%s", tx.GetHash().ToString().c_str(), hashProofOfStake.ToString().c_str())); // may occur during initial download or if behind on block chain sync
return true;
#include "alert.h"
#include "checkpoints.h"
#include "db.h"
-#include "txdb.h"
#include "net.h"
#include "init.h"
#include "ui_interface.h"
}
+//////////////////////////////////////////////////////////////////////////////
+//
+// CCoinsView implementations
+//
+bool CCoinsView::GetCoins(uint256 txid, CCoins &coins) { return false; }
+bool CCoinsView::SetCoins(uint256 txid, const CCoins &coins) { return false; }
+bool CCoinsView::HaveCoins(uint256 txid) { return false; }
+CBlockIndex *CCoinsView::GetBestBlock() { return NULL; }
+bool CCoinsView::SetBestBlock(CBlockIndex *pindex) { return false; }
+
+CCoinsViewBacked::CCoinsViewBacked(CCoinsView &viewIn) : base(&viewIn) { }
+bool CCoinsViewBacked::GetCoins(uint256 txid, CCoins &coins) { return base->GetCoins(txid, coins); }
+bool CCoinsViewBacked::SetCoins(uint256 txid, const CCoins &coins) { return base->SetCoins(txid, coins); }
+bool CCoinsViewBacked::HaveCoins(uint256 txid) { return base->HaveCoins(txid); }
+CBlockIndex *CCoinsViewBacked::GetBestBlock() { return base->GetBestBlock(); }
+bool CCoinsViewBacked::SetBestBlock(CBlockIndex *pindex) { return base->SetBestBlock(pindex); }
+void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
+
+CCoinsViewDB::CCoinsViewDB(CCoinsDB &dbIn) : db(dbIn) {}
+bool CCoinsViewDB::GetCoins(uint256 txid, CCoins &coins) { return db.ReadCoins(txid, coins); }
+bool CCoinsViewDB::SetCoins(uint256 txid, const CCoins &coins) { return db.WriteCoins(txid, coins); }
+bool CCoinsViewDB::HaveCoins(uint256 txid) { return db.HaveCoins(txid); }
+CBlockIndex *CCoinsViewDB::GetBestBlock() { return pindexBest; }
+bool CCoinsViewDB::SetBestBlock(CBlockIndex *pindex) { return db.WriteHashBestChain(pindex->GetBlockHash()); }
+
+CCoinsViewCache::CCoinsViewCache(CCoinsView &baseIn, bool fDummy) : CCoinsViewBacked(baseIn), pindexTip(NULL) { }
+
+bool CCoinsViewCache::GetCoins(uint256 txid, CCoins &coins) {
+ if (cacheCoins.count(txid)) {
+ coins = cacheCoins[txid];
+ return true;
+ }
+ if (base->GetCoins(txid, coins)) {
+ cacheCoins[txid] = coins;
+ return true;
+ }
+ return false;
+}
+bool CCoinsViewCache::SetCoins(uint256 txid, const CCoins &coins) {
+ cacheCoins[txid] = coins;
+ return true;
+}
+bool CCoinsViewCache::HaveCoins(uint256 txid) {
+ return cacheCoins.count(txid) || base->HaveCoins(txid);
+}
+
+CBlockIndex *CCoinsViewCache::GetBestBlock() {
+ if (pindexTip == NULL)
+ pindexTip = base->GetBestBlock();
+ return pindexTip;
+}
+
+bool CCoinsViewCache::SetBestBlock(CBlockIndex *pindex) {
+ pindexTip = pindex;
+ return true;
+}
+
+bool CCoinsViewCache::Flush() {
+ for (std::map<uint256,CCoins>::iterator it = cacheCoins.begin(); it != cacheCoins.end(); it++) {
+ if (!base->SetCoins(it->first, it->second))
+ return false;
+ }
+ if (!base->SetBestBlock(pindexTip))
+ return false;
+ cacheCoins.clear();
+ pindexTip = NULL;
+ return true;
+}
+
+/** CCoinsView that brings transactions from a memorypool into view.
+ It does not check for spendings by memory pool transactions. */
+CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
+
+bool CCoinsViewMemPool::GetCoins(uint256 txid, CCoins &coins) {
+ if (base->GetCoins(txid, coins))
+ return true;
+ if (mempool.exists(txid)) {
+ const CTransaction &tx = mempool.lookup(txid);
+ coins = CCoins(tx, MEMPOOL_HEIGHT, -1);
+ return true;
+ }
+ return false;
+}
+
+bool CCoinsViewMemPool::HaveCoins(uint256 txid) {
+ return mempool.exists(txid) || base->HaveCoins(txid);
+}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
-// CTransaction and CTxIndex
+// CTransaction
//
-bool CTransaction::ReadFromDisk(CTxDB& txdb, COutPoint prevout, CTxIndex& txindexRet)
-{
- SetNull();
- if (!txdb.ReadTxIndex(prevout.hash, txindexRet))
- return false;
- if (!ReadFromDisk(txindexRet.pos))
- return false;
- if (prevout.n >= vout.size())
- {
- SetNull();
- return false;
- }
- return true;
-}
-
-bool CTransaction::ReadFromDisk(CTxDB& txdb, COutPoint prevout)
-{
- CTxIndex txindex;
- return ReadFromDisk(txdb, prevout, txindex);
-}
-
-bool CTransaction::ReadFromDisk(COutPoint prevout)
-{
- CTxDB txdb("r");
- CTxIndex txindex;
- return ReadFromDisk(txdb, prevout, txindex);
-}
-
bool CTransaction::IsStandard() const
{
if (nVersion > CTransaction::CURRENT_VERSION)
// expensive-to-check-upon-redemption script like:
// DUP CHECKSIG DROP ... repeated 100 times... OP_1
//
-bool CTransaction::AreInputsStandard(const MapPrevTx& mapInputs) const
+bool CTransaction::AreInputsStandard(CCoinsView& mapInputs) const
{
if (IsCoinBase())
return true; // Coinbases don't use vin normally
// beside "push data" in the scriptSig the
// IsStandard() call returns false
vector<vector<unsigned char> > stack;
- if (!EvalScript(stack, vin[i].scriptSig, *this, i, 0))
+ if (!EvalScript(stack, vin[i].scriptSig, *this, i, false, 0))
return false;
if (whichType == TX_SCRIPTHASH)
else
{
CBlock blockTmp;
- if (pblock == NULL)
- {
- // Load the block this tx is in
- CTxIndex txindex;
- if (!CTxDB("r").ReadTxIndex(GetHash(), txindex))
- return 0;
- if (!blockTmp.ReadFromDisk(txindex.pos.blockPos))
- return 0;
- pblock = &blockTmp;
+
+ if (pblock == NULL) {
+ CCoinsDB coinsdb("r");
+ CCoins coins;
+ if (coinsdb.ReadCoins(GetHash(), coins)) {
+ CBlockIndex *pindex = FindBlockByHeight(coins.nHeight);
+ if (pindex) {
+ if (!blockTmp.ReadFromDisk(pindex))
+ return 0;
+ pblock = &blockTmp;
+ }
+ }
}
+ if (pblock) {
// Update the tx's hashBlock
hashBlock = pblock->GetHash();
// Fill in merkle branch
vMerkleBranch = pblock->GetMerkleBranch(nIndex);
+ }
}
// Is the tx in a block that's in the main chain
return pindexBest->nHeight - pindex->nHeight + 1;
}
-
-
-
-
-
-
bool CTransaction::CheckTransaction() const
{
// Basic checks that don't depend on any context
return nMinFee;
}
+void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
+{
+ LOCK(cs);
+
+ std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0));
+
+ // iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx
+ while (it != mapNextTx.end() && it->first.hash == hashTx) {
+ coins.Spend(it->first.n); // and remove those outputs from coins
+ it++;
+ }
+}
-bool CTxMemPool::accept(CTxDB& txdb, CTransaction &tx, bool fCheckInputs,
- bool* pfMissingInputs)
+bool CTxMemPool::accept(CCoinsDB& coinsdb, CTransaction &tx, bool fCheckInputs, bool* pfMissingInputs)
{
if (pfMissingInputs)
*pfMissingInputs = false;
if (tx.IsCoinBase())
return tx.DoS(100, error("CTxMemPool::accept() : coinbase as individual tx"));
- // ppcoin: coinstake is also only valid in a block, not as a loose transaction
+ // Coinstake is also only valid in a block, not as a loose transaction
if (tx.IsCoinStake())
return tx.DoS(100, error("CTxMemPool::accept() : coinstake as individual tx"));
if (!fTestNet && !tx.IsStandard())
return error("CTxMemPool::accept() : nonstandard transaction type");
- // Do we already have it?
+ // is it already in the memory pool?
uint256 hash = tx.GetHash();
{
LOCK(cs);
if (mapTx.count(hash))
return false;
}
- if (fCheckInputs)
- if (txdb.ContainsTx(hash))
- return false;
// Check for conflicts with in-memory transactions
CTransaction* ptxOld = NULL;
if (fCheckInputs)
{
- MapPrevTx mapInputs;
- map<uint256, CTxIndex> mapUnused;
- bool fInvalid = false;
- if (!tx.FetchInputs(txdb, mapUnused, false, false, mapInputs, fInvalid))
- {
- if (fInvalid)
- return error("CTxMemPool::accept() : FetchInputs found invalid tx %s", hash.ToString().substr(0,10).c_str());
- if (pfMissingInputs)
- *pfMissingInputs = true;
+ CCoinsViewDB viewDB(coinsdb);
+ CCoinsViewMemPool viewMemPool(viewDB, mempool);
+ CCoinsViewCache view(viewMemPool);
+
+ // do we already have it?
+ if (view.HaveCoins(hash))
return false;
+
+ // do all inputs exist?
+ BOOST_FOREACH(const CTxIn txin, tx.vin) {
+ if (!view.HaveCoins(txin.prevout.hash)) {
+ if (pfMissingInputs)
+ *pfMissingInputs = true;
+ return false;
+ }
}
// Check for non-standard pay-to-script-hash in inputs
- if (!tx.AreInputsStandard(mapInputs) && !fTestNet)
+ if (!tx.AreInputsStandard(view) && !fTestNet)
return error("CTxMemPool::accept() : nonstandard transaction input");
// Note: if you modify this code to accept non-standard transactions, then
// you should add code here to check that the transaction does a
// reasonable number of ECDSA signature verifications.
- int64 nFees = tx.GetValueIn(mapInputs)-tx.GetValueOut();
+ int64 nFees = tx.GetValueIn(view)-tx.GetValueOut();
unsigned int nSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
// Don't accept it if it can't get into a block
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 others' transactions take longer to confirm.
// Check against previous transactions
// This is done last to help prevent CPU exhaustion denial-of-service attacks.
- if (!tx.ConnectInputs(txdb, mapInputs, mapUnused, CDiskTxPos(true), pindexBest, false, false))
+ if (!tx.CheckInputs(view, CS_ALWAYS, true, false))
{
return error("CTxMemPool::accept() : ConnectInputs failed %s", hash.ToString().substr(0,10).c_str());
}
return true;
}
-bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMissingInputs)
+bool CTransaction::AcceptToMemoryPool(CCoinsDB& coinsdb, bool fCheckInputs, bool* pfMissingInputs)
{
- return mempool.accept(txdb, *this, fCheckInputs, pfMissingInputs);
+ return mempool.accept(coinsdb, *this, fCheckInputs, pfMissingInputs);
}
bool CTxMemPool::addUnchecked(const uint256& hash, CTransaction &tx)
return pindexBest->nHeight - pindex->nHeight + 1;
}
-
int CMerkleTx::GetBlocksToMaturity() const
{
if (!(IsCoinBase() || IsCoinStake()))
}
-bool CMerkleTx::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs)
+bool CMerkleTx::AcceptToMemoryPool(CCoinsDB& coinsdb, bool fCheckInputs)
{
if (fClient)
{
- if (!IsInMainChain() && !ClientConnectInputs())
+ if (!IsInMainChain() && !ClientCheckInputs())
return false;
- return CTransaction::AcceptToMemoryPool(txdb, false);
+ return CTransaction::AcceptToMemoryPool(coinsdb, false);
}
else
{
- return CTransaction::AcceptToMemoryPool(txdb, fCheckInputs);
+ return CTransaction::AcceptToMemoryPool(coinsdb, fCheckInputs);
}
}
bool CMerkleTx::AcceptToMemoryPool()
{
- CTxDB txdb("r");
- return AcceptToMemoryPool(txdb);
+ CCoinsDB coinsdb("r");
+ return AcceptToMemoryPool(coinsdb);
}
-
-
-bool CWalletTx::AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs)
+bool CWalletTx::AcceptWalletTransaction(CCoinsDB& coinsdb, bool fCheckInputs)
{
{
if (!(tx.IsCoinBase() || tx.IsCoinStake()))
{
uint256 hash = tx.GetHash();
- if (!mempool.exists(hash) && !txdb.ContainsTx(hash))
- tx.AcceptToMemoryPool(txdb, fCheckInputs);
+ if (!mempool.exists(hash) && !coinsdb.HaveCoins(hash))
+ tx.AcceptToMemoryPool(coinsdb, fCheckInputs);
}
}
- return AcceptToMemoryPool(txdb, fCheckInputs);
+ return AcceptToMemoryPool(coinsdb, fCheckInputs);
}
return false;
}
bool CWalletTx::AcceptWalletTransaction()
{
- CTxDB txdb("r");
- return AcceptWalletTransaction(txdb);
-}
-
-int CTxIndex::GetDepthInMainChain() const
-{
- // Read block header
- CBlock block;
- if (!block.ReadFromDisk(pos.blockPos, false))
- return 0;
- // Find the block in the index
- map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(block.GetHash());
- if (mi == mapBlockIndex.end())
- return 0;
- CBlockIndex* pindex = (*mi).second;
- if (!pindex || !pindex->IsInMainChain())
- return 0;
- return 1 + nBestHeight - pindex->nHeight;
+ CCoinsDB coinsdb("r");
+ return AcceptWalletTransaction(coinsdb);
}
// Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock
-bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock)
+bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
{
+ CBlockIndex *pindexSlow = NULL;
{
LOCK(cs_main);
{
LOCK(mempool.cs);
if (mempool.exists(hash))
{
- tx = mempool.lookup(hash);
+ txOut = mempool.lookup(hash);
return true;
}
}
- CTxDB txdb("r");
- CTxIndex txindex;
- if (tx.ReadFromDisk(txdb, COutPoint(hash, 0), txindex))
- {
- CBlock block;
- if (block.ReadFromDisk(txindex.pos.blockPos, false))
- hashBlock = block.GetHash();
- return true;
+
+ if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
+ int nHeight = -1;
+ {
+ CCoinsDB coindb("r");
+ CCoinsViewDB view(coindb);
+ CCoins coins;
+ if (view.GetCoins(hash, coins))
+ nHeight = coins.nHeight;
+ }
+ if (nHeight > 0)
+ pindexSlow = FindBlockByHeight(nHeight);
}
}
- return false;
-}
-
-
-
-
+ if (pindexSlow) {
+ CBlock block;
+ if (block.ReadFromDisk(pindexSlow)) {
+ BOOST_FOREACH(const CTransaction &tx, block.vtx) {
+ if (tx.GetHash() == hash) {
+ txOut = tx;
+ hashBlock = pindexSlow->GetBlockHash();
+ return true;
+ }
+ }
+ }
+ }
+ return false;
+}
//////////////////////////////////////////////////////////////////////////////
if (pindexNew->nChainTrust > nBestInvalidTrust)
{
nBestInvalidTrust = pindexNew->nChainTrust;
- CTxDB().WriteBestInvalidTrust(CBigNum(nBestInvalidTrust));
+ CChainDB().WriteBestInvalidTrust(CBigNum(nBestInvalidTrust));
uiInterface.NotifyBlocksChanged();
}
}
-
-
-
-
-
-
-
-
-
-bool CTransaction::DisconnectInputs(CTxDB& txdb)
+CTxOut CTransaction::GetOutputFor(const CTxIn& input, CCoinsView& view)
{
- // Relinquish previous transactions' spent pointers
- if (!IsCoinBase())
- {
- BOOST_FOREACH(const CTxIn& txin, vin)
- {
- COutPoint prevout = txin.prevout;
-
- // Get prev txindex from disk
- CTxIndex txindex;
- if (!txdb.ReadTxIndex(prevout.hash, txindex))
- return error("DisconnectInputs() : ReadTxIndex failed");
-
- if (prevout.n >= txindex.vSpent.size())
- return error("DisconnectInputs() : prevout.n out of range");
-
- // Mark outpoint as not spent
- txindex.vSpent[prevout.n].SetNull();
-
- // Write back
- if (!txdb.UpdateTxIndex(prevout.hash, txindex))
- return error("DisconnectInputs() : UpdateTxIndex failed");
- }
- }
-
- // 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 anyway.
- txdb.EraseTxIndex(*this);
-
- return true;
-}
-
-
-bool CTransaction::FetchInputs(CTxDB& txdb, const map<uint256, CTxIndex>& mapTestPool,
- bool fBlock, bool fMiner, MapPrevTx& inputsRet, bool& fInvalid)
-{
- // FetchInputs can return false either because we just haven't seen some inputs
- // (in which case the transaction should be stored as an orphan)
- // or because the transaction is malformed (in which case the transaction should
- // be dropped). If tx is definitely invalid, fInvalid will be set to true.
- fInvalid = false;
-
- if (IsCoinBase())
- return true; // Coinbase transactions have no inputs to fetch.
-
- for (unsigned int i = 0; i < vin.size(); i++)
- {
- COutPoint prevout = vin[i].prevout;
- if (inputsRet.count(prevout.hash))
- continue; // Got it already
-
- // Read txindex
- CTxIndex& txindex = inputsRet[prevout.hash].first;
- bool fFound = true;
- if ((fBlock || fMiner) && mapTestPool.count(prevout.hash))
- {
- // Get txindex from current proposed changes
- txindex = mapTestPool.find(prevout.hash)->second;
- }
- else
- {
- // Read txindex from txdb
- fFound = txdb.ReadTxIndex(prevout.hash, txindex);
- }
- if (!fFound && (fBlock || fMiner))
- return fMiner ? false : error("FetchInputs() : %s prev tx %s index entry not found", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
-
- // Read txPrev
- CTransaction& txPrev = inputsRet[prevout.hash].second;
- if (!fFound || txindex.pos.IsMemPool())
- {
- // Get prev tx from single transactions in memory
- {
- LOCK(mempool.cs);
- if (!mempool.exists(prevout.hash))
- return error("FetchInputs() : %s mempool Tx prev not found %s", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
- txPrev = mempool.lookup(prevout.hash);
- }
- if (!fFound)
- txindex.vSpent.resize(txPrev.vout.size());
- }
- else
- {
- // Get prev tx from disk
- if (!txPrev.ReadFromDisk(txindex.pos))
- return error("FetchInputs() : %s ReadFromDisk prev tx %s failed", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
- }
- }
-
- // Make sure all prevout.n indexes are valid:
- for (unsigned int i = 0; i < vin.size(); i++)
- {
- const COutPoint prevout = vin[i].prevout;
- assert(inputsRet.count(prevout.hash) != 0);
- const CTxIndex& txindex = inputsRet[prevout.hash].first;
- const CTransaction& txPrev = inputsRet[prevout.hash].second;
- if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
- {
- // Revisit this if/when transaction replacement is implemented and allows
- // adding inputs:
- fInvalid = true;
- return DoS(100, error("FetchInputs() : %s prevout.n out of range %d %"PRIszu" %"PRIszu" prev tx %s\n%s", GetHash().ToString().substr(0,10).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size(), prevout.hash.ToString().substr(0,10).c_str(), txPrev.ToString().c_str()));
- }
- }
-
- return true;
-}
-
-const CTxOut& CTransaction::GetOutputFor(const CTxIn& input, const MapPrevTx& inputs) const
-{
- MapPrevTx::const_iterator mi = inputs.find(input.prevout.hash);
- if (mi == inputs.end())
+ CCoins coins;
+ if (!view.GetCoins(input.prevout.hash, coins))
throw std::runtime_error("CTransaction::GetOutputFor() : prevout.hash not found");
- const CTransaction& txPrev = (mi->second).second;
- if (input.prevout.n >= txPrev.vout.size())
- throw std::runtime_error("CTransaction::GetOutputFor() : prevout.n out of range");
+ if (input.prevout.n >= coins.vout.size())
+ throw std::runtime_error("CTransaction::GetOutputFor() : prevout.n out of range or already spent");
- return txPrev.vout[input.prevout.n];
+ const CTxOut &out = coins.vout[input.prevout.n];
+ if (out.IsNull())
+ throw std::runtime_error("CTransaction::GetOutputFor() : already spent");
+
+ return out;
}
-int64 CTransaction::GetValueIn(const MapPrevTx& inputs) const
+int64 CTransaction::GetValueIn(CCoinsView& inputs) const
{
if (IsCoinBase())
return 0;
nResult += GetOutputFor(vin[i], inputs).nValue;
}
return nResult;
-
}
-unsigned int CTransaction::GetP2SHSigOpCount(const MapPrevTx& inputs) const
+unsigned int CTransaction::GetP2SHSigOpCount(CCoinsView& inputs) const
{
if (IsCoinBase())
return 0;
unsigned int nSigOps = 0;
for (unsigned int i = 0; i < vin.size(); i++)
{
- const CTxOut& prevout = GetOutputFor(vin[i], inputs);
+ CTxOut prevout = GetOutputFor(vin[i], inputs);
if (prevout.scriptPubKey.IsPayToScriptHash())
nSigOps += prevout.scriptPubKey.GetSigOpCount(vin[i].scriptSig);
}
return nSigOps;
}
-bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs, map<uint256, CTxIndex>& mapTestPool, const CDiskTxPos& posThisTx,
- const CBlockIndex* pindexBlock, bool fBlock, bool fMiner, bool fStrictPayToScriptHash)
+bool CTransaction::UpdateCoins(CCoinsView &inputs, CTxUndo &txundo, int nHeight, unsigned int nTimeStamp) const
{
- // Take over previous transactions' spent pointers
- // fBlock is true when this is called from AcceptBlock when a new best-block is added to the blockchain
- // fMiner is true when called from the internal bitcoin miner
- // ... both are false when called from CTransaction::AcceptToMemoryPool
- if (!IsCoinBase())
- {
- int64 nValueIn = 0;
- int64 nFees = 0;
- for (unsigned int i = 0; i < vin.size(); i++)
- {
- COutPoint prevout = vin[i].prevout;
- assert(inputs.count(prevout.hash) > 0);
- CTxIndex& txindex = inputs[prevout.hash].first;
- CTransaction& txPrev = inputs[prevout.hash].second;
+ uint256 hash = GetHash();
- if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
- return DoS(100, error("ConnectInputs() : %s prevout.n out of range %d %"PRIszu" %"PRIszu" prev tx %s\n%s", GetHash().ToString().substr(0,10).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size(), prevout.hash.ToString().substr(0,10).c_str(), txPrev.ToString().c_str()));
+ // mark inputs spent
+ if (!IsCoinBase()) {
+ BOOST_FOREACH(const CTxIn &txin, vin) {
+ CCoins coins;
+ if (!inputs.GetCoins(txin.prevout.hash, coins))
+ return error("UpdateCoins() : cannot find prevtx");
- // If prev is coinbase or coinstake, check that it's matured
- if (txPrev.IsCoinBase() || txPrev.IsCoinStake())
- for (const CBlockIndex* pindex = pindexBlock; pindex && pindexBlock->nHeight - pindex->nHeight < nCoinbaseMaturity; pindex = pindex->pprev)
- if (pindex->GetBlockPos() == txindex.pos.blockPos)
- return error("ConnectInputs() : tried to spend %s at depth %d", txPrev.IsCoinBase() ? "coinbase" : "coinstake", pindexBlock->nHeight - pindex->nHeight);
+ if (coins.nTime > nTimeStamp)
+ return error("UpdateCoins() : timestamp violation");
- // ppcoin: check transaction timestamp
- if (txPrev.nTime > nTime)
- return DoS(100, error("ConnectInputs() : transaction timestamp earlier than input transaction"));
+ CTxInUndo undo;
+ if (!coins.Spend(txin.prevout, undo))
+ return error("UpdateCoins() : cannot spend input");
+ txundo.vprevout.push_back(undo);
+ if (!inputs.SetCoins(txin.prevout.hash, coins))
+ return error("UpdateCoins() : cannot update input");
+ }
+ }
- // Check for negative or overflow input values
- nValueIn += txPrev.vout[prevout.n].nValue;
- if (!MoneyRange(txPrev.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
- return DoS(100, error("ConnectInputs() : txin values out of range"));
+ // add outputs
+ if (!inputs.SetCoins(hash, CCoins(*this, nHeight, nTimeStamp)))
+ return error("UpdateCoins() : cannot update output");
+ return true;
+}
+
+bool CTransaction::HaveInputs(CCoinsView &inputs) const
+{
+ if (!IsCoinBase()) {
+ // first check whether information about the prevout hash is available
+ for (unsigned int i = 0; i < vin.size(); i++) {
+ const COutPoint &prevout = vin[i].prevout;
+ if (!inputs.HaveCoins(prevout.hash))
+ return false;
}
- // The first loop above does all the inexpensive checks.
- // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
- // Helps prevent CPU exhaustion attacks.
+
+ // then check whether the actual outputs are available
+ for (unsigned int i = 0; i < vin.size(); i++) {
+ const COutPoint &prevout = vin[i].prevout;
+ CCoins coins;
+ inputs.GetCoins(prevout.hash, coins);
+ if (!coins.IsAvailable(prevout.n))
+ return false;
+ }
+ }
+ return true;
+}
+
+bool CTransaction::CheckInputs(CCoinsView &inputs, enum CheckSig_mode csmode, bool fStrictPayToScriptHash, bool fStrictEncodings, CBlock *pblock) const
+{
+ if (!IsCoinBase())
+ {
+ int64 nValueIn = 0;
+ int64 nFees = 0;
for (unsigned int i = 0; i < vin.size(); i++)
{
- COutPoint prevout = vin[i].prevout;
- assert(inputs.count(prevout.hash) > 0);
- CTxIndex& txindex = inputs[prevout.hash].first;
- CTransaction& txPrev = inputs[prevout.hash].second;
+ const COutPoint &prevout = vin[i].prevout;
+ CCoins coins;
+ if (!inputs.GetCoins(prevout.hash, coins))
+ return error("CheckInputs() : cannot find prevout tx");
// Check for conflicts (double-spend)
// This doesn't trigger the DoS code on purpose; if it did, it would make it easier
// for an attacker to attempt to split the network.
- if (!txindex.vSpent[prevout.n].IsNull())
- return fMiner ? false : error("ConnectInputs() : %s prev tx already used at %s", GetHash().ToString().substr(0,10).c_str(), txindex.vSpent[prevout.n].ToString().c_str());
-
- // Skip ECDSA signature verification when connecting blocks (fBlock=true)
- // before the last blockchain checkpoint. This is safe because block merkle hashes are
- // still computed and checked, and any change will be caught at the next checkpoint.
- if (!(fBlock && (nBestHeight < Checkpoints::GetTotalBlocksEstimate())))
- {
- // Verify signature
- if (!VerifySignature(txPrev, *this, i, fStrictPayToScriptHash, 0))
- {
- // only during transition phase for P2SH: do not invoke anti-DoS code for
- // potentially old clients relaying bad P2SH transactions
- if (fStrictPayToScriptHash && VerifySignature(txPrev, *this, i, false, 0))
- return error("ConnectInputs() : %s P2SH VerifySignature failed", GetHash().ToString().substr(0,10).c_str());
+ if (!coins.IsAvailable(prevout.n))
+ return error("CheckInputs() : %s prev tx already used", GetHash().ToString().substr(0,10).c_str());
- return DoS(100,error("ConnectInputs() : %s VerifySignature failed", GetHash().ToString().substr(0,10).c_str()));
- }
+ // If prev is coinbase or coinstake, check that it's matured
+ if (coins.IsCoinBase() || coins.IsCoinStake()) {
+ CBlockIndex *pindexBlock = inputs.GetBestBlock();
+ if (pindexBlock->nHeight - coins.nHeight < nCoinbaseMaturity)
+ return error("CheckInputs() : tried to spend %s at depth %d", coins.IsCoinBase() ? "coinbase" : "coinstake", pindexBlock->nHeight - coins.nHeight);
}
- // Mark outpoints as spent
- txindex.vSpent[prevout.n] = posThisTx;
+ // Check transaction timestamp
+ if (coins.nTime > nTime)
+ return DoS(100, error("CheckInputs() : transaction timestamp earlier than input transaction"));
- // Write back
- if (fBlock || fMiner)
- {
- mapTestPool[prevout.hash] = txindex;
- }
+ // Check for negative or overflow input values
+ nValueIn += coins.vout[prevout.n].nValue;
+ if (!MoneyRange(coins.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
+ return DoS(100, error("CheckInputs() : txin values out of range"));
}
if (IsCoinStake())
{
- // ppcoin: coin stake tx earns reward instead of paying fee
+ if (!pblock)
+ return error("CheckInputs() : %s is a coinstake, but no block specified", GetHash().ToString().substr(0,10).c_str());
+
+ // Coin stake tx earns reward instead of paying fee
uint64 nCoinAge;
- if (!GetCoinAge(txdb, nCoinAge))
- return error("ConnectInputs() : %s unable to get coin age for coinstake", GetHash().ToString().substr(0,10).c_str());
+ if (!GetCoinAge(inputs, nCoinAge))
+ return error("CheckInputs() : %s unable to get coin age for coinstake", GetHash().ToString().substr(0,10).c_str());
int64 nStakeReward = GetValueOut() - nValueIn;
- int64 nCalculatedStakeReward = GetProofOfStakeReward(nCoinAge, pindexBlock->nBits, nTime) - GetMinFee() + MIN_TX_FEE;
+ int64 nCalculatedStakeReward = GetProofOfStakeReward(nCoinAge, pblock->nBits, nTime) - GetMinFee() + MIN_TX_FEE;
if (nStakeReward > nCalculatedStakeReward)
- return DoS(100, error("ConnectInputs() : coinstake pays too much(actual=%"PRI64d" vs calculated=%"PRI64d")", nStakeReward, nCalculatedStakeReward));
+ return DoS(100, error("CheckInputs() : coinstake pays too much(actual=%"PRI64d" vs calculated=%"PRI64d")", nStakeReward, nCalculatedStakeReward));
}
else
{
if (nValueIn < GetValueOut())
- return DoS(100, error("ConnectInputs() : %s value in < value out", GetHash().ToString().substr(0,10).c_str()));
+ return DoS(100, error("ChecktInputs() : %s value in < value out", GetHash().ToString().substr(0,10).c_str()));
// Tally transaction fees
int64 nTxFee = nValueIn - GetValueOut();
if (nTxFee < 0)
- return DoS(100, error("ConnectInputs() : %s nTxFee < 0", GetHash().ToString().substr(0,10).c_str()));
+ return DoS(100, error("CheckInputs() : %s nTxFee < 0", GetHash().ToString().substr(0,10).c_str()));
+ nFees += nTxFee;
+ if (!MoneyRange(nFees))
+ return DoS(100, error("CheckInputs() : nFees out of range"));
// enforce transaction fees for every block until 1 May 2014 04:00:00 GMT
if (!fTestNet && nTxFee < GetMinFee() && nTime < OUTPUT_SWITCH_TIME)
- return fBlock? DoS(100, error("ConnectInputs() : %s not paying required fee=%s, paid=%s", GetHash().ToString().substr(0,10).c_str(), FormatMoney(GetMinFee()).c_str(), FormatMoney(nTxFee).c_str())) : false;
+ return pblock? DoS(100, error("CheckInputs() : %s not paying required fee=%s, paid=%s", GetHash().ToString().substr(0,10).c_str(), FormatMoney(GetMinFee()).c_str(), FormatMoney(nTxFee).c_str())) : false;
+ }
- nFees += nTxFee;
- if (!MoneyRange(nFees))
- return DoS(100, error("ConnectInputs() : nFees out of range"));
+ // The first loop above does all the inexpensive checks.
+ // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
+ // Helps prevent CPU exhaustion attacks.
+
+ // Skip ECDSA signature verification when connecting blocks
+ // before the last blockchain checkpoint. This is safe because block merkle hashes are
+ // still computed and checked, and any change will be caught at the next checkpoint.
+ if (csmode == CS_ALWAYS ||
+ (csmode == CS_AFTER_CHECKPOINT && inputs.GetBestBlock()->nHeight >= Checkpoints::GetTotalBlocksEstimate())) {
+ for (unsigned int i = 0; i < vin.size(); i++) {
+ const COutPoint &prevout = vin[i].prevout;
+ CCoins coins;
+ inputs.GetCoins(prevout.hash, coins);
+
+ // Verify signature
+ if (!VerifySignature(coins, *this, i, fStrictPayToScriptHash, fStrictEncodings, 0)) {
+ // only during transition phase for P2SH: do not invoke anti-DoS code for
+ // potentially old clients relaying bad P2SH transactions
+ if (fStrictPayToScriptHash && VerifySignature(coins, *this, i, false, fStrictEncodings, 0))
+ return error("CheckInputs() : %s P2SH VerifySignature failed", GetHash().ToString().substr(0,10).c_str());
+
+ return DoS(100,error("CheckInputs() : %s VerifySignature failed", GetHash().ToString().substr(0,10).c_str()));
+ }
+ }
}
+
+
}
return true;
}
-bool CTransaction::ClientConnectInputs()
+bool CTransaction::ClientCheckInputs() const
{
if (IsCoinBase())
return false;
return false;
// Verify signature
- if (!VerifySignature(txPrev, *this, i, true, 0))
+ if (!VerifySignature(CCoins(txPrev, -1, -1), *this, i, true, false, 0))
return error("ConnectInputs() : VerifySignature failed");
///// this is redundant with the mempool.mapNextTx stuff,
return true;
}
+bool CBlock::DisconnectBlock(CBlockIndex *pindex, CCoinsView &view)
+{
+ assert(pindex == view.GetBestBlock());
+ CBlockUndo blockUndo;
+ {
+ CDiskBlockPos pos = pindex->GetUndoPos();
+ if (pos.IsNull())
+ return error("DisconnectBlock() : no undo data available");
+ FILE *file = OpenUndoFile(pos, true);
+ if (file == NULL)
+ return error("DisconnectBlock() : undo file not available");
+ CAutoFile fileUndo(file, SER_DISK, CLIENT_VERSION);
+ fileUndo >> blockUndo;
+ }
+ assert(blockUndo.vtxundo.size() + 1 == vtx.size());
-bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex)
-{
- // Disconnect in reverse order
- for (int i = vtx.size()-1; i >= 0; i--)
- if (!vtx[i].DisconnectInputs(txdb))
- return false;
+ // undo transactions in reverse order
+ for (int i = vtx.size() - 1; i >= 0; i--) {
+ const CTransaction &tx = vtx[i];
+ uint256 hash = tx.GetHash();
- // Update block index on disk without changing it in memory.
- // The memory index structure will be changed after the db commits.
- if (pindex->pprev)
- {
- CDiskBlockIndex blockindexPrev(pindex->pprev);
- blockindexPrev.hashNext = 0;
- if (!txdb.WriteBlockIndex(blockindexPrev))
- return error("DisconnectBlock() : WriteBlockIndex failed");
+ // check that all outputs are available
+ CCoins outs;
+ if (!view.GetCoins(hash, outs))
+ return error("DisconnectBlock() : outputs still spent? database corrupted");
+
+ CCoins outsBlock = CCoins(tx, pindex->nHeight, pindex->nTime);
+ if (outs != outsBlock)
+ return error("DisconnectBlock() : added transaction mismatch? database corrupted");
+
+ // remove outputs
+ if (!view.SetCoins(hash, CCoins()))
+ return error("DisconnectBlock() : cannot delete coin outputs");
+
+ // restore inputs
+ if (i > 0) { // not coinbases
+ const CTxUndo &txundo = blockUndo.vtxundo[i-1];
+ assert(txundo.vprevout.size() == tx.vin.size());
+ for (unsigned int j = tx.vin.size(); j-- > 0;) {
+ const COutPoint &out = tx.vin[j].prevout;
+ const CTxInUndo &undo = txundo.vprevout[j];
+ CCoins coins;
+ view.GetCoins(out.hash, coins); // this can fail if the prevout was already entirely spent
+ if (coins.IsPruned()) {
+ if (undo.nHeight == 0)
+ return error("DisconnectBlock() : undo data doesn't contain tx metadata? database corrupted");
+ coins.fCoinBase = undo.fCoinBase;
+ coins.nHeight = undo.nHeight;
+ coins.nVersion = undo.nVersion;
+ } else {
+ if (undo.nHeight != 0)
+ return error("DisconnectBlock() : undo data contains unneeded tx metadata? database corrupted");
+ }
+ if (coins.IsAvailable(out.n))
+ return error("DisconnectBlock() : prevout output not spent? database corrupted");
+ if (coins.vout.size() < out.n+1)
+ coins.vout.resize(out.n+1);
+ coins.vout[out.n] = undo.txout;
+ if (!view.SetCoins(out.hash, coins))
+ return error("DisconnectBlock() : cannot restore coin inputs");
+ }
+ }
}
- // ppcoin: clean up wallet after disconnecting coinstake
- BOOST_FOREACH(CTransaction& tx, vtx)
- SyncWithWallets(tx, this, false, false);
+ // move best block pointer to prevout block
+ view.SetBestBlock(pindex->pprev);
return true;
}
-bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck)
+bool FindUndoPos(CChainDB &chaindb, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
+
+bool CBlock::ConnectBlock(CBlockIndex* pindex, CCoinsView &view, bool fJustCheck)
{
- // Check it again in case a previous version let a bad block in, but skip BlockSig checking
- if (!CheckBlock(!fJustCheck, !fJustCheck, false))
+ // Check it again in case a previous version let a bad block in
+ if (!CheckBlock(!fJustCheck, !fJustCheck))
return false;
+ // verify that the view's current state corresponds to the previous block
+ assert(pindex->pprev == view.GetBestBlock());
+
// Do not allow blocks that contain transactions which 'overwrite' older transactions,
// unless those are already completely spent.
// If such overwrites are allowed, coinbases and transactions depending upon those
// 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 = true; // Always active in NovaCoin
- bool fStrictPayToScriptHash = true; // Always active in NovaCoin
+ bool fEnforceBIP30 = true;
- //// issue here: it doesn't know the version
- unsigned int nTxPos;
- if (fJustCheck)
- // 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 = ::GetSerializeSize(CBlock(), SER_DISK, CLIENT_VERSION) - (2 * GetSizeOfCompactSize(0)) + GetSizeOfCompactSize(vtx.size());
+ if (fEnforceBIP30) {
+ BOOST_FOREACH(CTransaction& tx, vtx) {
+ uint256 hash = tx.GetHash();
+ CCoins coins;
+ if (view.GetCoins(hash, coins) && !coins.IsPruned())
+ return error("ConnectBlock() : tried to overwrite transaction");
+ }
+ }
+
+ // BIP16 always active
+ bool fStrictPayToScriptHash = true;
CBlockUndo blockundo;
- map<uint256, CTxIndex> mapQueuedChanges;
- int64 nFees = 0;
- int64 nValueIn = 0;
- int64 nValueOut = 0;
+ int64 nFees = 0, nValueIn = 0, nValueOut = 0;
unsigned int nSigOps = 0;
BOOST_FOREACH(CTransaction& tx, vtx)
{
- uint256 hashTx = tx.GetHash();
-
- if (fEnforceBIP30) {
- CTxIndex txindexOld;
- if (txdb.ReadTxIndex(hashTx, txindexOld)) {
- BOOST_FOREACH(CDiskTxPos &pos, txindexOld.vSpent)
- if (pos.IsNull())
- return false;
- }
- }
-
nSigOps += tx.GetLegacySigOpCount();
if (nSigOps > MAX_BLOCK_SIGOPS)
return DoS(100, error("ConnectBlock() : too many sigops"));
- CDiskTxPos posThisTx(pindex->GetBlockPos(), nTxPos);
- if (!fJustCheck)
- nTxPos += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
-
- MapPrevTx mapInputs;
- if (tx.IsCoinBase())
- nValueOut += tx.GetValueOut();
- else
+ if (!tx.IsCoinBase())
{
- CTxUndo undo;
-
- bool fInvalid;
- if (!tx.FetchInputs(txdb, mapQueuedChanges, true, false, mapInputs, fInvalid))
- return false;
+ if (!tx.HaveInputs(view))
+ return DoS(100, error("ConnectBlock() : inputs missing/spent"));
if (fStrictPayToScriptHash)
{
// Add in sigops done by pay-to-script-hash inputs;
// this is to prevent a "rogue miner" from creating
// an incredibly-expensive-to-validate block.
- nSigOps += tx.GetP2SHSigOpCount(mapInputs);
+ nSigOps += tx.GetP2SHSigOpCount(view);
if (nSigOps > MAX_BLOCK_SIGOPS)
- return DoS(100, error("ConnectBlock() : too many sigops"));
+ return DoS(100, error("ConnectBlock() : too many sigops"));
}
- int64 nTxValueIn = tx.GetValueIn(mapInputs);
int64 nTxValueOut = tx.GetValueOut();
+ int64 nTxValueIn = tx.GetValueIn(view);
+
nValueIn += nTxValueIn;
nValueOut += nTxValueOut;
+
if (!tx.IsCoinStake())
nFees += nTxValueIn - nTxValueOut;
-
- BOOST_FOREACH(const CTxIn &in, tx.vin) {
- undo.vprevout.push_back(CTxInUndo(mapInputs[in.prevout.hash].second.vout[in.prevout.n], pindex->nHeight));
- }
-
- if (!tx.ConnectInputs(txdb, mapInputs, mapQueuedChanges, posThisTx, pindex, true, false, fStrictPayToScriptHash))
+ if (!tx.CheckInputs(view, CS_AFTER_CHECKPOINT, fStrictPayToScriptHash, false, this))
return false;
-
- blockundo.vtxundo.push_back(undo);
+ }
+ else
+ {
+ nValueOut += tx.GetValueOut();
}
- mapQueuedChanges[hashTx] = CTxIndex(posThisTx, tx.vout.size());
+ CTxUndo txundo;
+ if (!tx.UpdateCoins(view, txundo, pindex->nHeight, pindex->nTime))
+ return error("ConnectBlock() : UpdateInputs failed");
+ if (!tx.IsCoinBase())
+ blockundo.vtxundo.push_back(txundo);
}
- // ppcoin: track money supply and mint amount info
pindex->nMint = nValueOut - nValueIn + nFees;
pindex->nMoneySupply = (pindex->pprev? pindex->pprev->nMoneySupply : 0) + nValueOut - nValueIn;
- if (!txdb.WriteBlockIndex(CDiskBlockIndex(pindex)))
- return error("Connect() : WriteBlockIndex for pindex failed");
-
- // ppcoin: fees are not collected by miners as in bitcoin
- // ppcoin: fees are destroyed to compensate the entire network
- if (fDebug && GetBoolArg("-printcreation"))
- printf("ConnectBlock() : destroy=%s nFees=%"PRI64d"\n", FormatMoney(nFees).c_str(), nFees);
if (fJustCheck)
return true;
- // Write queued txindex changes
- for (map<uint256, CTxIndex>::iterator mi = mapQueuedChanges.begin(); mi != mapQueuedChanges.end(); ++mi)
- {
- if (!txdb.UpdateTxIndex((*mi).first, (*mi).second))
- return error("ConnectBlock() : UpdateTxIndex failed");
- }
-
- // Update block index on disk without changing it in memory.
- // The memory index structure will be changed after the db commits.
- if (pindex->pprev)
- {
- CDiskBlockIndex blockindexPrev(pindex->pprev);
- blockindexPrev.hashNext = pindex->GetBlockHash();
- if (!txdb.WriteBlockIndex(blockindexPrev))
- return error("ConnectBlock() : WriteBlockIndex failed");
- }
+ CChainDB chaindb;
+ CDiskBlockPos pos;
// Write undo information to disk
- if (pindex->nHeight > Checkpoints::GetTotalBlocksEstimate())
+ if (pindex->GetUndoPos().IsNull())
{
- CAutoFile fileUndo(fopen(pindex->GetBlockPos().GetUndoFile(GetDataDir()).string().c_str(), "wb"), SER_DISK, CLIENT_VERSION);
- fileUndo << blockundo;
+ if (!FindUndoPos(chaindb, pindex->pos.nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 8))
+ return error("ConnectBlock() : FindUndoPos failed");
+ if (!blockundo.WriteToDisk(pos))
+ return error("ConnectBlock() : CBlockUndo::WriteToDisk failed");
+
+ // update nUndoPos in block index
+ pindex->nUndoPos = pos.nPos + 1;
}
+ CDiskBlockIndex blockindex(pindex);
+ if (!chaindb.WriteBlockIndex(blockindex))
+ return error("ConnectBlock() : WriteBlockIndex failed");
+
+ // add this block to the view's blockchain
+ if (!view.SetBestBlock(pindex))
+ return false;
+
+ // fees are destroyed to compensate the entire network
+ if (fDebug && GetBoolArg("-printcreation"))
+ printf("ConnectBlock() : destroy=%s nFees=%"PRI64d"\n", FormatMoney(nFees).c_str(), nFees);
+
// Watch for transactions paying to me
BOOST_FOREACH(CTransaction& tx, vtx)
SyncWithWallets(tx, this, true);
return true;
}
-bool static Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
+bool CBlock::SetBestChain(CBlockIndex* pindexNew)
{
- printf("REORGANIZE\n");
+ // if this functions exits prematurely, the transaction is aborted
+ CCoinsDB coinsdb;
+ if (!coinsdb.TxnBegin())
+ return error("SetBestChain() : TxnBegin failed");
+
+ // special case for attaching the genesis block
+ // note that no ConnectBlock is called, so its coinbase output is non-spendable
+ if (pindexGenesisBlock == NULL && pindexNew->GetBlockHash() == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet))
+ {
+ coinsdb.WriteHashBestChain(pindexNew->GetBlockHash());
+ if (!coinsdb.TxnCommit())
+ return error("SetBestChain() : TxnCommit failed");
+ pindexGenesisBlock = pindexNew;
+ pindexBest = pindexNew;
+ hashBestChain = pindexNew->GetBlockHash();
+ nBestHeight = pindexBest->nHeight;
+ nBestChainTrust = pindexNew->nChainTrust;
+ return true;
+ }
+
+ // create cached view to the coins database
+ CCoinsViewDB viewDB(coinsdb);
+ CCoinsViewCache view(viewDB);
- // Find the fork
- CBlockIndex* pfork = pindexBest;
+ // Find the fork (typically, there is none)
+ CBlockIndex* pfork = view.GetBestBlock();
CBlockIndex* plonger = pindexNew;
while (pfork != plonger)
{
while (plonger->nHeight > pfork->nHeight)
if (!(plonger = plonger->pprev))
- return error("Reorganize() : plonger->pprev is null");
+ return error("SetBestChain() : plonger->pprev is null");
if (pfork == plonger)
break;
if (!(pfork = pfork->pprev))
- return error("Reorganize() : pfork->pprev is null");
+ return error("SetBestChain() : pfork->pprev is null");
}
- // List of what to disconnect
+ // List of what to disconnect (typically nothing)
vector<CBlockIndex*> vDisconnect;
- for (CBlockIndex* pindex = pindexBest; pindex != pfork; pindex = pindex->pprev)
+ for (CBlockIndex* pindex = view.GetBestBlock(); pindex != pfork; pindex = pindex->pprev)
vDisconnect.push_back(pindex);
- // List of what to connect
+ // List of what to connect (typically only pindexNew)
vector<CBlockIndex*> vConnect;
for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev)
vConnect.push_back(pindex);
reverse(vConnect.begin(), vConnect.end());
- printf("REORGANIZE: Disconnect %"PRIszu" blocks; %s..%s\n", vDisconnect.size(), pfork->GetBlockHash().ToString().substr(0,20).c_str(), pindexBest->GetBlockHash().ToString().substr(0,20).c_str());
- printf("REORGANIZE: Connect %"PRIszu" blocks; %s..%s\n", vConnect.size(), pfork->GetBlockHash().ToString().substr(0,20).c_str(), pindexNew->GetBlockHash().ToString().substr(0,20).c_str());
+ if (vDisconnect.size() > 0) {
+ printf("REORGANIZE: Disconnect %"PRIszu" blocks; %s..%s\n", vDisconnect.size(), pfork->GetBlockHash().ToString().substr(0,20).c_str(), pindexBest->GetBlockHash().ToString().substr(0,20).c_str());
+ printf("REORGANIZE: Connect %"PRIszu" blocks; %s..%s\n", vConnect.size(), pfork->GetBlockHash().ToString().substr(0,20).c_str(), pindexNew->GetBlockHash().ToString().substr(0,20).c_str());
+ }
// Disconnect shorter branch
vector<CTransaction> vResurrect;
- BOOST_FOREACH(CBlockIndex* pindex, vDisconnect)
- {
+ BOOST_FOREACH(CBlockIndex* pindex, vDisconnect) {
CBlock block;
if (!block.ReadFromDisk(pindex))
- return error("Reorganize() : ReadFromDisk for disconnect failed");
- if (!block.DisconnectBlock(txdb, pindex))
- return error("Reorganize() : DisconnectBlock %s failed", pindex->GetBlockHash().ToString().substr(0,20).c_str());
+ return error("SetBestBlock() : ReadFromDisk for disconnect failed");
+ if (!block.DisconnectBlock(pindex, view))
+ return error("SetBestBlock() : DisconnectBlock %s failed", pindex->GetBlockHash().ToString().substr(0,20).c_str());
// Queue memory transactions to resurrect
BOOST_FOREACH(const CTransaction& tx, block.vtx)
- if (!(tx.IsCoinBase() || tx.IsCoinStake()))
+ if (!tx.IsCoinBase())
vResurrect.push_back(tx);
}
// Connect longer branch
vector<CTransaction> vDelete;
- for (unsigned int i = 0; i < vConnect.size(); i++)
- {
- CBlockIndex* pindex = vConnect[i];
+ BOOST_FOREACH(CBlockIndex *pindex, vConnect) {
CBlock block;
- if (!block.ReadFromDisk(pindex))
- return error("Reorganize() : ReadFromDisk for connect failed");
- if (!block.ConnectBlock(txdb, pindex))
- {
- // Invalid block
- return error("Reorganize() : ConnectBlock %s failed", pindex->GetBlockHash().ToString().substr(0,20).c_str());
+ CBlock *pblock;
+ if (pindex == pindexNew) // connecting *this block
+ pblock = this;
+ else { // other block; read it from disk
+ if (!block.ReadFromDisk(pindex))
+ return error("SetBestBlock() : ReadFromDisk for connect failed");
+ pblock = █
+ }
+ if (!pblock->ConnectBlock(pindex, view)) {
+ InvalidChainFound(pindexNew);
+ return error("SetBestBlock() : ConnectBlock %s failed", pindex->GetBlockHash().ToString().substr(0,20).c_str());
}
// Queue memory transactions to delete
- BOOST_FOREACH(const CTransaction& tx, block.vtx)
+ BOOST_FOREACH(const CTransaction& tx, pblock->vtx)
vDelete.push_back(tx);
}
- if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash()))
- return error("Reorganize() : WriteHashBestChain failed");
// Make sure it's successfully written to disk before changing memory structure
- if (!txdb.TxnCommit())
- return error("Reorganize() : TxnCommit failed");
+ if (!view.Flush())
+ return error("SetBestBlock() : failed to write coin changes");
+ if (!coinsdb.TxnCommit())
+ return error("SetBestBlock() : TxnCommit failed");
+ coinsdb.Close();
+
+ // At this point, all changes have been done to the database.
+ // Proceed by updating the memory structures.
// Disconnect shorter branch
BOOST_FOREACH(CBlockIndex* pindex, vDisconnect)
// Resurrect memory transactions that were in the disconnected branch
BOOST_FOREACH(CTransaction& tx, vResurrect)
- tx.AcceptToMemoryPool(txdb, false);
+ tx.AcceptToMemoryPool(coinsdb, false);
// Delete redundant memory transactions that are in the connected branch
BOOST_FOREACH(CTransaction& tx, vDelete)
mempool.remove(tx);
- printf("REORGANIZE: done\n");
-
- return true;
-}
-
-
-// Called from inside SetBestChain: attaches a block to the new best chain being built
-bool CBlock::SetBestChainInner(CTxDB& txdb, CBlockIndex *pindexNew)
-{
- uint256 hash = GetHash();
-
- // Adding to current best branch
- if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash))
- {
- txdb.TxnAbort();
- InvalidChainFound(pindexNew);
- return false;
- }
- if (!txdb.TxnCommit())
- return error("SetBestChain() : TxnCommit failed");
-
- // Add to current best branch
- pindexNew->pprev->pnext = pindexNew;
-
- // Delete redundant memory transactions
- BOOST_FOREACH(CTransaction& tx, vtx)
- mempool.remove(tx);
-
- return true;
-}
-
-bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
-{
- uint256 hash = GetHash();
-
- if (!txdb.TxnBegin())
- return error("SetBestChain() : TxnBegin failed");
-
- if (pindexGenesisBlock == NULL && hash == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet))
- {
- txdb.WriteHashBestChain(hash);
- if (!txdb.TxnCommit())
- return error("SetBestChain() : TxnCommit failed");
- pindexGenesisBlock = pindexNew;
- }
- else if (hashPrevBlock == hashBestChain)
- {
- if (!SetBestChainInner(txdb, pindexNew))
- return error("SetBestChain() : SetBestChainInner failed");
- }
- else
- {
- // the first block in the new chain that will cause it to become the new best chain
- CBlockIndex *pindexIntermediate = pindexNew;
-
- // list of blocks that need to be connected afterwards
- std::vector<CBlockIndex*> vpindexSecondary;
-
- // Reorganize is costly in terms of db load, as it works in a single db transaction.
- // Try to limit how much needs to be done inside
- while (pindexIntermediate->pprev && pindexIntermediate->pprev->nChainTrust > pindexBest->nChainTrust)
- {
- vpindexSecondary.push_back(pindexIntermediate);
- pindexIntermediate = pindexIntermediate->pprev;
- }
-
- if (!vpindexSecondary.empty())
- printf("Postponing %"PRIszu" reconnects\n", vpindexSecondary.size());
-
- // Switch to new best branch
- if (!Reorganize(txdb, pindexIntermediate))
- {
- txdb.TxnAbort();
- InvalidChainFound(pindexNew);
- return error("SetBestChain() : Reorganize failed");
- }
-
- // Connect further blocks
- BOOST_REVERSE_FOREACH(CBlockIndex *pindex, vpindexSecondary)
- {
- CBlock block;
- if (!block.ReadFromDisk(pindex))
- {
- printf("SetBestChain() : ReadFromDisk failed\n");
- break;
- }
- if (!txdb.TxnBegin()) {
- printf("SetBestChain() : TxnBegin 2 failed\n");
- break;
- }
- // errors now are not fatal, we still did a reorganisation to a new chain in a valid way
- if (!block.SetBestChainInner(txdb, pindex))
- break;
- }
- }
-
// Update best block in wallet (so we can detect restored wallets)
bool fIsInitialDownload = IsInitialBlockDownload();
if (!fIsInitialDownload)
}
// New best block
- hashBestChain = hash;
+ hashBestChain = pindexNew->GetBlockHash();
pindexBest = pindexNew;
pblockindexFBBHLast = NULL;
nBestHeight = pindexBest->nHeight;
return true;
}
-// ppcoin: total coin age spent in transaction, in the unit of coin-days.
+// Total coin age spent in transaction, in the unit of coin-days.
// Only those coins meeting minimum age requirement counts. As those
// transactions not in main chain are not currently indexed so we
// might not find out about their coin age. Older transactions are
// guaranteed to be in main chain by sync-checkpoint. This rule is
// introduced to help nodes establish a consistent view of the coin
// age (trust score) of competing branches.
-bool CTransaction::GetCoinAge(CTxDB& txdb, uint64& nCoinAge) const
+bool CTransaction::GetCoinAge(CCoinsView& inputs, uint64& nCoinAge) const
{
CBigNum bnCentSecond = 0; // coin age in the unit of cent-seconds
nCoinAge = 0;
if (IsCoinBase())
return true;
- BOOST_FOREACH(const CTxIn& txin, vin)
+ for (unsigned int i = 0; i < vin.size(); i++)
{
- // First try finding the previous transaction in database
- CTransaction txPrev;
- CTxIndex txindex;
- if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
- continue; // previous transaction not in main chain
- if (nTime < txPrev.nTime)
- return false; // Transaction timestamp violation
+ const COutPoint &prevout = vin[i].prevout;
+ CCoins coins;
+ if (!inputs.GetCoins(prevout.hash, coins))
+ continue;
- // Read block header
- CBlock block;
- if (!block.ReadFromDisk(txindex.pos.blockPos, false))
- return false; // unable to read block of previous transaction
- if (block.GetBlockTime() + nStakeMinAge > nTime)
- continue; // only count coins meeting min age requirement
+ if (nTime < coins.nTime)
+ return false; // Transaction timestamp violation
- int64 nValueIn = txPrev.vout[txin.prevout.n].nValue;
- bnCentSecond += CBigNum(nValueIn) * (nTime-txPrev.nTime) / CENT;
+ // only count coins meeting min age requirement
+ if (coins.nBlockTime + nStakeMinAge > nTime)
+ continue;
- if (fDebug && GetBoolArg("-printcoinage"))
- printf("coin age nValueIn=%"PRI64d" nTimeDiff=%d bnCentSecond=%s\n", nValueIn, nTime - txPrev.nTime, bnCentSecond.ToString().c_str());
+ int64 nValueIn = coins.vout[vin[i].prevout.n].nValue;
+ bnCentSecond += CBigNum(nValueIn) * (nTime-coins.nTime) / CENT;
}
CBigNum bnCoinDay = bnCentSecond * CENT / COIN / (24 * 60 * 60);
return true;
}
-// ppcoin: total coin age spent in block, in the unit of coin-days.
+// Total coin age spent in block, in the unit of coin-days.
bool CBlock::GetCoinAge(uint64& nCoinAge) const
{
nCoinAge = 0;
- CTxDB txdb("r");
+ CCoinsDB coindb("r");
+ CCoinsViewDB view(coindb);
+
BOOST_FOREACH(const CTransaction& tx, vtx)
{
uint64 nTxCoinAge;
- if (tx.GetCoinAge(txdb, nTxCoinAge))
+ if (tx.GetCoinAge(view, nTxCoinAge))
nCoinAge += nTxCoinAge;
else
return false;
CBlockIndex* pindexNew = new CBlockIndex(*this);
if (!pindexNew)
return error("AddToBlockIndex() : new CBlockIndex failed");
- pindexNew->phashBlock = &hash;
+ map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
+ pindexNew->phashBlock = &((*mi).first);
map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(hashPrevBlock);
if (miPrev != mapBlockIndex.end())
{
pindexNew->pprev = (*miPrev).second;
pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
}
-
- // ppcoin: compute chain trust score
pindexNew->nChainTrust = (pindexNew->pprev ? pindexNew->pprev->nChainTrust : 0) + pindexNew->GetBlockTrust();
+ pindexNew->pos = pos;
+ pindexNew->nUndoPos = 0;
- assert(pos.nHeight == pindexNew->nHeight);
- pindexNew->nAlternative = pos.nAlternative;
-
- // ppcoin: compute stake entropy bit for stake modifier
+ // Compute stake entropy bit for stake modifier
if (!pindexNew->SetStakeEntropyBit(GetStakeEntropyBit(pindexNew->nTime)))
return error("AddToBlockIndex() : SetStakeEntropyBit() failed");
- // ppcoin: record proof-of-stake hash value
+ // Record proof-of-stake hash value
if (pindexNew->IsProofOfStake())
{
if (!mapProofOfStake.count(hash))
pindexNew->hashProofOfStake = mapProofOfStake[hash];
}
- // ppcoin: compute stake modifier
+ // Compute stake modifier
uint64 nStakeModifier = 0;
bool fGeneratedStakeModifier = false;
if (!ComputeNextStakeModifier(pindexNew->pprev, nStakeModifier, fGeneratedStakeModifier))
if (!CheckStakeModifierCheckpoints(pindexNew->nHeight, pindexNew->nStakeModifierChecksum))
return error("AddToBlockIndex() : Rejected by stake modifier checkpoint height=%d, modifier=0x%016"PRI64x, pindexNew->nHeight, nStakeModifier);
- // Add to mapBlockIndex
- map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
- if (pindexNew->IsProofOfStake())
- setStakeSeen.insert(make_pair(pindexNew->prevoutStake, pindexNew->nStakeTime));
- pindexNew->phashBlock = &((*mi).first);
-
- // Write to disk block index
- CTxDB txdb;
- if (!txdb.TxnBegin())
+ CChainDB chaindb;
+ if (!chaindb.TxnBegin())
return false;
- txdb.WriteBlockIndex(CDiskBlockIndex(pindexNew));
- if (!txdb.TxnCommit())
+ chaindb.WriteBlockIndex(CDiskBlockIndex(pindexNew));
+ if (!chaindb.TxnCommit())
return false;
// New best
- if (pindexNew->nChainTrust > nBestChainTrust)
- if (!SetBestChain(txdb, pindexNew))
- return false;
+ if (pindexNew->nChainTrust > nBestChainTrust) {
+ if (!IsInitialBlockDownload() || (pindexNew->nHeight % 1) == 0)
+ if (!SetBestChain(pindexNew))
+ return false;
+ }
if (pindexNew == pindexBest)
{
return true;
}
+bool FindBlockPos(CChainDB &chaindb, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64 nTime)
+{
+ bool fUpdatedLast = false;
+
+ LOCK(cs_LastBlockFile);
+
+ while (infoLastBlockFile.nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
+ printf("Leaving block file %i: %s\n", nLastBlockFile, infoLastBlockFile.ToString().c_str());
+ FILE *file = OpenBlockFile(pos);
+ FileCommit(file);
+ fclose(file);
+ file = OpenUndoFile(pos);
+ FileCommit(file);
+ fclose(file);
+ nLastBlockFile++;
+ infoLastBlockFile.SetNull();
+ chaindb.ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile); // check whether data for the new file somehow already exist; can fail just fine
+ fUpdatedLast = true;
+ }
+
+ pos.nFile = nLastBlockFile;
+ pos.nPos = infoLastBlockFile.nSize;
+ infoLastBlockFile.nSize += nAddSize;
+ infoLastBlockFile.AddBlock(nHeight, nTime);
+
+ unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
+ unsigned int nNewChunks = (infoLastBlockFile.nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
+ if (nNewChunks > nOldChunks) {
+ FILE *file = OpenBlockFile(pos);
+ if (file) {
+ printf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
+ AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
+ }
+ fclose(file);
+ }
+
+ if (!chaindb.WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
+ return error("FindBlockPos() : cannot write updated block info");
+ if (fUpdatedLast)
+ chaindb.WriteLastBlockFile(nLastBlockFile);
+
+ return true;
+}
+
+bool FindUndoPos(CChainDB &chaindb, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
+{
+ pos.nFile = nFile;
+ LOCK(cs_LastBlockFile);
+ unsigned int nNewSize;
+ if (nFile == nLastBlockFile) {
+ pos.nPos = infoLastBlockFile.nUndoSize;
+ nNewSize = (infoLastBlockFile.nUndoSize += nAddSize);
+ if (!chaindb.WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
+ return error("FindUndoPos() : cannot write updated block info");
+ } else {
+ CBlockFileInfo info;
+ if (!chaindb.ReadBlockFileInfo(nFile, info))
+ return error("FindUndoPos() : cannot read block info");
+ pos.nPos = info.nUndoSize;
+ nNewSize = (info.nUndoSize += nAddSize);
+ if (!chaindb.WriteBlockFileInfo(nFile, info))
+ return error("FindUndoPos() : cannot write updated block info");
+ }
+
+ unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
+ unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
+ if (nNewChunks > nOldChunks) {
+ FILE *file = OpenUndoFile(pos);
+ if (file) {
+ printf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
+ AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
+ }
+ fclose(file);
+ }
+
+ return true;
+}
bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) const
{
return DoS(50, error("CheckBlock() : proof of work failed"));
// Check timestamp
- if (GetBlockTime() > FutureDrift(GetAdjustedTime()))
+ if (GetBlockTime() > GetAdjustedTime() + 2 * 60 * 60)
return error("CheckBlock() : block timestamp too far in the future");
// First transaction must be coinbase, the rest must not be
}
}
+
// Check transactions
BOOST_FOREACH(const CTransaction& tx, vtx)
{
if (!tx.CheckTransaction())
return DoS(tx.nDoS, error("CheckBlock() : CheckTransaction failed"));
- // ppcoin: check transaction timestamp
+ // check transaction timestamp
if (GetBlockTime() < (int64)tx.nTime)
return DoS(50, error("CheckBlock() : block timestamp earlier than transaction timestamp"));
}
if (fCheckMerkleRoot && hashMerkleRoot != BuildMerkleTree())
return DoS(100, error("CheckBlock() : hashMerkleRoot mismatch"));
-
return true;
}
+
bool CBlock::AcceptBlock()
{
// Check for duplicate
return DoS(100, error("AcceptBlock() : block height mismatch in coinbase"));
// Write block to history file
- CDiskBlockPos blockPos = CDiskBlockPos(nHeight);
+ unsigned int nBlockSize = ::GetSerializeSize(*this, SER_DISK, CLIENT_VERSION);
if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK, CLIENT_VERSION)))
return error("AcceptBlock() : out of disk space");
+ CDiskBlockPos blockPos;
+ {
+ CChainDB chaindb;
+ if (!FindBlockPos(chaindb, blockPos, nBlockSize+8, nHeight, nTime))
+ return error("AcceptBlock() : FindBlockPos failed");
+ }
if (!WriteToDisk(blockPos))
return error("AcceptBlock() : WriteToDisk failed");
if (!AddToBlockIndex(blockPos))
pnode->PushInventory(CInv(MSG_BLOCK, hash));
}
- // ppcoin: check pending sync-checkpoint
+ // Check pending sync-checkpoint
Checkpoints::AcceptPendingSyncCheckpoint();
return true;
}
-FILE* OpenBlockFile(const CDiskBlockPos &pos, const char* pszMode)
+CCriticalSection cs_LastBlockFile;
+CBlockFileInfo infoLastBlockFile;
+int nLastBlockFile = 0;
+
+FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
{
- boost::filesystem::path path = pos.GetFileName(GetDataDir());
- boost::filesystem::create_directories(path.parent_path());
- if (pos.IsNull() || pos.IsMemPool())
+ if (pos.IsNull())
return NULL;
- FILE* file = fopen(path.string().c_str(), pszMode);
- if (!file)
+ boost::filesystem::path path = GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
+ boost::filesystem::create_directories(path.parent_path());
+ FILE* file = fopen(path.string().c_str(), "rb+");
+ if (!file && !fReadOnly)
+ file = fopen(path.string().c_str(), "wb+");
+ if (!file) {
+ printf("Unable to open file %s\n", path.string().c_str());
return NULL;
+ }
+ if (pos.nPos) {
+ if (fseek(file, pos.nPos, SEEK_SET)) {
+ printf("Unable to seek to position %u of %s\n", pos.nPos, path.string().c_str());
+ fclose(file);
+ return NULL;
+ }
+ }
return file;
}
+FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
+ return OpenDiskFile(pos, "blk", fReadOnly);
+}
+
+FILE *OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
+ return OpenDiskFile(pos, "rev", fReadOnly);
+}
+
bool LoadBlockIndex(bool fAllowNew)
{
CBigNum bnTrustedModulus;
//
// Load block index
//
- CTxDB txdb("cr+");
- if (!txdb.LoadBlockIndex())
+ CChainDB chaindb("cr");
+ CCoinsDB coinsdb("cr");
+ if (!LoadBlockIndex(coinsdb, chaindb))
return false;
+ chaindb.Close();
+ coinsdb.Close();
//
// Init with genesis block
assert(block.CheckBlock());
// Start new block file
- CDiskBlockPos blockPos(0);
+ unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
+ CDiskBlockPos blockPos;
+ {
+ CChainDB chaindb;
+ if (!FindBlockPos(chaindb, blockPos, nBlockSize+8, 0, block.nTime))
+ return error("AcceptBlock() : FindBlockPos failed");
+ }
if (!block.WriteToDisk(blockPos))
return error("LoadBlockIndex() : writing genesis block to disk failed");
if (!block.AddToBlockIndex(blockPos))
return error("LoadBlockIndex() : genesis block not accepted");
- // ppcoin: initialize synchronized checkpoint
+ // initialize synchronized checkpoint
if (!Checkpoints::WriteSyncCheckpoint((!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet)))
return error("LoadBlockIndex() : failed to init sync checkpoint");
}
string strPubKey = "";
-
- // if checkpoint master key changed must reset sync-checkpoint
- if (!txdb.ReadCheckpointPubKey(strPubKey) || strPubKey != CSyncCheckpoint::strMasterPubKey)
{
- // write checkpoint master key to db
- txdb.TxnBegin();
- if (!txdb.WriteCheckpointPubKey(CSyncCheckpoint::strMasterPubKey))
- return error("LoadBlockIndex() : failed to write new checkpoint master key to db");
- if (!txdb.TxnCommit())
- return error("LoadBlockIndex() : failed to commit new checkpoint master key to db");
- if ((!fTestNet) && !Checkpoints::ResetSyncCheckpoint())
- return error("LoadBlockIndex() : failed to reset sync-checkpoint");
+ CChainDB chaindb;
+ // if checkpoint master key changed must reset sync-checkpoint
+ if (!chaindb.ReadCheckpointPubKey(strPubKey) || strPubKey != CSyncCheckpoint::strMasterPubKey)
+ {
+ // write checkpoint master key to db
+ chaindb.TxnBegin();
+ if (!chaindb.WriteCheckpointPubKey(CSyncCheckpoint::strMasterPubKey))
+ return error("LoadBlockIndex() : failed to write new checkpoint master key to db");
+ if (!chaindb.TxnCommit())
+ return error("LoadBlockIndex() : failed to commit new checkpoint master key to db");
+ if ((!fTestNet) && !Checkpoints::ResetSyncCheckpoint())
+ return error("LoadBlockIndex() : failed to reset sync-checkpoint");
+ }
}
return true;
}
-
-
void PrintBlockTree()
{
// pre-compute tree structure
// print item
CBlock block;
block.ReadFromDisk(pindex);
- printf("%d (%s) %s %08x %s mint %7s tx %"PRIszu"",
+ printf("%d (blk%05u.dat:0x%x) %s tx %"PRIszu"",
pindex->nHeight,
- pindex->GetBlockPos().GetFileName("").string().c_str(),
- block.GetHash().ToString().c_str(),
- block.nBits,
+ pindex->GetBlockPos().nFile, pindex->GetBlockPos().nPos,
DateTimeStrFormat("%x %H:%M:%S", block.GetBlockTime()).c_str(),
- FormatMoney(pindex->nMint).c_str(),
block.vtx.size());
PrintWallets(block);
//
-bool static AlreadyHave(CTxDB& txdb, const CInv& inv)
+bool static AlreadyHave(CCoinsDB &coinsdb, const CInv& inv)
{
switch (inv.type)
{
case MSG_TX:
{
- bool txInMap = false;
+ bool txInMap = false;
{
- LOCK(mempool.cs);
- txInMap = (mempool.exists(inv.hash));
+ LOCK(mempool.cs);
+ txInMap = mempool.exists(inv.hash);
}
- return txInMap ||
- mapOrphanTransactions.count(inv.hash) ||
- txdb.ContainsTx(inv.hash);
+ return txInMap || mapOrphanTransactions.count(inv.hash) ||
+ coinsdb.HaveCoins(inv.hash);
}
-
case MSG_BLOCK:
return mapBlockIndex.count(inv.hash) ||
mapOrphanBlocks.count(inv.hash);
break;
}
}
- CTxDB txdb("r");
+ CCoinsDB coinsdb("r");
for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
{
const CInv &inv = vInv[nInv];
return true;
pfrom->AddInventoryKnown(inv);
- bool fAlreadyHave = AlreadyHave(txdb, inv);
+ bool fAlreadyHave = AlreadyHave(coinsdb, inv);
if (fDebug)
printf(" got inventory: %s %s\n", inv.ToString().c_str(), fAlreadyHave ? "have" : "new");
vector<uint256> vWorkQueue;
vector<uint256> vEraseQueue;
CDataStream vMsg(vRecv);
- CTxDB txdb("r");
+ CCoinsDB coinsdb("r");
CTransaction tx;
vRecv >> tx;
pfrom->AddInventoryKnown(inv);
bool fMissingInputs = false;
- if (tx.AcceptToMemoryPool(txdb, true, &fMissingInputs))
+ if (tx.AcceptToMemoryPool(coinsdb, true, &fMissingInputs))
{
SyncWithWallets(tx, NULL, true);
RelayTransaction(tx, inv.hash);
CTransaction& orphanTx = mapOrphanTransactions[orphanTxHash];
bool fMissingInputs2 = false;
- if (orphanTx.AcceptToMemoryPool(txdb, true, &fMissingInputs2))
+ if (orphanTx.AcceptToMemoryPool(coinsdb, true, &fMissingInputs2))
{
printf(" accepted orphan tx %s\n", orphanTxHash.ToString().substr(0,10).c_str());
SyncWithWallets(tx, NULL, true);
//
vector<CInv> vGetData;
int64 nNow = GetTime() * 1000000;
- CTxDB txdb("r");
+ CCoinsDB coinsdb("r");
while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
{
const CInv& inv = (*pto->mapAskFor.begin()).second;
- if (!AlreadyHave(txdb, inv))
+ if (!AlreadyHave(coinsdb, inv))
{
if (fDebugNet)
printf("sending getdata: %s\n", inv.ToString().c_str());
static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
static const unsigned int MAX_ORPHAN_TRANSACTIONS = MAX_BLOCK_SIZE/100;
static const unsigned int MAX_INV_SZ = 50000;
+
+static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
+static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
+static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
+static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
+
static const int64 MIN_TX_FEE = CENT;
static const int64 MIN_RELAY_TX_FEE = CENT;
static const int64 MAX_MONEY = 2000000000 * COIN;
static const uint64 nMinDiskSpace = 52428800;
class CReserveKey;
-class CTxDB;
-class CTxIndex;
+class CCoinsDB;
+class CChainDB;
class CDiskBlockPos;
+class CCoins;
+class CTxUndo;
+class CCoinsView;
void RegisterWallet(CWallet* pwalletIn);
void UnregisterWallet(CWallet* pwalletIn);
void SyncWithWallets(const CTransaction& tx, const CBlock* pblock = NULL, bool fUpdate = false, bool fConnect = true);
bool ProcessBlock(CNode* pfrom, CBlock* pblock);
bool CheckDiskSpace(uint64 nAdditionalBytes=0);
-FILE* OpenBlockFile(const CDiskBlockPos &pos, const char* pszMode="rb");
+FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
+FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
bool LoadBlockIndex(bool fAllowNew=true);
void PrintBlockTree();
CBlockIndex* FindBlockByHeight(int nHeight);
int GetNumBlocksOfPeers();
bool IsInitialBlockDownload();
std::string GetWarnings(std::string strFor);
-bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock);
+bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, bool fAllowSlow);
uint256 WantedByOrphan(const CBlock* pblockOrphan);
const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfStake);
void StakeMiner(CWallet *pwallet);
class CDiskBlockPos
{
public:
- int nHeight;
- int nAlternative;
-
- CDiskBlockPos() {
- SetNull();
- }
-
- CDiskBlockPos(int nHeightIn, int nAlternativeIn = 0) {
- nHeight = nHeightIn;
- nAlternative = nAlternativeIn;
- }
-
- std::string GetAlternative() const {
- char c[9]={0,0,0,0,0,0,0,0,0};
- char *cp = &c[8];
- unsigned int n = nAlternative;
- while (n > 0 && cp>c) {
- n--;
- *(--cp) = 'a' + (n % 26);
- n /= 26;
- }
- return std::string(cp);
- }
-
- boost::filesystem::path GetDirectory(const boost::filesystem::path &base) const {
- assert(nHeight != -1);
- return base / strprintf("era%02u", nHeight / 210000) /
- strprintf("cycle%04u", nHeight / 2016);
- }
-
- boost::filesystem::path GetFileName(const boost::filesystem::path &base) const {
- return GetDirectory(base) / strprintf("%08u%s.blk", nHeight, GetAlternative().c_str());
- }
-
- boost::filesystem::path GetUndoFile(const boost::filesystem::path &base) const {
- return GetDirectory(base) / strprintf("%08u%s.und", nHeight, GetAlternative().c_str());
- }
-
- // TODO: make thread-safe (lockfile, atomic file creation, ...?)
- void MakeUnique(const boost::filesystem::path &base) {
- while (boost::filesystem::exists(GetFileName(base)))
- nAlternative++;
- }
-
- IMPLEMENT_SERIALIZE(({
- CDiskBlockPos *me = const_cast<CDiskBlockPos*>(this);
- if (!fRead) {
- unsigned int nCode = (nHeight + 1) * 2 + (nAlternative > 0);
- READWRITE(VARINT(nCode));
- if (nAlternative > 0) {
- unsigned int nAlt = nAlternative - 1;
- READWRITE(VARINT(nAlt));
- }
- } else {
- unsigned int nCode = 0;
- READWRITE(VARINT(nCode));
- me->nHeight = (nCode / 2) - 1;
- if (nCode & 1) {
- unsigned int nAlt = 0;
- READWRITE(VARINT(nAlt));
- me->nAlternative = 1 + nAlt;
- } else {
- me->nAlternative = 0;
- }
- }
- });)
-
- friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
- return ((a.nHeight == b.nHeight) && (a.nAlternative == b.nAlternative));
- }
-
- friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
- return !(a == b);
- }
-
- void SetNull() { nHeight = -1; nAlternative = 0; }
- bool IsNull() const { return ((nHeight == -1) && (nAlternative == 0)); }
-
- void SetMemPool() { nHeight = -1; nAlternative = -1; }
- bool IsMemPool() const { return ((nHeight == -1) && (nAlternative == -1)); }
-};
-
-
-/** Position on disk for a particular transaction. */
-class CDiskTxPos
-{
-public:
- CDiskBlockPos blockPos;
- unsigned int nTxPos;
-
- CDiskTxPos(bool fInMemPool = false)
- {
- SetNull();
- if (fInMemPool)
- blockPos.SetMemPool();
-
- }
-
- CDiskTxPos(const CDiskBlockPos &block, unsigned int nTxPosIn) : blockPos(block), nTxPos(nTxPosIn) { }
+ int nFile;
+ unsigned int nPos;
IMPLEMENT_SERIALIZE(
- READWRITE(blockPos);
- READWRITE(VARINT(nTxPos));
+ READWRITE(VARINT(nFile));
+ READWRITE(VARINT(nPos));
)
- void SetNull() { blockPos.SetNull(); nTxPos = 0; }
- bool IsNull() const { return blockPos.IsNull(); }
- bool IsMemPool() const { return blockPos.IsMemPool(); }
-
- friend bool operator==(const CDiskTxPos& a, const CDiskTxPos& b)
- {
- return (a.blockPos == b.blockPos &&
- a.nTxPos == b.nTxPos);
+ friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
+ return (a.nFile == b.nFile && a.nPos == b.nPos);
}
- friend bool operator!=(const CDiskTxPos& a, const CDiskTxPos& b)
- {
+ friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
return !(a == b);
}
-
- std::string ToString() const
- {
- if (IsNull())
- return "null";
- else if (blockPos.IsMemPool())
- return "mempool";
- else
- return strprintf("(%s, nTxPos=%u)", blockPos.GetFileName("").string().c_str(), nTxPos);
- }
-
- void print() const
- {
- printf("%s", ToString().c_str());
- }
+ void SetNull() { nFile = -1; nPos = 0; }
+ bool IsNull() const { return (nFile == -1); }
};
-
/** An inpoint - a combination of a transaction and an index n into its vin */
class CInPoint
{
std::string ToStringShort() const
{
- return strprintf(" out %s %s", FormatMoney(nValue).c_str(), scriptPubKey.ToString(true).c_str());
+ return strprintf(" out %s %s", FormatMoney(nValue).c_str(), scriptPubKey.ToString().substr(0, 10).c_str());
}
std::string ToString() const
GMF_SEND,
};
-typedef std::map<uint256, std::pair<CTxIndex, CTransaction> > MapPrevTx;
+// Modes for script/signature checking
+enum CheckSig_mode
+{
+ CS_NEVER, // never validate scripts
+ CS_AFTER_CHECKPOINT, // validate scripts after the last checkpoint
+ CS_ALWAYS // always validate scripts
+};
/** The basic transaction that is broadcasted on the network and contained in
* blocks. A transaction can contain multiple inputs and outputs.
@return True if all inputs (scriptSigs) use only standard transaction forms
@see CTransaction::FetchInputs
*/
- bool AreInputsStandard(const MapPrevTx& mapInputs) const;
+ bool AreInputsStandard(CCoinsView& mapInputs) const;
/** Count ECDSA signature operations the old-fashioned (pre-0.6) way
@return number of sigops this transaction's outputs will produce when spent
@return maximum number of sigops required to validate this transaction's inputs
@see CTransaction::FetchInputs
*/
- unsigned int GetP2SHSigOpCount(const MapPrevTx& mapInputs) const;
+ unsigned int GetP2SHSigOpCount(CCoinsView& mapInputs) const;
/** Amount of bitcoins spent by this transaction.
@return sum of all outputs (note: does not include fees)
@return Sum of value of all inputs (scriptSigs)
@see CTransaction::FetchInputs
*/
- int64 GetValueIn(const MapPrevTx& mapInputs) const;
+ int64 GetValueIn(CCoinsView& mapInputs) const;
static bool AllowFree(double dPriority)
{
int64 GetMinFee(unsigned int nBlockSize=1, bool fAllowFree=false, enum GetMinFee_mode mode=GMF_BLOCK, unsigned int nBytes = 0) const;
- bool ReadFromDisk(CDiskTxPos pos, FILE** pfileRet=NULL)
- {
- CAutoFile filein = CAutoFile(OpenBlockFile(pos.blockPos, pfileRet ? "rb+" : "rb"), SER_DISK, CLIENT_VERSION);
- if (!filein)
- return error("CTransaction::ReadFromDisk() : OpenBlockFile failed");
-
- // Read transaction
- if (fseek(filein, pos.nTxPos, SEEK_SET) != 0)
- return error("CTransaction::ReadFromDisk() : fseek failed");
-
- try {
- filein >> *this;
- }
- catch (std::exception &e) {
- return error("%s() : deserialize or I/O error", __PRETTY_FUNCTION__);
- }
-
- // Return file pointer
- if (pfileRet)
- {
- if (fseek(filein, pos.nTxPos, SEEK_SET) != 0)
- return error("CTransaction::ReadFromDisk() : second fseek failed");
- *pfileRet = filein.release();
- }
- return true;
- }
-
friend bool operator==(const CTransaction& a, const CTransaction& b)
{
return (a.nVersion == b.nVersion &&
}
- bool ReadFromDisk(CTxDB& txdb, COutPoint prevout, CTxIndex& txindexRet);
- bool ReadFromDisk(CTxDB& txdb, COutPoint prevout);
- bool ReadFromDisk(COutPoint prevout);
- bool DisconnectInputs(CTxDB& txdb);
+ // Do all possible client-mode checks
+ bool ClientCheckInputs() const;
- /** Fetch from memory and/or disk. inputsRet keys are transaction hashes.
+ // Check whether all prevouts of this transaction are present in the UTXO set represented by view
+ bool HaveInputs(CCoinsView &view) const;
- @param[in] txdb Transaction database
- @param[in] mapTestPool List of pending changes to the transaction index database
- @param[in] fBlock True if being called to add a new best-block to the chain
- @param[in] fMiner True if being called by CreateNewBlock
- @param[out] inputsRet Pointers to this transaction's inputs
- @param[out] fInvalid returns true if transaction is invalid
- @return Returns true if all inputs are in txdb or mapTestPool
- */
- bool FetchInputs(CTxDB& txdb, const std::map<uint256, CTxIndex>& mapTestPool,
- bool fBlock, bool fMiner, MapPrevTx& inputsRet, bool& fInvalid);
-
- /** Sanity check previous transactions, then, if all checks succeed,
- mark them as spent by this transaction.
-
- @param[in] inputs Previous transactions (from FetchInputs)
- @param[out] mapTestPool Keeps track of inputs that need to be updated on disk
- @param[in] posThisTx Position of this transaction on disk
- @param[in] pindexBlock
- @param[in] fBlock true if called from ConnectBlock
- @param[in] fMiner true if called from CreateNewBlock
- @param[in] fStrictPayToScriptHash true if fully validating p2sh transactions
- @return Returns true if all checks succeed
- */
- bool ConnectInputs(CTxDB& txdb, MapPrevTx inputs,
- std::map<uint256, CTxIndex>& mapTestPool, const CDiskTxPos& posThisTx,
- const CBlockIndex* pindexBlock, bool fBlock, bool fMiner, bool fStrictPayToScriptHash=true);
- bool ClientConnectInputs();
+ // Check whether all inputs of this transaction are valid (no double spends, scripts & sigs, amounts)
+ // This does not modify the UTXO set
+ bool CheckInputs(CCoinsView &view, enum CheckSig_mode csmode, bool fStrictPayToScriptHash=true, bool fStrictEncodings=true, CBlock *pblock=NULL) const;
+
+ // Apply the effects of this transaction on the UTXO set represented by view
+ bool UpdateCoins(CCoinsView &view, CTxUndo &txundo, int nHeight, unsigned int nBlockTime) const;
+
+ // Context-independent validity checks
bool CheckTransaction() const;
- bool AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs=true, bool* pfMissingInputs=NULL);
- bool GetCoinAge(CTxDB& txdb, uint64& nCoinAge) const; // ppcoin: get transaction coin age
+ // Try to accept this transaction into the memory pool
+ bool AcceptToMemoryPool(CCoinsDB& coinsdb, bool fCheckInputs=true, bool* pfMissingInputs=NULL);
+ bool GetCoinAge(CCoinsView& inputs, uint64& nCoinAge) const; // Get transaction coin age
protected:
- const CTxOut& GetOutputFor(const CTxIn& input, const MapPrevTx& inputs) const;
+ static CTxOut GetOutputFor(const CTxIn& input, CCoinsView& mapInputs);
};
class CTxInUndo
{
public:
- CTxOut txout; // the txout data before being spent
- bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase
- unsigned int nHeight; // if the outpoint was the last unspent: its height
- int nVersion; // if the outpoint was the last unspent: its version
+ CTxOut txout; // the txout data before being spent
+ bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase
+ bool fCoinStake; // if the outpoint was the last unspent: whether it belonged to a coinstake
+ unsigned int nHeight; // if the outpoint was the last unspent: its height
+ int nVersion; // if the outpoint was the last unspent: its version
+ unsigned int nTime; // if the outpoint was the last unspent: its timestamp
+ unsigned int nBlockTime; // if the outpoint was the last unspent: its block timestamp
- CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {}
- CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { }
+ CTxInUndo() : txout(), fCoinBase(false), fCoinStake(false), nHeight(0), nVersion(0), nTime(0), nBlockTime(0) {}
+ CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, bool fCoinStakeIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0, int nTimeIn = 0, int nBlockTimeIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), fCoinStake(fCoinStakeIn), nHeight(nHeightIn), nVersion(nVersionIn), nTime(nTimeIn), nBlockTime(nBlockTimeIn) { }
unsigned int GetSerializeSize(int nType, int nVersion) const {
return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) +
+ ::GetSerializeSize(VARINT(nTime*2+(fCoinStake ? 1 : 0)), nType, nVersion) +
+ ::GetSerializeSize(VARINT(nBlockTime), nType, nVersion) +
(nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) +
::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion);
}
template<typename Stream>
void Serialize(Stream &s, int nType, int nVersion) const {
::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);
+ ::Serialize(s, VARINT(nTime*2+(fCoinStake ? 1 : 0)), nType, nVersion);
+ ::Serialize(s, VARINT(nBlockTime), nType, nVersion);
if (nHeight > 0)
::Serialize(s, VARINT(this->nVersion), nType, nVersion);
::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion);
template<typename Stream>
void Unserialize(Stream &s, int nType, int nVersion) {
- unsigned int nCode = 0;
- ::Unserialize(s, VARINT(nCode), nType, nVersion);
- nHeight = nCode / 2;
- fCoinBase = nCode & 1;
+ unsigned int nCodeHeight = 0, nCodeTime = 0;
+ ::Unserialize(s, VARINT(nCodeHeight), nType, nVersion);
+ nHeight = nCodeHeight / 2;
+ fCoinBase = nCodeHeight & 1;
+ ::Unserialize(s, VARINT(nCodeTime), nType, nVersion);
+ nTime = nCodeTime / 2;
+ fCoinStake = nCodeTime & 1;
+ ::Unserialize(s, VARINT(nBlockTime), nType, nVersion);
if (nHeight > 0)
::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion);
class CTxUndo
{
public:
+ // undo information for all txins
std::vector<CTxInUndo> vprevout;
IMPLEMENT_SERIALIZE(
class CBlockUndo
{
public:
- std::vector<CTxUndo> vtxundo;
+ std::vector<CTxUndo> vtxundo; // for all but the coinbase
IMPLEMENT_SERIALIZE(
READWRITE(vtxundo);
)
+
+ bool WriteToDisk(CDiskBlockPos &pos)
+ {
+ // Open history file to append
+ CAutoFile fileout = CAutoFile(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
+ if (!fileout)
+ return error("CBlockUndo::WriteToDisk() : OpenUndoFile failed");
+
+ // Write index header
+ unsigned int nSize = fileout.GetSerializeSize(*this);
+ fileout << FLATDATA(pchMessageStart) << nSize;
+
+ // Write undo data
+ long fileOutPos = ftell(fileout);
+ if (fileOutPos < 0)
+ return error("CBlockUndo::WriteToDisk() : ftell failed");
+ pos.nPos = (unsigned int)fileOutPos;
+ fileout << *this;
+
+ // Flush stdio buffers and commit to disk before returning
+ fflush(fileout);
+ if (!IsInitialBlockDownload())
+ FileCommit(fileout);
+
+ return true;
+ }
+
};
/** pruned version of CTransaction: only retains metadata and unspent transaction outputs
* - unspentness bitvector, for vout[2] and further; least significant byte first
* - the non-spent CTxOuts (via CTxOutCompressor)
* - VARINT(nHeight)
+ * - VARINT(nTime + is_coinstake)
+ * - VARINT(nBlockTime)
*
* The nCode value consists of:
* - bit 1: IsCoinBase()
* - In case both bit 2 and bit 4 are unset, they encode N-1, as there must be at
* least one non-spent output).
*
- * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e
- * <><><--------------------------------------------><---->
- * | \ | /
- * version code vout[1] height
+ * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e40f1d75240f1d752
+ * <><><--------------------------------------------><----><------><------>
+ * | \ | / / /
+ * version code vout[1] height timestamp block timestamp
*
* - version = 1
* - code = 4 (vout[1] is not spent, and 0 non-zero bytes of bitvector follow)
* * 00: special txout type pay-to-pubkey-hash
* * 816115944e077fe7c803cfa57f29b36bf87c1d35: address uint160
* - height = 203998
+ * - time = 1389883712
+ * - is_coinstake = 0
+ * - block time = 1389883712
*
*
- * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b
- * <><><--><--------------------------------------------------><----------------------------------------------><---->
- * / \ \ | | /
- * version code unspentness vout[4] vout[16] height
+ * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b40f1d75240f1d752
+ * <><><--><--------------------------------------------------><----------------------------------------------><----><------><------>
+ * / \ \ | | / / /
+ * version code unspentness vout[4] vout[16] height timestamp block timestamp
*
* - version = 1
* - code = 9 (coinbase, neither vout[0] or vout[1] are unspent,
* * 00: special txout type pay-to-pubkey-hash
* * 8c988f1a4a4de2161e0f50aac7f17e7f9555caa4: address uint160
* - height = 120891
+ * - time = 1389883712
+ * - is_coinstake = 0
+ * - block time = 1389883712
*/
class CCoins
{
// whether transaction is a coinbase
bool fCoinBase;
+ // whether transaction is a coinstake
+ bool fCoinStake;
+
// unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped
std::vector<CTxOut> vout;
// as new tx version will probably only be introduced at certain heights
int nVersion;
- // construct a CCoins from a CTransaction, at a given height
- CCoins(const CTransaction &tx, int nHeightIn) : fCoinBase(tx.IsCoinBase()), vout(tx.vout), nHeight(nHeightIn), nVersion(tx.nVersion) { }
+ // transaction timestamp + coinstake flag
+ unsigned int nTime;
+
+ // block timestamp
+ unsigned int nBlockTime;
+
+ // construct a CCoins from a CTransaction, at a given height/timestamp
+ CCoins(const CTransaction &tx, int nHeightIn, int nBlockTimeIn) : fCoinBase(tx.IsCoinBase()), fCoinStake(tx.IsCoinStake()), vout(tx.vout), nHeight(nHeightIn), nVersion(tx.nVersion), nTime(tx.nTime), nBlockTime(nBlockTimeIn) { }
// empty constructor
- CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { }
+ CCoins() : fCoinBase(false), fCoinStake(false), vout(0), nHeight(0), nVersion(0), nTime(0), nBlockTime(0) { }
// remove spent outputs at the end of vout
void Cleanup() {
// equality test
friend bool operator==(const CCoins &a, const CCoins &b) {
- return a.fCoinBase == b.fCoinBase &&
+ return a.fCoinBase == b.fCoinBase &&
+ a.fCoinStake == b.fCoinStake &&
a.nHeight == b.nHeight &&
a.nVersion == b.nVersion &&
+ a.nTime == b.nTime &&
+ a.nBlockTime == b.nBlockTime &&
a.vout == b.vout;
}
friend bool operator!=(const CCoins &a, const CCoins &b) {
return fCoinBase;
}
+ bool IsCoinStake() const {
+ return fCoinStake;
+ }
+
unsigned int GetSerializeSize(int nType, int nVersion) const {
unsigned int nSize = 0;
unsigned int nMaskSize = 0, nMaskCode = 0;
bool fFirst = vout.size() > 0 && !vout[0].IsNull();
bool fSecond = vout.size() > 1 && !vout[1].IsNull();
assert(fFirst || fSecond || nMaskCode);
- unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
+ unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fCoinStake ? 1 : 0) + (fSecond ? 4 : 0);
// version
nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion);
// size of header code
nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion);
// height
nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
+ // timestamp and coinstake flag
+ nSize += ::GetSerializeSize(VARINT(nTime*2+(fCoinStake ? 1 : 0)), nType, nVersion);
+ // block timestamp
+ nSize += ::GetSerializeSize(VARINT(nBlockTime), nType, nVersion);
return nSize;
}
}
// coinbase height
::Serialize(s, VARINT(nHeight), nType, nVersion);
+ // transaction timestamp and coinstake flag
+ ::Serialize(s, VARINT(nTime*2+(fCoinStake ? 1 : 0)), nType, nVersion);
+ // block time
+ ::Serialize(s, VARINT(nBlockTime), nType, nVersion);
}
template<typename Stream>
void Unserialize(Stream &s, int nType, int nVersion) {
- unsigned int nCode = 0;
+ unsigned int nCode = 0, nCodeTime = 0;
// version
::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
// header code
}
// coinbase height
::Unserialize(s, VARINT(nHeight), nType, nVersion);
+ // transaction timestamp
+ ::Unserialize(s, VARINT(nCodeTime), nType, nVersion);
+ nTime = nCodeTime / 2;
+ fCoinStake = nCodeTime & 1;
+ // block timestamp
+ ::Unserialize(s, VARINT(nBlockTime), nType, nVersion);
Cleanup();
}
Cleanup();
if (vout.size() == 0) {
undo.nHeight = nHeight;
+ undo.nTime = nTime;
+ undo.nBlockTime = nBlockTime;
undo.fCoinBase = fCoinBase;
+ undo.fCoinStake = fCoinStake;
undo.nVersion = this->nVersion;
}
return true;
int GetDepthInMainChain() const { CBlockIndex *pindexRet; return GetDepthInMainChain(pindexRet); }
bool IsInMainChain() const { return GetDepthInMainChain() > 0; }
int GetBlocksToMaturity() const;
- bool AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs=true);
+ bool AcceptToMemoryPool(CCoinsDB& txdb, bool fCheckInputs=true);
bool AcceptToMemoryPool();
};
-
-/** A txdb record that contains the disk location of a transaction and the
- * locations of transactions that spend its outputs. vSpent is really only
- * used as a flag, but having the location is very helpful for debugging.
- */
-class CTxIndex
-{
-public:
- CDiskTxPos pos;
- std::vector<CDiskTxPos> vSpent;
-
- CTxIndex()
- {
- SetNull();
- }
-
- CTxIndex(const CDiskTxPos& posIn, unsigned int nOutputs)
- {
- pos = posIn;
- vSpent.resize(nOutputs);
- }
-
- IMPLEMENT_SERIALIZE
- (
- if (!(nType & SER_GETHASH))
- READWRITE(nVersion);
- READWRITE(pos);
- READWRITE(vSpent);
- )
-
- void SetNull()
- {
- pos.SetNull();
- vSpent.clear();
- }
-
- bool IsNull()
- {
- return pos.IsNull();
- }
-
- friend bool operator==(const CTxIndex& a, const CTxIndex& b)
- {
- return (a.pos == b.pos &&
- a.vSpent == b.vSpent);
- }
-
- friend bool operator!=(const CTxIndex& a, const CTxIndex& b)
- {
- return !(a == b);
- }
- int GetDepthInMainChain() const;
-
-};
-
-
-
-
-
/** Nodes collect new transactions into a block, hash them into a hash tree,
* and scan through nonce values to make the block's hash satisfy proof-of-work
* requirements. When they solve the proof-of-work, they broadcast the block
* to everyone and the block is added to the block chain. The first transaction
* in the block is a special one that creates a new coin owned by the creator
* of the block.
- *
- * Blocks are appended to blk0001.dat files on disk. Their location on disk
- * is indexed by CBlockIndex objects in memory.
*/
class CBlock
{
return hash;
}
-
bool WriteToDisk(CDiskBlockPos &pos)
{
// Open history file to append
- pos.MakeUnique(GetDataDir());
- CAutoFile fileout = CAutoFile(OpenBlockFile(pos, "ab"), SER_DISK, CLIENT_VERSION);
+ CAutoFile fileout = CAutoFile(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
if (!fileout)
- return error("CBlock::WriteToDisk() : AppendBlockFile failed");
+ return error("CBlock::WriteToDisk() : OpenBlockFile failed");
+
+ // Write index header
+ unsigned int nSize = fileout.GetSerializeSize(*this);
+ fileout << FLATDATA(pchMessageStart) << nSize;
// Write block
+ long fileOutPos = ftell(fileout);
+ if (fileOutPos < 0)
+ return error("CBlock::WriteToDisk() : ftell failed");
+ pos.nPos = (unsigned int)fileOutPos;
fileout << *this;
// Flush stdio buffers and commit to disk before returning
fflush(fileout);
- if (!IsInitialBlockDownload() || (nBestHeight+1) % 500 == 0)
+ if (!IsInitialBlockDownload())
FileCommit(fileout);
return true;
SetNull();
// Open history file to read
- CAutoFile filein = CAutoFile(OpenBlockFile(pos, "rb"), SER_DISK, CLIENT_VERSION);
+ CAutoFile filein = CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
if (!filein)
return error("CBlock::ReadFromDisk() : OpenBlockFile failed");
if (!fReadTransactions)
return true;
}
-
-
void print() const
{
printf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%"PRIszu", vchBlockSig=%s)\n",
printf("\n");
}
+ // Undo the effects of this block (with given index) on the UTXO set represented by coins
+ bool DisconnectBlock(CBlockIndex *pindex, CCoinsView &coins);
+
+ // Apply the effects of this block (with given index) on the UTXO set represented by coins
+ bool ConnectBlock(CBlockIndex *pindex, CCoinsView &coins, bool fJustCheck=false);
- bool DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex);
- bool ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck=false);
+ // Read a block from disk
bool ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions=true);
- bool SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew);
+
+ // Make this block (with given index) the new tip of the active block chain
+ bool SetBestChain(CBlockIndex* pindexNew);
+
+ // Add this block to the block index, and if necessary, switch the active block chain to this
bool AddToBlockIndex(const CDiskBlockPos &pos);
- bool CheckBlock(bool fCheckPOW=true, bool fCheckMerkleRoot=true, bool fCheckSig=true) const;
+
+ // Context-independent validity checks
+ bool CheckBlock(bool fCheckPOW=true, bool fCheckMerkleRoot=true, bool fCheckSig=false) const;
+
+ // Store block on disk
bool AcceptBlock();
- bool GetCoinAge(uint64& nCoinAge) const; // ppcoin: calculate total coin age spent in block
+
+ // Get total coinage consumed
+ bool GetCoinAge(uint64& nCoinAge) const;
+
+ // Generate proof-of-stake block signature
bool SignBlock(CWallet& keystore);
- bool CheckBlockSignature(bool fProofOfStake) const;
-private:
- bool SetBestChainInner(CTxDB& txdb, CBlockIndex *pindexNew);
+ // Validate header signature
+ bool CheckBlockSignature(bool fProofOfStake) const;
};
+class CBlockFileInfo
+{
+public:
+ unsigned int nBlocks; // number of blocks stored in file
+ unsigned int nSize; // number of used bytes of block file
+ unsigned int nUndoSize; // number of used bytes in the undo file
+ unsigned int nHeightFirst; // lowest height of block in file
+ unsigned int nHeightLast; // highest height of block in file
+ uint64 nTimeFirst; // earliest time of block in file
+ uint64 nTimeLast; // latest time of block in file
+ IMPLEMENT_SERIALIZE(
+ READWRITE(VARINT(nBlocks));
+ READWRITE(VARINT(nSize));
+ READWRITE(VARINT(nUndoSize));
+ READWRITE(VARINT(nHeightFirst));
+ READWRITE(VARINT(nHeightLast));
+ READWRITE(VARINT(nTimeFirst));
+ READWRITE(VARINT(nTimeLast));
+ )
+
+ void SetNull() {
+ nBlocks = 0;
+ nSize = 0;
+ nUndoSize = 0;
+ nHeightFirst = 0;
+ nHeightLast = 0;
+ nTimeFirst = 0;
+ nTimeLast = 0;
+ }
+
+ CBlockFileInfo() {
+ SetNull();
+ }
+
+ std::string ToString() const {
+ return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u..%u, time=%s..%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst).c_str(), DateTimeStrFormat("%Y-%m-%d", nTimeLast).c_str());
+ }
+
+ // update statistics (does not update nSize)
+ void AddBlock(unsigned int nHeightIn, uint64 nTimeIn) {
+ if (nBlocks==0 || nHeightFirst > nHeightIn)
+ nHeightFirst = nHeightIn;
+ if (nBlocks==0 || nTimeFirst > nTimeIn)
+ nTimeFirst = nTimeIn;
+ nBlocks++;
+ if (nHeightIn > nHeightFirst)
+ nHeightLast = nHeightIn;
+ if (nTimeIn > nTimeLast)
+ nTimeLast = nTimeIn;
+ }
+};
+extern CCriticalSection cs_LastBlockFile;
+extern CBlockFileInfo infoLastBlockFile;
+extern int nLastBlockFile;
/** The block chain is a tree shaped structure starting with the
* genesis block at the root, with each block potentially having multiple
const uint256* phashBlock;
CBlockIndex* pprev;
CBlockIndex* pnext;
- uint256 nChainTrust; // ppcoin: trust score of block chain
int nHeight;
- unsigned int nAlternative;
+ CDiskBlockPos pos;
+ unsigned int nUndoPos;
+ uint256 nChainTrust; // trust score of block chain
int64 nMint;
int64 nMoneySupply;
unsigned int nBits;
unsigned int nNonce;
+
CBlockIndex()
{
phashBlock = NULL;
pprev = NULL;
pnext = NULL;
nHeight = 0;
+ pos.SetNull();
+ nUndoPos = 0;
nChainTrust = 0;
nMint = 0;
nMoneySupply = 0;
hashProofOfStake = 0;
prevoutStake.SetNull();
nStakeTime = 0;
- nAlternative = 0;
nVersion = 0;
hashMerkleRoot = 0;
pprev = NULL;
pnext = NULL;
nHeight = 0;
+ pos.SetNull();
+ nUndoPos = 0;
nChainTrust = 0;
nMint = 0;
nMoneySupply = 0;
prevoutStake.SetNull();
nStakeTime = 0;
}
- nAlternative = 0;
nVersion = block.nVersion;
hashMerkleRoot = block.hashMerkleRoot;
}
CDiskBlockPos GetBlockPos() const {
- return CDiskBlockPos(nHeight, nAlternative);
+ return pos;
+ }
+
+ CDiskBlockPos GetUndoPos() const {
+ CDiskBlockPos ret = pos;
+ if (nUndoPos == 0)
+ ret.SetNull();
+ else
+ ret.nPos = nUndoPos - 1;
+ return ret;
}
CBlock GetBlockHeader() const
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart,
unsigned int nRequired, unsigned int nToCheck);
-
bool IsProofOfWork() const
{
return !(nFlags & BLOCK_PROOF_OF_STAKE);
GetBlockHash().ToString().c_str());
}
+
void print() const
{
printf("%s\n", ToString().c_str());
}
};
-
-
/** Used to marshal pointers into hashes for db storage. */
class CDiskBlockIndex : public CBlockIndex
{
private:
uint256 blockHash;
-
public:
uint256 hashPrev;
- uint256 hashNext;
- CDiskBlockIndex()
- {
+ CDiskBlockIndex() {
hashPrev = 0;
- hashNext = 0;
blockHash = 0;
}
- explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex)
- {
+ explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex) {
hashPrev = (pprev ? pprev->GetBlockHash() : 0);
- hashNext = (pnext ? pnext->GetBlockHash() : 0);
}
IMPLEMENT_SERIALIZE
if (!(nType & SER_GETHASH))
READWRITE(nVersion);
- READWRITE(hashNext);
READWRITE(nHeight);
- READWRITE(nAlternative);
+ READWRITE(pos);
+ READWRITE(nUndoPos);
READWRITE(nMint);
READWRITE(nMoneySupply);
READWRITE(nFlags);
const_cast<CDiskBlockIndex*>(this)->nStakeTime = 0;
const_cast<CDiskBlockIndex*>(this)->hashProofOfStake = 0;
}
+ READWRITE(blockHash);
// block header
READWRITE(this->nVersion);
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
- READWRITE(blockHash);
)
uint256 GetBlockHash() const
return blockHash;
}
+
std::string ToString() const
{
std::string str = "CDiskBlockIndex(";
str += CBlockIndex::ToString();
- str += strprintf("\n hashBlock=%s, hashPrev=%s, hashNext=%s)",
+ str += strprintf("\n hashBlock=%s, hashPrev=%s)",
GetBlockHash().ToString().c_str(),
- hashPrev.ToString().c_str(),
- hashNext.ToString().c_str());
+ hashPrev.ToString().substr(0,20).c_str());
return str;
}
};
-
-
-
-
-
-
/** Describes a place in the block chain to another node such that if the
* other node doesn't have the same branch, it can find a recent common trunk.
* The further back it is, the further before the fork it may be.
-
-
-
class CTxMemPool
{
public:
std::map<uint256, CTransaction> mapTx;
std::map<COutPoint, CInPoint> mapNextTx;
- bool accept(CTxDB& txdb, CTransaction &tx,
+ bool accept(CCoinsDB& coinsdb, CTransaction &tx,
bool fCheckInputs, bool* pfMissingInputs);
bool addUnchecked(const uint256& hash, CTransaction &tx);
bool remove(CTransaction &tx);
void clear();
void queryHashes(std::vector<uint256>& vtxid);
+ void pruneSpent(const uint256& hash, CCoins &coins);
unsigned long size()
{
extern CTxMemPool mempool;
+/** Abstract view on the open txout dataset. */
+class CCoinsView
+{
+public:
+ // Retrieve the CCoins (unspent transaction outputs) for a given txid
+ virtual bool GetCoins(uint256 txid, CCoins &coins);
+
+ // Modify the CCoins for a given txid
+ virtual bool SetCoins(uint256 txid, const CCoins &coins);
+
+ // Just check whether we have data for a given txid.
+ // This may (but cannot always) return true for fully spent transactions
+ virtual bool HaveCoins(uint256 txid);
+
+ // Retrieve the block index whose state this CCoinsView currently represents
+ virtual CBlockIndex *GetBestBlock();
+
+ // Modify the currently active block index
+ virtual bool SetBestBlock(CBlockIndex *pindex);
+};
+
+/** CCoinsView backed by another CCoinsView */
+class CCoinsViewBacked : public CCoinsView
+{
+protected:
+ CCoinsView *base;
+
+public:
+ CCoinsViewBacked(CCoinsView &viewIn);
+ bool GetCoins(uint256 txid, CCoins &coins);
+ bool SetCoins(uint256 txid, const CCoins &coins);
+ bool HaveCoins(uint256 txid);
+ CBlockIndex *GetBestBlock();
+ bool SetBestBlock(CBlockIndex *pindex);
+ void SetBackend(CCoinsView &viewIn);
+};
+
+
+/** CCoinsView backed by a CCoinsDB */
+class CCoinsViewDB : public CCoinsView
+{
+protected:
+ CCoinsDB &db;
+public:
+ CCoinsViewDB(CCoinsDB &dbIn);
+ bool GetCoins(uint256 txid, CCoins &coins);
+ bool SetCoins(uint256 txid, const CCoins &coins);
+ bool HaveCoins(uint256 txid);
+ CBlockIndex *GetBestBlock();
+ bool SetBestBlock(CBlockIndex *pindex);
+};
+
+/** CCoinsView that adds a memory cache for transactions to another CCoinsView */
+class CCoinsViewCache : public CCoinsViewBacked
+{
+protected:
+ CBlockIndex *pindexTip;
+ std::map<uint256,CCoins> cacheCoins;
+
+public:
+ CCoinsViewCache(CCoinsView &baseIn, bool fDummy = false);
+ bool GetCoins(uint256 txid, CCoins &coins);
+ bool SetCoins(uint256 txid, const CCoins &coins);
+ bool HaveCoins(uint256 txid);
+ CBlockIndex *GetBestBlock();
+ bool SetBestBlock(CBlockIndex *pindex);
+ bool Flush();
+};
+
+/** CCoinsView that brings transactions from a memorypool into view.
+ It does not check for spendings by memory pool transactions. */
+class CCoinsViewMemPool : public CCoinsViewBacked
+{
+protected:
+ CTxMemPool &mempool;
+
+public:
+ CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn);
+ bool GetCoins(uint256 txid, CCoins &coins);
+ bool HaveCoins(uint256 txid);
+};
+
#endif
obj/crypter.o \
obj/key.o \
obj/db.o \
+ obj/leveldb.o \
obj/init.o \
obj/irc.o \
obj/keystore.o \
LIBS += $(CURDIR)/leveldb/libleveldb.a $(CURDIR)/leveldb/libmemenv.a
DEFS += $(addprefix -I,$(CURDIR)/leveldb/include)
DEFS += $(addprefix -I,$(CURDIR)/leveldb/helpers)
-OBJS += obj/txdb-leveldb.o
leveldb/libleveldb.a:
@echo "Building LevelDB ..."; cd leveldb; make libleveldb.a libmemenv.a; cd ..;
obj/txdb-leveldb.o: leveldb/libleveldb.a
obj/crypter.o \
obj/key.o \
obj/db.o \
+ obj/leveldb.o \
obj/init.o \
obj/irc.o \
obj/keystore.o \
LIBS += $(CURDIR)/leveldb/libleveldb.a $(CURDIR)/leveldb/libmemenv.a
DEFS += -I"$(CURDIR)/leveldb/include"
DEFS += -I"$(CURDIR)/leveldb/helpers"
-OBJS += obj/txdb-leveldb.o
leveldb/libleveldb.a:
@echo "Building LevelDB ..." && cd leveldb && CC=$(CC) CXX=$(CXX) TARGET_OS=OS_WINDOWS_CROSSCOMPILE CXXFLAGS="-I$(INCLUDEPATHS)" LDFLAGS="-L$(LIBPATHS)" $(MAKE) libleveldb.a libmemenv.a && $(RANLIB) libleveldb.a && $(RANLIB) libmemenv.a && cd ..
obj/txdb-leveldb.o: leveldb/libleveldb.a
obj/crypter.o \
obj/key.o \
obj/db.o \
+ obj/leveldb.o \
obj/init.o \
obj/irc.o \
obj/keystore.o \
LIBS += $(CURDIR)/leveldb/libleveldb.a $(CURDIR)/leveldb/libmemenv.a
DEFS += $(addprefix -I,$(CURDIR)/leveldb/include)
DEFS += $(addprefix -I,$(CURDIR)/leveldb/helpers)
-OBJS += obj/txdb-leveldb.o
leveldb/libleveldb.a:
cd leveldb; make; cd ..
obj/txdb-leveldb.o: leveldb/libleveldb.lib
obj/crypter.o \
obj/key.o \
obj/db.o \
+ obj/leveldb.o \
obj/init.o \
obj/irc.o \
obj/keystore.o \
LIBS += $(CURDIR)/leveldb/libleveldb.a $(CURDIR)/leveldb/libmemenv.a
DEFS += $(addprefix -I,$(CURDIR)/leveldb/include)
DEFS += $(addprefix -I,$(CURDIR)/leveldb/helpers)
-OBJS += obj/txdb-leveldb.o
leveldb/libleveldb.a:
@echo "Building LevelDB ..."; cd leveldb; make; cd ..
obj/txdb-leveldb.o: leveldb/libleveldb.a
obj/crypter.o \
obj/key.o \
obj/db.o \
+ obj/leveldb.o \
obj/init.o \
obj/irc.o \
obj/keystore.o \
LIBS += $(CURDIR)/leveldb/libleveldb.a $(CURDIR)/leveldb/libmemenv.a
DEFS += $(addprefix -I,$(CURDIR)/leveldb/include)
DEFS += $(addprefix -I,$(CURDIR)/leveldb/helpers)
-OBJS += obj/txdb-leveldb.o
leveldb/libleveldb.a:
@echo "Building LevelDB ..."; cd leveldb; make libleveldb.a libmemenv.a; cd ..;
-obj/txdb-leveldb.o: leveldb/libleveldb.a
+obj/leveldb.o: leveldb/libleveldb.a
# auto-generated dependencies:
-include obj/*.P
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#include "txdb.h"
+#include "db.h"
#include "miner.h"
#include "kernel.h"
int64 nFees = 0;
{
LOCK2(cs_main, mempool.cs);
- CBlockIndex* pindexPrev = pindexBest;
- CTxDB txdb("r");
+ CCoinsDB coinsdb("r");
+ CCoinsViewDB viewdb(coinsdb);
+ CCoinsViewCache view(viewdb);
// Priority order to process transactions
list<COrphan> vOrphan; // list memory doesn't move
BOOST_FOREACH(const CTxIn& txin, tx.vin)
{
// Read prev transaction
- CTransaction txPrev;
- CTxIndex txindex;
- if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
+ CCoins coins;
+ if (!view.GetCoins(txin.prevout.hash, coins))
{
// This should never happen; all transactions in the memory
// pool should connect to either transactions in the chain
nTotalIn += mempool.mapTx[txin.prevout.hash].vout[txin.prevout.n].nValue;
continue;
}
- int64 nValueIn = txPrev.vout[txin.prevout.n].nValue;
+ int64 nValueIn = coins.vout[txin.prevout.n].nValue;
nTotalIn += nValueIn;
- int nConf = txindex.GetDepthInMainChain();
+ int nConf = pindexPrev->nHeight - coins.nHeight;
dPriority += (double)nValueIn * nConf;
}
if (fMissingInputs) continue;
}
// Collect transactions into block
- map<uint256, CTxIndex> mapTestPool;
uint64 nBlockSize = 1000;
uint64 nBlockTx = 0;
int nBlockSigOps = 100;
std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
vecPriority.pop_back();
+ // second layer cached modifications just for this transaction
+ CCoinsViewCache viewTemp(view, true);
+
// Size limits
unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
if (nBlockSize + nTxSize >= nBlockMaxSize)
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
- map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool);
- MapPrevTx mapInputs;
- bool fInvalid;
- if (!tx.FetchInputs(txdb, mapTestPoolTmp, false, true, mapInputs, fInvalid))
+ if (!tx.CheckInputs(viewTemp, CS_ALWAYS, true, false))
continue;
- int64 nTxFees = tx.GetValueIn(mapInputs)-tx.GetValueOut();
+ int64 nTxFees = tx.GetValueIn(viewTemp)-tx.GetValueOut();
if (nTxFees < nMinFee)
continue;
- nTxSigOps += tx.GetP2SHSigOpCount(mapInputs);
+ nTxSigOps += tx.GetP2SHSigOpCount(viewTemp);
if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
continue;
- if (!tx.ConnectInputs(txdb, mapInputs, mapTestPoolTmp, CDiskTxPos(true), pindexPrev, false, true))
+ CTxUndo txundo;
+ if (!tx.UpdateCoins(viewTemp, txundo, pindexPrev->nHeight+1, pblock->nTime))
continue;
- mapTestPoolTmp[tx.GetHash()] = CTxIndex(CDiskTxPos(true), tx.vout.size());
- swap(mapTestPool, mapTestPoolTmp);
+
+ // push changes from the second layer cache to the first one
+ viewTemp.Flush();
// Added
pblock->vtx.push_back(tx);
#include "main.h"
#include "wallet.h"
-#include "txdb.h"
+#include "db.h"
#include "ui_interface.h"
#include "base58.h"
strHTML += "<br><b>" + tr("Transaction") + ":</b><br>";
strHTML += GUIUtil::HtmlEscape(wtx.ToString(), true);
- CTxDB txdb("r"); // To fetch source txouts
+ CCoinsDB coinsdb("r"); // To fetch source txouts
+ CCoinsViewDB coins(coinsdb);
strHTML += "<br><b>" + tr("Inputs") + ":</b>";
strHTML += "<ul>";
{
COutPoint prevout = txin.prevout;
- CTransaction prev;
- if(txdb.ReadDiskTx(prevout.hash, prev))
+ CCoins prev;
+ if(coins.GetCoins(prevout.hash, prev))
{
if (prevout.n < prev.vout.size())
{
#include "main.h"
#include "db.h"
-#include "txdb.h"
#include "init.h"
#include "miner.h"
#include "bitcoinrpc.h"
}
}
-
Value getblocktemplate(const Array& params, bool fHelp)
{
- if (fHelp || params.size() > 1)
+ if (fHelp || params.size() != 1)
throw runtime_error(
"getblocktemplate [params]\n"
"Returns data needed to construct a block to work on:\n"
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
if (vNodes.empty())
- throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "NovaCoin is not connected!");
+ throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Bitcoin is not connected!");
if (IsInitialBlockDownload())
- throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "NovaCoin is downloading blocks...");
+ throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Bitcoin is downloading blocks...");
static CReserveKey reservekey(pwalletMain);
Array transactions;
map<uint256, int64_t> setTxIndex;
int i = 0;
- CTxDB txdb("r");
+ CCoinsDB coindb("r");
+ CCoinsViewDB viewdb(coindb);
+ CCoinsViewCache view(viewdb);
BOOST_FOREACH (CTransaction& tx, pblock->vtx)
{
uint256 txHash = tx.GetHash();
setTxIndex[txHash] = i++;
- if (tx.IsCoinBase() || tx.IsCoinStake())
+ if (tx.IsCoinBase())
continue;
Object entry;
entry.push_back(Pair("hash", txHash.GetHex()));
- MapPrevTx mapInputs;
- map<uint256, CTxIndex> mapUnused;
- bool fInvalid = false;
- if (tx.FetchInputs(txdb, mapUnused, false, false, mapInputs, fInvalid))
+ Array deps;
+ BOOST_FOREACH (const CTxIn &in, tx.vin)
{
- entry.push_back(Pair("fee", (int64_t)(tx.GetValueIn(mapInputs) - tx.GetValueOut())));
-
- Array deps;
- BOOST_FOREACH (MapPrevTx::value_type& inp, mapInputs)
- {
- if (setTxIndex.count(inp.first))
- deps.push_back(setTxIndex[inp.first]);
- }
- entry.push_back(Pair("depends", deps));
+ if (setTxIndex.count(in.prevout.hash))
+ deps.push_back(setTxIndex[in.prevout.hash]);
+ }
+ entry.push_back(Pair("depends", deps));
- int64_t nSigOps = tx.GetLegacySigOpCount();
- nSigOps += tx.GetP2SHSigOpCount(mapInputs);
- entry.push_back(Pair("sigops", nSigOps));
+ int64_t nSigOps = tx.GetLegacySigOpCount();
+ if (tx.HaveInputs(view))
+ {
+ entry.push_back(Pair("fee", (int64_t)(tx.GetValueIn(view) - tx.GetValueOut())));
+ nSigOps += tx.GetP2SHSigOpCount(view);
}
+ entry.push_back(Pair("sigops", nSigOps));
transactions.push_back(entry);
}
vector<unsigned char> blockData(ParseHex(params[0].get_str()));
CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
- CBlock block;
+ CBlock pblock;
try {
- ssBlock >> block;
+ ssBlock >> pblock;
}
catch (std::exception &e) {
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
}
- bool fAccepted = ProcessBlock(NULL, &block);
+ bool fAccepted = ProcessBlock(NULL, &pblock);
if (!fAccepted)
return "rejected";
return Value::null;
}
-
#include "base58.h"
#include "bitcoinrpc.h"
-#include "txdb.h"
+#include "db.h"
#include "init.h"
#include "main.h"
#include "net.h"
CTransaction tx;
uint256 hashBlock = 0;
- if (!GetTransaction(hash, tx, hashBlock))
+ if (!GetTransaction(hash, tx, hashBlock, true))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
return results;
}
+Value decodescript(const Array& params, bool fHelp)
+{
+ if (fHelp || params.size() != 1)
+ throw runtime_error(
+ "decodescript <hex string>\n"
+ "Decode a hex-encoded script.");
+
+ RPCTypeCheck(params, list_of(str_type));
+
+ Object r;
+ CScript script;
+ if (params[0].get_str().size() > 0){
+ vector<unsigned char> scriptData(ParseHexV(params[0], "argument"));
+ script = CScript(scriptData.begin(), scriptData.end());
+ } else {
+ // Empty scripts are valid
+ }
+ ScriptPubKeyToJSON(script, r, false);
+
+ r.push_back(Pair("p2sh", CBitcoinAddress(script.GetID()).ToString()));
+ return r;
+}
+
Value createrawtransaction(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 2)
{
CBitcoinAddress address(s.name_);
if (!address.IsValid())
- throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid NovaCoin address: ")+s.name_);
+ throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+s.name_);
if (setAddress.count(address))
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+s.name_);
return result;
}
-Value decodescript(const Array& params, bool fHelp)
-{
- if (fHelp || params.size() != 1)
- throw runtime_error(
- "decodescript <hex string>\n"
- "Decode a hex-encoded script.");
-
- RPCTypeCheck(params, list_of(str_type));
-
- Object r;
- CScript script;
- if (params[0].get_str().size() > 0){
- vector<unsigned char> scriptData(ParseHexV(params[0], "argument"));
- script = CScript(scriptData.begin(), scriptData.end());
- } else {
- // Empty scripts are valid
- }
- ScriptPubKeyToJSON(script, r, false);
-
- r.push_back(Pair("p2sh", CBitcoinAddress(script.GetID()).ToString()));
- return r;
-}
-
Value signrawtransaction(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 4)
bool fComplete = true;
// Fetch previous transactions (inputs):
- map<COutPoint, CScript> mapPrevOut;
- for (unsigned int i = 0; i < mergedTx.vin.size(); i++)
+ CCoinsView viewDummy;
+ CCoinsViewCache view(viewDummy);
{
- CTransaction tempTx;
- MapPrevTx mapPrevTx;
- CTxDB txdb("r");
- map<uint256, CTxIndex> unused;
- bool fInvalid;
-
- // FetchInputs aborts on failure, so we go one at a time.
- tempTx.vin.push_back(mergedTx.vin[i]);
- tempTx.FetchInputs(txdb, unused, false, false, mapPrevTx, fInvalid);
-
- // Copy results into mapPrevOut:
- BOOST_FOREACH(const CTxIn& txin, tempTx.vin)
- {
+ LOCK(mempool.cs);
+ CCoinsDB coinsdb("r");
+ CCoinsViewDB viewDB(coinsdb);
+ CCoinsViewMemPool viewMempool(viewDB, mempool);
+ view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
+
+ BOOST_FOREACH(const CTxIn& txin, mergedTx.vin) {
const uint256& prevHash = txin.prevout.hash;
- if (mapPrevTx.count(prevHash) && mapPrevTx[prevHash].second.vout.size()>txin.prevout.n)
- mapPrevOut[txin.prevout] = mapPrevTx[prevHash].second.vout[txin.prevout.n].scriptPubKey;
+ CCoins coins;
+ view.GetCoins(prevHash, coins); // this is certainly allowed to fail
}
+
+ view.SetBackend(viewDummy); // switch back to avoid locking db/mempool too long
}
// Add previous txouts given in the RPC call:
vector<unsigned char> pkData(ParseHex(pkHex));
CScript scriptPubKey(pkData.begin(), pkData.end());
- COutPoint outpoint(txid, nOut);
- if (mapPrevOut.count(outpoint))
- {
- // Complain if scriptPubKey doesn't match
- if (mapPrevOut[outpoint] != scriptPubKey)
- {
+ CCoins coins;
+ if (view.GetCoins(txid, coins)) {
+ if (coins.IsAvailable(nOut) && coins.vout[nOut].scriptPubKey != scriptPubKey) {
string err("Previous output scriptPubKey mismatch:\n");
- err = err + mapPrevOut[outpoint].ToString() + "\nvs:\n"+
+ err = err + coins.vout[nOut].scriptPubKey.ToString() + "\nvs:\n"+
scriptPubKey.ToString();
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
}
+ // what todo if txid is known, but the actual output isn't?
}
- else
- mapPrevOut[outpoint] = scriptPubKey;
+ coins.vout[nOut].scriptPubKey = scriptPubKey;
+ coins.vout[nOut].nValue = 0; // we don't know the actual output value
+ view.SetCoins(txid, coins);
}
}
for (unsigned int i = 0; i < mergedTx.vin.size(); i++)
{
CTxIn& txin = mergedTx.vin[i];
- if (mapPrevOut.count(txin.prevout) == 0)
+ CCoins coins;
+ if (!view.GetCoins(txin.prevout.hash, coins) || !coins.IsAvailable(txin.prevout.n))
{
fComplete = false;
continue;
}
- const CScript& prevPubKey = mapPrevOut[txin.prevout];
+ const CScript& prevPubKey = coins.vout[txin.prevout.n].scriptPubKey;
txin.scriptSig.clear();
// Only sign SIGHASH_SINGLE if there's a corresponding output:
{
txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
}
- if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, true, 0))
+ if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, true, true, 0))
fComplete = false;
}
}
uint256 hashTx = tx.GetHash();
- // See if the transaction is already in a block
- // or in the memory pool:
- CTransaction existingTx;
- uint256 hashBlock = 0;
- if (GetTransaction(hashTx, existingTx, hashBlock))
+ bool fHave = false;
+ CCoins existingCoins;
{
- if (hashBlock != 0)
- throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("transaction already in block ")+hashBlock.GetHex());
+ CCoinsDB coinsdb("r");
+ {
+ CCoinsViewDB coinsviewDB(coinsdb);
+ CCoinsViewMemPool coinsview(coinsviewDB, mempool);
+ fHave = coinsview.GetCoins(hashTx, existingCoins);
+ }
+ if (!fHave) {
+ // push to local node
+ if (!tx.AcceptToMemoryPool(coinsdb))
+ throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX rejected");
+ }
+ }
+ if (fHave) {
+ if (existingCoins.nHeight < 1000000000)
+ throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "transaction already in block chain");
// Not in block, but already in the memory pool; will drop
// through to re-relay it.
- }
- else
- {
- // push to local node
- CTxDB txdb("r");
- if (!tx.AcceptToMemoryPool(txdb))
- throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX rejected");
-
+ } else {
SyncWithWallets(tx, NULL, true);
}
RelayTransaction(tx, hashTx);
{
CTransaction tx;
uint256 hashBlock = 0;
- if (GetTransaction(hash, tx, hashBlock))
+ if (GetTransaction(hash, tx, hashBlock, true))
{
TxToJSON(tx, 0, entry);
if (hashBlock == 0)
bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
+
+
+typedef vector<unsigned char> valtype;
static const valtype vchFalse(0);
static const valtype vchZero(0);
static const valtype vchTrue(1, 1);
}
}
-bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType)
+bool IsCanonicalPubKey(const valtype &vchPubKey) {
+ if (vchPubKey.size() < 33)
+ return error("Non-canonical public key: too short");
+ if (vchPubKey[0] == 0x04) {
+ if (vchPubKey.size() != 65)
+ return error("Non-canonical public key: invalid length for uncompressed key");
+ } else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
+ if (vchPubKey.size() != 33)
+ return error("Non-canonical public key: invalid length for compressed key");
+ } else {
+ return error("Non-canonical public key: compressed nor uncompressed");
+ }
+ return true;
+}
+
+bool IsCanonicalSignature(const valtype &vchSig) {
+ // See https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623
+ // A canonical signature exists of: <30> <total len> <02> <len R> <R> <02> <len S> <S> <hashtype>
+ // Where R and S are not negative (their first byte has its highest bit not set), and not
+ // excessively padded (do not start with a 0 byte, unless an otherwise negative number follows,
+ // in which case a single 0 byte is necessary and even required).
+ if (vchSig.size() < 9)
+ return error("Non-canonical signature: too short");
+ if (vchSig.size() > 73)
+ return error("Non-canonical signature: too long");
+ if (vchSig[vchSig.size() - 1] & 0x7C)
+ return error("Non-canonical signature: unknown hashtype byte");
+ if (vchSig[0] != 0x30)
+ return error("Non-canonical signature: wrong type");
+ if (vchSig[1] != vchSig.size()-3)
+ return error("Non-canonical signature: wrong length marker");
+ unsigned int nLenR = vchSig[3];
+ if (5 + nLenR >= vchSig.size())
+ return error("Non-canonical signature: S length misplaced");
+ unsigned int nLenS = vchSig[5+nLenR];
+ if ((unsigned long)(nLenR+nLenS+7) != vchSig.size())
+ return error("Non-canonical signature: R+S length mismatch");
+
+ const unsigned char *R = &vchSig[4];
+ if (R[-2] != 0x02)
+ return error("Non-canonical signature: R value type mismatch");
+ if (nLenR == 0)
+ return error("Non-canonical signature: R length is zero");
+ if (R[0] & 0x80)
+ return error("Non-canonical signature: R value negative");
+ if (nLenR > 1 && (R[0] == 0x00) && !(R[1] & 0x80))
+ return error("Non-canonical signature: R value excessively padded");
+
+ const unsigned char *S = &vchSig[6+nLenR];
+ if (S[-2] != 0x02)
+ return error("Non-canonical signature: S value type mismatch");
+ if (nLenS == 0)
+ return error("Non-canonical signature: S length is zero");
+ if (S[0] & 0x80)
+ return error("Non-canonical signature: S value negative");
+ if (nLenS > 1 && (S[0] == 0x00) && !(S[1] & 0x80))
+ return error("Non-canonical signature: S value excessively padded");
+
+ return true;
+}
+
+bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, bool fStrictEncodings, int nHashType)
{
CAutoBN_CTX pctx;
CScript::const_iterator pc = script.begin();
// Drop the signature, since there's no way for a signature to sign itself
scriptCode.FindAndDelete(CScript(vchSig));
- bool fSuccess = CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType);
+ bool fSuccess = (!fStrictEncodings || (IsCanonicalSignature(vchSig) && IsCanonicalPubKey(vchPubKey)));
+ if (fSuccess)
+ fSuccess = CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType);
popstack(stack);
popstack(stack);
valtype& vchPubKey = stacktop(-ikey);
// Check signature
- if (CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType))
- {
+ bool fOk = (!fStrictEncodings || (IsCanonicalSignature(vchSig) && IsCanonicalPubKey(vchPubKey)));
+ if (fOk)
+ fOk = CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType);
+
+ if (fOk) {
isig++;
nSigsCount--;
}
return false;
}
-bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
-{
- vector<valtype> vSolutions;
- txnouttype whichType;
- if (!Solver(scriptPubKey, whichType, vSolutions))
- return false;
-
- if (whichType == TX_PUBKEY)
- {
- addressRet = CPubKey(vSolutions[0]).GetID();
- return true;
- }
- else if (whichType == TX_PUBKEYHASH)
- {
- addressRet = CKeyID(uint160(vSolutions[0]));
- return true;
- }
- else if (whichType == TX_SCRIPTHASH)
- {
- addressRet = CScriptID(uint160(vSolutions[0]));
- return true;
- }
- // Multisig txns have more than one address...
- return false;
-}
-
class CAffectedKeysVisitor : public boost::static_visitor<void> {
private:
const CKeyStore &keystore;
CAffectedKeysVisitor(keystore, vKeys).Process(scriptPubKey);
}
+bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
+{
+ vector<valtype> vSolutions;
+ txnouttype whichType;
+ if (!Solver(scriptPubKey, whichType, vSolutions))
+ return false;
+
+ if (whichType == TX_PUBKEY)
+ {
+ addressRet = CPubKey(vSolutions[0]).GetID();
+ return true;
+ }
+ else if (whichType == TX_PUBKEYHASH)
+ {
+ addressRet = CKeyID(uint160(vSolutions[0]));
+ return true;
+ }
+ else if (whichType == TX_SCRIPTHASH)
+ {
+ addressRet = CScriptID(uint160(vSolutions[0]));
+ return true;
+ }
+ // Multisig txns have more than one address...
+ return false;
+}
+
bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, vector<CTxDestination>& addressRet, int& nRequiredRet)
{
addressRet.clear();
}
bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
- bool fValidatePayToScriptHash, int nHashType)
+ bool fValidatePayToScriptHash, bool fStrictEncodings, int nHashType)
{
vector<vector<unsigned char> > stack, stackCopy;
- if (!EvalScript(stack, scriptSig, txTo, nIn, nHashType))
+ if (!EvalScript(stack, scriptSig, txTo, nIn, fStrictEncodings, nHashType))
return false;
if (fValidatePayToScriptHash)
stackCopy = stack;
- if (!EvalScript(stack, scriptPubKey, txTo, nIn, nHashType))
+ if (!EvalScript(stack, scriptPubKey, txTo, nIn, fStrictEncodings, nHashType))
return false;
if (stack.empty())
return false;
CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
popstack(stackCopy);
- if (!EvalScript(stackCopy, pubKey2, txTo, nIn, nHashType))
+ if (!EvalScript(stackCopy, pubKey2, txTo, nIn, fStrictEncodings, nHashType))
return false;
if (stackCopy.empty())
return false;
}
// Test solution
- return VerifyScript(txin.scriptSig, fromPubKey, txTo, nIn, true, 0);
+ return VerifyScript(txin.scriptSig, fromPubKey, txTo, nIn, true, true, 0);
}
bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType)
assert(nIn < txTo.vin.size());
CTxIn& txin = txTo.vin[nIn];
assert(txin.prevout.n < txFrom.vout.size());
- assert(txin.prevout.hash == txFrom.GetHash());
const CTxOut& txout = txFrom.vout[txin.prevout.n];
return SignSignature(keystore, txout.scriptPubKey, txTo, nIn, nHashType);
}
-bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType)
+bool VerifySignature(const CCoins& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, bool fStrictEncodings, int nHashType)
{
assert(nIn < txTo.vin.size());
const CTxIn& txin = txTo.vin[nIn];
return false;
const CTxOut& txout = txFrom.vout[txin.prevout.n];
- if (txin.prevout.hash != txFrom.GetHash())
- return false;
-
- return VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, fValidatePayToScriptHash, nHashType);
+ return VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, fValidatePayToScriptHash, fStrictEncodings, nHashType);
}
static CScript PushAll(const vector<valtype>& values)
Solver(scriptPubKey, txType, vSolutions);
vector<valtype> stack1;
- EvalScript(stack1, scriptSig1, CTransaction(), 0, 0);
+ EvalScript(stack1, scriptSig1, CTransaction(), 0, true, 0);
vector<valtype> stack2;
- EvalScript(stack2, scriptSig2, CTransaction(), 0, 0);
+ EvalScript(stack2, scriptSig2, CTransaction(), 0, true, 0);
return CombineSignatures(scriptPubKey, txTo, nIn, txType, vSolutions, stack1, stack2);
}
#include "keystore.h"
#include "bignum.h"
-typedef std::vector<unsigned char> valtype;
-
+class CCoins;
class CTransaction;
+typedef std::vector<unsigned char> valtype;
+
/** Signature hash types/flags */
enum
{
printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
}
- std::string ToString(bool fShort=false) const
+ std::string ToString() const
{
std::string str;
opcodetype opcode;
return str;
}
if (0 <= opcode && opcode <= OP_PUSHDATA4)
- str += fShort? ValueString(vch).substr(0, 10) : ValueString(vch);
+ str += ValueString(vch);
else
str += GetOpName(opcode);
}
}
};
+bool IsCanonicalPubKey(const std::vector<unsigned char> &vchPubKey);
+bool IsCanonicalSignature(const std::vector<unsigned char> &vchSig);
-
-
-bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
+bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, bool fStrictEncodings, int nHashType);
bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
bool IsStandard(const CScript& scriptPubKey);
bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
bool IsMine(const CKeyStore& keystore, const CTxDestination &dest);
+
void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys);
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
bool SignSignature(const CKeyStore& keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
- bool fValidatePayToScriptHash, int nHashType);
-bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);
+ bool fValidatePayToScriptHash, bool fStrictEncodings, int nHashType);
+bool VerifySignature(const CCoins& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, bool fStrictEncodings, int nHashType);
// Given two sets of signatures for scriptPubKey, possibly with OP_0 placeholders,
// combine them intelligently and return the result.
+++ /dev/null
-// Copyright (c) 2009-2010 Satoshi Nakamoto
-// Copyright (c) 2009-2012 The Bitcoin developers
-// Distributed under the MIT/X11 software license, see the accompanying
-// file license.txt or http://www.opensource.org/licenses/mit-license.php.
-
-#include <map>
-
-#include <boost/version.hpp>
-#include <boost/filesystem.hpp>
-#include <boost/filesystem/fstream.hpp>
-
-#include <leveldb/env.h>
-#include <leveldb/cache.h>
-#include <leveldb/filter_policy.h>
-#include <memenv/memenv.h>
-
-#include "kernel.h"
-#include "checkpoints.h"
-#include "txdb.h"
-#include "util.h"
-#include "main.h"
-
-using namespace std;
-using namespace boost;
-
-leveldb::DB *txdb; // global pointer for LevelDB object instance
-
-static leveldb::Options GetOptions() {
- leveldb::Options options;
- int nCacheSizeMB = GetArg("-dbcache", 25);
- options.block_cache = leveldb::NewLRUCache(nCacheSizeMB * 1048576);
- options.filter_policy = leveldb::NewBloomFilterPolicy(10);
- return options;
-}
-
-void init_blockindex(leveldb::Options& options, bool fRemoveOld = false) {
- // First time init.
- filesystem::path directory = GetDataDir() / "txleveldb";
-
- if (fRemoveOld) {
- filesystem::remove_all(directory); // remove directory
- unsigned int nFile = 1;
-
- while (true)
- {
- filesystem::path strBlockFile = GetDataDir() / strprintf("blk%04u.dat", nFile);
-
- // Break if no such file
- if( !filesystem::exists( strBlockFile ) )
- break;
-
- filesystem::remove(strBlockFile);
-
- nFile++;
- }
- }
-
- filesystem::create_directory(directory);
- printf("Opening LevelDB in %s\n", directory.string().c_str());
- leveldb::Status status = leveldb::DB::Open(options, directory.string(), &txdb);
- if (!status.ok()) {
- throw runtime_error(strprintf("init_blockindex(): error opening database environment %s", status.ToString().c_str()));
- }
-}
-
-// CDB subclasses are created and destroyed VERY OFTEN. That's why
-// we shouldn't treat this as a free operations.
-CTxDB::CTxDB(const char* pszMode)
-{
- assert(pszMode);
- activeBatch = NULL;
- fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
-
- if (txdb) {
- pdb = txdb;
- return;
- }
-
- bool fCreate = strchr(pszMode, 'c');
-
- options = GetOptions();
- options.create_if_missing = fCreate;
- options.filter_policy = leveldb::NewBloomFilterPolicy(10);
-
- init_blockindex(options); // Init directory
- pdb = txdb;
-
- if (Exists(string("version")))
- {
- ReadVersion(nVersion);
- printf("Transaction index version is %d\n", nVersion);
-
- if (nVersion < DATABASE_VERSION)
- {
- printf("Required index version is %d, removing old database\n", DATABASE_VERSION);
-
- // Leveldb instance destruction
- delete txdb;
- txdb = pdb = NULL;
- delete activeBatch;
- activeBatch = NULL;
-
- init_blockindex(options, true); // Remove directory and create new database
- pdb = txdb;
-
- bool fTmp = fReadOnly;
- fReadOnly = false;
- WriteVersion(DATABASE_VERSION); // Save transaction index version
- fReadOnly = fTmp;
- }
- }
- else if (fCreate)
- {
- bool fTmp = fReadOnly;
- fReadOnly = false;
- WriteVersion(DATABASE_VERSION);
- fReadOnly = fTmp;
- }
-
- printf("Opened LevelDB successfully\n");
-}
-
-void CTxDB::Close()
-{
- delete txdb;
- txdb = pdb = NULL;
- delete options.filter_policy;
- options.filter_policy = NULL;
- delete options.block_cache;
- options.block_cache = NULL;
- delete activeBatch;
- activeBatch = NULL;
-}
-
-bool CTxDB::TxnBegin()
-{
- assert(!activeBatch);
- activeBatch = new leveldb::WriteBatch();
- return true;
-}
-
-bool CTxDB::TxnCommit()
-{
- assert(activeBatch);
- leveldb::Status status = pdb->Write(leveldb::WriteOptions(), activeBatch);
- delete activeBatch;
- activeBatch = NULL;
- if (!status.ok()) {
- printf("LevelDB batch commit failure: %s\n", status.ToString().c_str());
- return false;
- }
- return true;
-}
-
-class CBatchScanner : public leveldb::WriteBatch::Handler {
-public:
- std::string needle;
- bool *deleted;
- std::string *foundValue;
- bool foundEntry;
-
- CBatchScanner() : foundEntry(false) {}
-
- virtual void Put(const leveldb::Slice& key, const leveldb::Slice& value) {
- if (key.ToString() == needle) {
- foundEntry = true;
- *deleted = false;
- *foundValue = value.ToString();
- }
- }
-
- virtual void Delete(const leveldb::Slice& key) {
- if (key.ToString() == needle) {
- foundEntry = true;
- *deleted = true;
- }
- }
-};
-
-// When performing a read, if we have an active batch we need to check it first
-// before reading from the database, as the rest of the code assumes that once
-// a database transaction begins reads are consistent with it. It would be good
-// to change that assumption in future and avoid the performance hit, though in
-// practice it does not appear to be large.
-bool CTxDB::ScanBatch(const CDataStream &key, string *value, bool *deleted) const {
- assert(activeBatch);
- *deleted = false;
- CBatchScanner scanner;
- scanner.needle = key.str();
- scanner.deleted = deleted;
- scanner.foundValue = value;
- leveldb::Status status = activeBatch->Iterate(&scanner);
- if (!status.ok()) {
- throw runtime_error(status.ToString());
- }
- return scanner.foundEntry;
-}
-
-bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
-{
- assert(!fClient);
- txindex.SetNull();
- return Read(make_pair(string("tx"), hash), txindex);
-}
-
-bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
-{
- assert(!fClient);
- return Write(make_pair(string("tx"), hash), txindex);
-}
-
-bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
-{
- assert(!fClient);
-
- // Add to tx index
- uint256 hash = tx.GetHash();
- CTxIndex txindex(pos, tx.vout.size());
- return Write(make_pair(string("tx"), hash), txindex);
-}
-
-bool CTxDB::EraseTxIndex(const CTransaction& tx)
-{
- assert(!fClient);
- uint256 hash = tx.GetHash();
-
- return Erase(make_pair(string("tx"), hash));
-}
-
-bool CTxDB::ContainsTx(uint256 hash)
-{
- assert(!fClient);
- return Exists(make_pair(string("tx"), hash));
-}
-
-bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
-{
- assert(!fClient);
- tx.SetNull();
- if (!ReadTxIndex(hash, txindex))
- return false;
- return (tx.ReadFromDisk(txindex.pos));
-}
-
-bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
-{
- CTxIndex txindex;
- return ReadDiskTx(hash, tx, txindex);
-}
-
-bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
-{
- return ReadDiskTx(outpoint.hash, tx, txindex);
-}
-
-bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
-{
- CTxIndex txindex;
- return ReadDiskTx(outpoint.hash, tx, txindex);
-}
-
-bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
-{
- return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
-}
-
-bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
-{
- return Read(string("hashBestChain"), hashBestChain);
-}
-
-bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
-{
- return Write(string("hashBestChain"), hashBestChain);
-}
-
-bool CTxDB::ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust)
-{
- return Read(string("bnBestInvalidTrust"), bnBestInvalidTrust);
-}
-
-bool CTxDB::WriteBestInvalidTrust(CBigNum bnBestInvalidTrust)
-{
- return Write(string("bnBestInvalidTrust"), bnBestInvalidTrust);
-}
-
-bool CTxDB::ReadSyncCheckpoint(uint256& hashCheckpoint)
-{
- return Read(string("hashSyncCheckpoint"), hashCheckpoint);
-}
-
-bool CTxDB::WriteSyncCheckpoint(uint256 hashCheckpoint)
-{
- return Write(string("hashSyncCheckpoint"), hashCheckpoint);
-}
-
-bool CTxDB::ReadCheckpointPubKey(string& strPubKey)
-{
- return Read(string("strCheckpointPubKey"), strPubKey);
-}
-
-bool CTxDB::WriteCheckpointPubKey(const string& strPubKey)
-{
- return Write(string("strCheckpointPubKey"), strPubKey);
-}
-
-static CBlockIndex *InsertBlockIndex(uint256 hash)
-{
- if (hash == 0)
- return NULL;
-
- // Return existing
- map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
- if (mi != mapBlockIndex.end())
- return (*mi).second;
-
- // Create new
- CBlockIndex* pindexNew = new CBlockIndex();
- if (!pindexNew)
- throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
- mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
- pindexNew->phashBlock = &((*mi).first);
-
- return pindexNew;
-}
-
-bool CTxDB::LoadBlockIndex()
-{
- if (mapBlockIndex.size() > 0) {
- // Already loaded once in this session. It can happen during migration
- // from BDB.
- return true;
- }
- // The block index is an in-memory structure that maps hashes to on-disk
- // locations where the contents of the block can be found. Here, we scan it
- // out of the DB and into mapBlockIndex.
- leveldb::Iterator *iterator = pdb->NewIterator(leveldb::ReadOptions());
- // Seek to start key.
- CDataStream ssStartKey(SER_DISK, CLIENT_VERSION);
- ssStartKey << make_pair(string("blockindex"), uint256(0));
- iterator->Seek(ssStartKey.str());
- // Now read each entry.
- while (iterator->Valid())
- {
- // Unpack keys and values.
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.write(iterator->key().data(), iterator->key().size());
- CDataStream ssValue(SER_DISK, CLIENT_VERSION);
- ssValue.write(iterator->value().data(), iterator->value().size());
- string strType;
- ssKey >> strType;
- // Did we reach the end of the data to read?
- if (fRequestShutdown || strType != "blockindex")
- break;
- CDiskBlockIndex diskindex;
- ssValue >> diskindex;
-
- uint256 blockHash = diskindex.GetBlockHash();
-
- // Construct block index object
- CBlockIndex* pindexNew = InsertBlockIndex(blockHash);
- pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
- pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
- pindexNew->nHeight = diskindex.nHeight;
- pindexNew->nAlternative = diskindex.nAlternative;
- pindexNew->nMint = diskindex.nMint;
- pindexNew->nMoneySupply = diskindex.nMoneySupply;
- pindexNew->nFlags = diskindex.nFlags;
- pindexNew->nStakeModifier = diskindex.nStakeModifier;
- pindexNew->prevoutStake = diskindex.prevoutStake;
- pindexNew->nStakeTime = diskindex.nStakeTime;
- pindexNew->hashProofOfStake = diskindex.hashProofOfStake;
- pindexNew->nVersion = diskindex.nVersion;
- pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
- pindexNew->nTime = diskindex.nTime;
- pindexNew->nBits = diskindex.nBits;
- pindexNew->nNonce = diskindex.nNonce;
-
- // Watch for genesis block
- if (pindexGenesisBlock == NULL && blockHash == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet))
- pindexGenesisBlock = pindexNew;
-
- if (!pindexNew->CheckIndex()) {
- delete iterator;
- return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
- }
-
- // NovaCoin: build setStakeSeen
- if (pindexNew->IsProofOfStake())
- setStakeSeen.insert(make_pair(pindexNew->prevoutStake, pindexNew->nStakeTime));
-
- iterator->Next();
- }
- delete iterator;
-
- if (fRequestShutdown)
- return true;
-
- // Calculate nChainTrust
- vector<pair<int, CBlockIndex*> > vSortedByHeight;
- vSortedByHeight.reserve(mapBlockIndex.size());
- BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
- {
- CBlockIndex* pindex = item.second;
- vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
- }
- sort(vSortedByHeight.begin(), vSortedByHeight.end());
- BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
- {
- CBlockIndex* pindex = item.second;
- pindex->nChainTrust = (pindex->pprev ? pindex->pprev->nChainTrust : 0) + pindex->GetBlockTrust();
- // NovaCoin: calculate stake modifier checksum
- pindex->nStakeModifierChecksum = GetStakeModifierChecksum(pindex);
- if (!CheckStakeModifierCheckpoints(pindex->nHeight, pindex->nStakeModifierChecksum))
- return error("CTxDB::LoadBlockIndex() : Failed stake modifier checkpoint height=%d, modifier=0x%016"PRI64x, pindex->nHeight, pindex->nStakeModifier);
- }
-
- // Load hashBestChain pointer to end of best chain
- if (!ReadHashBestChain(hashBestChain))
- {
- if (pindexGenesisBlock == NULL)
- return true;
- return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
- }
- if (!mapBlockIndex.count(hashBestChain))
- return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
- pindexBest = mapBlockIndex[hashBestChain];
- nBestHeight = pindexBest->nHeight;
- nBestChainTrust = pindexBest->nChainTrust;
-
- printf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%s date=%s\n",
- hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, CBigNum(nBestChainTrust).ToString().c_str(),
- DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
-
- // NovaCoin: load hashSyncCheckpoint
- if (!ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint))
- return error("CTxDB::LoadBlockIndex() : hashSyncCheckpoint not loaded");
- printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());
-
- // Load bnBestInvalidTrust, OK if it doesn't exist
- CBigNum bnBestInvalidTrust;
- ReadBestInvalidTrust(bnBestInvalidTrust);
- nBestInvalidTrust = bnBestInvalidTrust.getuint256();
-
- // Verify blocks in the best chain
- int nCheckLevel = GetArg("-checklevel", 1);
- int nCheckDepth = GetArg( "-checkblocks", 2500);
- if (nCheckDepth == 0)
- nCheckDepth = 1000000000; // suffices until the year 19000
- if (nCheckDepth > nBestHeight)
- nCheckDepth = nBestHeight;
- printf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
- CBlockIndex* pindexFork = NULL;
- for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
- {
- if (fRequestShutdown || pindex->nHeight < nBestHeight-nCheckDepth)
- break;
- CBlock block;
- CDiskBlockPos blockPos = pindex->GetBlockPos();
- if (!block.ReadFromDisk(pindex))
- return error("LoadBlockIndex() : block.ReadFromDisk failed");
- // check level 1: verify block validity
- // check level 7: verify block signature too
- if (nCheckLevel>0 && !block.CheckBlock(true, true, (nCheckLevel>6)))
- {
- printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
- pindexFork = pindex->pprev;
- }
- // check level 2: verify transaction index validity
- if (nCheckLevel>1)
- {
- BOOST_FOREACH(const CTransaction &tx, block.vtx)
- {
- uint256 hashTx = tx.GetHash();
- CTxIndex txindex;
- if (ReadTxIndex(hashTx, txindex))
- {
- // check level 3: checker transaction hashes
- if (nCheckLevel>2 || blockPos != txindex.pos.blockPos)
- {
- // either an error or a duplicate transaction
- CTransaction txFound;
- if (!txFound.ReadFromDisk(txindex.pos))
- {
- printf("LoadBlockIndex() : *** cannot read mislocated transaction %s\n", hashTx.ToString().c_str());
- pindexFork = pindex->pprev;
- }
- else
- if (txFound.GetHash() != hashTx) // not a duplicate tx
- {
- printf("LoadBlockIndex(): *** invalid tx position for %s\n", hashTx.ToString().c_str());
- pindexFork = pindex->pprev;
- }
- }
- // check level 4: check whether spent txouts were spent within the main chain
- unsigned int nOutput = 0;
- if (nCheckLevel>3)
- {
- BOOST_FOREACH(const CDiskTxPos &txpos, txindex.vSpent)
- {
- if (!txpos.IsNull())
- {
- // check level 6: check whether spent txouts were spent by a valid transaction that consume them
- if (nCheckLevel>5)
- {
- CTransaction txSpend;
- if (!txSpend.ReadFromDisk(txpos))
- {
- printf("LoadBlockIndex(): *** cannot read spending transaction of %s:%i from disk\n", hashTx.ToString().c_str(), nOutput);
- pindexFork = pindex->pprev;
- }
- else if (!txSpend.CheckTransaction())
- {
- printf("LoadBlockIndex(): *** spending transaction of %s:%i is invalid\n", hashTx.ToString().c_str(), nOutput);
- pindexFork = pindex->pprev;
- }
- else
- {
- bool fFound = false;
- BOOST_FOREACH(const CTxIn &txin, txSpend.vin)
- if (txin.prevout.hash == hashTx && txin.prevout.n == nOutput)
- fFound = true;
- if (!fFound)
- {
- printf("LoadBlockIndex(): *** spending transaction of %s:%i does not spend it\n", hashTx.ToString().c_str(), nOutput);
- pindexFork = pindex->pprev;
- }
- }
- }
- }
- nOutput++;
- }
- }
- }
- // check level 5: check whether all prevouts are marked spent
- if (nCheckLevel>4)
- {
- BOOST_FOREACH(const CTxIn &txin, tx.vin)
- {
- CTxIndex txindex;
- if (ReadTxIndex(txin.prevout.hash, txindex))
- if (txindex.vSpent.size()-1 < txin.prevout.n || txindex.vSpent[txin.prevout.n].IsNull())
- {
- printf("LoadBlockIndex(): *** found unspent prevout %s:%i in %s\n", txin.prevout.hash.ToString().c_str(), txin.prevout.n, hashTx.ToString().c_str());
- pindexFork = pindex->pprev;
- }
- }
- }
- }
- }
- }
- if (pindexFork && !fRequestShutdown)
- {
- // Reorg back to the fork
- printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
- CBlock block;
- if (!block.ReadFromDisk(pindexFork))
- return error("LoadBlockIndex() : block.ReadFromDisk failed");
- CTxDB txdb;
- block.SetBestChain(txdb, pindexFork);
- }
-
- return true;
-}
+++ /dev/null
-// Copyright (c) 2009-2012 The Bitcoin Developers.
-// Authored by Google, Inc.
-// Distributed under the MIT/X11 software license, see the accompanying
-// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-
-#ifndef BITCOIN_LEVELDB_H
-#define BITCOIN_LEVELDB_H
-
-#include "main.h"
-
-#include <map>
-#include <string>
-#include <vector>
-
-#include <leveldb/db.h>
-#include <leveldb/write_batch.h>
-
-// Class that provides access to a LevelDB. Note that this class is frequently
-// instantiated on the stack and then destroyed again, so instantiation has to
-// be very cheap. Unfortunately that means, a CTxDB instance is actually just a
-// wrapper around some global state.
-//
-// A LevelDB is a key/value store that is optimized for fast usage on hard
-// disks. It prefers long read/writes to seeks and is based on a series of
-// sorted key/value mapping files that are stacked on top of each other, with
-// newer files overriding older files. A background thread compacts them
-// together when too many files stack up.
-//
-// Learn more: http://code.google.com/p/leveldb/
-class CTxDB
-{
-public:
- CTxDB(const char* pszMode="r+");
- ~CTxDB() {
- // Note that this is not the same as Close() because it deletes only
- // data scoped to this TxDB object.
- delete activeBatch;
- }
-
- // Destroys the underlying shared global state accessed by this TxDB.
- void Close();
-
-private:
- leveldb::DB *pdb; // Points to the global instance.
-
- // A batch stores up writes and deletes for atomic application. When this
- // field is non-NULL, writes/deletes go there instead of directly to disk.
- leveldb::WriteBatch *activeBatch;
- leveldb::Options options;
- bool fReadOnly;
- int nVersion;
-
-protected:
- // Returns true and sets (value,false) if activeBatch contains the given key
- // or leaves value alone and sets deleted = true if activeBatch contains a
- // delete for it.
- bool ScanBatch(const CDataStream &key, std::string *value, bool *deleted) const;
-
- template<typename K, typename T>
- bool Read(const K& key, T& value)
- {
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- std::string strValue;
-
- bool readFromDb = true;
- if (activeBatch) {
- // First we must search for it in the currently pending set of
- // changes to the db. If not found in the batch, go on to read disk.
- bool deleted = false;
- readFromDb = ScanBatch(ssKey, &strValue, &deleted) == false;
- if (deleted) {
- return false;
- }
- }
- if (readFromDb) {
- leveldb::Status status = pdb->Get(leveldb::ReadOptions(),
- ssKey.str(), &strValue);
- if (!status.ok()) {
- if (status.IsNotFound())
- return false;
- // Some unexpected error.
- printf("LevelDB read failure: %s\n", status.ToString().c_str());
- return false;
- }
- }
- // Unserialize value
- try {
- CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(),
- SER_DISK, CLIENT_VERSION);
- ssValue >> value;
- }
- catch (std::exception &e) {
- return false;
- }
- return true;
- }
-
- template<typename K, typename T>
- bool Write(const K& key, const T& value)
- {
- if (fReadOnly)
- assert(!"Write called on database in read-only mode");
-
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- CDataStream ssValue(SER_DISK, CLIENT_VERSION);
- ssValue.reserve(10000);
- ssValue << value;
-
- if (activeBatch) {
- activeBatch->Put(ssKey.str(), ssValue.str());
- return true;
- }
- leveldb::Status status = pdb->Put(leveldb::WriteOptions(), ssKey.str(), ssValue.str());
- if (!status.ok()) {
- printf("LevelDB write failure: %s\n", status.ToString().c_str());
- return false;
- }
- return true;
- }
-
- template<typename K>
- bool Erase(const K& key)
- {
- if (!pdb)
- return false;
- if (fReadOnly)
- assert(!"Erase called on database in read-only mode");
-
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- if (activeBatch) {
- activeBatch->Delete(ssKey.str());
- return true;
- }
- leveldb::Status status = pdb->Delete(leveldb::WriteOptions(), ssKey.str());
- return (status.ok() || status.IsNotFound());
- }
-
- template<typename K>
- bool Exists(const K& key)
- {
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- std::string unused;
-
- if (activeBatch) {
- bool deleted;
- if (ScanBatch(ssKey, &unused, &deleted) && !deleted) {
- return true;
- }
- }
-
-
- leveldb::Status status = pdb->Get(leveldb::ReadOptions(), ssKey.str(), &unused);
- return status.IsNotFound() == false;
- }
-
-
-public:
- bool TxnBegin();
- bool TxnCommit();
- bool TxnAbort()
- {
- delete activeBatch;
- activeBatch = NULL;
- return true;
- }
-
- bool ReadVersion(int& nVersion)
- {
- nVersion = 0;
- return Read(std::string("version"), nVersion);
- }
-
- bool WriteVersion(int nVersion)
- {
- return Write(std::string("version"), nVersion);
- }
-
- bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
- bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
- bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
- bool EraseTxIndex(const CTransaction& tx);
- bool ContainsTx(uint256 hash);
- bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
- bool ReadDiskTx(uint256 hash, CTransaction& tx);
- bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
- bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
- bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
- bool ReadHashBestChain(uint256& hashBestChain);
- bool WriteHashBestChain(uint256 hashBestChain);
- bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
- bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust);
- bool ReadSyncCheckpoint(uint256& hashCheckpoint);
- bool WriteSyncCheckpoint(uint256 hashCheckpoint);
- bool ReadCheckpointPubKey(std::string& strPubKey);
- bool WriteCheckpointPubKey(const std::string& strPubKey);
- bool LoadBlockIndex();
-private:
- bool LoadBlockIndexGuts();
-};
-
-
-#endif // BITCOIN_DB_H
+++ /dev/null
-// Copyright (c) 2009-2010 Satoshi Nakamoto
-// Copyright (c) 2009-2012 The Bitcoin developers
-// Distributed under the MIT/X11 software license, see the accompanying
-// file license.txt or http://www.opensource.org/licenses/mit-license.php.
-
-#ifndef BITCOIN_TXDB_H
-#define BITCOIN_TXDB_H
-
-#include "txdb-leveldb.h"
-
-#endif // BITCOIN_TXDB_H
return nFilesize;
}
+// this function tries to make a particular range of a file allocated (corresponding to disk space)
+// it is advisory, and the range specified in the arguments will never contain live data
+void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
+ static const char buf[65536] = {};
+ fseek(file, offset, SEEK_SET);
+ while (length > 0) {
+ unsigned int now = 65536;
+ if (length < now)
+ now = length;
+ fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
+ length -= now;
+ }
+}
+
+
void ShrinkDebugFile()
{
// Scroll debug.log if it's getting too big
bool WildcardMatch(const std::string& str, const std::string& mask);
void FileCommit(FILE *fileout);
int GetFilesize(FILE* file);
+void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
boost::filesystem::path GetDefaultDataDir();
const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#include "txdb.h"
+#include "db.h"
#include "wallet.h"
#include "walletdb.h"
#include "crypter.h"
}
}
-void CWalletTx::AddSupportingTransactions(CTxDB& txdb)
+void CWalletTx::AddSupportingTransactions()
{
vtxPrev.clear();
BOOST_FOREACH(const CTxIn& txin, vin)
vWorkQueue.push_back(txin.prevout.hash);
- // This critsect is OK because txdb is already open
{
LOCK(pwallet->cs_wallet);
map<uint256, const CMerkleTx*> mapWalletPrev;
{
tx = *mapWalletPrev[hash];
}
- else if (!fClient && txdb.ReadDiskTx(hash, tx))
- {
- ;
- }
- else
- {
- printf("ERROR: AddSupportingTransactions() : unsupported transaction\n");
- continue;
- }
int nDepth = tx.SetMerkleBranch();
vtxPrev.push_back(tx);
return ret;
}
-int CWallet::ScanForWalletTransaction(const uint256& hashTx)
-{
- CTransaction tx;
- tx.ReadFromDisk(COutPoint(hashTx, 0));
- if (AddToWalletIfInvolvingMe(tx, NULL, true, true))
- return 1;
- return 0;
-}
-
void CWallet::ReacceptWalletTransactions()
{
- CTxDB txdb("r");
+ CCoinsDB coinsdb("r");
bool fRepeat = true;
while (fRepeat)
{
LOCK(cs_wallet);
fRepeat = false;
- vector<CDiskTxPos> vMissingTx;
+ bool fMissing = false;
BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
{
CWalletTx& wtx = item.second;
- if ((wtx.IsCoinBase() && wtx.IsSpent(0)) || (wtx.IsCoinStake() && wtx.IsSpent(1)))
+ if (wtx.IsCoinBase() && wtx.IsSpent(0))
continue;
- CTxIndex txindex;
+ CCoins coins;
bool fUpdated = false;
- if (txdb.ReadTxIndex(wtx.GetHash(), txindex))
+ bool fNotFound = coinsdb.ReadCoins(wtx.GetHash(), coins);
+ if (!fNotFound || wtx.GetDepthInMainChain() > 0)
{
// Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
- if (txindex.vSpent.size() != wtx.vout.size())
- {
- printf("ERROR: ReacceptWalletTransactions() : txindex.vSpent.size() %"PRIszu" != wtx.vout.size() %"PRIszu"\n", txindex.vSpent.size(), wtx.vout.size());
- continue;
- }
- for (unsigned int i = 0; i < txindex.vSpent.size(); i++)
+ for (unsigned int i = 0; i < wtx.vout.size(); i++)
{
if (wtx.IsSpent(i))
continue;
- if (!txindex.vSpent[i].IsNull() && IsMine(wtx.vout[i]))
+ if ((i >= coins.vout.size() || coins.vout[i].IsNull()) && IsMine(wtx.vout[i]))
{
wtx.MarkSpent(i);
fUpdated = true;
- vMissingTx.push_back(txindex.vSpent[i]);
+ fMissing = true;
}
}
if (fUpdated)
{
- printf("ReacceptWalletTransactions found spent coin %snvc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
+ printf("ReacceptWalletTransactions found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
wtx.MarkDirty();
wtx.WriteToDisk();
}
else
{
// Re-accept any txes of ours that aren't already in a block
- if (!(wtx.IsCoinBase() || wtx.IsCoinStake()))
- wtx.AcceptWalletTransaction(txdb, false);
+ if (!wtx.IsCoinBase())
+ wtx.AcceptWalletTransaction(coinsdb, false);
}
}
- if (!vMissingTx.empty())
+ if (fMissing)
{
// TODO: optimize this to scan just part of the block chain?
if (ScanForWalletTransactions(pindexGenesisBlock))
}
}
-void CWalletTx::RelayWalletTransaction(CTxDB& txdb)
+void CWalletTx::RelayWalletTransaction(CCoinsDB& coinsdb)
{
BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
{
- if (!(tx.IsCoinBase() || tx.IsCoinStake()))
+ if (!tx.IsCoinBase())
{
uint256 hash = tx.GetHash();
- if (!txdb.ContainsTx(hash))
+ if (!coinsdb.HaveCoins(hash))
RelayTransaction((CTransaction)tx, hash);
}
}
- if (!(IsCoinBase() || IsCoinStake()))
+ if (!IsCoinBase())
{
uint256 hash = GetHash();
- if (!txdb.ContainsTx(hash))
+ if (!coinsdb.HaveCoins(hash))
{
printf("Relaying wtx %s\n", hash.ToString().substr(0,10).c_str());
RelayTransaction((CTransaction)*this, hash);
void CWalletTx::RelayWalletTransaction()
{
- CTxDB txdb("r");
- RelayWalletTransaction(txdb);
+ CCoinsDB coinsdb("r");
+ RelayWalletTransaction(coinsdb);
}
void CWallet::ResendWalletTransactions()
// Rebroadcast any of our txes that aren't in a block yet
printf("ResendWalletTransactions()\n");
- CTxDB txdb("r");
+ CCoinsDB coinsdb("r");
{
LOCK(cs_wallet);
// Sort them in chronological order
BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
{
CWalletTx& wtx = *item.second;
- if (wtx.CheckTransaction())
- wtx.RelayWalletTransaction(txdb);
- else
- printf("ResendWalletTransactions() : CheckTransaction failed for transaction %s\n", wtx.GetHash().ToString().c_str());
+ wtx.RelayWalletTransaction(coinsdb);
}
}
}
{
LOCK2(cs_main, cs_wallet);
- // txdb must be opened before the mapWallet lock
- CTxDB txdb("r");
{
nFeeRet = nTransactionFee;
while (true)
nFeeRet += nMoveToFee;
}
- // ppcoin: sub-cent change is moved to fee
+ // sub-cent change is moved to fee
if (nChange > 0 && nChange < MIN_TXOUT_AMOUNT)
{
nFeeRet += nChange;
// Check that enough fee is included
int64 nPayFee = nTransactionFee * (1 + (int64)nBytes / 1000);
- int64 nMinFee = wtxNew.GetMinFee(1, false, GMF_SEND, nBytes);
-
+ bool fAllowFree = CTransaction::AllowFree(dPriority);
+ int64 nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND);
if (nFeeRet < max(nPayFee, nMinFee))
{
nFeeRet = max(nPayFee, nMinFee);
}
// Fill vtxPrev by copying from previous transactions vtxPrev
- wtxNew.AddSupportingTransactions(txdb);
+ wtxNew.AddSupportingTransactions();
wtxNew.fTimeReceivedIsTxTime = true;
break;
if (setCoins.empty())
return false;
- CTxDB txdb("r");
+ CCoinsDB coinsdb("r");
+ CCoinsViewDB view(coinsdb);
BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
{
- CTxIndex txindex;
+ CCoins coins;
{
LOCK2(cs_main, cs_wallet);
- if (!txdb.ReadTxIndex(pcoin.first->GetHash(), txindex))
+ if (!view.GetCoins(pcoin.first->GetHash(), coins))
continue;
}
int64 nCredit = 0;
CScript scriptPubKeyKernel;
- CTxDB txdb("r");
+
+ CCoinsDB coinsdb("r");
+ CCoinsViewDB view(coinsdb);
BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
{
- CTxIndex txindex;
+ CCoins coins;
{
LOCK2(cs_main, cs_wallet);
- if (!txdb.ReadTxIndex(pcoin.first->GetHash(), txindex))
+ if (!view.GetCoins(pcoin.first->GetHash(), coins))
continue;
}
+ static int nMaxStakeSearchInterval = 60;
+ if (coins.nBlockTime + nStakeMinAge > txNew.nTime - nMaxStakeSearchInterval)
+ continue; // only count coins meeting min age requirement
+
// Read block header
CBlock block;
+ unsigned int nTxPos = 0;
{
LOCK2(cs_main, cs_wallet);
- if (!block.ReadFromDisk(txindex.pos.blockPos, false))
+ CBlockIndex *pindex = FindBlockByHeight(coins.nHeight);
+
+ if (!block.ReadFromDisk(pindex))
continue;
+
+ BOOST_FOREACH(const CTransaction &tx, block.vtx) {
+ if (tx.GetHash() == pcoin.first->GetHash()) {
+ break;
+ }
+ nTxPos += tx.GetSerializeSize(SER_DISK, CLIENT_VERSION);
+ }
+ nTxPos += GetSerializeSize(CBlock(), SER_DISK, CLIENT_VERSION) - (2 * GetSizeOfCompactSize(0)) + GetSizeOfCompactSize(block.vtx.size());
}
- static int nMaxStakeSearchInterval = 60;
- if (block.GetBlockTime() + nStakeMinAge > txNew.nTime - nMaxStakeSearchInterval)
- continue; // only count coins meeting min age requirement
bool fKernelFound = false;
for (unsigned int n=0; n<min(nSearchInterval,(int64)nMaxStakeSearchInterval) && !fKernelFound && !fShutdown; n++)
// Search nSearchInterval seconds back up to nMaxStakeSearchInterval
uint256 hashProofOfStake = 0, targetProofOfStake = 0;
COutPoint prevoutStake = COutPoint(pcoin.first->GetHash(), pcoin.second);
- if (CheckStakeKernelHash(nBits, block, txindex.pos.nTxPos, *pcoin.first, prevoutStake, txNew.nTime - n, hashProofOfStake, targetProofOfStake))
+ if (CheckStakeKernelHash(nBits, block, nTxPos, *pcoin.first, prevoutStake, txNew.nTime - n, hashProofOfStake, targetProofOfStake))
{
// Found a kernel
if (fDebug && GetBoolArg("-printcoinstake"))
// Calculate coin age reward
{
uint64 nCoinAge;
- CTxDB txdb("r");
- if (!txNew.GetCoinAge(txdb, nCoinAge))
+
+ CCoinsDB coindb("r");
+ CCoinsViewDB view(coindb);
+
+ if (!txNew.GetCoinAge(view, nCoinAge))
return error("CreateCoinStake : failed to calculate coin age");
nCredit += GetProofOfStakeReward(nCoinAge, nBits, txNew.nTime);
}
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
vCoins.push_back(&(*it).second);
- CTxDB txdb("r");
+ CCoinsDB coinsdb("r");
BOOST_FOREACH(CWalletTx* pcoin, vCoins)
{
// Find the corresponding transaction index
- CTxIndex txindex;
- if (!txdb.ReadTxIndex(pcoin->GetHash(), txindex))
- continue;
+ CCoins coins;
+
+ bool fNotFound = coinsdb.ReadCoins(pcoin->GetHash(), coins);
+
for (unsigned int n=0; n < pcoin->vout.size(); n++)
{
- if (IsMine(pcoin->vout[n]) && pcoin->IsSpent(n) && (txindex.vSpent.size() <= n || txindex.vSpent[n].IsNull()))
+ if (!fNotFound && IsMine(pcoin->vout[n]) && pcoin->IsSpent(n) && coins.IsAvailable(n))
{
printf("FixSpentCoins found lost coin %sppc %s[%d], %s\n",
FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing");
pcoin->WriteToDisk();
}
}
- else if (IsMine(pcoin->vout[n]) && !pcoin->IsSpent(n) && (txindex.vSpent.size() > n && !txindex.vSpent[n].IsNull()))
+ else if (!fNotFound && IsMine(pcoin->vout[n]) && !pcoin->IsSpent(n) && coins.IsAvailable(n))
{
printf("FixSpentCoins found spent coin %sppc %s[%d], %s\n",
FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing");
bool EraseFromWallet(uint256 hash);
void WalletUpdateSpent(const CTransaction& prevout);
int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
- int ScanForWalletTransaction(const uint256& hashTx);
void ReacceptWalletTransactions();
void ResendWalletTransactions();
int64 GetBalance() const;
int64 GetTxTime() const;
int GetRequestCount() const;
- void AddSupportingTransactions(CTxDB& txdb);
+ void AddSupportingTransactions();
- bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
+ bool AcceptWalletTransaction(CCoinsDB& coinsdb, bool fCheckInputs=true);
bool AcceptWalletTransaction();
- void RelayWalletTransaction(CTxDB& txdb);
+ void RelayWalletTransaction(CCoinsDB& coinsdb);
void RelayWalletTransaction();
};