Skip to content
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

Migrate precompiles, opcodes, eei, message, txContext to typescript #497

Merged
merged 24 commits into from
Apr 21, 2019
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate evm memory to ts
s1na committed Apr 11, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 8efdefab275289e18f4a790de59e27af1917496f
2 changes: 1 addition & 1 deletion lib/evm/loop.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ const utils = require('ethereumjs-util')
const { StorageReader } = require('../state')
const PStateManager = require('../state/promisified')
const { ERROR, VmError } = require('../exceptions')
const Memory = require('./memory')
const Memory = require('./memory').default
const Stack = require('./stack').default
const EEI = require('./eei')
const lookupOpInfo = require('./opcodes.js')
26 changes: 13 additions & 13 deletions lib/evm/memory.ts
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
2 changes: 1 addition & 1 deletion tests/api/evm/memory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const tape = require('tape')
const Memory = require('../../../dist/evm/memory')
const Memory = require('../../../dist/evm/memory').default

tape('Memory', t => {
const m = new Memory()