Tweaking the Blockchain

Icon Link生成区块

您可以使用 produceBlocks 辅助函数来强制生成区块,以达到任意的区块高度。当您想进行一些关于交易成熟度的测试时,这将非常有用。

const provider = await Provider.create(FUEL_NETWORK_URL);
 
const block = await provider.getBlock('latest');
if (!block) {
  throw new Error('No latest block');
}
const { time: timeLastBlockProduced } = block;
 
const amountOfBlocksToProduce = 3;
const producedBlockHeigh = await provider.produceBlocks(amountOfBlocksToProduce);
 
const producedBlock = await provider.getBlock(producedBlockHeigh.toNumber());
 
expect(producedBlock).toBeDefined();
 
const oldest: Date = DateTime.fromTai64(timeLastBlockProduced);
const newest: Date = DateTime.fromTai64(producedBlock?.time || DateTime.TAI64_NULL);
 
expect(newest >= oldest).toBeTruthy();

Icon Link使用自定义时间戳生成区块

您还可以使用 produceBlocks 辅助函数通过指定第二个可选参数来生成具有自定义区块时间的区块。

const lastBlockTimestamp = DateTime.fromTai64(latestBlock.time).toUnixMilliseconds();
const latestBlockNumber = await provider.produceBlocks(3, lastBlockTimestamp + 1000);