Skip to content

Commit

Permalink
coordinator: add Web3jClientManager to improve test code reuse (#85)
Browse files Browse the repository at this point in the history
coordinator: add Web3jClientManager to improve integration test code reuse and debugging
  • Loading branch information
jpnovais authored Sep 25, 2024
1 parent ea17789 commit d28e9bd
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 26 deletions.
2 changes: 1 addition & 1 deletion coordinator/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
implementation project(":jvm-libs:teku-execution-client")
implementation "io.vertx:vertx-core"
// jackson shall never be used in the core module
// however, it is used already :( but was as transitive throught Teku Execution Client
// however, it is used already :( but was as transitive through Teku Execution Client
// necessary atm because of Type2StateManagerClient interface
implementation "com.fasterxml.jackson.core:jackson-databind:${libs.versions.jackson.get()}"
// jackson shall never be used in the core module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import net.consensys.linea.contract.LineaRollupAsyncFriendly
import net.consensys.toBigInteger
import net.consensys.toULong
import net.consensys.zkevm.ethereum.ContractsManager
import net.consensys.zkevm.ethereum.L1AccountManager
import net.consensys.zkevm.ethereum.Web3jClientManager
import org.apache.tuweni.bytes.Bytes32
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
Expand Down Expand Up @@ -37,7 +37,7 @@ class L1EventQuerierIntegrationTest {
fun beforeEach() {
val deploymentResult = ContractsManager.get().deployLineaRollup().get()
testLineaRollupContractAddress = deploymentResult.contractAddress
web3Client = L1AccountManager.web3jClient
web3Client = Web3jClientManager.l1Client
@Suppress("DEPRECATION")
contract = deploymentResult.rollupOperatorClientLegacy
l1ContractDeploymentBlockNumber = deploymentResult.contractDeploymentBlockNumber.toULong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import net.consensys.linea.contract.LineaRollupAsyncFriendly
import net.consensys.toBigInteger
import net.consensys.zkevm.coordinator.clients.smartcontract.LineaRollupSmartContractClient
import net.consensys.zkevm.ethereum.ContractsManager
import net.consensys.zkevm.ethereum.L1AccountManager
import net.consensys.zkevm.ethereum.L2AccountManager
import net.consensys.zkevm.ethereum.Web3jClientManager
import org.apache.tuweni.bytes.Bytes32
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
Expand Down Expand Up @@ -61,8 +60,8 @@ class L2MessageAnchorerIntegrationTest {
val deploymentResult = ContractsManager.get().deployRollupAndL2MessageService().get()
testLineaRollupContractAddress = deploymentResult.lineaRollup.contractAddress
l1ContractDeploymentBlockNumber = deploymentResult.lineaRollup.contractDeploymentBlockNumber
l1Web3jClient = L1AccountManager.web3jClient
l2Web3jClient = L2AccountManager.web3jClient
l1Web3jClient = Web3jClientManager.l1Client
l2Web3jClient = Web3jClientManager.l2Client
l2TransactionManager = deploymentResult.l2MessageService.anchorerOperator.txManager
@Suppress("DEPRECATION")
l1ContractLegacyClient = deploymentResult.lineaRollup.rollupOperatorClientLegacy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import net.consensys.toBigInteger
import net.consensys.toULong
import net.consensys.zkevm.coordinator.clients.smartcontract.LineaRollupSmartContractClient
import net.consensys.zkevm.ethereum.ContractsManager
import net.consensys.zkevm.ethereum.L1AccountManager
import net.consensys.zkevm.ethereum.L2AccountManager
import net.consensys.zkevm.ethereum.Web3jClientManager
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -33,8 +32,8 @@ class MessageServiceIntegrationTest {
private val log: Logger = LogManager.getLogger(this::class.java)

private val l2RecipientAddress = "0x03dfa322A95039BB679771346Ee2dBfEa0e2B773"
private val l1Web3Client = L1AccountManager.web3jClient
private val l2Web3jClient = L2AccountManager.web3jClient
private val l1Web3Client = Web3jClientManager.l1Client
private val l2Web3jClient = Web3jClientManager.l2Client
private lateinit var l2TransactionManager: AsyncFriendlyTransactionManager

private val messagePollingInterval = 200.milliseconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import org.web3j.crypto.Credentials
import org.web3j.protocol.Web3j
import org.web3j.protocol.core.DefaultBlockParameterName
import org.web3j.protocol.core.Response
import org.web3j.protocol.http.HttpService
import org.web3j.tx.response.PollingTransactionReceiptProcessor
import org.web3j.utils.Async
import tech.pegasys.teku.infrastructure.async.SafeFuture
import java.io.File
import java.math.BigInteger
Expand Down Expand Up @@ -75,7 +73,6 @@ fun getTransactionManager(
private inline val Int.ether get(): BigInteger = BigInteger.valueOf(this.toLong()).multiply(BigInteger.TEN.pow(18))

interface AccountManager {
val web3jClient: Web3j
val chainId: Long
fun whaleAccount(): Account
fun generateAccount(initialBalanceWei: BigInteger = 10.ether): Account
Expand All @@ -85,13 +82,12 @@ interface AccountManager {
}

private open class WhaleBasedAccountManager(
rpcUrl: String,
val web3jClient: Web3j,
genesisFile: Path,
val clock: Clock = Clock.System,
val testWorkerIdProvider: () -> Long = { ProcessHandle.current().pid() },
val log: Logger = LogManager.getLogger(WhaleBasedAccountManager::class.java)
) : AccountManager {
override val web3jClient: Web3j = Web3j.build(HttpService(rpcUrl), 500, Async.defaultExecutorService())
private val whaleAccounts: List<Account>
final override val chainId: Long

Expand Down Expand Up @@ -224,13 +220,13 @@ private open class WhaleBasedAccountManager(
}

object L1AccountManager : AccountManager by WhaleBasedAccountManager(
rpcUrl = System.getProperty("L1_RPC", "http://localhost:8445"),
web3jClient = Web3jClientManager.l1Client,
genesisFile = findFile(System.getProperty("L1_GENESIS", "docker/config/l1-node/el/genesis.json")),
log = LogManager.getLogger(L1AccountManager::class.java)
)

object L2AccountManager : AccountManager by WhaleBasedAccountManager(
rpcUrl = System.getProperty("L2_RPC", "http://localhost:8545"),
web3jClient = Web3jClientManager.l2Client,
genesisFile = findFile(System.getProperty("L2_GENESIS", "docker/config/linea-local-dev-genesis.json")),
log = LogManager.getLogger(L2AccountManager::class.java)
)
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ interface ContractsManager {

fun connectL2MessageService(
contractAddress: String,
web3jClient: Web3j = L2AccountManager.web3jClient,
web3jClient: Web3j = Web3jClientManager.l2Client,
transactionManager: AsyncFriendlyTransactionManager,
gasProvider: EIP1559GasProvider = EIP1559GasProvider(
web3jClient,
Expand Down Expand Up @@ -193,7 +193,7 @@ object MakeFileDelegatedContractsManager : ContractsManager {
): LineaRollupAsyncFriendly {
return LineaRollupAsyncFriendly.load(
contractAddress,
L1AccountManager.web3jClient,
Web3jClientManager.l1Client,
transactionManager,
gasProvider,
emptyMap()
Expand All @@ -207,7 +207,7 @@ object MakeFileDelegatedContractsManager : ContractsManager {
): LineaRollupSmartContractClient {
return Web3JLineaRollupSmartContractClient.load(
contractAddress,
L1AccountManager.web3jClient,
Web3jClientManager.l1Client,
transactionManager,
gasProvider,
emptyMap()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.consensys.zkevm.ethereum

import net.consensys.linea.web3j.okHttpClientBuilder
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.web3j.protocol.Web3j
import org.web3j.protocol.http.HttpService
import org.web3j.utils.Async

/**
* Helper Object to create Web3j clients for L1 and L2
* that allow overriding default log values for testing and debugging easily
*/
object Web3jClientManager {
val l1RpcUrl: String = System.getProperty("L1_RPC", "http://localhost:8445")
val l2RpcUrl: String = System.getProperty("L2_RPC", "http://localhost:8545")
val l1Client: Web3j = buildL1Client()
val l2Client: Web3j = buildL2Client()

fun buildL1Client(
rpcUrl: String = l1RpcUrl,
log: Logger = LogManager.getLogger("test.clients.l1.web3j-default"),
requestResponseLogLevel: Level = Level.TRACE,
failuresLogLevel: Level = Level.DEBUG
): Web3j {
return buildWeb3Client(rpcUrl, log, requestResponseLogLevel, failuresLogLevel)
}

fun buildL2Client(
rpcUrl: String = l2RpcUrl,
log: Logger = LogManager.getLogger("test.clients.l2.web3j-default"),
requestResponseLogLevel: Level = Level.TRACE,
failuresLogLevel: Level = Level.DEBUG
): Web3j {
return buildWeb3Client(rpcUrl, log, requestResponseLogLevel, failuresLogLevel)
}

fun buildWeb3Client(
rpcUrl: String,
log: Logger = LogManager.getLogger("test.clients.web3j"),
requestResponseLogLevel: Level = Level.TRACE,
failuresLogLevel: Level = Level.DEBUG
): Web3j {
return Web3j.build(
HttpService(
rpcUrl,
okHttpClientBuilder(
logger = log,
requestResponseLogLevel = requestResponseLogLevel,
failuresLogLevel = failuresLogLevel
).build()
),
500,
Async.defaultExecutorService()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okio.Buffer
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.web3j.protocol.http.HttpService
Expand Down Expand Up @@ -52,17 +53,19 @@ class OkHttpMinimalJsonRpcLoggerInterceptor(
}

fun okHttpClientBuilder(
logger: Logger = LogManager.getLogger(HttpService::class.java) // use same class to keep backward compatibility
logger: Logger = LogManager.getLogger(HttpService::class.java), // use same class to keep backward compatibility
// we make a lot of eth_call request that fail by design, having DEBUG/WARN level is too noisy
// ideally we should manage methods individually, but don't have time for that now
requestResponseLogLevel: Level = Level.TRACE,
failuresLogLevel: Level = Level.DEBUG
): OkHttpClient.Builder {
val httpClientBuilder = OkHttpClient.Builder()
httpClientBuilder.addInterceptor(
OkHttpMinimalJsonRpcLoggerInterceptor(
MinimalInLineJsonRpcLogger(
logger,
// we make a lot of eth_call request that fail by design, having DEBUG/WARN level is too noisy
// ideally we should manage methods individually, but don't have time for that now
requestResponseLogLevel = org.apache.logging.log4j.Level.TRACE,
failuresLogLevel = org.apache.logging.log4j.Level.DEBUG,
requestResponseLogLevel = requestResponseLogLevel,
failuresLogLevel = failuresLogLevel,
maskEndpoint = ::maskEndpointPath
)
)
Expand Down

0 comments on commit d28e9bd

Please sign in to comment.