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

Commit c10934d

Browse files
committed
Mv caches and some db ops to new DBManager
1 parent 29a337f commit c10934d

File tree

3 files changed

+212
-134
lines changed

3 files changed

+212
-134
lines changed

src/dbManager.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
const BN = require('bn.js')
2+
const level = require('level-mem')
3+
const rlp = require('rlp')
4+
const Block = require('ethereumjs-block')
5+
const Cache = require('./cache')
6+
const {
7+
headsKey,
8+
headHeaderKey,
9+
headBlockKey,
10+
hashToNumberKey,
11+
numberToHashKey,
12+
tdKey,
13+
bodyKey,
14+
headerKey
15+
} = require('./util')
16+
17+
module.exports = class DBManager {
18+
constructor (db, common) {
19+
this._db = db
20+
this._common = common
21+
this._opts = {
22+
keyEncoding: 'binary',
23+
valueEncoding: 'binary'
24+
}
25+
this._cache = {
26+
td: new Cache({ max: 1024 }),
27+
header: new Cache({ max: 512 }),
28+
body: new Cache({ max: 256 }),
29+
numberToHash: new Cache({ max: 2048 }),
30+
hashToNumber: new Cache({ max: 2048 })
31+
}
32+
}
33+
34+
getHeads () {
35+
return this._db.get(headsKey, {
36+
keyEncoding: 'binary',
37+
valueEncoding: 'json'
38+
})
39+
}
40+
41+
getHeadHeader () {
42+
return this._db.get(headHeaderKey, this._opts)
43+
}
44+
45+
getHeadBlock () {
46+
return this._db.get(headBlockKey, this._opts)
47+
}
48+
49+
get (k) {
50+
return this._db.get(k, this._opts)
51+
}
52+
53+
batch (ops) {
54+
return this._db.batch(ops)
55+
}
56+
57+
/**
58+
* Performs a block hash to block number lookup
59+
* @method _hashToNumber
60+
*/
61+
async _hashToNumber (hash) {
62+
const key = hashToNumberKey(hash)
63+
let number = this._cache.hashToNumber.get(key)
64+
if (number) {
65+
return new BN(number)
66+
}
67+
68+
number = await this.get(key)
69+
this._cache.hashToNumber.set(key, number)
70+
71+
return new BN(number)
72+
}
73+
74+
/**
75+
* Performs a block number to block hash lookup
76+
* @method _numberToHash
77+
*/
78+
async _numberToHash (number) {
79+
if (number.ltn(0)) {
80+
throw new level.errors.NotFoundError()
81+
}
82+
83+
const key = numberToHashKey(number)
84+
let hash = this._cache.numberToHash.get(key)
85+
if (hash) {
86+
return hash
87+
}
88+
89+
hash = await this._db.get(key)
90+
this._cache.numberToHash.set(key, hash)
91+
92+
return hash
93+
}
94+
95+
async _getTd (hash, number) {
96+
const key = tdKey(number, hash)
97+
let td = this._cache.td.get(key)
98+
if (td) {
99+
return new BN(rlp.decode(td))
100+
}
101+
102+
td = await this._db.get(key)
103+
this._cache.td.set(key, td)
104+
105+
return new BN(rlp.decode(td))
106+
}
107+
108+
async _getBody (hash, number) {
109+
const key = bodyKey(number, hash)
110+
let encodedBody = this._cache.body.get(key)
111+
if (encodedBody) {
112+
return rlp.decode(encodedBody)
113+
}
114+
115+
encodedBody = await this._db.get(key)
116+
this._cache.body.set(key, encodedBody)
117+
118+
return rlp.decode(encodedBody)
119+
}
120+
121+
async _getHeader (hash, number) {
122+
const key = headerKey(number, hash)
123+
let encodedHeader = this._cache.header.get(key)
124+
if (encodedHeader) {
125+
return new Block.Header(rlp.decode(encodedHeader), {common: this._common})
126+
}
127+
128+
encodedHeader = await this._db.get(key)
129+
this._cache.header.set(key, encodedHeader)
130+
return new Block.Header(rlp.decode(encodedHeader), {common: this._common})
131+
}
132+
}

0 commit comments

Comments
 (0)