Skip to content

Commit

Permalink
Minor syntactic changes
Browse files Browse the repository at this point in the history
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
  • Loading branch information
s1na committed Jan 28, 2019
1 parent ed3a7da commit 00e5fb7
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module.exports = class Cache {
* Puts account to cache under its address.
* @param {Buffer} key - Address of account
* @param {Account} val - Account
* @param {bool} fromTrie
* @param {bool} [fromTrie]
*/
put (key, val, fromTrie) {
var modified = !fromTrie
put (key, val, fromTrie = false) {
const modified = !fromTrie
this._update(key, val, modified, false)
}

Expand All @@ -26,7 +26,7 @@ module.exports = class Cache {
* @param {Buffer} key - Address of account
*/
get (key) {
var account = this.lookup(key)
let account = this.lookup(key)
if (!account) {
account = new Account()
}
Expand All @@ -40,9 +40,9 @@ module.exports = class Cache {
lookup (key) {
key = key.toString('hex')

var it = this._cache.find(key)
const it = this._cache.find(key)
if (it.node) {
var account = new Account(it.value.val)
const account = new Account(it.value.val)
return account
}
}
Expand All @@ -53,8 +53,7 @@ module.exports = class Cache {
* @param {Function} cb - Callback with params (err, account)
*/
_lookupAccount (address, cb) {
var self = this
self._trie.get(address, function (err, raw) {
this._trie.get(address, (err, raw) => {
if (err) return cb(err)
var account = new Account(raw)
cb(null, account)
Expand All @@ -68,14 +67,13 @@ module.exports = class Cache {
* @param {Function} cb - Callback with params (err, account)
*/
getOrLoad (key, cb) {
var self = this
var account = this.lookup(key)
const account = this.lookup(key)
if (account) {
async.nextTick(cb, null, account)
} else {
self._lookupAccount(key, function (err, account) {
this._lookupAccount(key, (err, account) => {
if (err) return cb(err)
self._update(key, account, false, false)
this._update(key, account, false, false)
cb(null, account)
})
}
Expand All @@ -88,18 +86,17 @@ module.exports = class Cache {
* @param {Function} cb - Callback
*/
warm (addresses, cb) {
var self = this
// shim till async supports iterators
var accountArr = []
addresses.forEach(function (val) {
addresses.forEach((val) => {
if (val) accountArr.push(val)
})

async.eachSeries(accountArr, function (addressHex, done) {
async.eachSeries(accountArr, (addressHex, done) => {
var address = Buffer.from(addressHex, 'hex')
self._lookupAccount(address, function (err, account) {
this._lookupAccount(address, (err, account) => {
if (err) return done(err)
self._update(address, account, false, false)
this._update(address, account, false, false)
done()
})
}, cb)
Expand All @@ -111,16 +108,13 @@ module.exports = class Cache {
* @param {function} cb - Callback
*/
flush (cb) {
var it = this._cache.begin
var self = this
var next = true
async.whilst(function () {
return next
}, function (done) {
const it = this._cache.begin
let next = true
async.whilst(() => next, (done) => {
if (it.value && it.value.modified) {
it.value.modified = false
it.value.val = it.value.val.serialize()
self._trie.put(Buffer.from(it.key, 'hex'), it.value.val, function () {
this._trie.put(Buffer.from(it.key, 'hex'), it.value.val, () => {
next = it.hasNext
it.next()
done()
Expand All @@ -129,7 +123,7 @@ module.exports = class Cache {
it.value.modified = false
it.value.deleted = false
it.value.val = (new Account()).serialize()
self._trie.del(Buffer.from(it.key, 'hex'), function () {
this._trie.del(Buffer.from(it.key, 'hex'), () => {
next = it.hasNext
it.next()
done()
Expand All @@ -154,7 +148,7 @@ module.exports = class Cache {
* Revert changes to cache last checkpoint (no effect on trie).
*/
revert () {
this._cache = this._checkpoints.pop(this._cache)
this._cache = this._checkpoints.pop()
}

/**
Expand All @@ -181,7 +175,7 @@ module.exports = class Cache {

_update (key, val, modified, deleted) {
key = key.toString('hex')
var it = this._cache.find(key)
const it = this._cache.find(key)
if (it.node) {
this._cache = it.update({
val: val,
Expand Down

0 comments on commit 00e5fb7

Please sign in to comment.