Skip to content

Commit

Permalink
check chain_ids on all networks
Browse files Browse the repository at this point in the history
  • Loading branch information
a10zn8 committed Jun 11, 2024
1 parent f9667e2 commit b5a2572
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ class ChainsConfigReader(
?: throw IllegalArgumentException("undefined code for $blockchain")
val grpcId = getValueAsInt(node, "grpcId")
?: throw IllegalArgumentException("undefined code for $blockchain")
val netVersion = getValueAsLong(node, "net-version")?.toBigInteger() ?: BigInteger(chainId.drop(2), 16)
val netVersion = getValueAsLong(node, "net-version")?.toBigInteger() ?:
if (chainId.startsWith("0x")) BigInteger(chainId.drop(2), 16) else BigInteger.ZERO
val shortNames = getListOfString(node, "short-names")
?: throw IllegalArgumentException("undefined shortnames for $blockchain")
val type = getValueAsString(node, "type")
Expand Down
26 changes: 13 additions & 13 deletions foundation/src/main/resources/chains.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,13 @@ chain-settings:
priority: 1
code: VARA_MAINNET
short-names: [vara]
chain-id: 0x0
chain-id: Vara Network
grpcId: 1027
- id: Testnet
priority: 1
code: VARA_TESTMET
short-names: [vara-testnet]
chain-id: 0x0
chain-id: Vara Network Testnet
grpcId: 10036
- id: solana
label: Solana
Expand Down Expand Up @@ -1282,19 +1282,19 @@ chain-settings:
code: NEAR_MAINNET
grpcId: 1050
short-names: [near]
chain-id: 0x0
chain-id: mainnet
- id: Testnet
priority: 5
code: NEAR_TESTNET
grpcId: 10064
short-names: [near-testnet]
chain-id: 0x0
chain-id: testnet
- id: Betanet
priority: 1
code: NEAR_BETANET
grpcId: 10065
short-names: [near-betanet]
chain-id: 0x0
chain-id: betanet
- id: opcelestia-raspberry
label: OP Celestia Raspberry
type: eth
Expand Down Expand Up @@ -1420,13 +1420,13 @@ chain-settings:
lagging: 1
chains:
- id: Mainnet
chain-id: 0x0
chain-id: cosmoshub-4
short-names: [ cosmos-hub ]
code: COSMOS_HUB
grpcId: 1057
priority: 100
- id: Testnet
chain-id: 0x0
chain-id: provider
short-names: [ cosmos-hub-testnet ]
code: COSMOS_HUB_TESTNET
grpcId: 10079
Expand All @@ -1441,13 +1441,13 @@ chain-settings:
lagging: 1
chains:
- id: Mainnet
chain-id: 0x0
chain-id: axelar-dojo-1
short-names: [ axelar ]
code: AXELAR
grpcId: 1059
priority: 100
- id: Testnet
chain-id: 0x0
chain-id: axelar-testnet-lisbon-3
short-names: [ axelar-testnet ]
code: AXELAR_TESTNET
grpcId: 10081
Expand All @@ -1462,13 +1462,13 @@ chain-settings:
lagging: 1
chains:
- id: Mainnet
chain-id: 0x0
chain-id: osmosis
short-names: [ osmosis ]
code: OSMOSIS
grpcId: 1060
priority: 100
- id: Testnet
chain-id: 0x0
chain-id: osmo-test-5
short-names: [ osmosis-testnet ]
code: OSMOSIS_TESTNET
grpcId: 10082
Expand All @@ -1483,13 +1483,13 @@ chain-settings:
lagging: 1
chains:
- id: Mainnet
chain-id: 0x0
chain-id: neutron-1
short-names: [ neutron ]
code: NEUTRON
grpcId: 1061
priority: 100
- id: Testnet
chain-id: 0x0
chain-id: pion-1
short-names: [ neutron-testnet ]
code: NEUTRON_TESTNET
grpcId: 10083
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,21 @@ abstract class UpstreamValidator(
val cp = Comparator { avail1: UpstreamAvailability, avail2: UpstreamAvailability -> if (avail1.isBetterTo(avail2)) -1 else 1 }
return results.sortedWith(cp).last()
}

fun resolve(results: Iterable<ValidateUpstreamSettingsResult>): ValidateUpstreamSettingsResult {
val cp = Comparator { res1: ValidateUpstreamSettingsResult, res2: ValidateUpstreamSettingsResult -> if (res1.priority < res2.priority) -1 else 1 }
return results.sortedWith(cp).last()
}
}
}

