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

Commit 0eec902

Browse files
committed
Add tsdoc to Blockchain
1 parent ec58f27 commit 0eec902

File tree

1 file changed

+111
-15
lines changed

1 file changed

+111
-15
lines changed

src/index.ts

Lines changed: 111 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,37 @@ const Stoplight = require('flow-stoplight')
2020
const level = require('level-mem')
2121
const semaphore = require('semaphore')
2222

23+
/**
24+
* This class stores and interacts with blocks.
25+
*
26+
* @remarks
27+
* This class performs write operations. Making a backup of your data before trying this module is
28+
* recommended. Otherwise, you can end up with a compromised DB state.
29+
*
30+
* @example
31+
* The following is an example to iterate through an existing Geth DB (needs `level` to be
32+
* installed separately).
33+
*
34+
* ```javascript
35+
* const level = require('level')
36+
* const Blockchain = require('ethereumjs-blockchain')
37+
* const utils = require('ethereumjs-util')
38+
*
39+
* const gethDbPath = './chaindata' // Add your own path here. It will get modified, see remarks.
40+
* const db = level(gethDbPath)
41+
*
42+
* new Blockchain({ db: db }).iterator(
43+
* 'i',
44+
* (block, reorg, cb) => {
45+
* const blockNumber = utils.bufferToInt(block.header.number)
46+
* const blockHash = block.hash().toString('hex')
47+
* console.log(`BLOCK ${blockNumber}: ${blockHash}`)
48+
* cb()
49+
* },
50+
* err => console.log(err || 'Done.'),
51+
* )
52+
* ```
53+
*/
2354
export default class Blockchain {
2455
private _common: Common
2556
private _genesis: any
@@ -35,8 +66,32 @@ export default class Blockchain {
3566
db: any
3667
dbManager: DBManager
3768
ethash: any
69+
70+
/**
71+
* A flag indicating if this Blockchain validates blocks or not.
72+
*/
3873
validate: boolean
3974

75+
/**
76+
* Creates new Blockchain object
77+
*
78+
* This constructor receives an object with different options, all of them are optional:
79+
*
80+
* * `opts.chain` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** The chain for the block [default: 'mainnet']
81+
* * `opts.hardfork` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Hardfork for the block [default: null, block number-based behavior]
82+
* * `opts.common` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Alternatively pass a Common instance (ethereumjs-common) instead of setting chain/hardfork directly
83+
* * `opts.db` - Database to store blocks and metadata. Should be a [levelup](https://github.com/rvagg/node-levelup) instance.
84+
* * `opts.validate` - this the flag to validate blocks (e.g. Proof-of-Work), latest HF rules supported: `Constantinople`.
85+
*
86+
* **Deprecation note**:
87+
*
88+
* The old separated DB constructor parameters `opts.blockDB` and `opts.detailsDB` from before the Geth DB-compatible
89+
* `v3.0.0` release are deprecated and continued usage is discouraged. When provided `opts.blockDB` will be used as
90+
* `opts.db` and `opts.detailsDB` is ignored. On the storage level the DB formats are not compatible and it is not
91+
* possible to load an old-format DB state into a post-`v3.0.0` `Blockchain` object.
92+
*
93+
* @param opts See above documentation.
94+
*/
4095
constructor(opts: any = {}) {
4196
if (opts.common) {
4297
if (opts.chain) {
@@ -76,7 +131,7 @@ export default class Blockchain {
76131
}
77132

78133
/**
79-
* Define meta getter for backwards compatibility
134+
* Returns an object with metadata about the Blockchain. It's defined for backwards compatibility.
80135
*/
81136
get meta() {
82137
return {
@@ -156,13 +211,19 @@ export default class Blockchain {
156211

157212
/**
158213
* Puts the genesis block in the database
214+
*
215+
* @param genesis - The genesis block to be added
216+
* @param cb - The callback. It is given two parameters `err` and the saved `block`
159217
*/
160218
putGenesis(genesis: any, cb: any): void {
161219
this.putBlock(genesis, cb, true)
162220
}
163221

164222
/**
165223
* Returns the specified iterator head.
224+
*
225+
* @param name - Optional name of the state root head (default: 'vm')
226+
* @param cb - The callback. It is given two parameters `err` and the returned `block`
166227
*/
167228
getHead(name: any, cb?: any): void {
168229
// handle optional args
@@ -184,6 +245,8 @@ export default class Blockchain {
184245

185246
/**
186247
* Returns the latest header in the canonical chain.
248+
*
249+
* @param cb - The callback. It is given two parameters `err` and the returned `header`
187250
*/
188251
getLatestHeader(cb: any): void {
189252
// ensure init completed
@@ -199,6 +262,8 @@ export default class Blockchain {
199262

200263
/**
201264
* Returns the latest full block in the canonical chain.
265+
*
266+
* @param cb - The callback. It is given two parameters `err` and the returned `block`
202267
*/
203268
getLatestBlock(cb: any) {
204269
// ensure init completed
@@ -208,7 +273,10 @@ export default class Blockchain {
208273
}
209274

210275
/**
211-
* Adds many blocks to the blockchain
276+
* Adds many blocks to the blockchain.
277+
*
278+
* @param blocks - The blocks to be added to the blockchain
279+
* @param cb - The callback. It is given two parameters `err` and the last of the saved `blocks`
212280
*/
213281
putBlocks(blocks: Array<any>, cb: any) {
214282
async.eachSeries(
@@ -221,7 +289,10 @@ export default class Blockchain {
221289
}
222290

223291
/**
224-
* Adds a block to the blockchain
292+
* Adds a block to the blockchain.
293+
*
294+
* @param block - The block to be added to the blockchain
295+
* @param cb - The callback. It is given two parameters `err` and the saved `block`
225296
*/
226297
putBlock(block: object, cb: any, isGenesis?: boolean) {
227298
// make sure init has completed
@@ -234,7 +305,10 @@ export default class Blockchain {
234305
}
235306

236307
/**
237-
* Adds many headers to the blockchain
308+
* Adds many headers to the blockchain.
309+
*
310+
* @param headers - The headers to be added to the blockchain
311+
* @param cb - The callback. It is given two parameters `err` and the last of the saved `headers`
238312
*/
239313
putHeaders(headers: Array<any>, cb: any) {
240314
async.eachSeries(
@@ -247,7 +321,10 @@ export default class Blockchain {
247321
}
248322

249323
/**
250-
* Adds a header to the blockchain
324+
* Adds a header to the blockchain.
325+
*
326+
* @param header - The header to be added to the blockchain
327+
* @param cb - The callback. It is given two parameters `err` and the saved `header`
251328
*/
252329
putHeader(header: object, cb: any) {
253330
// make sure init has completed
@@ -429,7 +506,10 @@ export default class Blockchain {
429506
}
430507

431508
/**
432-
*Gets a block by its hash
509+
* Gets a block by its hash.
510+
*
511+
* @param blockTag - The block's hash or number
512+
* @param cb - The callback. It is given two parameters `err` and the found `block` (an instance of https://github.com/ethereumjs/ethereumjs-block) if any.
433513
*/
434514
getBlock(blockTag: Buffer | number | BN, cb: any) {
435515
// ensure init completed
@@ -438,12 +518,18 @@ export default class Blockchain {
438518
})
439519
}
440520

441-
_getBlock(blockTag: Buffer | number | BN, cb: any) {
521+
private _getBlock(blockTag: Buffer | number | BN, cb: any) {
442522
util.callbackify(this.dbManager.getBlock.bind(this.dbManager))(blockTag, cb)
443523
}
444524

445525
/**
446526
* Looks up many blocks relative to blockId
527+
*
528+
* @param blockId - The block's hash or number
529+
* @param maxBlocks - Max number of blocks to return
530+
* @param skip - Number of blocks to skip
531+
* @param reverse - Fetch blocks in reverse
532+
* @param cb - The callback. It is given two parameters `err` and the found `blocks` if any.
447533
*/
448534
getBlocks(blockId: Buffer | number, maxBlocks: number, skip: number, reverse: boolean, cb: any) {
449535
const self = this
@@ -482,16 +568,19 @@ export default class Blockchain {
482568
}
483569

484570
/**
485-
* Gets block details by its hash
571+
* This method used to return block details by its hash. It's only here for backwards compatibility.
572+
*
486573
* @deprecated
487574
*/
488575
getDetails(_: string, cb: any) {
489576
cb(null, {})
490577
}
491578

492579
/**
493-
* Given an ordered array, returns to the callback an array of hashes that are
494-
* not in the blockchain yet
580+
* Given an ordered array, returns to the callback an array of hashes that are not in the blockchain yet.
581+
*
582+
* @param hashes - Ordered array of hashes
583+
* @param cb - The callback. It is given two parameters `err` and hashes found.
495584
*/
496585
selectNeededHashes(hashes: Array<any>, cb: any) {
497586
const self = this
@@ -664,8 +753,11 @@ export default class Blockchain {
664753
}
665754

666755
/**
667-
* Deletes a block from the blockchain. All child blocks in the chain are deleted
668-
* and any encountered heads are set to the parent block
756+
* Deletes a block from the blockchain. All child blocks in the chain are deleted and any
757+
* encountered heads are set to the parent block.
758+
*
759+
* @param blockHash - The hash of the block to be deleted
760+
* @param cb - A callback.
669761
*/
670762
delBlock(blockHash: Buffer, cb: any) {
671763
// make sure init has completed
@@ -787,9 +879,13 @@ export default class Blockchain {
787879
}
788880

789881
/**
790-
* Iterates through blocks starting at the specified iterator head and calls
791-
* the onBlock function on each block. The current location of an iterator head
792-
* can be retrieved using the `getHead()`` method
882+
* Iterates through blocks starting at the specified iterator head and calls the onBlock function
883+
* on each block. The current location of an iterator head can be retrieved using the `getHead()`
884+
* method.
885+
*
886+
* @param name - Name of the state root head
887+
* @param onBlock - Function called on each block with params (block, reorg, cb)
888+
* @param cb - A callback function
793889
*/
794890
iterator(name: string, onBlock: any, cb: any): void {
795891
// ensure init completed

0 commit comments

Comments
 (0)