Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created December 29, 2017 22:33
Show Gist options
  • Save azamsharp/debd83a661d5aacb166d9437601ea50d to your computer and use it in GitHub Desktop.
Save azamsharp/debd83a661d5aacb166d9437601ea50d to your computer and use it in GitHub Desktop.
service resolve
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