Chains config refactoring (#311)

Generate code from chains config
This commit is contained in:
Vyacheslav
2023-09-29 13:36:34 +03:00
committed by GitHub
parent a2c5743fb2
commit ef6af898e0
80 changed files with 1527 additions and 1321 deletions

View File

@@ -0,0 +1,52 @@
/**
* Copyright (c) 2020 EmeraldPay, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.emeraldpay.dshackle.config
import io.emeraldpay.dshackle.foundation.EnvVariables
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import org.junit.jupiter.params.provider.ValueSource
class EnvVariablesTest {
lateinit var reader: EnvVariables
@BeforeEach
fun setup() {
reader = EnvVariables()
}
@ParameterizedTest
@ValueSource(strings = ["", "a", "13143", "/etc/client1.myservice.com.key", "true", "1a68f20154fc258fe4149c199ad8f281"])
fun `Post process for usual strings`(s: String) {
assertEquals(s, reader.postProcess(s))
}
@ParameterizedTest
@CsvSource(
"p_\${id}, p_1",
"home: \${HOME}, home: /home/user",
"\${PASSWORD}, 1a68f20154fc258fe4149c199ad8f281",
)
fun `Post process replaces from env`(orig: String, replaced: String) {
System.setProperty("id", "1")
System.setProperty("HOME", "/home/user")
System.setProperty("PASSWORD", "1a68f20154fc258fe4149c199ad8f281")
assertEquals(replaced, reader.postProcess(orig))
}
}

View File

@@ -0,0 +1,80 @@
package io.emeraldpay.dshackle.foundation
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.nodes.MappingNode
import java.io.StringReader
class YamlConfigReaderTest {
@ParameterizedTest
@CsvSource(
"1024, 1024",
"1k, 1024",
"1kb, 1024",
"1K, 1024",
"16kb, 16384",
"1M, 1048576",
"4mb, 4194304",
)
fun `reads bytes values`(input: String, expected: Int) {
val rdr = Impl()
assertEquals(expected, rdr.getValueAsBytes(rdr.asNode("test", input), "test"))
}
@Test
fun `mergeMappingNode should merge nodes correctly`() {
val yaml = Yaml()
val a = yaml.compose(
StringReader(
"""
key1: value1
key2:
subkey1: subvalue1
""",
),
) as MappingNode
val b = yaml.compose(
StringReader(
"""
key1: value3
key2:
subkey1: subvalue1Modified
subkey2: subvalue2
key3: value3
""",
),
) as MappingNode
val result = Impl().mergeMappingNode(a, b)!!
val yml = Impl()
assertEquals(3, result.value.size)
assertEquals("value3", yml.getNodeValue(result, "key1"))
assertEquals("subvalue1Modified", yml.getNodeValue(yml.getNode(result, "key2") as MappingNode, "subkey1"))
assertEquals("subvalue2", yml.getNodeValue(yml.getNode(result, "key2") as MappingNode, "subkey2"))
assertEquals("value3", yml.getNodeValue(result, "key3"))
}
class Impl : YamlConfigReader<Any>() {
override fun read(input: MappingNode?): Any? {
return null
}
fun getNode(node: MappingNode, key: String): Any? {
return Impl().getMapping(node, key)
}
fun getNodeValue(node: MappingNode, key: String): String? {
return Impl().getValueAsString(node, key)
}
fun asNode(key: String, value: String): MappingNode {
return Yaml().compose(StringReader("$key: $value")) as MappingNode
}
}
}

View File

@@ -0,0 +1,44 @@
package org.drpc.chainsconfig
import io.emeraldpay.dshackle.config.ChainsConfigReader
import io.emeraldpay.dshackle.foundation.ChainOptionsReader
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
internal class ChainsConfigReaderTest {
@Test
fun `read standard config without custom`() {
val reader = ChainsConfigReader(ChainOptionsReader())
val config = reader.read(null)
val arb = config.resolve("arbitrum-goerli")
assertEquals(arb.chainId, "0x66eed")
assertEquals(arb.expectedBlockTime.seconds, 1L)
assertEquals(arb.options.validatePeers, false)
assertEquals(arb.id, "goerli")
val ethc = config.resolve("ethereum-classic")
assertEquals(ethc.chainId, "0x3d")
assertEquals(ethc.netVersion, 1)
assertEquals(ethc.code, "ETC")
assertEquals(ethc.grpcId, 101)
assertEquals(ethc.expectedBlockTime.seconds, 12)
assertEquals(ethc.laggingLagSize, 1)
assertEquals(ethc.syncingLagSize, 6)
assertEquals(ethc.id, "mainnet")
}
@Test
fun `read standard config with custom one`() {
val reader = ChainsConfigReader(ChainOptionsReader())
val chains = reader.read(this.javaClass.classLoader.getResourceAsStream("configs/chains-basic.yaml")!!)!!
val config = chains.resolve("fantom")
assertEquals(config.expectedBlockTime.seconds, 10)
assertEquals(config.chainId, "0xfb")
assertEquals(config.syncingLagSize, 11)
assertEquals(config.laggingLagSize, 4)
assertEquals(config.code, "FTA")
assertEquals(config.grpcId, 102)
assertEquals(config.netVersion, 251)
}
}

View File

@@ -0,0 +1,18 @@
version: v1
chain-settings:
protocols:
- id: fantom
settings:
expected-block-time: 10s
options:
validate-peers: true
lags:
syncing: 11
lagging: 4
chains:
- id: mainnet
short-names: [fantom]
code: FTA
grpcId: 102
chain-id: 0xfb