@ -257,15 +257,19 @@ public class MergeCoordinator implements MergeMiningCoordinator {
final Hash headBlockHash , final Hash finalizedBlockHash ) {
MutableBlockchain blockchain = protocolContext . getBlockchain ( ) ;
Optional < BlockHeader > currentFinalized = mergeContext . getFinalized ( ) ;
final Optional < BlockHeader > newFinalized = blockchain . getBlockHeader ( finalizedBlockHash ) ;
BlockHeader newHead = blockchain . getBlockHeader ( headBlockHash ) . orElse ( null ) ;
if ( newHead = = null ) {
return ForkchoiceResult . withFailure (
String . format ( "not able to find new head block %s" , headBlockHash ) , Optional . empty ( ) ) ;
}
final Optional < Hash > latestValid = getLatestValidAncestor ( newHead ) ;
if ( newFinalized . isEmpty ( ) & & ! finalizedBlockHash . equals ( Hash . ZERO ) ) {
// we should only fail to find when it's the special value 0x000..000
return ForkchoiceResult . withFailure (
String . format (
"should've been able to find block hash %s but couldn't" , finalizedBlockHash ) ,
null ) ;
latestValid ) ;
}
if ( currentFinalized . isPresent ( )
@ -275,15 +279,10 @@ public class MergeCoordinator implements MergeMiningCoordinator {
String . format (
"new finalized block %s is not a descendant of current finalized block %s" ,
finalizedBlockHash , currentFinalized . get ( ) . getBlockHash ( ) ) ,
null ) ;
latestValid ) ;
}
// ensure we have headBlock:
BlockHeader newHead = blockchain . getBlockHeader ( headBlockHash ) . orElse ( null ) ;
if ( newHead = = null ) {
return ForkchoiceResult . withFailure (
String . format ( "not able to find new head block %s" , headBlockHash ) , null ) ;
}
// ensure new head is descendant of finalized
Optional < String > descendantError =
@ -298,16 +297,14 @@ public class MergeCoordinator implements MergeMiningCoordinator {
newHead . getBlockHash ( ) , finalized . getBlockHash ( ) ) ) ;
if ( descendantError . isPresent ( ) ) {
return ForkchoiceResult . withFailure ( descendantError . get ( ) , null ) ;
return ForkchoiceResult . withFailure ( descendantError . get ( ) , latestValid ) ;
}
Optional < BlockHeader > parentOfNewHead = blockchain . getBlockHeader ( newHead . getParentHash ( ) ) ;
if ( parentOfNewHead . isPresent ( )
& & parentOfNewHead . get ( ) . getTimestamp ( ) > = newHead . getTimestamp ( ) ) {
final Optional < Hash > latestValid = getLatestValidAncestor ( newHead ) ;
return ForkchoiceResult . withFailure (
"new head timestamp not greater than parent" ,
latestValid . isPresent ( ) ? latestValid . get ( ) : null ) ;
"new head timestamp not greater than parent" , latestValid ) ;
}
// set the new head
blockchain . rewindToBlock ( newHead . getHash ( ) ) ;