You can use produce_blocks
to help achieve an arbitrary block height; this is useful when you want to do any testing regarding transaction maturity.
Note: For the
produce_blocks
API to work, it is imperative to havemanual_blocks_enabled = true
in the config for the running node. See example below.
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.try_provider()?;
assert_eq!(provider.latest_block_height().await?, 0u32);
provider.produce_blocks(3, None).await?;
assert_eq!(provider.latest_block_height().await?, 3u32);
You can also set a custom block time as the second, optional argument. Here is an example:
let config = Config {
manual_blocks_enabled: true, // Necessary so the `produce_blocks` API can be used locally
block_production: Trigger::Interval {
block_time: Duration::from_secs(10),
},
..Config::local_node()
};
let wallets =
launch_custom_provider_and_get_wallets(WalletsConfig::default(), Some(config), None).await;
let wallet = &wallets[0];
let provider = wallet.try_provider()?;
assert_eq!(provider.latest_block_height().await?, 0u32);
provider
.produce_blocks(3, Some(Utc.timestamp_opt(100, 0).unwrap()))
.await?;
assert_eq!(provider.latest_block_height().await?, 3u32);
let req = PaginationRequest {
cursor: None,
results: 10,
direction: PageDirection::Forward,
};
let blocks: Vec<Block> = provider.get_blocks(req).await?.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);