Beispiel #1
0
 def gen_valid_tx(self, node_num=0):
     """ Generate a single, valid, signed transaction using the wallet from node_num, and return its hex.
     This transaction is not submitted to mempool; it is simply generated, signed, and returned. """
     node = self.nodes[node_num]
     fee = self.min_relay_fee
     amount = Decimal("0.00002") * random.randrange(10)
     (total_in, inputs) = util.gather_inputs(node, amount + fee)
     outputs = util.make_change(node, total_in, amount, fee)
     outputs[node.getnewaddress()] = float(amount)
     rawtx = node.createrawtransaction(inputs, outputs)
     signresult = node.signrawtransactionwithwallet(rawtx)
     return signresult["hex"]
Beispiel #2
0
    def run_test(self):
        print "On REGTEST... \n\treward is {} per block\n\t100 blocks to maturity".format(
            self._reward)
        zckeypair = self.nodes[0].zcrawkeygen()
        zcsecretkey = zckeypair["zcsecretkey"]
        zcaddress = zckeypair["zcaddress"]

        amount = self._reward * 4
        amount_less = amount - self._fee
        (total_in, inputs) = gather_inputs(self.nodes[0], amount)
        protect_tx = self.nodes[0].createrawtransaction(inputs, {})
        joinsplit_result = self.nodes[0].zcrawjoinsplit(
            protect_tx, {}, {zcaddress: amount_less}, amount_less, 0)

        receive_result = self.nodes[0].zcrawreceive(
            zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], False)

        protect_tx = self.nodes[0].signrawtransaction(
            joinsplit_result["rawtxn"])
        self.nodes[0].sendrawtransaction(protect_tx["hex"])
        self.nodes[0].generate(1)

        receive_result = self.nodes[0].zcrawreceive(
            zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], True)

        # The pure joinsplit we create should be mined in the next block
        # despite other transactions being in the mempool.
        addrtest = self.nodes[0].getnewaddress()
        for xx in range(0, 10):
            self.nodes[0].generate(1)
            for x in range(0, 50):
                self.nodes[0].sendtoaddress(addrtest, self._fee)

        amount_less2 = amount_less - self._fee
        joinsplit_tx = self.nodes[0].createrawtransaction([], {})
        joinsplit_result = self.nodes[0].zcrawjoinsplit(
            joinsplit_tx, {receive_result["note"]: zcsecretkey},
            {zcaddress: amount_less2}, 0, self._fee)

        self.nodes[0].sendrawtransaction(joinsplit_result["rawtxn"])
        self.nodes[0].generate(1)

        print "Done!"
        receive_result = self.nodes[0].zcrawreceive(
            zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], True)
Beispiel #3
0
    def run_test(self):
        zckeypair = self.nodes[0].zcrawkeygen()
        zcsecretkey = zckeypair["zcsecretkey"]
        zcaddress = zckeypair["zcaddress"]

        (total_in, inputs) = gather_inputs(self.nodes[0], 40)
        shield_tx = self.nodes[0].createrawtransaction(inputs, {})
        joinsplit_result = self.nodes[0].zcrawjoinsplit(
            shield_tx, {}, {zcaddress: 39.99}, 39.99, 0)

        receive_result = self.nodes[0].zcrawreceive(
            zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], False)

        shield_tx = self.nodes[0].signrawtransaction(
            joinsplit_result["rawtxn"])
        self.nodes[0].sendrawtransaction(shield_tx["hex"])
        self.nodes[0].generate(1)
        self.sync_all()

        receive_result = self.nodes[0].zcrawreceive(
            zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], True)

        # The pure joinsplit we create should be mined in the next block
        # despite other transactions being in the mempool.
        addrtest = self.nodes[0].getnewaddress()
        for xx in range(0, 10):
            self.nodes[0].generate(1)
            self.sync_all()
            for x in range(0, 50):
                self.nodes[0].sendtoaddress(addrtest, 0.01)

        joinsplit_tx = self.nodes[0].createrawtransaction([], {})
        joinsplit_result = self.nodes[0].zcrawjoinsplit(
            joinsplit_tx, {receive_result["note"]: zcsecretkey},
            {zcaddress: 39.98}, 0, 0.01)

        self.nodes[0].sendrawtransaction(joinsplit_result["rawtxn"])
        self.nodes[0].generate(1)
        self.sync_all()

        print("Done!")
        receive_result = self.nodes[0].zcrawreceive(
            zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], True)
