Skip to content

Commit

Permalink
Fix infinite loop, maybe?
Browse files Browse the repository at this point in the history
  • Loading branch information
arik-so committed Oct 10, 2023
1 parent 70bcbf2 commit 835c9cb
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions ci/LDKSwift/Tests/LDKSwiftTests/HumanObjectPeerTestInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,16 @@ public class HumanObjectPeerTestInstance {
await master.pendingEventTracker.addEvent(event: event)
}
}

override func persistScorer(scorer: Bindings.WriteableScore) -> Bindings.Result_NoneIOErrorZ {
.initWithOk()
}

override func persistGraph(networkGraph: Bindings.NetworkGraph) -> Bindings.Result_NoneIOErrorZ {
let writtenGraph = networkGraph.write();
return .initWithOk()
}

override func persistManager(channelManager: Bindings.ChannelManager) -> Bindings.Result_NoneIOErrorZ {
.initWithOk()
}
Expand Down Expand Up @@ -403,11 +403,12 @@ public class HumanObjectPeerTestInstance {
}

fileprivate func getManagerEvents(expectedCount: UInt) async throws -> [Event] {
print("Awaiting \(expectedCount) manager events…")
while true {
if await self.pendingEventTracker.getCount() >= expectedCount {
print("Found enough events for expected count of \(expectedCount)")
print("Found enough manager events for expected count of \(expectedCount)")
let events = await self.pendingEventTracker.getAndClearEvents()
print("Found event count: \(events.count)")
print("Found manager event count: \(events.count)")
return events
}
// sleep for 0.1 seconds
Expand Down Expand Up @@ -557,6 +558,12 @@ public class HumanObjectPeerTestInstance {
XCTAssertNil(fundingTxo)
}

let channelsA = peer1.channelManager.listChannels()
let channelsB = peer2.channelManager.listChannels()

XCTAssertEqual(channelsA.count, 1)
XCTAssertEqual(channelsB.count, 1)

let managerEvents = try! await peer1.getManagerEvents(expectedCount: 1)
XCTAssertEqual(managerEvents.count, 1)

Expand Down Expand Up @@ -607,11 +614,13 @@ public class HumanObjectPeerTestInstance {
fundingBlock.nonce = 0
fundingBlock.transactions = [fundingTransaction]

print("Connecting funding block…")
peer1.connectBlock(block: fundingBlock, height: 1, expectedMonitorUpdateLength: 0)
peer2.connectBlock(block: fundingBlock, height: 1, expectedMonitorUpdateLength: 0)

print("Connecting confirmation blocks…")
var previousBlock = fundingBlock
for height in 2..<10 {
for height in 2..<101 {
let currentBlock = BTCBlock()
currentBlock.version = 2
currentBlock.previousBlockHash = previousBlock.calculateHash()
Expand All @@ -625,28 +634,32 @@ public class HumanObjectPeerTestInstance {
previousBlock = currentBlock
}

let peer1Events = try! await peer1.getManagerEvents(expectedCount: 2)
let peer1ReadyEvent = peer1Events[1]
guard case .ChannelReady = peer1ReadyEvent.getValueType() else {
return XCTAssert(false, "Expected .ChannelReady, got \(peer1ReadyEvent.getValueType())")
}

let peer2Events = try! await peer2.getManagerEvents(expectedCount: 2)
let peer2ReadyEvent = peer2Events[1]
guard case .ChannelReady = peer2ReadyEvent.getValueType() else {
return XCTAssert(false, "Expected .ChannelReady, got \(peer2ReadyEvent.getValueType())")
}

var usableChannelsA = [ChannelDetails]()
var usableChannelsB = [ChannelDetails]()
print("Awaiting usable channels to populate…")
while (usableChannelsA.isEmpty || usableChannelsB.isEmpty) {
usableChannelsA = peer1.channelManager.listUsableChannels()
usableChannelsB = peer2.channelManager.listUsableChannels()
// sleep for 100ms
try! await Task.sleep(nanoseconds: 0_100_000_000)
}
print("Usable channels have been populated")

XCTAssertEqual(usableChannelsA.count, 1)
XCTAssertEqual(usableChannelsB.count, 1)

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

let peer2Event = try! await peer2.getManagerEvents(expectedCount: 2)[1]
guard case .ChannelReady = peer2Event.getValueType() else {
return XCTAssert(false, "Expected .ChannelReady, got \(peer2Event.getValueType())")
}

let channelAToB = usableChannelsA[0]
let channelBToA = usableChannelsB[0]
XCTAssertEqual(channelAToB.getChannelValueSatoshis(), FUNDING_SATOSHI_AMOUNT)
Expand Down Expand Up @@ -676,24 +689,24 @@ 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, maxTotalRoutingFeeMsatArg: nil)
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.asScoreLookUp()

let scoreParams = ProbabilisticScoringFeeParameters.initWithDefault()

let foundRoute = Bindings.findRoute(
ourNodePubkey: payerPubkey,
routeParams: routeParameters,
Expand All @@ -704,7 +717,7 @@ public class HumanObjectPeerTestInstance {
scoreParams: scoreParams,
randomSeedBytes: randomSeedBytes
)

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

0 comments on commit 835c9cb

Please sign in to comment.