Skip to content

Commit

Permalink
Adds tx broadcaster snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Conor Okus committed Mar 21, 2023
1 parent 36ab19f commit 869cb87
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 15 deletions.
10 changes: 5 additions & 5 deletions docs/tutorials/building-a-node-with-ldk/connect-to-peers.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ loop {

<template v-slot:kotlin>

```kotlin
```java
val nioPeerHandler = channelManagerConstructor.nio_peer_handler
val port = 9777
nioPeerHandler.bind_listener(InetSocketAddress("127.0.0.1", port))
Expand Down Expand Up @@ -76,7 +76,7 @@ match lightning_net_tokio::connect_outbound(Arc::clone(&peer_manager), pubkey, a

<template v-slot:kotlin>

```kotlin
```java
try {
// Connect and wait for the handshake to complete.
val address: SocketAddress = InetSocketAddress(hostname, port)
Expand All @@ -87,7 +87,7 @@ try {
val peerNodeIds = peerManager._peer_node_ids

} catch (e: IOException) {
// Handle failure to successfully connect to a peer.
// Handle failure when connecting to a peer.
}
```

Expand All @@ -96,8 +96,8 @@ try {

**Dependencies:** `PeerManager`

**References:** [Rust `lightning-net-tokio` docs](https://docs.rs/lightning-net-tokio/*/lightning_net_tokio/), [Rust `PeerManager` docs](https://docs.rs/lightning/*/lightning/ln/peer_handler/struct.PeerManager.html), [Java `NioPeerHandler` docs](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/batteries/NioPeerHandler.java),
[Java `PeerManager` docs](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/PeerManager.java),
**References:** [Rust `lightning-net-tokio` docs](https://docs.rs/lightning-net-tokio/*/lightning_net_tokio/), [Rust `PeerManager` docs](https://docs.rs/lightning/*/lightning/ln/peer_handler/struct.PeerManager.html), [Java `NioPeerHandler` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/batteries/NioPeerHandler.java),
[Java `PeerManager` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/PeerManager.java),



Expand Down
3 changes: 2 additions & 1 deletion docs/tutorials/building-a-node-with-ldk/handling-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To start handling events in your application, run:

<template v-slot:kotlin>

```kotlin
```java
import org.ldk.structs.Event

if (event is Event.PaymentSent) {
Expand All @@ -44,3 +44,4 @@ To start handling events in your application, run:
</template>
</CodeSwitcher>

References: [Rust `Event` docs](https://docs.rs/lightning/0.0.114/lightning/util/events/enum.Event.html), [Java `Event` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Event.java)
81 changes: 76 additions & 5 deletions docs/tutorials/building-a-node-with-ldk/opening-a-channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ match event {

<template v-slot:kotlin>

```kotlin
```java
// After the peer responds with an `accept_channel` message, an
// Event.FundingGenerationReady event will be generated.

Expand All @@ -127,24 +127,95 @@ if (event is Event.FundingGenerationReady) {
}
}

fun buildFundingTx(value: Long, script: ByteArray): ByteArray {
fun buildFundingTx(value: Long, script: ByteArray): Transaction {
val scriptListUByte: List<UByte> = script.toUByteArray().asList()
val outputScript = Script(scriptListUByte)
val (psbt, _) = TxBuilder()
.addRecipient(outputScript, value.toULong())
.feeRate(4.0F)
.finish(onchainWallet)
sign(psbt)
val rawTx = psbt.extractTx().toUByteArray().toByteArray()
return rawTx
val rawTx = psbt.extractTx().serialize().toUByteArray().toByteArray()
return psbt.extractTx()
}
```

</template>

</CodeSwitcher>

**References:** [Rust `FundingGenerationReady` docs](https://docs.rs/lightning/*/lightning/util/events/enum.Event.html#variant.FundingGenerationReady),
**References:** [Rust `FundingGenerationReady` docs](https://docs.rs/lightning/*/lightning/util/events/enum.Event.html#variant.FundingGenerationReady), [Java `FundingGenerationReady` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Event.java#L95)
# Broadcasting the Funding Transaction

After crafting the funding transaction you'll need to send it to the Bitcoin network where it will hopefully be mined and added to the blockchain. You'll need to watch this transaction and wait for a minimum of 6 confirmations before the channel is ready to use.

<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin'}">
<template v-slot:rust>

```rust
// Using BDK (Bitcoin Dev Kit) to broadcast a transaction via the esplora client
impl BroadcasterInterface for YourTxBroadcaster {
fn broadcast_transaction(&self, tx: &Transaction) {
let locked_runtime = self.tokio_runtime.read().unwrap();
if locked_runtime.as_ref().is_none() {
log_error!(self.logger, "Failed to broadcast transaction: No runtime.");
return;
}

let res = tokio::task::block_in_place(move || {
locked_runtime
.as_ref()
.unwrap()
.block_on(async move { self.blockchain.broadcast(tx).await })
});

match res {
Ok(_) => {}
Err(err) => {
log_error!(self.logger, "Failed to broadcast transaction: {}", err);
}
}
}
}

```

</template>

<template v-slot:kotlin>

```java

// Using BDK (Bitcoin Dev Kit) to broadcast a transaction via the esplora client
object YourTxBroadcaster : BroadcasterInterface.BroadcasterInterfaceInterface {
override fun broadcast_transaction(tx: ByteArray?) {
val esploraURL = "esploraUrl"
val blockchainConfig = BlockchainConfig.Esplora(EsploraConfig(esploraURL, null, 5u, 20u, null))
val blockchain = Blockchain(blockchainConfig)

val uByteArray = UByteArray(tx.size) { tx[it].toUByte() }
val transaction = Transaction(uByteArray.toList())

tx?.let {
CoroutineScope(Dispatchers.IO).launch {
blockchain.broadcast(transaction)
}
} ?: throw(IllegalStateException("Broadcaster attempted to broadcast a null transaction"))

}
}

```

</template>
</CodeSwitcher>

::: tip Keep LDK in sync

Remember if you are restarting and have open channels then you should [let LDK know about the latest channel state.](./setting-up-a-channel-manager/#sync-channelmonitors-and-channelmanager-to-chain-tip)

:::




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ LDK's `lightning_block_sync` sample module as in the example above: the high-lev

**Esplora**

Alternatively, you can use LDK's `lightning-block-sync` crate. This provides utilities for syncing LDK via the transaction-based Confirm interface.
Alternatively, you can use LDK's `lightning-transaction-sync` crate. This provides utilities for syncing LDK via the transaction-based `Confirm` interface.

**Electrum/Esplora**

Expand Down Expand Up @@ -792,5 +792,5 @@ val p2pGossip : P2PGossipSync = P2PGossipSync.of(networkGraph, Option_AccessZ.no

**Optional dependency:** `Access`, a source of chain information. Recommended to be able to verify channels before adding them to the internal network graph.

**References:** [Rust `P2PGossipSync` docs](https://docs.rs/lightning/*/lightning/routing/gossip/struct.P2PGossipSync.html), [`Access` docs](https://docs.rs/lightning/*/lightning/chain/trait.Access.html), [Java `P2PGossipSync` docs](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/P2PGossipSync.java), [Rust `RapidGossipSync` docs](https://docs.rs/lightning-rapid-gossip-sync/*/lightning_rapid_gossip_sync/), [Java `RapidGossipSync` docs](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/RapidGossipSync.java)
**References:** [Rust `P2PGossipSync` docs](https://docs.rs/lightning/*/lightning/routing/gossip/struct.P2PGossipSync.html), [`Access` docs](https://docs.rs/lightning/*/lightning/chain/trait.Access.html), [Java `P2PGossipSync` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/P2PGossipSync.java), [Rust `RapidGossipSync` docs](https://docs.rs/lightning-rapid-gossip-sync/*/lightning_rapid_gossip_sync/), [Java `RapidGossipSync` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/RapidGossipSync.java)

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ To add a PeerManager to your application, run:

<template v-slot:kotlin>

```kotlin
```java
import org.ldk.structs.PeerManager

val peerManager: PeerManager = channelManagerConstructor.peer_manager;
Expand All @@ -47,5 +47,5 @@ To add a PeerManager to your application, run:

**Dependencies:** `ChannelManager`, `RoutingMessageHandler`, `KeysManager`, random bytes, `Logger`

**References:** [Rust `PeerManager` docs](https://docs.rs/lightning/*/lightning/ln/peer_handler/struct.PeerManager.html), [Rust `RoutingMessageHandler` docs](https://docs.rs/lightning/*/lightning/ln/msgs/trait.RoutingMessageHandler.html), [Java `PeerManager` docs](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/PeerManager.java), [Java `RoutingMessageHandler` docs](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/RoutingMessageHandler.java)
**References:** [Rust `PeerManager` docs](https://docs.rs/lightning/*/lightning/ln/peer_handler/struct.PeerManager.html), [Rust `RoutingMessageHandler` docs](https://docs.rs/lightning/*/lightning/ln/msgs/trait.RoutingMessageHandler.html), [Java `PeerManager` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/PeerManager.java), [Java `RoutingMessageHandler` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/RoutingMessageHandler.java)

0 comments on commit 869cb87

Please sign in to comment.