enum class ValidateUpstreamSettingsResult {
UPSTREAM_VALID,
UPSTREAM_SETTINGS_ERROR,
UPSTREAM_FATAL_SETTINGS_ERROR,
enum class ValidateUpstreamSettingsResult(val priority: Int) {
UPSTREAM_VALID(0),
UPSTREAM_SETTINGS_ERROR(1),
UPSTREAM_FATAL_SETTINGS_ERROR(2),
}

data class SingleCallValidator(
data class SingleCallValidator<T>(
val method: ChainRequest,
val check: (ByteArray) -> UpstreamAvailability,
val check: (ByteArray) -> T,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import io.emeraldpay.dshackle.upstream.SingleCallValidator
import io.emeraldpay.dshackle.upstream.Upstream
import io.emeraldpay.dshackle.upstream.UpstreamAvailability.OK
import io.emeraldpay.dshackle.upstream.UpstreamValidator
import io.emeraldpay.dshackle.upstream.ValidateUpstreamSettingsResult
import io.emeraldpay.dshackle.upstream.generic.AbstractPollChainSpecific
import io.emeraldpay.dshackle.upstream.generic.GenericUpstreamValidator
import io.emeraldpay.dshackle.upstream.rpcclient.ListParams
import io.emeraldpay.dshackle.upstream.rpcclient.ObjectParams
import org.slf4j.LoggerFactory
import reactor.core.publisher.Mono
import java.math.BigInteger
import java.time.Instant

object CosmosChainSpecific : AbstractPollChainSpecific() {

val log = LoggerFactory.getLogger(this::class.java)
override fun latestBlockRequest(): ChainRequest = ChainRequest("block", ObjectParams())

override fun parseBlock(data: ByteArray, upstreamId: String): BlockContainer {
Expand Down Expand Up @@ -75,9 +79,24 @@ object CosmosChainSpecific : AbstractPollChainSpecific() {
return GenericUpstreamValidator(
upstream,
options,
SingleCallValidator(
ChainRequest("health", ListParams()),
) { _ -> OK },
listOf(
SingleCallValidator(
ChainRequest("health", ListParams()),
) { _ -> OK },
),
listOf(
SingleCallValidator(
ChainRequest("status", ListParams()),
) { data ->
val resp = Global.objectMapper.readValue(data, CosmosStatus::class.java)
if (chain.chainId.isNotEmpty() && resp.nodeInfo.network.lowercase() != chain.chainId.lowercase()) {
ValidateUpstreamSettingsResult.UPSTREAM_FATAL_SETTINGS_ERROR
} else {
ValidateUpstreamSettingsResult.UPSTREAM_VALID
}
},
),

)
}

Expand Down Expand Up @@ -115,6 +134,7 @@ data class CosmosStatus(
@JsonIgnoreProperties(ignoreUnknown = true)
data class CosmosNodeInfo(
@JsonProperty("version") var version: String,
@JsonProperty("network") var network: String,
)

@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ import java.util.concurrent.TimeoutException
class GenericUpstreamValidator(
upstream: Upstream,
options: ChainOptions.Options,
private val validator: SingleCallValidator,
private val validators: List<SingleCallValidator<UpstreamAvailability>>,
private val startupValidators: List<SingleCallValidator<ValidateUpstreamSettingsResult>>,
) : UpstreamValidator(upstream, options) {

override fun validate(): Mono<UpstreamAvailability> {
return Mono.zip(
validators.map { exec(it, UpstreamAvailability.UNAVAILABLE) },
) { a -> a.map { it as UpstreamAvailability } }
.map(::resolve)
.defaultIfEmpty(UpstreamAvailability.UNAVAILABLE)
.onErrorResume {
log.error("Error during upstream validation for ${upstream.getId()}", it)
Mono.just(UpstreamAvailability.UNAVAILABLE)
}
}
fun <T : Any> exec(validator: SingleCallValidator<T>, onError: T): Mono<T> {
return upstream.getIngressReader()
.read(validator.method)
.flatMap(ChainResponse::requireResult)
Expand All @@ -29,10 +41,22 @@ class GenericUpstreamValidator(
.then(Mono.error(TimeoutException("Validation timeout for ${validator.method.method}"))),
)
.doOnError { err -> log.error("Error during ${validator.method.method} validation for ${upstream.getId()}", err) }
.onErrorReturn(UpstreamAvailability.UNAVAILABLE)
.onErrorReturn(onError)
}

override fun validateUpstreamSettings(): Mono<ValidateUpstreamSettingsResult> {
return Mono.just(UPSTREAM_VALID)
return Mono.zip(
startupValidators.map { exec(it, ValidateUpstreamSettingsResult.UPSTREAM_SETTINGS_ERROR) },
) { a -> a.map { it as ValidateUpstreamSettingsResult } }
.map(::resolve)
.defaultIfEmpty(ValidateUpstreamSettingsResult.UPSTREAM_FATAL_SETTINGS_ERROR)
.onErrorResume {
log.error("Error during upstream validation for ${upstream.getId()}", it)
Mono.just(ValidateUpstreamSettingsResult.UPSTREAM_FATAL_SETTINGS_ERROR)
}
}

override fun validateUpstreamSettingsOnStartup(): ValidateUpstreamSettingsResult {
return validateUpstreamSettings().block() ?: UPSTREAM_VALID
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.emeraldpay.dshackle.upstream.Upstream
import io.emeraldpay.dshackle.upstream.UpstreamAvailability
import io.emeraldpay.dshackle.upstream.UpstreamSettingsDetector
import io.emeraldpay.dshackle.upstream.UpstreamValidator
import io.emeraldpay.dshackle.upstream.ValidateUpstreamSettingsResult
import io.emeraldpay.dshackle.upstream.generic.AbstractPollChainSpecific
import io.emeraldpay.dshackle.upstream.generic.GenericUpstreamValidator
import io.emeraldpay.dshackle.upstream.lowerbound.LowerBoundService
Expand Down Expand Up @@ -64,11 +65,20 @@ object NearChainSpecific : AbstractPollChainSpecific() {
return GenericUpstreamValidator(
upstream,
options,
SingleCallValidator(
ChainRequest("status", ListParams()),
) { data ->
validate(data)
},
listOf(
SingleCallValidator(
ChainRequest("status", ListParams()),
) { data ->
validate(data)
},
),
listOf(
SingleCallValidator(
ChainRequest("status", ListParams()),
) { data ->
validateSettings(data, chain)
},
),
)
}

Expand All @@ -85,6 +95,15 @@ object NearChainSpecific : AbstractPollChainSpecific() {
}
}

fun validateSettings(data: ByteArray, chain: Chain): ValidateUpstreamSettingsResult {
val resp = Global.objectMapper.readValue(data, NearStatus::class.java)
return if (chain.chainId.isNotEmpty() && resp.chainId.lowercase() != chain.chainId.lowercase()) {
ValidateUpstreamSettingsResult.UPSTREAM_FATAL_SETTINGS_ERROR
} else {
ValidateUpstreamSettingsResult.UPSTREAM_VALID
}
}

override fun upstreamSettingsDetector(chain: Chain, upstream: Upstream): UpstreamSettingsDetector {
return NearUpstreamSettingsDetector(upstream)
}
Expand All @@ -108,6 +127,7 @@ data class NearHeader(

@JsonIgnoreProperties(ignoreUnknown = true)
data class NearStatus(
@JsonProperty("chain_id") var chainId: String,
@JsonProperty("sync_info") var syncInfo: NearSync,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import io.emeraldpay.dshackle.upstream.SingleCallValidator
import io.emeraldpay.dshackle.upstream.Upstream
import io.emeraldpay.dshackle.upstream.UpstreamAvailability
import io.emeraldpay.dshackle.upstream.UpstreamValidator
import io.emeraldpay.dshackle.upstream.ValidateUpstreamSettingsResult
import io.emeraldpay.dshackle.upstream.calls.CallMethods
import io.emeraldpay.dshackle.upstream.calls.DefaultPolkadotMethods
import io.emeraldpay.dshackle.upstream.ethereum.WsSubscriptions
Expand Down Expand Up @@ -97,11 +98,20 @@ object PolkadotChainSpecific : AbstractPollChainSpecific() {
return GenericUpstreamValidator(
upstream,
options,
SingleCallValidator(
ChainRequest("system_health", ListParams()),
) { data ->
validate(data, options.minPeers, upstream.getId())
},
listOf(
SingleCallValidator(
ChainRequest("system_health", ListParams()),
) { data ->
validate(data, options.minPeers, upstream.getId())
},
),
listOf(
SingleCallValidator(
ChainRequest("system_chain", ListParams()),
) { data ->
validateSettings(data, chain)
},
),
)
}

Expand All @@ -123,6 +133,15 @@ object PolkadotChainSpecific : AbstractPollChainSpecific() {
return UpstreamAvailability.OK
}

fun validateSettings(data: ByteArray, chain: Chain): ValidateUpstreamSettingsResult {
val id = Global.objectMapper.readValue(data, String::class.java)
return if (chain.chainId.isNotEmpty() && id.lowercase() != chain.chainId.lowercase()) {
ValidateUpstreamSettingsResult.UPSTREAM_FATAL_SETTINGS_ERROR
} else {
ValidateUpstreamSettingsResult.UPSTREAM_VALID
}
}

override fun makeIngressSubscription(ws: WsSubscriptions): IngressSubscription {
return GenericIngressSubscription(ws, DefaultPolkadotMethods.subs.map { it.first })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,20 @@ object SolanaChainSpecific : AbstractChainSpecific() {
return GenericUpstreamValidator(
upstream,
options,
SingleCallValidator(
ChainRequest("getHealth", ListParams()),
) { data ->
val resp = String(data)
if (resp == "\"ok\"") {
UpstreamAvailability.OK
} else {
log.warn("Upstream {} validation failed, solana status is {}", upstream.getId(), resp)
UpstreamAvailability.UNAVAILABLE
}
},
listOf(
SingleCallValidator(
ChainRequest("getHealth", ListParams()),
) { data ->
val resp = String(data)
if (resp == "\"ok\"") {
UpstreamAvailability.OK
} else {
log.warn("Upstream {} validation failed, solana status is {}", upstream.getId(), resp)
UpstreamAvailability.UNAVAILABLE
}
},
),
listOf(),
)
}

Expand Down
Loading

0 comments on commit b5a2572

Please sign in to comment.