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

Commit 16cddd1

Browse files
authored
Merge pull request #48 from ethereumjs/update-docs
Updated API docs (Geth compatibility PR #47)
2 parents 78e23e8 + cafa973 commit 16cddd1

File tree

1 file changed

+93
-47
lines changed

1 file changed

+93
-47
lines changed

README.md

Lines changed: 93 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,84 +9,130 @@ A module to store and interact with blocks.
99
# INSTALL
1010
`npm install ethereumjs-blockchain`
1111

12-
# API
12+
# EXAMPLE
13+
14+
The following is an example to iterate through an existing Geth DB (needs ``leveldown`` to be
15+
installed separately):
16+
17+
```javascript
18+
const levelup = require('levelup')
19+
const leveldown = require('leveldown')
20+
const Blockchain = require('./index.js')
21+
const utils = require('ethereumjs-util')
1322

14-
# ethereumjs-blockchain
15-
A module to store and interact with blocks
23+
const gethDbPath = './chaindata' // Add your own path here
24+
const db = levelup(gethDbPath, { db: leveldown })
25+
26+
new Blockchain({db: db}).iterator('i', (block, reorg, cb) => {
27+
const blockNumber = utils.bufferToInt(block.header.number)
28+
const blockHash = block.hash().toString('hex')
29+
console.log(`BLOCK ${blockNumber}: ${blockHash}`)
30+
cb()
31+
}, (err) => console.log(err || 'Done.'))
32+
```
33+
34+
# API
1635

1736
- [`Blockchain`](#blockchain)
1837
- [`new Blockchain(opts)`](#new-blockchainblockdb-detailsdb)
19-
- [`BlockChain` Properties](#blockchain-properties)
2038
- [`BlockChain` methods](#blockchain-methods)
21-
- [`blockchain.putBlock(block, [callback])`](#blockchainputblockblock-callback)
22-
- [`blockchain.getBlock(hash, [callback])`](#blockchaingetblockhash-callback)
23-
- [`blockchain.getBlockInfo(hash, cb)`](#blockchaingetblockinfohash-cb)
24-
- [`blockchain.getBlockHashes(parentHash, count, cb)`](#blockchaingetblockhashesparenthash-count-cb)
25-
- [`blockchain.getBlockChain(startingHashes, count, cb)`](#blockchaingetblockhashesparenthash-count-cb)
26-
- [`blockchain.selectNeededHashes(hashes, cb)`](#blockchainselectneededhasheshashes-cb)
39+
- [`blockchain.putGenesis(genesis, [cb])`](#blockchainputgenesisgenesis-cb)
40+
- [`blockchain.getHead(name, [cb])`](#blockchaingetheadname-cb)
41+
- [`blockchain.putBlocks(blocks, [cb])`](#blockchainputblocksblocks-cb)
42+
- [`blockchain.putBlock(block, [cb])`](#blockchainputblockblock-cb)
43+
- [`blockchain.getBlock(hash, [cb])`](#blockchaingetblockhash-cb)
44+
- [`blockchain.getBlocks(blockId, maxBlocks, skip, reverse, [cb])`](#blockchaingetblocksblockid-maxblocks-skip-reverse-cb)
45+
- [`blockchain.selectNeededHashes(hashes, [cb])`](#blockchainselectneededhasheshashes-cb)
46+
- [`blockchain.delBlock(blockHash, [cb])`](#blockchaindelblockblockhash-cb)
47+
- [`blockchain.iterator(name, onBlock, [cb])`](#blockchainiteratorname-onblock-cb)
2748

2849
## `Blockchain`
2950
Implements functions for retrieving, manipulating and storing Ethereum's blockchain
3051

3152
### `new Blockchain(opts)`
3253
Creates new Blockchain object
33-
- `opts.blockDB` - the database where Blocks are stored and retreived by hash. Should be a [levelup](https://github.com/rvagg/node-levelup) instance.
34-
- `opts.detailsDB` - the database where Block number resolutions and other metadata is stored. Should be a [levelup](https://github.com/rvagg/node-levelup) instance.
54+
- `opts.db` - Database to store blocks and metadata. Should be a [levelup](https://github.com/rvagg/node-levelup) instance.
3555
- `opts.validate` - this the flag to validate blocks (e.g. Proof-of-Work).
3656

37-
### `BlockChain` Properties
38-
- `head` - The Head, the block that has the most weight
39-
- `parentHead`- The parent of the head block
40-
- `genesisHash` - The hash of the genesis block
41-
- `height` - The height of the blockchain
42-
- `totallDifficulty` - The totall difficulty which is the some of a the difficulty of all the prevous blocks
57+
[DEPRECATION NOTE]
58+
The old separated DB constructor parameters `opts.blockDB` and `opts.detailsDB` from before the Geth DB-compatible ``v3.0.0`` release are deprecated and continued usage is discouraged. When provided `opts.blockDB` will be used
59+
as `opts.db` and `opts.detailsDB` is ignored. On the storage level the DB formats are not compatible and it is not
60+
possible to load an old-format DB state into a post-``v3.0.0`` ``Blockchain`` object.
4361

4462
### `BlockChain` methods
4563

46-
#### `blockchain.putBlock(block, callback)`
64+
#### `blockchain.putGenesis(genesis, cb)`
65+
Puts the genesis block in the database.
66+
- `genesis` - the genesis block to be added
67+
- `cb` - the callback. It is given two parameters `err` and the saved `block`
68+
69+
--------------------------------------------------------
70+
71+
#### `blockchain.getHead(name, cb)`
72+
Returns that head block.
73+
- `name` - Optional name of the state root head (default: 'vm')
74+
- `cb` - the callback. It is given two parameters `err` and the returned `block`
75+
76+
--------------------------------------------------------
77+
78+
#### `blockchain.putBlocks(blocks, cb)`
79+
Adds many blocks to the blockchain.
80+
- `blocks` - the blocks to be added to the blockchain
81+
- `cb` - the callback. It is given two parameters `err` and the last of the saved `blocks`
82+
--------------------------------------------------------
83+
84+
#### `blockchain.putBlock(block, cb)`
4785
Adds a block to the blockchain.
4886
- `block` - the block to be added to the blockchain
49-
- `callback` - the callback. It is given two parameters `err` and the saved `block`
87+
- `cb` - the callback. It is given two parameters `err` and the saved `block`
5088

5189
--------------------------------------------------------
5290

53-
#### `blockchain.getBlock(blockTag, callback)`
91+
#### `blockchain.getBlock(blockTag, cb)`
5492
Gets a block by its blockTag.
5593
- `blockTag` - the block's hash or number
56-
- `callback` - the callback. It is given two parameters `err` and the found `block` (an instance of https://github.com/ethereumjs/ethereumjs-block) if any.
94+
- `cb` - the callback. It is given two parameters `err` and the found `block` (an instance of https://github.com/ethereumjs/ethereumjs-block) if any.
5795

5896
--------------------------------------------------------
5997

60-
#### `blockchain.getDetails(hash, callback)`
61-
Retrieves meta infromation about the block and passed it to the `callback`
62-
- `hash` - the hash of the block as a `Buffer` or a hex `String`
63-
- `callback` - the callback which is passed an `Object` containing the following properties:
64-
- * `parent` - the hash of the parent block
65-
- * `td` - the total difficulty of the block
66-
- * `number` - the block number
67-
- * `child` - the block's children
68-
- * `genesis` - boolean (true if genesis block, false if not)
69-
- * `inChain` - TODO
70-
- * `staleChildren` - TODO
98+
#### `blockchain.getBlocks(blockId, maxBlocks, skip, reverse, cb)`
99+
Looks up many blocks relative to blockId.
100+
- `blockId` - the block's hash or number
101+
- `maxBlocks` - max number of blocks to return
102+
- `skip` - number of blocks to skip
103+
- `reverse` - fetch blocks in reverse
104+
- `cb` - the callback. It is given two parameters `err` and the found `blocks` if any.
71105

72106
--------------------------------------------------------
73107

74-
#### `blockchain.getBlockHashes(parentHash, count, callback)`
75-
Gets a segment of the blockchain starting at the parent hash and contuning for `count ` blocks returning an array of block hashes orders from oldest to youngest.
76-
- `parentHash` - the block to start from. Given as a `Buffer` or a hex `String`
77-
- `count` - a `Number` specifing how many block hashes to return
78-
- `callback` - the callback which is give an array of block hashes
108+
#### `blockchain.getDetails(hash, cb)`
109+
[DEPRECATED] Returns an empty object
79110

80111
--------------------------------------------------------
81112

113+
#### `blockchain.selectNeededHashes(hashes, cb)`
114+
Given an ordered array, returns to the callback an array of hashes that are not in the blockchain yet.
115+
- `hashes` - Ordered array of hashes
116+
- `cb` - the callback. It is given two parameters `err` and hashes found.
117+
118+
--------------------------------------------------------
82119

83-
#### `blockchain.getBlockChain(startingHashes, count, callback)`
84-
gets a section of the blockchain in a form of an array starting at the parent hash, up `count` blocks
85-
- `startingHashes` - an array of hashes or a single hash to start returning the chain from. The first hash in the array that is found in the blockchain will be used.
86-
- `count` - the max number of blocks to return
87-
- `callback` - the callback. It is given two parameters `err` and `blockchain`. `err` is any errors. If none of the starting hashes were found `err` will be `notFound`. `blockchain` is an array of blocks.
120+
#### `blockchain.delBlock(blockHash, cb)`
121+
Deletes a block from the blockchain. All child blocks in the chain are deleted and any encountered heads are set to the parent block
122+
- `blockHash` - the hash of the block to be deleted
123+
- `cb` - A callback.
124+
125+
--------------------------------------------------------
126+
127+
#### `blockchain.iterator(name, onBlock, cb)`
128+
Iterates through blocks starting at the specified verified state root head and calls the onBlock function on each block
129+
- `name` - name of the state root head
130+
- `onBlock` - function called on each block with params (block, reorg, cb)
131+
- `cb` - A callback function
132+
133+
# TESTS
134+
135+
Tests can be found in the ``test`` directory and run with ``npm run test``.
136+
137+
These can also be valuable as examples/inspiration on how to run the library and invoke different parts of the API.
88138

89-
#### `blockchain.selectNeededHashes(hashes, callback)`
90-
Given an ordered array, returns to the callback an array of hashes that are not in the blockchain yet.
91-
- `hashes` - an `Array` hashes
92-
- `callback` - the callback

0 commit comments

Comments
 (0)