-
Notifications
You must be signed in to change notification settings - Fork 725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setup gradual process for migrating to Typescript #495
Changes from all commits
189d068
1ec05ba
0272139
1d9786d
e3969ea
328f47c
eb46a82
8efdefa
f79e34e
9eb3b70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules | ||
.vscode | ||
package.json | ||
dist | ||
.nyc_output | ||
*.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import * as assert from 'assert' | ||
|
||
/** | ||
* Memory implements a simple memory model | ||
* for the ethereum virtual machine. | ||
*/ | ||
module.exports = class Memory { | ||
export default class Memory { | ||
_store: number[] | ||
|
||
constructor () { | ||
this._store = [] | ||
} | ||
|
@@ -11,9 +15,9 @@ module.exports = class Memory { | |
* Extends the memory given an offset and size. Rounds extended | ||
* memory to word-size. | ||
* @param {Number} offset | ||
* @param {size} size | ||
* @param {Number} size | ||
*/ | ||
extend (offset, size) { | ||
extend (offset: number, size: number) { | ||
if (size === 0) { | ||
return | ||
} | ||
|
@@ -31,18 +35,14 @@ module.exports = class Memory { | |
* @param {Number} size - How many bytes to write | ||
* @param {Buffer} value - Value | ||
*/ | ||
write (offset, size, value) { | ||
write (offset: number, size: number, value: Buffer) { | ||
if (size === 0) { | ||
return | ||
} | ||
|
||
if (value.length !== size) { | ||
throw new Error('Invalid value size') | ||
} | ||
|
||
if (offset + size > this._store.length) { | ||
throw new Error('Value exceeds memory capacity') | ||
} | ||
assert(value.length === size, 'Invalid value size') | ||
assert(offset + size <= this._store.length, 'Value exceeds memory capacity') | ||
assert(Buffer.isBuffer(value), 'Invalid value type') | ||
|
||
for (let i = 0; i < size; i++) { | ||
this._store[offset + i] = value[i] | ||
|
@@ -56,7 +56,7 @@ module.exports = class Memory { | |
* @param {Number} size - How many bytes to read | ||
* @returns {Buffer} | ||
*/ | ||
read (offset, size) { | ||
read (offset: number, size: number): Buffer { | ||
const loaded = this._store.slice(offset, offset + size) | ||
// Fill the remaining length with zeros | ||
for (let i = loaded.length; i < size; i++) { | ||
|
@@ -66,7 +66,7 @@ module.exports = class Memory { | |
} | ||
} | ||
|
||
const ceil = (value, ceiling) => { | ||
const ceil = (value: number, ceiling: number): number => { | ||
const r = value % ceiling | ||
if (r === 0) { | ||
return value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK (file). |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
const BN = require('bn.js') | ||
const ethUtil = require('ethereumjs-util') | ||
import BN = require('bn.js') | ||
import { MAX_INTEGER } from 'ethereumjs-util' | ||
const { ERROR, VmError } = require('../exceptions') | ||
|
||
/** | ||
* Implementation of the stack used in evm. | ||
*/ | ||
module.exports = class Stack { | ||
export default class Stack { | ||
_store: BN[] | ||
|
||
constructor () { | ||
this._store = [] | ||
} | ||
|
@@ -14,24 +16,29 @@ module.exports = class Stack { | |
return this._store.length | ||
} | ||
|
||
push (value) { | ||
if (this._store.length > 1023) { | ||
throw new VmError(ERROR.STACK_OVERFLOW) | ||
push (value: BN) { | ||
if (!BN.isBN(value)) { | ||
throw new VmError(ERROR.INTERNAL_ERROR) | ||
} | ||
|
||
if (!this._isValidValue(value)) { | ||
if (value.gt(MAX_INTEGER)) { | ||
throw new VmError(ERROR.OUT_OF_RANGE) | ||
} | ||
|
||
if (this._store.length > 1023) { | ||
throw new VmError(ERROR.STACK_OVERFLOW) | ||
} | ||
|
||
this._store.push(value) | ||
} | ||
|
||
pop () { | ||
pop (): BN { | ||
if (this._store.length < 1) { | ||
throw new VmError(ERROR.STACK_UNDERFLOW) | ||
} | ||
|
||
return this._store.pop() | ||
// Length is checked above, so pop shouldn't return undefined | ||
return this._store.pop()! | ||
} | ||
|
||
/** | ||
|
@@ -40,7 +47,7 @@ module.exports = class Stack { | |
* @param {Number} num - Number of items to pop | ||
* @returns {Array} | ||
*/ | ||
popN (num = 1) { | ||
popN (num: number = 1): BN[] { | ||
if (this._store.length < num) { | ||
throw new VmError(ERROR.STACK_UNDERFLOW) | ||
} | ||
|
@@ -56,7 +63,7 @@ module.exports = class Stack { | |
* Swap top of stack with an item in the stack. | ||
* @param {Number} position - Index of item from top of the stack (0-indexed) | ||
*/ | ||
swap (position) { | ||
swap (position: number) { | ||
if (this._store.length <= position) { | ||
throw new VmError(ERROR.STACK_UNDERFLOW) | ||
} | ||
|
@@ -73,26 +80,12 @@ module.exports = class Stack { | |
* Pushes a copy of an item in the stack. | ||
* @param {Number} position - Index of item to be copied (1-indexed) | ||
*/ | ||
dup (position) { | ||
dup (position: number) { | ||
if (this._store.length < position) { | ||
throw new VmError(ERROR.STACK_UNDERFLOW) | ||
} | ||
|
||
const i = this._store.length - position | ||
this.push(this._store[i]) | ||
} | ||
|
||
_isValidValue (value) { | ||
if (BN.isBN(value)) { | ||
if (value.lte(ethUtil.MAX_INTEGER)) { | ||
return true | ||
} | ||
} else if (Buffer.isBuffer(value)) { | ||
if (value.length <= 32) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK (file), yes, makes totally sense to inline this private method. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
], | ||
"scripts": { | ||
"coverage": "nyc npm run coverageTests && nyc report --reporter=text-lcov > .nyc_output/lcov.info", | ||
"coverageTests": "tape './tests/api/**/*.js' ./tests/tester.js -s", | ||
"coverageTests": "npm run build:dist && tape './tests/api/**/*.js' ./tests/tester.js -s --dist", | ||
"coveralls": "npm run coverage && if [ -n \"$COVERALLS_REPO_TOKEN\" ]; then coveralls <.nyc_output/lcov.info; fi", | ||
"testVM": "node ./tests/tester -v", | ||
"testStateByzantium": "npm run build:dist && node ./tests/tester -s --fork='Byzantium' --dist", | ||
|
@@ -17,12 +17,14 @@ | |
"testBuildIntegrity": "npm run build:dist && node ./tests/tester -s --dist --test='stackOverflow'", | ||
"testBlockchain": "npm run build:dist && node --stack-size=1500 ./tests/tester -b --fork='Petersburg' --dist --excludeDir='GeneralStateTests'", | ||
"testBlockchainGeneralStateTests": "npm run build:dist && node --stack-size=1500 ./tests/tester -b --dist --dir='GeneralStateTests'", | ||
"testAPI": "tape './tests/api/**/*.js'", | ||
"testAPI:browser": "karma start karma.conf.js", | ||
"testAPI": "npm run build:dist && tape './tests/api/**/*.js'", | ||
"testAPI:browser": "npm run build:dist && karma start karma.conf.js", | ||
"test": "echo \"[INFO] Generic test cmd not used. See package.json for more specific test run cmds.\"", | ||
"lint": "standard", | ||
"format": "ethereumjs-config-format", | ||
"format-fix": "ethereumjs-config-format-fix", | ||
"prepublishOnly": "npm run lint && npm run build:dist && npm run testBuildIntegrity", | ||
"build:dist": "babel lib/ -d dist/", | ||
"build:dist": "tsc", | ||
"build:docs": "documentation build ./lib/index.js ./lib/runBlockchain.js ./lib/runBlock.js ./lib/runTx.js ./lib/runCode.js ./lib/runCall.js --format md --shallow > ./docs/index.md", | ||
"formatTest": "node ./scripts/formatTest" | ||
}, | ||
|
@@ -35,14 +37,14 @@ | |
"VM" | ||
], | ||
"dependencies": { | ||
"@babel/runtime": "^7.4.0", | ||
"async": "^2.1.2", | ||
"async-eventemitter": "^0.2.2", | ||
"core-js-pure": "^3.0.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to be ok as a new dependency, see here. |
||
"ethereumjs-account": "^2.0.3", | ||
"ethereumjs-block": "~2.2.0", | ||
"ethereumjs-blockchain": "^3.4.0", | ||
"ethereumjs-common": "^1.1.0", | ||
"ethereumjs-util": "^6.0.0", | ||
"ethereumjs-util": "^6.1.0", | ||
"fake-merkle-patricia-tree": "^1.0.1", | ||
"functional-red-black-tree": "^1.0.1", | ||
"merkle-patricia-tree": "^2.3.2", | ||
|
@@ -51,10 +53,9 @@ | |
"util.promisify": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.2.3", | ||
"@babel/core": "^7.4.0", | ||
"@babel/plugin-transform-runtime": "^7.4.0", | ||
"@babel/preset-env": "^7.4.1", | ||
"@ethereumjs/config-prettier": "^1.1.1", | ||
"@types/bn.js": "^4.11.5", | ||
"@types/node": "^11.13.4", | ||
"browserify": "^16.2.3", | ||
"coveralls": "^3.0.0", | ||
"documentation": "^8.1.2", | ||
|
@@ -70,9 +71,12 @@ | |
"level-mem": "^3.0.1", | ||
"minimist": "^1.1.1", | ||
"nyc": "^12.0.2", | ||
"prettier": "^1.16.4", | ||
"rlp": "^2.2.3", | ||
"standard": "^10.0.0", | ||
"tap-spec": "^5.0.0", | ||
"tape": "4.6.3" | ||
"tape": "4.6.3", | ||
"typescript": "^3.4.3" | ||
}, | ||
"author": "mjbecze <mjbecze@gmail.com>", | ||
"contributors": [ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good (the whole file), super-much a fan of making the API here more strict and restrict to
Buffer
.