def run_test(self):
    starting_balance = (25 * INITIAL_BLOCK_REWARD)
    for n in self.nodes:
        assert (n.getblockchaininfo()['initialblockdownload'] == False)
    for i in range(4):
        assert_equal(self.nodes[i].getbalance(), starting_balance)
        self.nodes[i].getnewaddress('')
    spend_from_foo = (starting_balance - (INITIAL_BLOCK_REWARD * 5))
    spend_from_bar = ((INITIAL_BLOCK_REWARD * 5) - 100)
    spend_from_doublespend = ((spend_from_foo + spend_from_bar) - 8)
    node0_address_foo = self.nodes[0].getnewaddress()
    fund_foo_txid = self.nodes[0].sendtoaddress(node0_address_foo, spend_from_foo)
    fund_foo_tx = self.nodes[0].gettransaction(fund_foo_txid)
    node0_address_bar = self.nodes[0].getnewaddress()
    fund_bar_txid = self.nodes[0].sendtoaddress(node0_address_bar, spend_from_bar)
    fund_bar_tx = self.nodes[0].gettransaction(fund_bar_txid)
    assert_equal(self.nodes[0].getbalance(), ((starting_balance + fund_foo_tx['fee']) + fund_bar_tx['fee']))
    node1_address = self.nodes[1].getnewaddress()
    doublespend_fee = Decimal('-.02')
    rawtx_input_0 = {}
    rawtx_input_0['txid'] = fund_foo_txid
    rawtx_input_0['vout'] = find_output(self.nodes[0], fund_foo_txid, spend_from_foo)
    rawtx_input_1 = {}
    rawtx_input_1['txid'] = fund_bar_txid
    rawtx_input_1['vout'] = find_output(self.nodes[0], fund_bar_txid, spend_from_bar)
    inputs = [rawtx_input_0, rawtx_input_1]
    change_address = self.nodes[0].getnewaddress()
    outputs = {}
    outputs[node1_address] = spend_from_doublespend
    outputs[change_address] = (((spend_from_foo + spend_from_bar) - spend_from_doublespend) + doublespend_fee)
    rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
    doublespend = self.nodes[0].signrawtransactionwithwallet(rawtx)
    assert_equal(doublespend['complete'], True)
    txid1 = self.nodes[0].sendtoaddress(node1_address, ((INITIAL_BLOCK_REWARD / 5) * 4))
    txid2 = self.nodes[0].sendtoaddress(node1_address, ((INITIAL_BLOCK_REWARD / 5) * 2))
    if self.options.mine_block:
        self.nodes[0].generate(1)
        sync_blocks(self.nodes[0:2])
    tx1 = self.nodes[0].gettransaction(txid1)
    tx2 = self.nodes[0].gettransaction(txid2)
    expected = ((starting_balance + fund_foo_tx['fee']) + fund_bar_tx['fee'])
    if self.options.mine_block:
        expected += INITIAL_BLOCK_REWARD
    expected += (tx1['amount'] + tx1['fee'])
    expected += (tx2['amount'] + tx2['fee'])
    assert_equal(self.nodes[0].getbalance(), expected)
    if self.options.mine_block:
        assert_equal(tx1['confirmations'], 1)
        assert_equal(tx2['confirmations'], 1)
        assert_equal(self.nodes[1].getbalance(), (starting_balance - (tx1['amount'] + tx2['amount'])))
    else:
        assert_equal(tx1['confirmations'], 0)
        assert_equal(tx2['confirmations'], 0)
    self.nodes[2].sendrawtransaction(fund_foo_tx['hex'])
    self.nodes[2].sendrawtransaction(fund_bar_tx['hex'])
    doublespend_txid = self.nodes[2].sendrawtransaction(doublespend['hex'])
    self.nodes[2].generate(1)
    connect_nodes(self.nodes[1], 2)
    self.nodes[2].generate(1)
    sync_blocks(self.nodes)
    assert_equal(self.nodes[0].gettransaction(doublespend_txid)['confirmations'], 2)
    tx1 = self.nodes[0].gettransaction(txid1)
    tx2 = self.nodes[0].gettransaction(txid2)
    assert_equal(tx1['confirmations'], (- 2))
    assert_equal(tx2['confirmations'], (- 2))
    expected = (((((starting_balance + (2 * INITIAL_BLOCK_REWARD)) - spend_from_doublespend) + fund_foo_tx['fee']) + fund_bar_tx['fee']) + doublespend_fee)
    assert_equal(self.nodes[0].getbalance(), expected)
    assert_equal(self.nodes[0].getbalance('*'), expected)
    assert_equal(self.nodes[0].getbalance(), (((((starting_balance - spend_from_doublespend) + (2 * INITIAL_BLOCK_REWARD)) + fund_foo_tx['fee']) + fund_bar_tx['fee']) + doublespend_fee))