Beispiel #4
0
    def run_test(self):
        zckeypair = self.nodes[0].zcrawkeygen()
        zcsecretkey = zckeypair["zcsecretkey"]
        zcaddress = zckeypair["zcaddress"]

# ZEN_MOD_START
        (total_in, inputs) = gather_inputs(self.nodes[0], 45.75)
# ZEN_MOD_END
        protect_tx = self.nodes[0].createrawtransaction(inputs, {})
# ZEN_MOD_START
        joinsplit_result = self.nodes[0].zcrawjoinsplit(protect_tx, {}, {zcaddress:45.74}, 45.74, 0)
# ZEN_MOD_END

        receive_result = self.nodes[0].zcrawreceive(zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], False)

        protect_tx = self.nodes[0].signrawtransaction(joinsplit_result["rawtxn"])
        self.nodes[0].sendrawtransaction(protect_tx["hex"])
        self.nodes[0].generate(1)

        receive_result = self.nodes[0].zcrawreceive(zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], True)

        # The pure joinsplit we create should be mined in the next block
        # despite other transactions being in the mempool.
        addrtest = self.nodes[0].getnewaddress()
        for xx in range(0,10):
            self.nodes[0].generate(1)
            for x in range(0,50):
                self.nodes[0].sendtoaddress(addrtest, 0.01);

        joinsplit_tx = self.nodes[0].createrawtransaction([], {})
# ZEN_MOD_START
        joinsplit_result = self.nodes[0].zcrawjoinsplit(joinsplit_tx, {receive_result["note"] : zcsecretkey}, {zcaddress: 45.73}, 0, 0.01)
# ZEN_MOD_END

        self.nodes[0].sendrawtransaction(joinsplit_result["rawtxn"])
        self.nodes[0].generate(1)

        print "Done!"
        receive_result = self.nodes[0].zcrawreceive(zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], True)
