Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HUGE speedup #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/db.cpp
Expand Up @@ -634,7 +634,7 @@ CBlockIndex static * InsertBlockIndex(uint256 hash)
if (!pindexNew)
throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
pindexNew->phashBlock = &((*mi).first);
pindexNew->SetHash(hash);

return pindexNew;
}
Expand Down Expand Up @@ -693,7 +693,7 @@ bool CTxDB::LoadBlockIndex()

// Verify blocks in the best chain
int nCheckLevel = GetArg("-checklevel", 1);
int nCheckDepth = GetArg( "-checkblocks", 2500);
int nCheckDepth = GetArg( "-checkblocks", 666); // down from 2500 to speed up start, but should be >520
if (nCheckDepth == 0)
nCheckDepth = 1000000000; // suffices until the year 19000
if (nCheckDepth > nBestHeight)
Expand Down
9 changes: 5 additions & 4 deletions src/main.cpp
Expand Up @@ -926,7 +926,10 @@ bool CBlock::ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions)
}
if (!ReadFromDisk(pindex->nFile, pindex->nBlockPos, fReadTransactions))
return false;
if (GetHash() != pindex->GetBlockHash())

uint256 cachedHash = GetHash();
uint256 computedHash = pindex->GetBlockHash();
if (cachedHash != computedHash)
return error("CBlock::ReadFromDisk() : GetHash() doesn't match index");
return true;
}
Expand Down Expand Up @@ -1956,7 +1959,6 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
CBlockIndex* pindexNew = new CBlockIndex(nFile, nBlockPos, *this);
if (!pindexNew)
return error("AddToBlockIndex() : new CBlockIndex failed");
pindexNew->phashBlock = &hash;
map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(hashPrevBlock);
if (miPrev != mapBlockIndex.end())
{
Expand Down Expand Up @@ -1993,7 +1995,6 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
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;
Expand Down Expand Up @@ -3268,7 +3269,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
pindex = pindex->pnext;
}

vector<CBlock> vHeaders;
vector<CBlockHeader> vHeaders;
int nLimit = 2000;
printf("getheaders %d to %s\n", (pindex ? pindex->nHeight : -1), hashStop.ToString().substr(0,20).c_str());
for (; pindex; pindex = pindex->pnext)
Expand Down
180 changes: 96 additions & 84 deletions src/main.h
Expand Up @@ -820,7 +820,78 @@ class CTxIndex
};


class CBlockHeader
{
public:
static const int CURRENT_VERSION=3;
int nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
unsigned int nTime;
unsigned int nBits;
unsigned int nNonce;

// memory only
mutable bool fHashed;
mutable uint256 hashCached;

CBlockHeader()
{
fHashed = false;
SetNull();
}

IMPLEMENT_SERIALIZE
(
READWRITE(this->nVersion);
nVersion = this->nVersion;
READWRITE(hashPrevBlock);
READWRITE(hashMerkleRoot);
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
)

void SetNull()
{
fHashed = false;
nVersion = CURRENT_VERSION;
hashPrevBlock = 0;
hashMerkleRoot = 0;
nTime = 0;
nBits = 0;
nNonce = 0;
fHashed = false;
}

void SetHash(uint256 h) const
{
if (!fHashed) {
hashCached = h;
fHashed = true;
}
}

uint256 GetHash() const
{
if (!fHashed) {
scrypt_hash(CVOIDBEGIN(nVersion), sizeof(block_header), UINTBEGIN(hashCached), GetNfactor(nTime));
fHashed = true;
}

return hashCached;
}

int64 GetBlockTime() const
{
return (int64)nTime;
}

bool IsNull() const
{
return (nBits == 0);
}
};


