-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1606 from novasamatech/rc/8.3.0
Rc/8.3.0
- Loading branch information
Showing
18 changed files
with
126 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 0 additions & 61 deletions
61
...n/java/io/novafoundation/nova/feature_staking_impl/data/validators/KnownNovaValidators.kt
This file was deleted.
Oops, something went wrong.
5 changes: 3 additions & 2 deletions
5
...ain/java/io/novafoundation/nova/feature_staking_impl/data/validators/NovaValidatorsApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package io.novafoundation.nova.feature_staking_impl.data.validators | ||
|
||
import io.novafoundation.nova.feature_staking_impl.BuildConfig | ||
import retrofit2.http.GET | ||
|
||
interface NovaValidatorsApi { | ||
|
||
@GET("https://raw.githubusercontent.com/novasamatech/nova-utils/master/staking/nova_validators.json") | ||
suspend fun getValidators(): Map<String, List<String>> | ||
@GET(BuildConfig.RECOMMENDED_VALIDATORS_URL) | ||
suspend fun getValidators(): ValidatorsPreferencesRemote | ||
} |
6 changes: 6 additions & 0 deletions
6
...o/novafoundation/nova/feature_staking_impl/data/validators/ValidatorsPreferencesRemote.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.novafoundation.nova.feature_staking_impl.data.validators | ||
|
||
class ValidatorsPreferencesRemote( | ||
val preferred: Map<String, Set<String>>, | ||
val excluded: Map<String, Set<String>> | ||
) |
71 changes: 71 additions & 0 deletions
71
...o/novafoundation/nova/feature_staking_impl/data/validators/ValidatorsPreferencesSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.novafoundation.nova.feature_staking_impl.data.validators | ||
|
||
import io.novafoundation.nova.common.utils.mapNotNullToSet | ||
import io.novafoundation.nova.common.utils.mapValuesNotNull | ||
import io.novafoundation.nova.runtime.ext.accountIdOf | ||
import io.novafoundation.nova.runtime.multiNetwork.ChainRegistry | ||
import io.novafoundation.nova.runtime.multiNetwork.chain.model.Chain | ||
import io.novafoundation.nova.runtime.multiNetwork.chain.model.ChainId | ||
import io.novafoundation.nova.runtime.multiNetwork.chainsById | ||
import io.novasama.substrate_sdk_android.extensions.toHexString | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
|
||
interface ValidatorsPreferencesSource { | ||
|
||
suspend fun getRecommendedValidatorIds(chainId: ChainId): Set<String> | ||
|
||
suspend fun getExcludedValidatorIds(chainId: ChainId): Set<String> | ||
} | ||
|
||
class RemoteValidatorsPreferencesSource( | ||
private val validatorsApi: NovaValidatorsApi, | ||
private val chainRegistry: ChainRegistry, | ||
) : ValidatorsPreferencesSource { | ||
|
||
private var validatorsPreferences: ValidatorsPreferencesRemote? = null | ||
private val validatorsMutex = Mutex() | ||
|
||
override suspend fun getRecommendedValidatorIds(chainId: ChainId): Set<String> { | ||
return getValidators().preferred[chainId].orEmpty() | ||
} | ||
|
||
override suspend fun getExcludedValidatorIds(chainId: ChainId): Set<String> { | ||
return getValidators().excluded[chainId].orEmpty() | ||
} | ||
|
||
private suspend fun getValidators(): ValidatorsPreferencesRemote { | ||
return validatorsMutex.withLock { | ||
if (validatorsPreferences == null) { | ||
validatorsPreferences = fetchValidators() | ||
} | ||
|
||
validatorsPreferences ?: ValidatorsPreferencesRemote(emptyMap(), emptyMap()) | ||
} | ||
} | ||
|
||
private suspend fun fetchValidators(): ValidatorsPreferencesRemote? { | ||
return runCatching { | ||
val chainsById = chainRegistry.chainsById() | ||
val preferences = validatorsApi.getValidators() | ||
val recommended = preferences.preferred.mapValuesNotNull { (chainId, addresses) -> | ||
chainsById[chainId]?.convertAddressesToAccountIds(addresses) | ||
} | ||
val excluded = preferences.excluded.mapValuesNotNull { (chainId, addresses) -> | ||
chainsById[chainId]?.convertAddressesToAccountIds(addresses) | ||
} | ||
|
||
ValidatorsPreferencesRemote(recommended, excluded) | ||
}.getOrNull() | ||
} | ||
|
||
private fun Chain.convertAddressesToAccountIds(addresses: Set<String>): Set<String> { | ||
return addresses.mapNotNullToSet { | ||
this.tryConvertAddressToAccountIdHex(it) | ||
} | ||
} | ||
|
||
private fun Chain.tryConvertAddressToAccountIdHex(address: String): String? { | ||
return runCatching { accountIdOf(address).toHexString() }.getOrNull() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.