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

Use global tag for logging to start #27

Merged
merged 1 commit into from
Nov 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.umlandowallet.data.OnchainWallet
import com.example.umlandowallet.utils.LDKTAG
import java.io.File

private const val TAG = "DispatchActivity"

class DispatchActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -24,7 +23,7 @@ class DispatchActivity : AppCompatActivity() {
val ldkDataDirectory = File(Global.homeDir)
if(!ldkDataDirectory.exists()) {
ldkDataDirectory.mkdir()
Log.i(TAG, "Creating directory at $ldkDataDirectory")
Log.i(LDKTAG, "Creating directory at $ldkDataDirectory")
}

val latestBlockHeight = OnchainWallet.getHeight()
Expand Down Expand Up @@ -54,10 +53,6 @@ class DispatchActivity : AppCompatActivity() {
serializedChannelMonitors
)

Log.i(TAG, "Successfully created/restored wallet with mnemonic $mnemonic")

start(ldkEntropy, latestBlockHeight.toInt(), latestBlockHash, serializedChannelManager, serializedChannelMonitors)

startActivity(Intent(this, MainActivity::class.java))
}
}
9 changes: 6 additions & 3 deletions app/src/main/java/com/example/umlandowallet/HandleEvent.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.example.umlandowallet

import android.util.Log
import com.example.umlandowallet.data.OnchainWallet
import com.example.umlandowallet.utils.LDKTAG
import org.ldk.structs.*
import kotlin.random.Random