def run_test(self):
    starting_balance = (25 * INITIAL_BLOCK_REWARD)
    for n in self.nodes:
        assert (n.getblockchaininfo()['initialblockdownload'] == False)
    for i in range(4):
        assert_equal(self.nodes[i].getbalance(), starting_balance)
        self.nodes[i].getnewaddress()
    spend_from_foo = (starting_balance - (INITIAL_BLOCK_REWARD * 5))
    spend_from_bar = ((INITIAL_BLOCK_REWARD * 5) - 100)
    spend_from_doublespend = ((spend_from_foo + spend_from_bar) - 8)
    node0_address_foo = self.nodes[0].getnewaddress()
    fund_foo_txid = self.nodes[0].sendtoaddress(node0_address_foo, spend_from_foo)
    fund_foo_tx = self.nodes[0].gettransaction(fund_foo_txid)
    node0_address_bar = self.nodes[0].getnewaddress()
    fund_bar_txid = self.nodes[0].sendtoaddress(node0_address_bar, spend_from_bar)
    fund_bar_tx = self.nodes[0].gettransaction(fund_bar_txid)
    assert_equal(self.nodes[0].getbalance(), ((starting_balance + fund_foo_tx['fee']) + fund_bar_tx['fee']))
    node1_address = self.nodes[1].getnewaddress()
    doublespend_fee = Decimal('-.02')
    rawtx_input_0 = {}
    rawtx_input_0['txid'] = fund_foo_txid
    rawtx_input_0['vout'] = find_output(self.nodes[0], fund_foo_txid, spend_from_foo)
    rawtx_input_1 = {}
    rawtx_input_1['txid'] = fund_bar_txid
    rawtx_input_1['vout'] = find_output(self.nodes[0], fund_bar_txid, spend_from_bar)
    inputs = [rawtx_input_0, rawtx_input_1]
    change_address = self.nodes[0].getnewaddress()
    outputs = {}
    outputs[node1_address] = spend_from_doublespend
    outputs[change_address] = (((spend_from_foo + spend_from_bar) - spend_from_doublespend) + doublespend_fee)
    rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
    doublespend = self.nodes[0].signrawtransactionwithwallet(rawtx)
    assert_equal(doublespend['complete'], True)
    txid1 = self.nodes[0].sendtoaddress(node1_address, ((INITIAL_BLOCK_REWARD / 5) * 4))
    txid2 = self.nodes[0].sendtoaddress(node1_address, ((INITIAL_BLOCK_REWARD / 5) * 2))
    if self.options.mine_block:
        self.nodes[0].generate(1)
        sync_blocks(self.nodes[0:2])
    tx1 = self.nodes[0].gettransaction(txid1)
    tx2 = self.nodes[0].gettransaction(txid2)
    expected = ((starting_balance + fund_foo_tx['fee']) + fund_bar_tx['fee'])
    if self.options.mine_block:
        expected += INITIAL_BLOCK_REWARD
    expected += (tx1['amount'] + tx1['fee'])
    expected += (tx2['amount'] + tx2['fee'])
    assert_equal(self.nodes[0].getbalance(), expected)
    if self.options.mine_block:
        assert_equal(tx1['confirmations'], 1)
        assert_equal(tx2['confirmations'], 1)
        assert_equal(self.nodes[1].getbalance(), (starting_balance - (tx1['amount'] + tx2['amount'])))
    else:
        assert_equal(tx1['confirmations'], 0)
        assert_equal(tx2['confirmations'], 0)
    self.nodes[2].sendrawtransaction(fund_foo_tx['hex'])
    self.nodes[2].sendrawtransaction(fund_bar_tx['hex'])
    doublespend_txid = self.nodes[2].sendrawtransaction(doublespend['hex'])
    self.nodes[2].generate(1)
    connect_nodes(self.nodes[1], 2)
    self.nodes[2].generate(1)
    sync_blocks(self.nodes)
    assert_equal(self.nodes[0].gettransaction(doublespend_txid)['confirmations'], 2)
    tx1 = self.nodes[0].gettransaction(txid1)
    tx2 = self.nodes[0].gettransaction(txid2)
    assert_equal(tx1['confirmations'], (- 2))
    assert_equal(tx2['confirmations'], (- 2))
    expected = (((((starting_balance + (2 * INITIAL_BLOCK_REWARD)) - spend_from_doublespend) + fund_foo_tx['fee']) + fund_bar_tx['fee']) + doublespend_fee)
    assert_equal(self.nodes[0].getbalance(), expected)
    assert_equal(self.nodes[0].getbalance('*'), expected)
    assert_equal(self.nodes[0].getbalance(), (((((starting_balance - spend_from_doublespend) + (2 * INITIAL_BLOCK_REWARD)) + fund_foo_tx['fee']) + fund_bar_tx['fee']) + doublespend_fee))