Beispiel #5
0
    def run_test(self):
        zckeypair = self.nodes[0].zcrawkeygen()
        zcsecretkey = zckeypair["zcsecretkey"]
        zcaddress = zckeypair["zcaddress"]

        (total_in, inputs) = gather_inputs(self.nodes[0], 40)
        protect_tx = self.nodes[0].createrawtransaction(inputs, {})
        joinsplit_result = self.nodes[0].zcrawjoinsplit(protect_tx, {}, {zcaddress:39.99}, 39.99, 0)

        receive_result = self.nodes[0].zcrawreceive(zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], False)

        protect_tx = self.nodes[0].signrawtransaction(joinsplit_result["rawtxn"])
        self.nodes[0].sendrawtransaction(protect_tx["hex"])
        self.nodes[0].generate(1)

        receive_result = self.nodes[0].zcrawreceive(zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], True)

        # The pure joinsplit we create should be mined in the next block
        # despite other transactions being in the mempool.
        addrtest = self.nodes[0].getnewaddress()
        for xx in range(0,10):
            self.nodes[0].generate(1)
            for x in range(0,50):
                self.nodes[0].sendtoaddress(addrtest, 0.01);

        joinsplit_tx = self.nodes[0].createrawtransaction([], {})
        joinsplit_result = self.nodes[0].zcrawjoinsplit(joinsplit_tx, {receive_result["note"] : zcsecretkey}, {zcaddress: 39.98}, 0, 0.01)

        self.nodes[0].sendrawtransaction(joinsplit_result["rawtxn"])
        self.nodes[0].generate(1)

        print "Done!"
        receive_result = self.nodes[0].zcrawreceive(zcsecretkey, joinsplit_result["encryptednote1"])
        assert_equal(receive_result["exists"], True)
    def run_test(self):
        # All nodes should start with 25*11.4375 BTC:
        starting_balance = 25 * 11.4375
        for i in range(4):
            assert_equal(self.nodes[i].getbalance(), starting_balance)
            self.nodes[i].getnewaddress(
                ""
            )  # bug workaround, coins generated assigned to first getnewaddress!

        # Generate zcaddress keypairs
        zckeypair = self.nodes[0].zcrawkeygen()
        zcsecretkey = zckeypair["zcsecretkey"]
        zcaddress = zckeypair["zcaddress"]

        pool = [0, 1, 2, 3]
        for i in range(4):
            (total_in, inputs) = gather_inputs(self.nodes[i], 45.75)
            pool[i] = self.nodes[i].createrawtransaction(inputs, {})
            pool[i] = self.nodes[i].zcrawjoinsplit(pool[i], {},
                                                   {zcaddress: 45.74}, 45.74,
                                                   0)
            signed = self.nodes[i].signrawtransaction(pool[i]["rawtxn"])

            # send the tx to both halves of the network
            self.nodes[0].sendrawtransaction(signed["hex"])
            self.nodes[0].generate(1)
            self.nodes[2].sendrawtransaction(signed["hex"])
            self.nodes[2].generate(1)
            pool[i] = pool[i]["encryptednote1"]

        sync_blocks(self.nodes[0:2])
        sync_blocks(self.nodes[2:4])

        # Confirm that the protects have taken place
        for i in range(4):
            enc_note = pool[i]
            receive_result = self.nodes[0].zcrawreceive(zcsecretkey, enc_note)
            assert_equal(receive_result["exists"], True)
            pool[i] = receive_result["note"]

            # Extra confirmations
            receive_result = self.nodes[1].zcrawreceive(zcsecretkey, enc_note)
            assert_equal(receive_result["exists"], True)

            receive_result = self.nodes[2].zcrawreceive(zcsecretkey, enc_note)
            assert_equal(receive_result["exists"], True)

            receive_result = self.nodes[3].zcrawreceive(zcsecretkey, enc_note)
            assert_equal(receive_result["exists"], True)

        blank_tx = self.nodes[0].createrawtransaction([], {})
        # Create joinsplit {A, B}->{*}
        joinsplit_AB = self.nodes[0].zcrawjoinsplit(blank_tx, {
            pool[0]: zcsecretkey,
            pool[1]: zcsecretkey
        }, {zcaddress: (45.74 * 2) - 0.01}, 0, 0.01)

        # Create joinsplit {B, C}->{*}
        joinsplit_BC = self.nodes[0].zcrawjoinsplit(blank_tx, {
            pool[1]: zcsecretkey,
            pool[2]: zcsecretkey
        }, {zcaddress: (45.74 * 2) - 0.01}, 0, 0.01)

        # Create joinsplit {C, D}->{*}
        joinsplit_CD = self.nodes[0].zcrawjoinsplit(blank_tx, {
            pool[2]: zcsecretkey,
            pool[3]: zcsecretkey
        }, {zcaddress: (45.74 * 2) - 0.01}, 0, 0.01)

        # Create joinsplit {A, D}->{*}
        joinsplit_AD = self.nodes[0].zcrawjoinsplit(blank_tx, {
            pool[0]: zcsecretkey,
            pool[3]: zcsecretkey
        }, {zcaddress: (45.74 * 2) - 0.01}, 0, 0.01)

        # (a)    Node 0 will spend joinsplit AB, then attempt to
        # double-spend it with BC. It should fail before and
        # after Node 0 mines blocks.
        #
        # (b)    Then, Node 2 will spend BC, and mine 5 blocks.
        # Node 1 connects, and AB will be reorg'd from the chain.
        # Any attempts to spend AB or CD should fail for
        # both nodes.
        #
        # (c)    Then, Node 0 will spend AD, which should work
        # because the previous spend for A (AB) is considered
        # invalid due to the reorg.

        # (a)

        AB_txid = self.nodes[0].sendrawtransaction(joinsplit_AB["rawtxn"])

        self.expect_cannot_joinsplit(self.nodes[0], joinsplit_BC["rawtxn"])

        # Wait until node[1] receives AB before we attempt to double-spend
        # with BC.
        print "Waiting for AB_txid...\n"
        while True:
            if self.txid_in_mempool(self.nodes[1], AB_txid):
                break
            time.sleep(0.2)
        print "Done!\n"

        self.expect_cannot_joinsplit(self.nodes[1], joinsplit_BC["rawtxn"])

        # Generate a block
        self.nodes[0].generate(1)
        sync_blocks(self.nodes[0:2])

        self.expect_cannot_joinsplit(self.nodes[0], joinsplit_BC["rawtxn"])
        self.expect_cannot_joinsplit(self.nodes[1], joinsplit_BC["rawtxn"])

        # (b)
        self.nodes[2].sendrawtransaction(joinsplit_BC["rawtxn"])
        self.nodes[2].generate(5)

        # Connect the two nodes

        connect_nodes(self.nodes[1], 2)
        sync_blocks(self.nodes)

        # AB and CD should all be impossible to spend for each node.
        self.expect_cannot_joinsplit(self.nodes[0], joinsplit_AB["rawtxn"])
        self.expect_cannot_joinsplit(self.nodes[0], joinsplit_CD["rawtxn"])

        self.expect_cannot_joinsplit(self.nodes[1], joinsplit_AB["rawtxn"])
        self.expect_cannot_joinsplit(self.nodes[1], joinsplit_CD["rawtxn"])

        self.expect_cannot_joinsplit(self.nodes[2], joinsplit_AB["rawtxn"])
        self.expect_cannot_joinsplit(self.nodes[2], joinsplit_CD["rawtxn"])

        self.expect_cannot_joinsplit(self.nodes[3], joinsplit_AB["rawtxn"])
        self.expect_cannot_joinsplit(self.nodes[3], joinsplit_CD["rawtxn"])

        # (c)
        # AD should be possible to send due to the reorg that
        # tossed out AB.

        self.nodes[0].sendrawtransaction(joinsplit_AD["rawtxn"])
        self.nodes[0].generate(1)

        sync_blocks(self.nodes)
