Skip to content

Commit 869cb87

Browse files
author
Conor Okus
committed
Adds tx broadcaster snippets
1 parent 36ab19f commit 869cb87

File tree

5 files changed

+87
-15
lines changed

5 files changed

+87
-15
lines changed

docs/tutorials/building-a-node-with-ldk/connect-to-peers.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ loop {
3333

3434
<template v-slot:kotlin>
3535

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

7777
<template v-slot:kotlin>
7878

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

8989
} catch (e: IOException) {
90-
// Handle failure to successfully connect to a peer.
90+
// Handle failure when connecting to a peer.
9191
}
9292
```
9393

@@ -96,8 +96,8 @@ try {
9696

9797
**Dependencies:** `PeerManager`
9898

99-
**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),
100-
[Java `PeerManager` docs](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/PeerManager.java),
99+
**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),
100+
[Java `PeerManager` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/PeerManager.java),
101101

102102

103103

docs/tutorials/building-a-node-with-ldk/handling-events.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ To start handling events in your application, run:
2525

2626
<template v-slot:kotlin>
2727

28-
```kotlin
28+
```java
2929
import org.ldk.structs.Event
3030

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

47+
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)

docs/tutorials/building-a-node-with-ldk/opening-a-channel.md

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ match event {
107107

108108
<template v-slot:kotlin>
109109

110-
```kotlin
110+
```java
111111
// After the peer responds with an `accept_channel` message, an
112112
// Event.FundingGenerationReady event will be generated.
113113

@@ -127,24 +127,95 @@ if (event is Event.FundingGenerationReady) {
127127
}
128128
}
129129

130-
fun buildFundingTx(value: Long, script: ByteArray): ByteArray {
130+
fun buildFundingTx(value: Long, script: ByteArray): Transaction {
131131
val scriptListUByte: List<UByte> = script.toUByteArray().asList()
132132
val outputScript = Script(scriptListUByte)
133133
val (psbt, _) = TxBuilder()
134134
.addRecipient(outputScript, value.toULong())
135135
.feeRate(4.0F)
136136
.finish(onchainWallet)
137137
sign(psbt)
138-
val rawTx = psbt.extractTx().toUByteArray().toByteArray()
139-
return rawTx
138+
val rawTx = psbt.extractTx().serialize().toUByteArray().toByteArray()
139+
return psbt.extractTx()
140140
}
141141
```
142142

143143
</template>
144144

145145
</CodeSwitcher>
146146

147-
**References:** [Rust `FundingGenerationReady` docs](https://docs.rs/lightning/*/lightning/util/events/enum.Event.html#variant.FundingGenerationReady),
147+
**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)
148+
# Broadcasting the Funding Transaction
149+
150+
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.
151+
152+
<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin'}">
153+
<template v-slot:rust>
154+
155+
```rust
156+
// Using BDK (Bitcoin Dev Kit) to broadcast a transaction via the esplora client
157+
impl BroadcasterInterface for YourTxBroadcaster {
158+
fn broadcast_transaction(&self, tx: &Transaction) {
159+
let locked_runtime = self.tokio_runtime.read().unwrap();
160+
if locked_runtime.as_ref().is_none() {
161+
log_error!(self.logger, "Failed to broadcast transaction: No runtime.");
162+
return;
163+
}
164+
165+
let res = tokio::task::block_in_place(move || {
166+
locked_runtime
167+
.as_ref()
168+
.unwrap()
169+
.block_on(async move { self.blockchain.broadcast(tx).await })
170+
});
171+
172+
match res {
173+
Ok(_) => {}
174+
Err(err) => {
175+
log_error!(self.logger, "Failed to broadcast transaction: {}", err);
176+
}
177+
}
178+
}
179+
}
180+
181+
```
182+
183+
</template>
184+
185+
<template v-slot:kotlin>
186+
187+
```java
188+
189+
// Using BDK (Bitcoin Dev Kit) to broadcast a transaction via the esplora client
190+
object YourTxBroadcaster : BroadcasterInterface.BroadcasterInterfaceInterface {
191+
override fun broadcast_transaction(tx: ByteArray?) {
192+
val esploraURL = "esploraUrl"
193+
val blockchainConfig = BlockchainConfig.Esplora(EsploraConfig(esploraURL, null, 5u, 20u, null))
194+
val blockchain = Blockchain(blockchainConfig)
195+
196+
val uByteArray = UByteArray(tx.size) { tx[it].toUByte() }
197+
val transaction = Transaction(uByteArray.toList())
198+
199+
tx?.let {
200+
CoroutineScope(Dispatchers.IO).launch {
201+
blockchain.broadcast(transaction)
202+
}
203+
} ?: throw(IllegalStateException("Broadcaster attempted to broadcast a null transaction"))
204+
205+
}
206+
}
207+
208+
```
209+
210+
</template>
211+
</CodeSwitcher>
212+
213+
::: tip Keep LDK in sync
214+
215+
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)
216+
217+
:::
218+
148219

149220

150221

docs/tutorials/building-a-node-with-ldk/setting-up-a-channel-manager.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ LDK's `lightning_block_sync` sample module as in the example above: the high-lev
731731

732732
**Esplora**
733733

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

736736
**Electrum/Esplora**
737737

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

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

795-
**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)
795+
**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)
796796

docs/tutorials/building-a-node-with-ldk/setting-up-a-peer-manager.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To add a PeerManager to your application, run:
3434

3535
<template v-slot:kotlin>
3636

37-
```kotlin
37+
```java
3838
import org.ldk.structs.PeerManager
3939

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

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

50-
**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)
50+
**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)
5151

0 commit comments

Comments
 (0)