Track peers' available blocks

This commit is contained in:
Pieter Wuille 2014-06-23 00:00:26 +02:00
parent a11f648585
commit aa81564700
3 changed files with 48 additions and 0 deletions

View File

@ -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;
} }
@ -3613,6 +3653,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 +4402,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)
// //

View File

@ -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

View File

@ -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));