You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: finalize.rs
    path of file: ./repos/snarkVM/synthesizer/src/vm
    
    the code of the file until where you have to start completion: // Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

use ledger_committee::{MAX_DELEGATORS, MIN_DELEGATOR_STAKE, MIN_VALIDATOR_STAKE};

impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
    /// Speculates on the given list of transactions in the VM.
    ///
    /// Returns the confirmed transactions, aborted transaction IDs,
    /// and finalize operations from pre-ratify and post-ratify.
    ///
    /// Note: This method is used to create a new block (including the genesis block).
    ///   - If `coinbase_reward = None`, then the `ratifications` will not be modified.
    ///   - If `coinbase_reward = Some(coinbase_reward)`, then the method will append a
    ///     `Ratify::BlockReward(block_reward)` and `Ratify::PuzzleReward(puzzle_reward)`
    ///     to the front of the `ratifications` list.
    #[inline]
    pub fn speculate<'a>(
        &self,
        state: FinalizeGlobalState,
        coinbase_reward: Option<u64>,
        candidate_ratifications: Vec<Ratify<N>>,
        candidate_solutions: &Solutions<N>,
        candidate_transactions: impl ExactSizeIterator<Item = &'a Transaction<N>>,
    ) -> Result<(Ratifications<N>, Transactions<N>, Vec<N::TransactionID>, Vec<FinalizeOperation<N>>)> {
        let timer = timer!("VM::speculate");

        // Performs a **dry-run** over the list of ratifications, solutions, and transactions.
        let (ratifications, confirmed_transactions, aborted_transactions, ratified_finalize_operations) = self
            .atomic_speculate(
                state,
                coinbase_reward,
                candidate_ratifications,
                candidate_solutions,
                candidate_transactions,
            )?;

        // Convert the aborted transactions into aborted transaction IDs.
        let mut aborted_transaction_ids = Vec::with_capacity(aborted_transactions.len());
        for (tx, error) in aborted_transactions {
            warn!("Speculation safely aborted a transaction - {error} ({})", tx.id());
            aborted_transaction_ids.push(tx.id());
        }

        finish!(timer, "Finished dry-run of the transactions");

        // Return the ratifications, confirmed transactions, aborted transaction IDs, and ratified finalize operations.
        Ok((
            ratifications,
            confirmed_transactions.into_iter().collect(),
            aborted_transaction_ids,
            ratified_finalize_operations,
        ))
    }

    /// Checks the speculation on the given transactions in the VM.
    ///
    /// Returns the finalize operations from pre-ratify and post-ratify.
    #[inline]
    pub fn check_speculate(
        &self,
        state: FinalizeGlobalState,
        ratifications: &Ratifications<N>,
        solutions: &Solutions<N>,
        transactions: &Transactions<N>,
    ) -> Result<Vec<FinalizeOperation<N>>> {
        let timer = timer!("VM::check_speculate");

        // Reconstruct the candidate ratifications to verify the speculation.
        let candidate_ratifications = ratifications.iter().cloned().collect::<Vec<_>>();
        // Reconstruct the unconfirmed transactions to verify the speculation.
        let candidate_transactions =
            transactions.iter().map(|confirmed| confirmed.to_unconfirmed_transaction()).collect::<Result<Vec<_>>>()?;

        // Performs a **dry-run** over the list of ratifications, solutions, and transactions.
        let (speculate_ratificatio
    
    fn test_ratify_genesis_with_insufficient_delegator_balance() {
        // Sample an RNG.
        let rng = &mut TestRng::default();

        // Initialize the VM.
        let vm =
            VM::from(ConsensusStore::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::open(None).unwrap()).unwrap();

        // Track the allocated amount.
        let mut allocated_amount = 0;

        // Sample the validators.
        let validators = sample_validators(4, rng);

        // Attempt to construct a genesis quorum, with a delegator with an insufficient amount.
        let mut delegators = IndexMap::new();
        delegators.insert(
            PrivateKey::new(rng).unwrap(),
            (Address::try_from(validators.keys().next().unwrap()).unwrap(), MIN_DELEGATOR_STAKE - 1),
        );

        // Construct the committee.
        let mut committee_map = IndexMap::new();
        for (private_key, (amount, _)) in &validators {
            let address = Address::try_from(private_key).unwrap();
            let amount = if address == Address::try_from(validators.keys().next().unwrap()).unwrap() {
                *amount + MIN_DELEGATOR_STAKE - 1
            } else {
                *amount
            };
            committee_map.insert(address, (amount, true));
            allocated_amount += amount;
        }
        let committee = Committee::new_genesis(committee_map).unwrap();

        // Construct the public balances, allocating the remaining supply to rest of the validators.
        let mut public_balances = sample_public_balances(
            &validators.keys().map(|private_key| Address::try_from(private_key).unwrap()).collect::<Vec<_>>(),
            <CurrentNetwork as Network>::STARTING_SUPPLY - allocated_amount,
        );
        public_balances
            .extend(sample_public_balances(&[Address::try_from(delegators.keys().next().unwrap()).unwrap()], 0));

        // Construct the bonded balances.
        let bonded_balances = sample_bonded_balances(&validators, &delegators);

        // Construct the genesis block, which should fail.
        let result =
            vm.genesis_quorum(validators.keys().next().unwrap(), committee, public_balances, bonded_balances, rng);
        assert!(result.is_err());

        // Reset the delegators.
        let mut delegators = IndexMap::new();
        delegators.insert(
            PrivateKey::new(rng).unwrap(),
            (Address::try_from(validators.keys().next().unwrap()).unwrap(), MIN_DELEGATOR_STAKE),
        );

        // Track the allocated amount.
        let mut allocated_amount = 0;

        // Construct the committee.
        let mut committee_map = IndexMap::new();
        for (private_key, (amount, _)) in &validators {
            let address = Address::try_from(private_key).unwrap();
            let amount = if address == Address::try_from(validators.keys().next().unwrap()).unwrap() {
                *amount + MIN_DELEGATOR_STAKE
            } else {
                *amount
            };
            committee_map.insert(address, (amount, true));
            allocated_amount += amount;
        }
        let committee = Committee::new_genesis(committee_map).unwrap();

        // Construct the public balances, allocating the remaining supply to rest of the validators.
        let mut public_balances = sample_public_balances(
            &validators.keys().map(|private_key| Address::try_from(private_key).unwrap()).collect::<Vec<_>>(),
            <CurrentNetwork as Network>::STARTING_SUPPLY - allocated_amount,
        );
        public_balances
            .extend(sample_public_balances(&[Address::try_from(delegators.keys().next().unwrap()).unwrap()], 0));

        // Construct the bonded balances.
        let bonded_balances = sample_bonded_balances(&validators, &delegators);

        // Construct the genesis block, which should pass.
        let block = vm
            .genesis_quorum(validators.keys().next().unwrap(), committee, public_balances, bonded_balances, rng)
            .unwrap();

        // Add the block.
        vm.add_next_block(&block).unwrap();
    }