-
Notifications
You must be signed in to change notification settings - Fork 753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promisify blockchain and ethash packages #833
Changes from 1 commit
b4e2b76
c4992ff
e6e6487
8a2e714
0599975
324da98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
This file was deleted.
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side note, totally unrelated to the code changes done in the PR, just not down since I stumbled upon during reading the code, maybe someone wants to change along a future PR on the library: I could better read/understand the related code parts if these constants from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea, fixed in c4992ff |
||
|
@@ -12,23 +14,41 @@ import { | |
} from './util' | ||
|
||
import BN = require('bn.js') | ||
import type { LevelUp } from 'levelup' | ||
holgerd77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
} | ||
holgerd77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* 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 = { | ||
|
@@ -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> { | ||
holgerd77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not directly change-related, just realizing that I don't like the name So I would find There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree here and didn't find any use of "blockTag" outside of the blockchain package so have renamed in 8a2e714 |
||
// determine BlockTag type | ||
if (typeof blockTag === 'number' && Number.isInteger(blockTag)) { | ||
blockTag = new BN(blockTag) | ||
|
@@ -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 | ||
} | ||
holgerd77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
body = [[], []] | ||
} | ||
|
||
|
@@ -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', | ||
|
@@ -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[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is not an async operation and the return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added in e6e6487 |
||
return this._db.batch(ops as any) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the record: this is changing the API of the
block
library in the form thatvalidate()
- calling into_getBlockByHash()
now only works with the yet to be released promisifedBlockchain
version.Wonder if this would be a good case for adding a
peerDependency
(see Peer Dependencies | Node.js) entry in theBlock
package.json
referencing theBlockchain
version? // cc @alcuadradoDo we want to do a more systematic approach and generally introduce
peerDependency
references within the monorepo scope? These inter-library compatibility insecurities are a reoccurring problem.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone reading this think this is a good idea eventually directly open an issue on this, optimally (but leave if no time) directly with a TODO list with the listed directed references/dependencies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also be added as a linked reference to the v5 release TODO list (if we agree upon), since it would make very much sense to add this along the new releases.