Skip to content

Commit

Permalink
Fix warp cheatcode (#255)
Browse files Browse the repository at this point in the history
* fix warp cheatcode

* revert if warp input value is greater than type(uint64).max

* Make max uint64 a global value
  • Loading branch information
anishnaik authored Nov 9, 2023
1 parent ce962f9 commit e471c52
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
20 changes: 16 additions & 4 deletions chain/standard_cheat_code_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
// StandardCheatcodeContractAddress is the address for the standard cheatcode contract
var StandardCheatcodeContractAddress = common.HexToAddress("0x7109709ECfa91a80626fF3989D68f67F5b1DD12D")

// MaxUint64 holds the max value an uint64 can take
var _, MaxUint64 = utils.GetIntegerConstraints(false, 64)

// getStandardCheatCodeContract obtains a CheatCodeContract which implements common cheat codes.
// Returns the precompiled contract, or an error if one occurs.
func getStandardCheatCodeContract(tracer *cheatCodeTracer) (*CheatCodeContract, error) {
Expand Down Expand Up @@ -67,13 +70,22 @@ func getStandardCheatCodeContract(tracer *cheatCodeTracer) (*CheatCodeContract,

// Warp: Sets VM timestamp
contract.addMethod(
"warp", abi.Arguments{{Type: typeUint64}}, abi.Arguments{},
"warp", abi.Arguments{{Type: typeUint256}}, abi.Arguments{},
func(tracer *cheatCodeTracer, inputs []any) ([]any, *cheatCodeRawReturnData) {
// Maintain our changes until the transaction exits.
original := tracer.evm.Context.Time
tracer.evm.Context.Time = inputs[0].(uint64)
originalTime := tracer.evm.Context.Time

// Retrieve new timestamp and make sure it is LEQ max value of an uint64
newTime := inputs[0].(*big.Int)
if newTime.Cmp(MaxUint64) > 0 {
return nil, cheatCodeRevertData([]byte("warp: timestamp exceeds max value of type(uint64).max"))
}

// Set the time
tracer.evm.Context.Time = newTime.Uint64()
tracer.CurrentCallFrame().onTopFrameExitRestoreHooks.Push(func() {
tracer.evm.Context.Time = original
// Reset the time
tracer.evm.Context.Time = originalTime
})
return nil, nil
},
Expand Down
10 changes: 9 additions & 1 deletion fuzzing/testdata/contracts/cheat_codes/vm/warp.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This test ensures that the block timestamp can be set with cheat codes
interface CheatCodes {
function warp(uint64) external;
function warp(uint256) external;
}

contract TestContract {
Expand All @@ -15,5 +15,13 @@ contract TestContract {
assert(block.timestamp == 7);
cheats.warp(9);
assert(block.timestamp == 9);

// Ensure that a value greater than type(uint64).max will cause warp to revert
// This is not the best way to test it but gets the job done
try cheats.warp(type(uint64).max + 1) {
assert(false);
} catch {
assert(true);
}
}
}

0 comments on commit e471c52

Please sign in to comment.