Skip to content

Commit

Permalink
chore: merge with master
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrocheleau committed Aug 9, 2024
2 parents 533fa8c + 6021203 commit 31c94af
Show file tree
Hide file tree
Showing 33 changed files with 498 additions and 127 deletions.
21 changes: 16 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"mcl-wasm": "^1.5.0",
"memory-level": "^1.0.0",
"prom-client": "^15.1.0",
"rustbn-wasm": "^0.4.0",
"verkle-cryptography-wasm": "^0.4.5",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.5",
Expand Down
9 changes: 7 additions & 2 deletions packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
DBSetHashToNumber,
DBSetTD,
} from '@ethereumjs/blockchain'
import { ConsensusType, Hardfork } from '@ethereumjs/common'
import { MCLBLS } from '@ethereumjs/evm'
import { CacheType, ConsensusType, Hardfork } from '@ethereumjs/common'
import { MCLBLS, RustBN254 } from '@ethereumjs/evm'
import { getGenesis } from '@ethereumjs/genesis'
import {
CacheType,
Expand All @@ -25,6 +25,7 @@ import {
import { VM, runBlock, runTx } from '@ethereumjs/vm'
import { writeFileSync } from 'fs'
import * as mcl from 'mcl-wasm'
import { initRustBN } from 'rustbn-wasm'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'

import { Event } from '../types.js'
Expand Down Expand Up @@ -182,12 +183,14 @@ export class VMExecution extends Execution {
})

await mcl.init(mcl.BLS12_381)
const rustBN = await initRustBN()
this.merkleVM = await VM.create({
common: this.config.execCommon,
blockchain: this.chain.blockchain,
stateManager,
evmOpts: {
bls: new MCLBLS(mcl),
bn254: new RustBN254(rustBN),
},
profilerOpts: this.config.vmProfilerOpts,
})
Expand All @@ -205,12 +208,14 @@ export class VMExecution extends Execution {
verkleCrypto,
})
await mcl.init(mcl.BLS12_381)
const rustBN = await initRustBN()
this.verkleVM = await VM.create({
common: this.config.execCommon,
blockchain: this.chain.blockchain,
stateManager,
evmOpts: {
bls: new MCLBLS(mcl),
bn254: new RustBN254(rustBN),
},
profilerOpts: this.config.vmProfilerOpts,
})
Expand Down
37 changes: 34 additions & 3 deletions packages/client/src/rpc/modules/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { middleware } from '../validation.js'

import type { Chain } from '../../blockchain/index.js'
import type { EthereumClient } from '../../client.js'
import type { RlpxPeer } from '../../net/peer/rlpxpeer.js'
import type { Service } from '../../service/index.js'

/**
Expand All @@ -28,14 +29,14 @@ export class Admin {
this._rpcDebug = rpcDebug

this.nodeInfo = middleware(callWithStackTrace(this.nodeInfo.bind(this), this._rpcDebug), 0, [])
this.peers = middleware(callWithStackTrace(this.peers.bind(this), this._rpcDebug), 0, [])
}

/**
* Returns information about the currently running node.
* see for reference: https://geth.ethereum.org/docs/rpc/ns-admin#admin_nodeinfo
* @param params An empty array
* see for reference: https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-admin#admin_peers
*/
async nodeInfo(_params: []) {
async nodeInfo() {
const rlpxInfo = this._client.config.server!.getRlpxInfo()
const { enode, id, ip, listenAddr, ports } = rlpxInfo
const { discovery, listener } = ports
Expand Down Expand Up @@ -68,4 +69,34 @@ export class Admin {
}
return nodeInfo
}

/**
* Returns information about currently connected peers
* @returns an array of objects containing information about peers (including id, eth protocol versions supported, client name, etc.)
*/
async peers() {
const peers = this._client.services.filter((serv) => serv.name === 'eth')[0]?.pool
.peers as RlpxPeer[]

return peers?.map((peer) => {
return {
id: peer.id,
// Typescript complains about the typing of `_hello` if we make rlpxPeer possibly null
name: (peer.rlpxPeer as any)['_hello'].clientId ?? null,
protocols: {
eth: {
head: peer.eth?.updatedBestHeader
? bytesToHex(peer.eth.updatedBestHeader?.hash())
: bytesToHex(peer.eth?.status.bestHash),
difficulty: peer.eth?.status.td.toString(10),
version: peer.eth?.['versions'].slice(-1)[0] ?? null,
},
},
caps: peer.eth?.['versions'].map((ver) => 'eth/' + ver),
network: {
remoteAddress: peer.address,
},
}
})
}
}
38 changes: 38 additions & 0 deletions packages/client/test/rpc/admin/peers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { randomBytes } from 'crypto'
import { assert, describe, it } from 'vitest'

import { createClient, createManager, getRpcClient, startRPC } from '../helpers.js'

const method = 'admin_peers'

