@ -4,7 +4,7 @@ use std::time::{Duration, Instant};
use std ::vec ;
use std ::vec ;
use hyperlane_core ::rpc_clients ::call_and_retry_indefinitely ;
use hyperlane_core ::rpc_clients ::call_and_retry_indefinitely ;
use hyperlane_core ::{ ChainCommunicationError , Chain Result , MerkleTreeHook } ;
use hyperlane_core ::{ ChainResult , MerkleTreeHook } ;
use prometheus ::IntGauge ;
use prometheus ::IntGauge ;
use tokio ::time ::sleep ;
use tokio ::time ::sleep ;
use tracing ::{ debug , error , info } ;
use tracing ::{ debug , error , info } ;
@ -61,16 +61,7 @@ impl ValidatorSubmitter {
/// Runs idly forever once the target checkpoint is reached to avoid exiting the task.
/// Runs idly forever once the target checkpoint is reached to avoid exiting the task.
pub ( crate ) async fn backfill_checkpoint_submitter ( self , target_checkpoint : Checkpoint ) {
pub ( crate ) async fn backfill_checkpoint_submitter ( self , target_checkpoint : Checkpoint ) {
let mut tree = IncrementalMerkle ::default ( ) ;
let mut tree = IncrementalMerkle ::default ( ) ;
call_and_retry_indefinitely ( | | {
self . submit_checkpoints_until_correctness_checkpoint ( & mut tree , & target_checkpoint )
let target_checkpoint = target_checkpoint ;
let self_clone = self . clone ( ) ;
Box ::pin ( async move {
self_clone
. submit_checkpoints_until_correctness_checkpoint ( & mut tree , & target_checkpoint )
. await ? ;
Ok ( ( ) )
} )
} )
. await ;
. await ;
info ! (
info ! (
@ -132,20 +123,7 @@ impl ValidatorSubmitter {
sleep ( self . interval ) . await ;
sleep ( self . interval ) . await ;
continue ;
continue ;
}
}
self . submit_checkpoints_until_correctness_checkpoint ( & mut tree , & latest_checkpoint )
tree = call_and_retry_indefinitely ( | | {
let mut tree = tree ;
let self_clone = self . clone ( ) ;
Box ::pin ( async move {
self_clone
. submit_checkpoints_until_correctness_checkpoint (
& mut tree ,
& latest_checkpoint ,
)
. await ? ;
Ok ( tree )
} )
} )
. await ;
. await ;
self . metrics
self . metrics
@ -162,7 +140,7 @@ impl ValidatorSubmitter {
& self ,
& self ,
tree : & mut IncrementalMerkle ,
tree : & mut IncrementalMerkle ,
correctness_checkpoint : & Checkpoint ,
correctness_checkpoint : & Checkpoint ,
) -> ChainResult < ( ) > {
) {
// This should never be called with a tree that is ahead of the correctness checkpoint.
// This should never be called with a tree that is ahead of the correctness checkpoint.
assert! (
assert! (
! tree_exceeds_checkpoint ( correctness_checkpoint , tree ) ,
! tree_exceeds_checkpoint ( correctness_checkpoint , tree ) ,
@ -182,7 +160,14 @@ impl ValidatorSubmitter {
while tree . count ( ) as u32 < = correctness_checkpoint . index {
while tree . count ( ) as u32 < = correctness_checkpoint . index {
if let Some ( insertion ) = self
if let Some ( insertion ) = self
. message_db
. message_db
. retrieve_merkle_tree_insertion_by_leaf_index ( & ( tree . count ( ) as u32 ) ) ?
. retrieve_merkle_tree_insertion_by_leaf_index ( & ( tree . count ( ) as u32 ) )
. unwrap_or_else ( | err | {
panic! (
"Error fetching merkle tree insertion for leaf index {}: {}" ,
tree . count ( ) ,
err
)
} )
{
{
debug ! (
debug ! (
index = insertion . index ( ) ,
index = insertion . index ( ) ,
@ -225,9 +210,7 @@ impl ValidatorSubmitter {
? correctness_checkpoint ,
? correctness_checkpoint ,
"Incorrect tree root, something went wrong"
"Incorrect tree root, something went wrong"
) ;
) ;
return Err ( ChainCommunicationError ::CustomError (
panic! ( "Incorrect tree root, something went wrong" ) ;
"Incorrect tree root, something went wrong" . to_string ( ) ,
) ) ;
}
}
if ! checkpoint_queue . is_empty ( ) {
if ! checkpoint_queue . is_empty ( ) {
@ -236,56 +219,70 @@ impl ValidatorSubmitter {
queue_len = checkpoint_queue . len ( ) ,
queue_len = checkpoint_queue . len ( ) ,
"Reached tree consistency"
"Reached tree consistency"
) ;
) ;
self . sign_and_submit_checkpoints ( checkpoint_queue ) . await ;
self . sign_and_submit_checkpoints ( checkpoint_queue ) . await ? ;
info ! (
info ! (
index = checkpoint . index ,
index = checkpoint . index ,
"Signed all queued checkpoints until index"
"Signed all queued checkpoints until index"
) ;
) ;
}
}
Ok ( ( ) )
}
}
/// Signs and submits any previously unsubmitted checkpoints.
async fn sign_and_submit_checkpoint (
async fn sign_and_submit_checkpoints (
& self ,
& self ,
checkpoints : Vec < CheckpointWithMessageId > ,
checkpoint : CheckpointWithMessageId ,
) -> ChainResult < ( ) > {
) -> ChainResult < ( ) > {
let last_checkpoint = checkpoints . as_slice ( ) [ checkpoints . len ( ) - 1 ] ;
for queued_checkpoint in checkpoints {
let existing = self
let existing = self
. checkpoint_syncer
. checkpoint_syncer
. fetch_checkpoint ( queued_ checkpoint. index )
. fetch_checkpoint ( checkpoint . index )
. await ? ;
. await ? ;
if existing . is_some ( ) {
if existing . is_some ( ) {
debug ! (
debug ! ( index = checkpoint . index , "Checkpoint already submitted" ) ;
index = queued_checkpoint . index ,
return Ok ( ( ) ) ;
"Checkpoint already submitted"
) ;
continue ;
}
}
let signed_checkpoint = self . signer . sign ( queued_ checkpoint) . await ? ;
let signed_checkpoint = self . signer . sign ( checkpoint ) . await ? ;
self . checkpoint_syncer
self . checkpoint_syncer
. write_checkpoint ( & signed_checkpoint )
. write_checkpoint ( & signed_checkpoint )
. await ? ;
. await ? ;
debug ! (
debug ! ( index = checkpoint . index , "Signed and submitted checkpoint" ) ;
index = queued_checkpoint . index ,
"Signed and submitted checkpoint"
) ;
// TODO: move these into S3 implementations
// TODO: move these into S3 implementations
// small sleep before signing next checkpoint to avoid rate limiting
// small sleep before signing next checkpoint to avoid rate limiting
sleep ( Duration ::from_millis ( 100 ) ) . await ;
sleep ( Duration ::from_millis ( 100 ) ) . await ;
Ok ( ( ) )
}
}
self . checkpoint_syncer
/// Signs and submits any previously unsubmitted checkpoints.
. update_latest_index ( last_checkpoint . index )
async fn sign_and_submit_checkpoints ( & self , checkpoints : Vec < CheckpointWithMessageId > ) {
let last_checkpoint = checkpoints . as_slice ( ) [ checkpoints . len ( ) - 1 ] ;
// Submits checkpoints to the store in reverse order. This speeds up processing historic checkpoints (those before the validator is spun up),
// since those are the most likely to make messages become processable.
// A side effect is that new checkpoints will also be submitted in reverse order.
for queued_checkpoint in checkpoints . into_iter ( ) . rev ( ) {
// certain checkpoint stores rate limit very aggressively, so we retry indefinitely
call_and_retry_indefinitely ( | | {
let self_clone = self . clone ( ) ;
Box ::pin ( async move {
self_clone
. sign_and_submit_checkpoint ( queued_checkpoint )
. await ? ;
. await ? ;
Ok ( ( ) )
} )
} )
. await ;
}
call_and_retry_indefinitely ( | | {
let self_clone = self . clone ( ) ;
Box ::pin ( async move {
self_clone
. checkpoint_syncer
. update_latest_index ( last_checkpoint . index )
. await ? ;
Ok ( ( ) )
Ok ( ( ) )
} )
} )
. await ;
}
}
}
}