1
- const BN = require ( 'bn.js' )
2
- const level = require ( 'level-mem' )
3
- const rlp = require ( 'rlp' )
4
- const Block = require ( 'ethereumjs-block' )
5
- const Cache = require ( './cache' )
6
- const {
1
+ import * as rlp from 'rlp'
2
+ import Cache from './cache'
3
+ import {
7
4
headsKey ,
8
5
headHeaderKey ,
9
6
headBlockKey ,
10
7
hashToNumberKey ,
11
8
numberToHashKey ,
12
9
tdKey ,
13
10
bodyKey ,
14
- headerKey
15
- } = require ( './util' )
11
+ headerKey ,
12
+ } from './util'
13
+
14
+ import BN = require( 'bn.js' )
15
+
16
+ const level = require ( 'level-mem' )
17
+ const Block = require ( 'ethereumjs-block' )
16
18
17
19
/**
18
- * Abstraction over db to facilitate storing/fetching blockchain-related
20
+ * Abstraction over a DB to facilitate storing/fetching blockchain-related
19
21
* data, such as blocks and headers, indices, and the head block.
20
22
*/
21
- module . exports = class DBManager {
22
- constructor ( db , common ) {
23
+ export default class DBManager {
24
+ _db : any
25
+
26
+ _common : any
27
+
28
+ _cache : { [ k : string ] : Cache < Buffer > }
29
+
30
+ constructor ( db : any , common : any ) {
23
31
this . _db = db
24
32
this . _common = common
25
33
this . _cache = {
@@ -33,23 +41,20 @@ module.exports = class DBManager {
33
41
34
42
/**
35
43
* Fetches iterator heads from the db.
36
- * @returns Promise
37
44
*/
38
45
getHeads ( ) {
39
46
return this . get ( headsKey , { valueEncoding : 'json' } )
40
47
}
41
48
42
49
/**
43
50
* Fetches header of the head block.
44
- * @returns Promise
45
51
*/
46
52
getHeadHeader ( ) {
47
53
return this . get ( headHeaderKey )
48
54
}
49
55
50
56
/**
51
57
* Fetches head block.
52
- * @returns Promise
53
58
*/
54
59
getHeadBlock ( ) {
55
60
return this . get ( headBlockKey )
@@ -58,12 +63,10 @@ module.exports = class DBManager {
58
63
/**
59
64
* Fetches a block (header and body), given a block tag
60
65
* which can be either its hash or its number.
61
- * @param {Buffer|BN|number } blockTag - Hash or number of the block
62
- * @returns Promise
63
66
*/
64
- async getBlock ( blockTag ) {
67
+ async getBlock ( blockTag : Buffer | BN | number ) {
65
68
// determine BlockTag type
66
- if ( Number . isInteger ( blockTag ) ) {
69
+ if ( typeof blockTag === 'number' && Number . isInteger ( blockTag ) ) {
67
70
blockTag = new BN ( blockTag )
68
71
}
69
72
@@ -92,55 +95,42 @@ module.exports = class DBManager {
92
95
93
96
/**
94
97
* Fetches body of a block given its hash and number.
95
- * @param {Buffer } hash
96
- * @param {BN } number
97
- * @returns Promise
98
98
*/
99
- async getBody ( hash , number ) {
99
+ async getBody ( hash : Buffer , number : BN ) {
100
100
const key = bodyKey ( number , hash )
101
101
return rlp . decode ( await this . get ( key , { cache : 'body' } ) )
102
102
}
103
103
104
104
/**
105
105
* Fetches header of a block given its hash and number.
106
- * @param {Buffer } hash
107
- * @param {BN } number
108
- * @returns Promise
109
106
*/
110
- async getHeader ( hash , number ) {
107
+ async getHeader ( hash : Buffer , number : BN ) {
111
108
const key = headerKey ( number , hash )
112
- let encodedHeader = await this . get ( key , { cache : 'header' } )
109
+ const encodedHeader = await this . get ( key , { cache : 'header' } )
113
110
return new Block . Header ( rlp . decode ( encodedHeader ) , { common : this . _common } )
114
111
}
115
112
116
113
/**
117
114
* Fetches total difficulty for a block given its hash and number.
118
- * @param {Buffer } hash
119
- * @param {BN } number
120
- * @returns Promise
121
115
*/
122
- async getTd ( hash , number ) {
116
+ async getTd ( hash : Buffer , number : BN ) {
123
117
const key = tdKey ( number , hash )
124
118
const td = await this . get ( key , { cache : 'td' } )
125
119
return new BN ( rlp . decode ( td ) )
126
120
}
127
121
128
122
/**
129
123
* Performs a block hash to block number lookup.
130
- * @param {Buffer } hash
131
- * @returns Promise
132
124
*/
133
- async hashToNumber ( hash ) {
125
+ async hashToNumber ( hash : Buffer ) {
134
126
const key = hashToNumberKey ( hash )
135
127
return new BN ( await this . get ( key , { cache : 'hashToNumber' } ) )
136
128
}
137
129
138
130
/**
139
131
* Performs a block number to block hash lookup.
140
- * @param {BN } number
141
- * @returns Promise
142
132
*/
143
- async numberToHash ( number ) {
133
+ async numberToHash ( number : BN ) {
144
134
if ( number . ltn ( 0 ) ) {
145
135
throw new level . errors . NotFoundError ( )
146
136
}
@@ -153,14 +143,8 @@ module.exports = class DBManager {
153
143
* Fetches a key from the db. If `opts.cache` is specified
154
144
* it first tries to load from cache, and on cache miss will
155
145
* try to put the fetched item on cache afterwards.
156
- * @param {Buffer } key
157
- * @param {Object } opts - Options and their default values are:
158
- * - {string} [keyEncoding='binary']
159
- * - {string} [valueEncodingr='binary']
160
- * - {string} [cache=undefined] name of cache to use
161
- * @returns Promise
162
146
*/
163
- async get ( key , opts = { } ) {
147
+ async get ( key : string | Buffer , opts : any = { } ) {
164
148
const dbOpts = {
165
149
keyEncoding : opts . keyEncoding || 'binary' ,
166
150
valueEncoding : opts . valueEncoding || 'binary'
@@ -173,7 +157,7 @@ module.exports = class DBManager {
173
157
174
158
let value = this . _cache [ opts . cache ] . get ( key )
175
159
if ( ! value ) {
176
- value = await this . _db . get ( key , dbOpts )
160
+ value = < Buffer > await this . _db . get ( key , dbOpts )
177
161
this . _cache [ opts . cache ] . set ( key , value )
178
162
}
179
163
@@ -185,10 +169,8 @@ module.exports = class DBManager {
185
169
186
170
/**
187
171
* Performs a batch operation on db.
188
- * @param {Array } ops
189
- * @returns Promise
190
172
*/
191
- batch ( ops ) {
173
+ batch ( ops : Array < any > ) {
192
174
return this . _db . batch ( ops )
193
175
}
194
176
}
0 commit comments