forked from bit-warrior/Read-LevelDB-Geth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadLevelDB.js
74 lines (44 loc) · 1.63 KB
/
readLevelDB.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const level = require('level')
const rlp = require('rlp')
const ethUtil = require('ethereumjs-util')
const BN = ethUtil.BN
const bufBE8 = n => n.toArrayLike(Buffer, 'be', 8)
const bodyKey = (n, hash) => Buffer.concat([Buffer.from('b'), bufBE8(n), hash])
const headerKey = (n, hash) => Buffer.concat([Buffer.from('h'), bufBE8(n), hash])
const recptKey = (n, hash) => Buffer.concat([Buffer.from('r'), bufBE8(n), hash])
function ReadLevelDB(path){
this.db=level(path)
}
ReadLevelDB.prototype.getBody=function(blockNumber,blockHash,cb){
this.db.get(bodyKey(new BN(blockNumber),ethUtil.toBuffer(blockHash)),{
keyEncoding: 'binary',
valueEncoding: 'binary'
}, function (err, value) {
cb(err,rlp.decode(value))
})
}
ReadLevelDB.prototype.getHeader=function(blockNumber,blockHash,cb){
this.db.get(headerKey(new BN(blockNumber),ethUtil.toBuffer(blockHash)),{
keyEncoding: 'binary',
valueEncoding: 'binary'
}, function (err, value) {
cb(err,rlp.decode(value))
})
}
ReadLevelDB.prototype.getReceiptTrie=function(blockNumber,blockHash,cb){
this.db.get(recptKey(new BN(blockNumber),ethUtil.toBuffer(blockHash)),{
keyEncoding: 'binary',
valueEncoding: 'binary'
}, function (err, value) {
cb(err,rlp.decode(value))
})
}
ReadLevelDB.prototype.getStateTrie=function(blockHash,cb){
this.db.get(ethUtil.toBuffer(blockHash),{
keyEncoding: 'binary',
valueEncoding: 'binary'
}, function (err, value) {
cb(err,rlp.decode(value))
})
}
module.exports=ReadLevelDB;