async fn validate_block(
    rng: &mut TestRng,
    timestamp: Timestamp,
    deploys: Vec<Deploy>,
    transfers: Vec<Deploy>,
) -> bool {
    // Assemble the block to be validated.
    let deploy_hashes = deploys.iter().map(|deploy| *deploy.id()).collect_vec();
    let transfer_hashes = transfers.iter().map(|deploy| *deploy.id()).collect_vec();
    let proposed_block = new_proposed_block(timestamp, deploy_hashes, transfer_hashes);

    // Create the reactor and component.
    let reactor = MockReactor::new();
    let effect_builder = EffectBuilder::new(EventQueueHandle::without_shutdown(reactor.scheduler));
    let chainspec = Arc::new(Chainspec::from_resources("local"));
    let mut block_validator = BlockValidator::<NodeId>::new(chainspec);

    // Pass the block to the component. This future will eventually resolve to the result, i.e.
    // whether the block is valid or not.
    let validation_result =
        tokio::spawn(effect_builder.validate_block("Bob", proposed_block.clone()));
    let event = reactor.expect_block_validator_event().await;
    let effects = block_validator.handle_event(effect_builder, rng, event);

    // If validity could already be determined, the effect will be the validation response.
    if block_validator.validation_states.is_empty() {
        assert_eq!(1, effects.len());
        for effect in effects {
            tokio::spawn(effect).await.unwrap(); // Response.
        }
        return validation_result.await.unwrap();
    }

    // Otherwise the effects must be requests to fetch the block's deploys.
    let fetch_results: Vec<_> = effects.into_iter().map(tokio::spawn).collect();

    // We make our mock reactor answer with the expected deploys and transfers:
    for deploy in deploys.into_iter().chain(transfers) {
        reactor.expect_fetch_deploy(deploy).await;
    }

    // The resulting `FetchResult`s are passed back into the component. When any deploy turns out
    // to be invalid, or once all of them have been validated, the component will respond.
    let mut effects = Effects::new();
    for fetch_result in fetch_results {
        let events = fetch_result.await.unwrap();
        assert_eq!(1, events.len());
        effects.extend(events.into_iter().flat_map(|found_deploy| {
            block_validator.handle_event(effect_builder, rng, found_deploy)
        }));
    }

    // We expect exactly one effect: the validation response. This will resolve the result.
    assert_eq!(1, effects.len());
    for effect in effects {
        tokio::spawn(effect).await.unwrap(); // Response.
    }
    validation_result.await.unwrap()
}
/// Validates a block using a `BlockValidator` component, and returns the result.
Validates a block using a `BlockValidator` component, and returns the result.
Validates a block using a ` BlockValidator ` component and returns the result .
async fn validate_block(
    rng: &mut TestRng,
    timestamp: Timestamp,
    deploys: Vec<Deploy>,
    transfers: Vec<Deploy>,
) -> bool {
    // Assemble the block to be validated.
    let deploy_hashes = deploys.iter().map(|deploy| *deploy.id()).collect_vec();
    let transfer_hashes = transfers.iter().map(|deploy| *deploy.id()).collect_vec();
    let proposed_block = new_proposed_block(timestamp, deploy_hashes, transfer_hashes);
    // Create the reactor and component.
    let reactor = MockReactor::new();
    let effect_builder = EffectBuilder::new(EventQueueHandle::without_shutdown(reactor.scheduler));
    let chainspec = Arc::new(Chainspec::from_resources("local"));
    let mut block_validator = BlockValidator::<NodeId>::new(chainspec);
    // Pass the block to the component. This future will eventually resolve to the result, i.e.
    // whether the block is valid or not.
    let validation_result =
        tokio::spawn(effect_builder.validate_block("Bob", proposed_block.clone()));
    let event = reactor.expect_block_validator_event().await;
    let effects = block_validator.handle_event(effect_builder, rng, event);
    // If validity could already be determined, the effect will be the validation response.
    if block_validator.validation_states.is_empty() {
        assert_eq!(1, effects.len());
        for effect in effects {
            tokio::spawn(effect).await.unwrap(); // Response.
        }
        return validation_result.await.unwrap();
    }
    // Otherwise the effects must be requests to fetch the block's deploys.
    let fetch_results: Vec<_> = effects.into_iter().map(tokio::spawn).collect();
    // We make our mock reactor answer with the expected deploys and transfers:
    for deploy in deploys.into_iter().chain(transfers) {
        reactor.expect_fetch_deploy(deploy).await;
    }
    // The resulting `FetchResult`s are passed back into the component. When any deploy turns out
    // to be invalid, or once all of them have been validated, the component will respond.
    let mut effects = Effects::new();
    for fetch_result in fetch_results {
        let events = fetch_result.await.unwrap();
        assert_eq!(1, events.len());
        effects.extend(events.into_iter().flat_map(|found_deploy| {
            block_validator.handle_event(effect_builder, rng, found_deploy)
        }));
    }
    // We expect exactly one effect: the validation response. This will resolve the result.
    assert_eq!(1, effects.len());
    for effect in effects {
        tokio::spawn(effect).await.unwrap(); // Response.
    }
    validation_result.await.unwrap()
}
async fn validate_block ( rng : & mut TestRng , timestamp : Timestamp , deploys : Vec < Deploy > , transfers : Vec < Deploy > , ) -> bool { // Assemble the block to be validated. let deploy_hashes = deploys . iter ( ) . map ( | deploy | * deploy . id ( ) ) . collect_vec ( ) ; let transfer_hashes = transfers . iter ( ) . map ( | deploy | * deploy . id ( ) ) . collect_vec ( ) ; let proposed_block = new_proposed_block ( timestamp , deploy_hashes , transfer_hashes ) ; // Create the reactor and component. let reactor = MockReactor :: new ( ) ; let effect_builder = EffectBuilder :: new ( EventQueueHandle :: without_shutdown ( reactor . scheduler ) ) ; let chainspec = Arc :: new ( Chainspec :: from_resources ( " " ) ) ; let mut block_validator = BlockValidator :: < NodeId > :: new ( chainspec ) ; // Pass the block to the component. This future will eventually resolve to the result, i.e. // whether the block is valid or not. let validation_result = tokio :: spawn ( effect_builder . validate_block ( " " , proposed_block . clone ( ) ) ) ; let event = reactor . expect_block_validator_event ( ) . await ; let effects = block_validator . handle_event ( effect_builder , rng , event ) ; // If validity could already be determined, the effect will be the validation response. if block_validator . validation_states . is_empty ( ) { assert_eq ! ( 1 effects len ( ) ) ; for effect in effects { tokio :: spawn ( effect ) . await . unwrap ( ) ; // Response. } return validation_result . await . unwrap ( ) ; } // Otherwise the effects must be requests to fetch the block's deploys. let fetch_results : Vec < _ > = effects . into_iter ( ) . map ( tokio :: spawn ) . collect ( ) ; // We make our mock reactor answer with the expected deploys and transfers: for deploy in deploys . into_iter ( ) . chain ( transfers ) { reactor . expect_fetch_deploy ( deploy ) . await ; } // The resulting `FetchResult`s are passed back into the component. When any deploy turns out // to be invalid, or once all of them have been validated, the component will respond. let mut effects = Effects :: new ( ) ; for fetch_result in fetch_results { let events = fetch_result . await . unwrap ( ) ; assert_eq ! ( 1 events len ( ) ) ; effects . extend ( events . into_iter ( ) . flat_map ( | found_deploy | { block_validator . handle_event ( effect_builder , rng , found_deploy ) } ) ) ; } // We expect exactly one effect: the validation response. This will resolve the result. assert_eq ! ( 1 effects len ( ) ) ; for effect in effects { tokio :: spawn ( effect ) . await . unwrap ( ) ; // Response. } validation_result . await . unwrap ( ) }
Validates a block using a `BlockValidator` component, and returns the result.
Validates a block using a ` BlockValidator ` component and returns the result .
rng
&mut TestRng
timestamp
Timestamp
deploys
Vec<Deploy>
transfers
Vec<Deploy>
rng
&mut TestRng
timestamp
Timestamp
deploys
Vec<Deploy>
transfers
Vec<Deploy>