Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.

Commit 569967d

Browse files
committed
Migrate to TypeScript
1 parent 2262597 commit 569967d

File tree

12 files changed

+565
-495
lines changed

12 files changed

+565
-495
lines changed

.babelrc

Lines changed: 0 additions & 4 deletions
This file was deleted.

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
insert_final_newline = true
8+
9+
[*.md]
10+
indent_size = 4

.nycrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@ethereumjs/config-nyc"
3+
}

package.json

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88
],
99
"scripts": {
1010
"prepublishOnly": "npm run lint && npm run test && npm run build",
11-
"build": "babel src --out-dir dist",
11+
"build": "tsc -p ./tsconfig.prod.json",
1212
"coverage": "nyc npm run test && nyc report --reporter=text-lcov > .nyc_output/lcov.info",
1313
"coveralls": "npm run coverage && coveralls <.nyc_output/lcov.info",
14-
"lint": "standard",
15-
"test": "babel-tape-runner ./test/*.js"
14+
"format": "prettier --list-different **/*.{ts,json,md}",
15+
"format:fix": "prettier --write **/*.{ts,json,md}",
16+
"tslint": "tslint -p ./tsconfig.json -e node_modules/**/* -e **/node_modules/**/* -e dist/**/* **/*.ts",
17+
"tslint:fix": "tslint --fix --format stylish -p ./tsconfig.json -e node_modules/**/* -e **/node_modules/**/* -e dist/**/* **/*.ts",
18+
"tsc": "tsc --noEmit",
19+
"lint": "npm run format && npm run tslint && npm run tsc",
20+
"lint:fix": "npm run format:fix && npm run tslint:fix && npm run tsc",
21+
"test": "ts-node node_modules/tape/bin/tape ./test/index.ts"
1622
},
1723
"repository": {
1824
"type": "git",
@@ -29,7 +35,6 @@
2935
},
3036
"homepage": "https://github.com/ethereumjs/ethereumjs-blockchain#readme",
3137
"dependencies": {
32-
"@babel/runtime": "^7.3.1",
3338
"async": "^2.6.1",
3439
"ethashjs": "~0.0.7",
3540
"ethereumjs-block": "~2.2.0",
@@ -38,19 +43,26 @@
3843
"flow-stoplight": "^1.0.0",
3944
"level-mem": "^3.0.1",
4045
"lru-cache": "^5.1.1",
41-
"safe-buffer": "^5.1.2",
46+
"rlp": "^2.2.2",
4247
"semaphore": "^1.1.0",
4348
"util": "^0.11.1"
4449
},
4550
"devDependencies": {
46-
"@babel/cli": "^7.2.3",
47-
"@babel/core": "^7.2.2",
48-
"@babel/plugin-transform-runtime": "^7.2.0",
49-
"@babel/preset-env": "^7.3.1",
50-
"babel-tape-runner": "^3.0.0",
51+
"@ethereumjs/config-nyc": "^1.1.1",
52+
"@ethereumjs/config-tsc": "^1.1.1",
53+
"@ethereumjs/config-tslint": "^1.1.1",
54+
"@types/async": "^2.4.1",
55+
"@types/bn.js": "^4.11.4",
56+
"@types/lru-cache": "^5.1.0",
57+
"@types/node": "^10.12.30",
58+
"@types/semaphore": "^1.1.0",
59+
"@types/tape": "^4.2.33",
5160
"coveralls": "^3.0.2",
5261
"nyc": "^13.0.1",
53-
"standard": "^11.0.1",
54-
"tape": "^4.9.1"
62+
"prettier": "^1.16.4",
63+
"tape": "^4.9.1",
64+
"tslint": "^5.13.1",
65+
"typescript": "^3.3.3333",
66+
"typestrict": "^1.0.2"
5567
}
5668
}

src/cache.js renamed to src/cache.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
const LRU = require('lru-cache')
1+
import * as LRU from 'lru-cache'
22