describe(method, () => {
it('works', async () => {
const manager = createManager(await createClient({ opened: true, noPeers: true }))
const rpc = getRpcClient(startRPC(manager.getMethods()))

console.log(manager['_client'].services[0].pool)
//@ts-ignore
manager['_client'].services[0].pool.peers = [
{
id: 'abcd',
eth: {
versions: ['68'],
status: {
td: 1n,
bestHash: randomBytes(32),
},
},
rlpxPeer: {
_hello: {
clientId: 'fakeClient',
},
},
address: '127.0.0.1:8545',
},
]
const res = await rpc.request(method, [])
const { result } = res
console.log(res)
assert.notEqual(result, undefined, 'admin_peers returns a value')
})
})
6 changes: 3 additions & 3 deletions packages/evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@
"@ethereumjs/statemanager": "^2.3.0",
"@ethereumjs/tx": "^5.3.0",
"@ethereumjs/util": "^9.0.3",
"@noble/curves": "^1.4.2",
"@noble/curves": "^1.5.0",
"@types/debug": "^4.1.9",
"debug": "^4.3.3",
"ethereum-cryptography": "^2.2.1",
"rustbn-wasm": "^0.4.0"
"ethereum-cryptography": "^2.2.1"
},
"devDependencies": {
"@ethersproject/abi": "^5.0.12",
Expand All @@ -81,6 +80,7 @@
"minimist": "^1.2.5",
"node-dir": "^0.1.17",
"rollup-plugin-visualizer": "^5.12.0",
"rustbn-wasm": "^0.4.0",
"solc": "^0.8.1",
"split": "^1.0.1"
},
Expand Down
12 changes: 5 additions & 7 deletions packages/evm/src/constructors.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Common, Mainnet } from '@ethereumjs/common'
import { SimpleStateManager } from '@ethereumjs/statemanager'
import { initRustBN } from 'rustbn-wasm'

import { NobleBN254 } from './precompiles/index.js'
import { DefaultBlockchain } from './types.js'

import { EVM } from './index.js'

import type { EVMOpts, bn128 } from './index.js'

let initializedRustBN: bn128 | undefined = undefined
import type { EVMOpts } from './index.js'

/**
* Use this async static constructor for the initialization
Expand All @@ -19,8 +17,8 @@ let initializedRustBN: bn128 | undefined = undefined
*/
export async function createEVM(createOpts?: EVMOpts) {
const opts = createOpts ?? ({} as EVMOpts)
const bn128 = initializedRustBN ?? ((await initRustBN()) as bn128)
initializedRustBN = bn128

opts.bn254 = new NobleBN254()

if (opts.common === undefined) {
opts.common = new Common({ chain: Mainnet })
Expand All @@ -34,5 +32,5 @@ export async function createEVM(createOpts?: EVMOpts) {
opts.stateManager = new SimpleStateManager()
}

return new EVM(opts, bn128)
return new EVM(opts)
}
11 changes: 6 additions & 5 deletions packages/evm/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ import type {
Blockchain,
CustomOpcode,
EVMBLSInterface,
EVMBN254Interface,
EVMEvents,
EVMInterface,
EVMOpts,
EVMResult,
EVMRunCallOpts,
EVMRunCodeOpts,
ExecResult,
bn128,
} from './types.js'
import type { Common, StateManagerInterface } from '@ethereumjs/common'

Expand Down Expand Up @@ -143,7 +143,7 @@ export class EVM implements EVMInterface {

protected readonly _emit: (topic: string, data: any) => Promise<void>

private _bn128: bn128
private _bn254: EVMBN254Interface

/**
*
Expand All @@ -156,7 +156,7 @@ export class EVM implements EVMInterface {
* @param opts The EVM options
* @param bn128 Initialized bn128 WASM object for precompile usage (internal)
*/
constructor(opts: EVMOpts, bn128: bn128) {
constructor(opts: EVMOpts) {
this.common = opts.common!
this.blockchain = opts.blockchain!
this.stateManager = opts.stateManager!
Expand All @@ -172,7 +172,6 @@ export class EVM implements EVMInterface {
}
}

this._bn128 = bn128
this.events = new AsyncEventEmitter()
this._optsCached = opts

Expand Down Expand Up @@ -215,10 +214,12 @@ export class EVM implements EVMInterface {
this.getActiveOpcodes()
this._precompiles = getActivePrecompiles(this.common, this._customPrecompiles)

// Precompile crypto libraries
if (this.common.isActivatedEIP(2537)) {
this._bls = opts.bls ?? new NobleBLS()
this._bls.init?.()
}
this._bn254 = opts.bn254!

this._emit = async (topic: string, data: any): Promise<void> => {
return new Promise((resolve) => this.events.emit(topic as keyof EVMEvents, data, resolve))
Expand Down Expand Up @@ -1085,7 +1086,7 @@ export class EVM implements EVMInterface {
stateManager: this.stateManager.shallowCopy(),
}
;(opts.stateManager as any).common = common
return new EVM(opts, this._bn128)
return new EVM(opts)
}

public getPerformanceLogs() {
Expand Down
3 changes: 3 additions & 0 deletions packages/evm/src/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export enum ERROR {
BLS_12_381_INPUT_EMPTY = 'input is empty',
BLS_12_381_FP_NOT_IN_FIELD = 'fp point not in field',

// BN254 errors
BN254_FP_NOT_IN_FIELD = 'fp point not in field',

// Point Evaluation Errors
INVALID_COMMITMENT = 'kzg commitment does not match versioned hash',
INVALID_INPUTS = 'kzg inputs invalid',
Expand Down
10 changes: 8 additions & 2 deletions packages/evm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ import { getOpcodesForHF } from './opcodes/index.js'
import {
MCLBLS,
NobleBLS,
NobleBN254,
type PrecompileInput,
RustBN254,
getActivePrecompiles,
} from './precompiles/index.js'

import type { InterpreterStep } from './interpreter.js'
import type {
EVMBLSInterface,
EVMBN254Interface,
EVMInterface,
EVMOpts,
EVMResult,
EVMRunCallOpts,
EVMRunCodeOpts,
ExecResult,
Log,
bn128,
} from './types.js'
export * from './logger.js'

export type {
bn128,
EVMBLSInterface,
EVMBN254Interface,
EVMInterface,
EVMOpts,
EVMResult,
Expand All @@ -46,6 +50,8 @@ export {
MCLBLS,
Message,
NobleBLS,
NobleBN254,
RustBN254,
validateEOF,
}

Expand Down
Loading

0 comments on commit 31c94af

Please sign in to comment.