@@ -123,7 +123,7 @@ public class BitcoinTests: XCTestCase {
123
123
// let address = try await rpcInterface.generateAddress()
124
124
// let details = try await rpcInterface.mineBlocks(number: 1, coinbaseDestinationAddress: address)
125
125
let helpDetails = try await rpcInterface. getHelp ( )
126
- print ( helpDetails)
126
+ // print(helpDetails)
127
127
128
128
let exampleFundingTx : [ UInt8 ] = [ 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 255 , 255 , 255 , 255 , 0 , 255 , 255 , 255 , 255 , 1 , 160 , 134 , 1 , 0 , 0 , 0 , 0 , 0 , 34 , 0 , 32 , 15 , 45 , 153 , 123 , 98 , 37 , 242 , 142 , 113 , 51 , 126 , 45 , 206 , 175 , 66 , 247 , 173 , 2 , 141 , 2 , 27 , 84 , 38 , 188 , 34 , 74 , 82 , 164 , 28 , 229 , 39 , 139 , 1 , 1 , 1 , 0 , 0 , 0 , 0 ]
129
129
@@ -132,16 +132,18 @@ public class BitcoinTests: XCTestCase {
132
132
133
133
class Listener : BlockchainListener {
134
134
var initializationComplete = false
135
- var newBlockDetected = false
135
+ var newBlocksDetected = 0
136
+ var blocksLost = 0
136
137
137
138
func blockConnected( block: [ UInt8 ] , height: UInt32 ) {
138
139
if self . initializationComplete {
139
- self . newBlockDetected = true
140
+ self . newBlocksDetected += 1
140
141
}
141
142
print ( " block connected at height \( height) : \( block) " )
142
143
}
143
144
144
145
func blockDisconnected( header: [ UInt8 ] ? , height: UInt32 ) {
146
+ self . blocksLost += 1
145
147
print ( " block disconnected from height \( height) : \( header) " )
146
148
}
147
149
}
@@ -157,10 +159,36 @@ public class BitcoinTests: XCTestCase {
157
159
let exampleOutputScript : [ UInt8 ] = [ 0 , 32 , 200 , 194 , 75 , 55 , 227 , 33 , 251 , 71 , 196 , 33 , 177 , 196 , 155 , 145 , 17 , 78 , 244 , 226 , 155 , 141 , 216 , 230 , 180 , 183 , 149 , 172 , 116 , 249 , 56 , 6 , 118 , 255 ]
158
160
let scriptInfo = try await rpcInterface. decodeScript ( script: exampleOutputScript)
159
161
let outputAddress = ( scriptInfo [ " addresses " ] as! [ String ] ) . first!
162
+ try await rpcInterface. mineBlocks ( number: 1 , coinbaseDestinationAddress: testAddress)
160
163
try await rpcInterface. mineBlocks ( number: 1 , coinbaseDestinationAddress: outputAddress)
164
+
161
165
// sleep for six seconds
162
166
try await Task . sleep ( nanoseconds: 6_000_000_000 )
163
- XCTAssertEqual ( listener. newBlockDetected, true )
167
+ XCTAssertEqual ( listener. newBlocksDetected, 2 )
168
+ XCTAssertEqual ( listener. blocksLost, 0 )
169
+
170
+ do {
171
+ let testAddress = try await rpcInterface. generateAddress ( )
172
+ let chaintip = try await rpcInterface. getChaintipHeight ( )
173
+ let penultimateBlockHash = try await rpcInterface. getBlockHashHex ( height: chaintip - 1 )
174
+ try await rpcInterface. unmineBlock ( hash: penultimateBlockHash)
175
+ try await rpcInterface. mineBlocks ( number: 1 , coinbaseDestinationAddress: testAddress)
176
+
177
+ try await rpcInterface. reconcileChaintips ( )
178
+ XCTAssertEqual ( listener. newBlocksDetected, 3 )
179
+ XCTAssertEqual ( listener. blocksLost, 2 )
180
+ }
181
+
182
+ do {
183
+ let testAddress = try await rpcInterface. generateAddress ( )
184
+ let chaintipHash = try await rpcInterface. getChaintipHashHex ( )
185
+ try await rpcInterface. unmineBlock ( hash: chaintipHash)
186
+ try await rpcInterface. mineBlocks ( number: 2 , coinbaseDestinationAddress: testAddress)
187
+
188
+ try await rpcInterface. reconcileChaintips ( )
189
+ XCTAssertEqual ( listener. newBlocksDetected, 5 )
190
+ XCTAssertEqual ( listener. blocksLost, 3 )
191
+ }
164
192
}
165
193
166
194
}
0 commit comments