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

Stop adding account to cache when checking if it is empty. #375

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions lib/stateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ proto.getAccount = function (address, cb) {
this.cache.getOrLoad(address, cb)
}

proto._getAccountPure = function (address, cb) {
const self = this
const cachedAccount = this.cache.get(address)

if (cachedAccount) {
cb(null, cachedAccount)
} else {
self.trie.get(address, function (err, account) {
if (err) {
return cb(err)
}
cb(null, account)
})
}
}

// saves the account
proto.putAccount = function (address, account, cb) {
var self = this
Expand Down Expand Up @@ -285,9 +301,16 @@ proto.generateGenesis = function (initState, cb) {
}, cb)
}

proto.accountIsEmpty = function (address, cb) {
proto.accountIsEmpty = function (address, getAccountWithoutLoad, cb) {
if (cb === undefined) {
cb = getAccountWithoutLoad
getAccountWithoutLoad = null
}

var self = this
self.getAccount(address, function (err, account) {
var getAccountCall = getAccountWithoutLoad ? self._getAccountPure : self.getAccount

getAccountCall.bind(this)(address, function (err, account) {
if (err) {
return cb(err)
}
Expand All @@ -301,7 +324,7 @@ proto.cleanupTouchedAccounts = function (cb) {
var touchedArray = Array.from(self._touched)
async.forEach(touchedArray, function (addressHex, next) {
var address = Buffer.from(addressHex, 'hex')
self.accountIsEmpty(address, function (err, empty) {
self.accountIsEmpty(address, true, function (err, empty) {
if (err) {
next(err)
return
Expand Down
75 changes: 74 additions & 1 deletion tests/api/stateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tape('StateManager', (t) => {
st.end()
})

t.test('should put and get account', async (st) => {
t.test('should put and get account, and add to the underlying cache if the account is not found', async (st) => {
const stateManager = new StateManager()
const account = createAccount()

Expand All @@ -32,6 +32,79 @@ tape('StateManager', (t) => {
)

st.equal(res.balance.toString('hex'), 'fff384')

stateManager.cache.clear()

res = await promisify(stateManager.getAccount.bind(stateManager))(
'a94f5374fce5edbc8e2a8697c15331677e6ebf0b'
)

st.equal(stateManager.cache._cache.keys[0], 'a94f5374fce5edbc8e2a8697c15331677e6ebf0b')

st.end()
})

t.test('should retrieve a cached account, without adding to the underlying cache if the account is not found', async (st) => {
const stateManager = new StateManager()
const account = createAccount()

await promisify(stateManager.putAccount.bind(stateManager))(
'a94f5374fce5edbc8e2a8697c15331677e6ebf0b',
account
)

let res = await promisify(stateManager._getAccountPure.bind(stateManager))(
'a94f5374fce5edbc8e2a8697c15331677e6ebf0b'
)

st.equal(res.balance.toString('hex'), 'fff384')

stateManager.cache.clear()

res = await promisify(stateManager._getAccountPure.bind(stateManager))(
'a94f5374fce5edbc8e2a8697c15331677e6ebf0b'
)

st.notOk(stateManager.cache._cache.keys[0])

st.end()
})

t.test('should call the callback with a boolean representing emptiness, and not cache the account if passed correct params', async (st) => {
const stateManager = new StateManager()

const promisifiedAccountIsEmpty = promisify(stateManager.accountIsEmpty.bind(stateManager), function (err, result) {
return err || result
})
let res = await promisifiedAccountIsEmpty('a94f5374fce5edbc8e2a8697c15331677e6ebf0b', true)

st.ok(res)
st.notOk(stateManager.cache._cache.keys[0])

let res2 = await promisifiedAccountIsEmpty('0x095e7baea6a6c7c4c2dfeb977efac326af552d87')

st.ok(res2)
st.equal(stateManager.cache._cache.keys[0], '0x095e7baea6a6c7c4c2dfeb977efac326af552d87')

st.end()
})

t.test('should call the callback with a false boolean representing emptiness when the account is empty', async (st) => {
const stateManager = new StateManager()
const account = createAccount('0x1', '0x1')

await promisify(stateManager.putAccount.bind(stateManager))(
'a94f5374fce5edbc8e2a8697c15331677e6ebf0b',
account
)

const promisifiedAccountIsEmpty = promisify(stateManager.accountIsEmpty.bind(stateManager), function (err, result) {
return err || result
})
let res = await promisifiedAccountIsEmpty('a94f5374fce5edbc8e2a8697c15331677e6ebf0b', true)

st.notOk(res)

st.end()
})
})
6 changes: 3 additions & 3 deletions tests/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ function createGenesis () {
return genesis
}

function createAccount () {
function createAccount (nonce, balance) {
const raw = {
nonce: '0x00',
balance: '0xfff384'
nonce: nonce || '0x00',
balance: balance || '0xfff384'
}
const acc = new Account(raw)
return acc
Expand Down