Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

Fix remaining skipped tests #182

Merged
merged 1 commit into from
Nov 20, 2020
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
18 changes: 14 additions & 4 deletions lib/blockchain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,23 @@ export class Chain extends EventEmitter {
if (!this.opened) {
return false
}
const headers: any = {}
const blocks: any = {}

const headers: ChainHeaders = {
latest: null,
td: new BN(0),
height: new BN(0),
}
const blocks: ChainBlocks = {
latest: null,
td: new BN(0),
height: new BN(0),
}

headers.latest = await this.getLatestHeader()
blocks.latest = await this.getLatestBlock()

headers.height = new BN(headers.latest.number)
blocks.height = new BN(blocks.latest.header.number)
headers.height = headers.latest.number
blocks.height = blocks.latest.header.number

headers.td = await this.getTd(headers.latest.hash(), headers.height)
blocks.td = await this.getTd(blocks.latest.hash(), blocks.height)
Expand Down
2 changes: 1 addition & 1 deletion lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface ConfigOptions {
/**
* Logging verbosity
*
* Choices: ['error', 'warn', 'info', 'debug']
* Choices: ['debug', 'info', 'warn', 'error', 'off']
* Default: 'info'
*/
loglevel?: string
Expand Down
1 change: 1 addition & 0 deletions lib/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function getLogger(options = { loglevel: 'info' }) {
logFormat()
),
level: options.loglevel,
silent: options.loglevel === 'off',
transports: [new transports.Console()],
exceptionHandlers: [new transports.Console()],
})
Expand Down
8 changes: 4 additions & 4 deletions lib/net/peer/libp2pnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const LibP2p = require('libp2p')
const promisify = require('util-promisify')

