From 4fe79bc7fa4d96611a973377117037374806f042 Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Thu, 2 Jun 2022 17:01:20 +0400 Subject: [PATCH] chain: take into account the compaction depth. --- lib/blockchain/chain.js | 2 +- test/chain-tree-compaction-test.js | 82 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 00975afa50..e22d54ca81 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -146,7 +146,7 @@ class Chain extends AsyncEmitter { const {compactionHeight} = await this.db.getTreeState(); const {compactTreeInitInterval} = this.options; - const compactFrom = compactionHeight + compactTreeInitInterval; + const compactFrom = compactionHeight + keepBlocks + compactTreeInitInterval; if (compactFrom > this.height) { this.logger.debug( diff --git a/test/chain-tree-compaction-test.js b/test/chain-tree-compaction-test.js index 8ce9a1ad4a..0547da7b3a 100644 --- a/test/chain-tree-compaction-test.js +++ b/test/chain-tree-compaction-test.js @@ -761,6 +761,88 @@ describe('Tree Compacting', function() { await node.close(); }); + it('should recompact tree if tree init interval passed', async () => { + const {keepBlocks} = network.block; + const compactInterval = keepBlocks; + const nodeOptions = { + prefix, + network: 'regtest', + memory: false, + compactTreeOnInit: true, + compactTreeInitInterval: compactInterval + }; + + node = new FullNode(nodeOptions); + + await node.ensure(); + + let compacted = false; + const compactWrapper = (node) => { + const compactTree = node.chain.compactTree.bind(node.chain); + + node.chain.compactTree = () => { + compacted = true; + return compactTree(); + }; + }; + + compactWrapper(node); + await node.open(); + assert.strictEqual(compacted, false); + + // get enough blocks for the compaction check. + let blocks = compactInterval + keepBlocks + 1; + let waiter = forEventCondition(node, 'connect', e => e.height >= blocks); + + await node.rpc.generateToAddress( + [blocks, new Address().toString('regtest')] + ); + + await waiter; + await node.close(); + + // Should compact, because we have enough blocks. + node = new FullNode(nodeOptions); + compactWrapper(node); + + await node.open(); + assert.strictEqual(compacted, true); + + // setup interval - 1 blocks for next test. + blocks = compactInterval + network.names.treeInterval - 1; + waiter = forEvent(node, 'connect', blocks); + await node.rpc.generateToAddress( + [blocks, new Address().toString('regtest')] + ); + await waiter; + await node.close(); + + // Should not recompact because interval has not passed. + compacted = false; + node = new FullNode(nodeOptions); + compactWrapper(node); + + await node.open(); + assert.strictEqual(compacted, false); + + waiter = forEvent(node, 'connect'); + // commit 1 + treeInterval to be sure we need to recompact. + await node.rpc.generateToAddress( + [1, new Address().toString('regtest')] + ); + await waiter; + await node.close(); + + // Should recompact because interval has passed + compacted = false; + node = new FullNode(nodeOptions); + compactWrapper(node); + + await node.open(); + assert.strictEqual(compacted, true); + await node.close(); + }); + it('should compact tree on launch (disk sizes)', async () => { this.timeout(4000); // Fresh start