@@ -18,6 +18,7 @@ use tokio::{
18
18
sync:: { Mutex , MutexGuard } ,
19
19
time:: { sleep, sleep_until, Instant } ,
20
20
} ;
21
+ use types:: ChainSpec ;
21
22
22
23
pub use engine_api:: { http:: HttpJsonRpc , ExecutePayloadResponseStatus } ;
23
24
@@ -47,8 +48,6 @@ impl From<ApiError> for Error {
47
48
48
49
struct Inner {
49
50
engines : Engines < HttpJsonRpc > ,
50
- terminal_total_difficulty : Uint256 ,
51
- terminal_block_hash : Hash256 ,
52
51
fee_recipient : Option < Address > ,
53
52
execution_blocks : Mutex < LruCache < Hash256 , ExecutionBlock > > ,
54
53
executor : TaskExecutor ,
@@ -73,8 +72,6 @@ impl ExecutionLayer {
73
72
/// Instantiate `Self` with `urls.len()` engines, all using the JSON-RPC via HTTP.
74
73
pub fn from_urls (
75
74
urls : Vec < SensitiveUrl > ,
76
- terminal_total_difficulty : Uint256 ,
77
- terminal_block_hash : Hash256 ,
78
75
fee_recipient : Option < Address > ,
79
76
executor : TaskExecutor ,
80
77
log : Logger ,
@@ -98,8 +95,6 @@ impl ExecutionLayer {
98
95
latest_forkchoice_state : <_ >:: default ( ) ,
99
96
log : log. clone ( ) ,
100
97
} ,
101
- terminal_total_difficulty,
102
- terminal_block_hash,
103
98
fee_recipient,
104
99
execution_blocks : Mutex :: new ( LruCache :: new ( EXECUTION_BLOCKS_LRU_CACHE_SIZE ) ) ,
105
100
executor,
@@ -121,14 +116,6 @@ impl ExecutionLayer {
121
116
& self . inner . executor
122
117
}
123
118
124
- fn terminal_total_difficulty ( & self ) -> Uint256 {
125
- self . inner . terminal_total_difficulty
126
- }
127
-
128
- fn terminal_block_hash ( & self ) -> Hash256 {
129
- self . inner . terminal_block_hash
130
- }
131
-
132
119
fn fee_recipient ( & self ) -> Result < Address , Error > {
133
120
self . inner
134
121
. fee_recipient
@@ -455,11 +442,14 @@ impl ExecutionLayer {
455
442
/// `get_terminal_pow_block_hash`
456
443
///
457
444
/// https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/merge/validator.md
458
- pub async fn get_terminal_pow_block_hash ( & self ) -> Result < Option < Hash256 > , Error > {
445
+ pub async fn get_terminal_pow_block_hash (
446
+ & self ,
447
+ spec : & ChainSpec ,
448
+ ) -> Result < Option < Hash256 > , Error > {
459
449
let hash_opt = self
460
450
. engines ( )
461
451
. first_success ( |engine| async move {
462
- if self . terminal_block_hash ( ) != Hash256 :: zero ( ) {
452
+ if spec . terminal_block_hash != Hash256 :: zero ( ) {
463
453
// Note: the specification is written such that if there are multiple blocks in
464
454
// the PoW chain with the terminal block hash, then to select 0'th one.
465
455
//
@@ -468,11 +458,12 @@ impl ExecutionLayer {
468
458
// hash. Such a scenario would be a devestating hash collision with external
469
459
// implications far outweighing those here.
470
460
Ok ( self
471
- . get_pow_block ( engine, self . terminal_block_hash ( ) )
461
+ . get_pow_block ( engine, spec . terminal_block_hash )
472
462
. await ?
473
463
. map ( |block| block. block_hash ) )
474
464
} else {
475
- self . get_pow_block_hash_at_total_difficulty ( engine) . await
465
+ self . get_pow_block_hash_at_total_difficulty ( engine, spec)
466
+ . await
476
467
}
477
468
} )
478
469
. await
@@ -482,8 +473,8 @@ impl ExecutionLayer {
482
473
info ! (
483
474
self . log( ) ,
484
475
"Found terminal block hash" ;
485
- "terminal_block_hash_override" => ?self . terminal_block_hash( ) ,
486
- "terminal_total_difficulty" => ?self . terminal_total_difficulty( ) ,
476
+ "terminal_block_hash_override" => ?spec . terminal_block_hash,
477
+ "terminal_total_difficulty" => ?spec . terminal_total_difficulty,
487
478
"block_hash" => ?hash,
488
479
) ;
489
480
}
@@ -503,6 +494,7 @@ impl ExecutionLayer {
503
494
async fn get_pow_block_hash_at_total_difficulty (
504
495
& self ,
505
496
engine : & Engine < HttpJsonRpc > ,
497
+ spec : & ChainSpec ,
506
498
) -> Result < Option < Hash256 > , ApiError > {
507
499
let mut ttd_exceeding_block = None ;
508
500
let mut block = engine
@@ -518,7 +510,7 @@ impl ExecutionLayer {
518
510
//
519
511
// https://github.com/ethereum/consensus-specs/issues/2636
520
512
loop {
521
- if block. total_difficulty >= self . terminal_total_difficulty ( ) {
513
+ if block. total_difficulty >= spec . terminal_total_difficulty {
522
514
ttd_exceeding_block = Some ( block. block_hash ) ;
523
515
524
516
// Try to prevent infinite loops.
@@ -565,6 +557,7 @@ impl ExecutionLayer {
565
557
pub async fn is_valid_terminal_pow_block_hash (
566
558
& self ,
567
559
block_hash : Hash256 ,
560
+ spec : & ChainSpec ,
568
561
) -> Result < Option < bool > , Error > {
569
562
let broadcast_results = self
570
563
. engines ( )
@@ -574,7 +567,7 @@ impl ExecutionLayer {
574
567
self . get_pow_block ( engine, pow_block. parent_hash ) . await ?
575
568
{
576
569
return Ok ( Some (
577
- self . is_valid_terminal_pow_block ( pow_block, pow_parent) ,
570
+ self . is_valid_terminal_pow_block ( pow_block, pow_parent, spec ) ,
578
571
) ) ;
579
572
}
580
573
}
@@ -618,15 +611,19 @@ impl ExecutionLayer {
618
611
/// This function should remain internal.
619
612
///
620
613
/// External users should use `self.is_valid_terminal_pow_block_hash`.
621
- fn is_valid_terminal_pow_block ( & self , block : ExecutionBlock , parent : ExecutionBlock ) -> bool {
622
- if block. block_hash == self . terminal_block_hash ( ) {
614
+ fn is_valid_terminal_pow_block (
615
+ & self ,
616
+ block : ExecutionBlock ,
617
+ parent : ExecutionBlock ,
618
+ spec : & ChainSpec ,
619
+ ) -> bool {
620
+ if block. block_hash == spec. terminal_block_hash {
623
621
return true ;
624
622
}
625
623
626
- let is_total_difficulty_reached =
627
- block. total_difficulty >= self . terminal_total_difficulty ( ) ;
624
+ let is_total_difficulty_reached = block. total_difficulty >= spec. terminal_total_difficulty ;
628
625
let is_parent_total_difficulty_valid =
629
- parent. total_difficulty < self . terminal_total_difficulty ( ) ;
626
+ parent. total_difficulty < spec . terminal_total_difficulty ;
630
627
is_total_difficulty_reached && is_parent_total_difficulty_valid
631
628
}
632
629
@@ -685,14 +682,14 @@ mod test {
685
682
async fn finds_valid_terminal_block_hash ( ) {
686
683
MockExecutionLayer :: default_params ( )
687
684
. move_to_block_prior_to_terminal_block ( )
688
- . with_terminal_block ( |el, _| async move {
689
- assert_eq ! ( el. get_terminal_pow_block_hash( ) . await . unwrap( ) , None )
685
+ . with_terminal_block ( |spec , el, _| async move {
686
+ assert_eq ! ( el. get_terminal_pow_block_hash( & spec ) . await . unwrap( ) , None )
690
687
} )
691
688
. await
692
689
. move_to_terminal_block ( )
693
- . with_terminal_block ( |el, terminal_block| async move {
690
+ . with_terminal_block ( |spec , el, terminal_block| async move {
694
691
assert_eq ! (
695
- el. get_terminal_pow_block_hash( ) . await . unwrap( ) ,
692
+ el. get_terminal_pow_block_hash( & spec ) . await . unwrap( ) ,
696
693
Some ( terminal_block. unwrap( ) . block_hash)
697
694
)
698
695
} )
@@ -703,9 +700,9 @@ mod test {
703
700
async fn verifies_valid_terminal_block_hash ( ) {
704
701
MockExecutionLayer :: default_params ( )
705
702
. move_to_terminal_block ( )
706
- . with_terminal_block ( |el, terminal_block| async move {
703
+ . with_terminal_block ( |spec , el, terminal_block| async move {
707
704
assert_eq ! (
708
- el. is_valid_terminal_pow_block_hash( terminal_block. unwrap( ) . block_hash)
705
+ el. is_valid_terminal_pow_block_hash( terminal_block. unwrap( ) . block_hash, & spec )
709
706
. await
710
707
. unwrap( ) ,
711
708
Some ( true )
@@ -718,11 +715,11 @@ mod test {
718
715
async fn rejects_invalid_terminal_block_hash ( ) {
719
716
MockExecutionLayer :: default_params ( )
720
717
. move_to_terminal_block ( )
721
- . with_terminal_block ( |el, terminal_block| async move {
718
+ . with_terminal_block ( |spec , el, terminal_block| async move {
722
719
let invalid_terminal_block = terminal_block. unwrap ( ) . parent_hash ;
723
720
724
721
assert_eq ! (
725
- el. is_valid_terminal_pow_block_hash( invalid_terminal_block)
722
+ el. is_valid_terminal_pow_block_hash( invalid_terminal_block, & spec )
726
723
. await
727
724
. unwrap( ) ,
728
725
Some ( false )
@@ -735,11 +732,11 @@ mod test {
735
732
async fn rejects_unknown_terminal_block_hash ( ) {
736
733
MockExecutionLayer :: default_params ( )
737
734
. move_to_terminal_block ( )
738
- . with_terminal_block ( |el, _| async move {
735
+ . with_terminal_block ( |spec , el, _| async move {
739
736
let missing_terminal_block = Hash256 :: repeat_byte ( 42 ) ;
740
737
741
738
assert_eq ! (
742
- el. is_valid_terminal_pow_block_hash( missing_terminal_block)
739
+ el. is_valid_terminal_pow_block_hash( missing_terminal_block, & spec )
743
740
. await
744
741
. unwrap( ) ,
745
742
None
0 commit comments