Skip to content

Commit

Permalink
promisify blockchain, ethash
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Aug 15, 2020
1 parent 546736d commit ad9e983
Show file tree
Hide file tree
Showing 27 changed files with 1,525 additions and 2,495 deletions.
466 changes: 109 additions & 357 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 5 additions & 10 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,10 @@ export class BlockHeader {
}

private async _getBlockByHash(blockchain: Blockchain, hash: Buffer): Promise<Block | undefined> {
return new Promise((resolve, reject) => {
blockchain.getBlock(hash, (err, block) => {
if (err) {
reject(err)
return
}

resolve(block)
})
})
try {
return blockchain.getBlock(hash)
} catch (e) {
return undefined
}
}
}
2 changes: 1 addition & 1 deletion packages/block/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ export interface BlockData {
}

export interface Blockchain {
getBlock(hash: Buffer, callback: (err: Error | null, block?: Block) => void): void
getBlock(hash: Buffer): Promise<Block>
}
8 changes: 2 additions & 6 deletions packages/blockchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,11 @@
"@ethereumjs/block": "^3.0.0",
"@ethereumjs/common": "^1.5.1",
"@ethereumjs/ethash": "^1.0.0",
"async": "^2.6.1",
"ethereumjs-util": "^7.0.4",
"flow-stoplight": "^1.0.0",
"level-mem": "^3.0.1",
"level-mem": "^5.0.1",
"lru-cache": "^5.1.1",
"rlp": "^2.2.3",
"semaphore": "^1.1.0",
"util.promisify": "^1.0.1"
"semaphore-async-await": "^1.5.1"
},
"devDependencies": {
"@ethereumjs/config-nyc": "^1.1.1",
Expand All @@ -57,7 +54,6 @@
"@types/bn.js": "^4.11.6",
"@types/lru-cache": "^5.1.0",
"@types/node": "^11.13.4",
"@types/semaphore": "^1.1.0",
"@types/tape": "^4.13.0",
"nyc": "^14.0.0",
"prettier": "^2.0.5",
Expand Down
77 changes: 0 additions & 77 deletions packages/blockchain/src/callbackify.ts

This file was deleted.

63 changes: 45 additions & 18 deletions packages/blockchain/src/dbManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as rlp from 'rlp'
import { Block, BlockHeader } from '@ethereumjs/block'
import Common from '@ethereumjs/common'
import Cache from './cache'
import {
headsKey,
Expand All @@ -12,23 +14,41 @@ import {
} from './util'

import BN = require('bn.js')
import type { LevelUp } from 'levelup'

const level = require('level-mem')
import { Block, BlockHeader } from '@ethereumjs/block'

/**
* @hidden
*/
export interface DBOp {
type: String
key: Buffer | String
keyEncoding: String
valueEncoding?: String
value?: Buffer | object
}

/**
* @hidden
*/
export interface GetOpts {
keyEncoding?: string
valueEncoding?: string
cache?: string
}

/**
* Abstraction over a DB to facilitate storing/fetching blockchain-related
* data, such as blocks and headers, indices, and the head block.
* @hidden
*/
export default class DBManager {
export class DBManager {
_cache: { [k: string]: Cache<Buffer> }
_common: Common
_db: LevelUp

_common: any

_db: any

constructor(db: any, common: any) {
constructor(db: LevelUp, common: Common) {
this._db = db
this._common = common
this._cache = {
Expand All @@ -43,29 +63,33 @@ export default class DBManager {
/**
* Fetches iterator heads from the db.
*/
getHeads(): Promise<any> {
return this.get(headsKey, { valueEncoding: 'json' })
async getHeads(): Promise<{ [key: string]: Buffer }> {
const heads = await this.get(headsKey, { valueEncoding: 'json' })
Object.keys(heads).forEach((key) => {
heads[key] = Buffer.from(heads[key])
})
return heads
}

/**
* Fetches header of the head block.
*/
getHeadHeader(): Promise<any> {
async getHeadHeader(): Promise<Buffer> {
return this.get(headHeaderKey)
}

/**
* Fetches head block.
*/
getHeadBlock(): Promise<any> {
async getHeadBlock(): Promise<Buffer> {
return this.get(headBlockKey)
}

/**
* Fetches a block (header and body), given a block tag
* which can be either its hash or its number.
*/
async getBlock(blockTag: Buffer | BN | number): Promise<any> {
async getBlock(blockTag: Buffer | BN | number): Promise<Block> {
// determine BlockTag type
if (typeof blockTag === 'number' && Number.isInteger(blockTag)) {
blockTag = new BN(blockTag)
Expand All @@ -83,11 +107,14 @@ export default class DBManager {
throw new Error('Unknown blockTag type')
}

const header: any = (await this.getHeader(hash, number)).raw
let body
const header = (await this.getHeader(hash, number)).raw
let body: any
try {
body = await this.getBody(hash, number)
} catch (e) {
} catch (error) {
if (error.type !== 'NotFoundError') {
throw error
}
body = [[], []]
}

Expand Down Expand Up @@ -149,7 +176,7 @@ export default class DBManager {
* it first tries to load from cache, and on cache miss will
* try to put the fetched item on cache afterwards.
*/
async get(key: string | Buffer, opts: any = {}): Promise<any> {
async get(key: string | Buffer, opts: GetOpts = {}): Promise<any> {
const dbOpts = {
keyEncoding: opts.keyEncoding || 'binary',
valueEncoding: opts.valueEncoding || 'binary',
Expand All @@ -175,7 +202,7 @@ export default class DBManager {
/**
* Performs a batch operation on db.
*/
batch(ops: Array<any>): Promise<any> {
return this._db.batch(ops)
batch(ops: DBOp[]) {
return this._db.batch(ops as any)
}
}
Loading

0 comments on commit ad9e983

Please sign in to comment.