better way to listen and log new blocks

pull/5370/head
yann300 2 years ago committed by Aniket
parent 609e6c4438
commit 3cde784cf5
  1. 39
      libs/remix-lib/src/execution/txListener.ts

@ -1,5 +1,4 @@
'use strict' 'use strict'
import { each } from 'async'
import { ethers } from 'ethers' import { ethers } from 'ethers'
import { toBuffer, addHexPrefix } from 'ethereumjs-util' import { toBuffer, addHexPrefix } from 'ethereumjs-util'
import { EventManager } from '../eventManager' import { EventManager } from '../eventManager'
@ -122,7 +121,7 @@ export class TxListener {
if (this._loopId) { if (this._loopId) {
clearInterval(this._loopId) clearInterval(this._loopId)
} }
this._listenOnNetwork ? this._startListenOnNetwork() : this.stopListening() this._listenOnNetwork ? this.startListening() : this.stopListening()
} }
/** /**
@ -162,22 +161,31 @@ export class TxListener {
async _startListenOnNetwork () { async _startListenOnNetwork () {
let lastSeenBlock = this.executionContext.lastBlock?.number let lastSeenBlock = this.executionContext.lastBlock?.number
let processingBlock = false
this._loopId = setInterval(() => { this._loopId = setInterval(() => {
if (processingBlock) return
processingBlock = true
const currentLoopId = this._loopId const currentLoopId = this._loopId
if (this._loopId === null) return if (this._loopId === null) {
processingBlock = false
return
}
if (!lastSeenBlock) { if (!lastSeenBlock) {
lastSeenBlock = this.executionContext.lastBlock?.number // trying to resynchronize lastSeenBlock = this.executionContext.lastBlock?.number // trying to resynchronize
console.log('listen on blocks, resynchronising') console.log('listen on blocks, resynchronising')
processingBlock = false
return return
} }
const current = this.executionContext.lastBlock?.number const current = this.executionContext.lastBlock?.number
if (!current) { if (!current) {
console.log(new Error('no last block found')) console.log(new Error('no last block found'))
processingBlock = false
return return
} }
if (currentLoopId === this._loopId && lastSeenBlock < current) { if (currentLoopId === this._loopId && lastSeenBlock < current) {
while (lastSeenBlock <= current) { while (lastSeenBlock <= current) {
try { try {
if (!this._isListening) break
this._manageBlock(lastSeenBlock) this._manageBlock(lastSeenBlock)
} catch (e) { } catch (e) {
console.log(e) console.log(e)
@ -186,7 +194,8 @@ export class TxListener {
} }
lastSeenBlock = current lastSeenBlock = current
} }
}, 10000) processingBlock = false
}, 20000)
} }
_manageBlock (blockNumber) { _manageBlock (blockNumber) {
@ -225,24 +234,32 @@ export class TxListener {
}) })
} }
_resolve (transactions, callback) { _resolveAsync (tx) {
each(transactions, (tx, cb) => { return new Promise((resolve, reject) => {
this._api.resolveReceipt(tx, (error, receipt) => { this._api.resolveReceipt(tx, (error, receipt) => {
if (error) return cb(error) if (error) return reject(error)
this._resolveTx(tx, receipt, (error, resolvedData) => { this._resolveTx(tx, receipt, (error, resolvedData) => {
if (error) cb(error) if (error) return reject(error)
if (resolvedData) { if (resolvedData) {
this.event.trigger('txResolved', [tx, receipt, resolvedData]) this.event.trigger('txResolved', [tx, receipt, resolvedData])
} }
this.event.trigger('newTransaction', [tx, receipt]) this.event.trigger('newTransaction', [tx, receipt])
cb() resolve({})
}) })
}) })
}, () => {
callback()
}) })
} }
async _resolve (transactions, callback) {
for (const tx of transactions) {
try {
if (!this._isListening) break
await this._resolveAsync(tx)
} catch (e) {}
}
callback()
}
_resolveTx (tx, receipt, cb) { _resolveTx (tx, receipt, cb) {
const contracts = this._api.contracts() const contracts = this._api.contracts()
if (!contracts) return cb() if (!contracts) return cb()

Loading…
Cancel
Save