async fn archive_blocks() {
    let ledger_guard = LEDGER.try_read().expect("Failed to get ledger read lock");
    let archive_arc = ledger_guard.blockchain.archive.clone();
    let mut archive_guard = match archive_arc.try_write() {
        Ok(g) => g,
        Err(_) => {
            print("Ledger is currently archiving. Skipping archive_blocks()");
            return;
        }
    };
    if archive_guard.is_none() {
        return; // Archiving not enabled
    }
    let archive = archive_guard.as_mut().unwrap();

    let blocks_to_archive = ledger_guard
        .get_blocks_for_archiving(archive.trigger_threshold, archive.num_blocks_to_archive);
    if blocks_to_archive.is_empty() {
        return;
    }

    drop(ledger_guard); // Drop the lock on the ledger

    let num_blocks = blocks_to_archive.len();
    print(format!("[ledger] archiving {} blocks", num_blocks,));

    let max_msg_size = *MAX_MESSAGE_SIZE_BYTES.read().unwrap();
    let res = archive
        .send_blocks_to_archive(blocks_to_archive, max_msg_size)
        .await;

    let mut ledger = LEDGER.try_write().expect("Failed to get ledger write lock");
    match res {
        Ok(num_sent_blocks) => ledger.remove_archived_blocks(num_sent_blocks),
        Err((num_sent_blocks, FailedToArchiveBlocks(err))) => {
            ledger.remove_archived_blocks(num_sent_blocks);
            print(format!(
                "[ledger] Archiving failed. Archived {} out of {} blocks. Error {}",
                num_sent_blocks, num_blocks, err
            ));
        }
    }
}
/// Upon reaching a `trigger_threshold` we will archive `num_blocks`.
/// This really should be an action on the ledger canister, but since we don't
/// want to hold a mutable lock on the whole ledger while we're archiving, we
/// split this method up into the parts that require async (this function) and
/// the parts that require a lock (Ledger::get_blocks_for_archiving).
Upon reaching a `trigger_threshold` we will archive `num_blocks`.
This really should be an action on the ledger canister, but since we don't
want to hold a mutable lock on the whole ledger while we're archiving, we
split this method up into the parts that require async (this function) and
the parts that require a lock (Ledger::get_blocks_for_archiving).
Upon reaching a ` trigger_threshold ` we will archive ` num_blocks ` . This really should be an action on the ledger canister but since we don ' t want to hold a mutable lock on the whole ledger while we ' re archiving we split this method up into the parts that require async ( this function ) and the parts that require a lock ( Ledger :: get_blocks_for_archiving ) .
async fn archive_blocks() {
    let ledger_guard = LEDGER.try_read().expect("Failed to get ledger read lock");
    let archive_arc = ledger_guard.blockchain.archive.clone();
    let mut archive_guard = match archive_arc.try_write() {
        Ok(g) => g,
        Err(_) => {
            print("Ledger is currently archiving. Skipping archive_blocks()");
            return;
        }
    };
    if archive_guard.is_none() {
        return; // Archiving not enabled
    }
    let archive = archive_guard.as_mut().unwrap();
    let blocks_to_archive = ledger_guard
        .get_blocks_for_archiving(archive.trigger_threshold, archive.num_blocks_to_archive);
    if blocks_to_archive.is_empty() {
        return;
    }
    drop(ledger_guard); // Drop the lock on the ledger
    let num_blocks = blocks_to_archive.len();
    print(format!("[ledger] archiving {} blocks", num_blocks,));
    let max_msg_size = *MAX_MESSAGE_SIZE_BYTES.read().unwrap();
    let res = archive
        .send_blocks_to_archive(blocks_to_archive, max_msg_size)
        .await;
    let mut ledger = LEDGER.try_write().expect("Failed to get ledger write lock");
    match res {
        Ok(num_sent_blocks) => ledger.remove_archived_blocks(num_sent_blocks),
        Err((num_sent_blocks, FailedToArchiveBlocks(err))) => {
            ledger.remove_archived_blocks(num_sent_blocks);
            print(format!(
                "[ledger] Archiving failed. Archived {} out of {} blocks. Error {}",
                num_sent_blocks, num_blocks, err
            ));
        }
    }
}
async fn archive_blocks ( ) { let ledger_guard = LEDGER . try_read ( ) . expect ( " " ) ; let archive_arc = ledger_guard . blockchain . archive . clone ( ) ; let mut archive_guard = match archive_arc . try_write ( ) { Ok ( g ) => g , Err ( _ ) => { print ( " " ) ; return ; } } ; if archive_guard . is_none ( ) { return ; // Archiving not enabled } let archive = archive_guard . as_mut ( ) . unwrap ( ) ; let blocks_to_archive = ledger_guard . get_blocks_for_archiving ( archive . trigger_threshold , archive . num_blocks_to_archive ) ; if blocks_to_archive . is_empty ( ) { return ; } drop ( ledger_guard ) ; // Drop the lock on the ledger let num_blocks = blocks_to_archive . len ( ) ; print ( format ! ( " " num_blocks ) ) ; let max_msg_size = * MAX_MESSAGE_SIZE_BYTES . read ( ) . unwrap ( ) ; let res = archive . send_blocks_to_archive ( blocks_to_archive , max_msg_size ) . await ; let mut ledger = LEDGER . try_write ( ) . expect ( " " ) ; match res { Ok ( num_sent_blocks ) => ledger . remove_archived_blocks ( num_sent_blocks ) , Err ( ( num_sent_blocks , FailedToArchiveBlocks ( err ) ) ) => { ledger . remove_archived_blocks ( num_sent_blocks ) ; print ( format ! ( " " num_sent_blocks num_blocks err ) ) ; } } }
Upon reaching a `trigger_threshold` we will archive `num_blocks`.
Upon reaching a ` trigger_threshold ` we will archive ` num_blocks ` .