Skip to content
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

Update blockchain readme example #836

Merged
merged 3 commits into from
Aug 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions packages/blockchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,24 @@ The following is an example to iterate through an existing Geth DB (needs `level

This module performs write operations. Making a backup of your data before trying it is recommended. Otherwise, you can end up with a compromised DB state.

```javascript
const level = require('level')
const Blockchain = require('@ethereumjs/blockchain').default
const utils = require('ethereumjs-util')
```typescript
import Blockchain from '@ethereumjs/blockchain'
import { bufferToInt } from 'ethereumjs-util'
import level from 'level'

const gethDbPath = './chaindata' // Add your own path here. It will get modified, see remarks.

const db = level(gethDbPath)
const blockchain = new Blockchain({ db })

new Blockchain({ db: db }).iterator(
'i',
(block, reorg, cb) => {
const blockNumber = utils.bufferToInt(block.header.number)
const blockHash = block.hash().toString('hex')
console.log(`BLOCK ${blockNumber}: ${blockHash}`)
cb()
},
(err) => console.log(err || 'Done.'),
)
blockchain.iterator('i', (block) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intended to leave the await here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the await would be useful for awaiting until the iterator is finished before moving onto the next line of code but since there is none I thought it’d be okay (and then I’d have to wrap it in an inner async func since you can’t await from the top level). I didn’t actually test it but I think it’ll work like that! Please correct me if I’m wrong :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess another option could be to add a .then(() => { console.log('Done.') }).catch(console.error)

const blockNumber = bufferToInt(block.header.number)
const blockHash = block.hash().toString('hex')
console.log(`Block ${blockNumber}: ${blockHash}`)
})
```

**WARNING**: Since `@ethereumjs/blockchain` is also doing write operations
on the DB for safety reasons only run this on a copy of your database, otherwise this might lead
to a compromised DB state.
**WARNING**: Since `@ethereumjs/blockchain` is also doing write operations on the DB for safety reasons only run this on a copy of your database, otherwise this might lead to a compromised DB state.

# EthereumJS

Expand Down