Merge pull request #1241 from poanetwork/1204-realtime-blocks-issue

Handle new blocks older than last block
pull/1257/head
Andrew Cravenho 6 years ago committed by GitHub
commit 3e3c9198ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      apps/block_scout_web/assets/__tests__/pages/blocks.js
  2. 7
      apps/block_scout_web/assets/js/pages/blocks.js

@ -103,6 +103,27 @@ describe('RECEIVED_NEW_BLOCK', () => {
'<div data-block-number="4"></div>' '<div data-block-number="4"></div>'
]) ])
}) })
test('replaces duplicated block older than last one', () => {
const state = Object.assign({}, initialState, {
items: [
'<div data-block-number="5"></div>',
'<div data-block-number="4"></div>'
],
blockType: 'block'
})
const action = {
type: 'RECEIVED_NEW_BLOCK',
msg: {
blockHtml: '<div data-block-number="4" class="new"></div>',
}
}
const output = reducer(state, action)
expect(output.items).toEqual([
'<div data-block-number="5"></div>',
'<div data-block-number="4" class="new"></div>'
])
})
test('skips if new block height is lower than lowest on page', () => { test('skips if new block height is lower than lowest on page', () => {
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
items: [ items: [

@ -56,15 +56,16 @@ function withMissingBlocks (reducer) {
if (result.items.length < 2) return result if (result.items.length < 2) return result
const maxBlock = getBlockNumber(_.first(result.items))
const minBlock = getBlockNumber(_.last(result.items))
const blockNumbersToItems = result.items.reduce((acc, item) => { const blockNumbersToItems = result.items.reduce((acc, item) => {
const blockNumber = getBlockNumber(item) const blockNumber = getBlockNumber(item)
acc[blockNumber] = acc[blockNumber] || item acc[blockNumber] = acc[blockNumber] || item
return acc return acc
}, {}) }, {})
const blockNumbers = _(blockNumbersToItems).keys().map(x => parseInt(x, 10)).value()
const minBlock = _.min(blockNumbers)
const maxBlock = _.max(blockNumbers)
return Object.assign({}, result, { return Object.assign({}, result, {
items: _.rangeRight(minBlock, maxBlock + 1) items: _.rangeRight(minBlock, maxBlock + 1)
.map((blockNumber) => blockNumbersToItems[blockNumber] || placeHolderBlock(blockNumber)) .map((blockNumber) => blockNumbersToItems[blockNumber] || placeHolderBlock(blockNumber))

Loading…
Cancel
Save