Skip to content

Commit

Permalink
Merge pull request #93 from ava-labs/fix/mempool-limit
Browse files Browse the repository at this point in the history
Fix mempool limit
  • Loading branch information
rajranjan0608 authored May 3, 2023
2 parents cf28f22 + 719257c commit e4614c8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion client/src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"banner": "/banner.webp",
"apiBaseEndpointProduction": "/api/",
"apiBaseEndpointDevelopment": "http://localhost:8000/api/",
"apiTimeout": 10000,
"apiTimeout": 20000,
"CAPTCHA": {
"siteKey": "6LerTgYgAAAAALa7WiJs3q0pM30PH6JH4oDi_DaK",
"v2siteKey": "6LcPmIYgAAAAADKWHw28ercYsZV7QbDMz_SUeK-Q",
Expand Down
34 changes: 29 additions & 5 deletions vms/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { calculateBaseUnit } from './utils'
import Log from './Log'
import ERC20Interface from './ERC20Interface.json'
import { ChainType, SendTokenResponse, RequestType } from './evmTypes'

// cannot issue tx if no. of pending requests is > 16
const MEMPOOL_LIMIT = 15

export default class EVM {
web3: any
account: any
Expand All @@ -25,6 +29,7 @@ export default class EVM {
recalibrate: boolean
waitingForRecalibration: boolean
waitArr: any[]
preMempoolQueue: any[]
queue: any[]
error: boolean
log: Log
Expand Down Expand Up @@ -58,6 +63,7 @@ export default class EVM {
this.waitingForRecalibration = false

this.waitArr = []
this.preMempoolQueue = []
this.queue = []

this.error = false
Expand All @@ -68,6 +74,16 @@ export default class EVM {
setInterval(() => {
this.recalibrateNonceAndBalance()
}, this.RECALIBRATE * 1000)

setInterval(() => {
this.emptyPreMempoolQueue()
}, 300)
}

emptyPreMempoolQueue() {
if (this.preMempoolQueue.length > 0 && this.pendingTxNonces.size < MEMPOOL_LIMIT) {
this.putInQueue(this.preMempoolQueue.shift())
}
}

// Setup Legacy or EIP1559 transaction type
Expand Down Expand Up @@ -110,11 +126,13 @@ export default class EVM {
}
}

this.processRequest({ receiver, amount, id })
const requestId = Math.random().toString()

this.processRequest({ receiver, amount, id, requestId })

// After transaction is being processed, the nonce will be available and txHash can be returned to user
const waitingForNonce = setInterval(async () => {
if (this.hasNonce.get(receiver+id) != undefined) {
if (this.hasNonce.get(receiver+id+requestId) != undefined) {
clearInterval(waitingForNonce)

const nonce: number | undefined = this.hasNonce.get(receiver + id)
Expand Down Expand Up @@ -188,7 +206,7 @@ export default class EVM {
await this.fetchERC20Balance()

this.balance = new BN(this.balance)

this.error && this.log.info("RPC server recovered!")
this.error = false

Expand Down Expand Up @@ -223,9 +241,15 @@ export default class EVM {
}

async putInQueue(req: RequestType): Promise<void> {
if (this.pendingTxNonces.size >= MEMPOOL_LIMIT) {
// push to pre-mempool queue
this.preMempoolQueue.push(req)
return
}

if (this.balanceCheck(req)) {
this.queue.push({ ...req, nonce: this.nonce })
this.hasNonce.set(req.receiver+req.id, this.nonce)
this.hasNonce.set(req.receiver+req.id+req.requestId, this.nonce)
this.nonce++
this.executeQueue()
} else {
Expand All @@ -252,7 +276,7 @@ export default class EVM {
const timeout = setTimeout(() => {
this.log.error(`Timeout reached for transaction with nonce ${nonce}`)
this.pendingTxNonces.delete(nonce)
}, 10*1000)
}, 20*1000)

await this.web3.eth.sendSignedTransaction(rawTransaction)
this.pendingTxNonces.delete(nonce)
Expand Down
3 changes: 2 additions & 1 deletion vms/evmTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ export type SendTokenResponse = {
export type RequestType = {
receiver: string,
amount: BN | number,
id?: string
id?: string,
requestId?: string,
}

0 comments on commit e4614c8

Please sign in to comment.