From 85734fbff24fed0dad7ddf9434448d8ceb77bfb3 Mon Sep 17 00:00:00 2001 From: ligi Date: Fri, 17 May 2019 03:21:51 +0200 Subject: [PATCH] Cleanup and get better RPC checking --- build.gradle | 17 +++-- .../org/ethereum/lists/chains/ChainChecker.kt | 74 ++++++++----------- src/test/kotlin/TheChainChecker.kt | 12 ++- .../resources/test_chains/invalid/1.nojson | 14 ++++ 4 files changed, 63 insertions(+), 54 deletions(-) create mode 100644 src/test/resources/test_chains/invalid/1.nojson diff --git a/build.gradle b/build.gradle index 7aa92c04..0afd1e8d 100644 --- a/build.gradle +++ b/build.gradle @@ -27,16 +27,17 @@ repositories { } dependencies { - compile "org.jetbrains.kotlin:kotlin-stdlib:${KOTLIN_VERSION}" - compile "com.github.ethereum-lists:cilib:1.0" + implementation "org.jetbrains.kotlin:kotlin-stdlib:${KOTLIN_VERSION}" + implementation "com.github.ethereum-lists:cilib:1.0" - compile "com.github.komputing.kethereum:erc55:${KETHEREUM_VERSION}" - compile "com.github.komputing.kethereum:model:${KETHEREUM_VERSION}" - compile "com.github.komputing.kethereum:functions:${KETHEREUM_VERSION}" - compile "com.github.komputing.kethereum:crypto_impl_bouncycastle:${KETHEREUM_VERSION}" + implementation "com.github.komputing.kethereum:rpc:${KETHEREUM_VERSION}" + implementation "com.github.komputing.kethereum:model:${KETHEREUM_VERSION}" + implementation "com.github.komputing.kethereum:functions:${KETHEREUM_VERSION}" + implementation "com.github.komputing.kethereum:crypto_impl_bouncycastle:${KETHEREUM_VERSION}" - compile 'com.beust:klaxon:5.0.5' - compile 'com.squareup.moshi:moshi-kotlin:1.8.0' + implementation 'com.beust:klaxon:5.0.5' + implementation 'com.squareup.moshi:moshi-kotlin:1.8.0' + implementation 'com.squareup.okhttp3:okhttp:3.12.1' testImplementation "org.jetbrains.kotlin:kotlin-test" testImplementation "org.jetbrains.kotlin:kotlin-test-junit" diff --git a/src/main/kotlin/org/ethereum/lists/chains/ChainChecker.kt b/src/main/kotlin/org/ethereum/lists/chains/ChainChecker.kt index e8ad349d..8fbf6541 100644 --- a/src/main/kotlin/org/ethereum/lists/chains/ChainChecker.kt +++ b/src/main/kotlin/org/ethereum/lists/chains/ChainChecker.kt @@ -2,7 +2,9 @@ package org.ethereum.lists.chains import com.beust.klaxon.JsonObject import com.beust.klaxon.Klaxon +import org.kethereum.rpc.EthereumRPC import java.io.File +import java.math.BigInteger val all_fields = listOf( "name", @@ -16,9 +18,12 @@ val all_fields = listOf( "info_url" ) -class FileNameMustMatchChainId: Exception("chain_id must match the filename") -class ShouldHaveNoExtraFields(fields: Set): Exception("should have no extra field $fields") -class ShouldHaveNoMissingFields(fields: Set): Exception("missing field(s) $fields") +class FileNameMustMatchChainId : Exception("chain_id must match the filename") +class ExtensionMustBeJSON : Exception("filename extension must be json") +class ShouldHaveNoExtraFields(fields: Set) : Exception("should have no extra field $fields") +class ShouldHaveNoMissingFields(fields: Set) : Exception("missing field(s) $fields") +class RPCMustBeList : Exception("rpc must be a list") +class RPCMustBeListOfStrings : Exception("rpc must be a list of strings") fun checkChain(it: File) { println("processing $it") @@ -29,6 +34,10 @@ fun checkChain(it: File) { throw(FileNameMustMatchChainId()) } + if (it.extension != "json") { + throw(ExtensionMustBeJSON()) + } + getNumber(jsonObject, "network_id") val extraFields = jsonObject.map.keys.subtract(all_fields) @@ -40,29 +49,25 @@ fun checkChain(it: File) { if (missingFields.isNotEmpty()) { throw ShouldHaveNoMissingFields(missingFields) } -} - -private fun getNumber(jsonObject: JsonObject, field: String): Long { - return when (val chainId = jsonObject[field]) { - is Int -> chainId.toLong() - is Long -> chainId - else -> throw(Exception("chain_id must be a number")) + if (jsonObject["rpc"] is List<*>) { + (jsonObject["rpc"] as List<*>).forEach { + if (it !is String) { + throw(RPCMustBeListOfStrings()) + } else { + println("connecting to $it") + val ethereumRPC = EthereumRPC(it) + println("Client:" + ethereumRPC.clientVersion()?.result) + println("BlockNumber:" + ethereumRPC.blockNumber()?.result?.tryBigint()) + println("GasPrice:" + ethereumRPC.gasPrice()?.result?.tryBigint()) + } + } + println() + } else { + throw(RPCMustBeList()) } } -/* - -open class InvalidTokenException(message: String) : IllegalArgumentException(message) -class InvalidChecksum(message: String) : InvalidTokenException("The address is not valid with ERC-55 checksum $message") - -class InvalidAddress(address: Address) : InvalidTokenException("The address is not valid $address") -class InvalidDecimals : InvalidTokenException("Decimals must be a number") -class InvalidFileName : InvalidTokenException("Filename must be the address + .json") -class InvalidWebsite : InvalidTokenException("Website invalid") -class InvalidJSON(message: String?) : InvalidTokenException("JSON invalid $message") -class InvalidDeprecationMigrationType : InvalidTokenException("Invalid Deprecation Migration type - currently only auto and instructions: is allowed") -class InvalidDeprecationTime : InvalidTokenException("Invalid Deprecation Time - Must be ISO8601") fun String.tryBigint() = if (startsWith("0x")) { try { @@ -73,25 +78,10 @@ fun String.tryBigint() = if (startsWith("0x")) { } else { null } - -fun checkChainsFile(file: File) { - val moshi = Moshi.Builder().build() - - - val listMyData = Types.newParameterizedType(List::class.java, Chain::class.java) - val foo: JsonAdapter> = moshi.adapter(listMyData) - - foo.fromJson(Okio.buffer(Okio.source(file)))?.forEach { - println() - println(it.name) - for (s in it.rpc) { - val ethereumRPC = EthereumRPC(s) - println("Client:" + ethereumRPC.clientVersion()?.result) - println("BlockNumber:" + ethereumRPC.blockNumber()?.result?.tryBigint()) - println("GasPrice:" + ethereumRPC.gasPrice()?.result?.tryBigint()) - } - +private fun getNumber(jsonObject: JsonObject, field: String): Long { + return when (val chainId = jsonObject[field]) { + is Int -> chainId.toLong() + is Long -> chainId + else -> throw(Exception("chain_id must be a number")) } } - - */ \ No newline at end of file diff --git a/src/test/kotlin/TheChainChecker.kt b/src/test/kotlin/TheChainChecker.kt index 7678fb94..54da46e9 100644 --- a/src/test/kotlin/TheChainChecker.kt +++ b/src/test/kotlin/TheChainChecker.kt @@ -1,8 +1,5 @@ -import org.ethereum.lists.chains.FileNameMustMatchChainId -import org.ethereum.lists.chains.ShouldHaveNoExtraFields -import org.ethereum.lists.chains.ShouldHaveNoMissingFields -import org.ethereum.lists.chains.checkChain +import org.ethereum.lists.chains.* import org.junit.Test import java.io.File @@ -43,6 +40,13 @@ class TheChainChecker { checkChain(file) } + @Test(expected = ExtensionMustBeJSON::class) + fun shouldFailFoNonJSON() { + val file = getFile("invalid/1.nojson") + + checkChain(file) + } + private fun getFile(s: String) = File(javaClass.classLoader.getResource("test_chains/$s").file) diff --git a/src/test/resources/test_chains/invalid/1.nojson b/src/test/resources/test_chains/invalid/1.nojson new file mode 100644 index 00000000..d3520800 --- /dev/null +++ b/src/test/resources/test_chains/invalid/1.nojson @@ -0,0 +1,14 @@ +{ + "name": "Ethereum Mainnet", + "short_name": "eth", + "chain": "ETH", + "network": "mainnet", + "chain_id": 1, + "network_id": 1, + "rpc": [ + "https://mainnet.infura.io/v3/${INFURA_API_KEY}", + "https://api.mycryptoapi.com/eth" + ], + "faucets": [], + "info_url": "https://ethereum.org" +}