PatrickAlphaC / hardhat-smartcontract-lottery-fcc

MIT License
117 stars 183 forks source link

block.timestamp - lastTimestamp is not equals interval #196

Closed lzl1450318612 closed 8 months ago

lzl1450318612 commented 8 months ago
it("it should not upkeepNeeded when time is not enough", async () => {
          await lottery.enterLottery({ value: lotteryEnterFee });
          await network.provider.send("evm_increaseTime", [
            Number(interval) - 1,
          ]);
          await network.provider.send("evm_mine", []);
          const blockTs = await lottery.getBlockTimestamp();
          const lastTs = await lottery.getLastTimestamp();

          console.log(`block: ${blockTs}`);
          console.log(`last: ${lastTs}`);
          console.log(`diff: ${blockTs - lastTs}`);

          const { upkeepNeeded } = await lottery.checkUpkeep.staticCall("0x");
          assert(!upkeepNeeded);
        });

when I test "it should not upkeepNeeded when time is not enough", I found AssertionError. Then I print block.timestamp and lastTimestamp, and I found the lastTimestamp is not actually equals interval -1. For example, my interval is 30, so interval-1 should be 29, but after I send the evm_increaseTime, my log is:

block: 1700726771 last: 1700726740 diff: 31

Why diff is 31?

The part of setting lastTimestamp in my contract:

 constructor(
        address vrfCoordinatorAddress,
        uint256 enterFee,
        bytes32 gasLimitKeyHash,
        uint64 subscriptionId,
        uint32 callbackGasLimit,
        uint256 lotteryInterval
    ) VRFConsumerBaseV2(vrfCoordinatorAddress) {
        i_vrfCoordinator = VRFCoordinatorV2Interface(vrfCoordinatorAddress);
        i_gasLimitKeyHash = gasLimitKeyHash;
        i_enterFee = enterFee;
        i_subscriptionId = subscriptionId;
        i_callbackGasLimit = callbackGasLimit;
        s_lotteryState = LotteryState.OPEN;
        s_lastTimestamp = block.timestamp;
        i_lotteryInterval = lotteryInterval;
    }

function getLastTimestamp() public view returns (uint256) {
    return s_lastTimestamp;
}

function getBlockTimestamp() public view returns (uint256) {
    return block.timestamp;
}

Is there any kind person who can help?

PatrickAlphaC commented 8 months ago

Hello! Thank you for making this issue.

Can you:

  1. Make this a discusson on the full repo? https://github.com/smartcontractkit/full-blockchain-solidity-course-js/

Thanks!