-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_britcoin.py
332 lines (252 loc) · 8.84 KB
/
test_britcoin.py
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import mock
from freezegun import freeze_time
from helga_britcoin import (
BritBlock, BritChain, proof_of_conversation,
work, BritCoinPlugin
)
class BaseBritcoinTest(object):
@mock.patch('helga_britcoin.db')
def setup(self, mock_db):
db = mock_db.britcoin.find.return_value
db.sort.return_value = []
self.blockchain = BritChain()
class TestBritcoinPreprocessTest(BaseBritcoinTest):
@mock.patch('helga_britcoin.db')
def setup(self, mock_db):
super(TestBritcoinPreprocessTest, self).setup()
self.plugin = BritCoinPlugin()
self.mine_mock = mock.Mock()
self.plugin.blockchain.mine = self.mine_mock
def test_preprocess_mine(self):
self.plugin.preprocess(None, None, 'bigjust', 'message')
self.mine_mock.assert_called()
@mock.patch('helga_britcoin.IGNORED', ['bigjust'])
def test_preprocess_ignored(self):
self.plugin.preprocess(None, None, 'bigjust', 'message')
self.mine_mock.assert_not_called()
@mock.patch('helga_britcoin.CMD_PREFIX', '!')
def test_preprocess_ignore_command_prefix(self):
self.plugin.preprocess(None, None, 'bigjust', '!message')
self.mine_mock.assert_not_called()
class TestBritcoinPlugin(BaseBritcoinTest):
@mock.patch('helga_britcoin.db')
def test_plugin_init(self, mock_db):
db = mock_db.britcoin.find.return_value
db.sort.return_value = []
plugin = BritCoinPlugin()
assert plugin.blockchain is not None
assert len(plugin.blockchain) == 1
@mock.patch('helga_britcoin.work')
def test_proofer_successful(self, mock_work):
"""
Make sure proof function returns hash attempt if successful.
"""
mock_work.return_value = '00abcd'
attempt = proof_of_conversation('00aaaa', 'test message')
assert attempt is not None
@mock.patch('helga_britcoin.work')
def test_proofer_unsuccessful(self, mock_work):
"""
Make sure proof function returns None for bad attempt.
"""
mock_work.return_value = '1234abcd'
attempt = proof_of_conversation('00aaaa', 'test message')
assert attempt is None
@mock.patch('helga_britcoin.db')
@mock.patch('helga_britcoin.work')
def test_mine_successful(self, mock_work, mock_db):
"""
If mining is successful, test that the miner gets credit.
"""
mock_work.return_value = '00'
self.blockchain.mine('bigjust', 'test message')
last_block = self.blockchain.latest_block()
mock_db.britcoin.insert.assert_called_with(last_block.__dict__)
assert len(last_block.data['transactions']) == 1
assert last_block.data['transactions'][0]['to'] == 'bigjust'
@mock.patch('helga_britcoin.work')
def test_mine_unsuccessful(self, mock_work):
"""
if mining is unsuccessful, nothing happens.
"""
mock_work.return_value = '01'
output = self.blockchain.mine('bigjust', 'test message')
assert output is None
def test_britcoin_balances(self):
self.blockchain.append(
BritBlock(
0, 0,
{'transactions': [{
'from': 'network',
'to': 'brit',
'amount': 1
},{
'from': 'network',
'to': 'bigjust',
'amount': 2
}
]},
"0"
))
self.blockchain.append(
BritBlock(
0, 0,
{'transactions': [{
'from': 'bigjust',
'to': 'brit',
'amount': 1
}]},
"0"
))
balances = self.blockchain.calculate_balances()
assert balances['brit'] == 2
assert balances['bigjust'] == 1
assert balances['network'] == -3
def test_genesis_block_creation(self):
"""
Test that the genesis block gets created on an empty
blockchain.
"""
assert len(self.blockchain) == 1
@mock.patch('helga_britcoin.db')
@mock.patch('helga_britcoin.proof_of_conversation')
def test_send_britcoin(self, mock_proof, mock_db):
"""
Test that a user can send britcoins to another user.
"""
mock_proof.return_value = True
self.blockchain.append(
BritBlock(
0, '2016-11-07 10:00:00',
{'transactions': [{
'from': 'network',
'to': 'brit',
'amount': 90
},{
'from': 'network',
'to': 'bigjust',
'amount': 60
}
]},
"0"
))
self.blockchain.pending_transactions.append({
u'from': 'bigjust',
u'to': 'brit',
u'amount': 1,
u'memo': 'have a coin.',
})
self.blockchain.mine('alkapwn', 'some wisdom')
balances = self.blockchain.calculate_balances()
assert balances['bigjust'] == 59
assert balances['brit'] == 91
@mock.patch('helga_britcoin.db')
def test_genesis_block_defer(self, mock_db):
"""
Test that the genesis block doesn't get created if a
blockchain is being retrieved from storage.
"""
genesis_block = {
'index': 0,
'timestamp': 'now',
'data': 'from db',
'previous_hash': '0',
}
db = mock_db.britcoin.find.return_value
db.sort.return_value = [genesis_block]
blockchain = BritChain()
assert len(blockchain) == 1
assert blockchain[0].data == 'from db'
@mock.patch('helga_britcoin.db')
def test_load_blockchain(self, mock_db):
"""
Tests loading and verifying blockchain from mocked data
store. The third block will fail verification, leaving two
verified blocks in the chain.
"""
genesis_block = {
'index': 0,
'timestamp': 'now',
'data': 'from db',
'previous_hash': '0',
}
second_block = {
'index': 1,
'timestamp': 'now',
'data': 'another from db',
'previous_hash': 'cb8c80e8f7311d050c078021c38382bfc3c3a6ad9fb2255cbe619de54703df8e',
}
third_block = {
'index': 2,
'timestamp': 'now',
'data': 'tampered block',
'previous_hash': 'bleh',
}
db = mock_db.britcoin.find.return_value
db.sort.return_value = [genesis_block, second_block, third_block]
blockchain = BritChain()
assert len(blockchain) == 2
assert blockchain[1].data == 'another from db'
def test_order_in_hash(self):
"""
Make sure that arbitrary ordering in dictionary elements results in the same hash.
"""
block1_data = {
'proof-of-work': 'work_did',
'transactions': [{
'to': 'bigjust',
'from': 'brit',
'amount': 9000,
}],
}
block2_data = {
'transactions': [{
'from': 'brit',
'amount': 9000,
'to': 'bigjust',
}],
'proof-of-work': 'work_did',
}
block1 = BritBlock(0, 'nowish', block1_data, 'abcd')
block2 = BritBlock(0, 'nowish', block2_data, 'abcd')
assert block1.hash == block2.hash
def test_work_function(self):
"""
Make sure work works.
"""
expected_hash = '52a86b9b940a0539ffe8fa4517fb3569329b7219d0c74d82b2130d0f0dff56d1'
assert work('abc', 'message') == expected_hash
@freeze_time("2016-11-07 13:00:00")
class TestBritcoinPluginStats(BaseBritcoinTest):
def test_stats(self):
"""
make sure the stats we care about are included in the output
"""
self.blockchain.append(
BritBlock(
0, '2016-11-07 10:00:00',
{'transactions': [{
'from': 'network',
'to': 'brit',
'amount': 90
},{
'from': 'network',
'to': 'bigjust',
'amount': 60
}
]},
"0"
))
self.blockchain.append(
BritBlock(
0, '2016-11-07 11:00:00',
{'transactions': [{
'from': 'bigjust',
'to': 'brit',
'amount': 1
}]},
"0"
))
stats_message = self.blockchain.stats()
assert '150 britcoins' in stats_message
assert '48 seconds' in stats_message