Skip to content

Commit 457863c

Browse files
acolytec3g11tech
andauthored
Fix client integration tests (ethereumjs#1956)
* Re-enable integration tests * Remove hardcoded difficulty in mockchain blocks * Check for subchain before examining bounds * Copy common to avoid max listener warnings * make destroyServer async * Proper fix for setting difficulty in mockchain * reqId fix * remove the redundant while loop * wait for setTineout to clear out Co-authored-by: harkamal <[email protected]>
1 parent a4c379a commit 457863c

File tree

8 files changed

+28
-18
lines changed

8 files changed

+28
-18
lines changed

.github/workflows/client-build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ jobs:
4545
flags: client
4646
if: ${{ matrix.node-version == 16 }}
4747

48+
- run: npm run test:integration
49+
4850
- run: npm run lint
4951
test-client-cli:
5052
runs-on: ubuntu-latest

packages/client/lib/net/protocol/ethprotocol.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ export interface EthProtocolMethods {
7575
getReceipts: (opts: GetReceiptsOpts) => Promise<[bigint, TxReceipt[]]>
7676
}
7777

78-
let id = BigInt(0)
79-
8078
/**
8179
* Implements eth/66 protocol
8280
* @memberof module:net/protocol
@@ -119,7 +117,7 @@ export class EthProtocol extends Protocol {
119117
code: 0x03,
120118
response: 0x04,
121119
encode: ({ reqId, block, max, skip = 0, reverse = false }: GetBlockHeadersOpts) => [
122-
bigIntToBuffer(reqId ?? ++id),
120+
bigIntToBuffer(reqId ?? ++this.nextReqId),
123121
[
124122
typeof block === 'bigint' ? bigIntToBuffer(block) : block,
125123
max === 0 ? Buffer.from([]) : intToBuffer(max),
@@ -160,7 +158,10 @@ export class EthProtocol extends Protocol {
160158
name: 'GetBlockBodies',
161159
code: 0x05,
162160
response: 0x06,
163-
encode: ({ reqId, hashes }: GetBlockBodiesOpts) => [bigIntToBuffer(reqId ?? ++id), hashes],
161+
encode: ({ reqId, hashes }: GetBlockBodiesOpts) => [
162+
bigIntToBuffer(reqId ?? ++this.nextReqId),
163+
hashes,
164+
],
164165
decode: ([reqId, hashes]: [Buffer, Buffer[]]) => ({
165166
reqId: bufferToBigInt(reqId),
166167
hashes,
@@ -196,7 +197,7 @@ export class EthProtocol extends Protocol {
196197
code: 0x09,
197198
response: 0x0a,
198199
encode: ({ reqId, hashes }: GetPooledTransactionsOpts) => [
199-
bigIntToBuffer(reqId ?? ++id),
200+
bigIntToBuffer(reqId ?? ++this.nextReqId),
200201
hashes,
201202
],
202203
decode: ([reqId, hashes]: [Buffer, Buffer[]]) => ({
@@ -231,7 +232,7 @@ export class EthProtocol extends Protocol {
231232
code: 0x0f,
232233
response: 0x10,
233234
encode: ({ reqId, hashes }: { reqId: bigint; hashes: Buffer[] }) => [
234-
bigIntToBuffer(reqId ?? ++id),
235+
bigIntToBuffer(reqId ?? ++this.nextReqId),
235236
hashes,
236237
],
237238
decode: ([reqId, hashes]: [Buffer, Buffer[]]) => ({

packages/client/lib/net/protocol/lesprotocol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ export interface LesProtocolMethods {
3434
) => Promise<{ reqId: bigint; bv: bigint; headers: BlockHeader[] }>
3535
}
3636

37-
let id = BigInt(0)
38-
3937
/**
4038
* Implements les/1 and les/2 protocols
4139
* @memberof module:net/protocol
@@ -44,7 +42,9 @@ export class LesProtocol extends Protocol {
4442
private chain: Chain
4543
private flow: FlowControl | undefined
4644
private isServer: boolean
45+
private nextReqId = BigInt(0)
4746

47+
/* eslint-disable no-invalid-this */
4848
private protocolMessages: Message[] = [
4949
{
5050
name: 'Announce',
@@ -69,7 +69,7 @@ export class LesProtocol extends Protocol {
6969
code: 0x02,
7070
response: 0x03,
7171
encode: ({ reqId, block, max, skip = 0, reverse = false }: GetBlockHeadersOpts) => [
72-
bigIntToBuffer(reqId ?? ++id),
72+
bigIntToBuffer(reqId ?? ++this.nextReqId),
7373
[typeof block === 'bigint' ? bigIntToBuffer(block) : block, max, skip, !reverse ? 0 : 1],
7474
],
7575
decode: ([reqId, [block, max, skip, reverse]]: any) => ({

packages/client/lib/sync/beaconsync.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ export class BeaconSynchronizer extends Synchronizer {
249249
// Execute single block when within 50 blocks of head,
250250
// otherwise run execution in batch of 50 blocks when filling canonical chain.
251251
if (
252-
this.chain.blocks.height > this.skeleton.bounds().head - BigInt(50) ||
252+
(this.skeleton.bounds() &&
253+
this.chain.blocks.height > this.skeleton.bounds().head - BigInt(50)) ||
253254
this.chain.blocks.height % BigInt(50) === BigInt(0)
254255
) {
255256
void this.execution.run(false)

packages/client/test/integration/mocks/mockchain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Block } from '@ethereumjs/block'
2+
import { Hardfork } from '@ethereumjs/common'
23
import { Chain, ChainOptions } from '../../../lib/blockchain'
34

45
interface MockChainOptions extends ChainOptions {
@@ -29,7 +30,7 @@ export default class MockChain extends Chain {
2930
{
3031
header: {
3132
number: number + 1,
32-
difficulty: 1,
33+
difficulty: common.gteHardfork(Hardfork.Merge) ? 0 : 1,
3334
parentHash: number ? blocks[number - 1].hash() : this.genesis.hash(),
3435
},
3536
},

packages/client/test/integration/mocks/mockserver.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Server, ServerOptions } from '../../../lib/net/server'
22
import { Event } from '../../../lib/types'
33
import MockPeer from './mockpeer'
4-
import { RemoteStream, createServer, destroyServer } from './network'
4+
import { RemoteStream, createServer, destroyServer, servers } from './network'
55

66
interface MockServerOptions extends ServerOptions {
77
location?: string
@@ -42,8 +42,12 @@ export default class MockServer extends Server {
4242
}
4343

4444
async stop(): Promise<boolean> {
45+
// This wait is essential to clear out the pending setTimeout in the
46+
// createStream in ./network.ts
4547
await this.wait(20)
46-
destroyServer(this.location)
48+
while (servers[this.location]) {
49+
await destroyServer(this.location)
50+
}
4751
await super.stop()
4852
return this.started
4953
}

packages/client/test/integration/mocks/network.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ export function destroyStream(id: string, location: string) {
4545
}
4646
}
4747

48-
export function destroyServer(location: string) {
49-
if (servers[location]) {
48+
export async function destroyServer(location: string) {
49+
return new Promise<void>((resolve) => {
5050
for (const id of Object.keys(servers[location].streams)) {
5151
destroyStream(id, location)
5252
}
53-
}
54-
delete servers[location]
53+
delete servers[location]
54+
resolve()
55+
})
5556
}
5657

5758
export function createStream(id: string, location: string, protocols: string[]) {

packages/client/test/integration/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function setup(
2323
const minPeers = options.minPeers ?? 1
2424

2525
const lightserv = syncmode === 'full'
26-
const common = options.common
26+
const common = options.common?.copy()
2727
const config = new Config({ syncmode, lightserv, minPeers, common, safeReorgDistance: 0 })
2828

2929
const server = new MockServer({ config, location })

0 commit comments

Comments
 (0)