Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a7d9bca
vm: v4.2.0 release notes
evertonfraga May 4, 2020
debe156
vm: explicitly duplicate bignumbers on stack (#733)
jochem-brouwer Apr 30, 2020
2046561
Updated to use state dump method
smartcontracts Sep 25, 2020
09c3735
Removed logger
smartcontracts Sep 25, 2020
b328681
Linted files
smartcontracts Sep 25, 2020
a6ce7e5
Added logging back into the interpreter
smartcontracts Sep 25, 2020
89223b3
Skip balance and nonce checks
smartcontracts Sep 28, 2020
bb5d954
Updated package.json
smartcontracts Sep 28, 2020
9bfb4b9
Added experimental logging fix
smartcontracts Oct 5, 2020
b01eb13
Started contracts v2 migration
smartcontracts Oct 7, 2020
bb45255
Linted files
smartcontracts Oct 7, 2020
2a48aeb
kelvin changes
ben-chain Oct 8, 2020
3147553
update logging
ben-chain Oct 12, 2020
35e0fd0
random fixes
Oct 20, 2020
7c7e236
clean up logging
Oct 21, 2020
bc1dcb6
remove more console.logs
Oct 21, 2020
9ea2835
fix address begins with 0 bug
Oct 24, 2020
5e9c2b8
no-message create revert fix
Oct 27, 2020
957801c
improve logging
ben-chain Nov 5, 2020
d35c0e7
remove unused code
ben-chain Nov 5, 2020
238b6be
remove unused code
ben-chain Nov 5, 2020
0c1b35a
more cleanup
ben-chain Nov 5, 2020
1088789
whitespace removal
ben-chain Nov 5, 2020
9b24d41
remove unused commented line
ben-chain Nov 10, 2020
7cf29dd
fix decoding and update isAuthenticated interface
ben-chain Dec 14, 2020
d51fba8
publish new version, lint
K-Ho Nov 10, 2020
b919f87
Bumped version and linted
smartcontracts Dec 14, 2020
7573f22
Update README.md
K-Ho Feb 19, 2021
bc4dc4c
Fixed an issue because gas limit was too low
smartcontracts Mar 7, 2021
17462fd
Bump version too
smartcontracts Mar 7, 2021
0312576
Simplify diff
cgewecke Apr 27, 2021
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ Produces `dist` files for all sub-packages. This command can be scoped

#### `npm run build:tree -- --scope @ethereumjs/blockchain`

Builds all local packages that the provided package (eg: @ethereumjs/blockchain) depends on, and itself. This unusual syntax just means: pass whatever arguments are after `--` to the underlying script.
Builds all local packages that the provided package (eg: @ethereumjs/blockchain) depends on, and itself. This unusual syntax just means: pass whatever arguments are after `--` to the underlying script.

If no scope is provided, `npm run build:tree`, will build all sub-packages.

### Package scripts — run from `./packages/<name>`

**⚠️ Important: if you run `npm install` from the package directory, it will remove all links to the local packages, pulling all dependencies from npm. Run `npm install` from the root only.**

There's a set of rather standardized commands you will find in each package of this repository.

#### `npm run build`
Expand All @@ -101,7 +101,7 @@ Checks code style, according to the rules defined in [ethereumjs-config](https:/

#### `npm run lint:fix`

Fixes code style, according to the rules
Fixes code style, according to the rules

#### `npm run test`

Expand Down
126 changes: 126 additions & 0 deletions lib/ovm/ovm-state-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* External Imports */
import BN = require('bn.js')
import { BigNumber, ethers } from 'ethers'

/* Internal Imports */
import VM from '../index'
import Message from '../evm/message'
import { fromHexString, toHexString } from './utils/buffer-utils'
import { NULL_BYTES32 } from './utils/constants'

export interface OvmStateManagerOpts {
vm: VM
}

export class OvmStateManager {
public vm: VM
private _iface: any
private _handlers: {
[name: string]: any
}

constructor(opts: OvmStateManagerOpts) {
this.vm = opts.vm
this._iface = this.vm.contracts.OVM_StateManager.iface

this._handlers = {
hasAccount: this.hasAccount.bind(this),
hasEmptyAccount: this.hasEmptyAccount.bind(this),
setAccountNonce: this.setAccountNonce.bind(this),
getAccountNonce: this.getAccountNonce.bind(this),
getAccountEthAddress: this.getAccountEthAddress.bind(this),
getContractStorage: this.getContractStorage.bind(this),
hasContractStorage: this.hasContractStorage.bind(this),
putContractStorage: this.putContractStorage.bind(this),
testAndSetAccountLoaded: this.testAndSetAccountLoaded.bind(this),
testAndSetAccountChanged: this.testAndSetAccountChanged.bind(this),
testAndSetContractStorageLoaded: this.testAndSetContractStorageLoaded.bind(this),
testAndSetContractStorageChanged: this.testAndSetContractStorageChanged.bind(this),
isAuthenticated: this.isAuthenticated.bind(this),
}
}

async isAuthenticated(message: Message, context: any): Promise<boolean> {
return true
}

async handleCall(message: Message, context: any): Promise<any> {
const methodId = '0x' + message.data.slice(0, 4).toString('hex')
const fragment = this._iface.getFunction(methodId)
const functionArgs = this._iface.decodeFunctionData(fragment, toHexString(message.data))

let ret: any
if (fragment.name in this._handlers) {
ret = await this._handlers[fragment.name](...functionArgs)
ret = ret === null || ret === undefined ? ret : [ret]
}

try {
const encodedRet = this._iface.encodeFunctionResult(fragment, ret)
return fromHexString(encodedRet)
} catch (err) {
console.log(`Caught encoding error in ovmStateManager: ${err}`)
throw err
}
}

async hasAccount(address: string): Promise<boolean> {
return true
}

async hasEmptyAccount(address: string): Promise<boolean> {
return true
}

async setAccountNonce(address: string, nonce: BigNumber): Promise<void> {
const account = await this.vm.pStateManager.getAccount(fromHexString(address))
account.nonce = new BN(nonce.toNumber()).toArrayLike(Buffer)
return this.vm.pStateManager.putAccount(fromHexString(address), account)
}

async getAccountNonce(address: string): Promise<number> {
const account = await this.vm.pStateManager.getAccount(fromHexString(address))
return new BN(account.nonce).toNumber()
}

async getAccountEthAddress(address: string): Promise<string> {
return address
}

async getContractStorage(address: string, key: string): Promise<string> {
const ret = await this.vm.pStateManager.getContractStorage(
fromHexString(address),
fromHexString(key),
)

return ret.length ? toHexString(ret) : NULL_BYTES32
}

async hasContractStorage(address: string, key: string): Promise<boolean> {
return true
}

async putContractStorage(address: string, key: string, value: string): Promise<void> {
return this.vm.pStateManager.putContractStorage(
fromHexString(address),
fromHexString(key),
fromHexString(value),
)
}

async testAndSetAccountLoaded(address: string): Promise<boolean> {
return true
}

async testAndSetAccountChanged(address: string): Promise<boolean> {
return true
}

async testAndSetContractStorageLoaded(address: string, key: string): Promise<boolean> {
return true
}

async testAndSetContractStorageChanged(address: string, key: string): Promise<boolean> {
return true
}
}
26 changes: 26 additions & 0 deletions lib/ovm/utils/buffer-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const toHexAddress = (buf: any): string => {
// pad shorter values out
if (buf.length < 20) {
buf = Buffer.concat([Buffer.alloc(31), buf])
}

return (
'0x' +
buf
.slice(buf.length - 20)
.toString('hex')
.padStart(40, '0')
)
}

export const toHexString = (buf: any): string => {
return '0x' + buf.toString('hex')
}

export const fromHexString = (str: string): Buffer => {
return Buffer.from(str.slice(2), 'hex')
}

export const toAddressBuf = (address: string | Buffer): Buffer => {
return typeof address === 'string' ? fromHexString(address) : address
}
6 changes: 6 additions & 0 deletions lib/ovm/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const makeNullBytes = (length: number): string => {
return '0x' + '00'.repeat(length)
}

export const NULL_ADDRESS = makeNullBytes(20)
export const NULL_BYTES32 = makeNullBytes(32)
48 changes: 48 additions & 0 deletions lib/ovm/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import debug, { Debugger } from 'debug'
import { v4 as uuidv4 } from 'uuid'

export class Logger {
private _namespace: string
private _debugger: Debugger
private _section: string | undefined

constructor(namespace: string) {
this._namespace = namespace
this._debugger = debug(namespace)
}

log(message: string): void {
this._debugger(message)
}

scope(namespace: string, id?: string): Logger {
return new Logger(`${this._namespace}:${namespace}:${id || uuidv4()}`)
}

get enabled(): boolean {
return this._debugger.enabled
}

get namespace(): string {
return this._namespace
}

logSection(message: string): void {
this.log(`------${'-'.repeat(message.length)}------`)
this.log(`----- ${message} -----`)
this.log(`------${'-'.repeat(message.length)}------`)
}

open(message: string): void {
this._section = message
this.logSection(`BEGIN: ${this._section}`)
}

close(): void {
if (!this._section) {
return
}

this.logSection(`END : ${this._section}`)
}
}
2 changes: 1 addition & 1 deletion packages/vm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Added EIPs:
- [EIP-152](https://eips.ethereum.org/EIPS/eip-152): Blake 2b `F` precompile,
PR [#584](https://github.com/ethereumjs/ethereumjs-vm/pull/584)
- [EIP-1108](https://eips.ethereum.org/EIPS/eip-1108): Reduce `alt_bn128`
precompile gas costs,
precompile gas costs,
PR [#540](https://github.com/ethereumjs/ethereumjs-vm/pull/540)
(already released in `v4.0.0`)
- [EIP-1344](https://eips.ethereum.org/EIPS/eip-1344): Add ChainID Opcode,
Expand Down
8 changes: 8 additions & 0 deletions packages/vm/lib/evm/eei.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export interface Env {
contract: Account
// Different than address for DELEGATECALL and CALLCODE
codeAddress: Buffer

// Custom variables
originalTargetAddress: Buffer
}

/**
Expand Down Expand Up @@ -402,6 +405,7 @@ export default class EEI {
data: data,
isStatic: this._env.isStatic,
depth: this._env.depth + 1,
originalTargetAddress: this._env.originalTargetAddress,
})

return this._baseCall(msg)
Expand All @@ -420,6 +424,7 @@ export default class EEI {
data: data,
isStatic: this._env.isStatic,
depth: this._env.depth + 1,
originalTargetAddress: this._env.originalTargetAddress,
})

return this._baseCall(msg)
Expand All @@ -439,6 +444,7 @@ export default class EEI {
data: data,
isStatic: true,
depth: this._env.depth + 1,
originalTargetAddress: this._env.originalTargetAddress,
})

return this._baseCall(msg)
Expand All @@ -459,6 +465,7 @@ export default class EEI {
isStatic: this._env.isStatic,
delegatecall: true,
depth: this._env.depth + 1,
originalTargetAddress: this._env.originalTargetAddress,
})

return this._baseCall(msg)
Expand Down Expand Up @@ -520,6 +527,7 @@ export default class EEI {
salt: salt,
depth: this._env.depth + 1,
selfdestruct: selfdestruct,
originalTargetAddress: this._env.originalTargetAddress,
})

// empty the return data buffer
Expand Down
Loading