Skip to content

Commit ed63600

Browse files
random-zebrafurszy
authored andcommitted
PIVX_fake_stake: zerocoin tests
1 parent 07b981a commit ed63600

File tree

5 files changed

+128
-8
lines changed

5 files changed

+128
-8
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
import os
4+
import sys
5+
sys.path.append(os.path.abspath(__file__))
6+
sys.path.append(os.path.abspath(".."))
7+
8+
from test01 import Test_01
9+
from test02 import Test_02
10+
from test03 import Test_03
11+
12+
if __name__ == '__main__':
13+
#Test_01().main()
14+
#Test_02().main()
15+
Test_03().main()

test/functional/fake_stake/test01.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import time
55

66
from test_framework.messages import msg_block
7+
from test_framework.util import bytes_to_hex_str
78

89
from base_test import PIVX_FakeStakeTest
9-
from util import utxos_to_stakingPrevOuts, dir_size
10+
from util import utxos_to_stakingPrevOuts, dir_size, assert_not_equal
1011

1112
class Test_01(PIVX_FakeStakeTest):
1213

@@ -15,7 +16,7 @@ def run_test(self):
1516

1617
FORK_DEPTH = 20 # Depth at which we are creating a fork. We are mining
1718
INITAL_MINED_BLOCKS = 150
18-
self.NUM_BLOCKS = 15
19+
self.NUM_BLOCKS = 7
1920

2021
# 1) Starting mining blocks
2122
self.log.info("Mining %d blocks.." % INITAL_MINED_BLOCKS)
@@ -41,13 +42,13 @@ def run_test(self):
4142
stakingPrevOuts = utxos_to_stakingPrevOuts(utxo_list, tx_block_time)
4243
time.sleep(1)
4344

44-
# 3) Start mining again so that spent prevouts get confirmted in a block.
45+
# 5) Start mining again so that spent prevouts get confirmted in a block.
4546
self.log.info("Mining 5 more blocks...")
4647
self.node.generate(5)
4748
self.log.info("Sleeping 2 sec. Now mining PoS blocks based on already spent transactions...")
4849
time.sleep(2)
4950

50-
# 4) Create "Fake Stake" blocks and send them
51+
# 6) Create "Fake Stake" blocks and send them
5152
init_size = dir_size(self.node.datadir + "/regtest/blocks")
5253
self.log.info("Initial size of data dir: %s kilobytes" % str(init_size))
5354

@@ -65,6 +66,7 @@ def run_test(self):
6566
msg = msg_block(block)
6667
self.log.info("Sending block (size: %.2f Kbytes)...", len(block.serialize())/1000)
6768
self.test_nodes[0].send_message(msg)
69+
#assert_not_equal(self.node.submitblock(bytes_to_hex_str(block.serialize())), None)
6870

6971

7072
self.log.info("Sent all %s blocks." % str(self.NUM_BLOCKS))

test/functional/fake_stake/test02.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
import time
55

66
from test_framework.messages import msg_block
7+
from test_framework.util import bytes_to_hex_str,assert_equal
78

89
from base_test import PIVX_FakeStakeTest
9-
10-
from test_framework.util import bytes_to_hex_str,assert_equal
1110
from util import utxos_to_stakingPrevOuts, dir_size
1211

1312
class Test_02(PIVX_FakeStakeTest):
@@ -16,7 +15,7 @@ def run_test(self):
1615
self.init_test()
1716
INITAL_MINED_BLOCKS = 950
1817
MORE_MINED_BLOCKS = 50
19-
self.NUM_BLOCKS = 3
18+
self.NUM_BLOCKS = 10
2019

