aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers/recent-blocks.js
blob: 0c1ee4e38e2f80fe794a647e33a576e4bc6d297e (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const ObservableStore = require('obs-store')
const extend = require('xtend')
const BN = require('ethereumjs-util').BN
const EthQuery = require('eth-query')
const log = require('loglevel')

class RecentBlocksController {

  constructor (opts = {}) {
    const { blockTracker, provider } = opts
    this.blockTracker = blockTracker
    this.ethQuery = new EthQuery(provider)
    this.historyLength = opts.historyLength || 40

    const initState = extend({
      recentBlocks: [],
    }, opts.initState)
    this.store = new ObservableStore(initState)

    this.blockTracker.on('block', this.processBlock.bind(this))
    this.backfill()
  }

  resetState () {
    this.store.updateState({
      recentBlocks: [],
    })
  }

  processBlock (newBlock) {
    const block = this.mapTransactionsToPrices(newBlock)

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

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

    this.store.updateState(state)
  }

  backfillBlock (newBlock) {
    const block = this.mapTransactionsToPrices(newBlock)

    const state = this.store.getState()

    if (state.recentBlocks.length < this.historyLength) {
      state.recentBlocks.unshift(block)
    }

    this.store.updateState(state)
  }

  mapTransactionsToPrices (newBlock) {
    const block = extend(newBlock, {
      gasPrices: newBlock.transactions.map((tx) => {
        return tx.gasPrice
      }),
    })
    delete block.transactions
    return block
  }

  async backfill() {
    this.blockTracker.once('block', async (block) => {
      let blockNum = block.number
      let recentBlocks
      let state = this.store.getState()
      recentBlocks = state.recentBlocks

      while (recentBlocks.length < this.historyLength) {
        try {
          let blockNumBn = new BN(blockNum.substr(2), 16)
          const newNum = blockNumBn.subn(1).toString(10)
          const newBlock = await this.getBlockByNumber(newNum)

          if (newBlock) {
            this.backfillBlock(newBlock)
            blockNum = newBlock.number
          }

          state = this.store.getState()
          recentBlocks = state.recentBlocks
        } catch (e) {
          log.error(e)
        }
        await this.wait()
      }
    })
  }

  async wait () {
    return new Promise((resolve) => {
      setTimeout(resolve, 100)
    })
  }

  async getBlockByNumber (number) {
    const bn = new BN(number)
    return new Promise((resolve, reject) => {
      this.ethQuery.getBlockByNumber('0x' + bn.toString(16), true, (err, block) => {
        if (err) reject(err)
        resolve(block)
      })
    })
  }

}

module.exports = RecentBlocksController