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

Commit

Permalink
Merge pull request #402 from horizontalsystems/bugfixes
Browse files Browse the repository at this point in the history
Bugfixes (#400, #401)
  • Loading branch information
ant013 committed Jun 25, 2019
2 parents 93a6b3f + b9117df commit 9cd93c4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
7 changes: 3 additions & 4 deletions BitcoinCore/BitcoinCore/Blocks/InitialBlockDownload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class InitialBlockDownload {
public var syncedPeers = [IPeer]()

init(blockSyncer: IBlockSyncer, peerManager: IPeerManager, merkleBlockValidator: IMerkleBlockValidator, syncStateListener: ISyncStateListener,
peersQueue: DispatchQueue = DispatchQueue(label: "PeerGroup Local Queue", qos: .userInitiated),
peersQueue: DispatchQueue = DispatchQueue(label: "InitialBlockDownload Local Queue", qos: .userInitiated),
scheduler: SchedulerType = SerialDispatchQueueScheduler(qos: .background),
logger: Logger? = nil) {
self.blockSyncer = blockSyncer
Expand Down Expand Up @@ -125,7 +125,6 @@ public class InitialBlockDownload {
syncedPeers.append(peer)

subject.onNext(.onPeerSynced(peer: peer))
syncStateListener.syncFinished(all: allPeersSynced)
}

private func setPeerNotSynced(_ peer: IPeer) {
Expand Down Expand Up @@ -154,8 +153,8 @@ public class InitialBlockDownload {
.disposed(by: disposeBag)
}

public var allPeersSynced: Bool {
return syncedPeers.count > 0 && syncedPeers.count >= peerManager.connected().count / 3
public var hasSyncedPeer: Bool {
return syncedPeers.count > 0
}

}
Expand Down
17 changes: 11 additions & 6 deletions BitcoinCore/BitcoinCore/Core/KitStateProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ extension KitStateProvider: ISyncStateListener {
syncState = .notSynced
}

func syncFinished(all: Bool) {
syncState = all ? .synced : .syncing(progress: 1)
}

func initialBestBlockHeightUpdated(height: Int32) {
initialBestBlockHeight = height
currentBestBlockHeight = height
Expand All @@ -43,9 +39,18 @@ extension KitStateProvider: ISyncStateListener {

let blocksDownloaded = currentBestBlockHeight - initialBestBlockHeight
let allBlocksToDownload = maxBlockHeight - initialBestBlockHeight
var progress: Double = 0

if allBlocksToDownload <= 0 || allBlocksToDownload <= blocksDownloaded {
progress = 1.0
} else {
progress = Double(blocksDownloaded) / Double(allBlocksToDownload)
}

if allBlocksToDownload > 0 && allBlocksToDownload > blocksDownloaded {
syncState = .syncing(progress: Double(blocksDownloaded) / Double(allBlocksToDownload))
if progress >= 1 {
syncState = .synced
} else {
syncState = .syncing(progress: progress)
}
}

Expand Down
3 changes: 1 addition & 2 deletions BitcoinCore/BitcoinCore/Core/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ protocol IConnectionTimeoutManager: class {
protocol ISyncStateListener: class {
func syncStarted()
func syncStopped()
func syncFinished(all: Bool)
func initialBestBlockHeightUpdated(height: Int32)
func currentBestBlockHeightUpdated(height: Int32, maxBlockHeight: Int32)
}
Expand Down Expand Up @@ -500,7 +499,7 @@ public protocol IMessageSerializer {
}

public protocol IInitialBlockDownload {
var allPeersSynced: Bool { get }
var hasSyncedPeer: Bool { get }
var observable: Observable<InitialBlockDownloadEvent> { get }
var syncedPeers: [IPeer] { get }
func isSynced(peer: IPeer) -> Bool
Expand Down
21 changes: 13 additions & 8 deletions BitcoinCore/BitcoinCore/Managers/SyncedReadyPeerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ public class SyncedReadyPeerManager {
private var peerStates = [String: Bool]()

private let peerSyncedAndReadySubject = PublishSubject<IPeer>()
private let peersQueue: DispatchQueue

init(peerGroup: IPeerGroup, initialBlockDownload: IInitialBlockDownload) {
init(peerGroup: IPeerGroup, initialBlockDownload: IInitialBlockDownload,
peersQueue: DispatchQueue = DispatchQueue(label: "SyncedReadyPeerManager Local Queue", qos: .userInitiated)) {
self.peerGroup = peerGroup
self.initialBlockDownload = initialBlockDownload
self.peersQueue = peersQueue
}

private func set(state: Bool, to peer: IPeer) {
let oldState = peerStates[peer.host] ?? false
peerStates[peer.host] = state

if oldState != state {
if state {
peerSyncedAndReadySubject.onNext(peer)
} else {
peersQueue.async {
let oldState = self.peerStates[peer.host] ?? false
self.peerStates[peer.host] = state

if oldState != state {
if state {
self.peerSyncedAndReadySubject.onNext(peer)
} else {
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion BitcoinCore/BitcoinCore/Network/TransactionSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TransactionSender {
throw BitcoinCoreErrors.TransactionSendError.noConnectedPeers
}

guard initialBlockDownload.allPeersSynced else {
guard initialBlockDownload.hasSyncedPeer else {
throw BitcoinCoreErrors.TransactionSendError.peersNotSynced
}

Expand Down

0 comments on commit 9cd93c4

Please sign in to comment.