aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers/recent-blocks.js
blob: 4a906261eeade6dfc9a5567816758d78aafbbce6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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) {
    const block = extend(newBlock, {
      gasPrices: newBlock.transactions.map((tx) => {
        return tx.gasPrice
      }),
    })
    delete block.transactions

    const state = this.store.getState()
    state.recentBlocks.push(block)

    while (state.recentBlocks.length > this.historyLength) {
      state.recentBlocks.shift()
    }

    this.store.updateState(state)
  }
}

module.exports = RecentBlocksController