2120
# 1) Starting mining blocks
2221
self.log.info("Mining %d blocks.." % INITAL_MINED_BLOCKS)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from random import randint
4+
import time
5+
6+
from test_framework.messages import msg_block
7+
from test_framework.authproxy import JSONRPCException
8+
9+
from base_test import PIVX_FakeStakeTest
10+
from util import utxos_to_stakingPrevOuts, dir_size
11+
12+
class Test_03(PIVX_FakeStakeTest):
13+
14+
def run_test(self):
15+
self.init_test()
16+
17+
FORK_DEPTH = 20 # Depth at which we are creating a fork. We are mining
18+
INITAL_MINED_BLOCKS = 1101
19+
self.NUM_BLOCKS = 15
20+
21+
# 1) Starting mining blocks
22+
self.log.info("Mining %d blocks to get to zPOS activation...." % INITAL_MINED_BLOCKS)
23+
self.node.generate(INITAL_MINED_BLOCKS)
24+
time.sleep(2)
25+
26+
# 2) Collect the possible prevouts and mint zerocoins with those
27+
self.log.info("Collecting all unspent coins which we generated from mining...")
28+
balance = self.node.getbalance()
29+
self.log.info("Minting zerocoins...")
30+
initial_mints = 0
31+
while balance > 5000:
32+
try:
33+
self.node.mintzerocoin(5000)
34+
except JSONRPCException:
35+
break
36+
time.sleep(1)
37+
initial_mints += 1
38+
self.node.generate(1)
39+
time.sleep(1)
40+
balance = self.node.getbalance()
41+
self.log.info("Minted %d coins in the 5000-denom..." % initial_mints)
42+
time.sleep(2)
43+
44+
# 3) mine 10 blocks
45+
self.log.info("Mining 10 blocks ... and getting spendable zerocoins")
46+
self.node.generate(10)
47+
mints_list = [x["serial hash"] for x in self.node.listmintedzerocoins(True, True)]
48+
self.log.info("Got %d spendable (confirmed & mature) mints" % len(mints_list))
49+
50+
# 4) spend mints
51+
self.log.info("Spending mints...")
52+
spends = 0
53+
for mint in mints_list:
54+
mint_arg = []
55+
mint_arg.append(mint)
56+
try:
57+
self.node.spendzerocoinmints(mint_arg)
58+
time.sleep(1)
59+
spends += 1
60+
except JSONRPCException as e:
61+
self.log.warning(str(e))
62+
continue
63+
time.sleep(1)
64+
self.log.info("Successfully spent %d mints" % spends)
65+
66+
# 5) Start mining again so that spends get confirmted in a block.
67+
self.log.info("Mining 5 more blocks...")
68+
self.node.generate(5)
69+
self.log.info("Sleeping 2 sec. Now mining PoS blocks based on already spent transactions...")
70+
time.sleep(2)
71+
72+
'''
73+
# 4) Create "Fake Stake" blocks and send them
74+
init_size = dir_size(self.node.datadir + "/regtest/blocks")
75+
self.log.info("Initial size of data dir: %s kilobytes" % str(init_size))
76+
77+
for i in range(0, self.NUM_BLOCKS):
78+
if i != 0 and i % 5 == 0:
79+
self.log.info("Sent %s blocks out of %s" % (str(i), str(self.NUM_BLOCKS)))
80+
81+
# Create the spam block
82+
block_count = self.node.getblockcount()
83+
randomCount = randint(block_count-FORK_DEPTH-1, block_count)
84+
pastBlockHash = self.node.getblockhash(randomCount)
85+
block = self.create_spam_block(pastBlockHash, stakingPrevOuts, randomCount+1)
86+
timeStamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(block.nTime))
87+
self.log.info("Created PoS block with nTime %s: %s", timeStamp, block.hash)
88+
msg = msg_block(block)
89+
self.log.info("Sending block (size: %.2f Kbytes)...", len(block.serialize())/1000)
90+
self.test_nodes[0].send_message(msg)
91+
92+
93+
self.log.info("Sent all %s blocks." % str(self.NUM_BLOCKS))
94+
self.stop_node(0)
95+
time.sleep(5)
96+
97+
final_size = dir_size(self.node.datadir + "/regtest/blocks")
98+
self.log.info("Final size of data dir: %s kilobytes" % str(final_size))
99+
'''

test/functional/fake_stake/util.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,9 @@ def utxos_to_stakingPrevOuts(utxolist, blocktime):
126126
outPoint = COutPoint(int(utxo['txid'], 16), utxo['vout'])
127127
stakingPrevOuts[outPoint] = (int(utxo['amount'])*COIN, blocktime)
128128

129-
return stakingPrevOuts
129+
return stakingPrevOuts
130+
131+
132+
def assert_not_equal(thing1, thing2):
133+
if thing1 == thing2:
134+
raise AssertionError("not(%s)" % " != ".join(str(arg) for arg in (thing1, thing2)))

0 commit comments

Comments
 (0)