Skip to content

Commit d1d262e

Browse files
authored
Merge pull request #113 from arik-so/arik/2023/07/wrapper-signer-tests
Fix occasional trait return values' premature deallocation
2 parents 2ca0b8e + 905e2aa commit d1d262e

19 files changed

+221
-52
lines changed

ci/LDKSwift/Tests/LDKSwiftTests/HumanObjectPeerTestInstance.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class HumanObjectPeerTestInstance {
1919
//
2020
public var useFilter: Bool = false
2121
public var useRouter: Bool = false
22+
public var useWrappedSignerProvider: Bool = false
2223
public var shouldRecipientRejectPayment: Bool = false
2324
public var ephemeralNetworkGraphForScorer: Bool = false
2425
public var reserializedProbabilisticScorer: Bool = false
@@ -31,7 +32,7 @@ public class HumanObjectPeerTestInstance {
3132
// public var use_nio_peer_handler: Bool = false;
3233

3334
private class func listCustomizeableProperties() -> [String] {
34-
return ["useFilter", "useRouter", "shouldRecipientRejectPayment", "ephemeralNetworkGraphForScorer", "reserializedProbabilisticScorer"]
35+
return ["useFilter", "useRouter", "useWrappedSignerProvider", "shouldRecipientRejectPayment", "ephemeralNetworkGraphForScorer", "reserializedProbabilisticScorer"]
3536
}
3637

3738
public class func combinationCount() -> UInt {
@@ -57,6 +58,7 @@ public class HumanObjectPeerTestInstance {
5758
let monitors: [String: ChannelMonitor]
5859
private(set) var filter: Filter?
5960
private(set) var explicitKeysManager: KeysManager!
61+
private(set) var wrappedSignerProvider: SignerProvider?
6062
private(set) var router: GossipSync!
6163
private(set) var channelManager: ChannelManager!
6264
private(set) var peerManager: PeerManager!
@@ -288,10 +290,15 @@ public class HumanObjectPeerTestInstance {
288290
let keysManager = KeysManager(seed: keySeed, startingTimeSecs: timestamp_seconds, startingTimeNanos: timestamp_nanos)
289291
self.explicitKeysManager = keysManager
290292

293+
if master.configuration.useWrappedSignerProvider {
294+
let keysManager = WrappedSignerProviderTests.MyKeysManager(seed: keySeed, startingTimeSecs: timestamp_seconds, startingTimeNanos: timestamp_nanos)
295+
self.wrappedSignerProvider = keysManager.signerProvider;
296+
}
297+
291298
if master.configuration.useRouter {
292299
let networkGraph = NetworkGraph(network: .Regtest, logger: self.logger)
293300
self.router = GossipSync.initWithP2P(a: P2PGossipSync(networkGraph: networkGraph, utxoLookup: nil, logger: self.logger))
294-
}else{
301+
} else {
295302
self.router = GossipSync.none()
296303
}
297304
}
@@ -317,18 +324,20 @@ public class HumanObjectPeerTestInstance {
317324
}
318325
let score = probabalisticScorer.asScore()
319326
let multiThreadedScorer = MultiThreadedLockableScore(score: score)
320-
327+
328+
let signerProvider = master.configuration.useWrappedSignerProvider ? self.wrappedSignerProvider! : self.explicitKeysManager.asSignerProvider()
321329
let constructionParameters = ChannelManagerConstructionParameters(
322330
config: UserConfig.initWithDefault(),
323331
entropySource: self.explicitKeysManager.asEntropySource(),
324332
nodeSigner: self.explicitKeysManager.asNodeSigner(),
325-
signerProvider: self.explicitKeysManager.asSignerProvider(),
333+
signerProvider: signerProvider,
326334
feeEstimator: self.feeEstimator,
327335
chainMonitor: self.chainMonitor!,
328336
txBroadcaster: self.txBroadcaster,
329337
logger: self.logger,
330338
scorer: multiThreadedScorer
331339
)
340+
332341
self.constructor = ChannelManagerConstructor(network: .Bitcoin, currentBlockchainTipHash: [UInt8](repeating: 0, count: 32), currentBlockchainTipHeight: 0, netGraph: graph, params: constructionParameters)
333342

334343
self.constructor?.chainSyncCompleted(persister: TestChannelManagerPersister(master: self))
@@ -345,7 +354,7 @@ public class HumanObjectPeerTestInstance {
345354
do {
346355
// channel manager constructor is mandatory
347356
let graph = NetworkGraph(network: .Bitcoin, logger: self.logger)
348-
357+
349358
let constructionParameters = ChannelManagerConstructionParameters(
350359
config: UserConfig.initWithDefault(),
351360
entropySource: self.explicitKeysManager.asEntropySource(),

ci/LDKSwift/Tests/LDKSwiftTests/LDKSwiftTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,13 @@ class LDKSwiftTests: XCTestCase {
475475
config.useRouter = (i & (1 << 2)) != 0
476476
print("useRouter: \(config.useRouter)")
477477

478-
config.ephemeralNetworkGraphForScorer = (i & (1 << 3)) != 0
478+
config.useWrappedSignerProvider = (i & (1 << 3)) != 0
479+
print("useWrappedSignerProvider: \(config.useWrappedSignerProvider)")
480+
481+
config.ephemeralNetworkGraphForScorer = (i & (1 << 4)) != 0
479482
print("ephemeralNetworkGraphForScorer: \(config.ephemeralNetworkGraphForScorer)")
480483

481-
config.reserializedProbabilisticScorer = (i & (1 << 4)) != 0
484+
config.reserializedProbabilisticScorer = (i & (1 << 5)) != 0
482485
print("reserializedProbabilisticScorer: \(config.reserializedProbabilisticScorer)")
483486

484487
let instance = HumanObjectPeerTestInstance(configuration: config)
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
//
2+
// LDK
3+
//
4+
// Created by Arik Sosman on 7/27/23.
5+
//
6+
7+
#if SWIFT_PACKAGE
8+
import LDKSwift
9+
import LDKHeaders
10+
#endif
11+
import XCTest
12+
13+
class WrappedSignerProviderTests: XCTestCase {
14+
15+
override func setUpWithError() throws {
16+
// Put setup code here. This method is called before the invocation of each test method in the class.
17+
Bindings.setLogThreshold(severity: .DEBUG)
18+
}
19+
20+
override func tearDownWithError() throws {
21+
// Put teardown code here. This method is called after the invocation of each test method in the class.
22+
}
23+
24+
func testChannelManagerConstruction() {
25+
let feeEstimator = TestFeeEstimator()
26+
let logger = TestLogger()
27+
let broadcaster = TestBroadcasterInterface()
28+
let persister = TestPersister()
29+
let filter = TestFilter()
30+
31+
let chainMonitor = ChainMonitor(chainSource: filter, broadcaster: broadcaster, logger: logger, feeest: feeEstimator, persister: persister)
32+
33+
let seed = [UInt8](Data(base64Encoded: "//////////////////////////////////////////8=")!)
34+
let timestampSeconds = UInt64(NSDate().timeIntervalSince1970)
35+
let timestampNanos = UInt32.init(truncating: NSNumber(value: timestampSeconds * 1000 * 1000))
36+
let myKeysManager = MyKeysManager(seed: seed, startingTimeSecs: timestampSeconds, startingTimeNanos: timestampNanos)
37+
38+
let handshakeConfig = ChannelHandshakeConfig.initWithDefault()
39+
handshakeConfig.setMinimumDepth(val: 1)
40+
handshakeConfig.setAnnouncedChannel(val: false)
41+
42+
let handshakeLimits = ChannelHandshakeLimits.initWithDefault()
43+
handshakeLimits.setForceAnnouncedChannelPreference(val: false)
44+
45+
let userConfig = UserConfig.initWithDefault()
46+
userConfig.setChannelHandshakeConfig(val: handshakeConfig)
47+
userConfig.setChannelHandshakeLimits(val: handshakeLimits)
48+
userConfig.setAcceptInboundChannels(val: true)
49+
50+
let network = Network.Testnet
51+
let netGraph = NetworkGraph(network: network, logger: logger)
52+
53+
let params = ProbabilisticScoringParameters.initWithDefault()
54+
let probabilisticScorer = ProbabilisticScorer(params: params, networkGraph: netGraph, logger: logger)
55+
let score = probabilisticScorer.asScore()
56+
let scorer = MultiThreadedLockableScore(score: score)
57+
58+
let channelManagerConstructionParameters = ChannelManagerConstructionParameters(config: userConfig, entropySource: myKeysManager.entropySource, nodeSigner: myKeysManager.nodeSigner, signerProvider: myKeysManager.signerProvider, feeEstimator: feeEstimator, chainMonitor: chainMonitor, txBroadcaster: broadcaster, logger: logger, enableP2PGossip: true, scorer: scorer)
59+
60+
let reversedGenesisHashHex = "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000"
61+
let reversedGenesisHash = LDKSwiftTests.hexStringToBytes(hexString: reversedGenesisHashHex)!
62+
let latestBlockHash = reversedGenesisHash
63+
let channelManagerConstructor = ChannelManagerConstructor(network: network, currentBlockchainTipHash: latestBlockHash, currentBlockchainTipHeight: 0, netGraph: netGraph, params: channelManagerConstructionParameters)
64+
65+
let channelManager = channelManagerConstructor.channelManager
66+
}
67+
68+
class MyKeysManager {
69+
let keysManager: KeysManager
70+
let nodeSigner: MyNodeSigner
71+
let entropySource: MyEntropySource
72+
let signerProvider: MySignerProvider
73+
74+
init(seed: [UInt8], startingTimeSecs: UInt64, startingTimeNanos: UInt32) {
75+
self.keysManager = KeysManager(seed: seed, startingTimeSecs: startingTimeSecs, startingTimeNanos: startingTimeNanos)
76+
nodeSigner = MyNodeSigner()
77+
entropySource = MyEntropySource()
78+
signerProvider = MySignerProvider()
79+
nodeSigner.myKeysManager = self
80+
entropySource.myKeysManager = self
81+
signerProvider.myKeysManager = self
82+
}
83+
}
84+
85+
class MyNodeSigner: NodeSigner {
86+
var myKeysManager: MyKeysManager?
87+
override func ecdh(recipient: Bindings.Recipient, otherKey: [UInt8], tweak: [UInt8]?) -> Bindings.Result_SharedSecretNoneZ {
88+
print("entering wrapper: ecdh()")
89+
return myKeysManager!.keysManager.asNodeSigner().ecdh(recipient: recipient, otherKey: otherKey, tweak: tweak)
90+
}
91+
92+
override func getNodeId(recipient: Bindings.Recipient) -> Bindings.Result_PublicKeyNoneZ {
93+
print("entering wrapper: getNodeId()")
94+
let nodeId = myKeysManager!.keysManager.asNodeSigner().getNodeId(recipient: recipient)
95+
return nodeId
96+
}
97+
98+
override func getInboundPaymentKeyMaterial() -> [UInt8] {
99+
print("entering wrapper: getInboundPaymentKeyMaterial()")
100+
return myKeysManager!.keysManager.asNodeSigner().getInboundPaymentKeyMaterial()
101+
}
102+
103+
override func signGossipMessage(msg: Bindings.UnsignedGossipMessage) -> Bindings.Result_SignatureNoneZ {
104+
print("entering wrapper: signGossipMessage()")
105+
return myKeysManager!.keysManager.asNodeSigner().signGossipMessage(msg: msg)
106+
}
107+
108+
override func signInvoice(hrpBytes: [UInt8], invoiceData: [UInt8], recipient: Bindings.Recipient) -> Bindings.Result_RecoverableSignatureNoneZ {
109+
print("entering wrapper: signInvoice()")
110+
return myKeysManager!.keysManager.asNodeSigner().signInvoice(hrpBytes: hrpBytes, invoiceData: invoiceData, recipient: recipient)
111+
}
112+
}
113+
114+
class MyEntropySource: EntropySource {
115+
var myKeysManager: MyKeysManager?
116+
override func getSecureRandomBytes() -> [UInt8] {
117+
print("entering wrapper: getSecureRandomBytes()")
118+
return myKeysManager!.keysManager.asEntropySource().getSecureRandomBytes()
119+
}
120+
}
121+
122+
class MySignerProvider: SignerProvider {
123+
var myKeysManager: MyKeysManager?
124+
override func deriveChannelSigner(channelValueSatoshis: UInt64, channelKeysId: [UInt8]) -> Bindings.WriteableEcdsaChannelSigner {
125+
print("entering wrapper: deriveChannelSigner()")
126+
return myKeysManager!.keysManager.asSignerProvider().deriveChannelSigner(channelValueSatoshis: channelValueSatoshis, channelKeysId: channelKeysId)
127+
}
128+
129+
override func generateChannelKeysId(inbound: Bool, channelValueSatoshis: UInt64, userChannelId: [UInt8]) -> [UInt8] {
130+
print("entering wrapper: generateChannelKeysId()")
131+
return myKeysManager!.keysManager.asSignerProvider().generateChannelKeysId(inbound: inbound, channelValueSatoshis: channelValueSatoshis, userChannelId: userChannelId)
132+
}
133+
134+
override func readChanSigner(reader: [UInt8]) -> Bindings.Result_WriteableEcdsaChannelSignerDecodeErrorZ {
135+
print("entering wrapper: readChanSigner()")
136+
return myKeysManager!.keysManager.asSignerProvider().readChanSigner(reader: reader)
137+
}
138+
139+
override func getDestinationScript() -> [UInt8] {
140+
print("entering wrapper: getDestinationScript()")
141+
return myKeysManager!.keysManager.asSignerProvider().getDestinationScript()
142+
}
143+
144+
override func getShutdownScriptpubkey() -> Bindings.ShutdownScript {
145+
print("entering wrapper: getShutdownScriptpubkey()")
146+
let scriptPubkey = myKeysManager!.keysManager.asSignerProvider().getShutdownScriptpubkey()
147+
return scriptPubkey
148+
}
149+
}
150+
151+
}

out/traits/ChannelMessageHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ extension Bindings {
593593

594594

595595
// return value (do some wrapping)
596-
let returnValue = swiftCallbackResult.cType!
596+
let returnValue = swiftCallbackResult.dangle().cType!
597597

598598
return returnValue
599599
}
@@ -705,7 +705,7 @@ extension Bindings {
705705

706706

707707
// return value (do some wrapping)
708-
let returnValue = swiftCallbackResult.cType!
708+
let returnValue = swiftCallbackResult.dangle().cType!
709709

710710
return returnValue
711711
}
@@ -730,7 +730,7 @@ extension Bindings {
730730

731731

732732
// return value (do some wrapping)
733-
let returnValue = swiftCallbackResult.cType!
733+
let returnValue = swiftCallbackResult.dangle().cType!
734734

735735
return returnValue
736736
}

out/traits/ChannelSigner.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ extension Bindings {
144144

145145

146146
// return value (do some wrapping)
147-
let returnValue = swiftCallbackResult.cType!
147+
let returnValue = swiftCallbackResult.dangle().cType!
148148

149149
return returnValue
150150
}

out/traits/CustomMessageHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ extension Bindings {
109109

110110

111111
// return value (do some wrapping)
112-
let returnValue = swiftCallbackResult.cType!
112+
let returnValue = swiftCallbackResult.dangle().cType!
113113

114114
return returnValue
115115
}

out/traits/CustomMessageReader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extension Bindings {
9494

9595

9696
// return value (do some wrapping)
97-
let returnValue = swiftCallbackResult.cType!
97+
let returnValue = swiftCallbackResult.dangle().cType!
9898

9999
return returnValue
100100
}

out/traits/CustomOnionMessageHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ extension Bindings {
134134

135135

136136
// return value (do some wrapping)
137-
let returnValue = swiftCallbackResult.cType!
137+
let returnValue = swiftCallbackResult.dangle().cType!
138138

139139
return returnValue
140140
}

out/traits/EcdsaChannelSigner.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ extension Bindings {
110110

111111

112112
// return value (do some wrapping)
113-
let returnValue = swiftCallbackResult.cType!
113+
let returnValue = swiftCallbackResult.dangle().cType!
114114

115115
return returnValue
116116
}
@@ -136,7 +136,7 @@ extension Bindings {
136136

137137

138138
// return value (do some wrapping)
139-
let returnValue = swiftCallbackResult.cType!
139+
let returnValue = swiftCallbackResult.dangle().cType!
140140

141141
return returnValue
142142
}
@@ -162,7 +162,7 @@ extension Bindings {
162162

163163

164164
// return value (do some wrapping)
165-
let returnValue = swiftCallbackResult.cType!
165+
let returnValue = swiftCallbackResult.dangle().cType!
166166

167167
return returnValue
168168
}
@@ -194,7 +194,7 @@ extension Bindings {
194194

195195

196196
// return value (do some wrapping)
197-
let returnValue = swiftCallbackResult.cType!
197+
let returnValue = swiftCallbackResult.dangle().cType!
198198

199199
return returnValue
200200
}
@@ -231,7 +231,7 @@ extension Bindings {
231231

232232

233233
// return value (do some wrapping)
234-
let returnValue = swiftCallbackResult.cType!
234+
let returnValue = swiftCallbackResult.dangle().cType!
235235

236236
return returnValue
237237
}
@@ -267,7 +267,7 @@ extension Bindings {
267267

268268

269269
// return value (do some wrapping)
270-
let returnValue = swiftCallbackResult.cType!
270+
let returnValue = swiftCallbackResult.dangle().cType!
271271

272272
return returnValue
273273
}
@@ -293,7 +293,7 @@ extension Bindings {
293293

294294

295295
// return value (do some wrapping)
296-
let returnValue = swiftCallbackResult.cType!
296+
let returnValue = swiftCallbackResult.dangle().cType!
297297

298298
return returnValue
299299
}
@@ -319,7 +319,7 @@ extension Bindings {
319319

320320

321321
// return value (do some wrapping)
322-
let returnValue = swiftCallbackResult.cType!
322+
let returnValue = swiftCallbackResult.dangle().cType!
323323

324324
return returnValue
325325
}
@@ -345,7 +345,7 @@ extension Bindings {
345345

346346

347347
// return value (do some wrapping)
348-
let returnValue = swiftCallbackResult.cType!
348+
let returnValue = swiftCallbackResult.dangle().cType!
349349

350350
return returnValue
351351
}

0 commit comments

Comments
 (0)