Skip to content

Commit

Permalink
chain: take into account the compaction depth.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Jun 2, 2022
1 parent dca22e8 commit 4fe79bc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
82 changes: 82 additions & 0 deletions test/chain-tree-compaction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4fe79bc

Please sign in to comment.