Changeset View
Changeset View
Standalone View
Standalone View
test/functional/feature_pruning.py
Show All 36 Lines | def mine_large_blocks(node, n): | ||||
# different for each block created => blockhash is unique. | # different for each block created => blockhash is unique. | ||||
if "nTimes" not in mine_large_blocks.__dict__: | if "nTimes" not in mine_large_blocks.__dict__: | ||||
mine_large_blocks.nTime = 0 | mine_large_blocks.nTime = 0 | ||||
# Get the block parameters for the first block | # Get the block parameters for the first block | ||||
big_script = CScript([OP_RETURN] + [OP_NOP] * 950000) | big_script = CScript([OP_RETURN] + [OP_NOP] * 950000) | ||||
best_block = node.getblock(node.getbestblockhash()) | best_block = node.getblock(node.getbestblockhash()) | ||||
height = int(best_block["height"]) + 1 | height = int(best_block["height"]) + 1 | ||||
mine_large_blocks.nTime = max( | mine_large_blocks.nTime = max(mine_large_blocks.nTime, int(best_block["time"])) + 1 | ||||
mine_large_blocks.nTime, int(best_block["time"])) + 1 | |||||
previousblockhash = int(best_block["hash"], 16) | previousblockhash = int(best_block["hash"], 16) | ||||
for _ in range(n): | for _ in range(n): | ||||
# Build the coinbase transaction (with large scriptPubKey) | # Build the coinbase transaction (with large scriptPubKey) | ||||
coinbase_tx = create_coinbase(height) | coinbase_tx = create_coinbase(height) | ||||
coinbase_tx.vin[0].nSequence = 2 ** 32 - 1 | coinbase_tx.vin[0].nSequence = 2**32 - 1 | ||||
coinbase_tx.vout[0].scriptPubKey = big_script | coinbase_tx.vout[0].scriptPubKey = big_script | ||||
coinbase_tx.rehash() | coinbase_tx.rehash() | ||||
# Build the block | # Build the block | ||||
block = CBlock() | block = CBlock() | ||||
block.nVersion = best_block["version"] | block.nVersion = best_block["version"] | ||||
block.hashPrevBlock = previousblockhash | block.hashPrevBlock = previousblockhash | ||||
block.nTime = mine_large_blocks.nTime | block.nTime = mine_large_blocks.nTime | ||||
block.nBits = int('207fffff', 16) | block.nBits = int("207fffff", 16) | ||||
block.nNonce = 0 | block.nNonce = 0 | ||||
block.vtx = [coinbase_tx] | block.vtx = [coinbase_tx] | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
block.solve() | block.solve() | ||||
# Submit to the node | # Submit to the node | ||||
node.submitblock(ToHex(block)) | node.submitblock(ToHex(block)) | ||||
previousblockhash = block.sha256 | previousblockhash = block.sha256 | ||||
height += 1 | height += 1 | ||||
mine_large_blocks.nTime += 1 | mine_large_blocks.nTime += 1 | ||||
def calc_usage(blockdir): | def calc_usage(blockdir): | ||||
return sum(os.path.getsize(blockdir + f) for f in os.listdir(blockdir) | return sum( | ||||
if os.path.isfile(os.path.join(blockdir, f))) / (1024. * 1024.) | os.path.getsize(blockdir + f) | ||||
for f in os.listdir(blockdir) | |||||
if os.path.isfile(os.path.join(blockdir, f)) | |||||
) / (1024.0 * 1024.0) | |||||
class PruneTest(BitcoinTestFramework): | class PruneTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 6 | self.num_nodes = 6 | ||||
self.supports_cli = False | self.supports_cli = False | ||||
# Create nodes 0 and 1 to mine. | # Create nodes 0 and 1 to mine. | ||||
# Create node 2 to test pruning. | # Create node 2 to test pruning. | ||||
self.full_node_default_args = ["-maxreceivebuffer=20000", "-blockmaxsize=999000", | self.full_node_default_args = [ | ||||
"-checkblocks=5", "-noparkdeepreorg"] | "-maxreceivebuffer=20000", | ||||
"-blockmaxsize=999000", | |||||
"-checkblocks=5", | |||||
"-noparkdeepreorg", | |||||
] | |||||
# Create nodes 3 and 4 to test manual pruning (they will be re-started with manual pruning later) | # Create nodes 3 and 4 to test manual pruning (they will be re-started with manual pruning later) | ||||
# Create nodes 5 to test wallet in prune mode, but do not connect | # Create nodes 5 to test wallet in prune mode, but do not connect | ||||
self.extra_args = [self.full_node_default_args, | self.extra_args = [ | ||||
self.full_node_default_args, | self.full_node_default_args, | ||||
["-maxreceivebuffer=20000", "-prune=550", | self.full_node_default_args, | ||||
"-noparkdeepreorg"], | ["-maxreceivebuffer=20000", "-prune=550", "-noparkdeepreorg"], | ||||
["-maxreceivebuffer=20000", "-blockmaxsize=999000", | ["-maxreceivebuffer=20000", "-blockmaxsize=999000", "-noparkdeepreorg"], | ||||
"-noparkdeepreorg"], | ["-maxreceivebuffer=20000", "-blockmaxsize=999000", "-noparkdeepreorg"], | ||||
["-maxreceivebuffer=20000", "-blockmaxsize=999000", | ["-prune=550"], | ||||
"-noparkdeepreorg"], | ] | ||||
["-prune=550"]] | |||||
self.rpc_timeout = 120 | self.rpc_timeout = 120 | ||||
def skip_test_if_missing_module(self): | def skip_test_if_missing_module(self): | ||||
self.skip_if_no_wallet() | self.skip_if_no_wallet() | ||||
def setup_network(self): | def setup_network(self): | ||||
self.setup_nodes() | self.setup_nodes() | ||||
self.prunedir = os.path.join( | self.prunedir = os.path.join(self.nodes[2].datadir, self.chain, "blocks", "") | ||||
self.nodes[2].datadir, self.chain, 'blocks', '') | |||||
self.connect_nodes(0, 1) | self.connect_nodes(0, 1) | ||||
self.connect_nodes(1, 2) | self.connect_nodes(1, 2) | ||||
self.connect_nodes(0, 2) | self.connect_nodes(0, 2) | ||||
self.connect_nodes(0, 3) | self.connect_nodes(0, 3) | ||||
self.connect_nodes(0, 4) | self.connect_nodes(0, 4) | ||||
self.sync_blocks(self.nodes[0:5]) | self.sync_blocks(self.nodes[0:5]) | ||||
def setup_nodes(self): | def setup_nodes(self): | ||||
self.add_nodes(self.num_nodes, self.extra_args) | self.add_nodes(self.num_nodes, self.extra_args) | ||||
self.start_nodes() | self.start_nodes() | ||||
self.import_deterministic_coinbase_privkeys() | self.import_deterministic_coinbase_privkeys() | ||||
def create_big_chain(self): | def create_big_chain(self): | ||||
# Start by creating some coinbases we can spend later | # Start by creating some coinbases we can spend later | ||||
self.generate( | self.generate( | ||||
self.nodes[1], 200, sync_fun=lambda: self.sync_blocks(self.nodes[0:2])) | self.nodes[1], 200, sync_fun=lambda: self.sync_blocks(self.nodes[0:2]) | ||||
) | |||||
self.generate(self.nodes[0], 150, sync_fun=self.no_op) | self.generate(self.nodes[0], 150, sync_fun=self.no_op) | ||||
# Then mine enough full blocks to create more than 550MiB of data | # Then mine enough full blocks to create more than 550MiB of data | ||||
mine_large_blocks(self.nodes[0], 645) | mine_large_blocks(self.nodes[0], 645) | ||||
self.sync_blocks(self.nodes[0:5]) | self.sync_blocks(self.nodes[0:5]) | ||||
def test_height_min(self): | def test_height_min(self): | ||||
assert os.path.isfile(os.path.join( | assert os.path.isfile( | ||||
self.prunedir, "blk00000.dat")), "blk00000.dat is missing, pruning too early" | os.path.join(self.prunedir, "blk00000.dat") | ||||
), "blk00000.dat is missing, pruning too early" | |||||
self.log.info("Success") | self.log.info("Success") | ||||
self.log.info("Though we're already using more than 550MiB, current usage: " | |||||
f"{calc_usage(self.prunedir)}") | |||||
self.log.info( | self.log.info( | ||||
"Mining 25 more blocks should cause the first block file to be pruned") | "Though we're already using more than 550MiB, current usage: " | ||||
f"{calc_usage(self.prunedir)}" | |||||
) | |||||
self.log.info( | |||||
"Mining 25 more blocks should cause the first block file to be pruned" | |||||
) | |||||
# Pruning doesn't run until we're allocating another chunk, 20 full | # Pruning doesn't run until we're allocating another chunk, 20 full | ||||
# blocks past the height cutoff will ensure this | # blocks past the height cutoff will ensure this | ||||
mine_large_blocks(self.nodes[0], 25) | mine_large_blocks(self.nodes[0], 25) | ||||
# Wait for blk00000.dat to be pruned | # Wait for blk00000.dat to be pruned | ||||
self.wait_until( | self.wait_until( | ||||
lambda: not os.path.isfile( | lambda: not os.path.isfile(os.path.join(self.prunedir, "blk00000.dat")), | ||||
os.path.join(self.prunedir, "blk00000.dat")), | timeout=30, | ||||
timeout=30) | ) | ||||
self.log.info("Success") | self.log.info("Success") | ||||
usage = calc_usage(self.prunedir) | usage = calc_usage(self.prunedir) | ||||
self.log.info(f"Usage should be below target: {usage}") | self.log.info(f"Usage should be below target: {usage}") | ||||
assert_greater_than(550, usage) | assert_greater_than(550, usage) | ||||
def create_chain_with_staleblocks(self): | def create_chain_with_staleblocks(self): | ||||
# Create stale blocks in manageable sized chunks | # Create stale blocks in manageable sized chunks | ||||
self.log.info( | self.log.info( | ||||
"Mine 24 (stale) blocks on Node 1, followed by 25 (main chain) block reorg from Node 0, for 12 rounds") | "Mine 24 (stale) blocks on Node 1, followed by 25 (main chain) block reorg" | ||||
" from Node 0, for 12 rounds" | |||||
) | |||||
for _ in range(12): | for _ in range(12): | ||||
# Disconnect node 0 so it can mine a longer reorg chain without knowing about node 1's soon-to-be-stale chain | # Disconnect node 0 so it can mine a longer reorg chain without knowing about node 1's soon-to-be-stale chain | ||||
# Node 2 stays connected, so it hears about the stale blocks and | # Node 2 stays connected, so it hears about the stale blocks and | ||||
# then reorg's when node0 reconnects | # then reorg's when node0 reconnects | ||||
self.disconnect_nodes(0, 1) | self.disconnect_nodes(0, 1) | ||||
self.disconnect_nodes(0, 2) | self.disconnect_nodes(0, 2) | ||||
# Mine 24 blocks in node 1 | # Mine 24 blocks in node 1 | ||||
mine_large_blocks(self.nodes[1], 24) | mine_large_blocks(self.nodes[1], 24) | ||||
# Reorg back with 25 block chain from node 0 | # Reorg back with 25 block chain from node 0 | ||||
mine_large_blocks(self.nodes[0], 25) | mine_large_blocks(self.nodes[0], 25) | ||||
# Create connections in the order so both nodes can see the reorg | # Create connections in the order so both nodes can see the reorg | ||||
# at the same time | # at the same time | ||||
self.connect_nodes(0, 1) | self.connect_nodes(0, 1) | ||||
self.connect_nodes(0, 2) | self.connect_nodes(0, 2) | ||||
self.sync_blocks(self.nodes[0:3]) | self.sync_blocks(self.nodes[0:3]) | ||||
self.log.info("Usage can be over target because of high stale rate: " | self.log.info( | ||||
f"{calc_usage(self.prunedir)}") | "Usage can be over target because of high stale rate: " | ||||
f"{calc_usage(self.prunedir)}" | |||||
) | |||||
def reorg_test(self): | def reorg_test(self): | ||||
# Node 1 will mine a 300 block chain starting 287 blocks back from Node | # Node 1 will mine a 300 block chain starting 287 blocks back from Node | ||||
# 0 and Node 2's tip. This will cause Node 2 to do a reorg requiring | # 0 and Node 2's tip. This will cause Node 2 to do a reorg requiring | ||||
# 288 blocks of undo data to the reorg_test chain. | # 288 blocks of undo data to the reorg_test chain. | ||||
height = self.nodes[1].getblockcount() | height = self.nodes[1].getblockcount() | ||||
self.log.info(f"Current block height: {height}") | self.log.info(f"Current block height: {height}") | ||||
Show All 23 Lines | def reorg_test(self): | ||||
self.generate(self.nodes[1], 300, sync_fun=self.no_op) | self.generate(self.nodes[1], 300, sync_fun=self.no_op) | ||||
self.log.info("Reconnect nodes") | self.log.info("Reconnect nodes") | ||||
self.connect_nodes(0, 1) | self.connect_nodes(0, 1) | ||||
self.connect_nodes(1, 2) | self.connect_nodes(1, 2) | ||||
self.sync_blocks(self.nodes[0:3], timeout=120) | self.sync_blocks(self.nodes[0:3], timeout=120) | ||||
self.log.info(f"Verify height on node 2: {self.nodes[2].getblockcount()}") | self.log.info(f"Verify height on node 2: {self.nodes[2].getblockcount()}") | ||||
self.log.info("Usage possibly still high because of stale blocks in block " | |||||
f"files: {calc_usage(self.prunedir)}") | |||||
self.log.info( | self.log.info( | ||||
"Mine 220 more large blocks so we have requisite history") | "Usage possibly still high because of stale blocks in block " | ||||
f"files: {calc_usage(self.prunedir)}" | |||||
) | |||||
self.log.info("Mine 220 more large blocks so we have requisite history") | |||||
mine_large_blocks(self.nodes[0], 220) | mine_large_blocks(self.nodes[0], 220) | ||||
self.sync_blocks(self.nodes[0:3], timeout=120) | self.sync_blocks(self.nodes[0:3], timeout=120) | ||||
usage = calc_usage(self.prunedir) | usage = calc_usage(self.prunedir) | ||||
self.log.info(f"Usage should be below target: {usage}") | self.log.info(f"Usage should be below target: {usage}") | ||||
assert_greater_than(550, usage) | assert_greater_than(550, usage) | ||||
def reorg_back(self): | def reorg_back(self): | ||||
# Verify that a block on the old main chain fork has been pruned away | # Verify that a block on the old main chain fork has been pruned away | ||||
assert_raises_rpc_error( | assert_raises_rpc_error( | ||||
-1, "Block not available (pruned data)", self.nodes[2].getblock, self.forkhash) | -1, | ||||
with self.nodes[2].assert_debug_log(expected_msgs=['block verification stopping at height', '(pruning, no data)']): | "Block not available (pruned data)", | ||||
self.nodes[2].getblock, | |||||
self.forkhash, | |||||
) | |||||
with self.nodes[2].assert_debug_log( | |||||
expected_msgs=[ | |||||
"block verification stopping at height", | |||||
"(pruning, no data)", | |||||
] | |||||
): | |||||
self.nodes[2].verifychain(checklevel=4, nblocks=0) | self.nodes[2].verifychain(checklevel=4, nblocks=0) | ||||
self.log.info( | self.log.info(f"Will need to redownload block {self.forkheight}") | ||||
f"Will need to redownload block {self.forkheight}") | |||||
# Verify that we have enough history to reorg back to the fork point. | # Verify that we have enough history to reorg back to the fork point. | ||||
# Although this is more than 288 blocks, because this chain was written | # Although this is more than 288 blocks, because this chain was written | ||||
# more recently and only its other 299 small and 220 large blocks are in | # more recently and only its other 299 small and 220 large blocks are in | ||||
# the block files after it, it is expected to still be retained. | # the block files after it, it is expected to still be retained. | ||||
self.nodes[2].getblock(self.nodes[2].getblockhash(self.forkheight)) | self.nodes[2].getblock(self.nodes[2].getblockhash(self.forkheight)) | ||||
first_reorg_height = self.nodes[2].getblockcount() | first_reorg_height = self.nodes[2].getblockcount() | ||||
Show All 11 Lines | def reorg_back(self): | ||||
# to have a more work chain than the reorg_test chain in order to | # to have a more work chain than the reorg_test chain in order to | ||||
# trigger node 2's block download logic. At this point node 2 is within | # trigger node 2's block download logic. At this point node 2 is within | ||||
# 288 blocks of the fork point so it will preserve its ability to | # 288 blocks of the fork point so it will preserve its ability to | ||||
# reorg. | # reorg. | ||||
if self.nodes[2].getblockcount() < self.mainchainheight: | if self.nodes[2].getblockcount() < self.mainchainheight: | ||||
blocks_to_mine = first_reorg_height + 1 - self.mainchainheight | blocks_to_mine = first_reorg_height + 1 - self.mainchainheight | ||||
self.log.info( | self.log.info( | ||||
"Rewind node 0 to prev main chain to mine longer chain to trigger " | "Rewind node 0 to prev main chain to mine longer chain to trigger " | ||||
f"redownload. Blocks needed: {blocks_to_mine}") | f"redownload. Blocks needed: {blocks_to_mine}" | ||||
) | |||||
self.nodes[0].invalidateblock(curchainhash) | self.nodes[0].invalidateblock(curchainhash) | ||||
assert_equal(self.nodes[0].getblockcount(), self.mainchainheight) | assert_equal(self.nodes[0].getblockcount(), self.mainchainheight) | ||||
assert_equal(self.nodes[0].getbestblockhash(), self.mainchainhash2) | assert_equal(self.nodes[0].getbestblockhash(), self.mainchainhash2) | ||||
goalbesthash = self.generate( | goalbesthash = self.generate( | ||||
self.nodes[0], blocks_to_mine, sync_fun=self.no_op)[-1] | self.nodes[0], blocks_to_mine, sync_fun=self.no_op | ||||
)[-1] | |||||
goalbestheight = first_reorg_height + 1 | goalbestheight = first_reorg_height + 1 | ||||
self.log.info( | self.log.info( | ||||
"Verify node 2 reorged back to the main chain, some blocks of which it had to redownload") | "Verify node 2 reorged back to the main chain, some blocks of which it had" | ||||
" to redownload" | |||||
) | |||||
# Wait for Node 2 to reorg to proper height | # Wait for Node 2 to reorg to proper height | ||||
self.wait_until(lambda: self.nodes[2].getblockcount() >= goalbestheight, | self.wait_until( | ||||
timeout=900) | lambda: self.nodes[2].getblockcount() >= goalbestheight, timeout=900 | ||||
) | |||||
assert_equal(self.nodes[2].getbestblockhash(), goalbesthash) | assert_equal(self.nodes[2].getbestblockhash(), goalbesthash) | ||||
# Verify we can now have the data for a block previously pruned | # Verify we can now have the data for a block previously pruned | ||||
assert_equal(self.nodes[2].getblock( | assert_equal(self.nodes[2].getblock(self.forkhash)["height"], self.forkheight) | ||||
self.forkhash)["height"], self.forkheight) | |||||
def manual_test(self, node_number, use_timestamp): | def manual_test(self, node_number, use_timestamp): | ||||
# at this point, node has 995 blocks and has not yet run in prune mode | # at this point, node has 995 blocks and has not yet run in prune mode | ||||
self.start_node(node_number) | self.start_node(node_number) | ||||
node = self.nodes[node_number] | node = self.nodes[node_number] | ||||
assert_equal(node.getblockcount(), 995) | assert_equal(node.getblockcount(), 995) | ||||
assert_raises_rpc_error(-1, "not in prune mode", | assert_raises_rpc_error(-1, "not in prune mode", node.pruneblockchain, 500) | ||||
node.pruneblockchain, 500) | |||||
# now re-start in manual pruning mode | # now re-start in manual pruning mode | ||||
self.restart_node(node_number, extra_args=["-prune=1"]) | self.restart_node(node_number, extra_args=["-prune=1"]) | ||||
node = self.nodes[node_number] | node = self.nodes[node_number] | ||||
assert_equal(node.getblockcount(), 995) | assert_equal(node.getblockcount(), 995) | ||||
def height(index): | def height(index): | ||||
if use_timestamp: | if use_timestamp: | ||||
return node.getblockheader(node.getblockhash(index))[ | return ( | ||||
"time"] + TIMESTAMP_WINDOW | node.getblockheader(node.getblockhash(index))["time"] | ||||
+ TIMESTAMP_WINDOW | |||||
) | |||||
else: | else: | ||||
return index | return index | ||||
def prune(index): | def prune(index): | ||||
ret = node.pruneblockchain(height=height(index)) | ret = node.pruneblockchain(height=height(index)) | ||||
assert_equal(ret, node.getblockchaininfo()['pruneheight']) | assert_equal(ret, node.getblockchaininfo()["pruneheight"]) | ||||
def has_block(index): | def has_block(index): | ||||
return os.path.isfile(os.path.join( | return os.path.isfile( | ||||
self.nodes[node_number].datadir, self.chain, "blocks", f"blk{index:05}.dat")) | os.path.join( | ||||
self.nodes[node_number].datadir, | |||||
self.chain, | |||||
"blocks", | |||||
f"blk{index:05}.dat", | |||||
) | |||||
) | |||||
# should not prune because chain tip of node 3 (995) < PruneAfterHeight | # should not prune because chain tip of node 3 (995) < PruneAfterHeight | ||||
# (1000) | # (1000) | ||||
assert_raises_rpc_error( | assert_raises_rpc_error( | ||||
-1, "Blockchain is too short for pruning", node.pruneblockchain, height(500)) | -1, "Blockchain is too short for pruning", node.pruneblockchain, height(500) | ||||
) | |||||
# Save block transaction count before pruning, assert value | # Save block transaction count before pruning, assert value | ||||
block1_details = node.getblock(node.getblockhash(1)) | block1_details = node.getblock(node.getblockhash(1)) | ||||
assert_equal(block1_details["nTx"], len(block1_details["tx"])) | assert_equal(block1_details["nTx"], len(block1_details["tx"])) | ||||
# mine 6 blocks so we are at height 1001 (i.e., above PruneAfterHeight) | # mine 6 blocks so we are at height 1001 (i.e., above PruneAfterHeight) | ||||
self.generate(node, 6, sync_fun=self.no_op) | self.generate(node, 6, sync_fun=self.no_op) | ||||
assert_equal(node.getblockchaininfo()["blocks"], 1001) | assert_equal(node.getblockchaininfo()["blocks"], 1001) | ||||
# Pruned block should still know the number of transactions | # Pruned block should still know the number of transactions | ||||
assert_equal(node.getblockheader(node.getblockhash(1)) | assert_equal( | ||||
["nTx"], block1_details["nTx"]) | node.getblockheader(node.getblockhash(1))["nTx"], block1_details["nTx"] | ||||
) | |||||
# negative heights should raise an exception | # negative heights should raise an exception | ||||
assert_raises_rpc_error(-8, "Negative", node.pruneblockchain, -10) | assert_raises_rpc_error(-8, "Negative", node.pruneblockchain, -10) | ||||
# height=100 too low to prune first block file so this is a no-op | # height=100 too low to prune first block file so this is a no-op | ||||
prune(100) | prune(100) | ||||
assert has_block( | assert has_block(0), "blk00000.dat is missing when should still be there" | ||||
0), "blk00000.dat is missing when should still be there" | |||||
# Does nothing | # Does nothing | ||||
node.pruneblockchain(height(0)) | node.pruneblockchain(height(0)) | ||||
assert has_block( | assert has_block(0), "blk00000.dat is missing when should still be there" | ||||
0), "blk00000.dat is missing when should still be there" | |||||
# height=500 should prune first file | # height=500 should prune first file | ||||
prune(500) | prune(500) | ||||
assert not has_block( | assert not has_block(0), "blk00000.dat is still there, should be pruned by now" | ||||
0), "blk00000.dat is still there, should be pruned by now" | assert has_block(1), "blk00001.dat is missing when should still be there" | ||||
assert has_block( | |||||
1), "blk00001.dat is missing when should still be there" | |||||
# height=650 should prune second file | # height=650 should prune second file | ||||
prune(650) | prune(650) | ||||
assert not has_block( | assert not has_block(1), "blk00001.dat is still there, should be pruned by now" | ||||
1), "blk00001.dat is still there, should be pruned by now" | |||||
# height=1000 should not prune anything more, because tip-288 is in | # height=1000 should not prune anything more, because tip-288 is in | ||||
# blk00002.dat. | # blk00002.dat. | ||||
prune(1000) | prune(1000) | ||||
assert has_block( | assert has_block(2), "blk00002.dat is still there, should be pruned by now" | ||||
2), "blk00002.dat is still there, should be pruned by now" | |||||
# advance the tip so blk00002.dat and blk00003.dat can be pruned (the | # advance the tip so blk00002.dat and blk00003.dat can be pruned (the | ||||
# last 288 blocks should now be in blk00004.dat) | # last 288 blocks should now be in blk00004.dat) | ||||
self.generate(node, 288, sync_fun=self.no_op) | self.generate(node, 288, sync_fun=self.no_op) | ||||
prune(1000) | prune(1000) | ||||
assert not has_block( | assert not has_block(2), "blk00002.dat is still there, should be pruned by now" | ||||
2), "blk00002.dat is still there, should be pruned by now" | assert not has_block(3), "blk00003.dat is still there, should be pruned by now" | ||||
assert not has_block( | |||||
3), "blk00003.dat is still there, should be pruned by now" | |||||
# stop node, start back up with auto-prune at 550 MiB, make sure still | # stop node, start back up with auto-prune at 550 MiB, make sure still | ||||
# runs | # runs | ||||
self.restart_node(node_number, extra_args=["-prune=550"]) | self.restart_node(node_number, extra_args=["-prune=550"]) | ||||
self.log.info("Success") | self.log.info("Success") | ||||
def wallet_test(self): | def wallet_test(self): | ||||
# check that the pruning node's wallet is still in good shape | # check that the pruning node's wallet is still in good shape | ||||
self.log.info("Stop and start pruning node to trigger wallet rescan") | self.log.info("Stop and start pruning node to trigger wallet rescan") | ||||
self.restart_node( | self.restart_node(2, extra_args=["-prune=550", "-noparkdeepreorg"]) | ||||
2, extra_args=["-prune=550", "-noparkdeepreorg"]) | |||||
self.log.info("Success") | self.log.info("Success") | ||||
# check that wallet loads successfully when restarting a pruned node after IBD. | # check that wallet loads successfully when restarting a pruned node after IBD. | ||||
# this was reported to fail in #7494. | # this was reported to fail in #7494. | ||||
self.log.info("Syncing node 5 to test wallet") | self.log.info("Syncing node 5 to test wallet") | ||||
self.connect_nodes(0, 5) | self.connect_nodes(0, 5) | ||||
nds = [self.nodes[0], self.nodes[5]] | nds = [self.nodes[0], self.nodes[5]] | ||||
self.sync_blocks(nds, wait=5, timeout=300) | self.sync_blocks(nds, wait=5, timeout=300) | ||||
self.restart_node( | self.restart_node(5, extra_args=["-prune=550", "-noparkdeepreorg"]) | ||||
5, extra_args=["-prune=550", "-noparkdeepreorg"]) | |||||
self.log.info("Success") | self.log.info("Success") | ||||
def run_test(self): | def run_test(self): | ||||
self.log.info("Warning! This test requires 4GB of disk space") | self.log.info("Warning! This test requires 4GB of disk space") | ||||
self.log.info("Mining a big blockchain of 995 blocks") | self.log.info("Mining a big blockchain of 995 blocks") | ||||
self.create_big_chain() | self.create_big_chain() | ||||
# Chain diagram key: | # Chain diagram key: | ||||
# * blocks on main chain | # * blocks on main chain | ||||
# +,&,$,@ blocks on other forks | # +,&,$,@ blocks on other forks | ||||
# X invalidated block | # X invalidated block | ||||
# N1 Node 1 | # N1 Node 1 | ||||
# | # | ||||
# Start by mining a simple chain that all nodes have | # Start by mining a simple chain that all nodes have | ||||
# N0=N1=N2 **...*(995) | # N0=N1=N2 **...*(995) | ||||
# stop manual-pruning node with 995 blocks | # stop manual-pruning node with 995 blocks | ||||
self.stop_node(3) | self.stop_node(3) | ||||
self.stop_node(4) | self.stop_node(4) | ||||
self.log.info( | self.log.info( | ||||
"Check that we haven't started pruning yet because we're below PruneAfterHeight") | "Check that we haven't started pruning yet because we're below" | ||||
" PruneAfterHeight" | |||||
) | |||||
self.test_height_min() | self.test_height_min() | ||||
# Extend this chain past the PruneAfterHeight | # Extend this chain past the PruneAfterHeight | ||||
# N0=N1=N2 **...*(1020) | # N0=N1=N2 **...*(1020) | ||||
self.log.info( | self.log.info( | ||||
"Check that we'll exceed disk space target if we have a very high stale block rate") | "Check that we'll exceed disk space target if we have a very high stale" | ||||
" block rate" | |||||
) | |||||
self.create_chain_with_staleblocks() | self.create_chain_with_staleblocks() | ||||
# Disconnect N0 | # Disconnect N0 | ||||
# And mine a 24 block chain on N1 and a separate 25 block chain on N0 | # And mine a 24 block chain on N1 and a separate 25 block chain on N0 | ||||
# N1=N2 **...*+...+(1044) | # N1=N2 **...*+...+(1044) | ||||
# N0 **...**...**(1045) | # N0 **...**...**(1045) | ||||
# | # | ||||
# reconnect nodes causing reorg on N1 and N2 | # reconnect nodes causing reorg on N1 and N2 | ||||
# N1=N2 **...*(1020) *...**(1045) | # N1=N2 **...*(1020) *...**(1045) | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# \ \ | # \ \ | ||||
# ++...++(1044) .. | # ++...++(1044) .. | ||||
# | # | ||||
# N0 ********************(1032) @@...@@@(1552) | # N0 ********************(1032) @@...@@@(1552) | ||||
# \ | # \ | ||||
# *...**(1320) | # *...**(1320) | ||||
self.log.info( | self.log.info( | ||||
"Test that we can rerequest a block we previously pruned if needed for a reorg") | "Test that we can rerequest a block we previously pruned if needed for a" | ||||
" reorg" | |||||
) | |||||
self.reorg_back() | self.reorg_back() | ||||
# Verify that N2 still has block 1033 on current chain (@), but not on main chain (*) | # Verify that N2 still has block 1033 on current chain (@), but not on main chain (*) | ||||
# Invalidate 1033 on current chain (@) on N2 and we should be able to reorg to | # Invalidate 1033 on current chain (@) on N2 and we should be able to reorg to | ||||
# original main chain (*), but will require redownload of some blocks | # original main chain (*), but will require redownload of some blocks | ||||
# In order to have a peer we think we can download from, must also perform this invalidation | # In order to have a peer we think we can download from, must also perform this invalidation | ||||
# on N0 and mine a new longest chain to trigger. | # on N0 and mine a new longest chain to trigger. | ||||
# Final result: | # Final result: | ||||
# N0 ********************(1032) **...****(1553) | # N0 ********************(1032) **...****(1553) | ||||
Show All 15 Lines | def run_test(self): | ||||
self.manual_test(4, use_timestamp=True) | self.manual_test(4, use_timestamp=True) | ||||
self.log.info("Test wallet re-scan") | self.log.info("Test wallet re-scan") | ||||
self.wallet_test() | self.wallet_test() | ||||
self.log.info("Done") | self.log.info("Done") | ||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
PruneTest().main() | PruneTest().main() |