你可以使用 produce_blocks
来帮助实现任意的区块高度;当你想进行任何关于交易成熟度的测试时,这非常有用。
注意:为了使
produce_blocks
API 生效,必须在运行节点的配置中将manual_blocks_enabled
设置为true
。参见下面的示例。
let wallets =
launch_custom_provider_and_get_wallets(WalletsConfig::default(), None, 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);
你还可以将自定义的区块时间作为第二个可选参数。下面是一个示例:
let block_time = 20u32; // seconds
let config = NodeConfig {
// This is how you specify the time between blocks
block_production: Trigger::Interval {
block_time: std::time::Duration::from_secs(block_time.into()),
},
..NodeConfig::default()
};
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);
let origin_block_time = provider.latest_block_time().await?.unwrap();
let blocks_to_produce = 3;
provider.produce_blocks(blocks_to_produce, None).await?;
assert_eq!(provider.latest_block_height().await?, blocks_to_produce);
let expected_latest_block_time = origin_block_time
.checked_add_signed(Duration::try_seconds((blocks_to_produce * block_time) as i64).unwrap())
.unwrap();
assert_eq!(
provider.latest_block_time().await?.unwrap(),
expected_latest_block_time
);