3-
// Simple LRU Cache that allows for keys of type Buffer
4-
module.exports = class Cache {
5-
constructor (opts) {
3+
/**
4+
* Simple LRU Cache that allows for keys of type Buffer
5+
*/
6+
export default class Cache<V> {
7+
_cache: LRU<string, V>
8+
9+
constructor (opts: LRU.Options<string, V>) {
610
this._cache = new LRU(opts)
711
}
812

9-
set (key, value) {
13+
set (key: string | Buffer, value: V) {
1014
if (key instanceof Buffer) {
1115
key = key.toString('hex')
1216
}
1317
this._cache.set(key, value)
1418
}
1519

16-
get (key) {
20+
get (key: string | Buffer) {
1721
if (key instanceof Buffer) {
1822
key = key.toString('hex')
1923
}
2024
return this._cache.get(key)
2125
}
2226

23-
del (key) {
27+
del (key: string | Buffer) {
2428
if (key instanceof Buffer) {
2529
key = key.toString('hex')
2630
}
Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
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 {
74
headsKey,
85
headHeaderKey,
96
headBlockKey,
107
hashToNumberKey,
118
numberToHashKey,
129
tdKey,
1310
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')
1618

1719
/**
18-
* Abstraction over db to facilitate storing/fetching blockchain-related
20+
* Abstraction over a DB to facilitate storing/fetching blockchain-related
1921
* data, such as blocks and headers, indices, and the head block.
2022
*/
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) {
2331
this._db = db
2432
this._common = common
2533
this._cache = {
@@ -33,23 +41,20 @@ module.exports = class DBManager {
3341

3442
/**
3543
* Fetches iterator heads from the db.
36-
* @returns Promise
3744
*/
3845
getHeads () {
3946
return this.get(headsKey, { valueEncoding: 'json' })
4047
}
4148

4249
/**
4350
* Fetches header of the head block.
44-
* @returns Promise
4551
*/
4652
getHeadHeader () {
4753
return this.get(headHeaderKey)
4854
}
4955

5056
/**
5157
* Fetches head block.
52-
* @returns Promise
5358
*/
5459
getHeadBlock () {
5560
return this.get(headBlockKey)
@@ -58,12 +63,10 @@ module.exports = class DBManager {
5863
/**
5964
* Fetches a block (header and body), given a block tag
6065
* which can be either its hash or its number.
61-
* @param {Buffer|BN|number} blockTag - Hash or number of the block
62-
* @returns Promise
6366
*/
64-
async getBlock (blockTag) {
67+
async getBlock (blockTag: Buffer | BN | number) {
6568
// determine BlockTag type
66-
if (Number.isInteger(blockTag)) {
69+
if (typeof blockTag === 'number' && Number.isInteger(blockTag)) {
6770
blockTag = new BN(blockTag)
6871
}
6972

@@ -92,55 +95,42 @@ module.exports = class DBManager {
9295

9396
/**
9497
* Fetches body of a block given its hash and number.
95-
* @param {Buffer} hash
96-
* @param {BN} number
97-
* @returns Promise
9898
*/
99-
async getBody (hash, number) {
99+
async getBody (hash: Buffer, number: BN) {
100100
const key = bodyKey(number, hash)
101101
return rlp.decode(await this.get(key, { cache: 'body' }))
102102
}
103103

104104
/**
105105
* Fetches header of a block given its hash and number.
106-
* @param {Buffer} hash
107-
* @param {BN} number
108-
* @returns Promise
109106
*/
110-
async getHeader (hash, number) {
107+
async getHeader (hash: Buffer, number: BN) {
111108
const key = headerKey(number, hash)
112-
let encodedHeader = await this.get(key, { cache: 'header' })
109+
const encodedHeader = await this.get(key, { cache: 'header' })
113110
return new Block.Header(rlp.decode(encodedHeader), { common: this._common })
114111
}
115112

116113
/**
117114
* Fetches total difficulty for a block given its hash and number.
118-
* @param {Buffer} hash
119-
* @param {BN} number
120-
* @returns Promise
121115
*/
122-
async getTd (hash, number) {
116+
async getTd (hash: Buffer, number: BN) {
123117
const key = tdKey(number, hash)
124118
const td = await this.get(key, { cache: 'td' })
125119
return new BN(rlp.decode(td))
126120
}
127121

128122
/**
129123
* Performs a block hash to block number lookup.
130-
* @param {Buffer} hash
131-
* @returns Promise
132124
*/
133-
async hashToNumber (hash) {
125+
async hashToNumber (hash: Buffer) {
134126
const key = hashToNumberKey(hash)
135127
return new BN(await this.get(key, { cache: 'hashToNumber' }))
136128
}
137129

138130
/**
139131
* Performs a block number to block hash lookup.
140-
* @param {BN} number
141-
* @returns Promise
142132
*/
143-
async numberToHash (number) {
133+
async numberToHash (number: BN) {
144134
if (number.ltn(0)) {
145135
throw new level.errors.NotFoundError()
146136
}
@@ -153,14 +143,8 @@ module.exports = class DBManager {
153143
* Fetches a key from the db. If `opts.cache` is specified
154144
* it first tries to load from cache, and on cache miss will
155145
* 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
162146
*/
163-
async get (key, opts = {}) {
147+
async get (key: string | Buffer, opts: any = {}) {
164148
const dbOpts = {
165149
keyEncoding: opts.keyEncoding || 'binary',
166150
valueEncoding: opts.valueEncoding || 'binary'
@@ -173,7 +157,7 @@ module.exports = class DBManager {
173157

174158
let value = this._cache[opts.cache].get(key)
175159
if (!value) {
176-
value = await this._db.get(key, dbOpts)
160+
value = <Buffer> await this._db.get(key, dbOpts)
177161
this._cache[opts.cache].set(key, value)
178162
}
179163

@@ -185,10 +169,8 @@ module.exports = class DBManager {
185169

186170
/**
187171
* Performs a batch operation on db.
188-
* @param {Array} ops
189-
* @returns Promise
190172
*/
191-
batch (ops) {
173+
batch (ops: Array<any>) {
192174
return this._db.batch(ops)
193175
}
194176
}

0 commit comments

Comments
 (0)