Skip to content

Commit 72cc8eb

Browse files
jochem-brouwerholgerd77
authored andcommitted
client: fetch: fix Peer type in Job
client: remove any types from Sync client: mark Sync.ts as abstract class
1 parent d0b2d9a commit 72cc8eb

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Peer } from "@ethereumjs/devp2p"
1+
import { Peer } from '../../net/peer'
22

33
export type Job<JobTask, JobResult, StorageItem> = {
4-
task: JobTask
5-
time: number
6-
index: number
7-
result?: JobResult | StorageItem[]
8-
state: 'idle' | 'expired' | 'active'
9-
peer: Peer | null
10-
}
4+
task: JobTask
5+
time: number
6+
index: number
7+
result?: JobResult | StorageItem[]
8+
state: 'idle' | 'expired' | 'active'
9+
peer: Peer | null
10+
}

packages/client/lib/sync/fullsync.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Peer } from '../net/peer/peer'
44
import { short } from '../util'
55
import { Synchronizer, SynchronizerOptions } from './sync'
66
import { BlockFetcher } from './fetcher'
7+
import { Block } from '@ethereumjs/block'
78

89
/**
910
* Implements an ethereum full sync synchronizer
@@ -58,7 +59,7 @@ export class FullSynchronizer extends Synchronizer {
5859
if (peer.eth?.status) {
5960
const td = peer.eth.status.td
6061
if (
61-
(!best && td.gte((this.chain.blocks as any).td)) ||
62+
(!best && td.gte(this.chain.blocks.td)) ||
6263
(best && best.eth && best.eth.status.td.lt(td))
6364
) {
6465
best = peer
@@ -110,7 +111,7 @@ export class FullSynchronizer extends Synchronizer {
110111
.on('error', (error: Error) => {
111112
this.emit('error', error)
112113
})
113-
.on('fetched', (blocks: any[]) => {
114+
.on('fetched', (blocks: Block[]) => {
114115
const first = new BN(blocks[0].header.number)
115116
const hash = short(blocks[0].hash())
116117
this.config.logger.info(
@@ -155,9 +156,9 @@ export class FullSynchronizer extends Synchronizer {
155156
async open(): Promise<void> {
156157
await this.chain.open()
157158
await this.pool.open()
158-
const number = ((this.chain.blocks as any).height as number).toString(10)
159-
const td = ((this.chain.blocks as any).td as number).toString(10)
160-
const hash = ((this.chain.blocks as any).latest as any).hash()
159+
const number = this.chain.blocks.height.toString(10)
160+
const td = this.chain.blocks.td.toString(10)
161+
const hash = this.chain.blocks.latest!.hash()
161162
this.config.logger.info(`Latest local block: number=${number} td=${td} hash=${short(hash)}`)
162163
}
163164

packages/client/lib/sync/lightsync.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Synchronizer, SynchronizerOptions } from './sync'
33
import { HeaderFetcher } from './fetcher/headerfetcher'
44
import { BN } from 'ethereumjs-util'
55
import { short } from '../util'
6+
import { BlockHeader } from '@ethereumjs/block'
67

78
/**
89
* Implements an ethereum light sync synchronizer
@@ -44,7 +45,7 @@ export class LightSynchronizer extends Synchronizer {
4445
if (peer.les) {
4546
const td = peer.les.status.headTd
4647
if (
47-
(!best && td.gte((this.chain.headers as any).td)) ||
48+
(!best && td.gte(this.chain.headers.td)) ||
4849
(best && best.les && best.les.status.headTd.lt(td))
4950
) {
5051
best = peer
@@ -62,7 +63,7 @@ export class LightSynchronizer extends Synchronizer {
6263
async syncWithPeer(peer?: Peer): Promise<boolean> {
6364
if (!peer) return false
6465
const height = new BN(peer.les!.status.headNum)
65-
const first = ((this.chain.headers as any).height as BN).addn(1)
66+
const first = this.chain.headers.height.addn(1)
6667
const count = height.sub(first).addn(1)
6768
if (count.lten(0)) return false
6869

@@ -83,7 +84,7 @@ export class LightSynchronizer extends Synchronizer {
8384
.on('error', (error: Error) => {
8485
this.emit('error', error)
8586
})
86-
.on('fetched', (headers: any[]) => {
87+
.on('fetched', (headers: BlockHeader[]) => {
8788
const first = new BN(headers[0].number)
8889
const hash = short(headers[0].hash())
8990
this.config.logger.info(
@@ -114,9 +115,9 @@ export class LightSynchronizer extends Synchronizer {
114115
async open(): Promise<void> {
115116
await this.chain.open()
116117
await this.pool.open()
117-
const number = ((this.chain.headers as any).height as number).toString(10)
118-
const td = ((this.chain.headers as any).td as number).toString(10)
119-
const hash = ((this.chain.blocks as any).latest as any).hash()
118+
const number = this.chain.headers.height.toString(10)
119+
const td = this.chain.headers.td.toString(10)
120+
const hash = this.chain.blocks.latest!.hash()
120121
this.config.logger.info(`Latest local header: number=${number} td=${td} hash=${short(hash)}`)
121122
}
122123

packages/client/lib/sync/sync.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface SynchronizerOptions {
2626
* Base class for blockchain synchronizers
2727
* @memberof module:sync
2828
*/
29-
export class Synchronizer extends EventEmitter {
29+
export abstract class Synchronizer extends EventEmitter {
3030
public config: Config
3131

3232
protected pool: PeerPool
@@ -59,6 +59,8 @@ export class Synchronizer extends EventEmitter {
5959
})
6060
}
6161

62+
abstract async sync(): Promise<boolean>
63+
6264
/**
6365
* Returns synchronizer type
6466
*/
@@ -77,7 +79,7 @@ export class Synchronizer extends EventEmitter {
7779
* @return {boolean}
7880
*/
7981
// TODO: evaluate syncability of peer
80-
syncable(_peer: any): boolean {
82+
syncable(_peer: Peer): boolean {
8183
return true
8284
}
8385

@@ -94,7 +96,7 @@ export class Synchronizer extends EventEmitter {
9496
}, this.interval * 30)
9597
while (this.running) {
9698
try {
97-
if (await (this as any).sync()) this.emit('synchronized')
99+
if (await this.sync()) this.emit('synchronized')
98100
} catch (error) {
99101
this.emit('error', error)
100102
}

packages/client/lib/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Peer } from './net/peer'
2-
31
export type Key = Buffer
42
export type KeyLike = string | Key
53

@@ -33,4 +31,4 @@ export interface QHeap<T> {
3331
peek(): T | undefined
3432
length: number
3533
gc(opts: { minLength: number; maxLength: number }): void
36-
}
34+
}

0 commit comments

Comments
 (0)