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

Remove libp2p transport layer #2758

Merged
merged 17 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16,851 changes: 6,081 additions & 10,770 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/client/.eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
devnets/**/*.ts
libp2pBrowserBuild
14 changes: 6 additions & 8 deletions packages/client/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ to help contributors better understand how the project is organized.

- `/bin` Contains the CLI script for the `ethereumjs` command.
- `/docs` Contains auto-generated API docs.
- `/lib/blockchain` Contains the `Chain` class.
- `/lib/net` Contains all of the network layer classes including `Peer`, `Protocol` and its subclasses, `Server` and its subclasses, and `PeerPool`.
- `/lib/service` Contains the main Ethereum services (`FullEthereumService` and `LightEthereumService`).
- `/lib/rpc` Contains the RPC server (optionally) embedded in the client.
- `/lib/sync` Contains the various chain synchronizers and `Fetcher` helpers.
- `/lib/miner` Contains the miner module that can build new blocks on top of the chain.
- `/src/blockchain` Contains the `Chain` class.
- `/src/net` Contains all of the network layer classes including `Peer`, `Protocol` and its subclasses, `Server` and its subclasses, and `PeerPool`.
- `/src/service` Contains the main Ethereum services (`FullEthereumService` and `LightEthereumService`).
- `/src/rpc` Contains the RPC server (optionally) embedded in the client.
- `/src/sync` Contains the various chain synchronizers and `Fetcher` helpers.
- `/src/miner` Contains the miner module that can build new blocks on top of the chain.
- `/test` Contains test cases, testing helper functions, mocks and test data.

**Components**
Expand All @@ -48,12 +48,10 @@ to help contributors better understand how the project is organized.
if a new peer understands the `eth` protocol, it will contain an `eth` property that provides all `eth`
protocol methods (for example: `peer.eth.getBlockHeaders()`)
- `RlpxServer` [**In Progress**] Subclass of `Server` that implements the `devp2p/rlpx` transport.
- `Libp2pServer` [**In Progress**] Subclass of `Server` that implements the `libp2p` transport.
- `Peer` Represents a network peer. Instances of `Peer` are generated by the `Server`
subclasses and contain instances of supported protocol classes as properties. Instances of `Peer` subclasses can also be used to directly connect to other nodes via the `connect()` method. Peers emit `message` events
whenever a new message is received using any of the supported protocols.
- `RlpxPeer` [**In Progress**] Subclass of `Peer` that implements the `devp2p/rlpx` transport.
- `Libp2pPeer` [**In Progress**] Subclass of `Peer` that implements the `libp2p` transport.
- `Protocol` [**In Progress**] This class and subclasses provide a user-friendly wrapper around the
low level ethereum protocols `eth/66` and `les/4`. Subclasses must define the messages provided by the protocol.
- `EthProtocol` [**In Progress**] Implements `eth/66` protocol.
Expand Down
1 change: 0 additions & 1 deletion packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Here are some use cases:
- Set up your own local development networks (PoS with consensus client / PoA Clique / PoW with CPU miner)
- Run a network with your own custom [EthereumJS VM](../vm)
- Analyze what's in the Ethereum `mainnet` [transaction pool (mempool)](./lib/sync/txpool.ts)
- Run experiments with Ethereum browser sync (see [example](./examples/light-browser-sync.md)) **currently unsupported due to browser dependency issues**

The client has an extremely modular design by building upon central other libraries in the EthereumJS monorepo ([VM](../vm), [Merkle Patricia Tree](../trie), [Blockchain](../blockchain), [Block](../block), [tx](../tx), [devp2p](../devp2p) and [Common](../common)) and is therefore extremely well suited for a deep dive into Ethereum protocol development.

Expand Down
9 changes: 3 additions & 6 deletions packages/client/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import debug from 'debug'
import { Level } from 'level'

