Skip to content

Commit ef8bc31

Browse files
committed
Starting work on blockchain mining algorithm..
1 parent e1acca6 commit ef8bc31

File tree

4 files changed

+72
-25
lines changed

4 files changed

+72
-25
lines changed

Source/Extensions/Chain.swift

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import Foundation
1010

1111

1212
extension Blockchain {
13-
14-
15-
//TODO: create a computed property of hashValue to simulate bitcoin mining that
16-
//includes some sort of difficulty level..
1713

1814

1915
//initialize starting block
@@ -43,4 +39,55 @@ extension Blockchain {
4339
}
4440

4541

42+
//TODO: estblish new block based on provided input and the level of difficulty.
43+
func hashValue(_ element: Block) -> Hash? {
44+
45+
var rawvalue = Int()
46+
var leadvalue = Int()
47+
48+
49+
//check tranactions
50+
guard let items = element.transactions else {
51+
return nil
52+
}
53+
54+
55+
//sum hash values
56+
for exchange in items {
57+
rawvalue = exchange.amount.hashValue
58+
rawvalue += exchange.from.hashValue
59+
rawvalue += exchange.to.hashValue
60+
rawvalue += exchange.lastModified.hashValue
61+
}
62+
63+
64+
//sum previous hash
65+
if let previous = element.previous {
66+
rawvalue += previous.hashValue
67+
}
68+
69+
70+
while leadvalue >= difficulty {
71+
72+
print("mining new block..")
73+
let testvalue = rawvalue + String(describing: Date()).hashValue
74+
75+
let rawlist: Array<Int> = String(testvalue).characters.flatMap{Int(String($0))}
76+
77+
for m in 0..<rawlist.startIndex + 3 {
78+
leadvalue += rawlist[m]
79+
}
80+
81+
//perform final bit shift and test leading values
82+
if (leadvalue >> 2) >= difficulty {
83+
return String(testvalue)
84+
}
85+
86+
}
87+
88+
return nil
89+
90+
}
91+
92+
4693
}

Source/Factories/Blockchain.swift

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public class Blockchain: Graph {
1515
var queue: Queue<Exchange>
1616

1717

18-
let threshold: Int = 0
19-
let difficulty: Int = 0
18+
//todo: change these to init parameters..
19+
let threshold: Int = 3
20+
let difficulty: Int = 25
2021

2122

2223
//initialize as an undirected graph.
@@ -53,16 +54,18 @@ public class Blockchain: Graph {
5354
}
5455

5556

56-
/*
57+
/*
5758
note: exchanges are queued before they are
5859
added into the main blockchain.
5960
*/
61+
62+
let queue = network.queue
63+
let threshold = network.threshold
64+
let chain = network.chain
6065

66+
6167
for exchange in peer.intentions {
6268

63-
let queue = network.queue
64-
let threshold = network.threshold
65-
6669
//queue items depending on the network threshold.
6770
if queue.count <= threshold {
6871
queue.enQueue(exchange)
@@ -78,8 +81,11 @@ public class Blockchain: Graph {
7881
asynchronous process.
7982
*/
8083

81-
self.newBlock(for: network)
82-
84+
if self.newBlock(for: network) != true {
85+
print("unable to create new block..")
86+
return
87+
}
88+
8389
//remove pending transactions
8490
peer.intentions.removeAll()
8591
}
@@ -91,7 +97,7 @@ public class Blockchain: Graph {
9197
}
9298

9399

94-
private func newBlock(for network: Blockchain) {
100+
private func newBlock(for network: Blockchain) -> Bool {
95101

96102
//esablish queue
97103
let queue = network.queue
@@ -100,9 +106,9 @@ public class Blockchain: Graph {
100106

101107

102108
/*
103-
note: dequeue all pending exchanges from the main queue into a single block. now how the
104-
queue is a member of the Network not the specific Minder. As a result,
105-
other miner instances could theroetically be able to access the shared queue to
109+
note: dequeue all pending exchanges from the main queue into a single block. note how the
110+
queue is a member of the network a not the specific Miner. As a result,
111+
other miner instances could theroetically access the shared queue to
106112
push exchanges.
107113
*/
108114

@@ -114,20 +120,18 @@ public class Blockchain: Graph {
114120
}
115121

116122

117-
//building the new block
123+
//build the new block
118124
newblock.miner = self
119125
newblock.previous = network.currentBlock().key
120126
newblock.transactions = transactions
121-
122-
127+
newblock.key = network.hashValue(newblock)
123128

124129
/*
125130
note: This is also where the hash algorithm for each block obtained. For clarity, make the
126131
algorithm function an extension.
127132
*/
128133

129-
130-
134+
return false
131135
}
132136

133137
}

SwiftTests/BlockTest.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,8 @@ class BlockTest: XCTestCase {
4848

4949
//miners monitor the peer network for intended transactions
5050
func testMiningBlock() {
51-
5251
let miner = Blockchain.Miner()
53-
5452
miner.poll(startingv: PeerA, network: bitcoin)
55-
56-
5753
}
5854

5955

0 commit comments

Comments
 (0)