Skip to content

Commit

Permalink
Make full node use timestamp (#22)
Browse files Browse the repository at this point in the history
* Adding timestamp and ability to configure fullnode to support test methods (test node)

Published packages:
 - @eth-optimism/[email protected]
 - @eth-optimism/[email protected]
 - @eth-optimism/[email protected]
 - @eth-optimism/[email protected]
 - @eth-optimism/[email protected]
 - @eth-optimism/[email protected]
 - @eth-optimism/[email protected]
 - @eth-optimism/[email protected]
 - @eth-optimism/[email protected]
 - @eth-optimism/[email protected]
 - @eth-optimism/[email protected]
  • Loading branch information
willmeister authored Feb 26, 2020
1 parent 7e74844 commit 4bf0c7e
Show file tree
Hide file tree
Showing 22 changed files with 243 additions and 112 deletions.
4 changes: 2 additions & 2 deletions packages/core-db/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eth-optimism/core-db",
"version": "0.0.1-alpha.11",
"version": "0.0.1-alpha.12",
"description": "Optimism DB Utils",
"main": "build/index.js",
"files": [
Expand Down Expand Up @@ -29,7 +29,7 @@
"url": "https://github.com/ethereum-optimism/optimism-monorepo.git"
},
"dependencies": {
"@eth-optimism/core-utils": "0.0.1-alpha.11",
"@eth-optimism/core-utils": "0.0.1-alpha.12",
"abstract-leveldown": "^6.2.2",
"async-lock": "^1.2.2",
"chai": "^4.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eth-optimism/core-utils",
"version": "0.0.1-alpha.11",
"version": "0.0.1-alpha.12",
"description": "Optimism Core Utils",
"main": "build/index.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@eth-optimism/docs",
"private": true,
"version": "0.0.1-alpha.11",
"version": "0.0.1-alpha.12",
"description": "Optimism docs",
"author": "Optimism",
"license": "MIT",
Expand Down
6 changes: 3 additions & 3 deletions packages/optimistic-game-semantics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eth-optimism/optimistic-game-semantics",
"version": "0.0.1-alpha.11",
"version": "0.0.1-alpha.12",
"description": "Optimism Optimistic Game Semantics",
"main": "build/index.js",
"files": [
Expand Down Expand Up @@ -29,8 +29,8 @@
"url": "https://github.com/ethereum-optimism/optimism-monorepo.git"
},
"dependencies": {
"@eth-optimism/core-db": "0.0.1-alpha.11",
"@eth-optimism/core-utils": "0.0.1-alpha.11",
"@eth-optimism/core-db": "0.0.1-alpha.12",
"@eth-optimism/core-utils": "0.0.1-alpha.12",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"debug": "^4.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ovm-truffle-provider-wrapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { execSync, spawn } from 'child_process'
* Starts a local OVM node process for testing. It will be killed when the current process terminates.
*/
const startLocalNode = () => {
const runText = `(async function(){ const {runFullnode} = require('@eth-optimism/rollup-full-node');runFullnode();})();`
const runText = `(async function(){ const {runFullnode} = require('@eth-optimism/rollup-full-node');runFullnode(true);})();`

// Assumes this process was kicked off with node, but that's true for `truffle test` and `yarn ...`
const sub = spawn(process.argv[0], [`-e`, `${runText}`], {
Expand Down
4 changes: 2 additions & 2 deletions packages/ovm-truffle-provider-wrapper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eth-optimism/ovm-truffle-provider-wrapper",
"version": "0.0.1-alpha.11",
"version": "0.0.1-alpha.12",
"description": "Optimism Truffle Provider Wrapper",
"main": "build/index.js",
"files": [
Expand Down Expand Up @@ -28,7 +28,7 @@
"url": "https://github.com/ethereum-optimism/optimism-monorepo.git"
},
"dependencies": {
"@eth-optimism/rollup-full-node": "0.0.1-alpha.11"
"@eth-optimism/rollup-full-node": "0.0.1-alpha.12"
},
"devDependencies": {
"@types/node": "^12.0.7",
Expand Down
8 changes: 4 additions & 4 deletions packages/ovm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eth-optimism/ovm",
"version": "0.0.1-alpha.11",
"version": "0.0.1-alpha.12",
"description": "An optimistic execution-compatible EVM",
"main": "build/index.js",
"files": [
Expand Down Expand Up @@ -49,9 +49,9 @@
"typescript": "^3.3.3333"
},
"dependencies": {
"@eth-optimism/core-db": "0.0.1-alpha.11",
"@eth-optimism/core-utils": "0.0.1-alpha.11",
"@eth-optimism/rollup-core": "0.0.1-alpha.11",
"@eth-optimism/core-db": "0.0.1-alpha.12",
"@eth-optimism/core-utils": "0.0.1-alpha.12",
"@eth-optimism/rollup-core": "0.0.1-alpha.12",
"@types/sinon-chai": "^3.2.2",
"chai": "^4.2.0",
"ethereum-waffle": "2.1.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/rollup-contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eth-optimism/rollup-contracts",
"version": "0.0.1-alpha.11",
"version": "0.0.1-alpha.12",
"description": "Optimistic Rollup smart contracts",
"main": "build/index.js",
"files": [
Expand Down Expand Up @@ -43,9 +43,9 @@
"typescript": "^3.3.3333"
},
"dependencies": {
"@eth-optimism/core-db": "0.0.1-alpha.11",
"@eth-optimism/core-utils": "0.0.1-alpha.11",
"@eth-optimism/rollup-core": "0.0.1-alpha.11",
"@eth-optimism/core-db": "0.0.1-alpha.12",
"@eth-optimism/core-utils": "0.0.1-alpha.12",
"@eth-optimism/rollup-core": "0.0.1-alpha.12",
"@types/sinon-chai": "^3.2.2",
"chai": "^4.2.0",
"ethereum-waffle": "2.1.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/rollup-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eth-optimism/rollup-core",
"version": "0.0.1-alpha.11",
"version": "0.0.1-alpha.12",
"description": "[Optimism] Optimistic Rollup Core Library",
"main": "build/index.js",
"files": [
Expand Down Expand Up @@ -29,8 +29,8 @@
"url": "https://github.com/ethereum-optimism/optimism-monorepo.git"
},
"dependencies": {
"@eth-optimism/core-db": "0.0.1-alpha.11",
"@eth-optimism/core-utils": "0.0.1-alpha.11",
"@eth-optimism/core-db": "0.0.1-alpha.12",
"@eth-optimism/core-utils": "0.0.1-alpha.12",
"async-lock": "^1.2.2",
"ethers": "^4.0.39"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/rollup-dev-tools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eth-optimism/rollup-dev-tools",
"version": "0.0.1-alpha.11",
"version": "0.0.1-alpha.12",
"description": "[Optimism] Optimistic Rollup Dev Tools Library",
"main": "build/index.js",
"files": [
Expand Down Expand Up @@ -33,8 +33,8 @@
"url": "https://github.com/ethereum-optimism/optimism-monorepo.git"
},
"dependencies": {
"@eth-optimism/core-utils": "0.0.1-alpha.11",
"@eth-optimism/rollup-core": "0.0.1-alpha.11",
"@eth-optimism/core-utils": "0.0.1-alpha.12",
"@eth-optimism/rollup-core": "0.0.1-alpha.12",
"async-lock": "^1.2.2",
"bn.js": "^5.1.1",
"dotenv": "^8.2.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/rollup-full-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eth-optimism/rollup-full-node",
"version": "0.0.1-alpha.11",
"version": "0.0.1-alpha.12",
"description": "[Optimism] Optimistic Rollup Full Node Library",
"main": "build/index.js",
"files": [
Expand Down Expand Up @@ -32,10 +32,10 @@
"url": "https://github.com/ethereum-optimism/optimism-monorepo.git"
},
"dependencies": {
"@eth-optimism/core-db": "0.0.1-alpha.11",
"@eth-optimism/core-utils": "0.0.1-alpha.11",
"@eth-optimism/ovm": "0.0.1-alpha.11",
"@eth-optimism/rollup-core": "0.0.1-alpha.11",
"@eth-optimism/core-db": "0.0.1-alpha.12",
"@eth-optimism/core-utils": "0.0.1-alpha.12",
"@eth-optimism/ovm": "0.0.1-alpha.12",
"@eth-optimism/rollup-core": "0.0.1-alpha.12",
"async-lock": "^1.2.2",
"axios": "^0.19.0",
"cors": "^2.8.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/rollup-full-node/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export * from './block-builder'
export * from './block-submitter'
export * from './fullnode-rpc-server'
export * from './handler'
export * from './web3-rpc-handler'
export * from './test-web3-rpc-handler'
export * from './utils'

// Constant exports
Expand Down
97 changes: 97 additions & 0 deletions packages/rollup-full-node/src/app/test-web3-rpc-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* External Imports */
import { add0x, getLogger, remove0x } from '@eth-optimism/core-utils'
import { OPCODE_WHITELIST_MASK } from '@eth-optimism/ovm'

import { createMockProvider, getWallets } from 'ethereum-waffle'
import { Contract, Wallet } from 'ethers'
import { Web3Provider } from 'ethers/providers'

/* Internal Imports */
import { DEFAULT_ETHNODE_GAS_LIMIT } from './index'
import { DefaultWeb3Handler } from './web3-rpc-handler'
import { UnsupportedMethodError, Web3RpcMethods } from '../types'

const log = getLogger('test-web3-handler')

/**
* Test Handler that provides extra functionality for testing.
*/
export class TestWeb3Handler extends DefaultWeb3Handler {
public static readonly successString = 'success'

private timestampIncreaseSeconds: number = 0

/**
* Creates a local node, deploys the L2ExecutionManager to it, and returns a
* TestHandler that handles Web3 requests to it.
*
* @param provider (optional) The web3 provider to use.
* @returns The constructed Web3 handler.
*/
public static async create(
provider: Web3Provider = createMockProvider({
gasLimit: DEFAULT_ETHNODE_GAS_LIMIT,
})
): Promise<TestWeb3Handler> {
// Initialize a fullnode for us to interact with
const [wallet] = getWallets(provider)
const executionManager: Contract = await DefaultWeb3Handler.deployExecutionManager(
wallet,
OPCODE_WHITELIST_MASK
)

return new TestWeb3Handler(provider, wallet, executionManager)
}

protected constructor(
provider: Web3Provider,
wallet: Wallet,
executionManager: Contract
) {
super(provider, wallet, executionManager)
}

/**
* Override to add some test RPC methods.
*/
public async handleRequest(method: string, params: any[]): Promise<string> {
if (method === Web3RpcMethods.increaseTimestamp) {
this.assertParameters(params, 1)
this.increaseTimestamp(params[0])
log.debug(`Set increased timestamp by ${params[0]} seconds.`)
return TestWeb3Handler.successString
}
if (method === Web3RpcMethods.getTimestamp) {
this.assertParameters(params, 0)
return add0x(this.getTimestamp().toString(16))
}

return super.handleRequest(method, params)
}

/**
* Returns the configured timestamp if there is one, else standard timestamp calculation.
* @returns The timestamp.
*/
protected getTimestamp(): number {
return super.getTimestamp() + this.timestampIncreaseSeconds
}

/**
* Sets timestamp to use for future transactions.
* @param increaseSeconds The increase in seconds as a hex string
*/
private increaseTimestamp(increaseSeconds: any): void {
try {
const increaseNumber = parseInt(remove0x(increaseSeconds), 16)
if (increaseNumber < 0) {
throw Error('invalid param')
}
this.timestampIncreaseSeconds += increaseNumber
} catch (e) {
const msg: string = `Expected parameter for ${Web3RpcMethods.increaseTimestamp} to be a positive number or string of a positive, base-10 number. Received: ${increaseSeconds}`
log.error(msg)
throw new UnsupportedMethodError(msg)
}
}
}
2 changes: 1 addition & 1 deletion packages/rollup-full-node/src/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FullnodeHandler } from '../types'
import { Web3Provider } from 'ethers/providers'

/* Internal Imports */
import { DefaultWeb3Handler } from './handler'
import { DefaultWeb3Handler } from './web3-rpc-handler'
import { FullnodeRpcServer } from './fullnode-rpc-server'

const log = getLogger('utils')
Expand Down
Loading

0 comments on commit 4bf0c7e

Please sign in to comment.