Beispiel #7
0
    def run_test(self):
        mining_reward = 12500
        starting_balance = mining_reward * 25

        for i in range(4):
            assert_equal(self.nodes[i].getbalance(), starting_balance)
            self.nodes[i].getnewaddress(
                ""
            )  # bug workaround, coins generated assigned to first getnewaddress!

        # Coins are sent to node1_address
        node1_address = self.nodes[1].getnewaddress("")

        # First: use raw transaction API to send (starting_balance - (mining_reward - 2)) BTC to node1_address,
        # but don't broadcast:
        (total_in,
         inputs) = gather_inputs(self.nodes[0],
                                 (starting_balance - (mining_reward - 2)))
        change_address = self.nodes[0].getnewaddress("")
        outputs = {}
        outputs[change_address] = (mining_reward - 2)
        outputs[node1_address] = (starting_balance - (mining_reward - 2))
        rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
        doublespend = self.nodes[0].signrawtransaction(rawtx)
        assert_equal(doublespend["complete"], True)

        # Create two transaction from node[0] to node[1]; the
        # second must spend change from the first because the first
        # spends all mature inputs:
        txid1 = self.nodes[0].sendfrom(
            "", node1_address, (starting_balance - (mining_reward - 2)), 0)
        txid2 = self.nodes[0].sendfrom("", node1_address, 5, 0)

        # Have node0 mine a block:
        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)

        # Node0's balance should be starting balance, plus mining_reward for another
        # matured block, minus (starting_balance - (mining_reward - 2)), minus 5, and minus transaction fees:
        expected = starting_balance
        if self.options.mine_block: expected += mining_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)
            # Node1's total balance should be its starting balance plus both transaction amounts:
            assert_equal(self.nodes[1].getbalance(""),
                         starting_balance - (tx1["amount"] + tx2["amount"]))
        else:
            assert_equal(tx1["confirmations"], 0)
            assert_equal(tx2["confirmations"], 0)

        # Now give doublespend to miner:
        self.nodes[2].sendrawtransaction(doublespend["hex"])
        # ... mine a block...
        self.nodes[2].generate(1)

        # Reconnect the split network, and sync chain:
        connect_nodes(self.nodes[1], 2)
        self.nodes[2].generate(1)  # Mine another block to make sure we sync
        sync_blocks(self.nodes)

        # Re-fetch transaction info:
        tx1 = self.nodes[0].gettransaction(txid1)
        tx2 = self.nodes[0].gettransaction(txid2)

        # Both transactions should be conflicted
        assert_equal(tx1["confirmations"], -1)
        assert_equal(tx2["confirmations"], -1)

        # Node0's total balance should be starting balance, plus (mining_reward * 2) for
        # two more matured blocks, minus (starting_balance - (mining_reward - 2)) for the double-spend:
        expected = starting_balance + (mining_reward *
                                       2) - (starting_balance -
                                             (mining_reward - 2))
        assert_equal(self.nodes[0].getbalance(), expected)
        assert_equal(self.nodes[0].getbalance("*"), expected)

        # Node1's total balance should be its starting balance plus the amount of the mutated send:
        assert_equal(
            self.nodes[1].getbalance(""),
            starting_balance + (starting_balance - (mining_reward - 2)))
    def run_test(self):
        # All nodes should start with 250 ZEC:
        starting_balance = 250
        for i in range(4):
            assert_equal(self.nodes[i].getbalance(), starting_balance)
            self.nodes[i].getnewaddress("")  # bug workaround, coins generated assigned to first getnewaddress!
        
        # Generate zcaddress keypairs
        zckeypair = self.nodes[0].zcrawkeygen()
        zcsecretkey = zckeypair["zcsecretkey"]
        zcaddress = zckeypair["zcaddress"]

        pool = [0, 1, 2, 3]
        for i in range(4):
            (total_in, inputs) = gather_inputs(self.nodes[i], 40)
            pool[i] = self.nodes[i].createrawtransaction(inputs, {})
            pool[i] = self.nodes[i].zcrawjoinsplit(pool[i], {}, {zcaddress:39.99}, 39.99, 0)
            signed = self.nodes[i].signrawtransaction(pool[i]["rawtxn"])

            # send the tx to both halves of the network
            self.nodes[0].sendrawtransaction(signed["hex"])
            self.nodes[0].generate(1)
            self.nodes[2].sendrawtransaction(signed["hex"])
            self.nodes[2].generate(1)
            pool[i] = pool[i]["encryptednote1"]

        sync_blocks(self.nodes[0:2])
        sync_blocks(self.nodes[2:4])

        # Confirm that the protects have taken place
        for i in range(4):
            enc_note = pool[i]
            receive_result = self.nodes[0].zcrawreceive(zcsecretkey, enc_note)
            assert_equal(receive_result["exists"], True)
            pool[i] = receive_result["note"]

            # Extra confirmations
            receive_result = self.nodes[1].zcrawreceive(zcsecretkey, enc_note)
            assert_equal(receive_result["exists"], True)

            receive_result = self.nodes[2].zcrawreceive(zcsecretkey, enc_note)
            assert_equal(receive_result["exists"], True)

            receive_result = self.nodes[3].zcrawreceive(zcsecretkey, enc_note)
            assert_equal(receive_result["exists"], True)

        blank_tx = self.nodes[0].createrawtransaction([], {})
        # Create joinsplit {A, B}->{*}
        joinsplit_AB = self.nodes[0].zcrawjoinsplit(blank_tx,
                                               {pool[0] : zcsecretkey, pool[1] : zcsecretkey},
                                               {zcaddress:(39.99*2)-0.01},
                                               0, 0.01)

        # Create joinsplit {B, C}->{*}
        joinsplit_BC = self.nodes[0].zcrawjoinsplit(blank_tx,
                                               {pool[1] : zcsecretkey, pool[2] : zcsecretkey},
                                               {zcaddress:(39.99*2)-0.01},
                                               0, 0.01)

        # Create joinsplit {C, D}->{*}
        joinsplit_CD = self.nodes[0].zcrawjoinsplit(blank_tx,
                                               {pool[2] : zcsecretkey, pool[3] : zcsecretkey},
                                               {zcaddress:(39.99*2)-0.01},
                                               0, 0.01)

        # Create joinsplit {A, D}->{*}
        joinsplit_AD = self.nodes[0].zcrawjoinsplit(blank_tx,
                                               {pool[0] : zcsecretkey, pool[3] : zcsecretkey},
                                               {zcaddress:(39.99*2)-0.01},
                                               0, 0.01)

        # (a)    Node 0 will spend joinsplit AB, then attempt to
        # double-spend it with BC. It should fail before and
        # after Node 0 mines blocks.
        #
        # (b)    Then, Node 2 will spend BC, and mine 5 blocks.
        # Node 1 connects, and AB will be reorg'd from the chain.
        # Any attempts to spend AB or CD should fail for
        # both nodes.
        #
        # (c)    Then, Node 0 will spend AD, which should work
        # because the previous spend for A (AB) is considered
        # invalid due to the reorg.

        # (a)

        AB_txid = self.nodes[0].sendrawtransaction(joinsplit_AB["rawtxn"])

        self.expect_cannot_joinsplit(self.nodes[0], joinsplit_BC["rawtxn"])

        # Wait until node[1] receives AB before we attempt to double-spend
        # with BC.
        print "Waiting for AB_txid...\n"
        while True:
            if self.txid_in_mempool(self.nodes[1], AB_txid):
                break
            time.sleep(0.2)
        print "Done!\n"

        self.expect_cannot_joinsplit(self.nodes[1], joinsplit_BC["rawtxn"])

        # Generate a block
        self.nodes[0].generate(1)
        sync_blocks(self.nodes[0:2])

        self.expect_cannot_joinsplit(self.nodes[0], joinsplit_BC["rawtxn"])
        self.expect_cannot_joinsplit(self.nodes[1], joinsplit_BC["rawtxn"])

        # (b)
        self.nodes[2].sendrawtransaction(joinsplit_BC["rawtxn"])
        self.nodes[2].generate(5)

        # Connect the two nodes

        connect_nodes(self.nodes[1], 2)
        sync_blocks(self.nodes)

        # AB and CD should all be impossible to spend for each node.
        self.expect_cannot_joinsplit(self.nodes[0], joinsplit_AB["rawtxn"])
        self.expect_cannot_joinsplit(self.nodes[0], joinsplit_CD["rawtxn"])

        self.expect_cannot_joinsplit(self.nodes[1], joinsplit_AB["rawtxn"])
        self.expect_cannot_joinsplit(self.nodes[1], joinsplit_CD["rawtxn"])

        self.expect_cannot_joinsplit(self.nodes[2], joinsplit_AB["rawtxn"])
        self.expect_cannot_joinsplit(self.nodes[2], joinsplit_CD["rawtxn"])

        self.expect_cannot_joinsplit(self.nodes[3], joinsplit_AB["rawtxn"])
        self.expect_cannot_joinsplit(self.nodes[3], joinsplit_CD["rawtxn"])

        # (c)
        # AD should be possible to send due to the reorg that
        # tossed out AB.

        self.nodes[0].sendrawtransaction(joinsplit_AD["rawtxn"])
        self.nodes[0].generate(1)

        sync_blocks(self.nodes)
