@ -72,11 +72,11 @@ h256Set EthereumHost::neededBlocks(h256Set const& _exclude)
m_blocksNeeded . erase ( it ) ;
}
}
if ( ! ret . siz e( ) )
if ( ret . empty ( ) )
for ( auto i = m_blocksOnWay . begin ( ) ; ret . size ( ) < c_maxBlocksAsk & & i ! = m_blocksOnWay . end ( ) & & ! _exclude . count ( * i ) ; + + i )
ret . insert ( * i ) ;
clog ( NetMessageSummary ) < < " Asking for " < < ret . size ( ) < < " blocks that we don't yet have. " < < m_blocksNeeded . size ( ) < < " blocks still needed, " < < m_blocksOnWay . size ( ) < < " blocks on way. " ;
if ( ret . size ( ) )
clog ( NetMessageSummary ) < < " Asking for " < < ret . size ( ) < < " blocks that we don't yet have. " < < m_blocksNeeded . size ( ) < < " blocks still needed, " < < m_blocksOnWay . size ( ) < < " total blocks on way." ;
return ret ;
}
@ -95,6 +95,83 @@ bool EthereumHost::ensureInitialised(TransactionQueue& _tq)
return false ;
}
void EthereumHost : : noteHavePeerState ( EthereumPeer * _who )
{
clog ( NetAllDetail ) < < " Have peer state. " ;
// if already downloading hash-chain, ignore.
if ( m_grabbing ! = Grabbing : : Nothing )
{
clog ( NetAllDetail ) < < " Already downloading chain. Just set to help out. " ;
_who - > restartGettingChain ( ) ;
return ;
}
// otherwise check to see if we should be downloading...
_who - > tryGrabbingHashChain ( ) ;
}
void EthereumHost : : updateGrabbing ( Grabbing _g )
{
m_grabbing = _g ;
if ( _g = = Grabbing : : Nothing )
readyForSync ( ) ;
else if ( _g = = Grabbing : : Chain )
for ( auto j : peers ( ) )
j - > cap < EthereumPeer > ( ) - > restartGettingChain ( ) ;
}
void EthereumHost : : noteHaveChain ( EthereumPeer * _from )
{
auto td = _from - > m_totalDifficulty ;
if ( _from - > m_neededBlocks . empty ( ) )
{
_from - > m_grabbing = Grabbing : : Nothing ;
updateGrabbing ( Grabbing : : Nothing ) ;
return ;
}
clog ( NetNote ) < < " Hash-chain COMPLETE: " < < _from - > m_totalDifficulty < < " vs " < < m_chain . details ( ) . totalDifficulty < < " , " < < m_totalDifficultyOfNeeded < < " ; " < < _from - > m_neededBlocks . size ( ) < < " blocks, ends " < < _from - > m_neededBlocks . back ( ) . abridged ( ) ;
if ( ( m_totalDifficultyOfNeeded & & ( td < m_totalDifficultyOfNeeded | | ( td = = m_totalDifficultyOfNeeded & & m_latestBlockSent = = _from - > m_latestHash ) ) ) | | td < m_chain . details ( ) . totalDifficulty | | ( td = = m_chain . details ( ) . totalDifficulty & & m_chain . currentHash ( ) = = _from - > m_latestHash ) )
{
clog ( NetNote ) < < " Difficulty of hashchain not HIGHER. Ignoring. " ;
_from - > m_grabbing = Grabbing : : Nothing ;
updateGrabbing ( Grabbing : : Nothing ) ;
return ;
}
clog ( NetNote ) < < " Difficulty of hashchain HIGHER. Replacing fetch queue [latest now " < < _from - > m_latestHash . abridged ( ) < < " , was " < < m_latestBlockSent . abridged ( ) < < " ] " ;
// Looks like it's the best yet for total difficulty. Set to download.
{
Guard l ( x_blocksNeeded ) ;
m_blocksNeeded = _from - > m_neededBlocks ;
m_blocksOnWay . clear ( ) ;
m_totalDifficultyOfNeeded = td ;
m_latestBlockSent = _from - > m_latestHash ;
}
_from - > m_grabbing = Grabbing : : Chain ;
updateGrabbing ( Grabbing : : Chain ) ;
}
void EthereumHost : : readyForSync ( )
{
// start grabbing next hash chain if there is one.
for ( auto j : peers ( ) )
{
j - > cap < EthereumPeer > ( ) - > tryGrabbingHashChain ( ) ;
if ( j - > cap < EthereumPeer > ( ) - > m_grabbing = = Grabbing : : Hashes )
{
m_grabbing = Grabbing : : Hashes ;
return ;
}
}
clog ( NetNote ) < < " No more peers to sync with. " ;
}
void EthereumHost : : noteDoneBlocks ( )
{
if ( m_blocksOnWay . empty ( ) )
@ -104,7 +181,7 @@ void EthereumHost::noteDoneBlocks()
clog ( NetNote ) < < " No more blocks coming. Missing " < < m_blocksNeeded . size ( ) < < " blocks. " ;
else
clog ( NetNote ) < < " No more blocks to get. " ;
m_latestBlockSent = m_chain . currentHash ( ) ;
updateGrabbing ( Grabbing : : Nothing ) ;
}
}
@ -129,6 +206,7 @@ void EthereumHost::doWork()
maintainBlocks ( m_bq , h ) ;
// return netChange;
// TODO: Figure out what to do with netChange.
( void ) netChange ;
}
void EthereumHost : : maintainTransactions ( TransactionQueue & _tq , h256 _currentHash )
@ -175,6 +253,21 @@ void EthereumHost::maintainTransactions(TransactionQueue& _tq, h256 _currentHash
}
}
void EthereumHost : : reset ( )
{
m_grabbing = Grabbing : : Nothing ;
m_incomingTransactions . clear ( ) ;
m_incomingBlocks . clear ( ) ;
m_totalDifficultyOfNeeded = 0 ;
m_blocksNeeded . clear ( ) ;
m_blocksOnWay . clear ( ) ;
m_latestBlockSent = h256 ( ) ;
m_transactionsSent . clear ( ) ;
}
void EthereumHost : : maintainBlocks ( BlockQueue & _bq , h256 _currentHash )
{
// Import new blocks
@ -187,14 +280,8 @@ void EthereumHost::maintainBlocks(BlockQueue& _bq, h256 _currentHash)
m_incomingBlocks . clear ( ) ;
}
// If we've finished our initial sync...
{
Guard l ( x_blocksNeeded ) ;
if ( m_blocksOnWay . size ( ) )
return ;
}
// ...send any new blocks.
if ( m_latestBlockSent ! = _currentHash )
// If we've finished our initial sync send any new blocks.
if ( m_grabbing = = Grabbing : : Nothing & & m_chain . details ( m_latestBlockSent ) & & m_chain . details ( m_latestBlockSent ) . totalDifficulty < m_chain . details ( _currentHash ) . totalDifficulty )
{
RLPStream ts ;
EthereumPeer : : prep ( ts ) ;
@ -222,32 +309,3 @@ void EthereumHost::maintainBlocks(BlockQueue& _bq, h256 _currentHash)
m_latestBlockSent = _currentHash ;
}
}
void EthereumHost : : noteHaveChain ( EthereumPeer * _from )
{
auto td = _from - > m_totalDifficulty ;
if ( _from - > m_neededBlocks . empty ( ) )
return ;
clog ( NetNote ) < < " Hash-chain COMPLETE: " < < _from - > m_totalDifficulty < < " vs " < < m_chain . details ( ) . totalDifficulty < < " , " < < m_totalDifficultyOfNeeded < < " ; " < < _from - > m_neededBlocks . size ( ) < < " blocks, ends " < < _from - > m_neededBlocks . back ( ) . abridged ( ) ;
if ( ( m_totalDifficultyOfNeeded & & td < m_totalDifficultyOfNeeded ) | | td < m_chain . details ( ) . totalDifficulty )
{
clog ( NetNote ) < < " Difficulty of hashchain LOWER. Ignoring. " ;
return ;
}
clog ( NetNote ) < < " Difficulty of hashchain HIGHER. Replacing fetch queue. " ;
// Looks like it's the best yet for total difficulty. Set to download.
{
Guard l ( x_blocksNeeded ) ;
m_blocksNeeded = _from - > m_neededBlocks ;
m_blocksOnWay . clear ( ) ;
m_totalDifficultyOfNeeded = td ;
}
for ( auto j : peers ( ) )
j - > cap < EthereumPeer > ( ) - > restartGettingChain ( ) ;
}