Skip to content

Commit 7a5e731

Browse files
committed
create util methods and a channel manager constructor battery
1 parent 9f56bad commit 7a5e731

File tree

3 files changed

+105
-21
lines changed

3 files changed

+105
-21
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// ChannelManagerConstructor.swift
3+
// LDKSwiftARC
4+
//
5+
// Created by Arik Sosman on 5/19/21.
6+
//
7+
8+
import Foundation
9+
10+
enum InvalidSerializedDataError: Error {
11+
case runtimeError
12+
}
13+
14+
class ChannelManagerConstructor {
15+
16+
public let channelManager: ChannelManager
17+
/**
18+
* The latest block has the channel manager saw. If this is non-null it is a 32-byte block hash.
19+
* You should sync the blockchain starting with the block that builds on this block.
20+
*/
21+
public let channel_manager_latest_block_hash: [UInt8]
22+
/**
23+
* A list of ChannelMonitors and the last block they each saw. You should sync the blockchain on each individually
24+
* starting with the block that builds on the hash given.
25+
* After doing so (and syncing the blockchain on the channel manager as well), you should call chain_sync_completed()
26+
* and then continue to normal application operation.
27+
*/
28+
public private(set) var channel_monitors: [(ChannelMonitor, [UInt8])]
29+
30+
private let chain_monitor: ChainMonitor
31+
32+
init(channel_manager_serialized: [UInt8], channel_monitors_serialized: [[UInt8]], keys_interface: KeysInterface, fee_estimator: FeeEstimator, chain_monitor: ChainMonitor, filter: Filter?, tx_broadcaster: BroadcasterInterface, logger: Logger) throws {
33+
34+
var monitors: [LDKChannelMonitor] = []
35+
self.channel_monitors = []
36+
for index in 0..<channel_monitors_serialized.count {
37+
let currentSerializedChannelMonitor = channel_monitors_serialized[index]
38+
let res: Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ = UtilMethods.constructor_BlockHashChannelMonitorZ_read(ser: currentSerializedChannelMonitor, arg: keys_interface)
39+
if res.cOpaqueStruct?.result_ok != true {
40+
throw InvalidSerializedDataError.runtimeError
41+
}
42+
let value: LDKCResult_C2Tuple_BlockHashChannelMonitorZDecodeErrorZPtr = res.cOpaqueStruct!.contents
43+
let a: LDKThirtyTwoBytes = value.result!.pointee.a
44+
let b: LDKChannelMonitor = value.result!.pointee.b
45+
let currentChannelMonitor = ChannelMonitor(pointer: b)
46+
monitors.append(b)
47+
self.channel_monitors.append((currentChannelMonitor, Bindings.LDKThirtyTwoBytes_to_array(nativeType: a)))
48+
}
49+
50+
let res = UtilMethods.constructor_BlockHashChannelManagerZ_read(ser: channel_manager_serialized, arg_keys_manager: keys_interface, arg_fee_estimator: fee_estimator, arg_chain_monitor: chain_monitor.as_Watch(), arg_tx_broadcaster: tx_broadcaster, arg_logger: logger, arg_default_config: UserConfig(), arg_channel_monitors: monitors)
51+
if res.cOpaqueStruct?.result_ok != true {
52+
throw InvalidSerializedDataError.runtimeError
53+
}
54+
let latestBlockHash = Bindings.LDKThirtyTwoBytes_to_array(nativeType: res.cOpaqueStruct!.contents.result.pointee.a)
55+
let channelManager = ChannelManager(pointer: res.cOpaqueStruct!.contents.result.pointee.b)
56+
57+
self.channelManager = channelManager
58+
self.channel_manager_latest_block_hash = latestBlockHash
59+
self.chain_monitor = chain_monitor
60+
61+
if let filter = filter {
62+
for (currentMonitor, _) in self.channel_monitors {
63+
currentMonitor.load_outputs_to_watch(filter: filter)
64+
}
65+
}
66+
67+
}
68+
69+
}

bindings/batteries/UtilMethods.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// UtilMethods.swift
3+
// LDKSwiftARC
4+
//
5+
// Created by Arik Sosman on 5/19/21.
6+
//
7+
8+
import Foundation
9+
10+
class UtilMethods {
11+
/**
12+
* Read a C2Tuple_BlockHashChannelMonitorZ from a byte array, created by C2Tuple_BlockHashChannelMonitorZ_write
13+
*/
14+
public class func constructor_BlockHashChannelMonitorZ_read(ser: [UInt8], arg: KeysInterface) -> Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ {
15+
let ret = withUnsafePointer(to: arg.cOpaqueStruct!) { (pointer: UnsafePointer<LDKKeysInterface>) -> LDKCResult_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ in
16+
return C2Tuple_BlockHashChannelMonitorZ_read(Bindings.new_LDKu8slice(array: ser), pointer)
17+
}
18+
return Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ(pointer: ret)
19+
}
20+
21+
/**
22+
* Read a C2Tuple_BlockHashChannelManagerZ from a byte array, created by C2Tuple_BlockHashChannelManagerZ_write
23+
*/
24+
public class func constructor_BlockHashChannelManagerZ_read(ser: [UInt8], arg_keys_manager: KeysInterface, arg_fee_estimator: FeeEstimator, arg_chain_monitor: Watch, arg_tx_broadcaster: BroadcasterInterface, arg_logger: Logger, arg_default_config: UserConfig, arg_channel_monitors: [LDKChannelMonitor]) -> Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ {
25+
26+
let args = ChannelManagerReadArgs(keys_manager: arg_keys_manager, fee_estimator: arg_fee_estimator, chain_monitor: arg_chain_monitor, tx_broadcaster: arg_tx_broadcaster, logger: arg_logger, default_config: arg_default_config, channel_monitors: arg_channel_monitors)
27+
let ret = C2Tuple_BlockHashChannelManagerZ_read(Bindings.new_LDKu8slice(array: ser), args.cOpaqueStruct!)
28+
29+
return Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ(pointer: ret)
30+
}
31+
}

docs/setup.md

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,6 @@ Instantiate it:
182182
let filter = MyFilter()
183183
```
184184

185-
### KeysInterface
186-
187-
Define the subclass:
188-
189-
```swift
190-
// MyKeysInterface.swift
191-
192-
import Foundation
193-
194-
class MyKeysInterface: KeysInterface {
195-
override func get_node_secret() -> [UInt8] {
196-
return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
197-
}
198-
}
199-
```
200-
201-
We won't be instantiating it directly, but we will need the keys interface later on.
202-
203185
## Phase 2: Initializations
204186

205187
### ChainMonitor
@@ -225,10 +207,12 @@ let timestamp_nanos = UInt32.init(truncating: NSNumber(value: timestamp_seconds
225207
let keysManager = KeysManager(seed: seed, starting_time_secs: timestamp_seconds, starting_time_nanos: timestamp_nanos)
226208
```
227209

228-
We will keep needing to pass around a keysInterface instance, so let's prepare it right here:
210+
We will keep needing to pass around a keysInterface instance, and we will also need to
211+
pass its node secret to the peer manager initialization, so let's prepare it right here:
229212

230213
```swift
231-
let keysInterface = MyKeysInterface(pointer: keysManager.as_KeysInterface().cOpaqueStruct!)
214+
let keysInterface = keysManager.as_KeysInterface()
215+
let nodeSecret = Bindings.LDKSecretKey_to_array(nativeType: keysInterface.cOpaqueStruct!.get_node_secret(keysInterface.cOpaqueStruct!.this_arg))
232216
```
233217

234218
This is a bit inelegant, but we will be providing simpler casting methods for user-provided types shortly.
@@ -299,7 +283,7 @@ And finally, let's instantiate the peer manager itself:
299283
```swift
300284
// main context (continued)
301285

302-
let peerManager = PeerManager(message_handler: messageHandler, our_node_secret: keysInterface.get_node_secret(), ephemeral_random_data: peerManagerSeed, logger: logger)
286+
let peerManager = PeerManager(message_handler: messageHandler, our_node_secret: nodeSecret, ephemeral_random_data: peerManagerSeed, logger: logger)
303287
```
304288

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

0 commit comments

Comments
 (0)