Skip to content

Commit 025d285

Browse files
committed
[Tests] Add Wrapping Serials Test
1 parent 7373704 commit 025d285

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
'p2p_pos_fakestake_accepted.py',
6161
'p2p_zpos_fakestake.py',
6262
'p2p_zpos_fakestake_accepted.py',
63+
'zerocoin_wrapped_serials.py',
6364
# vv Tests less than 5m vv
6465
'feature_block.py',
6566
'rpc_fundrawtransaction.py',
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2019 The PIVX Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
'''
7+
Covers the 'Wrapped Serials Attack' scenario
8+
'''
9+
10+
from time import sleep
11+
12+
from test_framework.authproxy import JSONRPCException
13+
from test_framework.util import assert_equal, assert_greater_than
14+
15+
from fake_stake.base_test import PIVX_FakeStakeTest
16+
17+
class zPIVwrappedSerialsTest(PIVX_FakeStakeTest):
18+
19+
def run_test(self):
20+
q = 73829871667027927151400291810255409637272593023945445234219354687881008052707
21+
pow2 = 2**256
22+
self.description = "Covers the 'Wrapped Serials Attack' scenario."
23+
self.init_test()
24+
25+
INITAL_MINED_BLOCKS = 351 # Blocks mined before minting
26+
MORE_MINED_BLOCKS = 31 # Blocks mined after minting (before spending)
27+
DENOM_TO_USE = 1000 # zc denomination used for double spending attack
28+
29+
# 1) Start mining blocks
30+
self.log.info("Mining %d first blocks..." % INITAL_MINED_BLOCKS)
31+
self.node.generate(INITAL_MINED_BLOCKS)
32+
sleep(2)
33+
34+
# 2) Mint zerocoins
35+
self.log.info("Minting %d-denom zPIVs..." % DENOM_TO_USE)
36+
balance = self.node.getbalance("*", 100)
37+
assert_greater_than(balance, DENOM_TO_USE)
38+
total_mints = 0
39+
while balance > DENOM_TO_USE:
40+
try:
41+
self.node.mintzerocoin(DENOM_TO_USE)
42+
except JSONRPCException:
43+
break
44+
sleep(1)
45+
total_mints += 1
46+
self.node.generate(1)
47+
sleep(1)
48+
if total_mints % 5 == 0:
49+
self.log.info("Minted %d coins" % total_mints)
50+
if total_mints >= 20:
51+
break
52+
balance = self.node.getbalance("*", 100)
53+
sleep(2)
54+
55+
# 3) Mine more blocks and collect the mint
56+
self.log.info("Mining %d more blocks..." % MORE_MINED_BLOCKS)
57+
self.node.generate(MORE_MINED_BLOCKS)
58+
sleep(2)
59+
mint = self.node.listmintedzerocoins(True, True)[0]
60+
61+
# 4) Get the raw zerocoin data
62+
exported_zerocoins = self.node.exportzerocoins(False)
63+
zc = [x for x in exported_zerocoins if mint["serial hash"] == x["id"]]
64+
if len(zc) == 0:
65+
raise AssertionError("mint not found")
66+
67+
# 5) Spend the minted coin (mine two more blocks)
68+
self.log.info("Spending the minted coin with serial %s and mining two more blocks..." % zc[0]["s"])
69+
txid = self.node.spendzerocoinmints([mint["serial hash"]])['txid']
70+
self.log.info("Spent on tx %s" % txid)
71+
self.node.generate(2)
72+
sleep(2)
73+
74+
# 6) create the new serial
75+
serial = hex(int(zc[0]["s"], 16) + q*pow2)[2:]
76+
randomness = zc[0]["r"]
77+
privkey = zc[0]["k"]
78+
79+
# 7) Spend the new zerocoin
80+
self.log.info("Spending the wrapping serial %s" % serial)
81+
tx = None
82+
try:
83+
tx = self.node.spendrawzerocoin(serial, randomness, DENOM_TO_USE, privkey)
84+
except JSONRPCException as e:
85+
exc_msg = str(e)
86+
if exc_msg == "The new spend coin transaction did not verify (-4)":
87+
self.log.info("GOOD: Transaction did not verify")
88+
else:
89+
raise e
90+
91+
# 8) Check
92+
if tx is not None:
93+
self.log.warning("Tx is: %s" % tx)
94+
raise AssertionError("TEST FAILED")
95+
96+
self.log.info("%s PASSED" % self.__class__.__name__)
97+
98+
99+
100+
if __name__ == '__main__':
101+
zPIVwrappedSerialsTest().main()

0 commit comments

Comments
 (0)