Skip to content

Commit

Permalink
Simplify rpc calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ruXlab committed Sep 30, 2023
1 parent 288d43b commit c16d1f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '11'
java-version: '17'
cache: 'gradle'

- name: Build with Gradle
Expand Down
90 changes: 28 additions & 62 deletions web3j/src/main/kotlin/vc/rux/pokefork/web3j/LocalWeb3jNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,69 +28,32 @@ class LocalWeb3jNode(
val realRpcUrl = (hardhatNode.config.nodeMode as? NodeMode.Fork)?.realNodeRpc
?: throw IllegalArgumentException("Node is not running in fork mode, used configuration: ${hardhatNode.config.nodeMode.javaClass.simpleName}")

val resp = httpService.send(
Request(
"hardhat_reset",
listOf(
mapOf("forking" to mapOf(
"jsonRpcUrl" to realRpcUrl,
"blockNumber" to blockNumber
)),
),
httpService,
HardhatStringResponse::class.java
),
HardhatStringResponse::class.java
)

resp.throwIfErroredOrResultIsNotTrue()
sendRpcCallAndCheckResponse("hardhat_reset", listOf(
mapOf("forking" to mapOf(
"jsonRpcUrl" to realRpcUrl,
"blockNumber" to blockNumber
)),
))

log.debug("forkBlock: forked from $blockNumber, fork took: ${currentTimeMillis() - startedAt}ms")
}

override fun setNextBlockBaseFeePerGas(baseFeePerGas: BigInteger) {
log.debug("setNextBlockBaseFeePerGas: setting baseFeePerGas to {}", baseFeePerGas)
val resp = httpService.send(
Request(
"hardhat_setNextBlockBaseFeePerGas",
listOf(baseFeePerGas.toHexStringPrefixed()),
httpService,
HardhatStringResponse::class.java
),
HardhatStringResponse::class.java
)

resp.throwIfErroredOrResultIsNotTrue()
sendRpcCallAndCheckResponse("hardhat_setNextBlockBaseFeePerGas", listOf(baseFeePerGas.toHexStringPrefixed()))
}

override fun mine(blocks: Int) {
log.debug("mine: Mining $blocks blocks")
val resp = httpService.send(
Request(
"hardhat_mine",
listOf(blocks.toHexStringPrefixed()),
httpService,
HardhatMineResponse::class.java
),
HardhatMineResponse::class.java
)

resp.throwIfErroredOrResultIsNotTrue()
sendRpcCallAndCheckResponse("hardhat_mine", listOf(blocks.toHexStringPrefixed()))
}

override fun setBalance(destination: String, balance: BigInteger) {
log.info("mine: set balance of {} to {}", destination, balance)
val resp = httpService.send(
Request(
"hardhat_setBalance",
listOf(destination, balance.toHexStringPrefixed()),
httpService,
HardhatSetBalanceResponse::class.java
),
HardhatSetBalanceResponse::class.java
)

resp.throwIfErroredOrResultIsNotTrue()
sendRpcCallAndCheckResponse("hardhat_setBalance",
listOf(destination, balance.toHexStringPrefixed())
)
}

override fun setStorageAt(destination: String, offset: BigInteger, value: BigInteger) {
Expand All @@ -106,25 +69,28 @@ class LocalWeb3jNode(
val (hexOffset, hexValue) = (offset.toHexStringPrefixed() to value.toHexStringSuffixed(32))

log.info("setStorageAt: set storage of {} at {} to {}", destination, hexOffset, hexValue)
val resp = httpService.send(
Request(
"hardhat_setStorageAt",
listOf(destination, hexOffset, hexValue),
httpService,
HardhatSetBalanceResponse::class.java
),
HardhatSetBalanceResponse::class.java
sendRpcCallAndCheckResponse("hardhat_setStorageAt",
listOf(destination, hexOffset, hexValue)
)

resp.throwIfErroredOrResultIsNotTrue()
}

/**
* Helper method; reduces boilerplate
*/
private fun sendRpcCallAndCheckResponse(
method: String,
params: List<Any>,
) {
httpService.send(
Request(method, params, httpService, HardhatStringResponse::class.java),
HardhatStringResponse::class.java
).also { resp ->
resp.throwIfErroredOrResultIsNotTrue()
}
}

internal class HardhatStringResponse : Response<String>()

internal class HardhatMineResponse : Response<String>()
internal class HardhatSetBalanceResponse : Response<String>()


companion object {
private val log = LoggerFactory.getLogger(LocalWeb3jNode::class.java)

Expand Down

0 comments on commit c16d1f4

Please sign in to comment.