-
Notifications
You must be signed in to change notification settings - Fork 35
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 #1505 from novasamatech/rc/7.12
Rc/7.12
- Loading branch information
Showing
30 changed files
with
331 additions
and
91 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
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
23 changes: 0 additions & 23 deletions
23
...ain/java/io/novafoundation/nova/feature_staking_impl/data/collators/KnownNovaCollators.kt
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
.../main/java/io/novafoundation/nova/feature_staking_impl/data/config/StakingGlobalConfig.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,5 @@ | ||
package io.novafoundation.nova.feature_staking_impl.data.config | ||
|
||
class StakingGlobalConfig( | ||
val multiStakingApiUrl: String, | ||
) |
11 changes: 11 additions & 0 deletions
11
...ava/io/novafoundation/nova/feature_staking_impl/data/config/api/StakingGlobalConfigApi.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,11 @@ | ||
package io.novafoundation.nova.feature_staking_impl.data.config.api | ||
|
||
import io.novafoundation.nova.feature_staking_impl.BuildConfig | ||
import io.novafoundation.nova.feature_staking_impl.data.config.api.response.StakingGlobalConfigRemote | ||
import retrofit2.http.GET | ||
|
||
interface StakingGlobalConfigApi { | ||
|
||
@GET(BuildConfig.GLOBAL_CONFIG_URL) | ||
suspend fun getStakingGlobalConfig(): StakingGlobalConfigRemote | ||
} |
5 changes: 5 additions & 0 deletions
5
...oundation/nova/feature_staking_impl/data/config/api/response/StakingGlobalConfigRemote.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,5 @@ | ||
package io.novafoundation.nova.feature_staking_impl.data.config.api.response | ||
|
||
class StakingGlobalConfigRemote( | ||
val multiStakingApiUrl: String | ||
) |
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
36 changes: 36 additions & 0 deletions
36
...novafoundation/nova/feature_staking_impl/data/repository/StakingGlobalConfigRepository.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,36 @@ | ||
package io.novafoundation.nova.feature_staking_impl.data.repository | ||
|
||
import io.novafoundation.nova.feature_staking_impl.data.config.StakingGlobalConfig | ||
import io.novafoundation.nova.feature_staking_impl.data.config.api.StakingGlobalConfigApi | ||
import io.novafoundation.nova.feature_staking_impl.data.config.api.response.StakingGlobalConfigRemote | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
|
||
interface StakingGlobalConfigRepository { | ||
|
||
suspend fun getStakingGlobalConfig(): StakingGlobalConfig | ||
} | ||
|
||
class RealStakingGlobalConfigRepository( | ||
private val api: StakingGlobalConfigApi | ||
) : StakingGlobalConfigRepository { | ||
|
||
private val mutex = Mutex() | ||
private var cache: StakingGlobalConfig? = null | ||
|
||
override suspend fun getStakingGlobalConfig(): StakingGlobalConfig { | ||
return mutex.withLock { | ||
if (cache == null) { | ||
cache = api.getStakingGlobalConfig().toDomain() | ||
} | ||
|
||
cache!! | ||
} | ||
} | ||
|
||
private fun StakingGlobalConfigRemote.toDomain(): StakingGlobalConfig { | ||
return StakingGlobalConfig( | ||
multiStakingApiUrl = multiStakingApiUrl | ||
) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...c/main/java/io/novafoundation/nova/feature_staking_impl/data/repository/VaraRepository.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,38 @@ | ||
package io.novafoundation.nova.feature_staking_impl.data.repository | ||
|
||
import io.novafoundation.nova.common.data.network.runtime.binding.asPerQuintill | ||
import io.novafoundation.nova.common.utils.Perbill | ||
import io.novafoundation.nova.runtime.multiNetwork.ChainRegistry | ||
import io.novafoundation.nova.runtime.multiNetwork.chain.model.ChainId | ||
import io.novafoundation.nova.runtime.multiNetwork.getSocket | ||
import io.novasama.substrate_sdk_android.wsrpc.SocketService | ||
import io.novasama.substrate_sdk_android.wsrpc.executeAsync | ||
import io.novasama.substrate_sdk_android.wsrpc.mappers.nonNull | ||
import io.novasama.substrate_sdk_android.wsrpc.mappers.pojo | ||
import io.novasama.substrate_sdk_android.wsrpc.request.runtime.RuntimeRequest | ||
import java.math.BigInteger | ||
|
||
interface VaraRepository { | ||
|
||
suspend fun getVaraInflation(chainId: ChainId): Perbill | ||
} | ||
|
||
class RealVaraRepository( | ||
private val chainRegistry: ChainRegistry | ||
) : VaraRepository { | ||
|
||
override suspend fun getVaraInflation(chainId: ChainId): Perbill { | ||
return chainRegistry.getSocket(chainId).inflationInfo().inflation.asPerQuintill() | ||
} | ||
|
||
private suspend fun SocketService.inflationInfo(): InflationInfo { | ||
return executeAsync(InflationInfoRequest(), mapper = pojo<InflationInfo>().nonNull()) | ||
} | ||
|
||
private class InflationInfoRequest : RuntimeRequest( | ||
method = "stakingRewards_inflationInfo", | ||
params = emptyList() | ||
) | ||
|
||
private class InflationInfo(val inflation: BigInteger) | ||
} |
64 changes: 42 additions & 22 deletions
64
...n/java/io/novafoundation/nova/feature_staking_impl/data/validators/KnownNovaValidators.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,41 +1,61 @@ | ||
package io.novafoundation.nova.feature_staking_impl.data.validators | ||
|
||
import io.novafoundation.nova.common.utils.toHexAccountId | ||
import io.novafoundation.nova.runtime.ext.Geneses | ||
import io.novafoundation.nova.common.utils.filterNotNull | ||
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 KnownNovaValidators { | ||
|
||
fun getValidatorIds(chainId: ChainId): Set<String> | ||
suspend fun getValidatorIds(chainId: ChainId): List<String> | ||
} | ||
|
||
class FixedKnownNovaValidators : KnownNovaValidators { | ||
class RemoteKnownNovaValidators( | ||
private val validatorsApi: NovaValidatorsApi, | ||
private val chainRegistry: ChainRegistry, | ||
) : KnownNovaValidators { | ||
|
||
private val novaValidators by lazy { | ||
val sharedAccounts = sharedValidatorsAccountIdsHex() | ||
private var validatorsByNetwork: Map<String, List<String>>? = null | ||
private val validatorsMutex = Mutex() | ||
|
||
mapOf( | ||
Chain.Geneses.POLKADOT to sharedAccounts, | ||
Chain.Geneses.KUSAMA to kusamaValidators(), | ||
Chain.Geneses.ALEPH_ZERO to sharedAccounts | ||
) | ||
override suspend fun getValidatorIds(chainId: ChainId): List<String> { | ||
return getValidators()[chainId].orEmpty() | ||
} | ||
|
||
override fun getValidatorIds(chainId: ChainId): Set<String> { | ||
return novaValidators[chainId].orEmpty() | ||
private suspend fun getValidators(): Map<String, List<String>> { | ||
return validatorsMutex.withLock { | ||
if (validatorsByNetwork == null) { | ||
validatorsByNetwork = fetchValidators() | ||
} | ||
|
||
requireNotNull(validatorsByNetwork) | ||
} | ||
} | ||
|
||
private suspend fun fetchValidators(): Map<String, List<String>> { | ||
return runCatching { | ||
val chainsById = chainRegistry.chainsById() | ||
|
||
validatorsApi.getValidators().mapValues { (chainId, addresses) -> | ||
chainsById[chainId]?.let { chain -> | ||
addresses.convertAddressesToAccountIds(chain) | ||
} | ||
}.filterNotNull() | ||
}.getOrDefault(emptyMap()) | ||
} | ||
|
||
private fun sharedValidatorsAccountIdsHex(): Set<String> { | ||
return setOf( | ||
"127zarPDhVzmCXVQ7Kfr1yyaa9wsMuJ74GJW9Q7ezHfQEgh6".toHexAccountId() | ||
) | ||
private fun List<String>.convertAddressesToAccountIds(chain: Chain): List<String> { | ||
return mapNotNull { | ||
chain.tryConvertAddressToAccountIdHex(it) | ||
} | ||
} | ||
|
||
private fun kusamaValidators(): Set<String> { | ||
return setOf( | ||
"DhK6qU2U5kDWeJKvPRtmnWRs8ETUGZ9S9QmNmQFuzrNoKm4".toHexAccountId(), | ||
"EtETk1FbrDg7FoAfkREuXT7xHxCjbEf28sBvWf6zfB5wFyV".toHexAccountId() | ||
) | ||
private fun Chain.tryConvertAddressToAccountIdHex(address: String): String? { | ||
return runCatching { accountIdOf(address).toHexString() }.getOrNull() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.novafoundation.nova.feature_staking_impl.data.validators | ||
|
||
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>> | ||
} |
Oops, something went wrong.