@@ -60,8 +60,8 @@ import Foundation
60
60
61
61
class MyLogger : Logger {
62
62
63
- override func log (record : String ? ) {
64
- print (" record : \( record ) " )
63
+ override func log (record : Record ) {
64
+ print (" log event : \( record. get_args () ) " )
65
65
}
66
66
67
67
}
@@ -112,24 +112,22 @@ import Foundation
112
112
113
113
class MyPersister : Persist {
114
114
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 ()
118
118
119
119
// persist monitorBytes to disk, keyed by idBytes
120
120
121
- // simplified result instantiation calls coming shortly!
122
- return Result_NoneChannelMonitorUpdateErrZ ()
121
+ return Result_NoneChannelMonitorUpdateErrZ.ok ()
123
122
}
124
123
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 ()
128
127
129
128
// modify persisted monitorBytes keyed by idBytes on disk
130
129
131
- // simplified result instantiation calls coming shortly!
132
- return Result_NoneChannelMonitorUpdateErrZ ()
130
+ return Result_NoneChannelMonitorUpdateErrZ.ok ()
133
131
}
134
132
135
133
}
@@ -160,7 +158,7 @@ class MyFilter: Filter {
160
158
161
159
override func register_output (output : WatchedOutput) -> Option_C2Tuple_usizeTransactionZZ {
162
160
let scriptPubkeyBytes = output.get_script_pubkey ()
163
- let outpoint = output.get_outpoint ()
161
+ let outpoint = output.get_outpoint ()!
164
162
let txid = outpoint.get_txid ()
165
163
let outputIndex = outpoint.get_index ()
166
164
@@ -169,7 +167,7 @@ class MyFilter: Filter {
169
167
let blockHashBytes = output.get_block_hash ()
170
168
// if block hash bytes are not null, return any transaction spending the output that is found in the corresponding block along with its index
171
169
172
- return Option_C2Tuple_usizeTransactionZZ ( value : nil )
170
+ return Option_C2Tuple_usizeTransactionZZ. none ( )
173
171
}
174
172
}
175
173
```
@@ -189,7 +187,8 @@ let filter = MyFilter()
189
187
``` swift
190
188
// main context (continued)
191
189
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)
193
192
```
194
193
195
194
### KeysManager
@@ -199,12 +198,15 @@ let chainMonitor = ChainMonitor.init(chain_source: filter, broadcaster: broadcas
199
198
200
199
var keyData = Data (count : 32 )
201
200
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 )
203
204
}
204
205
let seed = [UInt8 ](keyData)
205
206
let timestamp_seconds = UInt64 (NSDate ().timeIntervalSince1970 )
206
207
let timestamp_nanos = UInt32 .init (truncating : NSNumber (value : timestamp_seconds * 1000 * 1000 ))
207
208
let keysManager = KeysManager (seed : seed, starting_time_secs : timestamp_seconds, starting_time_nanos : timestamp_nanos)
209
+ let keysInterface = keysManager.as_KeysInterface ()
208
210
```
209
211
210
212
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,
225
227
a random block at a height that does not exist at the time of this writing.
226
228
227
229
``` 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
230
232
```
231
233
232
234
Second, we also need to initialize a default user config, which we simply do like this:
233
235
234
236
``` swift
235
- let userConfig = UserConfig. init ()
237
+ let userConfig = UserConfig ()
236
238
```
237
239
238
- Finally, we can proceed by instantiating the ChannelManager.
240
+ Finally, we can proceed by instantiating the ` ChannelManager ` using ` ChannelManagerConstructor ` .
239
241
240
242
``` swift
241
243
// main context (continued)
242
244
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
262
258
```
263
259
264
- ### NetGraphMsgHandler
260
+ ### NetworkGraph
265
261
266
262
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 ` :
268
264
269
265
``` swift
270
266
// main context (continued)
271
267
272
268
let networkGraph = NetworkGraph (genesis_hash : [UInt8 ](Data (base64Encoded : " AAAAAAAZ1micCFrhZYMek0/3Y65GoqbBcrPxtgqM4m8=" )! ))
273
- let router = NetGraphMsgHandler (chain_access : nil , logger : logger, network_graph : networkGraph)
274
269
```
275
270
276
271
Note that a network graph instance needs to be provided upon initialization, which in turn requires the genesis block hash.
277
272
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
282
274
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 :
284
276
285
277
``` 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)
291
279
```
292
280
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 :
294
282
295
283
``` 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
297
299
```
298
300
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 ` .
300
304
301
305
``` swift
302
306
// main context (continued)
303
307
304
- let peerManager = PeerManager ( message_handler : messageHandler, our_node_secret : nodeSecret, ephemeral_random_data : peerManagerSeed, logger : logger)
308
+ let peerManager = channelManagerConstructor. peerManager
305
309
```
306
310
307
311
Now, all that remains is setting up the actual syscalls that are necessary within
0 commit comments