Skip to content

Commit 84af35c

Browse files
authored
Merge pull request #454 from cache-storeid-walletindex
refactor: cache VSS storeId by walletIndex
2 parents 8de3bf4 + c1a4a6c commit 84af35c

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

app/src/main/java/to/bitkit/data/backup/VssBackupClient.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ class VssBackupClient @Inject constructor(
2626
) {
2727
private val isSetup = CompletableDeferred<Unit>()
2828

29-
suspend fun setup() = withContext(bgDispatcher) {
29+
suspend fun setup(walletIndex: Int = 0) = withContext(bgDispatcher) {
3030
try {
3131
withTimeout(30.seconds) {
3232
Logger.debug("VSS client setting up…", context = TAG)
3333
val vssUrl = Env.vssServerUrl
3434
val lnurlAuthServerUrl = Env.lnurlAuthServerUrl
35+
val vssStoreId = vssStoreIdProvider.getVssStoreId(walletIndex)
3536
Logger.verbose("Building VSS client with vssUrl: '$vssUrl'")
3637
Logger.verbose("Building VSS client with lnurlAuthServerUrl: '$lnurlAuthServerUrl'")
3738
if (lnurlAuthServerUrl.isNotEmpty()) {
@@ -41,15 +42,15 @@ class VssBackupClient @Inject constructor(
4142

4243
vssNewClientWithLnurlAuth(
4344
baseUrl = vssUrl,
44-
storeId = vssStoreIdProvider.getVssStoreId(),
45+
storeId = vssStoreId,
4546
mnemonic = mnemonic,
4647
passphrase = passphrase,
4748
lnurlAuthServerUrl = lnurlAuthServerUrl,
4849
)
4950
} else {
5051
vssNewClient(
5152
baseUrl = vssUrl,
52-
storeId = vssStoreIdProvider.getVssStoreId(),
53+
storeId = vssStoreId,
5354
)
5455
}
5556
isSetup.complete(Unit)

app/src/main/java/to/bitkit/data/backup/VssStoreIdProvider.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ import to.bitkit.data.keychain.Keychain
55
import to.bitkit.env.Env
66
import to.bitkit.utils.Logger
77
import to.bitkit.utils.ServiceError
8+
import java.util.concurrent.ConcurrentHashMap
89
import javax.inject.Inject
910
import javax.inject.Singleton
1011

1112
@Singleton
1213
class VssStoreIdProvider @Inject constructor(
1314
private val keychain: Keychain,
1415
) {
15-
@Volatile
16-
private var cachedStoreId: String? = null
16+
private val cacheMap: MutableMap<Int, String> = ConcurrentHashMap()
1717

18-
fun getVssStoreId(): String {
19-
cachedStoreId?.let { return it }
20-
21-
return synchronized(this) {
22-
cachedStoreId?.let { return it }
18+
fun getVssStoreId(walletIndex: Int = 0): String {
19+
synchronized(this) {
20+
cacheMap[walletIndex]?.let { return it }
2321

2422
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name) ?: throw ServiceError.MnemonicNotFound
2523
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
@@ -30,12 +28,16 @@ class VssStoreIdProvider @Inject constructor(
3028
passphrase = passphrase,
3129
)
3230

33-
Logger.info("VSS store id: '$storeId'", context = TAG)
34-
cachedStoreId = storeId
35-
storeId
31+
cacheMap[walletIndex] = storeId
32+
Logger.info("VSS store id setup for wallet[$walletIndex]: '$storeId'", context = TAG)
33+
return storeId
3634
}
3735
}
3836

37+
fun clearCache(walletIndex: Int = 0) {
38+
cacheMap.remove(walletIndex)
39+
}
40+
3941
companion object {
4042
private const val TAG = "VssStoreIdProvider"
4143
}

app/src/main/java/to/bitkit/repositories/WalletRepo.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import org.lightningdevkit.ldknode.Event
1818
import to.bitkit.data.AppDb
1919
import to.bitkit.data.CacheStore
2020
import to.bitkit.data.SettingsStore
21+
import to.bitkit.data.backup.VssStoreIdProvider
2122
import to.bitkit.data.entities.TagMetadataEntity
2223
import to.bitkit.data.keychain.Keychain
2324
import to.bitkit.di.BgDispatcher
@@ -50,6 +51,7 @@ class WalletRepo @Inject constructor(
5051
private val lightningRepo: LightningRepo,
5152
private val cacheStore: CacheStore,
5253
private val deriveBalanceStateUseCase: DeriveBalanceStateUseCase,
54+
private val vssStoreIdProvider: VssStoreIdProvider,
5355
) {
5456
private val repoScope = CoroutineScope(bgDispatcher + SupervisorJob())
5557

@@ -242,6 +244,7 @@ class WalletRepo @Inject constructor(
242244
suspend fun wipeWallet(walletIndex: Int = 0): Result<Unit> = withContext(bgDispatcher) {
243245
try {
244246
keychain.wipe()
247+
vssStoreIdProvider.clearCache(walletIndex)
245248
db.clearAllTables()
246249
settingsStore.reset()
247250
cacheStore.reset()

app/src/main/java/to/bitkit/services/LightningService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class LightningService @Inject constructor(
104104

105105
Logger.debug("Building node…")
106106

107-
val vssStoreId = vssStoreIdProvider.getVssStoreId()
107+
val vssStoreId = vssStoreIdProvider.getVssStoreId(walletIndex)
108108

109109
ServiceQueue.LDK.background {
110110
node = try {

app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import to.bitkit.data.AppDb
2020
import to.bitkit.data.CacheStore
2121
import to.bitkit.data.SettingsData
2222
import to.bitkit.data.SettingsStore
23+
import to.bitkit.data.backup.VssStoreIdProvider
2324
import to.bitkit.data.keychain.Keychain
2425
import to.bitkit.models.BalanceState
2526
import to.bitkit.services.CoreService
@@ -46,6 +47,7 @@ class WalletRepoTest : BaseUnitTest() {
4647
private val lightningRepo: LightningRepo = mock()
4748
private val cacheStore: CacheStore = mock()
4849
private val deriveBalanceStateUseCase: DeriveBalanceStateUseCase = mock()
50+
private val vssStoreIdProvider = mock<VssStoreIdProvider>()
4951

5052
@Before
5153
fun setUp() {
@@ -75,6 +77,7 @@ class WalletRepoTest : BaseUnitTest() {
7577
lightningRepo = lightningRepo,
7678
cacheStore = cacheStore,
7779
deriveBalanceStateUseCase = deriveBalanceStateUseCase,
80+
vssStoreIdProvider = vssStoreIdProvider,
7881
)
7982

8083
@Test

0 commit comments

Comments
 (0)