fun handleEvent(event: Event) {
if (event is Event.FundingGenerationReady) {
println("FundingGenerationReady")
Log.i(LDKTAG, "FundingGenerationReady")
val funding_spk = event.output_script
if (funding_spk.size == 34 && funding_spk[0].toInt() == 0 && funding_spk[1].toInt() == 32) {
val params = WritableMap()

params.putString("counterparty_node_id", event.counterparty_node_id.toHex())
params.putString("channel_value_satoshis", event.channel_value_satoshis.toString())
params.putString("output_script", event.output_script.toHex())
Log.i(LDKTAG, "Output script is ${event.output_script.toHex()}")
params.putString("temporary_channel_id", event.temporary_channel_id.toHex())
params.putString("user_channel_id", event.user_channel_id.toString())
Global.temporaryChannelId = event.temporary_channel_id
Expand Down Expand Up @@ -54,9 +57,9 @@ fun handleEvent(event: Event) {
}

if (event is Event.ChannelClosed) {
println("ChannelClosed");
Log.i(LDKTAG, "ChannelClosed")
val params = WritableMap()
val reason = event.reason;
val reason = event.reason
params.putString("channel_id", event.channel_id.toHex())
params.putString("user_channel_id", event.user_channel_id.toString())

Expand Down
53 changes: 26 additions & 27 deletions app/src/main/java/com/example/umlandowallet/Start.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.umlandowallet

import android.util.Log
import com.example.umlandowallet.data.remote.Service
import com.example.umlandowallet.utils.LDKTAG
import kotlinx.coroutines.*
import org.ldk.batteries.ChannelManagerConstructor
import org.ldk.enums.ChannelMonitorUpdateStatus
Expand All @@ -19,9 +21,8 @@ fun start(
serializedChannelManager: String,
serializedChannelMonitors: String
) {
println("LDK starting...")

println(org.ldk.impl.version.get_ldk_java_bindings_version())
Log.i(LDKTAG, "LDK starting...")
Log.i(LDKTAG, "This wallet is using the LDK Java bindings version ${org.ldk.impl.version.get_ldk_java_bindings_version()}")

// Estimating fees for on-chain transactions that LDK wants to broadcast.
val feeEstimator: FeeEstimator = FeeEstimator.new_impl(LDKFeeEstimator)
Expand Down Expand Up @@ -62,7 +63,7 @@ fun start(
// This hashmap will later be given to the `ChannelManager` on initialization.
var channelMonitors = arrayOf<ByteArray>()
if (serializedChannelMonitors != "") {
println("LDK: initiating channel monitors...")
Log.i(LDKTAG, "LDK: initiating channel monitors...")
val channelMonitorHexes = serializedChannelMonitors.split(",").toTypedArray()
val channelMonitorList = ArrayList<ByteArray>()
channelMonitorHexes.iterator().forEach {
Expand Down Expand Up @@ -153,7 +154,7 @@ fun start(
// If you want to do the reverse use 10.0.2.2 instead of localhost
Global.nioPeerHandler!!.bind_listener(InetSocketAddress("127.0.0.1", 9777))
} catch (e: Exception) {
println("LDK: can't start, " + e.message)
Log.i(LDKTAG, "LDK: can't start, ${e.message}")
}
}

Expand Down Expand Up @@ -182,7 +183,7 @@ object LDKBroadcaster : BroadcasterInterface.BroadcasterInterfaceInterface {
tx?.let {
GlobalScope.launch {
val txid = service.broadcastTx(tx)
println("We've broadcast a transaction with txid $txid")
Log.i(LDKTAG, "We've broadcast a transaction with txid $txid")
}
} ?: throw(IllegalStateException("Broadcaster attempted to broadcast a null transaction"))
}
Expand All @@ -192,34 +193,32 @@ fun initializeNetworkGraph(genesisBlockHash: ByteArray, logger: Logger) {
val f = File(Global.homeDir + "/" + Global.prefixNetworkGraph)

if (f.exists()) {
println("loading network graph from: ${Global.homeDir + "/" + Global.prefixNetworkGraph}")
Log.i(LDKTAG, "Loading network graph from: ${Global.homeDir + "/" + Global.prefixNetworkGraph}")
val serializedGraph = File(Global.homeDir + "/" + Global.prefixNetworkGraph).readBytes()
val readResult = NetworkGraph.read(serializedGraph, logger)
println("readResult: $readResult")
Log.i(LDKTAG, "ReadResult: $readResult")

if (readResult is Result_NetworkGraphDecodeErrorZ.Result_NetworkGraphDecodeErrorZ_OK) {
Global.router = readResult.res
Global.p2pGossipSync = P2PGossipSync.of(readResult.res, Option_AccessZ.none(), logger)
println("loaded network graph ok")
Log.i(LDKTAG, "Loaded network graph ok")
} else {
println("network graph load failed")
Log.i(LDKTAG,"Network graph load failed")
if (readResult is Result_NetworkGraphDecodeErrorZ.Result_NetworkGraphDecodeErrorZ_Err) {
println(readResult.err)
Log.i(LDKTAG, "${readResult.err}")
}

// error, creating from scratch
Global.router =
NetworkGraph.of(genesisBlockHash.reversedArray(), logger)
Global.router = NetworkGraph.of(genesisBlockHash.reversedArray(), logger)
}
} else {
// first run, creating from scratch
Global.router =
NetworkGraph.of(genesisBlockHash.reversedArray(), logger)
Global.router = NetworkGraph.of(genesisBlockHash.reversedArray(), logger)
}
}


// To create a Perisister for our Channel Monitors we need to provide an object that implements the PersistInterface
// To create a Persister for our Channel Monitors we need to provide an object that implements the PersistInterface
// which has 2 functions persist_new_channel & update_persisted_channel
object LDKPersister : Persist.PersistInterface {
override fun persist_new_channel(
Expand All @@ -228,7 +227,7 @@ object LDKPersister : Persist.PersistInterface {
updateId: MonitorUpdateId?
): ChannelMonitorUpdateStatus? {
return try {
println("persist_new_channel")
Log.i(LDKTAG, "persist_new_channel")
if (data != null) {
if (id != null) {
File(
Expand All @@ -253,7 +252,7 @@ object LDKPersister : Persist.PersistInterface {
data: ChannelMonitor?,
updateId: MonitorUpdateId
): ChannelMonitorUpdateStatus? {
println("update_persisted_channel")
Log.i(LDKTAG, "update_persisted_channel")
return try {
if (id != null) {
if (update != null) {
Expand All @@ -277,33 +276,33 @@ object LDKPersister : Persist.PersistInterface {
// Responsible for backing up channel_manager bytes
object ChannelManagerEventHandler : ChannelManagerConstructor.EventHandler {
override fun handle_event(event: Event) {
println("Getting ready to handle event")
Log.i(LDKTAG, "Getting ready to handle event")
handleEvent(event)
}

override fun persist_manager(channel_manager_bytes: ByteArray?) {
println("persist_manager")
Log.i(LDKTAG, "persist_manager")
if (channel_manager_bytes != null) {
val hex = channel_manager_bytes.toHex()
println("channel_manager_bytes: $hex")
Log.i(LDKTAG, "channel_manager_bytes: $hex")
File(Global.homeDir + "/" + Global.prefixChannelManager).writeText(channel_manager_bytes.toHex())
}
}

override fun persist_network_graph(network_graph: ByteArray?) {
println("persist_network_graph")
Log.i(LDKTAG, "persist_network_graph")
if (Global.prefixNetworkGraph != "" && network_graph !== null) {
val hex = network_graph.toHex()
println("persist_network_graph_bytes: $hex")
Log.i(LDKTAG, "persist_network_graph_bytes: $hex")
File(Global.homeDir + "/" + Global.prefixNetworkGraph).writeText(network_graph.toHex())
}
}

override fun persist_scorer(scorer: ByteArray?) {
println("scorer")
Log.i(LDKTAG, "scorer")
if (Global.prefixScorer != "" && scorer !== null) {
val hex = scorer.toHex()
println("scorer_bytes: $hex")
Log.i(LDKTAG, "scorer_bytes: $hex")
File(Global.homeDir + "/" + Global.prefixScorer).writeText(scorer.toHex())
}
}
Expand All @@ -313,7 +312,7 @@ object ChannelManagerEventHandler : ChannelManagerConstructor.EventHandler {
// useful if you pre-filter blocks or use compact filters. Otherwise, LDK will need full blocks.
object LDKTxFilter : Filter.FilterInterface {
override fun register_tx(txid: ByteArray, script_pubkey: ByteArray) {
println("register_tx")
Log.i(LDKTAG, "register_tx")
val params = WritableMap()
params.putString("txid", txid.reversedArray().toHex())
params.putString("script_pubkey", script_pubkey.toHex())
Expand All @@ -322,7 +321,7 @@ object LDKTxFilter : Filter.FilterInterface {
}

override fun register_output(output: WatchedOutput) {
println("register_output")
Log.i(LDKTAG, "register_output")
val params = WritableMap()
val blockHash = output._block_hash
if (blockHash is ByteArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package com.example.umlandowallet.data
import android.util.Log
import com.example.umlandowallet.Global
import com.example.umlandowallet.toHex
import com.example.umlandowallet.utils.LDKTAG
import org.bitcoindevkit.*
import java.io.File

private const val TAG = "OnchainWallet"

// The onchain wallet is currently always in regtest mode
object OnchainWallet {
private lateinit var onchainWallet: Wallet
Expand All @@ -31,7 +30,7 @@ object OnchainWallet {
Network.REGTEST,
databaseConfig,
)
Log.i(TAG, "Successfully created/restored wallet with mnemonic $mnemonic")
Log.i(LDKTAG, "Successfully created/restored wallet with mnemonic $mnemonic")
}

fun getNewAddress(): String {
Expand Down Expand Up @@ -69,7 +68,7 @@ object OnchainWallet {
val child: DescriptorSecretKey = bip32RootKey.derive(derivationPath)
val entropy: ByteArray = child.secretBytes().toUByteArray().toByteArray()

Log.i(TAG, "Entropy used for LDK is ${entropy.toHex()}")
Log.i(LDKTAG, "Entropy used for LDK is ${entropy.toHex()}")
return entropy
}

Expand All @@ -84,7 +83,7 @@ object OnchainWallet {
.finish(onchainWallet)
sign(psbt)
val rawTx = psbt.extractTx().toUByteArray().toByteArray()
println("The raw funding tx is ${rawTx.toHex()}")
Log.i(LDKTAG, "The raw funding tx is ${rawTx.toHex()}")
return rawTx
}

Expand All @@ -109,7 +108,7 @@ object OnchainWallet {
return File(Global.homeDir + "/" + "mnemonic.txt").readText()
} catch (e: Throwable) {
// if mnemonic doesn't exist, generate one and save it
Log.i(TAG, "No mnemonic backup, we'll create a new wallet")
Log.i(LDKTAG, "No mnemonic backup, we'll create a new wallet")
val mnemonic = generateMnemonic(WordCount.WORDS12)
File(Global.homeDir + "/" + "mnemonic.txt").writeText(mnemonic)
return mnemonic
Expand All @@ -122,7 +121,7 @@ object OnchainWallet {

object LogProgress: Progress {
override fun update(progress: Float, message: String?) {
Log.d(TAG, "updating wallet $progress $message")
Log.d(LDKTAG, "updating wallet $progress $message")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class AccessImpl(
) {
val service = Service.create()

val height = service.getlatestBlockHeight()
val hash = service.getlatestBlockHash()
val height = service.getLatestBlockHeight()
val hash = service.getLatestBlockHash()
val header = service.getHeader(hash)

channelManager.as_Confirm().best_block_updated(header.toByteArray(), height)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.umlandowallet.data.remote

import android.util.Log
import com.example.umlandowallet.Global
import com.example.umlandowallet.data.MerkleProof
import com.example.umlandowallet.data.Tx
import com.example.umlandowallet.data.TxStatus
import com.example.umlandowallet.toByteArray
import com.example.umlandowallet.toHex
import com.example.umlandowallet.utils.LDKTAG
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
Expand Down Expand Up @@ -55,16 +57,16 @@ class ServiceImpl(private val client: HttpClient) : Service {
}

override suspend fun connectPeer(pubkeyHex: String, hostname: String, port: Int): Boolean {
println("LDK: attempting to connect to peer $pubkeyHex")
Log.i(LDKTAG, "LDK: attempting to connect to peer $pubkeyHex")
return try {
Global.nioPeerHandler!!.connect(
pubkeyHex.toByteArray(),
InetSocketAddress(hostname, port), 5555
)
println("LDK: successfully connected to peer $pubkeyHex")
Log.i(LDKTAG, "LDK: successfully connected to peer $pubkeyHex")
true
} catch (e: IOException) {
println("connectPeer exception: " + e.message)
Log.i(LDKTAG, "connectPeer exception: " + e.message)
false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.example.umlandowallet.R
import com.example.umlandowallet.data.OnchainWallet

private const val TAG = "SettingsScreen"
import com.example.umlandowallet.utils.LDKTAG

@Composable
fun SettingsScreen(navController: NavController) {
Expand Down Expand Up @@ -133,14 +132,13 @@ fun SettingsScreen(navController: NavController) {
// Get new address
SettingButton(
label = "Get new address",
onClick = { Log.i(TAG, "New bitcoin address: ${OnchainWallet.getNewAddress()}") }
// onClick = { Log.i(TAG, "New bitcoin address: ${Global.wallet!!.getAddress(AddressIndex.NEW).address}") }
onClick = { Log.i(LDKTAG, "New bitcoin address: ${OnchainWallet.getNewAddress()}") }
)

// Get balance
SettingButton(
label = "Get balance",
onClick = { Log.i(TAG, "On chain balance: ${OnchainWallet.getBalance()}") }
onClick = { Log.i(LDKTAG, "On chain balance: ${OnchainWallet.getBalance()}") }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import com.example.umlandowallet.ChannelManagerEventHandler
import com.example.umlandowallet.Global
import com.example.umlandowallet.data.OnchainWallet
import com.example.umlandowallet.data.remote.Access
import com.example.umlandowallet.utils.LDKTAG
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

private const val TAG = "WalletScreen"

@Composable
fun WalletScreen() {
Column(
Expand Down Expand Up @@ -68,7 +67,7 @@ fun WalletScreen() {
ChannelManagerEventHandler, Global.scorer!!)
}

Log.i(TAG, "Wallet synced")
Log.i(LDKTAG, "Wallet synced")
},
modifier = Modifier
.padding(start = 24.dp, end = 24.dp)
Expand Down