Skip to content

Commit

Permalink
chain, node: catch critical errors and shut down
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Oct 21, 2021
1 parent 6f409ea commit 4cd7381
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,10 @@ class Chain extends AsyncEmitter {
const ns = await view.getNameState(this.db, nameHash);

if (ns.isNull()) {
if (!covenant.isClaim() && !covenant.isOpen())
if (!covenant.isClaim() && !covenant.isOpen()) {
this.emit('CRITICAL ERROR', 'Unexpected null NameState.');
throw new Error('Database inconsistency.');
}

const name = covenant.get(2);
ns.set(name, height);
Expand Down Expand Up @@ -1832,7 +1834,12 @@ class Chain extends AsyncEmitter {
}

// Save block and connect inputs.
await this.db.save(entry, block, view);
try {
await this.db.save(entry, block, view);
} catch (e) {
this.emit('CRITICAL ERROR', e.message);
throw e;
}

// Expose the new state.
this.tip = entry;
Expand Down Expand Up @@ -1891,7 +1898,12 @@ class Chain extends AsyncEmitter {
entry.height, util.hex32(entry.version));
}

await this.db.save(entry, block);
try {
await this.db.save(entry, block);
} catch (e) {
this.emit('CRITICAL ERROR', e.message);
throw e;
}

this.logger.warning('Heads up: Competing chain at height %d:'
+ ' tip-height=%d competitor-height=%d'
Expand Down
6 changes: 6 additions & 0 deletions lib/node/fullnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ class FullNode extends Node {
init() {
// Bind to errors
this.chain.on('error', err => this.error(err));
this.chain.on('CRITICAL ERROR', (msg) => {
this.logger.warning(`Critical error, shutting down: ${msg}`);
this.close();
});

this.mempool.on('error', err => this.error(err));
this.pool.on('error', err => this.error(err));
this.miner.on('error', err => this.error(err));
Expand Down Expand Up @@ -326,6 +331,7 @@ class FullNode extends Node {
await this.handleClose();

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

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/node/spvnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ class SPVNode extends Node {
init() {
// Bind to errors
this.chain.on('error', err => this.error(err));
this.chain.on('CRITICAL ERROR', (msg) => {
this.logger.warning(`Critical error, shutting down: ${msg}`);
this.close();
});

this.pool.on('error', err => this.error(err));

if (this.http)
Expand Down Expand Up @@ -213,6 +218,9 @@ class SPVNode extends Node {
await this.pool.close();
await this.chain.close();
await this.handleClose();

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

/**
Expand Down

0 comments on commit 4cd7381

Please sign in to comment.