Skip to content

Commit

Permalink
Support parent field - closes ethereum-lists#318
Browse files Browse the repository at this point in the history
  • Loading branch information
ligi committed Jul 6, 2021
1 parent 3f5ca6d commit c8477c7
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/kotlin/org/ethereum/lists/chains/Env.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ val optionalFields = listOf(
"slip44",
"ens",
"icon",
"explorers"
"explorers",
"parent"
)

val moshi: Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/org/ethereum/lists/chains/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.lang.IllegalArgumentException

val parsedShortNames = mutableSetOf<String>()
val parsedNames = mutableSetOf<String>()
Expand Down Expand Up @@ -207,6 +208,25 @@ fun checkChain(chainFile: File, connectRPC: Boolean) {
}
}

jsonObject["parent"]?.let {
if (it !is JsonObject) {
throw ParentMustBeObject()
}

if (it.keys != mutableSetOf("chain", "type")) {
throw ParentMustHaveChainAndType()
}

if (!setOf("L2", "shard").contains(it["type"])) {
throw ParentHasInvalidType(it["type"] as? String)
}

if (!File(chainFile.parentFile, it["chain"] as String + ".json").exists()) {
throw ParentChainDoesNotExist(it["chain"] as String)
}

}

if (connectRPC) {
if (jsonObject["rpc"] is List<*>) {
(jsonObject["rpc"] as List<*>).forEach {
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/org/ethereum/lists/chains/model/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ class ExplorersMustBeArray: Exception("explorers must be an array")
class ExplorerMustHaveName: Exception("Explorer must have name")
class ExplorerInvalidUrl: Exception("Explorer have url starting with https://")
class ExplorerStandardMustBeEIP3091: Exception("explorer standard must be EIP3091 - currently the only one supported")
class ParentHasInvalidType(type: String?): Exception("Parent has invalid type $type - only L2 or shard allowed")
class ParentMustBeObject: Exception("parent must be an object")
class ParentMustHaveChainAndType: Exception("parent must have fields 'chain' and 'type'")
class ParentChainDoesNotExist(chain: String): Exception("Referenced parent chain ($chain) does not exist")
29 changes: 29 additions & 0 deletions src/test/kotlin/org/ethereum/lists/chains/TheChainChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ class TheChainChecker {
checkChain(file, false)
}

@Test
fun shouldPassForValidChainWithParent() {
val file = getFile("valid/withparent/eip155-2.json")

checkChain(file, false)
}

@Test(expected = ParentMustBeObject::class)
fun shouldFailForParentNoObject() {
val file = getFile("invalid/withparentnobject/eip155-2.json")

checkChain(file, false)
}

@Test(expected = ParentHasInvalidType::class)
fun shouldFailForParentWithInvalidType() {
val file = getFile("invalid/withparentinvalidtype/eip155-2.json")

checkChain(file, false)
}


@Test(expected = ParentChainDoesNotExist::class)
fun shouldFailIfParentChainDoesNotExist() {
val file = getFile("invalid/withparentchaindoesnotexist/eip155-2.json")

checkChain(file, false)
}

@Test(expected = FileNameMustMatchChainId::class)
fun shouldFailForInvalidFilename() {
val file = getFile("invalid/eip155-invalid_filename.json")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Ethereum Mainnet",
"shortName": "eth",
"chain": "ETH",
"network": "mainnet",
"chainId": 2,
"networkId": 2,
"rpc": [
"https://mainnet.infura.io/v3/${INFURA_API_KEY}",
"https://api.mycryptoapi.com/eth"
],
"faucets": [],
"infoURL": "https://ethereum.org",
"nativeCurrency": {
"name": "Ether",
"symbol": "ETH",
"decimals": 18
},
"parent": {
"chain": "eip155-1",
"type": "L2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Ethereum Mainnet",
"shortName": "eth",
"chain": "ETH",
"network": "mainnet",
"chainId": 2,
"networkId": 2,
"rpc": [
"https://mainnet.infura.io/v3/${INFURA_API_KEY}",
"https://api.mycryptoapi.com/eth"
],
"faucets": [],
"infoURL": "https://ethereum.org",
"nativeCurrency": {
"name": "Ether",
"symbol": "ETH",
"decimals": 18
},
"parent": {
"chain": "eip155-1",
"type": "yolo"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Ethereum Mainnet",
"shortName": "eth",
"chain": "ETH",
"network": "mainnet",
"chainId": 2,
"networkId": 2,
"rpc": [
"https://mainnet.infura.io/v3/${INFURA_API_KEY}",
"https://api.mycryptoapi.com/eth"
],
"faucets": [],
"infoURL": "https://ethereum.org",
"nativeCurrency": {
"name": "Ether",
"symbol": "ETH",
"decimals": 18
},
"parent": "yolo"
}
19 changes: 19 additions & 0 deletions src/test/resources/test_chains/valid/withparent/eip155-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "Ethereum Mainnet",
"shortName": "eth",
"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",
"nativeCurrency": {
"name": "Ether",
"symbol": "ETH",
"decimals": 18
}
}
23 changes: 23 additions & 0 deletions src/test/resources/test_chains/valid/withparent/eip155-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Ethereum Mainnet",
"shortName": "eth",
"chain": "ETH",
"network": "mainnet",
"chainId": 2,
"networkId": 2,
"rpc": [
"https://mainnet.infura.io/v3/${INFURA_API_KEY}",
"https://api.mycryptoapi.com/eth"
],
"faucets": [],
"infoURL": "https://ethereum.org",
"nativeCurrency": {
"name": "Ether",
"symbol": "ETH",
"decimals": 18
},
"parent": {
"chain": "eip155-1",
"type": "L2"
}
}

0 comments on commit c8477c7

Please sign in to comment.