2017-12-18 01:36:03 +01:00
|
|
|
const ObservableStore = require('obs-store')
|
|
|
|
const extend = require('xtend')
|
|
|
|
|
|
|
|
class RecentBlocksController {
|
|
|
|
|
|
|
|
constructor (opts = {}) {
|
|
|
|
const { blockTracker } = opts
|
|
|
|
this.blockTracker = blockTracker
|
|
|
|
this.historyLength = opts.historyLength || 40
|
|
|
|
|
|
|
|
const initState = extend({
|
|
|
|
recentBlocks: [],
|
|
|
|
}, opts.initState)
|
|
|
|
this.store = new ObservableStore(initState)
|
|
|
|
|
|
|
|
this.blockTracker.on('block', this.processBlock.bind(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
resetState () {
|
|
|
|
this.store.updateState({
|
|
|
|
recentBlocks: [],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
processBlock (newBlock) {
|
2017-12-19 21:22:48 +01:00
|
|
|
const block = extend(newBlock, {
|
|
|
|
gasPrices: newBlock.transactions.map((tx) => {
|
|
|
|
return tx.gasPrice
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
delete block.transactions
|
|
|
|
|
2017-12-18 01:36:03 +01:00
|
|
|
const state = this.store.getState()
|
2017-12-19 21:22:48 +01:00
|
|
|
state.recentBlocks.push(block)
|
2017-12-18 01:36:03 +01:00
|
|
|
|
|
|
|
while (state.recentBlocks.length > this.historyLength) {
|
|
|
|
state.recentBlocks.shift()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.updateState(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RecentBlocksController
|