1
- import { Fetcher , FetcherOptions } from './fetcher'
2
1
import { Block , BlockBodyBuffer } from '@ethereumjs/block'
3
- import { BN } from 'ethereumjs-util'
4
2
import { Peer } from '../../net/peer'
5
3
import { EthProtocolMethods } from '../../net/protocol'
6
- import { Chain } from '../../blockchain'
7
-
8
- export interface BlockFetcherOptions extends FetcherOptions {
9
- /* Blockchain */
10
- chain : Chain
11
-
12
- /* Block number to start fetching from */
13
- first : BN
14
-
15
- /* How many blocks to fetch */
16
- count : BN
17
- }
4
+ import { Job } from './types'
5
+ import { BlockFetcherBase , JobTask , BlockFetcherOptions } from './blockfetcherbase'
18
6
19
7
/**
20
8
* Implements an eth/62 based block fetcher
21
9
* @memberof module:sync/fetcher
22
10
*/
23
- export class BlockFetcher extends Fetcher {
24
- protected chain : Chain
25
- protected first : BN
26
- protected count : BN
27
-
11
+ export class BlockFetcher extends BlockFetcherBase < Block [ ] , Block > {
28
12
/**
29
13
* Create new block fetcher
30
14
* @param {BlockFetcherOptions }
31
15
*/
32
16
constructor ( options : BlockFetcherOptions ) {
33
17
super ( options )
34
-
35
- this . chain = options . chain
36
- this . maxPerRequest = options . maxPerRequest ?? 128
37
- this . first = options . first
38
- this . count = options . count
39
18
}
40
19
41
20
/**
42
21
* Generate list of tasks to fetch
43
22
* @return {Object[] } tasks
44
23
*/
45
- tasks ( ) : object [ ] {
24
+ tasks ( ) : JobTask [ ] {
46
25
const { first, count } = this
47
26
const max = this . maxPerRequest
48
- const tasks = [ ]
27
+ const tasks : JobTask [ ] = [ ]
49
28
while ( count . gten ( max ) ) {
50
29
tasks . push ( { first : first . clone ( ) , count : max } )
51
30
first . iaddn ( max )
@@ -61,18 +40,20 @@ export class BlockFetcher extends Fetcher {
61
40
* Requests blocks associated with this job
62
41
* @param job
63
42
*/
64
- async request ( job : any ) : Promise < any > {
43
+ async request ( job : Job < JobTask , Block [ ] , Block > ) : Promise < Block [ ] > {
65
44
const { task, peer } = job
66
45
const { first, count } = task
67
- const headers = await ( peer . eth as EthProtocolMethods ) . getBlockHeaders ( {
46
+ const headers = await ( peer ! . eth as EthProtocolMethods ) . getBlockHeaders ( {
68
47
block : first ,
69
48
max : count ,
70
49
} )
71
- const bodies = await peer . eth . getBlockBodies ( headers . map ( ( h ) => h . hash ( ) ) )
72
- const blocks = bodies . map ( ( [ txsData , unclesData ] : BlockBodyBuffer , i : number ) =>
50
+ const bodies : BlockBodyBuffer [ ] = < BlockBodyBuffer [ ] > (
51
+ await peer ! . eth ! . getBlockBodies ( headers . map ( ( h ) => h . hash ( ) ) )
52
+ )
53
+ const blocks : Block [ ] = bodies . map ( ( [ txsData , unclesData ] : BlockBodyBuffer , i : number ) =>
73
54
Block . fromValuesArray ( [ headers [ i ] . raw ( ) , txsData , unclesData ] , { common : this . config . common } )
74
55
)
75
- return { blocks }
56
+ return blocks
76
57
}
77
58
78
59
/**
@@ -81,18 +62,20 @@ export class BlockFetcher extends Fetcher {
81
62
* @param result fetch result
82
63
* @return {* } results of processing job or undefined if job not finished
83
64
*/
84
- process ( job : any , result : any ) {
85
- if ( result . blocks && result . blocks . length === job . task . count ) {
86
- return result . blocks
65
+ process ( job : Job < JobTask , Block [ ] , Block > , result : Block [ ] ) {
66
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
67
+ if ( result && result . length === job . task . count ) {
68
+ return result
87
69
}
70
+ return
88
71
}
89
72
90
73
/**
91
74
* Store fetch result. Resolves once store operation is complete.
92
75
* @param {Block[] } blocks fetch result
93
76
* @return {Promise }
94
77
*/
95
- async store ( blocks : Array < any > ) {
78
+ async store ( blocks : Block [ ] ) {
96
79
await this . chain . putBlocks ( blocks )
97
80
}
98
81
@@ -102,7 +85,7 @@ export class BlockFetcher extends Fetcher {
102
85
* @return {Peer }
103
86
*/
104
87
// TODO: find out what _job is supposed to be doing here...
105
- peer ( _job : any ) : Peer {
88
+ peer ( ) : Peer {
106
89
return this . pool . idle ( ( p : any ) => p . eth )
107
90
}
108
91
}
0 commit comments