Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Node Sync in Block Explorer #587

Merged
merged 5 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app/src/renderer/components/common/NiDataLoading.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<template lang="pug">
data-msg(icon="rotate_right" spin="true")
div(slot="title") Data is loading&hellip;
div(slot="title") {{ title }}
div(slot="subtitle") Please wait a moment.
</template>

<script>
import DataMsg from 'common/NiDataMsg'
export default {
name: 'ni-data-loading',
components: { DataMsg }
components: { DataMsg },
props: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea

title: {
type: String,
default: 'Data is loading…'
}
}
}
</script>
3 changes: 2 additions & 1 deletion app/src/renderer/components/monitor/PageBlocks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ page(title='Block Explorer')

modal-search(type="blocks")

data-loading(v-if="!blockchain.subscription")
data-loading(v-if="blockchain.syncing" title="Node is syncing blockchain…")
data-loading(v-if="!blockchain.syncing && !blockchain.subscription")

part(title='Current Block' v-if="blockchain.subscription")
list-item(dt='Block Height' :dd='num.prettyInt(lastHeader.height)' :to="{ name: 'block', params: { block: lastHeader.height} }")
Expand Down
37 changes: 28 additions & 9 deletions app/src/renderer/vuex/modules/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default ({ commit, node }) => {
blockHeight: null, // we remember the height so we can requery the block, if querying failed
blockLoading: false,
subscription: false,
syncing: true,
blockMetas: []
}

Expand Down Expand Up @@ -69,21 +70,39 @@ export default ({ commit, node }) => {
blockMetaInfo && state.blockMetas.push(blockMetaInfo)
return blockMetaInfo
},
subscribeToBlocks ({ commit }) {
node.rpc.subscribe({ query: "tm.event = 'NewBlock'" }, (err, event) => {
state.subscription = true
subscribeToBlocks ({ state, commit, dispatch }) {
// ensure we never subscribe twice
if (state.subscription) return

if (err) {
function error (err) {
state.subscription = false
commit('notifyError', { title: `Error subscribing to new blocks`, body: err.message })
}

node.rpc.status((err, status) => {
if (err) return error(err)

if (status.syncing) {
// still syncing, let's try subscribing again in 30 seconds
state.syncing = true
state.subscription = false
commit('notifyError', { title: `Error subscribing to new blocks`, body: err.message })
setTimeout(() => dispatch('subscribeToBlocks'), 30e3)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

30e3? never seen that before. can't find anything on google about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scientific notation for 30000, maybe I should do 30 * 1000 so it's more obvious.

return
}

state.blocks.unshift(event.data.data.block)
state.syncing = false

if (state.blocks.length === 20) {
state.blocks.pop()
}
node.rpc.subscribe({ query: "tm.event = 'NewBlock'" }, (err, event) => {
state.subscription = true

if (err) return error(err)

state.blocks.unshift(event.data.data.block)

if (state.blocks.length === 20) {
state.blocks.pop()
}
})
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ exports[`PageBlocks has the expected html structure 1`] = `
<main class=\\"ni-page-main ps\\">
<modal-search type=\\"blocks\\"></modal-search>
<!---->
<!---->
<section class=\\"ni-part\\">
<div class=\\"ni-part-container\\">
<header class=\\"ni-part-header\\">
Expand Down
18 changes: 18 additions & 0 deletions test/unit/specs/store/blockchain.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,22 @@ describe('Module: Blockchain', () => {
expect(store.state.blockchain.blocks.length).toBe(0)
expect(store.state.notifications[0].title).toContain(`Error subscribing to new blocks`)
})

it('should not subscribe if still syncing', async () => {
node.rpc.status = (cb) => {
cb(null, { syncing: true })
}
node.rpc.subscribe = jest.fn()
store.dispatch('subscribeToBlocks')
expect(node.rpc.subscribe.mock.calls.length).toBe(0)
})

it('should subscribe if not syncing', async () => {
node.rpc.status = (cb) => {
cb(null, { syncing: false })
}
node.rpc.subscribe = jest.fn()
store.dispatch('subscribeToBlocks')
expect(node.rpc.subscribe.mock.calls.length).toBe(1)
})
})