Beispiel #9
0
    def run_test(self):
        mining_reward = 10
        starting_balance = mining_reward * 25

        for i in range(4):
            assert_equal(self.nodes[i].getbalance(), starting_balance)
            self.nodes[i].getnewaddress("")  # bug workaround, coins generated assigned to first getnewaddress!

        # Coins are sent to node1_address
        node1_address = self.nodes[1].getnewaddress("")

        # First: use raw transaction API to send (starting_balance - (mining_reward - 2)) BTC to node1_address,
        # but don't broadcast:
        (total_in, inputs) = gather_inputs(self.nodes[0], (starting_balance - (mining_reward - 2)))
        change_address = self.nodes[0].getnewaddress("")
        outputs = {}
        outputs[change_address] = (mining_reward - 2)
        outputs[node1_address] = (starting_balance - (mining_reward - 2))
        rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
        doublespend = self.nodes[0].signrawtransaction(rawtx)
        assert_equal(doublespend["complete"], True)

        # Create two transaction from node[0] to node[1]; the
        # second must spend change from the first because the first
        # spends all mature inputs:
        txid1 = self.nodes[0].sendfrom("", node1_address, (starting_balance - (mining_reward - 2)), 0)
        txid2 = self.nodes[0].sendfrom("", node1_address, 5, 0)

        # Have node0 mine a block:
        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)

        # Node0's balance should be starting balance, plus mining_reward for another
        # matured block, minus (starting_balance - (mining_reward - 2)), minus 5, and minus transaction fees:
        expected = starting_balance
        if self.options.mine_block: expected += mining_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)
            # Node1's total balance should be its starting balance plus both transaction amounts:
            assert_equal(self.nodes[1].getbalance(""), starting_balance - (tx1["amount"]+tx2["amount"]))
        else:
            assert_equal(tx1["confirmations"], 0)
            assert_equal(tx2["confirmations"], 0)

        # Now give doublespend to miner:
        self.nodes[2].sendrawtransaction(doublespend["hex"])
        # ... mine a block...
        self.nodes[2].generate(1)

        # Reconnect the split network, and sync chain:
        connect_nodes(self.nodes[1], 2)
        self.nodes[2].generate(1)  # Mine another block to make sure we sync
        sync_blocks(self.nodes)

        # Re-fetch transaction info:
        tx1 = self.nodes[0].gettransaction(txid1)
        tx2 = self.nodes[0].gettransaction(txid2)

        # Both transactions should be conflicted
        assert_equal(tx1["confirmations"], -1)
        assert_equal(tx2["confirmations"], -1)

        # Node0's total balance should be starting balance, plus (mining_reward * 2) for
        # two more matured blocks, minus (starting_balance - (mining_reward - 2)) for the double-spend:
        expected = starting_balance + (mining_reward * 2) - (starting_balance - (mining_reward - 2))
        assert_equal(self.nodes[0].getbalance(), expected)
        assert_equal(self.nodes[0].getbalance("*"), expected)

        # Node1's total balance should be its starting balance plus the amount of the mutated send:
        assert_equal(self.nodes[1].getbalance(""), starting_balance + (starting_balance - (mining_reward - 2)))