Skip to content

Commit

Permalink
unnest length checks, uncomment logger ouptuts, increase stat log int…
Browse files Browse the repository at this point in the history
…erval to 10s
  • Loading branch information
ryanio committed Aug 20, 2021
1 parent 13922f0 commit 63afbc1
Showing 1 changed file with 43 additions and 38 deletions.
81 changes: 43 additions & 38 deletions packages/client/lib/service/txpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type TxPoolObject = {
}

const TX_RETRIEVAL_LIMIT = 256
const LOG_STATISTICS_INTERVAL = 3000 // ms
const LOG_STATISTICS_INTERVAL = 10000 // ms

/**
* @module service
Expand Down Expand Up @@ -75,46 +75,51 @@ export class TxPool {
* @param peer peer
*/
async announced(txHashes: Buffer[], peer: Peer) {
if (txHashes.length) {
//this.config.logger.info(`Tx pool: received new pooled tx hashes number=${txHashes.length}`)

const reqHashes = []
for (const txHash of txHashes) {
const txHashStr = txHash.toString('hex')
if (txHashStr in this.pending || txHashStr in this.handled) {
continue
}
reqHashes.push(txHash)
if (txHashes.length === 0) {
return
}
this.config.logger.info(`TxPool: received new pooled hashes number=${txHashes.length}`)

const reqHashes = []
for (const txHash of txHashes) {
const txHashStr = txHash.toString('hex')
if (txHashStr in this.pending || txHashStr in this.handled) {
continue
}
reqHashes.push(txHash)
}

if (reqHashes.length === 0) {
return
}

if (reqHashes.length > 0) {
const reqHashesStr = reqHashes.map((hash) => hash.toString('hex'))
this.pending.concat(reqHashesStr)
//console.log(`txHashes: ${txHashes.length} reqHashes: ${reqHashes.length} pending: ${this.pending.length}`)
const txsResult = await (peer!.eth as EthProtocolMethods).getPooledTransactions({
hashes: reqHashes.slice(0, TX_RETRIEVAL_LIMIT),
})

// Remove from pending list regardless if tx is in result
for (const reqHashStr of reqHashesStr) {
this.pending = this.pending.filter((hash) => hash !== reqHashStr)
}

// this.config.logger.info(`Tx pool: received txs number=${txsResult[1].length}`)
for (const tx of txsResult[1]) {
const sender = tx.getSenderAddress().toString()
const inPool = this.pool.get(sender)
let add: TxPoolObject[] = []
if (inPool) {
// Replace pooled txs with the same nonce
add = inPool.filter((poolObj) => !poolObj.tx.nonce.eq(tx.nonce))
}
add.push({ tx, added: Date.now() })

this.pool.set(tx.getSenderAddress().toString(), add)
this.handled.push(tx.hash().toString('hex'))
}
const reqHashesStr = reqHashes.map((hash) => hash.toString('hex'))
this.pending.concat(reqHashesStr)
this.config.logger.info(
`TxPool: requesting txs number=${reqHashes.length} pending=${this.pending.length}`
)
const txsResult = await (peer!.eth as EthProtocolMethods).getPooledTransactions({
hashes: reqHashes.slice(0, TX_RETRIEVAL_LIMIT),
})

// Remove from pending list regardless if tx is in result
for (const reqHashStr of reqHashesStr) {
this.pending = this.pending.filter((hash) => hash !== reqHashStr)
}

this.config.logger.info(`TxPool: received txs number=${txsResult[1].length}`)
for (const tx of txsResult[1]) {
const sender = tx.getSenderAddress().toString()
const inPool = this.pool.get(sender)
let add: TxPoolObject[] = []
if (inPool) {
// Replace pooled txs with the same nonce
add = inPool.filter((poolObj) => !poolObj.tx.nonce.eq(tx.nonce))
}
add.push({ tx, added: Date.now() })

this.pool.set(tx.getSenderAddress().toString(), add)
this.handled.push(tx.hash().toString('hex'))
}
}

Expand Down

0 comments on commit 63afbc1

Please sign in to comment.