Skip to content

Commit cc7336e

Browse files
authored
Merge pull request #107 from arik-so/2023-04-non-init-anchoring
Test and fix non-init-based anchoring
2 parents 6f2617b + 0fbcb2b commit cc7336e

File tree

5 files changed

+68
-23
lines changed

5 files changed

+68
-23
lines changed

ci/LDKSwift/Tests/LDKSwiftTests/HumanObjectPeerTestInstance.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ public class HumanObjectPeerTestInstance {
1717

1818
public class Configuration {
1919
//
20-
public var useFilter: Bool = false;
21-
public var useRouter: Bool = false;
22-
public var shouldRecipientRejectPayment: Bool = false;
20+
public var useFilter: Bool = false
21+
public var useRouter: Bool = false
22+
public var shouldRecipientRejectPayment: Bool = false
2323
public var ephemeralNetworkGraphForScorer: Bool = false
24+
public var reserializedProbabilisticScorer: Bool = false
2425

2526
// public var nice_close: Bool = false;
2627
// public var use_km_wrapper: Bool = false;
@@ -30,7 +31,7 @@ public class HumanObjectPeerTestInstance {
3031
// public var use_nio_peer_handler: Bool = false;
3132

3233
private class func listCustomizeableProperties() -> [String] {
33-
return ["useFilter", "useRouter", "shouldRecipientRejectPayment", "ephemeralNetworkGraphForScorer"]
34+
return ["useFilter", "useRouter", "shouldRecipientRejectPayment", "ephemeralNetworkGraphForScorer", "reserializedProbabilisticScorer"]
3435
}
3536

3637
public class func combinationCount() -> UInt {
@@ -308,7 +309,12 @@ public class HumanObjectPeerTestInstance {
308309
}
309310

310311
let scoringParams = ProbabilisticScoringParameters.initWithDefault()
311-
let probabalisticScorer = ProbabilisticScorer(params: scoringParams, networkGraph: scorerGraph, logger: self.logger)
312+
var probabalisticScorer = ProbabilisticScorer(params: scoringParams, networkGraph: scorerGraph, logger: self.logger)
313+
if master.configuration.reserializedProbabilisticScorer {
314+
let serializedScorer = probabalisticScorer.write()
315+
let probabalisticScorerResult = ProbabilisticScorer.read(ser: serializedScorer, argA: scoringParams, argB: scorerGraph, argC: self.logger)
316+
probabalisticScorer = probabalisticScorerResult.getValue()!
317+
}
312318
let score = probabalisticScorer.asScore()
313319
let multiThreadedScorer = MultiThreadedLockableScore(score: score)
314320

ci/LDKSwift/Tests/LDKSwiftTests/LDKSwiftTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ class LDKSwiftTests: XCTestCase {
472472

473473
config.ephemeralNetworkGraphForScorer = (i & (1 << 3)) != 0
474474
print("ephemeralNetworkGraphForScorer: \(config.ephemeralNetworkGraphForScorer)")
475+
476+
config.reserializedProbabilisticScorer = (i & (1 << 4)) != 0
477+
print("reserializedProbabilisticScorer: \(config.reserializedProbabilisticScorer)")
475478

476479
let instance = HumanObjectPeerTestInstance(configuration: config)
477480
await instance.testMessageHandling()

out/Bindings.swift

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@
392392
let returnValue = InitFeatures(cType: nativeCallResult)
393393

394394

395-
return returnValue
395+
try! returnValue.addAnchor(anchor: config)
396+
return returnValue
396397
}
397398

398399
/// Equivalent to [`crate::ln::channelmanager::ChannelManager::create_inbound_payment`], but no
@@ -437,7 +438,8 @@
437438
let returnValue = Result_C2Tuple_PaymentHashPaymentSecretZNoneZ(cType: nativeCallResult)
438439

439440

440-
return returnValue
441+
try! returnValue.addAnchor(anchor: keys)
442+
return returnValue
441443
}
442444

443445
/// Equivalent to [`crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash`],
@@ -478,7 +480,8 @@
478480
let returnValue = Result_PaymentSecretNoneZ(cType: nativeCallResult)
479481

480482

481-
return returnValue
483+
try! returnValue.addAnchor(anchor: keys)
484+
return returnValue
482485
}
483486

484487
/// Gets the weight for an HTLC-Success transaction.
@@ -1079,7 +1082,9 @@
10791082
let returnValue = Result_RouteLightningErrorZ(cType: nativeCallResult)
10801083

10811084

1082-
return returnValue
1085+
try! returnValue.addAnchor(anchor: routeParams)
1086+
try! returnValue.addAnchor(anchor: networkGraph)
1087+
return returnValue
10831088
}
10841089

10851090
/// Construct a route from us (payer) to the target node (payee) via the given hops (which should
@@ -1124,7 +1129,9 @@
11241129
let returnValue = Result_RouteLightningErrorZ(cType: nativeCallResult)
11251130

11261131

1127-
return returnValue
1132+
try! returnValue.addAnchor(anchor: routeParams)
1133+
try! returnValue.addAnchor(anchor: networkGraph)
1134+
return returnValue
11281135
}
11291136

