async fn archive_blocks() {
    let (mut blocks_to_archive, archive) = {
        let mut state = LEDGER
            .try_write()
            .expect("Failed to get a lock on the ledger");

        match state.archive_blocks() {
            Some((bta, lock)) => (bta, lock),
            None => return,
        }
    };
    // ^ Drop the write lock on the ledger

    while !blocks_to_archive.is_empty() {
        let chunk = get_chain_prefix(
            &mut blocks_to_archive,
            *MAX_MESSAGE_SIZE_BYTES.read().unwrap(),
        );
        assert!(!chunk.is_empty());

        print(format!(
            "[ledger] archiving a chunk of blocks of size {}",
            chunk.len(),
        ));

        let chunk = VecDeque::from(chunk);

        if let Err(FailedToArchiveBlocks(err)) = archive
            .try_write()
            .expect("Failed to get write lock on archive")
            .as_mut()
            .expect("Archiving is not enabled")
            .archive_blocks(chunk.clone())
            .await
        {
            print(format!(
                "[ledger] Failed to archive {} blocks with error {}",
                chunk.len(),
                err
            ));
            // We're in real trouble if we can't acquire this lock
            let blockchain = &mut LEDGER
                .try_write()
                .expect("Failed to get a lock on the ledger")
                .blockchain;

            // Revert the change to the index of blocks not on this canister that was made
            // in archive_blocks
            blockchain.sub_num_archived_blocks(chunk.len() as u64);
            // Add the blocks back to the local blockchain
            recover_from_failed_archive(&mut blockchain.blocks, blocks_to_archive, chunk);
            return;
        }
    }
}
/// 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::archive_blocks).
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::archive_blocks).
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 :: archive_blocks ) .
async fn archive_blocks() {
    let (mut blocks_to_archive, archive) = {
        let mut state = LEDGER
            .try_write()
            .expect("Failed to get a lock on the ledger");
        match state.archive_blocks() {
            Some((bta, lock)) => (bta, lock),
            None => return,
        }
    };
    // ^ Drop the write lock on the ledger
    while !blocks_to_archive.is_empty() {
        let chunk = get_chain_prefix(
            &mut blocks_to_archive,
            *MAX_MESSAGE_SIZE_BYTES.read().unwrap(),
        );
        assert!(!chunk.is_empty());
        print(format!(
            "[ledger] archiving a chunk of blocks of size {}",
            chunk.len(),
        ));
        let chunk = VecDeque::from(chunk);
        if let Err(FailedToArchiveBlocks(err)) = archive
            .try_write()
            .expect("Failed to get write lock on archive")
            .as_mut()
            .expect("Archiving is not enabled")
            .archive_blocks(chunk.clone())
            .await
        {
            print(format!(
                "[ledger] Failed to archive {} blocks with error {}",
                chunk.len(),
                err
            ));
            // We're in real trouble if we can't acquire this lock
            let blockchain = &mut LEDGER
                .try_write()
                .expect("Failed to get a lock on the ledger")
                .blockchain;
            // Revert the change to the index of blocks not on this canister that was made
            // in archive_blocks
            blockchain.sub_num_archived_blocks(chunk.len() as u64);
            // Add the blocks back to the local blockchain
            recover_from_failed_archive(&mut blockchain.blocks, blocks_to_archive, chunk);
            return;
        }
    }
}
async fn archive_blocks ( ) { let ( mut blocks_to_archive , archive ) = { let mut state = LEDGER . try_write ( ) . expect ( " " ) ; match state . archive_blocks ( ) { Some ( ( bta , lock ) ) => ( bta , lock ) , None => return , } } ; // ^ Drop the write lock on the ledger while ! blocks_to_archive . is_empty ( ) { let chunk = get_chain_prefix ( & mut blocks_to_archive , * MAX_MESSAGE_SIZE_BYTES . read ( ) . unwrap ( ) , ) ; assert ! ( chunk is_empty ( ) ) ; print ( format ! ( " " chunk len ( ) ) ) ; let chunk = VecDeque :: from ( chunk ) ; if let Err ( FailedToArchiveBlocks ( err ) ) = archive . try_write ( ) . expect ( " " ) . as_mut ( ) . expect ( " " ) . archive_blocks ( chunk . clone ( ) ) . await { print ( format ! ( " " chunk len ( ) err ) ) ; // We're in real trouble if we can't acquire this lock let blockchain = & mut LEDGER . try_write ( ) . expect ( " " ) . blockchain ; // Revert the change to the index of blocks not on this canister that was made // in archive_blocks blockchain . sub_num_archived_blocks ( chunk . len ( ) as u64 ) ; // Add the blocks back to the local blockchain recover_from_failed_archive ( & mut blockchain . blocks , blocks_to_archive , chunk ) ; return ; } } }
Upon reaching a `trigger_threshold` we will archive `num_blocks`.
Upon reaching a ` trigger_threshold ` we will archive ` num_blocks ` .