Increasing the block height
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.get_provider()?;
assert_eq!(provider.latest_block_height().await?, 0);
provider.produce_blocks(3, None).await?;
assert_eq!(provider.latest_block_height().await?, 3);
You can also set a custom block time by providing TimeParameters
as the second, optional argument. TimeParameters
is defined as:
pub struct TimeParameters {
// The time to set on the first block
pub start_time: DateTime<Utc>,
// The time interval between subsequent blocks
pub block_time_interval: Duration,
}
And here is an example:
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[1].header.time.unwrap().timestamp(), 100);
assert_eq!(blocks[2].header.time.unwrap().timestamp(), 110);
assert_eq!(blocks[3].header.time.unwrap().timestamp(), 120);