@ -91,7 +91,10 @@ void EthereumHost::doWork()
bool netChange = ensureInitialised ( ) ;
auto h = m_chain . currentHash ( ) ;
// If we've finished our initial sync (including getting all the blocks into the chain so as to reduce invalid transactions), start trading transactions & blocks
if ( ! isSyncing ( ) & & m_chain . isKnown ( m_latestBlockSent ) )
bool syncing = false ;
DEV_GUARDED ( x_sync )
syncing = isSyncing ( ) ;
if ( syncing & & m_chain . isKnown ( m_latestBlockSent ) )
{
if ( m_newTransactions )
{
@ -241,7 +244,6 @@ void EthereumHost::onPeerStatus(EthereumPeer* _peer)
_peer - > disable ( " Peer banned for previous bad behaviour. " ) ;
else
{
_peer - > m_protocolVersion = EthereumHost : : c_oldProtocolVersion ; //force V60 for now
if ( _peer - > m_protocolVersion ! = protocolVersion ( ) )
estimatePeerHashes ( _peer ) ;
else if ( _peer - > m_latestBlockNumber > m_chain . number ( ) )
@ -283,6 +285,7 @@ void EthereumHost::onPeerHashes(EthereumPeer* _peer, h256s const& _hashes, bool
unsigned knowns = 0 ;
unsigned unknowns = 0 ;
h256s neededBlocks ;
bool syncByNumber = ! m_syncingLatestHash ;
for ( unsigned i = 0 ; i < _hashes . size ( ) ; + + i )
{
_peer - > addRating ( 1 ) ;
@ -290,11 +293,15 @@ void EthereumHost::onPeerHashes(EthereumPeer* _peer, h256s const& _hashes, bool
auto status = m_bq . blockStatus ( h ) ;
if ( status = = QueueStatus : : Importing | | status = = QueueStatus : : Ready | | m_chain . isKnown ( h ) )
{
clog ( NetMessageSummary ) < < " block hash ready: " < < h < < " . Start blocks download... " ;
clog ( NetMessageSummary ) < < " Block hash already known: " < < h ;
if ( ! syncByNumber )
{
m_hashes + = neededBlocks ;
clog ( NetMessageSummary ) < < " Start blocks download... " ;
onPeerDoneHashes ( _peer , true ) ;
return ;
}
}
else if ( status = = QueueStatus : : Bad )
{
cwarn < < " block hash bad! " < < h < < " . Bailing... " ;
@ -308,65 +315,25 @@ void EthereumHost::onPeerHashes(EthereumPeer* _peer, h256s const& _hashes, bool
}
else
knowns + + ;
if ( ! syncByNumber )
m_syncingLatestHash = h ;
}
m_hashes + = neededBlocks ;
clog ( NetMessageSummary ) < < knowns < < " knowns, " < < unknowns < < " unknowns; now at " < < m_syncingLatestHash ;
if ( _complete )
if ( syncByNumber )
{
m_needSyncBlocks = true ;
continueSync ( _peer ) ;
}
else if ( m_hashes . size ( ) > _peer - > m_expectedHashes )
{
_peer - > disable ( " Too many hashes " ) ;
m_hashes . clear ( ) ;
m_syncingLatestHash = h256 ( ) ;
continueSync ( ) ; ///Try with some other peer, keep the chain
m_man . appendToChain ( neededBlocks ) ; // Append to download manager immediatelly
clog ( NetMessageSummary ) < < knowns < < " knowns, " < < unknowns < < " unknowns " ;
}
else
continueSync ( _peer ) ; /// Grab next hashes
}
void EthereumHost : : onPeerHashes ( EthereumPeer * _peer , unsigned /*_index*/ , h256s const & _hashes )
{
Guard l ( x_sync ) ;
assert ( _peer - > m_asking = = Asking : : Nothing ) ;
if ( _hashes . empty ( ) )
{
onPeerDoneHashes ( _peer , true ) ;
return ;
}
unsigned knowns = 0 ;
unsigned unknowns = 0 ;
h256s neededBlocks ;
for ( unsigned i = 0 ; i < _hashes . size ( ) ; + + i )
{
_peer - > addRating ( 1 ) ;
auto h = _hashes [ i ] ;
auto status = m_bq . blockStatus ( h ) ;
if ( status = = QueueStatus : : Importing | | status = = QueueStatus : : Ready | | m_chain . isKnown ( h ) )
{
clog ( NetWarn ) < < " block hash already known: " < < h ;
}
else if ( status = = QueueStatus : : Bad )
{
clog ( NetWarn ) < < " block hash bad! " < < h < < " . Bailing... " ;
_peer - > setIdle ( ) ;
return ;
m_hashes + = neededBlocks ; // Append to local list
clog ( NetMessageSummary ) < < knowns < < " knowns, " < < unknowns < < " unknowns; now at " < < m_syncingLatestHash ;
}
else if ( status = = QueueStatus : : Unknown )
if ( _complete )
{
unknowns + + ;
neededBlocks . push_back ( h ) ;
}
else
knowns + + ;
m_needSyncBlocks = true ;
continueSync ( _peer ) ;
}
m_man . appendToChain ( neededBlocks ) ;
clog ( NetMessageSummary ) < < knowns < < " knowns, " < < unknowns < < " unknowns " ;
if ( m_hashMan . isComplete ( ) )
else if ( syncByNumber & & m_hashMan . isComplete ( ) )
{
// Done our chain-get.
m_needSyncHashes = false ;
@ -376,8 +343,15 @@ void EthereumHost::onPeerHashes(EthereumPeer* _peer, unsigned /*_index*/, h256s
m_hashMan . reset ( m_chain . number ( ) + 1 ) ;
continueSync ( ) ;
}
else if ( m_hashes . size ( ) > _peer - > m_expectedHashes )
{
_peer - > disable ( " Too many hashes " ) ;
m_hashes . clear ( ) ;
m_syncingLatestHash = h256 ( ) ;
continueSync ( ) ; ///Try with some other peer, keep the chain
}
else
continueSync ( _peer ) ;
continueSync ( _peer ) ; /// Grab next hashes
}
void EthereumHost : : onPeerDoneHashes ( EthereumPeer * _peer , bool _localChain )
@ -470,8 +444,7 @@ void EthereumHost::onPeerBlocks(EthereumPeer* _peer, RLP const& _r)
void EthereumHost : : onPeerNewHashes ( EthereumPeer * _peer , h256s const & _hashes )
{
Guard l ( x_sync ) ;
if ( _peer - > m_asking ! = Asking : : Nothing )
if ( isSyncing ( ) )
{
clog ( NetMessageSummary ) < < " Ignoring new hashes since we're already downloading. " ;
return ;
@ -483,7 +456,7 @@ void EthereumHost::onPeerNewHashes(EthereumPeer* _peer, h256s const& _hashes)
void EthereumHost : : onPeerNewBlock ( EthereumPeer * _peer , RLP const & _r )
{
Guard l ( x_sync ) ;
if ( _peer - > m_asking ! = Asking : : Nothing )
if ( isSyncing ( ) )
{
clog ( NetMessageSummary ) < < " Ignoring new blocks since we're already downloading. " ;
return ;
@ -525,7 +498,7 @@ void EthereumHost::onPeerNewBlock(EthereumPeer* _peer, RLP const& _r)
_peer - > m_totalDifficulty = difficulty ;
m_needSyncHashes = true ;
m_needSyncBlocks = true ;
m_syncingLatestHash = _peer - > m_latestHas h;
m_syncingLatestHash = h ;
sync = true ;
}
}
@ -648,7 +621,6 @@ bool EthereumHost::peerShouldGrabChain(EthereumPeer* _peer) const
bool EthereumHost : : isSyncing ( ) const
{
Guard l ( x_sync ) ;
bool syncing = false ;
forEachPeer ( [ & ] ( EthereumPeer * _p )
{