Skip to content

Commit 6296eb4

Browse files
authored
Merge pull request #109 from arik-so/2023-05-release-115
Build experimental bindings for 0.0.115
2 parents 489bc1a + 01a551d commit 6296eb4

File tree

113 files changed

+14523
-5619
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+14523
-5619
lines changed

.github/actions/install-dependencies/action.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ runs:
2222
run: |
2323
echo `pwd`
2424
mkdir -p ${{ inputs.destination }}/bin
25-
git clone --branch 2023-02-0.0.114-java-bindings https://github.com/TheBlueMatt/rust-lightning ${{ inputs.destination }}/rust-lightning
26-
git clone --branch v0.0.114.1 https://github.com/lightningdevkit/ldk-c-bindings ${{ inputs.destination }}/ldk-c-bindings
25+
git clone --branch 2023-04-0.0.115-java-bindings https://github.com/TheBlueMatt/rust-lightning ${{ inputs.destination }}/rust-lightning
26+
git clone --branch v0.0.115.0 https://github.com/lightningdevkit/ldk-c-bindings ${{ inputs.destination }}/ldk-c-bindings
2727
- name: Install Rust, required targets
2828
if: ${{ inputs.configureRustNightly == 'true' }}
2929
shell: bash

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Automatic Swift bindings generation for [`rust-lightning`](https://github.com/li
77
### Swift Package Manager
88
To install using Swift Package Manager, add the following line to the depedencies array of your `Package.swift`:
99
```
10-
.package(url: "https://github.com/lightningdevkit/ldk-swift/", exact: "0.0.114")
10+
.package(url: "https://github.com/lightningdevkit/ldk-swift/", exact: "0.0.115")
1111
```
1212
Be sure to also include "LightningDevKit" into your packages target like so:
1313

@@ -58,13 +58,13 @@ In order to generate these bindings from scratch, you will need to clone two dep
5858
**[rust-lightning](https://github.com/lightningdevkit/rust-lightning)**, (a specific branch built for bindings compatibility):
5959

6060
```shell
61-
git clone --branch 2023-02-0.0.114-java-bindings https://github.com/TheBlueMatt/rust-lightning /path/to/rust-lightning
61+
git clone --branch 2023-04-0.0.115-java-bindings https://github.com/TheBlueMatt/rust-lightning /path/to/rust-lightning
6262
```
6363

6464
**[ldk-c-bindings](https://github.com/lightningdevkit/ldk-c-bindings)**:
6565

6666
```shell
67-
git clone --branch v0.0.114.1 https://github.com/lightningdevkit/ldk-c-bindings /path/to/ldk-c-bindings
67+
git clone --branch v0.0.115.0 https://github.com/lightningdevkit/ldk-c-bindings /path/to/ldk-c-bindings
6868
```
6969

7070
Take note of where you clone these directories, it's best you save the absolute path somewhere handy for the rest of the remaining steps.
@@ -95,11 +95,11 @@ folder due to the previous `genbindings.sh` step. As Docker won't have access to
9595
replace those lines with the following:
9696

9797
```yaml
98-
lightning = { git = "https://github.com/thebluematt/rust-lightning", branch = "2023-02-0.0.114-java-bindings", default-features = false }
99-
lightning-persister = { git = "https://github.com/thebluematt/rust-lightning", branch = "2023-02-0.0.114-java-bindings", default-features = false }
100-
lightning-invoice = { git = "https://github.com/thebluematt/rust-lightning", branch = "2023-02-0.0.114-java-bindings", default-features = false }
101-
lightning-background-processor = { git = "https://github.com/thebluematt/rust-lightning", branch = "2023-02-0.0.114-java-bindings", default-features = false }
102-
lightning-rapid-gossip-sync = { git = "https://github.com/thebluematt/rust-lightning", branch = "2023-02-0.0.114-java-bindings", default-features = false }
98+
lightning = { git = "https://github.com/thebluematt/rust-lightning", branch = "2023-04-0.0.115-java-bindings", default-features = false }
99+
lightning-persister = { git = "https://github.com/thebluematt/rust-lightning", branch = "2023-04-0.0.115-java-bindings", default-features = false }
100+
lightning-invoice = { git = "https://github.com/thebluematt/rust-lightning", branch = "2023-05-0.0.115-java-bindings", default-features = false }
101+
lightning-background-processor = { git = "https://github.com/thebluematt/rust-lightning", branch = "2023-04-0.0.115-java-bindings", default-features = false }
102+
lightning-rapid-gossip-sync = { git = "https://github.com/thebluematt/rust-lightning", branch = "2023-04-0.0.115-java-bindings", default-features = false }
103103
```
104104

105105
You will note that the revision is unspecified and is currently just placeholder `xxx`s. To obtain the revision,

ci/LDKSwift/Tests/LDKSwiftTests/HumanObjectPeerTestInstance.swift

+5-6
Original file line numberDiff line numberDiff line change
@@ -627,12 +627,12 @@ public class HumanObjectPeerTestInstance {
627627
XCTAssertEqual(usableChannelsA.count, 1)
628628
XCTAssertEqual(usableChannelsB.count, 1)
629629

630-
let peer1Event = try! await peer1.getManagerEvents(expectedCount: 1)[0]
630+
let peer1Event = try! await peer1.getManagerEvents(expectedCount: 2)[1]
631631
guard case .ChannelReady = peer1Event.getValueType() else {
632632
return XCTAssert(false, "Expected .ChannelReady, got \(peer1Event.getValueType())")
633633
}
634634

635-
let peer2Event = try! await peer2.getManagerEvents(expectedCount: 1)[0]
635+
let peer2Event = try! await peer2.getManagerEvents(expectedCount: 2)[1]
636636
guard case .ChannelReady = peer2Event.getValueType() else {
637637
return XCTAssert(false, "Expected .ChannelReady, got \(peer2Event.getValueType())")
638638
}
@@ -726,8 +726,7 @@ public class HumanObjectPeerTestInstance {
726726
"payment_hash": paymentPathFailed.getPaymentHash(),
727727
"payment_failed_permanently": paymentPathFailed.getPaymentFailedPermanently(),
728728
"short_channel_id": paymentPathFailed.getShortChannelId(),
729-
"path": paymentPathFailed.getPath().map { $0.getShortChannelId() },
730-
"retry": paymentPathFailed.getRetry(),
729+
"path": paymentPathFailed.getPath().getHops().map { $0.getShortChannelId() },
731730
"failure": paymentPathFailed.getFailure()
732731
]
733732

@@ -748,7 +747,7 @@ public class HumanObjectPeerTestInstance {
748747
}
749748
let paymentSent = paymentSentEvent.getValueAsPaymentSent()!
750749
let paymentPathSuccessful = paymentPathSuccessfulEvent.getValueAsPaymentPathSuccessful()!
751-
print("sent payment \(paymentSent.getPaymentId()) with fee \(paymentSent.getFeePaidMsat()) via \(paymentPathSuccessful.getPath().map { h in h.getShortChannelId() })")
750+
print("sent payment \(paymentSent.getPaymentId()) with fee \(paymentSent.getFeePaidMsat()) via \(paymentPathSuccessful.getPath().getHops().map { h in h.getShortChannelId() })")
752751
}
753752

754753
var currentChannelABalance = originalChannelBalanceAToB
@@ -846,7 +845,7 @@ public class HumanObjectPeerTestInstance {
846845
let paymentClaimed = paymentClaimedEvent.getValueAsPaymentClaimed()!
847846
let paymentSent = paymentSentEvent.getValueAsPaymentSent()!
848847
let paymentPathSuccessful = paymentPathSuccessfulEvent.getValueAsPaymentPathSuccessful()!
849-
print("sent payment \(paymentSent.getPaymentId()) worth \(paymentClaimed.getAmountMsat()) with fee \(paymentSent.getFeePaidMsat()) via \(paymentPathSuccessful.getPath().map { h in h.getShortChannelId() })")
848+
print("sent payment \(paymentSent.getPaymentId()) worth \(paymentClaimed.getAmountMsat()) with fee \(paymentSent.getFeePaidMsat()) via \(paymentPathSuccessful.getPath().getHops().map { h in h.getShortChannelId() })")
850849
}
851850

852851
var currentChannelABalance = prePaymentBalanceAToB

ci/LDKSwift/Tests/LDKSwiftTests/LDKSwiftTests.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ class LDKSwiftTests: XCTestCase {
346346
cltvExpiryDeltaArg: finalCltvValue
347347
)
348348

349-
var path: [RouteHop] = [routeHop]
349+
var hops: [RouteHop] = [routeHop]
350350

351351
for _ in 0..<3 {
352352

@@ -358,10 +358,13 @@ class LDKSwiftTests: XCTestCase {
358358
feeMsatArg: paymentValueMsat,
359359
cltvExpiryDeltaArg: finalCltvValue
360360
)
361-
path.append(extraHop)
361+
hops.append(extraHop)
362362
}
363363

364-
let paymentParams = PaymentParameters.initForKeysend(payeePubkey: Self.hexStringToBytes(hexString: destPubkeyHex)!, finalCltvExpiryDelta: 3)
364+
let pubkeyBytes = Self.hexStringToBytes(hexString: destPubkeyHex)!
365+
let paymentParams = PaymentParameters.initForKeysend(payeePubkey: pubkeyBytes, finalCltvExpiryDelta: 3)
366+
let blindedTail = BlindedTail(hopsArg: [], blindingPointArg: pubkeyBytes, excessFinalCltvExpiryDeltaArg: 0, finalValueMsatArg: 0)
367+
let path = Path(hopsArg: hops, blindedTailArg: blindedTail)
365368
let route = Route(pathsArg: [path], paymentParamsArg: paymentParams)
366369
}
367370

@@ -388,7 +391,7 @@ class LDKSwiftTests: XCTestCase {
388391
let gossipDataRaw = [UInt8](data)
389392
print("Applying rapid sync data…")
390393
let startB = DispatchTime.now()
391-
let timestamp = rapidSync.updateNetworkGraph(updateData: gossipDataRaw)
394+
let timestamp = rapidSync.updateNetworkGraphNoStd(updateData: gossipDataRaw, currentTimeUnix: nil)
392395
if let error = timestamp.getError() {
393396
print("error! type: \(error.getValueType())")
394397
let specificError = error.getValueAsLightningError()
@@ -441,7 +444,7 @@ class LDKSwiftTests: XCTestCase {
441444
print("found route with \(paths.count) paths!")
442445
for currentPath in paths {
443446
print("\nPath Option:")
444-
for currentHop in currentPath {
447+
for currentHop in currentPath.getHops() {
445448
print("scid: \(currentHop.getShortChannelId()), pubkey: \(currentHop.getPubkey()), fee (msat): \(currentHop.getFeeMsat()), CLTV delta: \(currentHop.getCltvExpiryDelta())")
446449
}
447450
}

out/Bindings.swift

+39-58
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
internal var pointerDebugDescription: String? = nil
2929

3030
init(conflictAvoidingVariableName: UInt, instantiationContext: String) {
31-
Self.globalInstanceCounter += 1
32-
self.globalInstanceNumber = Self.globalInstanceCounter
31+
var instanceIndex: UInt! = nil
32+
Bindings.instanceIndexQueue.sync {
33+
Self.globalInstanceCounter += 1
34+
instanceIndex = Self.globalInstanceCounter
35+
}
36+
self.globalInstanceNumber = instanceIndex
3337
self.instantiationContext = instantiationContext
3438
}
3539

@@ -85,6 +89,10 @@
8589

8690
public class Bindings {
8791

92+
fileprivate static let instanceIndexQueue = DispatchQueue(label: "org.lightningdevkit.Bindings.instanceIndexQueue")
93+
static var nativelyExposedInstances = [UInt: NativeTraitWrapper]()
94+
static var nativelyExposedInstanceReferenceCounter = [UInt: Int]()
95+
8896
internal static var suspendFreedom = false
8997

9098
internal static var minimumPrintSeverity: PrintSeverity = .WARNING
@@ -100,86 +108,59 @@
100108

101109
internal class func print(_ string: String, severity: PrintSeverity = .DEBUG) {
102110
if severity.rawValue >= Self.minimumPrintSeverity.rawValue {
103-
104-
// Swift.print(string)
105111
NSLog(string)
106112
fflush(stdout)
107-
108-
// if #available(iOS 14.0, *) {
109-
// #if canImport(os)
110-
// if severity == Self.PrintSeverity.DEBUG {
111-
// logger.debug("\(string)")
112-
// }else if severity == Self.PrintSeverity.WARNING {
113-
// logger.warning("\(string)")
114-
// }else if severity == Self.PrintSeverity.ERROR {
115-
// logger.error("\(string)")
116-
// }else {
117-
// logger.log("\(string)")
118-
// }
119-
// #else
120-
// Swift.print(string)
121-
// #endif
122-
// } else {
123-
// // Fallback on earlier versions
124-
// Swift.print(string)
125-
// }
126113
}
127114
}
128115

129116
public class func setLogThreshold(severity: PrintSeverity){
130117
Self.minimumPrintSeverity = severity
131118
}
132119

133-
static var nativelyExposedInstances = [UInt: NativeTraitWrapper]()
134-
static var nativelyExposedInstanceReferenceCounter = [UInt: Int]()
135-
136120
public class func cacheInstance(instance: NativeTraitWrapper, countIdempotently: Bool = false) {
137121
let key = instance.globalInstanceNumber
138-
let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) + 1
139-
if (!countIdempotently || referenceCount == 1){
140-
// if we count non-idempotently, always update the counter
141-
// otherwise, only update the counter the first time
142-
Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
143-
}
144-
if referenceCount == 1 {
145-
print("Caching global instance \(key). Cached instance count: \(nativelyExposedInstanceReferenceCounter.count)")
146-
Self.nativelyExposedInstances[key] = instance
147-
}
122+
123+
Bindings.instanceIndexQueue.sync {
124+
let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) + 1
125+
if (!countIdempotently || referenceCount == 1){
126+
// if we count non-idempotently, always update the counter
127+
// otherwise, only update the counter the first time
128+
Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
129+
}
130+
if referenceCount == 1 {
131+
print("Caching global instance (key). Cached instance count: (nativelyExposedInstanceReferenceCounter.count)")
132+
Self.nativelyExposedInstances[key] = instance
133+
}
134+
}
148135
}
149136

150137
public class func instanceToPointer(instance: NativeTraitWrapper) -> UnsafeMutableRawPointer {
151138
let key = instance.globalInstanceNumber
152139
let pointer = UnsafeMutableRawPointer(bitPattern: key)!
140+
print("Caching instance (key) -> (pointer)", severity: .DEBUG)
153141
// don't automatically cache the trait instance
154-
Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
142+
Bindings.instanceIndexQueue.sync {
143+
Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
144+
}
155145
return pointer
156146
}
157147

158148
public class func pointerToInstance<T: NativeTraitWrapper>(pointer: UnsafeRawPointer, sourceMarker: String?) -> T{
159149
let key = UInt(bitPattern: pointer)
160-
let referenceCount = Self.nativelyExposedInstanceReferenceCounter[key] ?? 0
161-
if referenceCount < 1 {
162-
print("Bad lookup: non-positive reference count for instance \(key): \(referenceCount)!", severity: .ERROR)
163-
}
164-
let value = Self.nativelyExposedInstances[key] as! T
150+
print("Looking up instance (pointer) -> (key)", severity: .DEBUG)
151+
152+
var rawValue: NativeTraitWrapper! = nil
153+
Bindings.instanceIndexQueue.sync {
154+
let referenceCount = Self.nativelyExposedInstanceReferenceCounter[key] ?? 0
155+
if referenceCount < 1 {
156+
print("Bad lookup: non-positive reference count for instance (key): (referenceCount)!", severity: .ERROR)
157+
}
158+
rawValue = Self.nativelyExposedInstances[key]
159+
}
160+
let value = rawValue as! T
165161
return value
166162
}
167163

168-
public class func removeInstancePointer(instance: NativeTraitWrapper) -> Bool {
169-
let key = instance.globalInstanceNumber
170-
let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) - 1
171-
Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
172-
if referenceCount == 0 {
173-
print("Uncaching global instance \(key)")
174-
// TODO: fix counting
175-
// Self.nativelyExposedInstances.removeValue(forKey: key)
176-
// instance.pointerDebugDescription = nil
177-
} else if referenceCount < 0 {
178-
print("Bad uncache: negative reference count (\(referenceCount)) for instance \(key)!", severity: .ERROR)
179-
}
180-
return true
181-
}
182-
183164
/*
184165
public class func clearInstancePointers() {
185166
for (_, currentInstance) in Self.nativelyExposedInstances {
@@ -1031,7 +1012,7 @@ return returnValue
10311012
/// [`ChannelManager::list_usable_channels`] will never include such channels.
10321013
///
10331014
/// [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
1034-
/// [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
1015+
/// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed
10351016
/// [`NetworkGraph`]: crate::routing::gossip::NetworkGraph
10361017
///
10371018
/// Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None

out/VersionDescriptor.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
extension Bindings {
77
public class func getLDKSwiftBindingsSerializationHash() -> String {
8-
return "4e8c574f7430b4e821f96c72b92afac96f4d156550f5a48f37368294157986bd"
8+
return "7ab6ba0549828e3bc55e8b6dc63e8c2c8878bbc1b7e3e7f7300bd90f8e038906"
99
}
1010
public class func getLDKSwiftBindingsVersion() -> String {
11-
return "0.0.114-19-geb193641-dirty"
11+
return "0.0.114-26-g698e90d7-dirty"
1212
}
1313
public class func getLDKSwiftBindingsCommitHash() -> String {
14-
return "eb193641b9e7ace910c8a0f20c330230f00ec6dd"
14+
return "698e90d745f3bded117d4a0188250c8570e51868"
1515
}
1616
}
1717

out/enums/complex/ClosureReason.swift

+8-11
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,15 @@
183183
}
184184

185185
/// Utility method to constructs a new CounterpartyForceClosed-variant ClosureReason
186-
public class func initWithCounterpartyForceClosed(peerMsg: String) -> ClosureReason {
186+
public class func initWithCounterpartyForceClosed(peerMsg: Bindings.UntrustedString) -> ClosureReason {
187187
// native call variable prep
188188

189-
let peerMsgPrimitiveWrapper = Str(value: peerMsg, instantiationContext: "ClosureReason.swift::\(#function):\(#line)").dangle()
190-
191189

192190
// native method call
193-
let nativeCallResult = ClosureReason_counterparty_force_closed(peerMsgPrimitiveWrapper.cType!)
191+
let nativeCallResult = ClosureReason_counterparty_force_closed(peerMsg.dynamicallyDangledClone().cType!)
194192

195193
// cleanup
196194

197-
// for elided types, we need this
198-
peerMsgPrimitiveWrapper.noOpRetain()
199-
200195

201196

202197
// return value (do some wrapping)
@@ -527,12 +522,14 @@
527522

528523
/// The error which the peer sent us.
529524
///
530-
/// The string should be sanitized before it is used (e.g emitted to logs
531-
/// or printed to stdout). Otherwise, a well crafted error message may exploit
525+
/// Be careful about printing the peer_msg, a well-crafted message could exploit
532526
/// a security vulnerability in the terminal emulator or the logging subsystem.
533-
public func getPeerMsg() -> String {
527+
/// To be safe, use `Display` on `UntrustedString`
528+
///
529+
/// [`UntrustedString`]: crate::util::string::UntrustedString
530+
public func getPeerMsg() -> Bindings.UntrustedString {
534531
// return value (do some wrapping)
535-
let returnValue = Str(cType: self.cType!.peer_msg, instantiationContext: "ClosureReason.swift::\(#function):\(#line)", anchor: self).getValue()
532+
let returnValue = Bindings.UntrustedString(cType: self.cType!.peer_msg, instantiationContext: "ClosureReason.swift::\(#function):\(#line)", anchor: self)
536533

537534
return returnValue;
538535
}

0 commit comments

Comments
 (0)