Skip to content

Commit

Permalink
Implement isolate_node/reconnect_isolated_node in tests (#2901)
Browse files Browse the repository at this point in the history
  • Loading branch information
UdjinM6 authored May 6, 2019
1 parent 7fdc66d commit 357b727
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
15 changes: 6 additions & 9 deletions qa/rpc-tests/llmq-chainlocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,23 @@ def run_test(self):
assert(block['chainlock'])

# Isolate node, mine on another, and reconnect
self.nodes[0].setnetworkactive(False)
isolate_node(self.nodes[0])
node0_tip = self.nodes[0].getbestblockhash()
self.nodes[1].generate(5)
self.wait_for_chainlock_tip(self.nodes[1])
assert(self.nodes[0].getbestblockhash() == node0_tip)
self.nodes[0].setnetworkactive(True)
connect_nodes(self.nodes[0], 1)
reconnect_isolated_node(self.nodes[0], 1)
self.nodes[1].generate(1)
self.wait_for_chainlock(self.nodes[0], self.nodes[1].getbestblockhash())

# Isolate node, mine on both parts of the network, and reconnect
self.nodes[0].setnetworkactive(False)
isolate_node(self.nodes[0])
self.nodes[0].generate(5)
self.nodes[1].generate(1)
good_tip = self.nodes[1].getbestblockhash()
self.wait_for_chainlock_tip(self.nodes[1])
assert(not self.nodes[0].getblock(self.nodes[0].getbestblockhash())["chainlock"])
self.nodes[0].setnetworkactive(True)
connect_nodes(self.nodes[0], 1)
reconnect_isolated_node(self.nodes[0], 1)
self.nodes[1].generate(1)
self.wait_for_chainlock(self.nodes[0], self.nodes[1].getbestblockhash())
assert(self.nodes[0].getblock(self.nodes[0].getbestblockhash())["previousblockhash"] == good_tip)
Expand Down Expand Up @@ -97,7 +95,7 @@ def run_test(self):
self.wait_for_sporks_same()

# Isolate a node and let it create some transactions which won't get IS locked
self.nodes[0].setnetworkactive(False)
isolate_node(self.nodes[0])
txs = []
for i in range(3):
txs.append(self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1))
Expand All @@ -120,8 +118,7 @@ def run_test(self):
assert("confirmations" in tx and tx["confirmations"] > 0)
# Enable network on first node again, which will cause the blocks to propagate and IS locks to happen retroactively
# for the mined TXs, which will then allow the network to create a CLSIG
self.nodes[0].setnetworkactive(True)
connect_nodes(self.nodes[0], 1)
reconnect_isolated_node(self.nodes[0], 1)
self.wait_for_chainlock(self.nodes[0], self.nodes[1].getbestblockhash())

def wait_for_chainlock_tip_all_nodes(self):
Expand Down
14 changes: 5 additions & 9 deletions qa/rpc-tests/p2p-instantsend.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def test_block_doublespend(self):

# create doublespending transaction, but don't relay it
dblspnd_tx = self.create_raw_tx(sender, isolated, 0.5, 1, 100)
# stop one node to isolate it from network
isolated.setnetworkactive(False)
# isolate one node from network
isolate_node(isolated)
# instantsend to receiver
receiver_addr = receiver.getnewaddress()
is_id = sender.instantsendtoaddress(receiver_addr, 0.9)
Expand All @@ -68,9 +68,7 @@ def test_block_doublespend(self):
isolated.generate(1)
wrong_block = isolated.getbestblockhash()
# connect isolated block to network
isolated.setnetworkactive(True)
for i in range(0, self.isolated_idx):
connect_nodes(self.nodes[i], self.isolated_idx)
reconnect_isolated_node(isolated, 0)
# check doublespend block is rejected by other nodes
timeout = 10
for i in range(0, self.num_nodes):
Expand Down Expand Up @@ -102,14 +100,12 @@ def test_mempool_doublespend(self):
dblspnd_tx = self.create_raw_tx(sender, isolated, 0.5, 1, 100)
dblspnd_txid = bytes_to_hex_str(hash256(hex_str_to_bytes(dblspnd_tx['hex']))[::-1])
# isolate one node from network
isolated.setnetworkactive(False)
isolate_node(isolated)
# send doublespend transaction to isolated node
isolated.sendrawtransaction(dblspnd_tx['hex'])
# let isolated node rejoin the network
# The previously isolated node should NOT relay the doublespending TX
isolated.setnetworkactive(True)
for i in range(0, self.isolated_idx):
connect_nodes(self.nodes[i], self.isolated_idx)
reconnect_isolated_node(isolated, 0)
for node in self.nodes:
if node is not isolated:
assert_raises_jsonrpc(-5, "No such mempool or blockchain transaction", node.getrawtransaction, dblspnd_txid)
Expand Down
13 changes: 13 additions & 0 deletions qa/rpc-tests/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,19 @@ def connect_nodes_bi(nodes, a, b):
connect_nodes(nodes[a], b)
connect_nodes(nodes[b], a)

def isolate_node(node, timeout=5):
node.setnetworkactive(False)
st = time.time()
while time.time() < st + timeout:
if node.getconnectioncount() == 0:
return
time.sleep(0.5)
raise AssertionError("disconnect_node timed out")

def reconnect_isolated_node(node, node_num):
node.setnetworkactive(True)
connect_nodes(node, node_num)

def find_output(node, txid, amount):
"""
Return index to output of txid with value amount
Expand Down

0 comments on commit 357b727

Please sign in to comment.