-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBlockchain.swift
169 lines (102 loc) · 4.55 KB
/
Blockchain.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// Blockchain.swift
// SwiftStructures
//
// Created by Wayne Bishop on 1/31/18.
// Copyright © 2018 Arbutus Software Inc. All rights reserved.
//
import Foundation
public class Blockchain: Graph {
var chain = Array<Block>()
var queue = Queue<Exchange>()
var threshold: Int
init(ithreshold: Int = 3) {
threshold = ithreshold
super.init(directed: false)
chain.append(genesisBlock())
}
//MARK: helper functions
//starting block
private func genesisBlock()-> Block {
let firstBlock = Block()
firstBlock.id = blockIdentifier()
firstBlock.description = "Genesis Block"
return firstBlock
}
//broadcast latest block to peers
func broadcast(to peer: inout Peer) {
peer.chain = self.chain
}
//obtain latest block
private func currentBlock()-> Block {
return chain[chain.endIndex - 1]
}
//generate block identifier (e.g. potential mining operation..)
private func blockIdentifier() -> String {
return UUID().uuidString
}
class Miner {
//poll the network for pending exchanges..
func poll(startingv: Vertex, network: Blockchain) {
/*
note: this sequence performs a graph traversal via bfs
(breadth-first search). Note the trailing closure declared
as an inout variable. This provides the mechanisim to update effected
peers "by reference".
*/
network.traverse(startingv) { ( node: inout Vertex) -> () in
//trival case
guard let peer = node as? Peer else {
return
}
/*
note: exchanges are queued before they are
added into the main blockchain.
*/
let queue = network.queue
let threshold = network.threshold
let intentions = peer.exchanges(requester: self)
for exchange in intentions {
//queue exchange
queue.enQueue(exchange)
print("queued exchange of $\(exchange.amount).")
if queue.count == threshold {
/*
note: due to the potential complexity in mining
a new block, this process would be initiated through an
asynchronous process.
*/
let newBlock = self.createBlock(for: network)
//update main network chain
network.chain.append(newBlock)
}
} //end for
peer.flush(requester: self)
peer.visited = true
}
}
private func createBlock(for network: Blockchain) -> Block {
//establish queue
let queue = network.queue
let newblock = Block()
var transactions = Array<Exchange>()
/*
note: dequeue all pending exchanges from the main queue into a single block. note how the
queue is a member of the network a not the specific Miner. As a result,
other miner instances could theroetically access the shared queue to
push exchanges.
*/
while queue.count != 0 {
if let exchange = queue.deQueue() {
transactions.append(exchange)
}
}
//build the new block
newblock.miner = self
newblock.id = network.blockIdentifier()
newblock.previous = network.currentBlock().id
newblock.transactions = transactions
return newblock
}
}
}