Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. static bool ProcessBlockFound( const CBlock * const block, const CChainParams & chainparams )
  2. {
  3. - // Found a solution
  4. - LogPrintf( "%s\n", block->ToString() ) ;
  5. - LogPrintf( "generated %s\n", FormatMoney( block->vtx[0]->vout[0].nValue ) ) ;
  6. + assert( block != nullptr ) ;
  7. + CBlockIndex* lastTip = chainActive.Tip() ; /* mapBlockIndex[ chainActive.Tip()->GetBlockSha256Hash() ] */
  8.  
  9. {
  10. LOCK( cs_main );
  11. - if ( block->hashPrevBlock != chainActive.Tip()->GetBlockSha256Hash() )
  12. - return error( "ProcessBlockFound: generated block is stale" ) ;
  13. +
  14. + if ( lastTip != nullptr &&
  15. + block->hashPrevBlock != lastTip->GetBlockSha256Hash() )
  16. + { // generated block isn't above the chain's current tip block
  17. + std::string why = "lost in the chain" ;
  18. +
  19. + // is it above the previous block?
  20. + if ( lastTip->pprev != nullptr &&
  21. + lastTip->pprev->GetBlockSha256Hash() == block->hashPrevBlock )
  22. + {
  23. + CValidationState state ;
  24. + RejectBlock( state, Params(), lastTip ) ;
  25. + if ( state.IsValid() )
  26. + if ( ActivateBestChain( state, Params() ) ) why = "" ;
  27. + else
  28. + why = state.GetRejectReason() ;
  29. + }
  30. +
  31. + if ( ! why.empty() ) {
  32. + if ( lastTip->nStatus & BLOCK_FAILED_MASK ) ResetBlockFailureFlags( lastTip ) ;
  33. + return error( "%s: generated block is stale (%s)", __func__, why ) ;
  34. + }
  35. + }
  36. }
  37.  
  38. + // Found a solution
  39. + LogPrintf( "%s\n", block->ToString() ) ;
  40. + LogPrintf( "reward %s\n", FormatMoney( block->vtx[0]->vout[0].nValue ) ) ;
  41. +
  42. // Say about the new block
  43. GetMainSignals().BlockFound( block->GetSha256Hash() ) ;
  44.  
  45. // Process this block the same as if it were received from another node
  46. - if ( ! ProcessNewBlock( chainparams, std::make_shared< const CBlock >( *block ), true, nullptr ) )
  47. + if ( ! ProcessNewBlock( chainparams, std::make_shared< const CBlock >( *block ), true, nullptr ) ) {
  48. + if ( lastTip->nStatus & BLOCK_FAILED_MASK ) {
  49. + LOCK( cs_main );
  50. + ResetBlockFailureFlags( lastTip ) ;
  51. + }
  52. return error( "ProcessBlockFound: ProcessNewBlock, block not accepted" ) ;
  53. + }
  54. +
  55. + if ( lastTip->nStatus & BLOCK_FAILED_MASK ) {
  56. + LOCK( cs_main );
  57. + ResetBlockFailureFlags( lastTip ) ; // it was just to rewind, not to mark as rejected forever
  58. + }
  59.  
  60. return true ;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement