Skip to content

Commit

Permalink
Merge pull request #12 from jvsena42/update-daemon-2
Browse files Browse the repository at this point in the history
Update daemon 2
  • Loading branch information
jvsena42 authored Oct 28, 2024
2 parents c8edcdd + ec64173 commit d2d6001
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 24 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

lint {
abortOnError = false
}

buildTypes {
release {
isMinifyEnabled = false
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/florestad/Florestad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@file:Suppress("NAME_SHADOWING")

package uniffi.floresta;
package com.florestad

// Common helper code.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.github.jvsena42.floresta.domain.model.TxType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import org.bitcoindevkit.Address
import org.bitcoindevkit.AddressInfo
Expand All @@ -27,6 +29,7 @@ import org.bitcoindevkit.WordCount
import org.rustbitcoin.bitcoin.Amount
import org.rustbitcoin.bitcoin.FeeRate
import org.rustbitcoin.bitcoin.Network
import kotlin.time.Duration.Companion.seconds
import org.bitcoindevkit.ChainPosition as BdkChainPosition

class WalletManager(
Expand Down Expand Up @@ -96,7 +99,7 @@ class WalletManager(
)

walletRepository.saveMnemonic(mnemonic.toString())
florestaRpc.loadDescriptor(descriptor.toString())
florestaRpc.loadDescriptor(descriptor.toString()).firstOrNull()
}

suspend fun loadWallet(): Result<Unit> {
Expand All @@ -117,8 +120,6 @@ class WalletManager(
connection = dbConnection
)

florestaRpc.loadDescriptor(descriptor.toString())

Log.d(TAG, "loadWallet: in network ${wallet.network()}")

return Result.success(Unit)
Expand Down Expand Up @@ -155,26 +156,34 @@ class WalletManager(
changeDescriptor.toStringWithSecret()
)
walletRepository.saveMnemonic(mnemonic.toString())
florestaRpc.loadDescriptor(descriptor.toString())
florestaRpc.loadDescriptor(descriptor.toString()).firstOrNull()
}

private fun fullScan() {
val fullScanRequest = wallet.startFullScan().build()
val update: Update = blockchainClient.fullScan(
fullScanRequest = fullScanRequest,
stopGap = 100u,
batchSize = 10u,
fetchPrevTxouts = true
)
wallet.applyUpdate(update)
wallet.persist(dbConnection)
private suspend fun fullScan() {
Log.d(TAG, "fullScan: ")
try {
val fullScanRequest = wallet.startFullScan().build()
val update: Update = blockchainClient.fullScan(
fullScanRequest = fullScanRequest,
stopGap = 20u,
batchSize = 10u,
fetchPrevTxouts = true
)
wallet.applyUpdate(update)
wallet.persist(dbConnection)
} catch (e: Exception) {
Log.e(TAG, "fullScan error:", e)
}
}

fun sync() {
suspend fun sync() {
if (!walletRepository.doesWalletExist()) return

if (fullScanRequired) {
Log.d(TAG, "sync: fullScanRequired")
fullScan()
fullScanRequired = false
delay(15.seconds)
fullScan()
} else {
Log.d(TAG, "sync: normal sync")
val syncRequest = wallet.startSyncWithRevealedSpks().build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.github.jvsena42.floresta.domain.floresta

import android.util.Log
import com.florestad.Florestad
import com.github.jvsena42.floresta.domain.bitcoin.WalletRepository
import com.github.jvsena42.floresta.presentation.util.filterInternalBrackets
import org.bitcoindevkit.Descriptor
import org.rustbitcoin.bitcoin.Network
import uniffi.floresta.Config
import uniffi.floresta.Florestad
import uniffi.floresta.Network as FlorestaNetwork
import com.florestad.Config
import com.florestad.Network as FlorestaNetwork
import kotlin.let

interface FlorestaDaemon {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.google.gson.Gson
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
Expand Down Expand Up @@ -41,9 +42,10 @@ class FlorestaRpcImpl(
arguments.put(descriptor)

getBlockchainInfo().first().onSuccess { result ->
Log.d(TAG, "loadDescriptor: loading initial block: ${result.result.ibd}")
if (result.result.ibd) {
delay(10.seconds)
loadDescriptor(descriptor)
loadDescriptor(descriptor).firstOrNull()
} else {
emit(
sendJsonRpcRequest(
Expand All @@ -55,7 +57,7 @@ class FlorestaRpcImpl(
}
}.onFailure {
delay(30.seconds)
loadDescriptor(descriptor)
loadDescriptor(descriptor).firstOrNull()
}
}

Expand All @@ -69,6 +71,7 @@ class FlorestaRpcImpl(
arguments
).fold(
onSuccess = { json ->
Log.d(TAG, "getPeerInfo: ")
emit(
Result.success(
gson.fromJson(json.toString(), GetPeerInfoResponse::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class HomeViewModel(
} else {
Log.d(TAG, "setup: Wallet does not exists")
walletManager.createWallet()
// walletManager.recoverWallet("bird unique ridge dose run problem scare label teach return inflict struggle")
}
}

Expand All @@ -72,7 +73,6 @@ class HomeViewModel(
delay(5.seconds)
updateUI()
walletManager.sync()
delay(5.seconds)
syncInLoop()
}
}
Expand Down
Binary file modified app/src/main/jniLibs/arm64-v8a/libuniffi_floresta.so
Binary file not shown.
Binary file modified app/src/main/jniLibs/x86_64/libuniffi_floresta.so
Binary file not shown.

0 comments on commit d2d6001

Please sign in to comment.