From d7b53597d5cab12d10d9e06ba8835048ae443ff3 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sun, 3 Jul 2022 14:14:17 +0430 Subject: [PATCH 1/2] bc-gateway: Reduce database calls when syncing deposits --- .../core/service/WalletSyncServiceImpl.kt | 14 +++----- .../impl/AssignedAddressHandlerImpl.kt | 35 ++++++++----------- .../postgres/impl/CurrencyHandlerImpl.kt | 4 +-- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/bc-gateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt b/bc-gateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt index cacf7ff34..01ff7d969 100644 --- a/bc-gateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt +++ b/bc-gateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt @@ -11,10 +11,6 @@ import co.nilin.opex.bcgateway.core.spi.WalletProxy import co.nilin.opex.bcgateway.core.utils.LoggerDelegate import kotlinx.coroutines.async import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.flow.asFlow -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.mapNotNull -import kotlinx.coroutines.flow.toList import org.slf4j.Logger import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -32,15 +28,13 @@ class WalletSyncServiceImpl( @Transactional override suspend fun syncTransfers(transfers: List) = coroutineScope { + val groupedByChain = currencyHandler.fetchAllImplementations().groupBy { it.chain.name } val deposits = transfers.map { async { coroutineScope { - val currencyImpl = async { - currencyHandler.findByChainAndTokenAddress(it.chain, it.tokenAddress) - ?: throw IllegalStateException("Currency implementation not found") - } - val uuid = async { assignedAddressHandler.findUuid(it.receiver.address, it.receiver.memo) } - uuid.await()?.let { it to currencyImpl.await() } + val currencyImpl = groupedByChain[it.chain]?.find { c -> c.tokenAddress == it.tokenAddress } + ?: throw IllegalStateException("Currency implementation not found") + assignedAddressHandler.findUuid(it.receiver.address, it.receiver.memo)?.let { it to currencyImpl } }?.let { (uuid, currencyImpl) -> sendDeposit(uuid, currencyImpl, it) logger.info("Deposit synced for $uuid on ${currencyImpl.currency.symbol} - to ${it.receiver.address}") diff --git a/bc-gateway/bc-gateway-ports/bc-gateway-persister-postgres/src/main/kotlin/co/nilin/opex/bcgateway/ports/postgres/impl/AssignedAddressHandlerImpl.kt b/bc-gateway/bc-gateway-ports/bc-gateway-persister-postgres/src/main/kotlin/co/nilin/opex/bcgateway/ports/postgres/impl/AssignedAddressHandlerImpl.kt index 010ae69ef..61fc0b9b6 100644 --- a/bc-gateway/bc-gateway-ports/bc-gateway-persister-postgres/src/main/kotlin/co/nilin/opex/bcgateway/ports/postgres/impl/AssignedAddressHandlerImpl.kt +++ b/bc-gateway/bc-gateway-ports/bc-gateway-persister-postgres/src/main/kotlin/co/nilin/opex/bcgateway/ports/postgres/impl/AssignedAddressHandlerImpl.kt @@ -23,29 +23,26 @@ class AssignedAddressHandlerImpl( ) : AssignedAddressHandler { override suspend fun fetchAssignedAddresses(user: String, addressTypes: List): List { if (addressTypes.isEmpty()) return emptyList() + val addressTypeMap = addressTypeRepository.findAll().map { aam -> + AddressType(aam.id!!, aam.type, aam.addressRegex, aam.memoRegex) + }.collectMap { it.id }.awaitFirst() return assignedAddressRepository.findByUuidAndAddressType( user, addressTypes.map(AddressType::id) - ) - .map { model -> - AssignedAddress( - model.uuid, model.address, model.memo, - addressTypeRepository - .findById(model.addressTypeId) - .map { aam -> - AddressType(aam.id!!, aam.type, aam.addressRegex, aam.memoRegex) - } - .awaitFirst(), - assignedAddressChainRepository.findByAssignedAddress(model.id!!) - .map { cm -> - chainLoader.fetchChainInfo(cm.chain) - } - .toList().toMutableList() - ) - }.toList() + ).map { model -> + AssignedAddress( + model.uuid, + model.address, + model.memo, + addressTypeMap.getValue(model.addressTypeId), + assignedAddressChainRepository.findByAssignedAddress(model.id!!).map { cm -> + chainLoader.fetchChainInfo(cm.chain) + }.toList().toMutableList() + ) + }.toList() } override suspend fun persist(assignedAddress: AssignedAddress) { - try { + runCatching { assignedAddressRepository.save( AssignedAddressModel( null, @@ -55,8 +52,6 @@ class AssignedAddressHandlerImpl( assignedAddress.type.id ) ).awaitFirst() - } catch (e: Exception) { - } } diff --git a/bc-gateway/bc-gateway-ports/bc-gateway-persister-postgres/src/main/kotlin/co/nilin/opex/bcgateway/ports/postgres/impl/CurrencyHandlerImpl.kt b/bc-gateway/bc-gateway-ports/bc-gateway-persister-postgres/src/main/kotlin/co/nilin/opex/bcgateway/ports/postgres/impl/CurrencyHandlerImpl.kt index 076f18ebf..caf51f8b4 100644 --- a/bc-gateway/bc-gateway-ports/bc-gateway-persister-postgres/src/main/kotlin/co/nilin/opex/bcgateway/ports/postgres/impl/CurrencyHandlerImpl.kt +++ b/bc-gateway/bc-gateway-ports/bc-gateway-persister-postgres/src/main/kotlin/co/nilin/opex/bcgateway/ports/postgres/impl/CurrencyHandlerImpl.kt @@ -147,13 +147,13 @@ class CurrencyHandlerImpl( currencyModel: CurrencyModel? = null ): CurrencyImplementation { val addressTypesModel = chainRepository.findAddressTypesByName(currencyImplementationModel.chain) - val addressTypes = addressTypesModel.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) } + val addressTypes = addressTypesModel.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) }.toList() val currencyModelVal = currencyModel ?: currencyRepository.findBySymbol(currencyImplementationModel.currencySymbol).awaitSingle() val currency = Currency(currencyModelVal.symbol, currencyModelVal.name) return CurrencyImplementation( currency, - Chain(currencyImplementationModel.chain, addressTypes.toList()), + Chain(currencyImplementationModel.chain, addressTypes), currencyImplementationModel.token, currencyImplementationModel.tokenAddress, currencyImplementationModel.tokenName, From 08b8360ea8cf5ae83a96abf1ddc3750cdf95b15b Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sun, 3 Jul 2022 14:28:05 +0430 Subject: [PATCH 2/2] bc-gateway: Add more logs --- .../nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/bc-gateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt b/bc-gateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt index 01ff7d969..2400ab198 100644 --- a/bc-gateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt +++ b/bc-gateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt @@ -28,6 +28,7 @@ class WalletSyncServiceImpl( @Transactional override suspend fun syncTransfers(transfers: List) = coroutineScope { + logger.debug("Received ${transfers.size} number of transfers") val groupedByChain = currencyHandler.fetchAllImplementations().groupBy { it.chain.name } val deposits = transfers.map { async {