Is there any way to manipulate the block timestamps?

Is there any way to manipulate the block timestamps in my test?

1 Like

Yes, there is. You can find more here: Tweaking the blockchain - The Fuel Rust SDK.

But here’s an example of how to do it:

    let config = Config {
        manual_blocks_enabled: true, // Necessary so the `produce_blocks` API can be used locally
        ..Config::local_node()
    };
    let wallets =
        launch_custom_provider_and_get_wallets(WalletsConfig::default(), Some(config), None).await;
    let wallet = &wallets[0];
    let provider = wallet.get_provider()?;

    assert_eq!(provider.latest_block_height().await?, 0);

    let time = TimeParameters {
        start_time: Utc.timestamp_opt(100, 0).unwrap(),
        block_time_interval: Duration::seconds(10),
    };
    provider.produce_blocks(3, Some(time)).await?;

    assert_eq!(provider.latest_block_height().await?, 3);

    let req = PaginationRequest {
        cursor: None,
        results: 10,
        direction: PageDirection::Forward,
    };
    let blocks: Vec<Block> = provider.get_blocks(req).await?.results;

    assert_eq!(blocks[2].header().time().unwrap().timestamp(), 100);
    assert_eq!(blocks[1].header().time().unwrap().timestamp(), 110);
    assert_eq!(blocks[0].header().time().unwrap().timestamp(), 120);
1 Like

@digorithm the code doesn’t work:
Multiple errors:

use of undeclared crate or module chrono

no function or associated item named secondsfound for structstd::time::Duration in the current scope

What are you importing for TimeParameters, Utc and Duration

Also this line:
let blocks: Vec<Block> = provider.get_blocks(req).await?.results; doesn’t work either.

Says no field "results" on type Result<PaginatedResult<fuels::types::block::Block, std::string::String>, fuels::signers::provider::ProviderError>

Hi @buddy-brontosaurus,

You can see the full context and most current version for this code snippet here in the can_set_custom_block_time test function: fuels-rs/providers.rs at master · FuelLabs/fuels-rs · GitHub

You can also copy and paste this (using version 0.34 of fuels):

use fuels::{
    prelude::*, 
    client::{PageDirection, PaginationRequest},
    types::{
        block::Block
    },
};
use chrono::Duration;
use chrono::{TimeZone, Utc};

#[tokio::test]
async fn test_functiooonn() {
    let config = Config {
        manual_blocks_enabled: true, // Necessary so the `produce_blocks` API can be used locally
        ..Config::local_node()
    };
    let wallets =
        launch_custom_provider_and_get_wallets(WalletsConfig::default(), Some(config), None).await;
    let wallet = &wallets[0];
    let provider = wallet.get_provider().unwrap();

    assert_eq!(provider.latest_block_height().await.unwrap(), 0);

    let time = TimeParameters {
        start_time: Utc.timestamp_opt(100, 0).unwrap(),
        block_time_interval: Duration::seconds(10),
    };
    provider.produce_blocks(3, Some(time)).await.unwrap();

    assert_eq!(provider.latest_block_height().await.unwrap(), 3);

    let req = PaginationRequest {
        cursor: None,
        results: 10,
        direction: PageDirection::Forward,
    };
    let blocks: Vec<Block> = provider.get_blocks(req).await.unwrap().results;

    assert_eq!(blocks[1].header.time.unwrap().timestamp(), 100);
    assert_eq!(blocks[2].header.time.unwrap().timestamp(), 110);
    assert_eq!(blocks[3].header.time.unwrap().timestamp(), 120);

}

and add chrono to your Cargo.toml file under [dev-dependencies]:

chrono = "0.4.2"

Ah thanks → I am now getting
panicked at 'called `Result::unwrap()` on an `Err` value: Custom { kind: Other, error: "Response errors; The start time must be set after the latest block time: 4611686020102744001" }', tests/harness.rs:84:80

This is weird because before I execute the code that produced blocks, I do
chain_info().await.unwrap().latest_block; and that returns block number 1 and block time is None.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.