11301137
/// Pays the given [`Invoice`], retrying if needed based on [`Retry`].
@@ -1157,7 +1164,9 @@
11571164
let returnValue = Result_PaymentIdPaymentErrorZ(cType: nativeCallResult)
11581165

11591166

1160-
return returnValue
1167+
try! returnValue.addAnchor(anchor: invoice)
1168+
try! returnValue.addAnchor(anchor: channelmanager)
1169+
return returnValue
11611170
}
11621171

11631172
/// Pays the given [`Invoice`] with a custom idempotency key, retrying if needed based on [`Retry`].
@@ -1197,7 +1206,9 @@
11971206
let returnValue = Result_NonePaymentErrorZ(cType: nativeCallResult)
11981207

11991208

1200-
return returnValue
1209+
try! returnValue.addAnchor(anchor: invoice)
1210+
try! returnValue.addAnchor(anchor: channelmanager)
1211+
return returnValue
12011212
}
12021213

12031214
/// Pays the given zero-value [`Invoice`] using the given amount, retrying if needed based on
@@ -1232,7 +1243,9 @@
12321243
let returnValue = Result_PaymentIdPaymentErrorZ(cType: nativeCallResult)
12331244

12341245

1235-
return returnValue
1246+
try! returnValue.addAnchor(anchor: invoice)
1247+
try! returnValue.addAnchor(anchor: channelmanager)
1248+
return returnValue
12361249
}
12371250

12381251
/// Pays the given zero-value [`Invoice`] using the given amount and custom idempotency key,
@@ -1274,7 +1287,9 @@
12741287
let returnValue = Result_NonePaymentErrorZ(cType: nativeCallResult)
12751288

12761289

1277-
return returnValue
1290+
try! returnValue.addAnchor(anchor: invoice)
1291+
try! returnValue.addAnchor(anchor: channelmanager)
1292+
return returnValue
12781293
}
12791294

12801295
/// Utility to create an invoice that can be paid to one of multiple nodes, or a \"phantom invoice.\"
@@ -1467,7 +1482,8 @@
14671482
let returnValue = Result_InvoiceSignOrCreationErrorZ(cType: nativeCallResult)
14681483

14691484

1470-
return returnValue
1485+
try! returnValue.addAnchor(anchor: channelmanager)
1486+
return returnValue
14711487
}
14721488

14731489
/// Utility to construct an invoice. Generally, unless you want to do something like a custom
@@ -1509,7 +1525,8 @@
15091525
let returnValue = Result_InvoiceSignOrCreationErrorZ(cType: nativeCallResult)
15101526

15111527

1512-
return returnValue
1528+
try! returnValue.addAnchor(anchor: channelmanager)
1529+
return returnValue
15131530
}
15141531

