Skip to content

Commit

Permalink
Merge pull request #33 from jurvis/regtest_monitor_tooling
Browse files Browse the repository at this point in the history
Add blockchain monitor publisher to reconcile chain tips
  • Loading branch information
arik-so committed Apr 20, 2022
2 parents 147518f + ff6533a commit f579b1d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@
07BBCE7726D03B49000D96C4 /* TestBroadcasterInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07BBCE6726D03B49000D96C4 /* TestBroadcasterInterface.swift */; };
07C753E126D6BE4E00081BF8 /* HumanObjectPeerTestInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07C753DF26D6BE4E00081BF8 /* HumanObjectPeerTestInstance.swift */; };
07C753F326D9560200081BF8 /* PromiseKit in Frameworks */ = {isa = PBXBuildFile; productRef = 07C753F226D9560200081BF8 /* PromiseKit */; };
28F5174A280E7903009A7D10 /* CombineUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28F51749280E7903009A7D10 /* CombineUtils.swift */; };
2DD11C86114C6827D6E338A2 /* BlockchainObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 076D2A6D28039ACB00970AFC /* BlockchainObserver.swift */; };
2DD11F1338488839D8955B7A /* PolarIntegrationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DD1172C8DF0B244CF69B0FA /* PolarIntegrationTest.swift */; };
2DD11F99CF6E547FA7FB9225 /* RegtestBlockchainManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 076D2A6B280399F500970AFC /* RegtestBlockchainManager.swift */; };
Expand Down Expand Up @@ -1137,6 +1138,7 @@
07BBCE6726D03B49000D96C4 /* TestBroadcasterInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestBroadcasterInterface.swift; sourceTree = "<group>"; };
07C753DF26D6BE4E00081BF8 /* HumanObjectPeerTestInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HumanObjectPeerTestInstance.swift; sourceTree = "<group>"; };
07C753E826D954B100081BF8 /* PolarConnectionExperiment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PolarConnectionExperiment.swift; sourceTree = "<group>"; };
28F51749280E7903009A7D10 /* CombineUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombineUtils.swift; sourceTree = "<group>"; };
2DD1172C8DF0B244CF69B0FA /* PolarIntegrationTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PolarIntegrationTest.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -1666,6 +1668,7 @@
0780F9C6272865260095FD6A /* LNSyncHandler.swift */,
076D2A6D28039ACB00970AFC /* BlockchainObserver.swift */,
076D2A6B280399F500970AFC /* RegtestBlockchainManager.swift */,
28F51749280E7903009A7D10 /* CombineUtils.swift */,
);
path = "app-batteries";
sourceTree = "<group>";
Expand Down Expand Up @@ -1880,6 +1883,7 @@
076D24A927FC219200970AFC /* RouteParameters.swift in Sources */,
076D230927FC219000970AFC /* C2Tuple_usizeTransactionZ.swift in Sources */,
076D238527FC219100970AFC /* Result_FundingSignedDecodeErrorZ.swift in Sources */,
28F5174A280E7903009A7D10 /* CombineUtils.swift in Sources */,
076D235327FC219100970AFC /* Result_PaymentPreimageAPIErrorZ.swift in Sources */,
076D22FB27FC219000970AFC /* C2Tuple_PaymentHashPaymentSecretZ.swift in Sources */,
076D232727FC219100970AFC /* Result_UpdateFulfillHTLCDecodeErrorZ.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation

import Combine

@available(iOS 15.0, *)
class BlockchainObserver {
Expand Down Expand Up @@ -478,6 +478,18 @@ class BlockchainObserver {
return result
}

public func isMonitoring() async throws -> Bool {
await monitoringTracker.isTracking
}

public var blockchainMonitorPublisher: AnyPublisher<Void, Error> {
Timer.publish(every: 5.0, on: RunLoop.main, in: .default)
.autoconnect()
.asyncMap { [unowned self] _ in
try await self.reconcileChaintips()
}
.eraseToAnyPublisher()
}
}

public protocol BlockchainListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// CombineUtils.swift
// DirectBindingsApp
//
// From https://www.swiftbysundell.com/articles/async-and-concurrent-forEach-and-map/
//
// Created by Jurvis Tan on 4/18/22.
//

import Foundation
import Combine

extension Publisher {
func asyncMap<T>(
_ transform: @escaping (Output) async throws -> T
) -> Publishers.FlatMap<Future<T, Error>,
Publishers.SetFailureType<Self, Error>> {
flatMap { value in
Future { promise in
Task {
do {
let output = try await transform(value)
promise(.success(output))
} catch {
promise(.failure(error))
}
}
}
}
}
}

0 comments on commit f579b1d

Please sign in to comment.