Skip to content

Commit a3e33cd

Browse files
authored
Merge pull request #19 from lightningdevkit/doc_update_0.0.14
Update docs to use ChannelManagerConstructor-based instantiations.
2 parents 6345e1d + 62421b9 commit a3e33cd

File tree

1 file changed

+60
-56
lines changed

1 file changed

+60
-56
lines changed

docs/setup.md

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ import Foundation
6060

6161
class MyLogger: Logger {
6262

63-
override func log(record: String?) {
64-
print("record: \(record)")
63+
override func log(record: Record) {
64+
print("log event: \(record.get_args())")
6565
}
6666

6767
}
@@ -112,24 +112,22 @@ import Foundation
112112

113113
class MyPersister: Persist {
114114

115-
override func persist_new_channel(id: OutPoint, data: ChannelMonitor) -> Result_NoneChannelMonitorUpdateErrZ {
116-
let idBytes: [UInt8] = id.write(obj: id)
117-
let monitorBytes: [UInt8] = data.write(obj: data)
115+
override func persist_new_channel(channel_id: OutPoint, data: ChannelMonitor, update_id: MonitorUpdateId) -> Result_NoneChannelMonitorUpdateErrZ {
116+
let idBytes: [UInt8] = channel_id.write()
117+
let monitorBytes: [UInt8] = data.write()
118118

119119
// persist monitorBytes to disk, keyed by idBytes
120120

121-
// simplified result instantiation calls coming shortly!
122-
return Result_NoneChannelMonitorUpdateErrZ()
121+
return Result_NoneChannelMonitorUpdateErrZ.ok()
123122
}
124123

125-
override func update_persisted_channel(id: OutPoint, update: ChannelMonitorUpdate, data: ChannelMonitor) -> Result_NoneChannelMonitorUpdateErrZ {
126-
let idBytes: [UInt8] = id.write(obj: id)
127-
let monitorBytes: [UInt8] = data.write(obj: data)
124+
override func update_persisted_channel(channel_id: OutPoint, update: ChannelMonitorUpdate, data: ChannelMonitor, update_id: MonitorUpdateId) -> Result_NoneChannelMonitorUpdateErrZ {
125+
let idBytes: [UInt8] = channel_id.write()
126+
let monitorBytes: [UInt8] = data.write()
128127

129128
// modify persisted monitorBytes keyed by idBytes on disk
130129

131-
// simplified result instantiation calls coming shortly!
132-
return Result_NoneChannelMonitorUpdateErrZ()
130+
return Result_NoneChannelMonitorUpdateErrZ.ok()
133131
}
134132

135133
}
@@ -160,7 +158,7 @@ class MyFilter: Filter {
160158

161159
override func register_output(output: WatchedOutput) -> Option_C2Tuple_usizeTransactionZZ {
162160
let scriptPubkeyBytes = output.get_script_pubkey()
163-
let outpoint = output.get_outpoint()
161+
let outpoint = output.get_outpoint()!
164162
let txid = outpoint.get_txid()
165163
let outputIndex = outpoint.get_index()
166164

@@ -169,7 +167,7 @@ class MyFilter: Filter {
169167
let blockHashBytes = output.get_block_hash()
170168
// if block hash bytes are not null, return any transaction spending the output that is found in the corresponding block along with its index
171169

172-
return Option_C2Tuple_usizeTransactionZZ(value: nil)
170+
return Option_C2Tuple_usizeTransactionZZ.none()
173171
}
174172
}
175173
```
@@ -189,7 +187,8 @@ let filter = MyFilter()
189187
```swift
190188
// main context (continued)
191189

192-
let chainMonitor = ChainMonitor.init(chain_source: filter, broadcaster: broadcaster, logger: logger, feeest: feeEstimator, persister: persister)
190+
let filterOption = Option_FilterZ(value: filter)
191+
let chainMonitor = ChainMonitor(chain_source: filterOption.dangle(), broadcaster: broadcaster, logger: logger, feeest: feeEstimator, persister: persister)
193192
```
194193

195194
### KeysManager
@@ -199,12 +198,15 @@ let chainMonitor = ChainMonitor.init(chain_source: filter, broadcaster: broadcas
199198

200199
var keyData = Data(count: 32)
201200
keyData.withUnsafeMutableBytes {
202-
SecRandomCopyBytes(kSecRandomDefault, 32, $0.baseAddress!)
201+
// returns 0 on success
202+
let didCopySucceed = SecRandomCopyBytes(kSecRandomDefault, 32, $0.baseAddress!)
203+
assert(didCopySucceed == 0)
203204
}
204205
let seed = [UInt8](keyData)
205206
let timestamp_seconds = UInt64(NSDate().timeIntervalSince1970)
206207
let timestamp_nanos = UInt32.init(truncating: NSNumber(value: timestamp_seconds * 1000 * 1000))
207208
let keysManager = KeysManager(seed: seed, starting_time_secs: timestamp_seconds, starting_time_nanos: timestamp_nanos)
209+
let keysInterface = keysManager.as_KeysInterface()
208210
```
209211

210212
We will keep needing to pass around a keysInterface instance, and we will also need to
@@ -225,83 +227,85 @@ First, we need the current block height and hash. For the sake of this example,
225227
a random block at a height that does not exist at the time of this writing.
226228

227229
```swift
228-
let bestBlock = BestBlock(block_hash: [UInt8](Data(base64Encoded: "AAAAAAAAAAAABe5Xh25D12zkQuLAJQbBeLoF1tEQqR8=")!), height: 700123)
229-
let chainParameters = ChainParameters(network_arg: LDKNetwork_Bitcoin, best_block_arg: bestBlock)
230+
let latestBlockHash = [UInt8](Data(base64Encoded: "AAAAAAAAAAAABe5Xh25D12zkQuLAJQbBeLoF1tEQqR8=")!)
231+
let latestBlockHeight = 700123
230232
```
231233

232234
Second, we also need to initialize a default user config, which we simply do like this:
233235

234236
```swift
235-
let userConfig = UserConfig.init()
237+
let userConfig = UserConfig()
236238
```
237239

238-
Finally, we can proceed by instantiating the ChannelManager.
240+
Finally, we can proceed by instantiating the `ChannelManager` using `ChannelManagerConstructor`.
239241

240242
```swift
241243
// main context (continued)
242244

243-
let channelManager = ChannelManager.init(fee_est: feeEstimator, chain_monitor: chainMonitor.as_Watch(), tx_broadcaster: broadcaster, logger: logger, keys_manager: keysInterface, config: userConfig, params: chainParameters)
244-
```
245-
246-
#### Serializing and restoring a ChannelManager
247-
248-
If you need to serialize a channel manager, you can simply call its write method on itself:
249-
250-
```swift
251-
let serializedChannelManager: [UInt8] = channelManager.write(obj: channelManager)
252-
```
253-
254-
If you have a channel manager you previously serialized, you can restore it like this:
255-
256-
```swift
257-
let serialized_channel_manager: [UInt8] = [2, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 0, 10, 174, 219, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 238, 87, 135, 110, 67, 215, 108, 228, 66, 226, 192, 37, 6, 193, 120, 186, 5, 214, 209, 16, 169, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] // <insert bytes you would have written in following the later step "Persist channel manager">
258-
let serializedChannelMonitors: [[UInt8]] = []
259-
let channel_manager_constructor = try ChannelManagerConstructor(channel_manager_serialized: serialized_channel_manager, channel_monitors_serialized: serializedChannelMonitors, keys_interface: keysInterface, fee_estimator: feeEstimator, chain_monitor: chainMonitor, filter: filter, tx_broadcaster: broadcaster, logger: logger)
260-
261-
let channel_manager = channel_manager_constructor.channelManager;
245+
let channelManagerConstructor = ChannelManagerConstructor(
246+
network: LDKNetwork_Bitcoin,
247+
config: userConfig,
248+
current_blockchain_tip_hash: latestBlockHash,
249+
current_blockchain_tip_height: latestBlockHeight,
250+
keys_interface: keysInterface,
251+
fee_estimator: feeEstimator,
252+
chain_monitor: chainMonitor,
253+
net_graph: nil, // see `NetworkGraph`
254+
tx_broadcaster: broadcaster,
255+
logger: logger
256+
)
257+
let channelManager = channelManagerConstructor.channelManager
262258
```
263259

264-
### NetGraphMsgHandler
260+
### NetworkGraph
265261

266262
If you intend to use the LDK's built-in routing algorithm, you will need to instantiate
267-
a graph message handler:
263+
a `NetworkGraph` that can later be passed to the `ChannelManagerConstructor`:
268264

269265
```swift
270266
// main context (continued)
271267

272268
let networkGraph = NetworkGraph(genesis_hash: [UInt8](Data(base64Encoded: "AAAAAAAZ1micCFrhZYMek0/3Y65GoqbBcrPxtgqM4m8=")!))
273-
let router = NetGraphMsgHandler(chain_access: nil, logger: logger, network_graph: networkGraph)
274269
```
275270

276271
Note that a network graph instance needs to be provided upon initialization, which in turn requires the genesis block hash.
277272

278-
### PeerHandler
279-
280-
Finally, let's instantiate the peer handler. It requires a seed for ephemeral key generation,
281-
as well as a hybrid message handler for routing and channel events.
273+
#### Serializing and restoring a ChannelManager
282274

283-
Let's first set up the seed:
275+
If you need to serialize a channel manager, you can simply call its write method on itself:
284276

285277
```swift
286-
var keyData2 = Data(count: 32)
287-
keyData2.withUnsafeMutableBytes {
288-
SecRandomCopyBytes(kSecRandomDefault, 32, $0.baseAddress!)
289-
}
290-
let peerManagerSeed = [UInt8](keyData2)
278+
let serializedChannelManager: [UInt8] = channelManager.write(obj: channelManager)
291279
```
292280

293-
Next, let's prepare the combined message handler:
281+
If you have a channel manager you previously serialized, you can restore it like this:
294282

295283
```swift
296-
let messageHandler = MessageHandler(chan_handler_arg: channelManager.as_ChannelMessageHandler(), route_handler_arg: router.as_RoutingMessageHandler())
284+
let serializedChannelManager: [UInt8] = [2, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 0, 10, 174, 219, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 238, 87, 135, 110, 67, 215, 108, 228, 66, 226, 192, 37, 6, 193, 120, 186, 5, 214, 209, 16, 169, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] // <insert bytes you would have written in following the later step "Persist channel manager">
285+
let serializedChannelMonitors: [[UInt8]] = []
286+
let channelManagerConstructor = try ChannelManagerConstructor(
287+
channel_manager_serialized: serializedChannelManager,
288+
channel_monitors_serialized: serializedChannelMonitors,
289+
keys_interface: keysInterface,
290+
fee_estimator: feeEstimator,
291+
chain_monitor: chainMonitor,
292+
filter: filter,
293+
net_graph: nil, // or networkGraph
294+
tx_broadcaster: broadcaster,
295+
logger: logger
296+
)
297+
298+
let channelManager = channelManagerConstructor.channelManager
297299
```
298300

299-
And finally, let's instantiate the peer manager itself:
301+
### PeerHandler
302+
303+
Finally, let's get the peer handler. It has conveniently already been instantiated by the `ChannelManagerConstructor`.
300304

301305
```swift
302306
// main context (continued)
303307

304-
let peerManager = PeerManager(message_handler: messageHandler, our_node_secret: nodeSecret, ephemeral_random_data: peerManagerSeed, logger: logger)
308+
let peerManager = channelManagerConstructor.peerManager
305309
```
306310

307311
Now, all that remains is setting up the actual syscalls that are necessary within

0 commit comments

Comments
 (0)