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

Implement admin_addPeer #3788

Merged
merged 8 commits into from
Nov 6, 2024
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
194 changes: 6 additions & 188 deletions package-lock.json

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

47 changes: 45 additions & 2 deletions packages/client/src/rpc/modules/admin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { bytesToHex } from '@ethereumjs/util'

import { Config } from '../../index.js'
import { RlpxPeer } from '../../net/peer/rlpxpeer.js'

Check warning on line 4 in packages/client/src/rpc/modules/admin.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/modules/admin.ts#L3-L4

Added lines #L3 - L4 were not covered by tests
import { getClientVersion } from '../../util/index.js'
import { INTERNAL_ERROR } from '../error-code.js'

Check warning on line 6 in packages/client/src/rpc/modules/admin.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/modules/admin.ts#L6

Added line #L6 was not covered by tests
import { callWithStackTrace } from '../helpers.js'
import { middleware } from '../validation.js'
import { middleware, validators } from '../validation.js'

Check warning on line 8 in packages/client/src/rpc/modules/admin.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/modules/admin.ts#L8

Added line #L8 was not covered by tests

import type { Chain } from '../../blockchain/index.js'
import type { EthereumClient } from '../../client.js'
import type { RlpxPeer } from '../../net/peer/rlpxpeer.js'
import type { RlpxServer } from '../../net/server/rlpxserver.js'

Check warning on line 12 in packages/client/src/rpc/modules/admin.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/modules/admin.ts#L12

Added line #L12 was not covered by tests
import type { FullEthereumService } from '../../service/index.js'

/**
Expand All @@ -30,6 +33,15 @@

this.nodeInfo = middleware(callWithStackTrace(this.nodeInfo.bind(this), this._rpcDebug), 0, [])
this.peers = middleware(callWithStackTrace(this.peers.bind(this), this._rpcDebug), 0, [])
this.addPeer = middleware(callWithStackTrace(this.addPeer.bind(this), this._rpcDebug), 1, [
[
validators.object({
address: validators.ipv4Address,
udpPort: validators.unsignedInteger,
tcpPort: validators.unsignedInteger,
}),
],
])

Check warning on line 44 in packages/client/src/rpc/modules/admin.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/modules/admin.ts#L36-L44

Added lines #L36 - L44 were not covered by tests
}

/**
Expand Down Expand Up @@ -97,4 +109,35 @@
}
})
}

/**
* Attempts to add a peer to client service peer pool using the RLPx server address and port
* e.g. `.admin_addPeer [{"address": "127.0.0.1", "tcpPort": 30303, "udpPort": 30303}]`
* @param params An object containing an address, tcpPort, and udpPort for target server to connect to
*/
async addPeer(params: [Object]) {
const service = this._client.service as any as FullEthereumService
const server = service.pool.config.server as RlpxServer
const dpt = server.dpt

let peerInfo
try {
peerInfo = await dpt!.addPeer(params[0])
const rlpxPeer = new RlpxPeer({
config: new Config(),
id: bytesToHex(peerInfo.id!),
host: peerInfo.address!,
port: peerInfo.tcpPort as number,
})
service.pool.add(rlpxPeer)
} catch (err: any) {
throw {
code: INTERNAL_ERROR,
message: `failed to add peer: ${JSON.stringify(params)}`,
stack: err?.stack,
}
}

return peerInfo !== undefined
}

Check warning on line 142 in packages/client/src/rpc/modules/admin.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/modules/admin.ts#L112-L142

Added lines #L112 - L142 were not covered by tests
}
20 changes: 19 additions & 1 deletion packages/client/src/rpc/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,29 @@
},

/**
* number validator to check if type is integer
* bool validator to check if type is valid ipv4 address

Check warning on line 310 in packages/client/src/rpc/validation.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/validation.ts#L310

Added line #L310 was not covered by tests
* @param params parameters of method
* @param index index of parameter
*/
get ipv4Address() {
// regex from https://stackoverflow.com/questions/5284147/validating-ipv4-addresses-with-regexp
const ipv4Regex = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/

return (params: any[], index: number) => {
if (!ipv4Regex.test(params[index])) {
return {
code: INVALID_PARAMS,
message: `invalid argument ${index}: argument is not ipv4 address`,
}
}
}
},

Check warning on line 326 in packages/client/src/rpc/validation.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/validation.ts#L314-L326

Added lines #L314 - L326 were not covered by tests

/**
* number validator to check if type is integer
* @param params parameters of method
* @param index index of parameter
*/

Check warning on line 332 in packages/client/src/rpc/validation.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/validation.ts#L328-L332

Added lines #L328 - L332 were not covered by tests
get integer() {
return (params: any[], index: number) => {
if (!Number.isInteger(params[index])) {
Expand Down
Loading
Loading