2017-09-22 22:59:25 +02:00
|
|
|
/* Account Tracker
|
2017-01-04 23:07:08 +01:00
|
|
|
*
|
|
|
|
* This module is responsible for tracking any number of accounts
|
|
|
|
* and caching their current balances & transaction counts.
|
|
|
|
*
|
|
|
|
* It also tracks transaction hashes, and checks their inclusion status
|
|
|
|
* on each new block.
|
|
|
|
*/
|
|
|
|
|
2017-01-04 22:56:21 +01:00
|
|
|
const async = require('async')
|
|
|
|
const EthQuery = require('eth-query')
|
2017-02-03 07:05:06 +01:00
|
|
|
const ObservableStore = require('obs-store')
|
2017-09-26 23:15:16 +02:00
|
|
|
const EventEmitter = require('events').EventEmitter
|
2017-04-27 06:05:45 +02:00
|
|
|
function noop () {}
|
2017-01-04 22:56:21 +01:00
|
|
|
|
|
|
|
|
2017-09-26 23:15:16 +02:00
|
|
|
class AccountTracker extends EventEmitter {
|
2017-01-04 22:56:21 +01:00
|
|
|
|
2017-02-03 07:05:06 +01:00
|
|
|
constructor (opts = {}) {
|
2017-09-26 23:15:16 +02:00
|
|
|
super()
|
|
|
|
|
|
|
|
const initState = {
|
2017-02-03 07:05:06 +01:00
|
|
|
accounts: {},
|
2017-09-26 22:36:41 +02:00
|
|
|
currentBlockGasLimit: '',
|
2017-09-26 23:15:16 +02:00
|
|
|
}
|
|
|
|
this.store = new ObservableStore(initState)
|
|
|
|
|
2017-02-03 07:05:06 +01:00
|
|
|
this._provider = opts.provider
|
|
|
|
this._query = new EthQuery(this._provider)
|
|
|
|
this._blockTracker = opts.blockTracker
|
|
|
|
// subscribe to latest block
|
|
|
|
this._blockTracker.on('block', this._updateForBlock.bind(this))
|
2017-02-03 21:35:01 +01:00
|
|
|
// blockTracker.currentBlock may be null
|
|
|
|
this._currentBlockNumber = this._blockTracker.currentBlock
|
2017-01-04 22:56:21 +01:00
|
|
|
}
|
|
|
|
|
2017-02-03 07:05:06 +01:00
|
|
|
//
|
|
|
|
// public
|
|
|
|
//
|
2017-01-04 22:56:21 +01:00
|
|
|
|
2017-02-03 07:05:06 +01:00
|
|
|
addAccount (address) {
|
2017-09-26 23:15:16 +02:00
|
|
|
const accounts = this.store.getState().accounts
|
2017-02-03 07:05:06 +01:00
|
|
|
accounts[address] = {}
|
2017-09-26 23:15:16 +02:00
|
|
|
this.store.updateState({ accounts })
|
2017-02-03 07:05:06 +01:00
|
|
|
if (!this._currentBlockNumber) return
|
|
|
|
this._updateAccount(address)
|
|
|
|
}
|
2017-01-04 22:56:21 +01:00
|
|
|
|
2017-02-03 07:05:06 +01:00
|
|
|
removeAccount (address) {
|
2017-09-26 23:15:16 +02:00
|
|
|
const accounts = this.store.getState().accounts
|
2017-02-03 07:05:06 +01:00
|
|
|
delete accounts[address]
|
2017-09-26 23:15:16 +02:00
|
|
|
this.store.updateState({ accounts })
|
2017-02-03 07:05:06 +01:00
|
|
|
}
|
2017-01-04 22:56:21 +01:00
|
|
|
|
2017-02-03 07:05:06 +01:00
|
|
|
//
|
|
|
|
// private
|
|
|
|
//
|
|
|
|
|
|
|
|
_updateForBlock (block) {
|
2017-09-27 21:33:00 +02:00
|
|
|
this._currentBlockNumber = block.number
|
2017-09-27 22:58:54 +02:00
|
|
|
const currentBlockGasLimit = block.gasLimit
|
2017-09-22 22:33:53 +02:00
|
|
|
|
2017-09-27 21:09:32 +02:00
|
|
|
this.store.updateState({ currentBlockGasLimit })
|
2017-09-26 22:36:41 +02:00
|
|
|
|
2017-02-03 07:05:06 +01:00
|
|
|
async.parallel([
|
|
|
|
this._updateAccounts.bind(this),
|
|
|
|
], (err) => {
|
|
|
|
if (err) return console.error(err)
|
2017-09-26 23:15:16 +02:00
|
|
|
this.emit('block', this.store.getState())
|
2017-02-03 07:05:06 +01:00
|
|
|
})
|
|
|
|
}
|
2017-01-04 22:56:21 +01:00
|
|
|
|
2017-02-03 07:32:00 +01:00
|
|
|
_updateAccounts (cb = noop) {
|
2017-09-26 23:15:16 +02:00
|
|
|
const accounts = this.store.getState().accounts
|
2017-02-03 07:05:06 +01:00
|
|
|
const addresses = Object.keys(accounts)
|
|
|
|
async.each(addresses, this._updateAccount.bind(this), cb)
|
|
|
|
}
|
2017-01-04 22:56:21 +01:00
|
|
|
|
2017-02-03 07:32:00 +01:00
|
|
|
_updateAccount (address, cb = noop) {
|
2017-02-03 07:05:06 +01:00
|
|
|
this._getAccount(address, (err, result) => {
|
|
|
|
if (err) return cb(err)
|
|
|
|
result.address = address
|
2017-09-26 23:15:16 +02:00
|
|
|
const accounts = this.store.getState().accounts
|
2017-02-03 07:05:06 +01:00
|
|
|
// only populate if the entry is still present
|
|
|
|
if (accounts[address]) {
|
|
|
|
accounts[address] = result
|
2017-09-26 23:15:16 +02:00
|
|
|
this.store.updateState({ accounts })
|
2017-02-03 07:05:06 +01:00
|
|
|
}
|
|
|
|
cb(null, result)
|
|
|
|
})
|
|
|
|
}
|
2017-01-04 22:56:21 +01:00
|
|
|
|
2017-02-03 07:32:00 +01:00
|
|
|
_getAccount (address, cb = noop) {
|
2017-02-03 07:05:06 +01:00
|
|
|
const query = this._query
|
|
|
|
async.parallel({
|
|
|
|
balance: query.getBalance.bind(query, address),
|
|
|
|
nonce: query.getTransactionCount.bind(query, address),
|
|
|
|
code: query.getCode.bind(query, address),
|
|
|
|
}, cb)
|
|
|
|
}
|
2017-01-04 22:56:21 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-26 23:15:16 +02:00
|
|
|
module.exports = AccountTracker
|