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