Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update daemon 2 #12

Merged
merged 11 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading