Enforce unique names and shortNames

This commit is contained in:
ligi 2020-11-13 04:48:56 +01:00
parent a43a9b2a4d
commit fc8a0f9bd8
No known key found for this signature in database
GPG Key ID: 8E81894010ABF23D
6 changed files with 82 additions and 1 deletions

View File

@ -1,7 +1,9 @@
package org.ethereum.lists.chains
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import org.ethereum.lists.chains.model.Chain
val mandatory_fields = listOf(
"name",
@ -21,3 +23,4 @@ val optionalFields = listOf(
)
val moshi: Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val chainAdapter: JsonAdapter<Chain> = moshi.adapter(Chain::class.java)

View File

@ -8,6 +8,9 @@ import org.kethereum.erc55.isValid
import org.kethereum.model.Address
import org.kethereum.rpc.HttpEthereumRPC
val parsedShortNames= mutableSetOf<String>()
val parsedNames= mutableSetOf<String>()
fun main(args: Array<String>) {
File("_data/chains").listFiles()?.forEach {
@ -83,7 +86,16 @@ moshi fails for extra commas
https://github.com/ethereum-lists/chains/issues/126
*/
private fun parseWithMoshi(fileToParse: File) {
moshi.adapter(Chain::class.java).fromJson(fileToParse.readText())
val parsedChain = chainAdapter.fromJson(fileToParse.readText())
if (parsedNames.contains(parsedChain!!.name)) {
throw NameMustBeUnique(parsedChain.name)
}
parsedNames.add(parsedChain.name)
if (parsedShortNames.contains(parsedChain.shortName)) {
throw ShortNameMustBeUnique(parsedChain.shortName)
}
parsedShortNames.add(parsedChain.shortName)
}
private fun getNumber(jsonObject: JsonObject, field: String): Long {

View File

@ -9,3 +9,5 @@ class RPCMustBeListOfStrings : Exception("rpc must be a list of strings")
class ENSMustBeObject: Exception("ens must be an object")
class ENSMustHaveOnlyRegistry: Exception("ens can only have a registry currently")
class ENSRegistryAddressMustBeValid: Exception("ens registry must have valid address")
class NameMustBeUnique(dup: String): Exception(" name must be unique - but found `$dup` more than once")
class ShortNameMustBeUnique(dup: String): Exception("short name must be unique - but found `$dup` more than once")

View File

@ -1,11 +1,18 @@
import com.squareup.moshi.JsonEncodingException
import org.ethereum.lists.chains.*
import org.ethereum.lists.chains.model.*
import org.junit.Before
import org.junit.Test
import java.io.File
class TheChainChecker {
@Before
fun setup() {
parsedNames.clear()
parsedShortNames.clear()
}
@Test
fun shouldPassForValidChain() {
val file = getFile("valid/1.json")
@ -84,6 +91,23 @@ class TheChainChecker {
checkChain(file, false)
}
@Test(expected = NameMustBeUnique::class)
fun shouldFailOnNonUniqueName() {
checkChain(getFile("valid/1.json"), false)
checkChain(getFile("valid/1.json"), false)
}
@Test(expected = ShortNameMustBeUnique::class)
fun shouldFailOnNonUniqueShortName() {
checkChain(getFile("invalid/sameshortname/5.json"), false)
checkChain(getFile("invalid/sameshortname/1.json"), false)
}
@Test
fun canParse2chains() {
checkChain(getFile("valid/1.json"), false)
checkChain(getFile("valid/5.json"), false)
}
private fun getFile(s: String) = File(javaClass.classLoader.getResource("test_chains/$s").file)

View File

@ -0,0 +1,14 @@
{
"name": "Ethereum Mainnet",
"shortName": "willfail",
"chain": "ETH",
"network": "mainnet",
"chainId": 1,
"networkId": 1,
"rpc": [
"https://mainnet.infura.io/v3/${INFURA_API_KEY}",
"https://api.mycryptoapi.com/eth"
],
"faucets": [],
"infoURL": "https://ethereum.org"
}

View File

@ -0,0 +1,26 @@
{
"name": "Ethereum Testnet Görli",
"chain": "ETH",
"network": "goerli",
"rpc": [
"https://rpc.goerli.mudit.blog/",
"https://rpc.slock.it/goerli ",
"https://goerli.prylabs.net"
],
"faucets": [
"https://goerli-faucet.slock.it/?address=${ADDRESS}",
"https://faucet.goerli.mudit.blog"
],
"nativeCurrency": {
"name": "Görli Ether",
"symbol": "GOR",
"decimals": 18
},
"infoURL": "https://goerli.net/#about",
"shortName": "willfail",
"chainId": 5,
"networkId": 5,
"ens": {
"registry":"0x112234455c3a32fd11230c42e7bccd4a84e02010"
}
}