Skip to content

Commit

Permalink
Move RetryPolicies to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
ligi committed Jan 9, 2022
1 parent 57b706e commit 5471a27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/main/kotlin/org/komputing/fauceth/TransactionSender.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.komputing.fauceth

import com.github.michaelbull.retry.ContinueRetrying
import com.github.michaelbull.retry.StopRetrying
import com.github.michaelbull.retry.policy.RetryPolicy
import com.github.michaelbull.retry.policy.decorrelatedJitterBackoff
import com.github.michaelbull.retry.policy.limitAttempts
import com.github.michaelbull.retry.policy.plus
Expand All @@ -19,10 +16,11 @@ import org.kethereum.model.Address
import org.kethereum.model.ChainId
import org.kethereum.model.createEmptyTransaction
import org.kethereum.rpc.EthereumRPCException
import org.komputing.fauceth.util.handle1559NotAvailable
import org.komputing.fauceth.util.log
import org.komputing.fauceth.util.noRetryWhenKnown
import org.walleth.khex.toHexString
import java.io.IOException
import java.lang.IllegalStateException
import java.math.BigDecimal
import java.math.BigInteger
import java.math.BigInteger.*
Expand All @@ -46,9 +44,6 @@ suspend fun sendTransaction(address: Address, txChain: ExtendedChainInfo): Strin
txChain.rpc.estimateGas(tx) ?: throw EthereumRPCException("Could not estimate gas limit", 404)
}
if (txChain.useEIP1559) {
val handle1559NotAvailable: RetryPolicy<Throwable> = {
if (reason is EthereumRPCException && (reason.message == "the method eth_feeHistory does not exist/is not available") || (reason.message == "rpc method is not whitelisted")) StopRetrying else ContinueRetrying
}

if (tx.maxPriorityFeePerGas == null || System.currentTimeMillis() - (tx.creationEpochSecond ?: System.currentTimeMillis()) > 20000) {
try {
Expand Down Expand Up @@ -106,9 +101,6 @@ suspend fun sendTransaction(address: Address, txChain: ExtendedChainInfo): Strin
txHashList.add(hash.toHexString())

try {
val noRetryWhenKnown: RetryPolicy<Throwable> = {
if (reason is EthereumRPCException && reason.message == "already known") StopRetrying else ContinueRetrying
}

retry(limitAttempts(5) + noRetryWhenKnown + decorrelatedJitterBackoff(base = 10L, max = 5000L)) {
val res = txChain.rpc.sendRawTransaction(encodedTransaction.toHexString())
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/org/komputing/fauceth/util/RetryPolicies.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.komputing.fauceth.util

import com.github.michaelbull.retry.ContinueRetrying
import com.github.michaelbull.retry.StopRetrying
import com.github.michaelbull.retry.policy.RetryPolicy
import org.kethereum.rpc.EthereumRPCException

val handle1559NotAvailable: RetryPolicy<Throwable> = {
if (reason is EthereumRPCException &&
(reason.message == "the method eth_feeHistory does not exist/is not available") ||
(reason.message == "rpc method is not whitelisted")
) StopRetrying else ContinueRetrying
}

val noRetryWhenKnown: RetryPolicy<Throwable> = {
if (reason is EthereumRPCException && reason.message == "already known") StopRetrying else ContinueRetrying
}

0 comments on commit 5471a27

Please sign in to comment.