mirror of
https://github.com/Retropex/bitfeed.git
synced 2025-06-02 23:32:31 +02:00
More efficient block loading on reconnect
This commit is contained in:
parent
f5ed1ad1d3
commit
a28c8eb205
@ -12,7 +12,7 @@
|
|||||||
import LightningOverlay from '../components/LightningOverlay.svelte'
|
import LightningOverlay from '../components/LightningOverlay.svelte'
|
||||||
import DonationBar from '../components/DonationBar.svelte'
|
import DonationBar from '../components/DonationBar.svelte'
|
||||||
import { integerFormat } from '../utils/format.js'
|
import { integerFormat } from '../utils/format.js'
|
||||||
import { exchangeRates, localCurrency } from '../stores.js'
|
import { exchangeRates, localCurrency, lastBlockId } from '../stores.js'
|
||||||
import { formatCurrency } from '../utils/fx.js'
|
import { formatCurrency } from '../utils/fx.js'
|
||||||
import config from '../config.js'
|
import config from '../config.js'
|
||||||
|
|
||||||
@ -48,7 +48,10 @@
|
|||||||
txController.addTx(tx)
|
txController.addTx(tx)
|
||||||
})
|
})
|
||||||
txStream.subscribe('block', block => {
|
txStream.subscribe('block', block => {
|
||||||
txController.addBlock(block)
|
if (block) {
|
||||||
|
const added = txController.addBlock(block)
|
||||||
|
if (added && added.id) $lastBlockId = added.id
|
||||||
|
}
|
||||||
txStream.sendMempoolRequest()
|
txStream.sendMempoolRequest()
|
||||||
})
|
})
|
||||||
txStream.subscribe('mempool_count', count => {
|
txStream.subscribe('mempool_count', count => {
|
||||||
|
@ -6,7 +6,7 @@ export default {
|
|||||||
layoutHints: false,
|
layoutHints: false,
|
||||||
fps: true,
|
fps: true,
|
||||||
websocket_path: '/ws/txs',
|
websocket_path: '/ws/txs',
|
||||||
localSocket: false,
|
localSocket: true,
|
||||||
nofeed: false,
|
nofeed: false,
|
||||||
txDelay: 10000,
|
txDelay: 10000,
|
||||||
blockTimeout: 10000,
|
blockTimeout: 10000,
|
||||||
|
@ -1,10 +1,15 @@
|
|||||||
import { serverConnected, serverDelay } from '../stores.js'
|
import { serverConnected, serverDelay, lastBlockId } from '../stores.js'
|
||||||
import config from '../config.js'
|
import config from '../config.js'
|
||||||
|
|
||||||
|
let mempoolTimer
|
||||||
|
let lastBlockSeen
|
||||||
|
lastBlockId.subscribe(val => { lastBlockSeen = val })
|
||||||
|
|
||||||
|
|
||||||
class TxStream {
|
class TxStream {
|
||||||
constructor () {
|
constructor () {
|
||||||
this.websocketUri = config.localSocket ? `ws://localhost:4000${config.websocket_path}` : (config.dev ? `wss://bits.monospace.live${config.websocket_path}` : `wss://${window.location.host}${config.websocket_path}`)
|
this.websocketUri = config.localSocket ? `ws://localhost:4000${config.websocket_path}` : (config.dev ? `wss://bits.monospace.live${config.websocket_path}` : `wss://${window.location.host}${config.websocket_path}`)
|
||||||
this.reconnectBackoff = 128
|
this.reconnectBackoff = 250
|
||||||
this.websocket = null
|
this.websocket = null
|
||||||
this.setConnected(false)
|
this.setConnected(false)
|
||||||
this.setDelay(0)
|
this.setDelay(0)
|
||||||
@ -54,7 +59,7 @@ class TxStream {
|
|||||||
if (this.reconnectBackoff) clearTimeout(this.reconnectBackoff)
|
if (this.reconnectBackoff) clearTimeout(this.reconnectBackoff)
|
||||||
if (!this.connected) {
|
if (!this.connected) {
|
||||||
console.log('......trying to reconnect websocket')
|
console.log('......trying to reconnect websocket')
|
||||||
if (this.reconnectBackoff < 8000) this.reconnectBackoff *= 2
|
if (this.reconnectBackoff < 8000) this.reconnectBackoff *= (Math.random()+1)
|
||||||
this.reconnectTimeout = setTimeout(() => { this.init() }, this.reconnectBackoff)
|
this.reconnectTimeout = setTimeout(() => { this.init() }, this.reconnectBackoff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,11 +89,14 @@ class TxStream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sendBlockRequest () {
|
sendBlockRequest () {
|
||||||
this.websocket.send('block')
|
console.log('Checking for missed blocks...')
|
||||||
|
this.websocket.send(JSON.stringify({method: 'get_block', last: lastBlockSeen }))
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMempoolRequest () {
|
sendMempoolRequest () {
|
||||||
this.websocket.send('count')
|
this.websocket.send('count')
|
||||||
|
if (mempoolTimer) clearTimeout(mempoolTimer)
|
||||||
|
mempoolTimer = setTimeout(() => { this.sendMempoolRequest() }, 60000)
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnect () {
|
disconnect () {
|
||||||
@ -130,8 +138,7 @@ class TxStream {
|
|||||||
} else if (msg && msg.type === 'txn') {
|
} else if (msg && msg.type === 'txn') {
|
||||||
window.dispatchEvent(new CustomEvent('bitcoin_tx', { detail: msg.txn }))
|
window.dispatchEvent(new CustomEvent('bitcoin_tx', { detail: msg.txn }))
|
||||||
} else if (msg && msg.type === 'block') {
|
} else if (msg && msg.type === 'block') {
|
||||||
// console.log('Block received: ', msg.block)
|
if (msg.block && msg.block.id) window.dispatchEvent(new CustomEvent('bitcoin_block', { detail: msg.block }))
|
||||||
window.dispatchEvent(new CustomEvent('bitcoin_block', { detail: msg.block }))
|
|
||||||
} else {
|
} else {
|
||||||
// console.log('unknown message from websocket: ', msg)
|
// console.log('unknown message from websocket: ', msg)
|
||||||
}
|
}
|
||||||
|
@ -54,6 +54,7 @@ export const devEvents = writable({
|
|||||||
|
|
||||||
export const txQueueLength = createCounter()
|
export const txQueueLength = createCounter()
|
||||||
export const txCount = createCounter()
|
export const txCount = createCounter()
|
||||||
|
export const lastBlockId = writable(null)
|
||||||
export const mempoolCount = createCounter()
|
export const mempoolCount = createCounter()
|
||||||
export const mempoolScreenHeight = writable(0)
|
export const mempoolScreenHeight = writable(0)
|
||||||
export const frameRate = writable(null)
|
export const frameRate = writable(null)
|
||||||
|
Loading…
Reference in New Issue
Block a user