Skip to content

Commit

Permalink
node: add open/close events to node and spvnode.
Browse files Browse the repository at this point in the history
chain: reorganize getting tree compaction heights.
  • Loading branch information
nodech committed Oct 26, 2023
1 parent bb7da60 commit 5322e21
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ class Chain extends AsyncEmitter {
return this.db.close();
}

/**
* Get compaction heights.
* @returns {Promise<Object>}
*/

async getCompactionHeights() {
if (this.options.spv)
return null;

if (!this.options.compactTreeOnInit)
return null;

const {keepBlocks} = this.network.block;
const {compactionHeight} = await this.db.getTreeState();
const {compactTreeInitInterval} = this.options;
const compactFrom = compactionHeight + keepBlocks + compactTreeInitInterval;

return {
compactionHeight,
compactFrom
};
}

/**
* Check if we need to compact tree data.
* @returns {Promise<Boolean>} - Should we sync
Expand All @@ -150,9 +173,7 @@ class Chain extends AsyncEmitter {
if (this.height <= startFrom)
return true;

const {compactionHeight} = await this.db.getTreeState();
const {compactTreeInitInterval} = this.options;
const compactFrom = compactionHeight + keepBlocks + compactTreeInitInterval;
const {compactFrom} = await this.getCompactionHeights();

if (compactFrom > this.height) {
this.logger.debug(
Expand Down
2 changes: 2 additions & 0 deletions lib/node/fullnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ class FullNode extends Node {
}

this.logger.info('Node is loaded.');
this.emit('open');
}

/**
Expand Down Expand Up @@ -348,6 +349,7 @@ class FullNode extends Node {

this.logger.info('Node is closed.');
this.emit('closed');
this.emit('close');
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/node/spvnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class SPVNode extends Node {
await this.handleOpen();

this.logger.info('Node is loaded.');
this.emit('open');
}

/**
Expand Down Expand Up @@ -218,6 +219,7 @@ class SPVNode extends Node {

this.logger.info('Node is closed.');
this.emit('closed');
this.emit('close');
}

/**
Expand Down

0 comments on commit 5322e21

Please sign in to comment.