export class Libp2pNode extends LibP2p {
public asyncStart: any
public asyncStop: any
public asyncDial: any
public asyncDialProtocol: any
public asyncStart: Function
public asyncStop: Function
public asyncDial: Function
public asyncDialProtocol: Function

constructor(options: any) {
super({
Expand Down
2 changes: 1 addition & 1 deletion lib/net/peer/rlpxpeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class RlpxPeer extends Peer {
return
}
const key = randomBytes(32)
await Promise.all(this.protocols.map((p: any) => p.open()))
await Promise.all(this.protocols.map((p) => p.open()))
this.rlpx = new Devp2pRLPx(key, {
capabilities: RlpxPeer.capabilities(this.protocols),
listenPort: null,
Expand Down
15 changes: 6 additions & 9 deletions lib/net/peerpool.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { EventEmitter } from 'events'
import { Config } from '../config'
import { Peer } from './peer/peer'
import { Server } from './server'
import { RlpxServer } from './server'

export interface PeerPoolOptions {
/* Config */
config: Config

/* Servers to aggregate peers from */
servers?: Server[]
}

/**
Expand Down Expand Up @@ -108,10 +105,10 @@ export class PeerPool extends EventEmitter {
* @param peer object or peer id
*/
contains(peer: Peer | string): boolean {
if (peer instanceof Peer) {
if (typeof peer !== 'string') {
peer = peer.id
}
return !!this.pool.get(peer as string)
return !!this.pool.get(peer)
}

/**
Expand All @@ -120,7 +117,7 @@ export class PeerPool extends EventEmitter {
* @return {Peer}
*/
idle(filterFn = (_peer: Peer) => true): Peer {
const idle = this.peers.filter((p: any) => p.idle && filterFn(p))
const idle = this.peers.filter((p) => p.idle && filterFn(p))
const index = Math.floor(Math.random() * idle.length)
return idle[index]
}
Expand Down Expand Up @@ -206,8 +203,8 @@ export class PeerPool extends EventEmitter {
if (this.size === 0) {
this.noPeerPeriods += 1
if (this.noPeerPeriods >= 3) {
const promises = this.config.servers.map(async (server: any) => {
if (server.bootstrap) {
const promises = this.config.servers.map(async (server) => {
if (server instanceof RlpxServer) {
this.config.logger.info('Retriggering bootstrap.')
await server.bootstrap()
}
Expand Down
4 changes: 2 additions & 2 deletions lib/net/protocol/lesprotocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ export class LesProtocol extends Protocol {
headNum: new BN(status.headNum),
genesisHash: status.genesisHash,
serveHeaders: this.isServer,
serveChainSince: status.serveChainSince ? new BN(status.serveChainSince) : undefined,
serveStateSince: status.serveStateSince ? new BN(status.serveStateSince) : undefined,
serveChainSince: status.serveChainSince ?? 0,
serveStateSince: status.serveStateSince ?? 0,
txRelay: !!status.txRelay,
bl: status['flowControl/BL'] ? new BN(status['flowControl/BL']).toNumber() : undefined,
mrr: status['flowControl/MRR'] ? new BN(status['flowControl/MRR']).toNumber() : undefined,
Expand Down
23 changes: 5 additions & 18 deletions lib/net/server/libp2pserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ export class Libp2pServer extends Server {
peerInfo: await this.createPeerInfo(),
bootnodes: this.bootnodes,
})
this.protocols.forEach(async (p: any) => {
//@ts-ignore
const protocol: any = `/${p.name}/${p.versions[0]}`
this.protocols.forEach(async (p) => {
const protocol = `/${p.name}/${p.versions[0]}`
this.node!.handle(protocol, async (_: any, connection: any) => {
try {
const peerInfo = await this.getPeerInfo(connection)
Expand All @@ -79,7 +78,6 @@ export class Libp2pServer extends Server {
})
})
}
// eslint-disable-next-line no-extra-semi
this.node.on('peer:discovery', async (peerInfo: any) => {
try {
const id = peerInfo.id.toB58String()
Expand All @@ -102,12 +100,7 @@ export class Libp2pServer extends Server {
this.error(e)
}
})
await new Promise((resolve, reject) =>
this.node!.start((err: any) => {
if (err) reject(err)
resolve()
})
)
await this.node.asyncStart()
this.node.peerInfo.multiaddrs.toArray().map((ma: any) => {
this.emit('listening', {
transport: this.name,
Expand All @@ -123,12 +116,7 @@ export class Libp2pServer extends Server {
*/
async stop(): Promise<boolean> {
if (this.started) {
await new Promise((resolve, reject) =>
this.node!.stop((err: any) => {
if (err) reject(err)
resolve()
})
)
await this.node!.asyncStop()
await super.stop()
this.started = false
}
Expand Down Expand Up @@ -178,8 +166,7 @@ export class Libp2pServer extends Server {
if (err) {
return reject(err)
}
// eslint-disable-next-line no-extra-semi
this.multiaddrs.forEach((ma: any) => peerInfo.multiaddrs.add(ma))
this.multiaddrs.forEach((ma) => peerInfo.multiaddrs.add(ma))
resolve(peerInfo)
}
if (this.key) {
Expand Down
4 changes: 2 additions & 2 deletions lib/net/server/rlpxserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class RlpxServer extends Server {
*/
async stop(): Promise<boolean> {
if (this.started) {
this.rlpx!.destroy() // eslint-disable-line no-extra-semi
this.rlpx!.destroy()
this.dpt!.destroy()
await super.stop()
this.started = false
Expand All @@ -158,7 +158,7 @@ export class RlpxServer extends Server {
if (!this.started) {
return false
}
this.dpt!.banPeer(peerId, maxAge) // eslint-disable-line no-extra-semi
this.dpt!.banPeer(peerId, maxAge)
return true
}

Expand Down
8 changes: 1 addition & 7 deletions lib/sync/fetcher/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,7 @@ export class Fetcher extends Readable {

/**
* Create new fetcher
* @param {Object} options constructor parameters
* @param {PeerPool} options.pool peer pool
* @param {number} [options.timeout] fetch task timeout
* @param {number} [options.banTime] how long to ban misbehaving peers
* @param {number} [options.maxQueue] max write queue size
* @param {number} [options.maxPerRequest=128] max items per request
* @param {number} [options.interval] retry interval
* @param {FetcherOptions}
*/
constructor(options: FetcherOptions) {
super({ ...options, objectMode: true })
Expand Down
2 changes: 1 addition & 1 deletion lib/sync/lightsync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Peer } from '../net/peer/peer'
import { BoundProtocol } from '../net/protocol'
import { Synchronizer, SynchronizerOptions } from './sync'
import { HeaderFetcher } from './fetcher'
import { HeaderFetcher } from './fetcher/headerfetcher'
import { BN } from 'ethereumjs-util'
import { short } from '../util'

Expand Down
2 changes: 1 addition & 1 deletion lib/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class Synchronizer extends EventEmitter {
}

/**
* Stop synchronization. Returns a promise that resolves once its stopped.
* Stop synchronization. Returns a promise that resolves once stopped.
*/
async stop(): Promise<boolean> {
if (!this.running) {
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
"build:browser": "tsc -p ./tsconfig.browser.json && npm run bundle && rm -rf dist.browser",
"bundle": "webpack",
"client:start": "tsc -p tsconfig.prod.json && node dist/bin/cli.js",
"coverage": "nyc npm run coverage:test && nyc report --reporter=lcov",
"coverage:test": "tape -r ts-node/register 'test/**/*.spec.ts'",
"coverage": "nyc npm run test && nyc report --reporter=lcov",
"docs:build": "typedoc --tsconfig tsconfig.prod.json",
"lint": "ethereumjs-config-lint",
"lint:fix": "ethereumjs-config-lint-fix",
"tape": "tape -r ts-node/register",
"test": "npm run tape -- 'test/**/*.spec.ts'",
"test": "npm run test:unit && npm run test:integration",
"test:unit": "npm run tape -- 'test/!(integration)/**/*.spec.ts'",
"test:integration": "npm run tape -- 'test/integration/**/*.spec.ts'",
"test:browser": "karma start karma.conf.js"
Expand All @@ -43,8 +42,8 @@
"ethereumjs",
"client",
"blockchain",
"light",
"fast"
"light sync",
"full sync"
],
"engines": {
"node": ">=8.0.0"
Expand Down
3 changes: 2 additions & 1 deletion test/integration/fullethereumservice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ tape('[Integration:FullEthereumService]', async (t) => {
const config = new Config({ loglevel })
const server = new MockServer({ config })
const chain = new MockChain({ config })
const serviceConfig = new Config({ loglevel, servers: [server as any], lightserv: true })
const service = new FullEthereumService({
config: new Config({ loglevel, servers: [server as any], lightserv: true }),
config: serviceConfig,
chain,
})
await service.open()
Expand Down
2 changes: 1 addition & 1 deletion test/integration/lightsync.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tape from 'tape'
import { wait, setup, destroy } from './util'

tape.skip('[Integration:LightSync]', async (t) => {
tape('[Integration:LightSync]', async (t) => {
t.test('should sync headers', async (t) => {
const [remoteServer, remoteService] = await setup({
location: '127.0.0.2',
Expand Down
3 changes: 2 additions & 1 deletion test/integration/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Node from '../../lib/node'
import MockServer from './mocks/mockserver'

tape('[Integration:Node]', (t) => {
const servers = [new MockServer({ config: new Config({ loglevel: 'error' }) }) as any]
const serverConfig = new Config({ loglevel: 'error' })
const servers = [new MockServer({ config: serverConfig }) as any]
const config = new Config({ servers, syncmode: 'full', lightserv: false, loglevel: 'error' })
const node = new Node({ config })

Expand Down
9 changes: 5 additions & 4 deletions test/integration/peerpool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import MockChain from './mocks/mockchain'

tape('[Integration:PeerPool]', async (t) => {
async function setup(protocols: EthProtocol[] = []): Promise<[MockServer, PeerPool]> {
const server = new MockServer({ config: new Config({ loglevel: 'error' }) })
const serverConfig = new Config({ loglevel: 'error' })
const server = new MockServer({ config: serverConfig }) as any
server.addProtocols(protocols)
// @ts-ignore allow Config instantiation with MockServer
const config = new Config({ servers: [server] })
await server.start()
const pool = new PeerPool({ config })
Expand Down Expand Up @@ -56,11 +56,12 @@ tape('[Integration:PeerPool]', async (t) => {
})

t.test('should handle peer messages', async (t) => {
const chain = new MockChain({ config: new Config({ loglevel: 'error' }) })
const config = new Config({ transports: [], loglevel: 'error' })
const chain = new MockChain({ config })
await chain.open()
const protocols = [
new EthProtocol({
config: new Config({ transports: [] }),
config,
chain,
}),
]
Expand Down
14 changes: 11 additions & 3 deletions test/integration/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ interface SetupOptions {
export async function setup(
options: SetupOptions = {}
): Promise<[MockServer, FullEthereumService | LightEthereumService]> {
const loglevel = 'error'
const config = new Config({ loglevel })
const { location, height, interval, syncmode } = options

const loglevel = 'error'
const lightserv = syncmode === 'full'
const config = new Config({ loglevel, syncmode, lightserv })

const server = new MockServer({ config, location })
const chain = new MockChain({ config, height })

const servers = [server] as any
const serviceConfig = new Config({ loglevel, syncmode, servers, lightserv, minPeers: 1 })
const serviceOpts = {
config: new Config({ loglevel, servers: [server as any], minPeers: 1 }),
config: serviceConfig,
chain,
interval: interval ?? 10,
}

const service =
syncmode === 'light'
? new LightEthereumService(serviceOpts)
Expand All @@ -32,6 +39,7 @@ export async function setup(
})
await service.open()
await service.start()

return [server, service]
}

Expand Down
19 changes: 8 additions & 11 deletions test/net/peer/libp2pnode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import tape from 'tape-catch'
const td = require('testdouble')
// const Libp2pNode = require('../../../lib/net/peer/libp2pnode')
import td from 'testdouble'

tape('[Libp2pNode]', (t) => {
// const libp2p = td.replace('libp2p')
tape('[Libp2pNode]', async (t) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const libp2p = td.replace('libp2p')
const { Libp2pNode } = await import('../../../lib/net/peer/libp2pnode')

t.test('should be a libp2p bundle', (t) => {
// TODO: fix this test
// libp2p class members on node are there, but
// instanceof check is not working any more
// after TypeScript transition
// const peerInfo = td.object('PeerInfo')
// const node = new Libp2pNode({ peerInfo })
// t.ok(node instanceof libp2p, 'is libp2p bundle')
const peerInfo = td.object('PeerInfo')
const node = new Libp2pNode({ peerInfo })
t.equals(node.constructor.name, Libp2pNode.name, 'is libp2p bundle')
t.end()
})

Expand Down
Loading