def run_test(self):

    def get_zerocoin_data(coin):
        return (coin['s'], coin['r'], coin['k'], coin['id'], coin['d'], coin['t'])

    def check_balances(denom, zpny_bal, pny_bal):
        zpny_bal -= denom
        assert_equal(self.nodes[2].getzerocoinbalance()['Total'], zpny_bal)
        pny_bal += denom
        wi = self.nodes[2].getwalletinfo()
        assert_equal((wi['balance'] + wi['immature_balance']), pny_bal)
        return (zpny_bal, pny_bal)

    def stake_4_blocks(block_time):
        for peer in range(2):
            for i in range(2):
                block_time = self.generate_pos(peer, block_time)
            sync_blocks(self.nodes)
        return block_time
    self.log_title()
    block_time = self.mocktime
    set_node_times(self.nodes, block_time)
    wi = self.nodes[2].getwalletinfo()
    balance = (wi['balance'] + wi['immature_balance'])
    zpny_balance = self.nodes[2].getzerocoinbalance()['Total']
    assert_equal(balance, DecimalAmt(13833.92))
    assert_equal(zpny_balance, 6666)
    listmints = self.nodes[2].listmintedzerocoins(True, True)
    serial_ids = [mint['serial hash'] for mint in listmints]
    exported_zerocoins = [x for x in self.nodes[2].exportzerocoins(False) if (x['id'] in serial_ids)]
    exported_zerocoins.sort(key=(lambda x: x['d']), reverse=False)
    assert_equal(8, len(exported_zerocoins))
    (serial_1, randomness_1, privkey_1, id_1, denom_1, tx_1) = get_zerocoin_data(exported_zerocoins[1])
    self.log.info('Staking 70 blocks to get to public spend activation')
    for j in range(5):
        for peer in range(2):
            for i in range(7):
                block_time = self.generate_pos(peer, block_time)
            sync_blocks(self.nodes)
    old_spend_v3 = self.nodes[2].createrawzerocoinspend(id_1)
    (serial_2, randomness_2, privkey_2, id_2, denom_2, tx_2) = get_zerocoin_data(exported_zerocoins[2])
    self.log.info(('Spending the minted coin with serial %s...' % serial_2[:16]))
    txid = self.nodes[2].spendzerocoinmints([id_2])['txid']
    block_time = stake_4_blocks(block_time)
    self.check_tx_in_chain(0, txid)
    (zpny_balance, balance) = check_balances(denom_2, zpny_balance, balance)
    self.log.info('--> VALID PUBLIC COIN SPEND (v3) PASSED')
    self.log.info('Trying to spend the serial twice now...')
    assert_raises_rpc_error((- 4), 'Trying to spend an already spent serial', self.nodes[2].spendrawzerocoin, serial_2, randomness_2, denom_2, privkey_2, '', tx_2)
    self.setV4SpendEnforcement()
    (serial_3, randomness_3, privkey_3, id_3, denom_3, tx_3) = get_zerocoin_data(exported_zerocoins[3])
    self.log.info(('Spending the minted coin with serial %s...' % serial_3[:16]))
    txid = self.nodes[2].spendzerocoinmints([id_3])['txid']
    block_time = stake_4_blocks(block_time)
    self.check_tx_in_chain(0, txid)
    (zpny_balance, balance) = check_balances(denom_3, zpny_balance, balance)
    self.log.info('--> VALID PUBLIC COIN SPEND (v4) PASSED')
    self.log.info('Trying to spend the serial twice now...')
    assert_raises_rpc_error((- 4), 'Trying to spend an already spent serial', self.nodes[2].spendrawzerocoin, serial_3, randomness_3, denom_3, privkey_3, '', tx_3)
    self.log.info('Trying to send old v3 spend now...')
    assert_raises_rpc_error((- 26), 'bad-txns-invalid-zpny', self.nodes[2].sendrawtransaction, old_spend_v3)
    self.log.info('GOOD: Old transaction not sent.')
    self.log.info('Trying to double spend v4 against v3...')
    assert_raises_rpc_error((- 4), 'Trying to spend an already spent serial', self.nodes[2].spendrawzerocoin, serial_2, randomness_2, denom_2, privkey_2, '', tx_2)
    self.log.info('GOOD: Double-spending transaction did not verify.')
    self.setV4SpendEnforcement(False)
    self.log.info(('Trying to send old v3 spend now (serial: %s...)' % serial_1[:16]))
    txid = self.nodes[2].sendrawtransaction(old_spend_v3)
    _ = stake_4_blocks(block_time)
    self.check_tx_in_chain(0, txid)
    self.nodes[2].resetmintzerocoin()
    (_, _) = check_balances(denom_1, zpny_balance, balance)
    self.log.info('--> VALID PUBLIC COIN SPEND (v3) PASSED')
