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+ '''
0 commit comments