|
| 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