import { EthereumClient } from '../src/client'
import { Config } from '../src/config'
import { Config, SyncMode } from '../src/config'
import { LevelDB } from '../src/execution/level'
import { parseMultiaddrs } from '../src/util'

Expand All @@ -13,9 +13,7 @@ import { getLogger } from './logging'
export * from '../src/blockchain/chain'

// Peer
export * from '../src/net/peer/libp2ppeer'
export * from '../src/net/peer/peer'
export * from './libp2pnode'

// Peer Pool
export * from '../src/net/peerpool'
Expand All @@ -27,7 +25,6 @@ export * from '../src/net/protocol/lesprotocol'
export * from '../src/net/protocol/protocol'

// Server
export * from '../src/net/server/libp2pserver'
export * from '../src/net/server/server'

// EthereumClient
Expand Down Expand Up @@ -60,8 +57,8 @@ export async function createClient(args: any) {
const config = new Config({
common,
key,
transports: ['libp2p'],
syncmode: args.syncmode,
transports: ['rlpx'],
syncmode: SyncMode.None,
bootnodes,
multiaddrs: [],
logger,
Expand Down
10 changes: 10 additions & 0 deletions packages/client/browser/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ export function timeDiff(timestamp: number) {
const diff = new Date().getTime() / 1000 - timestamp
return timeDuration(diff)
}

/**
* Stub to exclude node only stats function
* @returns null
*/
export async function getV8Engine(): Promise<null> {
return null
}

export const isBrowser = new Function('try {return this===window;}catch(e){ return false;}')
93 changes: 93 additions & 0 deletions packages/client/libp2pBrowserBuild/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Blockchain } from '@ethereumjs/blockchain'
import { Chain, Common } from '@ethereumjs/common'
import debug from 'debug'
import { Level } from 'level'

import { EthereumClient } from '../src/client'
import { Config } from '../src/config'
import { LevelDB } from '../src/execution/level'
import { parseMultiaddrs } from '../src/util'

import { getLogger } from './logging'
// Blockchain
export * from '../src/blockchain/chain'

// Peer
export * from '../src/net/peer/libp2ppeer'
export * from '../src/net/peer/peer'
export * from './libp2pnode'

// Peer Pool
export * from '../src/net/peerpool'

// Protocol
export * from '../src/net/protocol/ethprotocol'
export * from '../src/net/protocol/flowcontrol'
export * from '../src/net/protocol/lesprotocol'
export * from '../src/net/protocol/protocol'

// Server
export * from '../src/net/server/libp2pserver'
export * from '../src/net/server/server'

// EthereumClient
export * from '../src/client'

// Service
export * from '../src/service/fullethereumservice'
export * from '../src/service/lightethereumservice'
export * from '../src/service/service'

// Synchronizer
export * from '../src/sync/fullsync'
export * from '../src/sync/lightsync'
export * from '../src/sync/sync'

// Utilities
export * from '../src/util/parse'

// Logging
export * from './logging'

export async function createClient(args: any) {
// Turn on `debug` logs, defaults to all client logging
debug.enable(args.debugLogs ?? '')
const logger = getLogger({ loglevel: args.loglevel })
const datadir = args.datadir ?? Config.DATADIR_DEFAULT
const common = new Common({ chain: args.network ?? Chain.Mainnet })
const key = await Config.getClientKey(datadir, common)
const bootnodes = args.bootnodes !== undefined ? parseMultiaddrs(args.bootnodes) : undefined
const config = new Config({
common,
key,
transports: ['libp2p'],
syncmode: args.syncmode,
bootnodes,
multiaddrs: [],
logger,
maxPerRequest: args.maxPerRequest,
minPeers: args.minPeers,
maxPeers: args.maxPeers,
discDns: false,
})
config.events.setMaxListeners(50)
const chainDB = new Level<string | Uint8Array, string | Uint8Array>(
`${datadir}/${common.chainName()}`
)

const blockchain = await Blockchain.create({
db: new LevelDB(chainDB),
common: config.chainCommon,
hardforkByHeadBlockNumber: true,
validateBlocks: true,
validateConsensus: false,
})
return EthereumClient.create({ config, blockchain, chainDB })
}

export async function run(args: any) {
const client = await createClient(args)
await client.open()
await client.start()
return client
}
10 changes: 10 additions & 0 deletions packages/client/libp2pBrowserBuild/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const pino = require('pino')

export function getLogger(options = { loglevel: 'info' }) {
return pino({
level: options.loglevel,
base: null,
})
}

export const defaultLogger = getLogger({ loglevel: 'debug' })
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { bytesToInt, bytesToUtf8, intToBytes, utf8ToBytes } from '@ethereumjs/ut
import * as pipe from 'it-pipe'
import * as pushable from 'it-pushable'

import { Sender } from './sender'
import { Sender } from '../../src/net/protocol/sender'

import type { Libp2pMuxedStream as MuxedStream } from '../../types'
import type { NestedUint8Array } from '@ethereumjs/util'
import type { MuxedStream } from 'libp2p-interfaces/dist/src/stream-muxer/types'

// TypeScript doesn't have support yet for ReturnType
// with generic types, so this wrapper is used as a helper.
Expand Down
161 changes: 161 additions & 0 deletions packages/client/libp2pBrowserBuild/net/package.json.browser.deps
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
{
"name": "@ethereumjs/client",
"version": "0.7.1",
"description": "EthereumJS client implementation",
"keywords": [
"ethereum",
"ethereumjs",
"client",
"blockchain",
"light sync",
"full sync"
],
"homepage": "https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/client#readme",
"bugs": {
"url": "https://github.com/ethereumjs/ethereumjs-monorepo/issues?q=is%3Aissue+label%3A%22package%3A+client%22"
},
"repository": {
"type": "git",
"url": "https://github.com/ethereumjs/ethereumjs-monorepo.git"
},
"license": "MPL-2.0",
"author": "Vinay Pulim ([email protected])",
"main": "dist/src/index.js",
"browser": "dist/bundle.js",
"types": "dist/src/index.d.ts",
"bin": {
"ethereumjs": "dist/bin/cli.js"
},
"files": [
"dist"
],
"scripts": {
"binWorkaround": "test -f dist/bin/cli.js || echo 'install fails if bin script does not exist (https://github.com/npm/cli/issues/2632), creating placeholder file at \"dist/bin/cli.js\"' && mkdir -p 'dist/bin' && touch dist/bin/cli.js",
"build": "npm run build:common && mkdir -p ./src/trustedSetup/ && cp -Rf ./src/trustedSetups ./dist/src/",
"build:browser": "npm install && ../../config/cli/ts-build.sh browser && npm run bundle && rm -rf dist.browser",
"build:common": "../../config/cli/ts-build.sh",
"bundle": "webpack",
"clean": "../../config/cli/clean-package.sh",
"client:start:ts": "ts-node bin/cli.ts",
"client:start:js": "npm run build && node dist/bin/cli.js",
"client:start": "npm run client:start:js --",
"client:start:dev1": "npm run client:start -- --discDns=false --discV4=false --bootnodes",
"client:start:dev2": "npm run client:start -- --discDns=false --discV4=false --port=30304 --dataDir=datadir-dev2",
"coverage": "c8 --all --reporter=lcov --reporter=text npm run test:unit",
"docs:build": "typedoc --options typedoc.js --tsconfig tsconfig.prod.cjs.json",
"lint": "../../config/cli/lint.sh",
"lint:diff": "../../config/cli/lint-diff.sh",
"lint:fix": "../../config/cli/lint-fix.sh",
"preinstall": "npm run binWorkaround",
"prepublishOnly": "../../config/cli/prepublish.sh",
"tape": "tape -r ts-node/register",
"test": "npm run test:unit && npm run test:integration",
"test:browser": "karma start karma.conf.js",
"test:cli": "npm run tape -- 'test/cli/*.spec.ts'",
"test:integration": "npm run tape -- 'test/integration/**/*.spec.ts'",
"test:unit": "npm run tape -- 'test/!(integration|cli|sim)/**/*.spec.ts' 'test/*.spec.ts'",
"tsc": "../../config/cli/ts-compile.sh"
},
"dependencies": {
"@ethereumjs/block": "4.2.2",
"@ethereumjs/blockchain": "6.2.2",
"@ethereumjs/common": "3.1.2",
"@ethereumjs/devp2p": "5.1.2",
"@ethereumjs/ethash": "2.0.5",
"@ethereumjs/evm": "1.3.2",
"@ethereumjs/rlp": "4.0.1",
"@ethereumjs/statemanager": "1.0.5",
"@ethereumjs/trie": "5.0.5",
"@ethereumjs/tx": "4.1.2",
"@ethereumjs/util": "8.0.6",
"@ethereumjs/vm": "6.4.2",
"abstract-level": "^1.0.3",
"body-parser": "^1.19.2",
"c-kzg": "^2.1.0",
"chalk": "^4.1.2",
"connect": "^3.7.0",
"cors": "^2.8.5",
"debug": "^4.3.3",
"ethereum-cryptography": "^2.0.0",
"fs-extra": "^10.1.0",
"it-pipe": "^1.1.0",
"jayson": "^4.0.0",
"jwt-simple": "^0.5.6",
"level": "^8.0.0",
"memory-level": "^1.0.0",
"peer-id": "^0.14.3",
"qheap": "^1.4.0",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.5",
"yargs": "^17.7.1"
},
"peerDependencies": {
"libp2p": "^0.30.7",
"libp2p-bootstrap": "^0.14.0",
"libp2p-interfaces": "^1.2.0",
"libp2p-kad-dht": "^0.20.6",
"libp2p-mplex": "^0.10.2",
"libp2p-tcp": "^0.15.3",
"libp2p-websockets": "^0.15.1",
"multiaddr": "^10.0.1",
"@chainsafe/libp2p-noise": "^4.1.1"
},
"peerDependenciesMeta": {
"libp2p": {
"optional": true
},
"libp2p-bootstrap": {
"optional": true
},
"libp2p-interfaces": {
"optional": true
},
"libp2p-kad-dht": {
"optional": true
},
"libp2p-mplex": {
"optional": true
},
"libp2p-tcp": {
"optional": true
},
"libp2p-websockets": {
"optional": true
},
"multiaddr": {
"optional": true
},
"@chainsafe/libp2p-noise": {
"optional": true
}
},
"devDependencies": {
"@types/body-parser": "^1.19.2",
"@types/connect": "^3.4.35",
"@types/fs-extra": "^9.0.13",
"@types/jwt-simple": "^0.5.33",
"@types/yargs": "^17.0.24",
"constants-browserify": "^1.0.0",
"crypto-browserify": "^3.12.0",
"file-replace-loader": "^1.2.0",
"it-pair": "^1.0.0",
"it-pushable": "^1.4.2",
"os-browserify": "^0.3.0",
"pino": "^5.8.0",
"process": "^0.11.10",
"stream-browserify": "^3.0.0",
"supertest": "^6.1.3",
"superwstest": "^2.0.1",
"testdouble": "^3.16.6",
"testdouble-timers": "^0.1.1",
"util": "^0.12.4",
"webpack": "^5.55.1",
"webpack-cli": "^4.8.0"
},
"engines": {
"node": ">=16"
},
"devDependenciesComments": {
"node-fetch": "Hotfix for client browser build error in older Node versions (12/14), #1305, 2021-06-18"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//@ts-nocheck
/**
* Libp2p Bundle
* @memberof module:net/peer
*/

import { NOISE } from '@chainsafe/libp2p-noise'
import * as LibP2P from 'libp2p'
import * as Bootstrap from 'libp2p-bootstrap'
Expand Down
Loading