Skip to content

Commit f579b1d

Browse files
authored
Merge pull request #33 from jurvis/regtest_monitor_tooling
Add blockchain monitor publisher to reconcile chain tips
2 parents 147518f + ff6533a commit f579b1d

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

xcode/DirectBindingsApp/DirectBindingsApp.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@
729729
07BBCE7726D03B49000D96C4 /* TestBroadcasterInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07BBCE6726D03B49000D96C4 /* TestBroadcasterInterface.swift */; };
730730
07C753E126D6BE4E00081BF8 /* HumanObjectPeerTestInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07C753DF26D6BE4E00081BF8 /* HumanObjectPeerTestInstance.swift */; };
731731
07C753F326D9560200081BF8 /* PromiseKit in Frameworks */ = {isa = PBXBuildFile; productRef = 07C753F226D9560200081BF8 /* PromiseKit */; };
732+
28F5174A280E7903009A7D10 /* CombineUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28F51749280E7903009A7D10 /* CombineUtils.swift */; };
732733
2DD11C86114C6827D6E338A2 /* BlockchainObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 076D2A6D28039ACB00970AFC /* BlockchainObserver.swift */; };
733734
2DD11F1338488839D8955B7A /* PolarIntegrationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DD1172C8DF0B244CF69B0FA /* PolarIntegrationTest.swift */; };
734735
2DD11F99CF6E547FA7FB9225 /* RegtestBlockchainManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 076D2A6B280399F500970AFC /* RegtestBlockchainManager.swift */; };
@@ -1137,6 +1138,7 @@
11371138
07BBCE6726D03B49000D96C4 /* TestBroadcasterInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestBroadcasterInterface.swift; sourceTree = "<group>"; };
11381139
07C753DF26D6BE4E00081BF8 /* HumanObjectPeerTestInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HumanObjectPeerTestInstance.swift; sourceTree = "<group>"; };
11391140
07C753E826D954B100081BF8 /* PolarConnectionExperiment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PolarConnectionExperiment.swift; sourceTree = "<group>"; };
1141+
28F51749280E7903009A7D10 /* CombineUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombineUtils.swift; sourceTree = "<group>"; };
11401142
2DD1172C8DF0B244CF69B0FA /* PolarIntegrationTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PolarIntegrationTest.swift; sourceTree = "<group>"; };
11411143
/* End PBXFileReference section */
11421144

@@ -1666,6 +1668,7 @@
16661668
0780F9C6272865260095FD6A /* LNSyncHandler.swift */,
16671669
076D2A6D28039ACB00970AFC /* BlockchainObserver.swift */,
16681670
076D2A6B280399F500970AFC /* RegtestBlockchainManager.swift */,
1671+
28F51749280E7903009A7D10 /* CombineUtils.swift */,
16691672
);
16701673
path = "app-batteries";
16711674
sourceTree = "<group>";
@@ -1880,6 +1883,7 @@
18801883
076D24A927FC219200970AFC /* RouteParameters.swift in Sources */,
18811884
076D230927FC219000970AFC /* C2Tuple_usizeTransactionZ.swift in Sources */,
18821885
076D238527FC219100970AFC /* Result_FundingSignedDecodeErrorZ.swift in Sources */,
1886+
28F5174A280E7903009A7D10 /* CombineUtils.swift in Sources */,
18831887
076D235327FC219100970AFC /* Result_PaymentPreimageAPIErrorZ.swift in Sources */,
18841888
076D22FB27FC219000970AFC /* C2Tuple_PaymentHashPaymentSecretZ.swift in Sources */,
18851889
076D232727FC219100970AFC /* Result_UpdateFulfillHTLCDecodeErrorZ.swift in Sources */,

xcode/DirectBindingsApp/DirectBindingsApp/app-batteries/BlockchainObserver.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
import Foundation
9-
9+
import Combine
1010

1111
@available(iOS 15.0, *)
1212
class BlockchainObserver {
@@ -478,6 +478,18 @@ class BlockchainObserver {
478478
return result
479479
}
480480

481+
public func isMonitoring() async throws -> Bool {
482+
await monitoringTracker.isTracking
483+
}
484+
485+
public var blockchainMonitorPublisher: AnyPublisher<Void, Error> {
486+
Timer.publish(every: 5.0, on: RunLoop.main, in: .default)
487+
.autoconnect()
488+
.asyncMap { [unowned self] _ in
489+
try await self.reconcileChaintips()
490+
}
491+
.eraseToAnyPublisher()
492+
}
481493
}
482494

483495
public protocol BlockchainListener {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// CombineUtils.swift
3+
// DirectBindingsApp
4+
//
5+
// From https://www.swiftbysundell.com/articles/async-and-concurrent-forEach-and-map/
6+
//
7+
// Created by Jurvis Tan on 4/18/22.
8+
//
9+
10+
import Foundation
11+
import Combine
12+
13+
extension Publisher {
14+
func asyncMap<T>(
15+
_ transform: @escaping (Output) async throws -> T
16+
) -> Publishers.FlatMap<Future<T, Error>,
17+
Publishers.SetFailureType<Self, Error>> {
18+
flatMap { value in
19+
Future { promise in
20+
Task {
21+
do {
22+
let output = try await transform(value)
23+
promise(.success(output))
24+
} catch {
25+
promise(.failure(error))
26+
}
27+
}
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)