Skip to content

Commit

Permalink
fix: apply fibonacci delay and threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
cazala committed Jan 13, 2025
1 parent e81c108 commit 478aeda
Showing 1 changed file with 52 additions and 9 deletions.
61 changes: 52 additions & 9 deletions src/modules/transaction/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ const BLOCKS_DEPTH = 100
const TRANSACTION_FETCH_RETIES = 120
const PENDING_TRANSACTION_THRESHOLD = 72 * 60 * 60 * 1000 // 72 hours
const REVERTED_TRANSACTION_THRESHOLD = 24 * 60 * 60 * 1000 // 24 hours
const TRANSACTION_FETCH_DELAY = 2 * 1000 // 2 seconds
const DROPPED_TRANSACTION_THRESHOLD = 24 * 60 * 60 * 1000 // 24 hours
const INITIAL_BACKOFF_DELAY = 2 * 1000 // 2 seconds

const isExpired = (transaction: Transaction, threshold: number) =>
Date.now() - transaction.timestamp > threshold
Expand Down Expand Up @@ -128,6 +129,7 @@ function* handleCrossChainTransactionRequest(

let statusResponse: StatusResponse | undefined
let txInState: Transaction
let attempt = 0
let squidNotFoundRetries: number =
config.crossChainProviderNotFoundRetries ?? TRANSACTION_FETCH_RETIES
while (
Expand Down Expand Up @@ -175,7 +177,10 @@ function* handleCrossChainTransactionRequest(
)
return
}
yield delay(config.crossChainProviderRetryDelay ?? TRANSACTION_FETCH_DELAY)

const fibonacciDelay: number = yield call(getFibonacciDelay, attempt)
yield delay(config.crossChainProviderRetryDelay ?? fibonacciDelay)
attempt++
}

txInState = yield select(state =>
Expand Down Expand Up @@ -222,6 +227,7 @@ function* handleRegularTransactionRequest(

try {
watchPendingIndex[hash] = true
let attempt = 0

let tx: AnyTransaction = yield call(
getTransactionFromChain,
Expand Down Expand Up @@ -266,8 +272,10 @@ function* handleRegularTransactionRequest(
yield put(updateTransactionStatus(hash, statusInNetwork))
}

// sleep
yield delay(TRANSACTION_FETCH_DELAY)
// Apply fibonacci backoff delay before next iteration
const fibonacciDelay: number = yield call(getFibonacciDelay, attempt)
yield delay(fibonacciDelay)
attempt++

// update tx status from network
tx = yield call(
Expand Down Expand Up @@ -309,6 +317,19 @@ function* handleRegularTransactionRequest(
}
}

function* getFibonacciDelay(attempt: number) {
if (attempt <= 1) return INITIAL_BACKOFF_DELAY

let prev = 1
let current = 1
for (let i = 2; i <= attempt; i++) {
const next = prev + current
prev = current
current = next
}
return current * INITIAL_BACKOFF_DELAY
}

function* handleReplaceTransactionRequest(
action: ReplaceTransactionRequestAction
) {
Expand All @@ -321,10 +342,25 @@ function* handleReplaceTransactionRequest(
return
}

// Check if transaction is already expired before starting to poll
if (isExpired(transaction, DROPPED_TRANSACTION_THRESHOLD)) {
yield put(updateTransactionStatus(hash, TransactionStatus.DROPPED))
return
}

let checkpoint = null
let attempt = 0
watchDroppedIndex[hash] = true

const startTime = Date.now()

while (true) {
// Check if we've exceeded the time threshold during polling
if (Date.now() - startTime > DROPPED_TRANSACTION_THRESHOLD) {
yield put(updateTransactionStatus(hash, TransactionStatus.DROPPED))
break
}

const eth: ethers.providers.Web3Provider = yield call(
getNetworkWeb3Provider,
transaction.chainId
Expand Down Expand Up @@ -398,16 +434,18 @@ function* handleReplaceTransactionRequest(
break
}

// if there was nonce higher to than the one in the tx, we can mark it as replaced (altough we don't know which tx replaced it)
// if there was nonce higher to than the one in the tx, we can mark it as replaced (although we don't know which tx replaced it)
if (highestNonce >= nonce) {
yield put(
updateTransactionStatus(action.payload.hash, TransactionStatus.REPLACED)
)
break
}

// sleep
yield delay(TRANSACTION_FETCH_DELAY)
// Apply fibonacci backoff delay before next iteration
const fibonacciDelay: number = yield call(getFibonacciDelay, attempt)
yield delay(fibonacciDelay)
attempt++
}

delete watchDroppedIndex[action.payload.hash]
Expand Down Expand Up @@ -440,7 +478,8 @@ function* handleWatchDroppedTransactions() {
const droppedTransactions = transactions.filter(
transaction =>
transaction.status === TransactionStatus.DROPPED &&
transaction.nonce != null
transaction.nonce != null &&
!isExpired(transaction, DROPPED_TRANSACTION_THRESHOLD)
)

for (const tx of droppedTransactions) {
Expand Down Expand Up @@ -468,9 +507,13 @@ function* handleWatchRevertedTransaction(
}

const address: string = yield select(state => getAddress(state))
let attempt = 0

do {
yield delay(TRANSACTION_FETCH_DELAY)
const fibonacciDelay: number = yield call(getFibonacciDelay, attempt)
yield delay(fibonacciDelay)
attempt++

const txInNetwork: AnyTransaction | null = yield call(() =>
getTransactionFromChain(address, txInState.chainId, hash)
)
Expand Down

0 comments on commit 478aeda

Please sign in to comment.