-
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.
- Loading branch information
Showing
19 changed files
with
234 additions
and
32 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
app/src/androidTest/java/io/novafoundation/nova/NftFullSyncIntegrationTest.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,56 @@ | ||
package io.novafoundation.nova | ||
|
||
import android.content.Context | ||
import androidx.test.core.app.ApplicationProvider | ||
import io.novafoundation.nova.common.di.FeatureUtils | ||
import io.novafoundation.nova.feature_account_api.di.AccountFeatureApi | ||
import io.novafoundation.nova.feature_nft_api.NftFeatureApi | ||
import io.novafoundation.nova.feature_nft_api.data.model.Nft | ||
import io.novafoundation.nova.feature_nft_api.data.model.isFullySynced | ||
import io.novafoundation.nova.runtime.di.RuntimeApi | ||
import io.novafoundation.nova.runtime.di.RuntimeComponent | ||
import io.novafoundation.nova.runtime.multiNetwork.connection.ChainConnection | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.takeWhile | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Test | ||
|
||
class NftFullSyncIntegrationTest { | ||
|
||
private val nftApi = FeatureUtils.getFeature<NftFeatureApi>( | ||
ApplicationProvider.getApplicationContext<Context>(), | ||
NftFeatureApi::class.java | ||
) | ||
|
||
private val accountApi = FeatureUtils.getFeature<AccountFeatureApi>( | ||
ApplicationProvider.getApplicationContext<Context>(), | ||
AccountFeatureApi::class.java | ||
) | ||
|
||
private val runtimeApi = FeatureUtils.getFeature<RuntimeComponent>( | ||
ApplicationProvider.getApplicationContext<Context>(), | ||
RuntimeApi::class.java | ||
) | ||
|
||
private val externalRequirementFlow = runtimeApi.externalRequirementFlow() | ||
|
||
@Test | ||
fun testUniquesIntegration(): Unit = runBlocking { | ||
externalRequirementFlow.emit(ChainConnection.ExternalRequirement.ALLOWED) | ||
|
||
val metaAccount = accountApi.accountUseCase().getSelectedMetaAccount() | ||
|
||
val nftRepository = nftApi.nftRepository | ||
|
||
nftRepository.initialNftSync(metaAccount) | ||
|
||
nftRepository.allNftFlow(metaAccount) | ||
.map { nfts -> nfts.filter { it.type is Nft.Type.Uniques && !it.isFullySynced } } | ||
.takeWhile { it.isNotEmpty() } | ||
.onEach { unsyncedNfts -> | ||
unsyncedNfts.forEach { nftRepository.fullNftSync(it) } | ||
}.launchIn(this) | ||
} | ||
} |
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
41 changes: 23 additions & 18 deletions
41
...va/io/novafoundation/nova/feature_nft_impl/data/network/distributed/FileStorageAdapter.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,46 @@ | ||
package io.novafoundation.nova.feature_nft_impl.data.network.distributed | ||
|
||
enum class FileStorage(val protocol: String, val defaultHttpsGateway: String?) { | ||
IPFS("ipfs", "https://cloudflare-ipfs.com"), | ||
HTTPS("https", null), | ||
HTTP("http", null); | ||
enum class FileStorage(val prefix: String, val defaultHttpsGateway: String?) { | ||
IPFS("ipfs://ipfs/", "https://rmrk.mypinata.cloud/ipfs/"), | ||
HTTPS("https://", null), | ||
HTTP("http://", null); | ||
|
||
init { | ||
require(!defaultHttpsGateway.orEmpty().endsWith("/")) { | ||
"Gateway should not end with '/' separator" | ||
} | ||
require(!protocol.endsWith("://")) { | ||
"Protocol should not end with '://' separator" | ||
} | ||
validateHttpsGateway(defaultHttpsGateway) | ||
} | ||
} | ||
|
||
val FileStorage.protocolPrefix | ||
get() = "$protocol://" | ||
private fun validateHttpsGateway(gateway: String?) { | ||
require(gateway == null || gateway.endsWith("/")) { | ||
"Gateway should end with '/' separator" | ||
} | ||
} | ||
|
||
object FileStorageAdapter { | ||
|
||
fun String.adoptFileStorageLinkToHttps( | ||
customGateways: Map<FileStorage, String> = emptyMap(), | ||
noProtocolStorage: FileStorage = FileStorage.IPFS | ||
) = adaptToHttps(this, customGateways, noProtocolStorage) | ||
|
||
fun adaptToHttps( | ||
distributedStorageLink: String, | ||
customGateways: Map<FileStorage, String> = emptyMap() | ||
): String? { | ||
customGateways: Map<FileStorage, String> = emptyMap(), | ||
noProtocolStorage: FileStorage = FileStorage.IPFS | ||
): String { | ||
val distributedStorage = FileStorage.values().firstOrNull { storage -> | ||
distributedStorageLink.pointsTo(storage) | ||
} ?: return null | ||
} ?: noProtocolStorage | ||
|
||
val gateway = customGateways[distributedStorage] ?: distributedStorage.defaultHttpsGateway | ||
?: return distributedStorageLink | ||
|
||
val path = distributedStorageLink.removePrefix(distributedStorage.protocolPrefix) | ||
validateHttpsGateway(gateway) | ||
|
||
val path = distributedStorageLink.removePrefix(distributedStorage.prefix) | ||
|
||
return "$gateway/$path" | ||
return "$gateway$path" | ||
} | ||
|
||
private fun String.pointsTo(fileStorage: FileStorage) = startsWith(fileStorage.protocolPrefix) | ||
private fun String.pointsTo(fileStorage: FileStorage) = startsWith(fileStorage.prefix) | ||
} |
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
27 changes: 27 additions & 0 deletions
27
...impl/src/main/java/io/novafoundation/nova/feature_nft_impl/data/source/JobOrchestrator.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,27 @@ | ||
package io.novafoundation.nova.feature_nft_impl.data.source | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import java.util.Collections | ||
import java.util.concurrent.ConcurrentHashMap | ||
import kotlin.coroutines.coroutineContext | ||
|
||
class JobOrchestrator { | ||
|
||
private val runningJobs: MutableSet<String> = Collections.newSetFromMap(ConcurrentHashMap()) | ||
|
||
private val mutex = Mutex() | ||
|
||
suspend fun runUniqueJob(id: String, action: suspend () -> Unit) = mutex.withLock { | ||
if (id in runningJobs) { | ||
return@withLock | ||
} | ||
|
||
runningJobs += id | ||
|
||
CoroutineScope(coroutineContext).async { action() } | ||
.invokeOnCompletion { runningJobs -= id } | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...nft-impl/src/main/java/io/novafoundation/nova/feature_nft_impl/data/source/NftProvider.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,12 @@ | ||
package io.novafoundation.nova.feature_nft_impl.data.source | ||
|
||
import io.novafoundation.nova.feature_account_api.domain.model.MetaAccount | ||
import io.novafoundation.nova.feature_nft_api.data.model.Nft | ||
import io.novafoundation.nova.runtime.multiNetwork.chain.model.Chain | ||
|
||
interface NftProvider { | ||
|
||
suspend fun initialNftsSync(chain: Chain, metaAccount: MetaAccount) | ||
|
||
suspend fun nftFullSync(nft: Nft) | ||
} |
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.