Created
December 29, 2017 22:33
-
-
Save azamsharp/debd83a661d5aacb166d9437601ea50d to your computer and use it in GitHub Desktop.
service resolve
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func resolve(completion :@escaping (Blockchain) -> ()) { | |
// get the nodes | |
let nodes = self.blockchain.nodes | |
for node in nodes { | |
let url = URL(string :"http://\(node.address)/blockchain")! | |
URLSession.shared.dataTask(with: url) { data, _, _ in | |
if let data = data { | |
let blockchain = try! JSONDecoder().decode(Blockchain.self, from: data) | |
if self.blockchain.blocks.count > blockchain.blocks.count { | |
completion(self.blockchain) | |
} else { | |
self.blockchain.blocks = blockchain.blocks | |
completion(blockchain) | |
} | |
} | |
}.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment