Skip to content

Commit

Permalink
Fix evm_increaseTime bugs (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
masonforest authored Mar 16, 2020
1 parent 62d4e15 commit 6362be2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
12 changes: 12 additions & 0 deletions packages/core-utils/src/app/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ export const hexStrToNumber = (hexString: string): number => {
return parseInt(remove0x(hexString), 16)
}

/**
* Parses a hex string if one is given otherwise returns the number that was
* given
* @param str the String to parse
* @returns the parsed number.
*/
export const castToNumber = (stringOrNumber: string | number): number => {
return typeof stringOrNumber === 'number'
? stringOrNumber
: hexStrToNumber(stringOrNumber)
}

/**
* Converts the provided buffer into a hex string.
* @param buff The buffer.
Expand Down
13 changes: 9 additions & 4 deletions packages/rollup-full-node/src/app/test-web3-rpc-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* External Imports */
import { add0x, getLogger, remove0x } from '@eth-optimism/core-utils'
import { add0x, getLogger, castToNumber } from '@eth-optimism/core-utils'
import { OPCODE_WHITELIST_MASK } from '@eth-optimism/ovm'

import { createMockProvider, getWallets } from 'ethereum-waffle'
Expand All @@ -20,6 +20,7 @@ export class TestWeb3Handler extends DefaultWeb3Handler {
public static readonly successString = 'success'

private timestampIncreaseSeconds: number = 0
private timestampIncreaseSnapshots: Object = {}

/**
* Creates a local node, deploys the L2ExecutionManager to it, and returns a
Expand Down Expand Up @@ -92,7 +93,7 @@ export class TestWeb3Handler extends DefaultWeb3Handler {
*/
private increaseTimestamp(increaseSeconds: any): void {
try {
const increaseNumber = parseInt(remove0x(increaseSeconds), 16)
const increaseNumber = castToNumber(increaseSeconds)
if (increaseNumber < 0) {
throw Error('invalid param')
}
Expand All @@ -109,14 +110,18 @@ export class TestWeb3Handler extends DefaultWeb3Handler {
* @returns The snapshot id that can be used as an parameter of the revert endpoint
*/
private async snapshot(): Promise<string> {
return this.provider.send(Web3RpcMethods.snapshot, [])
const snapShotId = await this.provider.send(Web3RpcMethods.snapshot, [])
this.timestampIncreaseSnapshots[snapShotId] = this.timestampIncreaseSeconds
return snapShotId
}

/**
* Reverts state to the specified snapshot
* @param The snapshot id of the snapshot to restore
*/
private async revert(snapShotId: string): Promise<string> {
return this.provider.send(Web3RpcMethods.revert, [snapShotId])
const response = this.provider.send(Web3RpcMethods.revert, [snapShotId])
this.timestampIncreaseSeconds = this.timestampIncreaseSnapshots[snapShotId]
return response
}
}
28 changes: 26 additions & 2 deletions packages/rollup-full-node/test/app/test-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import '../setup'
/* External Imports */
import { add0x, getLogger, remove0x } from '@eth-optimism/core-utils'
import {
add0x,
getLogger,
remove0x,
castToNumber,
} from '@eth-optimism/core-utils'
import { ethers, ContractFactory } from 'ethers'

/* Internal Imports */
Expand Down Expand Up @@ -52,7 +57,7 @@ describe('TestHandler', () => {
const increase: number = 9999
const setRes: string = await testHandler.handleRequest(
Web3RpcMethods.increaseTimestamp,
[add0x(increase.toString(16))]
[increase]
)
setRes.should.equal(
TestWeb3Handler.successString,
Expand Down Expand Up @@ -119,6 +124,25 @@ describe('TestHandler', () => {
storageKey
)
res.should.equal(storageValue)
testRpcServer.close()
})

it('should revert changes to the timestamp', async () => {
const testRpcServer = new FullnodeRpcServer(testHandler, host, port)
testRpcServer.listen()
const httpProvider = new ethers.providers.JsonRpcProvider(baseUrl)
const startTimestamp = await httpProvider.send('evm_getTime', [])
// Increase timestamp by 1 second
await httpProvider.send('evm_increaseTime', [1])
// Take a snapshot at timestamp + 1
const snapShotId = await httpProvider.send('evm_snapshot', [])
// Increase timestamp by 1 second again
await httpProvider.send('evm_increaseTime', [1])
const response2 = await httpProvider.send('evm_revert', [snapShotId])
const timestamp = await httpProvider.send('evm_getTime', [])
// Time should be reverted to the timestamp when the snapshot is taken (timestamp + 1)
castToNumber(timestamp).should.eq(castToNumber(startTimestamp) + 1)
testRpcServer.close()
})
})
})

0 comments on commit 6362be2

Please sign in to comment.