Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #89 from rkernegger/issue35
Browse files Browse the repository at this point in the history
Add more Ethereum state DB focused example accessing account values
  • Loading branch information
holgerd77 committed Jun 23, 2019
2 parents b8f612b + 917c50d commit cf40000
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,49 @@ trie.createReadStream()
})
```

## Read Account State including Storage from Geth DB

```javascript
var level = require('level')
var rlp = require('rlp')
var ethutil = require('ethereumjs-util')

var Trie = require('merkle-patricia-tree/secure')
var Account = require('ethereumjs-account').default
var BN = ethutil.BN

var stateRoot = 'STATE_ROOT_OF_A_BLOCK'

var db = level('YOUR_PATH_TO_THE_GETH_CHAINDATA_FOLDER')
var trie = new Trie(db, stateRoot)

var address = 'AN_ETHEREUM_ACCOUNT_ADDRESS'

trie.get(address, function (err, data) {
if (err) return cb(err)

var acc = new Account(data)
console.log('-------State-------')
console.log(`nonce: ${new BN(acc.nonce)}`)
console.log(`balance in wei: ${new BN(acc.balance)}`)
console.log(`storageRoot: ${ethutil.bufferToHex(acc.stateRoot)}`)
console.log(`codeHash: ${ethutil.bufferToHex(acc.codeHash)}`)

var storageTrie = trie.copy()
storageTrie.root = acc.stateRoot

console.log('------Storage------')
var stream = storageTrie.createReadStream()
stream.on('data', function(data) {
console.log(`key: ${ethutil.bufferToHex(data.key)}`)
console.log(`Value: ${ethutil.bufferToHex(rlp.decode(data.value))}`)
})
.on('end', function() {
console.log('Finished reading storage.')
})
})
```

# API
[./docs/](./docs/index.md)

Expand Down

0 comments on commit cf40000

Please sign in to comment.