mirror of
https://github.com/kvazar-network/kevacoin.git
synced 2025-01-11 15:48:05 +00:00
Merge pull request #4420 from sipa/skiplist
Add a skiplist to the CBlockIndex structure.
This commit is contained in:
commit
6fba25ef26
@ -47,6 +47,7 @@ BITCOIN_TESTS =\
|
|||||||
test/script_tests.cpp \
|
test/script_tests.cpp \
|
||||||
test/serialize_tests.cpp \
|
test/serialize_tests.cpp \
|
||||||
test/sigopcount_tests.cpp \
|
test/sigopcount_tests.cpp \
|
||||||
|
test/skiplist_tests.cpp \
|
||||||
test/test_bitcoin.cpp \
|
test/test_bitcoin.cpp \
|
||||||
test/transaction_tests.cpp \
|
test/transaction_tests.cpp \
|
||||||
test/uint256_tests.cpp \
|
test/uint256_tests.cpp \
|
||||||
|
105
src/main.cpp
105
src/main.cpp
@ -207,6 +207,10 @@ struct CNodeState {
|
|||||||
std::string name;
|
std::string name;
|
||||||
// List of asynchronously-determined block rejections to notify this peer about.
|
// List of asynchronously-determined block rejections to notify this peer about.
|
||||||
std::vector<CBlockReject> rejects;
|
std::vector<CBlockReject> rejects;
|
||||||
|
// The best known block we know this peer has announced.
|
||||||
|
CBlockIndex *pindexBestKnownBlock;
|
||||||
|
// The hash of the last unknown block this peer has announced.
|
||||||
|
uint256 hashLastUnknownBlock;
|
||||||
list<QueuedBlock> vBlocksInFlight;
|
list<QueuedBlock> vBlocksInFlight;
|
||||||
int nBlocksInFlight;
|
int nBlocksInFlight;
|
||||||
list<uint256> vBlocksToDownload;
|
list<uint256> vBlocksToDownload;
|
||||||
@ -217,6 +221,8 @@ struct CNodeState {
|
|||||||
CNodeState() {
|
CNodeState() {
|
||||||
nMisbehavior = 0;
|
nMisbehavior = 0;
|
||||||
fShouldBan = false;
|
fShouldBan = false;
|
||||||
|
pindexBestKnownBlock = NULL;
|
||||||
|
hashLastUnknownBlock = uint256(0);
|
||||||
nBlocksToDownload = 0;
|
nBlocksToDownload = 0;
|
||||||
nBlocksInFlight = 0;
|
nBlocksInFlight = 0;
|
||||||
nLastBlockReceive = 0;
|
nLastBlockReceive = 0;
|
||||||
@ -313,6 +319,39 @@ void MarkBlockAsInFlight(NodeId nodeid, const uint256 &hash) {
|
|||||||
mapBlocksInFlight[hash] = std::make_pair(nodeid, it);
|
mapBlocksInFlight[hash] = std::make_pair(nodeid, it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Check whether the last unknown block a peer advertized is not yet known. */
|
||||||
|
void ProcessBlockAvailability(NodeId nodeid) {
|
||||||
|
CNodeState *state = State(nodeid);
|
||||||
|
assert(state != NULL);
|
||||||
|
|
||||||
|
if (state->hashLastUnknownBlock != 0) {
|
||||||
|
map<uint256, CBlockIndex*>::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
|
||||||
|
if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
|
||||||
|
if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
|
||||||
|
state->pindexBestKnownBlock = itOld->second;
|
||||||
|
state->hashLastUnknownBlock = uint256(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Update tracking information about which blocks a peer is assumed to have. */
|
||||||
|
void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
|
||||||
|
CNodeState *state = State(nodeid);
|
||||||
|
assert(state != NULL);
|
||||||
|
|
||||||
|
ProcessBlockAvailability(nodeid);
|
||||||
|
|
||||||
|
map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(hash);
|
||||||
|
if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
|
||||||
|
// An actually better block was announced.
|
||||||
|
if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
|
||||||
|
state->pindexBestKnownBlock = it->second;
|
||||||
|
} else {
|
||||||
|
// An unknown block was announced; just assume that the latest one is the best one.
|
||||||
|
state->hashLastUnknownBlock = hash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // anon namespace
|
} // anon namespace
|
||||||
|
|
||||||
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
|
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
|
||||||
@ -321,6 +360,7 @@ bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
|
|||||||
if (state == NULL)
|
if (state == NULL)
|
||||||
return false;
|
return false;
|
||||||
stats.nMisbehavior = state->nMisbehavior;
|
stats.nMisbehavior = state->nMisbehavior;
|
||||||
|
stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -374,8 +414,11 @@ CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
|
|||||||
break;
|
break;
|
||||||
// Exponentially larger steps back, plus the genesis block.
|
// Exponentially larger steps back, plus the genesis block.
|
||||||
int nHeight = std::max(pindex->nHeight - nStep, 0);
|
int nHeight = std::max(pindex->nHeight - nStep, 0);
|
||||||
|
// Jump back quickly to the same height as the chain.
|
||||||
|
if (pindex->nHeight > nHeight)
|
||||||
|
pindex = pindex->GetAncestor(nHeight);
|
||||||
// In case pindex is not in this chain, iterate pindex->pprev to find blocks.
|
// In case pindex is not in this chain, iterate pindex->pprev to find blocks.
|
||||||
while (pindex->nHeight > nHeight && !Contains(pindex))
|
while (!Contains(pindex))
|
||||||
pindex = pindex->pprev;
|
pindex = pindex->pprev;
|
||||||
// If pindex is in this chain, use direct height-based access.
|
// If pindex is in this chain, use direct height-based access.
|
||||||
if (pindex->nHeight > nHeight)
|
if (pindex->nHeight > nHeight)
|
||||||
@ -402,6 +445,8 @@ CBlockIndex *CChain::FindFork(const CBlockLocator &locator) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
CBlockIndex *CChain::FindFork(CBlockIndex *pindex) const {
|
CBlockIndex *CChain::FindFork(CBlockIndex *pindex) const {
|
||||||
|
if (pindex->nHeight > Height())
|
||||||
|
pindex = pindex->GetAncestor(Height());
|
||||||
while (pindex && !Contains(pindex))
|
while (pindex && !Contains(pindex))
|
||||||
pindex = pindex->pprev;
|
pindex = pindex->pprev;
|
||||||
return pindex;
|
return pindex;
|
||||||
@ -2111,6 +2156,7 @@ CBlockIndex* AddToBlockIndex(CBlockHeader& block)
|
|||||||
{
|
{
|
||||||
pindexNew->pprev = (*miPrev).second;
|
pindexNew->pprev = (*miPrev).second;
|
||||||
pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
|
pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
|
||||||
|
pindexNew->BuildSkip();
|
||||||
}
|
}
|
||||||
pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + pindexNew->GetBlockWork();
|
pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + pindexNew->GetBlockWork();
|
||||||
pindexNew->RaiseValidity(BLOCK_VALID_TREE);
|
pindexNew->RaiseValidity(BLOCK_VALID_TREE);
|
||||||
@ -2468,6 +2514,55 @@ bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, uns
|
|||||||
return (nFound >= nRequired);
|
return (nFound >= nRequired);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
|
||||||
|
int static inline InvertLowestOne(int n) { return n & (n - 1); }
|
||||||
|
|
||||||
|
/** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
|
||||||
|
int static inline GetSkipHeight(int height) {
|
||||||
|
if (height < 2)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// Determine which height to jump back to. Any number strictly lower than height is acceptable,
|
||||||
|
// but the following expression seems to perform well in simulations (max 110 steps to go back
|
||||||
|
// up to 2**18 blocks).
|
||||||
|
return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
|
||||||
|
}
|
||||||
|
|
||||||
|
CBlockIndex* CBlockIndex::GetAncestor(int height)
|
||||||
|
{
|
||||||
|
if (height > nHeight || height < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
CBlockIndex* pindexWalk = this;
|
||||||
|
int heightWalk = nHeight;
|
||||||
|
while (heightWalk > height) {
|
||||||
|
int heightSkip = GetSkipHeight(heightWalk);
|
||||||
|
int heightSkipPrev = GetSkipHeight(heightWalk - 1);
|
||||||
|
if (heightSkip == height ||
|
||||||
|
(heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
|
||||||
|
heightSkipPrev >= height))) {
|
||||||
|
// Only follow pskip if pprev->pskip isn't better than pskip->pprev.
|
||||||
|
pindexWalk = pindexWalk->pskip;
|
||||||
|
heightWalk = heightSkip;
|
||||||
|
} else {
|
||||||
|
pindexWalk = pindexWalk->pprev;
|
||||||
|
heightWalk--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pindexWalk;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
|
||||||
|
{
|
||||||
|
return const_cast<CBlockIndex*>(this)->GetAncestor(height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CBlockIndex::BuildSkip()
|
||||||
|
{
|
||||||
|
if (pprev)
|
||||||
|
pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
|
||||||
|
}
|
||||||
|
|
||||||
void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)
|
void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)
|
||||||
{
|
{
|
||||||
AssertLockHeld(cs_main);
|
AssertLockHeld(cs_main);
|
||||||
@ -2818,6 +2913,8 @@ bool static LoadBlockIndexDB()
|
|||||||
setBlockIndexValid.insert(pindex);
|
setBlockIndexValid.insert(pindex);
|
||||||
if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
|
if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
|
||||||
pindexBestInvalid = pindex;
|
pindexBestInvalid = pindex;
|
||||||
|
if (pindex->pprev)
|
||||||
|
pindex->BuildSkip();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load block file info
|
// Load block file info
|
||||||
@ -3613,6 +3710,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
|
|||||||
PushGetBlocks(pfrom, chainActive.Tip(), GetOrphanRoot(inv.hash));
|
PushGetBlocks(pfrom, chainActive.Tip(), GetOrphanRoot(inv.hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (inv.type == MSG_BLOCK)
|
||||||
|
UpdateBlockAvailability(pfrom->GetId(), inv.hash);
|
||||||
|
|
||||||
// Track requests for our stuff
|
// Track requests for our stuff
|
||||||
g_signals.Inventory(inv.hash);
|
g_signals.Inventory(inv.hash);
|
||||||
}
|
}
|
||||||
@ -4359,6 +4459,9 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
|
|||||||
pto->fDisconnect = true;
|
pto->fDisconnect = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update knowledge of peer's block availability.
|
||||||
|
ProcessBlockAvailability(pto->GetId());
|
||||||
|
|
||||||
//
|
//
|
||||||
// Message: getdata (blocks)
|
// Message: getdata (blocks)
|
||||||
//
|
//
|
||||||
|
15
src/main.h
15
src/main.h
@ -185,6 +185,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
|
|||||||
|
|
||||||
struct CNodeStateStats {
|
struct CNodeStateStats {
|
||||||
int nMisbehavior;
|
int nMisbehavior;
|
||||||
|
int nSyncHeight;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CDiskBlockPos
|
struct CDiskBlockPos
|
||||||
@ -676,6 +677,9 @@ public:
|
|||||||
// pointer to the index of the predecessor of this block
|
// pointer to the index of the predecessor of this block
|
||||||
CBlockIndex* pprev;
|
CBlockIndex* pprev;
|
||||||
|
|
||||||
|
// pointer to the index of some further predecessor of this block
|
||||||
|
CBlockIndex* pskip;
|
||||||
|
|
||||||
// height of the entry in the chain. The genesis block has height 0
|
// height of the entry in the chain. The genesis block has height 0
|
||||||
int nHeight;
|
int nHeight;
|
||||||
|
|
||||||
@ -715,6 +719,7 @@ public:
|
|||||||
{
|
{
|
||||||
phashBlock = NULL;
|
phashBlock = NULL;
|
||||||
pprev = NULL;
|
pprev = NULL;
|
||||||
|
pskip = NULL;
|
||||||
nHeight = 0;
|
nHeight = 0;
|
||||||
nFile = 0;
|
nFile = 0;
|
||||||
nDataPos = 0;
|
nDataPos = 0;
|
||||||
@ -736,6 +741,7 @@ public:
|
|||||||
{
|
{
|
||||||
phashBlock = NULL;
|
phashBlock = NULL;
|
||||||
pprev = NULL;
|
pprev = NULL;
|
||||||
|
pskip = NULL;
|
||||||
nHeight = 0;
|
nHeight = 0;
|
||||||
nFile = 0;
|
nFile = 0;
|
||||||
nDataPos = 0;
|
nDataPos = 0;
|
||||||
@ -868,10 +874,15 @@ public:
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build the skiplist pointer for this entry.
|
||||||
|
void BuildSkip();
|
||||||
|
|
||||||
|
// Efficiently find an ancestor of this block.
|
||||||
|
CBlockIndex* GetAncestor(int height);
|
||||||
|
const CBlockIndex* GetAncestor(int height) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Used to marshal pointers into hashes for db storage. */
|
/** Used to marshal pointers into hashes for db storage. */
|
||||||
class CDiskBlockIndex : public CBlockIndex
|
class CDiskBlockIndex : public CBlockIndex
|
||||||
{
|
{
|
||||||
|
@ -134,6 +134,7 @@ Value getpeerinfo(const Array& params, bool fHelp)
|
|||||||
obj.push_back(Pair("startingheight", stats.nStartingHeight));
|
obj.push_back(Pair("startingheight", stats.nStartingHeight));
|
||||||
if (fStateStats) {
|
if (fStateStats) {
|
||||||
obj.push_back(Pair("banscore", statestats.nMisbehavior));
|
obj.push_back(Pair("banscore", statestats.nMisbehavior));
|
||||||
|
obj.push_back(Pair("syncheight", statestats.nSyncHeight));
|
||||||
}
|
}
|
||||||
obj.push_back(Pair("syncnode", stats.fSyncNode));
|
obj.push_back(Pair("syncnode", stats.fSyncNode));
|
||||||
|
|
||||||
|
45
src/test/skiplist_tests.cpp
Normal file
45
src/test/skiplist_tests.cpp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// Copyright (c) 2014 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT/X11 software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include <vector>
|
||||||
|
#include "main.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define SKIPLIST_LENGTH 300000
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(skiplist_tests)
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(skiplist_test)
|
||||||
|
{
|
||||||
|
std::vector<CBlockIndex> vIndex(SKIPLIST_LENGTH);
|
||||||
|
|
||||||
|
for (int i=0; i<SKIPLIST_LENGTH; i++) {
|
||||||
|
vIndex[i].nHeight = i;
|
||||||
|
vIndex[i].pprev = (i == 0) ? NULL : &vIndex[i - 1];
|
||||||
|
vIndex[i].BuildSkip();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0; i<SKIPLIST_LENGTH; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
|
||||||
|
BOOST_CHECK(vIndex[i].pskip->nHeight < i);
|
||||||
|
} else {
|
||||||
|
BOOST_CHECK(vIndex[i].pskip == NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0; i < 1000; i++) {
|
||||||
|
int from = insecure_rand() % (SKIPLIST_LENGTH - 1);
|
||||||
|
int to = insecure_rand() % (from + 1);
|
||||||
|
|
||||||
|
BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]);
|
||||||
|
BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
|
||||||
|
BOOST_CHECK(vIndex[from].GetAncestor(0) == &vIndex[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user