/** Nodes collect new transactions into a block, hash them into a hash tree,
Expand All @@ -833,17 +904,9 @@ class CTxIndex
* Blocks are appended to blk0001.dat files on disk. Their location on disk
* is indexed by CBlockIndex objects in memory.
*/
class CBlock
class CBlock : public CBlockHeader
{
public:
// header
static const int CURRENT_VERSION=3;
int nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
unsigned int nTime;
unsigned int nBits;
unsigned int nNonce;

// network and disk
std::vector<CTransaction> vtx;
Expand All @@ -860,6 +923,14 @@ class CBlock

CBlock()
{
fHashed = false;
SetNull();
}

CBlock(const CBlockHeader &blockHeader)
: CBlockHeader(blockHeader)
{
fHashed = false;
SetNull();
}

Expand Down Expand Up @@ -888,40 +959,15 @@ class CBlock

void SetNull()
{
nVersion = CBlock::CURRENT_VERSION;
hashPrevBlock = 0;
hashMerkleRoot = 0;
nTime = 0;
nBits = 0;
nNonce = 0;
fHashed = false;
CBlockHeader::SetNull();

vtx.clear();
vchBlockSig.clear();
vMerkleTree.clear();
nDoS = 0;
}

bool IsNull() const
{
return (nBits == 0);
}

uint256 GetHash() const
{
uint256 thash;
//void * scratchbuff = scrypt_buffer_alloc();

scrypt_hash(CVOIDBEGIN(nVersion), sizeof(block_header), UINTBEGIN(thash), GetNfactor(nTime));

//scrypt_buffer_free(scratchbuff);

return thash;
}

int64 GetBlockTime() const
{
return (int64)nTime;
}

void UpdateTime(const CBlockIndex* pindexPrev);

// ppcoin: entropy bit for stake modifier if chosen by modifier
Expand Down Expand Up @@ -1127,10 +1173,9 @@ class CBlock
* to it, but pnext will only point forward to the longest branch, or will
* be null if the block is not part of the longest chain.
*/
class CBlockIndex
class CBlockIndex : public CBlockHeader
{
public:
const uint256* phashBlock;
CBlockIndex* pprev;
CBlockIndex* pnext;
unsigned int nFile;
Expand All @@ -1157,16 +1202,11 @@ class CBlockIndex
unsigned int nStakeTime;
uint256 hashProofOfStake;

// block header
int nVersion;
uint256 hashMerkleRoot;
unsigned int nTime;
unsigned int nBits;
unsigned int nNonce;

CBlockIndex()
{
phashBlock = NULL;
fHashed = false;
CBlockHeader::SetNull();

pprev = NULL;
pnext = NULL;
nFile = 0;
Expand All @@ -1181,17 +1221,11 @@ class CBlockIndex
hashProofOfStake = 0;
prevoutStake.SetNull();
nStakeTime = 0;

nVersion = 0;
hashMerkleRoot = 0;
nTime = 0;
nBits = 0;
nNonce = 0;
}

CBlockIndex(unsigned int nFileIn, unsigned int nBlockPosIn, CBlock& block)
: CBlockHeader(block)
{
phashBlock = NULL;
pprev = NULL;
pnext = NULL;
nFile = nFileIn;
Expand All @@ -1215,35 +1249,16 @@ class CBlockIndex
prevoutStake.SetNull();
nStakeTime = 0;
}

nVersion = block.nVersion;
hashMerkleRoot = block.hashMerkleRoot;
nTime = block.nTime;
nBits = block.nBits;
nNonce = block.nNonce;
}

CBlock GetBlockHeader() const
const CBlockHeader & GetBlockHeader() const
{
CBlock block;
block.nVersion = nVersion;
if (pprev)
block.hashPrevBlock = pprev->GetBlockHash();
block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
return block;
return *this;
}

uint256 GetBlockHash() const
{
return *phashBlock;
}

int64 GetBlockTime() const
{
return (int64)nTime;
return GetHash();
}

CBigNum GetBlockTrust() const
Expand Down Expand Up @@ -1371,6 +1386,7 @@ class CDiskBlockIndex : public CBlockIndex

CDiskBlockIndex()
{
fHashed = false;
hashPrev = 0;
hashNext = 0;
}
Expand All @@ -1386,6 +1402,9 @@ class CDiskBlockIndex : public CBlockIndex
if (!(nType & SER_GETHASH))
READWRITE(nVersion);

READWRITE(hashCached);
SetHash(hashCached); // sets the hash if reading from disk; does nothing if writing

READWRITE(hashNext);
READWRITE(nFile);
READWRITE(nBlockPos);
Expand Down Expand Up @@ -1418,14 +1437,7 @@ class CDiskBlockIndex : public CBlockIndex

uint256 GetBlockHash() const
{
CBlock block;
block.nVersion = nVersion;
block.hashPrevBlock = hashPrev;
block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
return block.GetHash();
return GetHash();
}


Expand Down
30 changes: 26 additions & 4 deletions src/makefile.mingw
Expand Up @@ -25,10 +25,32 @@ LIBS= \
-l ssl \
-l crypto

DEFS=-DWIN32 -D_WINDOWS -DBOOST_THREAD_USE_LIB -DBOOST_SPIRIT_THREADSAFE
DEBUGFLAGS=-g
CFLAGS=-mthreads -O2 -msse2 -w -Wall -Wextra -Wformat -Wformat-security -Wno-unused-parameter $(DEBUGFLAGS) $(DEFS) $(INCLUDEPATHS)
LDFLAGS=-Wl,--dynamicbase -Wl,--nxcompat
DEFS= \
-DWIN32 \
-D_WINDOWS \
-DBOOST_THREAD_USE_LIB \
-DBOOST_SPIRIT_THREADSAFE \
-DSCRYPT_CHACHA \
-DSCRYPT_KECCAK512 \
-DSCRYPT_CHOOSE_COMPILETIME \
-DO2

DEBUGFLAGS=#-g

CFLAGS= \
-mthreads \
-O2 \
-msse2 \
-w -Wall -Wextra -Wformat -Wformat-security -Wno-unused-parameter \
$(DEBUGFLAGS) \
$(DEFS) \
$(INCLUDEPATHS)

LDFLAGS= \
-Wl,--dynamicbase \
-Wl,--nxcompat \
-Wl,--large-address-aware \
-static

TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data)

Expand Down
2 changes: 1 addition & 1 deletion src/makefile.unix
Expand Up @@ -89,7 +89,7 @@ LIBS+= \
#


DEBUGFLAGS=-g
DEBUGFLAGS=#-g -pg

# CXXFLAGS can be specified on the make command line, so we use xCXXFLAGS that only
# adds some defaults in front. Unfortunately, CXXFLAGS=... $(CXXFLAGS) does not work.
Expand Down
2 changes: 1 addition & 1 deletion src/net.cpp
Expand Up @@ -67,7 +67,7 @@ static bool vfLimited[NET_MAX] = {};
static CNode* pnodeLocalHost = NULL;
CAddress addrSeenByPeer(CService("0.0.0.0", 0), nLocalServices);
uint64 nLocalHostNonce = 0;
array<int, THREAD_MAX> vnThreadsRunning;
boost::array<int, THREAD_MAX> vnThreadsRunning;
static std::vector<SOCKET> vhListenSocket;
CAddrMan addrman;

Expand Down