2019-01-23 07:08:13 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2015-2018 The Dash Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2019-08-28 13:51:59 +02:00
|
|
|
import time
|
|
|
|
|
2019-01-23 07:08:13 +01:00
|
|
|
from test_framework.mininode import *
|
|
|
|
from test_framework.test_framework import DashTestFramework
|
|
|
|
from test_framework.util import *
|
|
|
|
|
|
|
|
'''
|
|
|
|
llmq-chainlocks.py
|
|
|
|
|
|
|
|
Checks LLMQs based ChainLocks
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
class LLMQChainLocksTest(DashTestFramework):
|
2019-09-24 00:57:30 +02:00
|
|
|
def set_test_params(self):
|
|
|
|
self.set_dash_test_params(6, 5, [], fast_dip3_enforcement=True)
|
2019-01-23 07:08:13 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
|
2019-03-22 11:51:50 +01:00
|
|
|
while self.nodes[0].getblockchaininfo()["bip9_softforks"]["dip0008"]["status"] != "active":
|
|
|
|
self.nodes[0].generate(10)
|
|
|
|
sync_blocks(self.nodes, timeout=60*5)
|
|
|
|
|
2019-01-23 07:08:13 +01:00
|
|
|
self.nodes[0].spork("SPORK_17_QUORUM_DKG_ENABLED", 0)
|
|
|
|
self.nodes[0].spork("SPORK_19_CHAINLOCKS_ENABLED", 0)
|
|
|
|
self.wait_for_sporks_same()
|
|
|
|
|
|
|
|
for i in range(4):
|
|
|
|
self.mine_quorum()
|
|
|
|
|
|
|
|
# mine single block, wait for chainlock
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.wait_for_chainlock_tip_all_nodes()
|
|
|
|
|
|
|
|
# mine many blocks, wait for chainlock
|
|
|
|
self.nodes[0].generate(20)
|
|
|
|
self.wait_for_chainlock_tip_all_nodes()
|
|
|
|
|
|
|
|
# assert that all blocks up until the tip are chainlocked
|
|
|
|
for h in range(1, self.nodes[0].getblockcount()):
|
|
|
|
block = self.nodes[0].getblock(self.nodes[0].getblockhash(h))
|
|
|
|
assert(block['chainlock'])
|
|
|
|
|
|
|
|
# Isolate node, mine on another, and reconnect
|
2019-05-06 16:58:38 +02:00
|
|
|
isolate_node(self.nodes[0])
|
2019-09-15 22:08:21 +02:00
|
|
|
node0_mining_addr = self.nodes[0].getnewaddress()
|
2019-01-23 07:08:13 +01:00
|
|
|
node0_tip = self.nodes[0].getbestblockhash()
|
2019-09-15 22:08:21 +02:00
|
|
|
self.nodes[1].generatetoaddress(5, node0_mining_addr)
|
2019-01-23 07:08:13 +01:00
|
|
|
self.wait_for_chainlock_tip(self.nodes[1])
|
|
|
|
assert(self.nodes[0].getbestblockhash() == node0_tip)
|
2019-05-06 16:58:38 +02:00
|
|
|
reconnect_isolated_node(self.nodes[0], 1)
|
2019-09-15 22:08:21 +02:00
|
|
|
self.nodes[1].generatetoaddress(1, node0_mining_addr)
|
2019-01-23 07:08:13 +01:00
|
|
|
self.wait_for_chainlock(self.nodes[0], self.nodes[1].getbestblockhash())
|
|
|
|
|
|
|
|
# Isolate node, mine on both parts of the network, and reconnect
|
2019-05-06 16:58:38 +02:00
|
|
|
isolate_node(self.nodes[0])
|
2019-01-23 07:08:13 +01:00
|
|
|
self.nodes[0].generate(5)
|
2019-09-15 22:08:21 +02:00
|
|
|
self.nodes[1].generatetoaddress(1, node0_mining_addr)
|
2019-01-23 07:08:13 +01:00
|
|
|
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"])
|
2019-05-06 16:58:38 +02:00
|
|
|
reconnect_isolated_node(self.nodes[0], 1)
|
2019-09-15 22:08:21 +02:00
|
|
|
self.nodes[1].generatetoaddress(1, node0_mining_addr)
|
2019-01-23 07:08:13 +01:00
|
|
|
self.wait_for_chainlock(self.nodes[0], self.nodes[1].getbestblockhash())
|
|
|
|
assert(self.nodes[0].getblock(self.nodes[0].getbestblockhash())["previousblockhash"] == good_tip)
|
|
|
|
assert(self.nodes[1].getblock(self.nodes[1].getbestblockhash())["previousblockhash"] == good_tip)
|
|
|
|
|
|
|
|
# Keep node connected and let it try to reorg the chain
|
|
|
|
good_tip = self.nodes[0].getbestblockhash()
|
|
|
|
# Restart it so that it forgets all the chainlocks from the past
|
2019-07-04 16:48:01 +02:00
|
|
|
self.stop_node(0)
|
2019-09-24 00:56:31 +02:00
|
|
|
self.start_node(0)
|
2019-01-23 07:08:13 +01:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
2019-03-13 14:00:54 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
2019-01-23 07:08:13 +01:00
|
|
|
# Now try to reorg the chain
|
|
|
|
self.nodes[0].generate(2)
|
2019-08-28 13:51:59 +02:00
|
|
|
time.sleep(6)
|
2019-01-23 07:08:13 +01:00
|
|
|
assert(self.nodes[1].getbestblockhash() == good_tip)
|
|
|
|
self.nodes[0].generate(2)
|
2019-08-28 13:51:59 +02:00
|
|
|
time.sleep(6)
|
2019-01-23 07:08:13 +01:00
|
|
|
assert(self.nodes[1].getbestblockhash() == good_tip)
|
|
|
|
|
|
|
|
# Now let the node which is on the wrong chain reorg back to the locked chain
|
|
|
|
self.nodes[0].reconsiderblock(good_tip)
|
|
|
|
assert(self.nodes[0].getbestblockhash() != good_tip)
|
2019-09-15 22:08:21 +02:00
|
|
|
self.nodes[1].generatetoaddress(1, node0_mining_addr)
|
2019-01-23 07:08:13 +01:00
|
|
|
self.wait_for_chainlock(self.nodes[0], self.nodes[1].getbestblockhash())
|
|
|
|
assert(self.nodes[0].getbestblockhash() == self.nodes[1].getbestblockhash())
|
|
|
|
|
2019-03-19 11:55:51 +01:00
|
|
|
# Enable LLMQ bases InstantSend, which also enables checks for "safe" transactions
|
2019-07-09 16:50:08 +02:00
|
|
|
self.nodes[0].spork("SPORK_2_INSTANTSEND_ENABLED", 0)
|
|
|
|
self.nodes[0].spork("SPORK_3_INSTANTSEND_BLOCK_FILTERING", 0)
|
2019-03-19 11:55:51 +01:00
|
|
|
self.wait_for_sporks_same()
|
|
|
|
|
|
|
|
# Isolate a node and let it create some transactions which won't get IS locked
|
2019-05-06 16:58:38 +02:00
|
|
|
isolate_node(self.nodes[0])
|
2019-03-19 11:55:51 +01:00
|
|
|
txs = []
|
|
|
|
for i in range(3):
|
|
|
|
txs.append(self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1))
|
|
|
|
txs += self.create_chained_txs(self.nodes[0], 1)
|
|
|
|
# Assert that after block generation these TXs are NOT included (as they are "unsafe")
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
for txid in txs:
|
|
|
|
tx = self.nodes[0].getrawtransaction(txid, 1)
|
|
|
|
assert("confirmations" not in tx)
|
2019-08-28 13:51:59 +02:00
|
|
|
time.sleep(1)
|
2019-03-19 11:55:51 +01:00
|
|
|
assert(not self.nodes[0].getblock(self.nodes[0].getbestblockhash())["chainlock"])
|
|
|
|
# Disable LLMQ based InstantSend for a very short time (this never gets propagated to other nodes)
|
2019-07-09 16:50:08 +02:00
|
|
|
self.nodes[0].spork("SPORK_2_INSTANTSEND_ENABLED", 4070908800)
|
2019-03-19 11:55:51 +01:00
|
|
|
# Now the TXs should be included
|
|
|
|
self.nodes[0].generate(1)
|
2019-07-09 16:50:08 +02:00
|
|
|
self.nodes[0].spork("SPORK_2_INSTANTSEND_ENABLED", 0)
|
2019-03-19 11:55:51 +01:00
|
|
|
# Assert that TXs got included now
|
|
|
|
for txid in txs:
|
|
|
|
tx = self.nodes[0].getrawtransaction(txid, 1)
|
|
|
|
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
|
2019-05-06 16:58:38 +02:00
|
|
|
reconnect_isolated_node(self.nodes[0], 1)
|
2019-09-27 20:21:22 +02:00
|
|
|
self.wait_for_chainlock(self.nodes[0], self.nodes[0].getbestblockhash(), 30)
|
2019-03-19 11:55:51 +01:00
|
|
|
|
2019-01-23 07:08:13 +01:00
|
|
|
def wait_for_chainlock_tip_all_nodes(self):
|
|
|
|
for node in self.nodes:
|
|
|
|
tip = node.getbestblockhash()
|
|
|
|
self.wait_for_chainlock(node, tip)
|
|
|
|
|
|
|
|
def wait_for_chainlock_tip(self, node):
|
|
|
|
tip = node.getbestblockhash()
|
|
|
|
self.wait_for_chainlock(node, tip)
|
|
|
|
|
2019-09-27 20:21:22 +02:00
|
|
|
def wait_for_chainlock(self, node, block_hash, timeout=15):
|
2019-08-28 13:51:59 +02:00
|
|
|
t = time.time()
|
2019-09-27 20:21:22 +02:00
|
|
|
while time.time() - t < timeout:
|
2019-01-23 07:08:13 +01:00
|
|
|
try:
|
|
|
|
block = node.getblock(block_hash)
|
2019-02-27 18:42:06 +01:00
|
|
|
if block["confirmations"] > 0 and block["chainlock"]:
|
2019-01-23 07:08:13 +01:00
|
|
|
return
|
|
|
|
except:
|
|
|
|
# block might not be on the node yet
|
|
|
|
pass
|
2019-08-28 13:51:59 +02:00
|
|
|
time.sleep(0.1)
|
2019-01-23 07:08:13 +01:00
|
|
|
raise AssertionError("wait_for_chainlock timed out")
|
|
|
|
|
2019-03-19 11:55:51 +01:00
|
|
|
def create_chained_txs(self, node, amount):
|
|
|
|
txid = node.sendtoaddress(node.getnewaddress(), amount)
|
|
|
|
tx = node.getrawtransaction(txid, 1)
|
|
|
|
inputs = []
|
|
|
|
valueIn = 0
|
|
|
|
for txout in tx["vout"]:
|
|
|
|
inputs.append({"txid": txid, "vout": txout["n"]})
|
|
|
|
valueIn += txout["value"]
|
|
|
|
outputs = {
|
|
|
|
node.getnewaddress(): round(float(valueIn) - 0.0001, 6)
|
|
|
|
}
|
|
|
|
|
|
|
|
rawtx = node.createrawtransaction(inputs, outputs)
|
|
|
|
rawtx = node.signrawtransaction(rawtx)
|
|
|
|
rawtxid = node.sendrawtransaction(rawtx["hex"])
|
|
|
|
|
|
|
|
return [txid, rawtxid]
|
|
|
|
|
2019-01-23 07:08:13 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
LLMQChainLocksTest().main()
|