15151532
/// See [`create_invoice_from_channelmanager_with_description_hash`]
@@ -1538,7 +1555,8 @@
15381555
let returnValue = Result_InvoiceSignOrCreationErrorZ(cType: nativeCallResult)
15391556

15401557

1541-
return returnValue
1558+
try! returnValue.addAnchor(anchor: channelmanager)
1559+
return returnValue
15421560
}
15431561

15441562
/// See [`create_invoice_from_channelmanager`]
@@ -1572,7 +1590,8 @@
15721590
let returnValue = Result_InvoiceSignOrCreationErrorZ(cType: nativeCallResult)
15731591

15741592

1575-
return returnValue
1593+
try! returnValue.addAnchor(anchor: channelmanager)
1594+
return returnValue
15761595
}
15771596

15781597
/// See [`create_invoice_from_channelmanager_and_duration_since_epoch`]
@@ -1612,7 +1631,8 @@
16121631
let returnValue = Result_InvoiceSignOrCreationErrorZ(cType: nativeCallResult)
16131632

16141633

1615-
return returnValue
1634+
try! returnValue.addAnchor(anchor: channelmanager)
1635+
return returnValue
16161636
}
16171637

16181638
/// Read a C2Tuple_BlockHashChannelMonitorZ from a byte array, created by C2Tuple_BlockHashChannelMonitorZ_write

out/structs/ProbabilisticScorer.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@
469469
let returnValue = Result_ProbabilisticScorerDecodeErrorZ(cType: nativeCallResult)
470470

471471

472-
return returnValue
472+
try! returnValue.addAnchor(anchor: argB)
473+
return returnValue
473474
}
474475

475476

src/generation/base_type_generator.mts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
194194
nativeCallWrapperSuffix += preparedArgument.methodCallWrapperSuffix;
195195
nativeCallValueAccessors.push(preparedArgument.accessor);
196196
nativeCallSuffix += preparedArgument.deferredCleanup;
197-
if(preparedArgument.requiresAnchoring){
197+
if (preparedArgument.requiresAnchoring && this.isAnchorableType(method.returnValue.type)) {
198198
anchors.push(swiftArgumentName);
199199
}
200200
}
@@ -236,7 +236,7 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
236236
let returnValueHandlingPrefix = '';
237237
let returnValueHandlingSuffix = '';
238238
if (swiftMethodName === 'init') {
239-
anchoringCommand = anchoringCommand.replaceAll('returnValue.addAnchor(', 'self.addAnchor(')
239+
anchoringCommand = anchoringCommand.replaceAll('returnValue.addAnchor(', 'self.addAnchor(');
240240
// it's a constructor
241241
methodDeclarationKeywords = visibility;
242242
returnCommand = `
@@ -443,6 +443,21 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
443443
return false;
444444
}
445445

446+
/**
447+
* This is a return type that may have anchors attached.
448+
* @param type
449+
* @protected
450+
*/
451+
protected isAnchorableType(type: RustType): boolean {
452+
if (this.isElidedType(type)) {
453+
return false;
454+
}
455+
if (type instanceof RustPrimitive || type instanceof RustArray) {
456+
return false;
457+
}
458+
return true;
459+
}
460+
446461
/**
447462
* Some types have multiple constructors but with the same argument types
448463
* We wanna allow init-based construction by default, but when the types conflict,
@@ -831,7 +846,7 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
831846
* complicated to handle, because doing it blindly will probably only lead us to
832847
* introduce some new memory bugs.
833848
*/
834-
if(memoryContext && memoryContext.isConstructor && (argument.type instanceof RustStruct || argument.type instanceof RustTaggedValueEnum || argument.type instanceof RustResult)){
849+
if (memoryContext && (memoryContext.isStatic || memoryContext.isConstructor) && /*argument.type !== containerType &&*/ !(argument.type instanceof RustTrait) && (argument.type instanceof RustStruct || argument.type instanceof RustTaggedValueEnum || argument.type instanceof RustResult)) {
835850
preparedArgument.requiresAnchoring = true;
836851
}
837852

0 commit comments

Comments
 (0)