Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix findRoute segfault #118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions ci/LDKSwift/Tests/LDKSwiftTests/HumanObjectPeerTestInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,39 @@ public class HumanObjectPeerTestInstance {

let recreatedInvoice = Bolt11Invoice.fromStr(s: invoice.toStr())
XCTAssertTrue(recreatedInvoice.isOk())

// find route

do {
let payerPubkey = peer1.channelManager.getOurNodeId()
let payeePubkey = peer2.channelManager.getOurNodeId()
let paymentParameters = PaymentParameters.initForKeysend(payeePubkey: payeePubkey, finalCltvExpiryDelta: 3, allowMpp: false)

let amount = invoice.amountMilliSatoshis()!
let routeParameters = RouteParameters(paymentParamsArg: paymentParameters, finalValueMsatArg: amount)
let randomSeedBytes: [UInt8] = [UInt8](repeating: 0, count: 32)
let scoringParams = ProbabilisticScoringDecayParameters.initWithDefault()
let networkGraph = peer1.constructor!.netGraph!
let scorer = ProbabilisticScorer(decayParams: scoringParams, networkGraph: networkGraph, logger: logger)
let score = scorer.asScore()

let scoreParams = ProbabilisticScoringFeeParameters.initWithDefault()

let foundRoute = Bindings.findRoute(
ourNodePubkey: payerPubkey,
routeParams: routeParameters,
networkGraph: networkGraph,
firstHops: usableChannelsA,
logger: logger,
scorer: score,
scoreParams: scoreParams,
randomSeedBytes: randomSeedBytes
)

let route = foundRoute.getValue()!
let fees = route.getTotalFees()
print("found route fees: \(fees)")
}

let channelManagerConstructor = peer1.constructor!
let invoicePaymentResult = Bindings.payInvoice(invoice: invoice, retryStrategy: Bindings.Retry.initWithAttempts(a: 3), channelmanager: channelManagerConstructor.channelManager)
Expand Down
4 changes: 3 additions & 1 deletion out/Bindings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,9 @@ public class Bindings {
if let firstHops = firstHops {

let firstHopsVector = Vec_ChannelDetailsZ(
array: firstHops, instantiationContext: "Bindings.swift::\(#function):\(#line)")
array: firstHops, instantiationContext: "Bindings.swift::\(#function):\(#line)"
)
.dangle()

firstHopsVectorPointer = UnsafeMutablePointer<LDKCVec_ChannelDetailsZ>.allocate(capacity: 1)
firstHopsVectorPointer!.initialize(to: firstHopsVector.cType!)
Expand Down
8 changes: 6 additions & 2 deletions out/traits/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ extension Bindings {
if let firstHops = firstHops {

let firstHopsVector = Vec_ChannelDetailsZ(
array: firstHops, instantiationContext: "Router.swift::\(#function):\(#line)")
array: firstHops, instantiationContext: "Router.swift::\(#function):\(#line)"
)
.dangle()

firstHopsVectorPointer = UnsafeMutablePointer<LDKCVec_ChannelDetailsZ>.allocate(capacity: 1)
firstHopsVectorPointer!.initialize(to: firstHopsVector.cType!)
Expand Down Expand Up @@ -323,7 +325,9 @@ extension Bindings {
if let firstHops = firstHops {

let firstHopsVector = Vec_ChannelDetailsZ(
array: firstHops, instantiationContext: "Router.swift::\(#function):\(#line)")
array: firstHops, instantiationContext: "Router.swift::\(#function):\(#line)"
)
.dangle()

firstHopsVectorPointer = UnsafeMutablePointer<LDKCVec_ChannelDetailsZ>.allocate(capacity: 1)
firstHopsVectorPointer!.initialize(to: firstHopsVector.cType!)
Expand Down
7 changes: 7 additions & 0 deletions src/generation/base_type_generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,13 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
// memoryManagementInfix = ''
// }
}
} else if (argument.isAsteriskPointer && this.isPointerArgumentNullable(argument, containerType)) {
tnull marked this conversation as resolved.
Show resolved Hide resolved
// if we're taking a nullable argument that needs to be passed to Rust as a nullable pointer,
// the initialization of the argument will occur inside an if let block, and the value
// to which the pointer will later refer must not get immediately cleaned up.
// It is not quite clear to me how to determine when to, eventually, clear that memory,
// which is why for the time being, this actually results in some additional direct leaks.
memoryManagementInfix = '.dangle()';
}
} else if (argument.type instanceof RustPrimitiveWrapper && argument.type.isDeallocatable()) {
// memoryManagementInfix = '.dangle()';
Expand Down
Loading