def run_test(self):

    def get_zerocoin_data(coin):
        return (coin['s'], coin['r'], coin['k'], coin['id'], coin['d'], coin['t'])

    def check_balances(denom, zpny_bal, pny_bal):
        zpny_bal -= denom
        assert_equal(self.nodes[2].getzerocoinbalance()['Total'], zpny_bal)
        pny_bal += denom
        wi = self.nodes[2].getwalletinfo()
        assert_equal((wi['balance'] + wi['immature_balance']), pny_bal)
        return (zpny_bal, pny_bal)

    def stake_4_blocks(block_time):
        for peer in range(2):
            for i in range(2):
                block_time = self.generate_pos(peer, block_time)
            sync_blocks(self.nodes)
        return block_time
    self.log_title()
    block_time = self.mocktime
    set_node_times(self.nodes, block_time)
    wi = self.nodes[2].getwalletinfo()
    balance = (wi['balance'] + wi['immature_balance'])
    zpny_balance = self.nodes[2].getzerocoinbalance()['Total']
    assert_equal(balance, DecimalAmt(13833.92))
    assert_equal(zpny_balance, 6666)
    listmints = self.nodes[2].listmintedzerocoins(True, True)
    serial_ids = [mint['serial hash'] for mint in listmints]
    exported_zerocoins = [x for x in self.nodes[2].exportzerocoins(False) if (x['id'] in serial_ids)]
    exported_zerocoins.sort(key=(lambda x: x['d']), reverse=False)
    assert_equal(8, len(exported_zerocoins))
    (serial_1, randomness_1, privkey_1, id_1, denom_1, tx_1) = get_zerocoin_data(exported_zerocoins[1])
    self.log.info('Staking 70 blocks to get to public spend activation')
    for j in range(5):
        for peer in range(2):
            for i in range(7):
                block_time = self.generate_pos(peer, block_time)
            sync_blocks(self.nodes)
    old_spend_v3 = self.nodes[2].createrawzerocoinspend(id_1)
    (serial_2, randomness_2, privkey_2, id_2, denom_2, tx_2) = get_zerocoin_data(exported_zerocoins[2])
    self.log.info(('Spending the minted coin with serial %s...' % serial_2[:16]))
    txid = self.nodes[2].spendzerocoinmints([id_2])['txid']
    block_time = stake_4_blocks(block_time)
    self.check_tx_in_chain(0, txid)
    (zpny_balance, balance) = check_balances(denom_2, zpny_balance, balance)
    self.log.info('--> VALID PUBLIC COIN SPEND (v3) PASSED')
    self.log.info('Trying to spend the serial twice now...')
    assert_raises_rpc_error((- 4), 'Trying to spend an already spent serial', self.nodes[2].spendrawzerocoin, serial_2, randomness_2, denom_2, privkey_2, , tx_2)
    self.setV4SpendEnforcement()
    (serial_3, randomness_3, privkey_3, id_3, denom_3, tx_3) = get_zerocoin_data(exported_zerocoins[3])
    self.log.info(('Spending the minted coin with serial %s...' % serial_3[:16]))
    txid = self.nodes[2].spendzerocoinmints([id_3])['txid']
    block_time = stake_4_blocks(block_time)
    self.check_tx_in_chain(0, txid)
    (zpny_balance, balance) = check_balances(denom_3, zpny_balance, balance)
    self.log.info('--> VALID PUBLIC COIN SPEND (v4) PASSED')
    self.log.info('Trying to spend the serial twice now...')
    assert_raises_rpc_error((- 4), 'Trying to spend an already spent serial', self.nodes[2].spendrawzerocoin, serial_3, randomness_3, denom_3, privkey_3, , tx_3)
    self.log.info('Trying to send old v3 spend now...')
    assert_raises_rpc_error((- 26), 'bad-txns-invalid-zpny', self.nodes[2].sendrawtransaction, old_spend_v3)
    self.log.info('GOOD: Old transaction not sent.')
    self.log.info('Trying to double spend v4 against v3...')
    assert_raises_rpc_error((- 4), 'Trying to spend an already spent serial', self.nodes[2].spendrawzerocoin, serial_2, randomness_2, denom_2, privkey_2, , tx_2)
    self.log.info('GOOD: Double-spending transaction did not verify.')
    self.setV4SpendEnforcement(False)
    self.log.info(('Trying to send old v3 spend now (serial: %s...)' % serial_1[:16]))
    txid = self.nodes[2].sendrawtransaction(old_spend_v3)
    _ = stake_4_blocks(block_time)
    self.check_tx_in_chain(0, txid)
    self.nodes[2].resetmintzerocoin()
    (_, _) = check_balances(denom_1, zpny_balance, balance)
    self.log.info('--> VALID PUBLIC COIN SPEND (v3) PASSED')