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

devp2p/client: upgrade to eth/66 #1331

Merged
merged 8 commits into from
Jul 8, 2021
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
4 changes: 1 addition & 3 deletions packages/client/lib/net/peer/rlpxpeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import { Peer, PeerOptions } from './peer'
import { RlpxServer } from '../server'

const devp2pCapabilities: any = {
eth63: Devp2pETH.eth63,
eth64: Devp2pETH.eth64,
eth65: Devp2pETH.eth65,
eth66: Devp2pETH.eth66,
les2: Devp2pLES.les2,
les3: Devp2pLES.les3,
les4: Devp2pLES.les4,
Expand Down
58 changes: 43 additions & 15 deletions packages/client/lib/net/protocol/ethprotocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface EthProtocolOptions extends ProtocolOptions {
}

type GetBlockHeadersOpts = {
/* Request id (default: next internal id) */
reqId?: BN
/* The block's number or hash */
block: BN | Buffer
/* Max number of blocks to return */
Expand All @@ -19,17 +21,27 @@ type GetBlockHeadersOpts = {
/* Fetch blocks in reverse (default: false) */
reverse?: boolean
}

type GetBlockBodiesOpts = {
/* Request id (default: next internal id) */
reqId?: BN
/* The block hashes */
hashes: Buffer[]
}

/*
* Messages with responses that are added as
* methods in camelCase to BoundProtocol.
*/
export interface EthProtocolMethods {
getBlockHeaders: (opts: GetBlockHeadersOpts) => Promise<BlockHeader[]>
getBlockBodies: (hashes: Buffer[]) => Promise<BlockBodyBuffer[]>
getBlockHeaders: (opts: GetBlockHeadersOpts) => Promise<[BN, BlockHeader[]]>
getBlockBodies: (opts: GetBlockBodiesOpts) => Promise<[BN, BlockBodyBuffer[]]>
}

const id = new BN(0)

/**
* Implements eth/62 and eth/63 protocols
* Implements eth/66 protocol
* @memberof module:net/protocol
*/
export class EthProtocol extends Protocol {
Expand All @@ -50,13 +62,12 @@ export class EthProtocol extends Protocol {
name: 'GetBlockHeaders',
code: 0x03,
response: 0x04,
encode: ({ block, max, skip = 0, reverse = false }: GetBlockHeadersOpts) => [
BN.isBN(block) ? block.toArrayLike(Buffer) : block,
max,
skip,
!reverse ? 0 : 1,
encode: ({ reqId, block, max, skip = 0, reverse = false }: GetBlockHeadersOpts) => [
(reqId === undefined ? id.iaddn(1) : new BN(reqId)).toArrayLike(Buffer),
[BN.isBN(block) ? block.toArrayLike(Buffer) : block, max, skip, !reverse ? 0 : 1],
],
decode: ([block, max, skip, reverse]: any) => ({
decode: ([reqId, [block, max, skip, reverse]]: any) => ({
reqId: new BN(reqId),
block: block.length === 32 ? block : new BN(block),
max: bufferToInt(max),
skip: bufferToInt(skip),
Expand All @@ -66,24 +77,41 @@ export class EthProtocol extends Protocol {
{
name: 'BlockHeaders',
code: 0x04,
encode: (headers: BlockHeader[]) => headers.map((h) => h.raw()),
decode: (headers: BlockHeaderBuffer[]) => {
return headers.map((h) =>
encode: ({ reqId, headers }: { reqId: BN; headers: BlockHeader[] }) => [
reqId.toNumber(),
headers.map((h) => h.raw()),
],
decode: ([reqId, headers]: [Buffer, BlockHeaderBuffer[]]) => [
new BN(reqId),
headers.map((h) =>
BlockHeader.fromValuesArray(h, {
hardforkByBlockNumber: true,
common: this.config.chainCommon, // eslint-disable-line no-invalid-this
})
)
},
),
],
},
{
name: 'GetBlockBodies',
code: 0x05,
response: 0x06,
encode: ({ reqId, hashes }: GetBlockBodiesOpts) => [
(reqId === undefined ? id.iaddn(1) : new BN(reqId)).toArrayLike(Buffer),
hashes,
],
decode: ([reqId, hashes]: [Buffer, Buffer[]]) => ({
reqId: new BN(reqId),
hashes,
}),
},
{
name: 'BlockBodies',
code: 0x06,
encode: ({ reqId, bodies }: { reqId: BN; bodies: BlockBodyBuffer[] }) => [
reqId.toNumber(),
bodies,
],
decode: ([reqId, bodies]: [Buffer, BlockBodyBuffer[]]) => [new BN(reqId), bodies],
},
]

Expand All @@ -110,7 +138,7 @@ export class EthProtocol extends Protocol {
* @type {number[]}
*/
get versions(): number[] {
return [65, 64, 63]
return [66]
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/client/lib/net/protocol/lesprotocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ export class LesProtocol extends Protocol {
decode: ([reqId, bv, headers]: any) => ({
reqId: new BN(reqId),
bv: new BN(bv),
headers: headers.map((h: BlockHeaderBuffer) => {
return BlockHeader.fromValuesArray(h, {
headers: headers.map((h: BlockHeaderBuffer) =>
BlockHeader.fromValuesArray(h, {
hardforkByBlockNumber: true,
common: this.config.chainCommon, // eslint-disable-line no-invalid-this
})
}),
),
}),
},
]
Expand Down
8 changes: 4 additions & 4 deletions packages/client/lib/service/fullethereumservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ export class FullEthereumService extends EthereumService {
*/
async handleEth(message: any, peer: Peer): Promise<void> {
if (message.name === 'GetBlockHeaders') {
const { block, max, skip, reverse } = message.data
const { reqId, block, max, skip, reverse } = message.data
const headers: any = await this.chain.getHeaders(block, max, skip, reverse)
peer.eth!.send('BlockHeaders', headers)
peer.eth!.send('BlockHeaders', { reqId, headers })
} else if (message.name === 'GetBlockBodies') {
const hashes = message.data
const { reqId, hashes } = message.data
const blocks = await Promise.all(hashes.map((hash: any) => this.chain.getBlock(hash)))
const bodies: any = blocks.map((block: any) => block.raw().slice(1))
peer.eth!.send('BlockBodies', bodies)
peer.eth!.send('BlockBodies', { reqId, bodies })
} else if (message.name === 'NewBlockHashes') {
await this.synchronizer.announced(message.data, peer)
}
Expand Down
30 changes: 25 additions & 5 deletions packages/client/lib/sync/fetcher/blockfetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Job } from './types'
import { BlockFetcherBase, JobTask, BlockFetcherOptions } from './blockfetcherbase'

/**
* Implements an eth/62 based block fetcher
* Implements an eth/66 based block fetcher
* @memberof module:sync/fetcher
*/
export class BlockFetcher extends BlockFetcherBase<Block[], Block> {
Expand All @@ -24,13 +24,33 @@ export class BlockFetcher extends BlockFetcherBase<Block[], Block> {
async request(job: Job<JobTask, Block[], Block>): Promise<Block[]> {
const { task, peer } = job
const { first, count } = task
const headers = await (peer!.eth as EthProtocolMethods).getBlockHeaders({
const headersResult = await (peer!.eth as EthProtocolMethods).getBlockHeaders({
block: first,
max: count,
})
const bodies: BlockBodyBuffer[] = <BlockBodyBuffer[]>(
await peer!.eth!.getBlockBodies(headers.map((h) => h.hash()))
)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!headersResult) {
// Catch occasional null responses from peers who do not return block headers from peer.eth request
this.config.logger.warn(
`peer ${peer?.id} returned no headers for blocks ${first}-${first.addn(count)} from ${
peer?.address
}`
)
return []
}
const headers = headersResult[1]
const bodiesResult = await peer!.eth!.getBlockBodies({ hashes: headers.map((h) => h.hash()) })
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!bodiesResult) {
// Catch occasional null responses from peers who do not return block bodies from peer.eth request
this.config.logger.warn(
`peer ${peer?.id} returned no bodies for blocks ${first}-${first.addn(count)} from ${
peer?.address
}`
)
return []
}
const bodies = bodiesResult[1]
const blocks: Block[] = bodies.map(([txsData, unclesData]: BlockBodyBuffer, i: number) => {
const opts = {
common: this.config.chainCommon,
Expand Down
4 changes: 2 additions & 2 deletions packages/client/lib/sync/fullsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ export class FullSynchronizer extends Synchronizer {
* @return {Promise} Resolves with header
*/
async latest(peer: Peer) {
const headers = await peer.eth?.getBlockHeaders({
const result = await peer.eth?.getBlockHeaders({
block: peer.eth!.status.bestHash,
max: 1,
})
return headers?.[0]
return result ? result[1][0] : undefined
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/client/lib/sync/lightsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ export class LightSynchronizer extends Synchronizer {
* @return {Promise} Resolves with header
*/
async latest(peer: Peer) {
const headers = await peer.eth?.getBlockHeaders({
block: peer.eth!.status.bestHash,
const result = await peer.les?.getBlockHeaders({
block: peer.les!.status.headHash,
max: 1,
})
return headers?.[0]
return result?.headers[0]
}

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/client/test/integration/fullethereumservice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ tape('[Integration:FullEthereumService]', async (t) => {
t.test('should handle ETH requests', async (t) => {
const [server, service] = await setup()
const peer = await server.accept('peer0')
const headers = await peer.eth!.getBlockHeaders({ block: new BN(1), max: 2 })
const [reqId1, headers] = await peer.eth!.getBlockHeaders({ block: new BN(1), max: 2 })
const hash = Buffer.from(
'a321d27cd2743617c1c1b0d7ecb607dd14febcdfca8f01b79c3f0249505ea069',
'hex'
)
t.ok(reqId1.eqn(1), 'handled GetBlockHeaders')
t.ok(headers![1].hash().equals(hash), 'handled GetBlockHeaders')
const bodies = await peer.eth!.getBlockBodies([hash])
const [reqId2, bodies] = await peer.eth!.getBlockBodies({ hashes: [hash] })
t.ok(reqId2.eqn(2), 'handled GetBlockBodies')
t.deepEquals(bodies, [[[], []]], 'handled GetBlockBodies')
peer.eth!.send('NewBlockHashes', [[hash, new BN(2)]])
t.pass('handled NewBlockHashes')
Expand Down
11 changes: 5 additions & 6 deletions packages/client/test/net/peer/rlpxpeer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ tape('[RlpxPeer]', async (t) => {

t.test('should compute capabilities', (t) => {
const protocols: any = [
{ name: 'eth', versions: [63, 64] },
{ name: 'les', versions: [2] },
{ name: 'eth', versions: [66] },
{ name: 'les', versions: [4] },
]
const caps = RlpxPeer.capabilities(protocols).map(({ name, version, length }) => ({
name,
Expand All @@ -39,9 +39,8 @@ tape('[RlpxPeer]', async (t) => {
t.deepEquals(
caps,
[
{ name: 'eth', version: 63, length: 17 },
{ name: 'eth', version: 64, length: 29 },
{ name: 'les', version: 2, length: 21 },
{ name: 'eth', version: 66, length: 29 },
{ name: 'les', version: 4, length: 23 },
],
'correct capabilities'
)
Expand All @@ -50,7 +49,7 @@ tape('[RlpxPeer]', async (t) => {

t.test('should connect to peer', async (t) => {
const config = new Config({ transports: [], loglevel: 'error' })
const proto0 = { name: 'les', versions: [2] } as any
const proto0 = { name: 'les', versions: [4] } as any
const peer = new RlpxPeer({
config,
id: 'abcdef0123',
Expand Down
6 changes: 3 additions & 3 deletions packages/client/test/sync/fullsync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ tape('[FullSynchronizer]', async (t) => {
const chain = new Chain({ config })
const sync = new FullSynchronizer({ config, pool, chain })
const peer = { eth: { getBlockHeaders: td.func(), status: { bestHash: 'hash' } } }
const headers = [{ number: 5 }]
td.when(peer.eth.getBlockHeaders({ block: 'hash', max: 1 })).thenResolve(headers)
const headers = [{ number: new BN(5) }]
td.when(peer.eth.getBlockHeaders({ block: 'hash', max: 1 })).thenResolve([new BN(1), headers])
const latest = await sync.latest(peer as any)
t.equals(new BN(latest!.number).toNumber(), 5, 'got height')
t.ok(latest!.number.eqn(5), 'got height')
await sync.stop()
t.end()
})
Expand Down
Loading