首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >随时间而变化的白帽测试?

随时间而变化的白帽测试?
EN

Ethereum用户
提问于 2020-08-11 21:02:15
回答 6查看 32.7K关注 0票数 50

对于Ganache,有几个解决方案

那安全帽呢?他们实现了自己的本地块链,即“草帽网络”,这与Ganache不同。

EN

回答 6

Ethereum用户

回答已采纳

发布于 2021-01-29 22:16:43

使用硬帽子网络助手的

最简单的方法是在安全帽网络帮手中使用时间助手。

安装它们时:

代码语言:javascript
运行
复制
npm install @nomicfoundation/hardhat-network-helpers

然后你可以像这样使用它们:

代码语言:javascript
运行
复制
import { time } from "@nomicfoundation/hardhat-network-helpers";

async function main() {
  // advance time by one hour and mine a new block
  await helpers.time.increase(3600);

  // mine a new block with timestamp `newTimestamp`
  await helpers.time.increaseTo(newTimestamp);

  // set the timestamp of the next block but don't mine a new block
  await helpers.time.setNextBlockTimestamp(newTimestamp);
}

main();

您可以检查引用这里

使用原始JSON-RPC调用

这里有两个相关的RPC方法:evm_increaseTimeevm_setNextBlockTimestamp。在这两种情况下,它们都会影响下一个区块,但不要开采一个区块。

evm_increaseTime接收到若干秒,这些秒将被添加到最新块的时间戳中。evm_setNextBlockTimestamp接收一个绝对的UNIX时间戳(同样,以秒为单位),因此它不受当前块的影响。

示例

evm_increaseTime

代码语言:javascript
运行
复制
// suppose the current block has a timestamp of 01:00 PM
await network.provider.send("evm_increaseTime", [3600])
await network.provider.send("evm_mine") // this one will have 02:00 PM as its timestamp

evm_setNextBlockTimestamp

代码语言:javascript
运行
复制
await network.provider.send("evm_setNextBlockTimestamp", [1625097600])
await network.provider.send("evm_mine") // this one will have 2021-07-01 12:00 AM as its timestamp, no matter what the previous block has

请记住,“草帽网络”验证新的时间戳是否大于前一个时间戳,因此您不能发送任何值。

票数 77
EN

Ethereum用户

发布于 2021-11-03 17:11:58

到Ganache的新更新evm_mine命令添加了一个时间参数。现在,移动时间的最好方法是

代码语言:javascript
运行
复制
await ethers.provider.send("evm_mine", [newTimestampInSeconds]);

这样做更好,因为您只进行了一个RPC调用,而不是接受的解决办法中的2个。

请注意,你不能把时间倒流在“硬帽子”里。

票数 11
EN

Ethereum用户

发布于 2021-08-12 05:26:35

代码语言:javascript
运行
复制
const { expect } = require("chai");
const { ethers } = require('hardhat');

const sevenDays = 7 * 24 * 60 * 60;

const blockNumBefore = await ethers.provider.getBlockNumber();
const blockBefore = await ethers.provider.getBlock(blockNumBefore);
const timestampBefore = blockBefore.timestamp;

await ethers.provider.send('evm_increaseTime', [sevenDays]);
await ethers.provider.send('evm_mine');

const blockNumAfter = await ethers.provider.getBlockNumber();
const blockAfter = await ethers.provider.getBlock(blockNumAfter);
const timestampAfter = blockAfter.timestamp;

expect(blockNumAfter).to.be.equal(blockNumBefore + 1);
expect(timestampAfter).to.be.equal(timestampBefore + sevenDays);
票数 8
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/86633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档