mirror of
				https://github.com/Instadapp/chains.git
				synced 2024-07-29 22:37:19 +00:00 
			
		
		
		
	Cleanup and get better RPC checking
This commit is contained in:
		
							parent
							
								
									51632aec61
								
							
						
					
					
						commit
						85734fbff2
					
				
							
								
								
									
										17
									
								
								build.gradle
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								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"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<String>): Exception("should have no extra field $fields")
 | 
			
		||||
class ShouldHaveNoMissingFields(fields: Set<String>): 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<String>) : Exception("should have no extra field $fields")
 | 
			
		||||
class ShouldHaveNoMissingFields(fields: Set<String>) : 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<List<Chain>> = 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"))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 */
 | 
			
		||||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										14
									
								
								src/test/resources/test_chains/invalid/1.nojson
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/test/resources/test_chains/invalid/1.nojson
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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"
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user