From a7cd0b68712dd4ef960353c111f182c64993719d Mon Sep 17 00:00:00 2001 From: ligi Date: Fri, 18 Nov 2022 15:53:11 +0100 Subject: [PATCH] Check chainID from RPC matches json --- .../kotlin/org/ethereum/lists/chains/Main.kt | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/processor/src/main/kotlin/org/ethereum/lists/chains/Main.kt b/processor/src/main/kotlin/org/ethereum/lists/chains/Main.kt index 4a45a34c..1850b9ff 100644 --- a/processor/src/main/kotlin/org/ethereum/lists/chains/Main.kt +++ b/processor/src/main/kotlin/org/ethereum/lists/chains/Main.kt @@ -8,6 +8,7 @@ import org.ethereum.lists.chains.model.* import org.kethereum.erc55.isValid import org.kethereum.model.Address import org.kethereum.rpc.HttpEthereumRPC +import java.math.BigInteger import javax.imageio.ImageIO import kotlin.io.OnErrorAction.* @@ -224,10 +225,10 @@ fun checkChain(chainFile: File, connectRPC: Boolean, verbose: Boolean = false) { } val jsonObject = Klaxon().parseJsonObject(chainFile.reader()) - val chainAsLong = getNumber(jsonObject, "chainId") + val chainIdAsLong = getNumber(jsonObject, "chainId") if (chainFile.nameWithoutExtension.startsWith("eip155-")) { - if (chainAsLong.toString() != chainFile.nameWithoutExtension.replace("eip155-", "")) { + if (chainIdAsLong.toString() != chainFile.nameWithoutExtension.replace("eip155-", "")) { throw (FileNameMustMatchChainId()) } } else { @@ -389,11 +390,24 @@ fun checkChain(chainFile: File, connectRPC: Boolean, verbose: Boolean = false) { if (it !is String) { throw (RPCMustBeListOfStrings()) } else { - println("connecting to $it") - val ethereumRPC = HttpEthereumRPC(it) - println("Client:" + ethereumRPC.clientVersion()) - println("BlockNumber:" + ethereumRPC.blockNumber()) - println("GasPrice:" + ethereumRPC.gasPrice()) + var chainId: BigInteger? = null + try { + println("connecting to $it") + val ethereumRPC = HttpEthereumRPC(it) + + println("Client:" + ethereumRPC.clientVersion()) + println("BlockNumber:" + ethereumRPC.blockNumber()) + println("GasPrice:" + ethereumRPC.gasPrice()) + + chainId = ethereumRPC.chainId()?.value + } catch (e: Exception) { + + } + chainId?.let { chainId -> + if (chainIdAsLong != chainId.toLong()) { + error("RPC chainId (${chainId.toLong()}) does not match chainId from json ($chainIdAsLong)") + } + } } } println()