2018-12-18 12:00:46 -08:00
|
|
|
#!/usr/bin/env python
|
2016-01-08 07:37:17 -08:00
|
|
|
|
|
|
|
#
|
2016-07-18 09:43:17 -07:00
|
|
|
# Tests a joinsplit double-spend and a subsequent reorg.
|
2016-01-08 07:37:17 -08:00
|
|
|
#
|
|
|
|
|
2018-12-18 12:00:46 -08:00
|
|
|
import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."
|
|
|
|
|
2016-01-08 07:37:17 -08:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2017-06-20 13:09:33 -07:00
|
|
|
from test_framework.authproxy import JSONRPCException
|
|
|
|
from test_framework.util import assert_equal, connect_nodes, \
|
|
|
|
gather_inputs, sync_blocks
|
|
|
|
|
2016-01-29 18:40:05 -08:00
|
|
|
import time
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-07-16 15:45:41 -07:00
|
|
|
class JoinSplitTest(BitcoinTestFramework):
|
2016-01-08 07:37:17 -08:00
|
|
|
def setup_network(self):
|
|
|
|
# Start with split network:
|
2016-07-16 15:45:41 -07:00
|
|
|
return super(JoinSplitTest, self).setup_network(True)
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-01-29 18:40:05 -08:00
|
|
|
def txid_in_mempool(self, node, txid):
|
|
|
|
exception_triggered = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
node.getrawtransaction(txid)
|
|
|
|
except JSONRPCException:
|
|
|
|
exception_triggered = True
|
|
|
|
|
|
|
|
return not exception_triggered
|
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
def cannot_joinsplit(self, node, txn):
|
2016-01-08 07:37:17 -08:00
|
|
|
exception_triggered = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
node.sendrawtransaction(txn)
|
|
|
|
except JSONRPCException:
|
|
|
|
exception_triggered = True
|
|
|
|
|
2016-01-29 18:40:05 -08:00
|
|
|
return exception_triggered
|
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
def expect_cannot_joinsplit(self, node, txn):
|
|
|
|
assert_equal(self.cannot_joinsplit(node, txn), True)
|
2016-01-08 07:37:17 -08:00
|
|
|
|
|
|
|
def run_test(self):
|
2018-02-28 14:29:05 -08:00
|
|
|
# All nodes should start with 250 ZEC:
|
2016-06-09 21:16:33 -07:00
|
|
|
starting_balance = 250
|
2016-01-08 07:37:17 -08:00
|
|
|
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):
|
2016-04-08 18:05:51 -07:00
|
|
|
(total_in, inputs) = gather_inputs(self.nodes[i], 40)
|
2016-01-08 07:37:17 -08:00
|
|
|
pool[i] = self.nodes[i].createrawtransaction(inputs, {})
|
2017-02-28 11:44:51 -08:00
|
|
|
pool[i] = self.nodes[i].zcrawjoinsplit(pool[i], {}, {zcaddress:39.99}, 39.99, 0)
|
2016-01-08 07:37:17 -08:00
|
|
|
signed = self.nodes[i].signrawtransaction(pool[i]["rawtxn"])
|
2016-01-29 18:40:05 -08:00
|
|
|
|
|
|
|
# send the tx to both halves of the network
|
2016-01-08 07:37:17 -08:00
|
|
|
self.nodes[0].sendrawtransaction(signed["hex"])
|
|
|
|
self.nodes[0].generate(1)
|
2016-01-29 18:40:05 -08:00
|
|
|
self.nodes[2].sendrawtransaction(signed["hex"])
|
|
|
|
self.nodes[2].generate(1)
|
2016-07-14 16:27:54 -07:00
|
|
|
pool[i] = pool[i]["encryptednote1"]
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-01-29 18:40:05 -08:00
|
|
|
sync_blocks(self.nodes[0:2])
|
|
|
|
sync_blocks(self.nodes[2:4])
|
|
|
|
|
2016-01-08 07:37:17 -08:00
|
|
|
# Confirm that the protects have taken place
|
|
|
|
for i in range(4):
|
2016-07-14 17:58:55 -07:00
|
|
|
enc_note = pool[i]
|
|
|
|
receive_result = self.nodes[0].zcrawreceive(zcsecretkey, enc_note)
|
2016-01-08 07:37:17 -08:00
|
|
|
assert_equal(receive_result["exists"], True)
|
2016-07-14 17:58:55 -07:00
|
|
|
pool[i] = receive_result["note"]
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-01-29 18:40:05 -08:00
|
|
|
# Extra confirmations
|
2016-07-14 17:58:55 -07:00
|
|
|
receive_result = self.nodes[1].zcrawreceive(zcsecretkey, enc_note)
|
2016-01-08 07:37:17 -08:00
|
|
|
assert_equal(receive_result["exists"], True)
|
|
|
|
|
2016-07-14 17:58:55 -07:00
|
|
|
receive_result = self.nodes[2].zcrawreceive(zcsecretkey, enc_note)
|
2016-01-29 18:40:05 -08:00
|
|
|
assert_equal(receive_result["exists"], True)
|
|
|
|
|
2016-07-14 17:58:55 -07:00
|
|
|
receive_result = self.nodes[3].zcrawreceive(zcsecretkey, enc_note)
|
2016-01-29 18:40:05 -08:00
|
|
|
assert_equal(receive_result["exists"], True)
|
|
|
|
|
2016-01-08 07:37:17 -08:00
|
|
|
blank_tx = self.nodes[0].createrawtransaction([], {})
|
2016-07-18 09:43:17 -07:00
|
|
|
# Create joinsplit {A, B}->{*}
|
|
|
|
joinsplit_AB = self.nodes[0].zcrawjoinsplit(blank_tx,
|
2016-07-16 15:45:41 -07:00
|
|
|
{pool[0] : zcsecretkey, pool[1] : zcsecretkey},
|
2017-02-28 11:44:51 -08:00
|
|
|
{zcaddress:(39.99*2)-0.01},
|
|
|
|
0, 0.01)
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
# Create joinsplit {B, C}->{*}
|
|
|
|
joinsplit_BC = self.nodes[0].zcrawjoinsplit(blank_tx,
|
2016-07-16 15:45:41 -07:00
|
|
|
{pool[1] : zcsecretkey, pool[2] : zcsecretkey},
|
2017-02-28 11:44:51 -08:00
|
|
|
{zcaddress:(39.99*2)-0.01},
|
|
|
|
0, 0.01)
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
# Create joinsplit {C, D}->{*}
|
|
|
|
joinsplit_CD = self.nodes[0].zcrawjoinsplit(blank_tx,
|
2016-07-16 15:45:41 -07:00
|
|
|
{pool[2] : zcsecretkey, pool[3] : zcsecretkey},
|
2017-02-28 11:44:51 -08:00
|
|
|
{zcaddress:(39.99*2)-0.01},
|
|
|
|
0, 0.01)
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
# Create joinsplit {A, D}->{*}
|
|
|
|
joinsplit_AD = self.nodes[0].zcrawjoinsplit(blank_tx,
|
2016-07-16 15:45:41 -07:00
|
|
|
{pool[0] : zcsecretkey, pool[3] : zcsecretkey},
|
2017-02-28 11:44:51 -08:00
|
|
|
{zcaddress:(39.99*2)-0.01},
|
|
|
|
0, 0.01)
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
# (a) Node 0 will spend joinsplit AB, then attempt to
|
2016-01-08 07:37:17 -08:00
|
|
|
# double-spend it with BC. It should fail before and
|
2016-01-29 18:40:05 -08:00
|
|
|
# after Node 0 mines blocks.
|
2016-01-08 07:37:17 -08:00
|
|
|
#
|
|
|
|
# (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.
|
|
|
|
#
|
2016-01-29 18:40:05 -08:00
|
|
|
# (c) Then, Node 0 will spend AD, which should work
|
2016-01-08 07:37:17 -08:00
|
|
|
# because the previous spend for A (AB) is considered
|
2016-01-29 18:40:05 -08:00
|
|
|
# invalid due to the reorg.
|
2016-01-08 07:37:17 -08:00
|
|
|
|
|
|
|
# (a)
|
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
AB_txid = self.nodes[0].sendrawtransaction(joinsplit_AB["rawtxn"])
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
self.expect_cannot_joinsplit(self.nodes[0], joinsplit_BC["rawtxn"])
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-01-29 18:40:05 -08:00
|
|
|
# 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"
|
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
self.expect_cannot_joinsplit(self.nodes[1], joinsplit_BC["rawtxn"])
|
2016-01-29 18:40:05 -08:00
|
|
|
|
2016-01-08 07:37:17 -08:00
|
|
|
# Generate a block
|
|
|
|
self.nodes[0].generate(1)
|
2016-01-29 18:40:05 -08:00
|
|
|
sync_blocks(self.nodes[0:2])
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
self.expect_cannot_joinsplit(self.nodes[0], joinsplit_BC["rawtxn"])
|
|
|
|
self.expect_cannot_joinsplit(self.nodes[1], joinsplit_BC["rawtxn"])
|
2016-01-08 07:37:17 -08:00
|
|
|
|
|
|
|
# (b)
|
2016-07-18 09:43:17 -07:00
|
|
|
self.nodes[2].sendrawtransaction(joinsplit_BC["rawtxn"])
|
2016-01-29 18:40:05 -08:00
|
|
|
self.nodes[2].generate(5)
|
2016-01-08 07:37:17 -08:00
|
|
|
|
|
|
|
# Connect the two nodes
|
|
|
|
|
2016-01-29 18:40:05 -08:00
|
|
|
connect_nodes(self.nodes[1], 2)
|
|
|
|
sync_blocks(self.nodes)
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-01-29 18:40:05 -08:00
|
|
|
# AB and CD should all be impossible to spend for each node.
|
2016-07-18 09:43:17 -07:00
|
|
|
self.expect_cannot_joinsplit(self.nodes[0], joinsplit_AB["rawtxn"])
|
|
|
|
self.expect_cannot_joinsplit(self.nodes[0], joinsplit_CD["rawtxn"])
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
self.expect_cannot_joinsplit(self.nodes[1], joinsplit_AB["rawtxn"])
|
|
|
|
self.expect_cannot_joinsplit(self.nodes[1], joinsplit_CD["rawtxn"])
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
self.expect_cannot_joinsplit(self.nodes[2], joinsplit_AB["rawtxn"])
|
|
|
|
self.expect_cannot_joinsplit(self.nodes[2], joinsplit_CD["rawtxn"])
|
2016-01-29 18:40:05 -08:00
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
self.expect_cannot_joinsplit(self.nodes[3], joinsplit_AB["rawtxn"])
|
|
|
|
self.expect_cannot_joinsplit(self.nodes[3], joinsplit_CD["rawtxn"])
|
2016-01-29 18:40:05 -08:00
|
|
|
|
2016-01-08 07:37:17 -08:00
|
|
|
# (c)
|
2016-01-29 18:40:05 -08:00
|
|
|
# AD should be possible to send due to the reorg that
|
|
|
|
# tossed out AB.
|
2016-01-08 07:37:17 -08:00
|
|
|
|
2016-07-18 09:43:17 -07:00
|
|
|
self.nodes[0].sendrawtransaction(joinsplit_AD["rawtxn"])
|
2016-01-08 07:37:17 -08:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
|
2016-01-29 18:40:05 -08:00
|
|
|
sync_blocks(self.nodes)
|
|
|
|
|
2016-01-08 07:37:17 -08:00
|
|
|
if __name__ == '__main__':
|
2016-07-16 15:45:41 -07:00
|
|
|
JoinSplitTest().main()
|