Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Feb 28, 2019
2 parents 0272c54 + d476714 commit 75e8b6c
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 103 deletions.
2 changes: 1 addition & 1 deletion HSBitcoinKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'HSBitcoinKit'
spec.version = '0.4'
spec.version = '0.4.1'
spec.summary = 'Bitcoin wallet library for Swift'
spec.description = <<-DESC
HSBitcoinKit implements Bitcoin protocol in Swift. It is an implementation of the Bitcoin SPV protocol written (almost) entirely in swift.
Expand Down
4 changes: 0 additions & 4 deletions HSBitcoinKit/HSBitcoinKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
58AAA23384287398D79F14A6 /* TransactionPublicKeySetterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58AAA97B8114B3CE27A6D32C /* TransactionPublicKeySetterTests.swift */; };
58AAA2C85BFE1C624D1A1997 /* SigHashType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58AAAA63CA9758D7D97E1416 /* SigHashType.swift */; };
58AAA2EC7A7825B8E4408AA7 /* HeaderValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58AAACDCDC4B51B6E2C9C07D /* HeaderValidatorTests.swift */; };
58AAA2F92A0A8FD899EC9EB8 /* PeriodicTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58AAAA8E38F67B6230810DF8 /* PeriodicTimer.swift */; };
58AAA317128E10EE72E0D0B1 /* BlockHashFetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58AAAD725CB287055DC7CCE5 /* BlockHashFetcherTests.swift */; };
58AAA3206EF27B4A13AD30E4 /* EDAValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58AAA454CCE3AE60B70E8DAF /* EDAValidatorTests.swift */; };
58AAA321A8780859892D0C73 /* TransactionOutputExtractorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58AAAE28D740FD1FCF9DC1B2 /* TransactionOutputExtractorTests.swift */; };
Expand Down Expand Up @@ -387,7 +386,6 @@
58AAA9BB5301D7CB5292473B /* BlockDiscoveryBatchTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockDiscoveryBatchTest.swift; sourceTree = "<group>"; };
58AAAA63CA9758D7D97E1416 /* SigHashType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SigHashType.swift; sourceTree = "<group>"; };
58AAAA7C33E2529A70D0E529 /* TransactionSizeCalculator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionSizeCalculator.swift; sourceTree = "<group>"; };
58AAAA8E38F67B6230810DF8 /* PeriodicTimer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PeriodicTimer.swift; sourceTree = "<group>"; };
58AAAAFCA9E73AD5A7AB4CA6 /* BitcoinMainNetTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BitcoinMainNetTests.swift; sourceTree = "<group>"; };
58AAAB31CA6F582F89A1BA14 /* LegacyTestNetDifficultyValidatorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LegacyTestNetDifficultyValidatorTests.swift; sourceTree = "<group>"; };
58AAAB4878F15C91F08E0C15 /* BlockValidatorError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockValidatorError.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -726,7 +724,6 @@
58AAA684704A912E02B1138B /* FeeRateManager.swift */,
58AAAD686D0739426895F243 /* FeeRateSyncer.swift */,
11B35959F0347D6C6866E616 /* IpfsApi.swift */,
58AAAA8E38F67B6230810DF8 /* PeriodicTimer.swift */,
2FA5D6676E3CB6028E1B6018 /* ReachabilityManager.swift */,
11B358481D7E7EB156912F52 /* StateManager.swift */,
58AAA983B426801C46003567 /* UnspentOutputSelector.swift */,
Expand Down Expand Up @@ -1459,7 +1456,6 @@
2FA5DA409AB91D54163250D7 /* Logger.swift in Sources */,
58AAAF9EF0EEF4BE3E5AD283 /* FeeRate.swift in Sources */,
58AAA94314B3E3498DEB5DD6 /* FeeRateSyncer.swift in Sources */,
58AAA2F92A0A8FD899EC9EB8 /* PeriodicTimer.swift in Sources */,
58AAA050DC164978E21C62FA /* RealmFactory.swift in Sources */,
58AAAA8E4240E008471D7D62 /* RealmStorage.swift in Sources */,
58AAAA5042AAE1661ECE5CB5 /* BcoinProvider.swift in Sources */,
Expand Down
7 changes: 2 additions & 5 deletions HSBitcoinKit/HSBitcoinKit/Core/BitcoinKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class BitcoinKit {

private let realmStorage: RealmStorage

private let feeRateTimer: IPeriodicTimer
private let feeRateApiProvider: IApiConfigProvider
private let feeRateSyncer: FeeRateSyncer
private let feeRateManager: FeeRateManager
Expand Down Expand Up @@ -154,10 +153,8 @@ public class BitcoinKit {

bcoinReachabilityManager = ReachabilityManager(configProvider: feeRateApiProvider)

feeRateTimer = PeriodicTimer(interval: 3 * 60)
feeRateSyncer = FeeRateSyncer(networkManager: ipfsApi, timer: feeRateTimer)
feeRateManager = FeeRateManager(storage: realmStorage, syncer: feeRateSyncer, reachabilityManager: bcoinReachabilityManager, timer: feeRateTimer)
feeRateSyncer.delegate = feeRateManager
feeRateSyncer = FeeRateSyncer(networkManager: ipfsApi)
feeRateManager = FeeRateManager(storage: realmStorage, syncer: feeRateSyncer, reachabilityManager: bcoinReachabilityManager)

inputSigner = InputSigner(hdWallet: hdWallet, network: network)
scriptBuilder = ScriptBuilder()
Expand Down
9 changes: 0 additions & 9 deletions HSBitcoinKit/HSBitcoinKit/Core/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ protocol IReachabilityManager {
var reachabilitySignal: Signal { get }
}

protocol IPeriodicTimer {
var delegate: IPeriodicTimerDelegate? { get set }
func schedule()
}

protocol IPeriodicTimerDelegate: class {
func onFire()
}

protocol IPeerHostManager {
var delegate: PeerHostManagerDelegate? { get set }
var peerHost: String? { get }
Expand Down
30 changes: 13 additions & 17 deletions HSBitcoinKit/HSBitcoinKit/Managers/FeeRateManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ class FeeRateManager {
let subject = PublishSubject<Void>()

private let storage: IFeeRateStorage
private let syncer: IFeeRateSyncer
private var timer: IPeriodicTimer
private var syncer: IFeeRateSyncer
private let reachabilityManager: IReachabilityManager

init(storage: IFeeRateStorage, syncer: IFeeRateSyncer, reachabilityManager: IReachabilityManager, timer: IPeriodicTimer, async: Bool = true) {
init(storage: IFeeRateStorage, syncer: IFeeRateSyncer, reachabilityManager: IReachabilityManager, async: Bool = true) {
self.storage = storage
self.syncer = syncer
self.timer = timer
self.reachabilityManager = reachabilityManager

self.timer.delegate = self
self.syncer.delegate = self

Observable<Int>.timer(0, period: 3 * 60, scheduler: ConcurrentDispatchQueueScheduler(qos: .background)).observeOn(ConcurrentDispatchQueueScheduler(qos: .background)).subscribe(onNext: { [weak self] _ in
self?.updateFeeRate()
}).disposed(by: disposeBag)

var observable = reachabilityManager.reachabilitySignal.asObservable()

Expand All @@ -24,15 +28,15 @@ class FeeRateManager {
}
observable
.subscribe(onNext: { [weak self] in
if reachabilityManager.isReachable {
self?.updateFeeRate()
}
self?.updateFeeRate()
})
.disposed(by: disposeBag)
}

private func updateFeeRate() {
syncer.sync()
if reachabilityManager.isReachable {
syncer.sync()
}
}

}
Expand All @@ -58,14 +62,6 @@ extension FeeRateManager: IFeeRateManager {

}

extension FeeRateManager: IPeriodicTimerDelegate {

func onFire() {
updateFeeRate()
}

}

extension FeeRateManager: IFeeRateSyncerDelegate {

func didSync(feeRate: FeeRate) {
Expand Down
5 changes: 1 addition & 4 deletions HSBitcoinKit/HSBitcoinKit/Managers/FeeRateSyncer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ class FeeRateSyncer {
weak var delegate: IFeeRateSyncerDelegate?

private let networkManager: IFeeRateApi
private let timer: IPeriodicTimer
private let async: Bool

init(networkManager: IFeeRateApi, timer: IPeriodicTimer, async: Bool = true) {
init(networkManager: IFeeRateApi, async: Bool = true) {
self.networkManager = networkManager
self.timer = timer
self.async = async
}

Expand All @@ -28,7 +26,6 @@ extension FeeRateSyncer: IFeeRateSyncer {
}
observable
.subscribe(onNext: { [weak self] feeRate in
self?.timer.schedule()
self?.delegate?.didSync(feeRate: feeRate)
}, onError: { _ in
//do nothing
Expand Down
30 changes: 0 additions & 30 deletions HSBitcoinKit/HSBitcoinKit/Managers/PeriodicTimer.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ class DataProviderTests: XCTestCase {
}
dataProvider.onInsert(block: TestData.firstBlock)

waitForMainQueue()
waitForMainQueue()

verify(mockDataProviderDelegate).balanceUpdated(balance: equal(to: 3))
Expand Down
25 changes: 9 additions & 16 deletions HSBitcoinKit/HSBitcoinKitTests/Managers/FeeRateManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class FeeRateManagerTests: XCTestCase {
private var mockStorage: MockIFeeRateStorage!
private var mockSyncer: MockIFeeRateSyncer!
private var mockReachabilityManager: MockIReachabilityManager!
private var mockTimer: MockIPeriodicTimer!

private var manager: FeeRateManager!

Expand All @@ -22,32 +21,29 @@ class FeeRateManagerTests: XCTestCase {
mockStorage = MockIFeeRateStorage()
mockSyncer = MockIFeeRateSyncer()
mockReachabilityManager = MockIReachabilityManager()
mockTimer = MockIPeriodicTimer()

stub(mockStorage) { mock in
when(mock.feeRate.get).thenReturn(feeRate)
when(mock.save(feeRate: any())).thenDoNothing()
}
stub(mockSyncer) { mock in
when(mock.sync()).thenDoNothing()
when(mock.sync()).then {
print("sdfsdfdssdf")
}
when(mock.delegate.set(any())).thenDoNothing()
}
stub(mockReachabilityManager) { mock in
when(mock.reachabilitySignal.get).thenReturn(reachabilitySubject)
}
stub(mockTimer) { mock in
when(mock.delegate.set(any())).thenDoNothing()
when(mock.schedule()).thenDoNothing()
when(mock.isReachable.get).thenReturn(false)
}


manager = FeeRateManager(storage: mockStorage, syncer: mockSyncer, reachabilityManager: mockReachabilityManager, timer: mockTimer, async: false)
manager = FeeRateManager(storage: mockStorage, syncer: mockSyncer, reachabilityManager: mockReachabilityManager, async: false)
}

override func tearDown() {
mockStorage = nil
mockSyncer = nil
mockReachabilityManager = nil
mockTimer = nil

manager = nil

Expand All @@ -72,7 +68,9 @@ class FeeRateManagerTests: XCTestCase {
}
reachabilitySubject.onNext(())

verify(mockSyncer).sync()
waitForMainQueue()

verify(mockSyncer, atLeastOnce()).sync()
}

func testSyncFeeRate_OnReachabilityChanged_Disconnected() {
Expand All @@ -83,11 +81,6 @@ class FeeRateManagerTests: XCTestCase {
verify(mockSyncer, never()).sync()
}

func testSyncFeeRate_OnTimerTick() {
manager.onFire()
verify(mockSyncer).sync()
}

func testDidSyncRate() {
let subjectExpectation = expectation(description: "Subject")
_ = manager.subject.subscribe(onNext: {
Expand Down
17 changes: 1 addition & 16 deletions HSBitcoinKit/HSBitcoinKitTests/Managers/FeeRateSyncerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class FeeRateSyncerTests: XCTestCase {

private var mockDelegate: MockIFeeRateSyncerDelegate!
private var mockNetworkManager: MockIFeeRateApi!
private var mockTimer: MockIPeriodicTimer!


private var syncer: FeeRateSyncer!

Expand All @@ -20,26 +18,21 @@ class FeeRateSyncerTests: XCTestCase {

mockDelegate = MockIFeeRateSyncerDelegate()
mockNetworkManager = MockIFeeRateApi()
mockTimer = MockIPeriodicTimer()

stub(mockNetworkManager) { mock in
when(mock.getFeeRate()).thenReturn(Observable.just(feeRate))
}
stub(mockDelegate) { mock in
when(mock.didSync(feeRate: any())).thenDoNothing()
}
stub(mockTimer) { mock in
when(mock.schedule()).thenDoNothing()
}

syncer = FeeRateSyncer(networkManager: mockNetworkManager, timer: mockTimer, async: false)
syncer = FeeRateSyncer(networkManager: mockNetworkManager, async: false)
syncer.delegate = mockDelegate
}

override func tearDown() {
mockDelegate = nil
mockNetworkManager = nil
mockTimer = nil

syncer = nil

Expand All @@ -52,19 +45,11 @@ class FeeRateSyncerTests: XCTestCase {
verify(mockDelegate).didSync(feeRate: equal(to: feeRate))
}

func testInvalidateTimerOnSync() {
syncer.sync()

verify(mockTimer).schedule()
}

func testNonInvalidateTimerOnError() {
stub(mockNetworkManager) { mock in
when(mock.getFeeRate()).thenReturn(Observable.error(AnyError.any))
}
syncer.sync()

verifyNoMoreInteractions(mockTimer)
}

}

0 comments on commit 75e8b6c

Please sign in to comment.