forked from DeFiCh/ain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_anchor_rewards.py
executable file
·428 lines (341 loc) · 18.3 KB
/
feature_anchor_rewards.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python3
# Copyright (c) 2014-2019 The Bitcoin Core developers
# Copyright (c) DeFi Blockchain Developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
"""Test the masternodes RPC.
- verify anchors rewards
"""
from test_framework.test_framework import DefiTestFramework
from test_framework.util import assert_equal, connect_nodes_bi, \
disconnect_nodes, wait_until, assert_raises_rpc_error
from decimal import Decimal
import time
class AnchorRewardsTest (DefiTestFramework):
def set_test_params(self):
self.num_nodes = 3
self.extra_args = [
[ "-dummypos=1", "-spv=1", '-amkheight=0', "-anchorquorum=2", "-dakotaheight=1", "-fortcanningheight=1"],
[ "-dummypos=1", "-spv=1", '-amkheight=0', "-anchorquorum=2", "-dakotaheight=1", "-fortcanningheight=1"],
[ "-dummypos=1", "-spv=1", '-amkheight=0', "-anchorquorum=2", "-dakotaheight=1", "-fortcanningheight=1"],
]
self.setup_clean_chain = True
def mock_time(self, offset_hours, hours = 0):
for i in range(0, self.num_nodes):
self.nodes[i % self.num_nodes].set_mocktime(int((time.time() - offset_hours * 60 * 60) + (hours * 60 * 60)))
self.nodes[i % self.num_nodes].setmocktime(int((time.time() - offset_hours * 60 * 60) + (hours * 60 * 60)))
# Masternodes have to mint blocks in the last 2 weeks to be valid for
# anchor teams, function here mines on all available nodes in turn.
def initmasternodesforanchors(self, offset_hours, blocks):
# Change node time time first.
self.mock_time(offset_hours)
for i in range(0, blocks):
block_count = self.nodes[i % self.num_nodes].getblockcount()
# Make sure that generate successfully ioncrements the chain by one.
while self.nodes[i % self.num_nodes].getblockcount() < block_count + 1:
self.nodes[i % self.num_nodes].generate(1)
# Make sure all nodes agree before creating the next block.
self.sync_blocks()
# Mine up to height.
def mine_diff(self, height):
if self.nodes[0].getblockcount() < height:
self.nodes[0].generate(height - self.nodes[0].getblockcount())
self.sync_blocks()
# Tiem is hours to move node time forward and blocks the number of blocks
# to mine in each hour. Offset allows us to put check clock back.
def rotateandgenerate(self, hours, offset_hours, blocks):
for increment in range(1, hours + 1):
# Change node time time first.
self.mock_time(offset_hours, increment)
block_count = self.nodes[0].getblockcount()
# Make sure that generate successfully ioncrements the chain by blocks
while self.nodes[0].getblockcount() < block_count + blocks:
self.nodes[0].generate(1)
self.sync_blocks()
def setlastheight(self, height):
for i in range(0, self.num_nodes):
self.nodes[int(i % self.num_nodes)].spv_setlastheight(height)
def authsquorum(self, height, node=None):
QUORUM = 2
if node is None:
node = 0
auths = self.nodes[node].spv_listanchorauths()
for auth in auths:
if auth['blockHeight'] == height and auth['signers'] >= QUORUM:
return True
return False
def run_test(self):
assert_equal(len(self.nodes[0].listmasternodes()), 8)
anchorFrequency = 15
# Create multiple active MNs
self.initmasternodesforanchors(13, 1 * anchorFrequency)
wait_until(lambda: len(self.nodes[0].getanchorteams()['auth']) == 3 and len(self.nodes[0].getanchorteams()['confirm']) == 3, timeout=10)
# Mo anchors created yet as we need three hours depth in chain
assert_equal(len(self.nodes[0].spv_listanchorauths()), 0)
# Mine forward 4 hours, from 12 hours ago, 5 blocks an hour
self.rotateandgenerate(3, 12, 5)
# Mine up to block 60
self.mine_diff(60)
# Anchor data
print(self.nodes[0].spv_listanchorauths())
print(self.nodes[0].getblockcount())
wait_until(lambda: len(self.nodes[0].spv_listanchorauths()) > 0 and self.nodes[0].spv_listanchorauths()[0]['signers'] == 3, timeout=10)
auth = self.nodes[0].spv_listanchorauths()
creation_height = auth[0]['creationHeight']
assert_equal(auth[0]['blockHeight'], 15)
hash15 = self.nodes[0].getblockhash(15)
hash_creation = self.nodes[0].getblockhash(creation_height)
block15 = self.nodes[0].getblock(hash15)
block_creation = self.nodes[0].getblock(hash_creation)
# Check the time
time_diff = block_creation['time'] - block15['time']
assert(time_diff > 3 * 60 * 60)
self.nodes[0].spv_setlastheight(1)
self.nodes[1].spv_setlastheight(1)
# Check errors
assert_raises_rpc_error(None, "Not enough money", self.nodes[1].spv_createanchor,
[{
'txid': "a0d5a294be3cde6a8bddab5815b8c4cb1b2ebf2c2b8a4018205d6f8c576e8963",
'vout': 3,
'amount': 1000,
'privkey': "cStbpreCo2P4nbehPXZAAM3gXXY1sAphRfEhj7ADaLx8i2BmxvEP"
}], "mgsE1SqrcfUhvuYuRjqy6rQCKmcCVKNhMu")
# Check some params:
assert_raises_rpc_error(None, "Expected type array, got object", self.nodes[1].spv_createanchor,
{
'txid': "a0d5a294be3cde6a8bddab5815b8c4cb1b2ebf2c2b8a4018205d6f8c576e8963",
'vout': 3,
'amount': 2262303,
'privkey': "cStbpreCo2P4nbehPXZAAM3gXXY1sAphRfEhj7ADaLx8i2BmxvEP"
}, "mgsE1SqrcfUhvuYuRjqy6rQCKmcCVKNhMu")
assert_raises_rpc_error(None, "txid must be of length 64", self.nodes[1].spv_createanchor,
[{
'txid': "a0d5a294be3cde6a8bddab5815b8c4cb1b2ebf2c2b8a4018205d6f8c576e8963aa",
'vout': 3,
'amount': 2262303,
'privkey': "cStbpreCo2P4nbehPXZAAM3gXXY1sAphRfEhj7ADaLx8i2BmxvEP"
}], "mgsE1SqrcfUhvuYuRjqy6rQCKmcCVKNhMu")
assert_raises_rpc_error(None, "value is not an integer", self.nodes[1].spv_createanchor,
[{
'txid': "a0d5a294be3cde6a8bddab5815b8c4cb1b2ebf2c2b8a4018205d6f8c576e8963",
'vout': "aa",
'amount': 2262303,
'privkey': "cStbpreCo2P4nbehPXZAAM3gXXY1sAphRfEhj7ADaLx8i2BmxvEP"
}], "mgsE1SqrcfUhvuYuRjqy6rQCKmcCVKNhMu")
assert_raises_rpc_error(None, "Can't parse WIF privkey", self.nodes[1].spv_createanchor,
[{
'txid': "a0d5a294be3cde6a8bddab5815b8c4cb1b2ebf2c2b8a4018205d6f8c576e8963",
'vout': 3,
'amount': 2262303,
'privkey': "1_cStbpreCo2P4nbehPXZAAM3gXXY1sAphRfEhj7ADaLx8i2BmxvEP"
}], "mgsE1SqrcfUhvuYuRjqy6rQCKmcCVKNhMu")
assert_raises_rpc_error(None, "does not refer to a P2PKH or P2WPKH address", self.nodes[1].spv_createanchor,
[{
'txid': "a0d5a294be3cde6a8bddab5815b8c4cb1b2ebf2c2b8a4018205d6f8c576e8963",
'vout': 3,
'amount': 2262303,
'privkey': "cStbpreCo2P4nbehPXZAAM3gXXY1sAphRfEhj7ADaLx8i2BmxvEP"
}], "__mgsE1SqrcfUhvuYuRjqy6rQCKmcCVKNhMu")
assert_raises_rpc_error(None, "does not refer to a P2PKH or P2WPKH address", self.nodes[1].spv_createanchor,
[{
'txid': "a0d5a294be3cde6a8bddab5815b8c4cb1b2ebf2c2b8a4018205d6f8c576e8963",
'vout': 3,
'amount': 2262303,
'privkey': "cStbpreCo2P4nbehPXZAAM3gXXY1sAphRfEhj7ADaLx8i2BmxvEP"
}], "")
# Test anchor creations
rewardAddress0 = self.nodes[0].getnewaddress("", "legacy")
rewardAddress1 = self.nodes[0].getnewaddress("", "legacy")
txAnc0 = self.nodes[0].spv_createanchor([{
'txid': "a0d5a294be3cde6a8bddab5815b8c4cb1b2ebf2c2b8a4018205d6f8c576e8963",
'vout': 3,
'amount': 2262303,
'privkey': "cStbpreCo2P4nbehPXZAAM3gXXY1sAphRfEhj7ADaLx8i2BmxvEP"}],
rewardAddress0)
txAnc1 = self.nodes[0].spv_createanchor([{
'txid': "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
'vout': 3,
'amount': 2262303,
'privkey': "cStbpreCo2P4nbehPXZAAM3gXXY1sAphRfEhj7ADaLx8i2BmxvEP"}],
rewardAddress1)
self.nodes[1].spv_sendrawtx(txAnc0['txHex'])
self.nodes[1].spv_sendrawtx(txAnc1['txHex'])
# just for triggering activation in regtest
self.nodes[0].spv_setlastheight(1)
self.nodes[1].spv_setlastheight(1)
pending = self.nodes[0].spv_listanchorspending()
assert_equal(len(pending), 2)
assert_equal(pending[0]['btcBlockHeight'], 1)
assert_equal(pending[0]['defiBlockHeight'], 15)
if pending[0]['rewardAddress'] == rewardAddress0:
assert_equal(pending[1]['rewardAddress'], rewardAddress1)
else:
assert_equal(pending[0]['rewardAddress'], rewardAddress1)
assert_equal(pending[0]['confirmations'], 1) # Bitcoin confirmations
assert_equal(pending[0]['signatures'], 2)
assert_equal(pending[0]['anchorCreationHeight'], creation_height)
# Check these are consistent across anchors life
btcHash0 = pending[0]['btcTxHash']
btcHash1 = pending[1]['btcTxHash']
dfiHash = pending[0]['defiBlockHash']
# Trigger anchor check
self.nodes[0].generate(1)
# Get anchors
anchors = self.nodes[0].spv_listanchors()
assert_equal(len(anchors), 2)
assert_equal(anchors[0]['btcBlockHeight'], 1)
if anchors[0]['btcTxHash'] == btcHash0:
assert_equal(anchors[1]['btcTxHash'], btcHash1)
else:
assert_equal(anchors[0]['btcTxHash'], btcHash1)
assert_equal(anchors[0]['previousAnchor'], '0000000000000000000000000000000000000000000000000000000000000000')
assert_equal(anchors[0]['defiBlockHeight'], 15)
assert_equal(anchors[0]['defiBlockHash'], dfiHash)
if anchors[0]['rewardAddress'] == rewardAddress0:
assert_equal(anchors[1]['rewardAddress'], rewardAddress1)
else:
assert_equal(anchors[0]['rewardAddress'], rewardAddress1)
assert_equal(anchors[0]['confirmations'], 1) # Bitcoin confirmations
assert_equal(anchors[0]['signatures'], 2)
assert_equal(anchors[0]['anchorCreationHeight'], creation_height)
assert_equal(anchors[0]['active'], False)
print ("Confs init:")
assert_equal(len(self.nodes[0].spv_listanchorrewardconfirms()), 0)
self.nodes[0].spv_setlastheight(5)
self.nodes[1].spv_setlastheight(5)
assert_equal(len(self.nodes[0].spv_listanchorrewardconfirms()), 0)
# Still no active anchor
anchors = self.nodes[0].spv_listanchors()
assert_equal(anchors[0]['confirmations'], 5) # Bitcoin confirmations
assert_equal(anchors[0]['active'], False)
# important (!) to be synced before disconnection
# disconnect node2 (BEFORE reward voting!) for future rollback
disconnect_nodes(self.nodes[1], 2)
self.nodes[0].spv_setlastheight(6)
self.nodes[1].spv_setlastheight(6)
anchors = self.nodes[0].spv_listanchors()
print(anchors)
assert_equal(anchors[0]['confirmations'], 6) # Bitcoin confirmations
if anchors[0]['active']:
activeAnc = anchors[0]
else:
# Make sure this actually is active
assert_equal(anchors[1]['active'], True)
activeAnc = anchors[1]
unrewarded = self.nodes[0].spv_listanchorsunrewarded()
assert_equal(len(unrewarded), 1)
assert_equal(unrewarded[0]['btcBlockHeight'], 1)
if unrewarded[0]['btcTxHash'] != btcHash0:
assert_equal(unrewarded[0]['btcTxHash'], btcHash1)
assert_equal(unrewarded[0]['defiBlockHeight'], 15)
assert_equal(unrewarded[0]['defiBlockHash'], dfiHash)
# important to wait here!
self.sync_blocks(self.nodes[0:2])
wait_until(lambda: len(self.nodes[0].spv_listanchorrewardconfirms()) == 1 and self.nodes[0].spv_listanchorrewardconfirms()[0]['signers'] == 2, timeout=10)
conf0 = self.nodes[0].spv_listanchorrewardconfirms()
print ("Confs created, only active anchor")
assert_equal(len(conf0), 1)
assert_equal(conf0[0]['anchorHeight'], 15)
assert_equal(conf0[0]['prevAnchorHeight'], 0)
assert_equal(conf0[0]['rewardAddress'], activeAnc['rewardAddress'])
assert_equal(conf0[0]['signers'], 2)
print ("Generate reward")
assert_equal(len(self.nodes[0].spv_listanchorrewards()), 0)
# Reward before
assert_equal(self.nodes[0].listcommunitybalances()['AnchorReward'], Decimal('6.10000000'))
self.nodes[0].generate(1)
# Reward after
assert_equal(self.nodes[0].listcommunitybalances()['AnchorReward'], Decimal('0.10000000'))
# confirms should disappear
wait_until(lambda: len(self.nodes[0].spv_listanchorrewardconfirms()) == 0, timeout=10)
# check reward tx
rew0 = self.nodes[0].spv_listanchorrewards()
assert_equal(len(rew0), 1)
assert_equal(rew0[0]['AnchorTxHash'], conf0[0]['btcTxHash'])
blockcount = self.nodes[0].getblockcount()
blockhash = self.nodes[0].getblockhash(blockcount)
rew0tx = self.nodes[0].getrawtransaction(rew0[0]['RewardTxHash'], 1, blockhash)
assert_equal(rew0tx['vout'][1]['scriptPubKey']['addresses'][0], conf0[0]['rewardAddress'])
assert_equal(rew0tx['vout'][1]['value'], Decimal('6.10000000'))
# Check data from list transactions
anchors = self.nodes[0].listanchors()
assert_equal(anchors[0]['anchorHeight'], 15)
assert_equal(anchors[0]['anchorHash'], dfiHash)
assert_equal(anchors[0]['rewardAddress'], conf0[0]['rewardAddress'])
assert_equal(anchors[0]['dfiRewardHash'], rew0tx['txid'])
assert_equal(anchors[0]['btcAnchorHeight'], 1)
if anchors[0]['btcAnchorHash'] != btcHash0:
assert_equal(anchors[0]['btcAnchorHash'], btcHash1)
print ("Rollback!")
self.nodes[2].generate(2)
connect_nodes_bi(self.nodes, 1, 2)
self.sync_blocks()
wait_until(lambda: len(self.nodes[0].spv_listanchorrewardconfirms()) == 1, timeout=10) # while rollback, it should appear w/o wait
assert_equal(len(self.nodes[0].spv_listanchorrewards()), 0)
wait_until(lambda: len(self.nodes[2].spv_listanchorrewardconfirms()) == 1, timeout=10) # but wait here
assert_equal(len(self.nodes[2].spv_listanchorrewards()), 0)
print ("Reward again")
# Reward before
assert_equal(self.nodes[0].listcommunitybalances()['AnchorReward'], Decimal('6.30000000')) # 2 more blocks on this chain
self.nodes[1].generate(1)
self.sync_blocks()
# Reward after
assert_equal(self.nodes[0].listcommunitybalances()['AnchorReward'], Decimal('0.10000000'))
wait_until(lambda: len(self.nodes[0].spv_listanchorrewardconfirms()) == 0, timeout=10)
assert_equal(len(self.nodes[0].spv_listanchorrewards()), 1)
print ("Generate another reward")
self.setlastheight(6)
# Mine forward 6 hours, from 9 hours ago, 5 blocks an hour
self.rotateandgenerate(6, 9, 5)
wait_until(lambda: self.authsquorum(60), timeout=10)
rewardAddress2 = self.nodes[0].getnewaddress("", "legacy")
txAnc2 = self.nodes[0].spv_createanchor([{
'txid': "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
'vout': 3,
'amount': 2262303,
'privkey': "cStbpreCo2P4nbehPXZAAM3gXXY1sAphRfEhj7ADaLx8i2BmxvEP"}],
rewardAddress2)
self.nodes[1].spv_sendrawtx(txAnc2['txHex'])
self.nodes[0].spv_setlastheight(7)
self.nodes[1].spv_setlastheight(7)
# Mine forward 3 hours, from 9 hours ago, 5 blocks an hour
self.rotateandgenerate(3, 3, 15)
wait_until(lambda: self.authsquorum(60), timeout=10)
# for rollback. HERE, to deny cofirmations for node2
disconnect_nodes(self.nodes[1], 2)
self.nodes[0].spv_setlastheight(13)
self.nodes[1].spv_setlastheight(13)
# important to wait here!
self.sync_blocks(self.nodes[0:2])
wait_until(lambda: len(self.nodes[0].spv_listanchorrewardconfirms()) == 1 and self.nodes[0].spv_listanchorrewardconfirms()[0]['signers'] == 2, timeout=10)
# check confirmations (revoting) after node restart:
self.stop_node(0)
self.start_node(0, ['-txindex=1', '-amkheight=0', "-dakotaheight=1"])
connect_nodes_bi(self.nodes, 0, 1)
self.sync_blocks(self.nodes[0:2])
wait_until(lambda: len(self.nodes[0].spv_listanchorrewardconfirms()) == 1 and self.nodes[0].spv_listanchorrewardconfirms()[0]['signers'] == 2, timeout=10)
# Reward before
assert_equal(self.nodes[1].listcommunitybalances()['AnchorReward'], Decimal('7.60000000')) # 2 more blocks on this chain
self.nodes[1].generate(1)
block_height = self.nodes[1].getblockcount()
block_hash = self.nodes[1].getblockhash(block_height)
# Reward after
assert_equal(self.nodes[1].listcommunitybalances()['AnchorReward'], Decimal('0.10000000')) # Subsidy halving!
self.sync_blocks(self.nodes[0:2])
wait_until(lambda: len(self.nodes[0].spv_listanchorrewardconfirms()) == 0, timeout=10)
assert_equal(len(self.nodes[0].spv_listanchorrewards()), 2)
rew = self.nodes[0].spv_listanchorrewards()
for i in rew:
if i['AnchorTxHash'] == txAnc2['txHash']:
rew2Hash = i['RewardTxHash']
rew2tx = self.nodes[0].getrawtransaction(rew2Hash, 1, block_hash)
assert_equal(rew2tx['vout'][1]['scriptPubKey']['addresses'][0], rewardAddress2)
assert_equal(rew2tx['vout'][1]['value'], Decimal('7.60000000'))
print ("Rollback a rewards")
self.nodes[2].generate(3)
connect_nodes_bi(self.nodes, 1, 2)
self.sync_blocks()
wait_until(lambda: len(self.nodes[0].spv_listanchorrewardconfirms()) == 1, timeout=10)
assert_equal(len(self.nodes[0].spv_listanchorrewards()), 1)
if __name__ == '__main__':
AnchorRewardsTest().main()