Skip to content

Commit 78b9c90

Browse files
authored
Merge pull request #1310 from ainblockchain/bugfix/platfowner/bugfix
Log stack trace for file handling or node starting errors
2 parents f3380d5 + 48efb6f commit 78b9c90

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

common/file-util.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class FileUtil {
257257
});
258258
});
259259
} catch (err) {
260-
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
260+
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err.stack}`);
261261
return false;
262262
}
263263
}
@@ -288,7 +288,7 @@ class FileUtil {
288288
});
289289
});
290290
} catch (err) {
291-
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
291+
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err.stack}`);
292292
return null;
293293
}
294294
}
@@ -299,7 +299,7 @@ class FileUtil {
299299
const zippedFs = fs.readFileSync(filePath);
300300
return FileUtil.buildObjectFromChunks(JSON.parse(zlib.gunzipSync(zippedFs).toString()).docs);
301301
} catch (err) {
302-
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
302+
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err.stack}`);
303303
return null;
304304
}
305305
}
@@ -314,7 +314,7 @@ class FileUtil {
314314
const zippedFs = fs.readFileSync(filePath);
315315
return JSON.parse(zlib.gunzipSync(zippedFs).toString());
316316
} catch (err) {
317-
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
317+
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err.stack}`);
318318
return null;
319319
}
320320
}
@@ -325,7 +325,7 @@ class FileUtil {
325325
const fileStr = fs.readFileSync(filePath);
326326
return JSON.parse(fileStr);
327327
} catch (err) {
328-
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
328+
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err.stack}`);
329329
return null;
330330
}
331331
}
@@ -399,7 +399,7 @@ class FileUtil {
399399
try {
400400
return Number(fs.readFileSync(h2nPath).toString());
401401
} catch (err) {
402-
logger.error(`[${LOG_HEADER}] Error while reading ${h2nPath}: ${err}`);
402+
logger.error(`[${LOG_HEADER}] Error while reading ${h2nPath}: ${err.stack}`);
403403
return -1;
404404
}
405405
}

node/index.js

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -361,43 +361,49 @@ class BlockchainNode {
361361
startNode(isFirstNode) {
362362
const LOG_HEADER = 'startNode';
363363

364-
// 1. Initialize DB (with the latest snapshot, if it exists)
365-
logger.info(
366-
`[${LOG_HEADER}] Initializing DB with bootstrap snapshot from ${this.bootstrapSnapshotSource}..`);
367-
const startingDb = DB.create(
368-
StateVersions.EMPTY, StateVersions.START, this.bc, true, this.bootstrapSnapshotBlockNumber,
369-
this.stateManager);
370-
startingDb.initDb(this.bootstrapSnapshot);
364+
try {
365+
// 1. Initialize DB (with the latest snapshot, if it exists)
366+
logger.info(
367+
`[${LOG_HEADER}] Initializing DB with bootstrap snapshot from ${this.bootstrapSnapshotSource}..`);
368+
const startingDb = DB.create(
369+
StateVersions.EMPTY, StateVersions.START, this.bc, true, this.bootstrapSnapshotBlockNumber,
370+
this.stateManager);
371+
startingDb.initDb(this.bootstrapSnapshot);
371372

372-
// 2. Initialize the blockchain, starting from `bootstrapSnapshotBlockNumber`.
373-
logger.info(
374-
`[${LOG_HEADER}] Initializing blockchain with bootstrap snapshot from ${this.bootstrapSnapshotSource}..`);
375-
const snapshotChunkSize = this.getBlockchainParam('resource/snapshot_chunk_size');
376-
const wasBlockDirEmpty = this.bc.initBlockchain(
377-
isFirstNode, this.bootstrapSnapshot, this.snapshotDir, snapshotChunkSize);
378-
379-
// 3. Execute the chain on the DB and finalize it.
380-
logger.info(`[${LOG_HEADER}] Executing chains on DB if needed..`);
381-
const isGenesisStart = (isFirstNode && wasBlockDirEmpty);
382-
if (!wasBlockDirEmpty || isGenesisStart || NodeConfigs.SYNC_MODE === SyncModeOptions.PEER) {
383-
if (!this.loadAndExecuteChainOnDb(
384-
this.bootstrapSnapshotBlockNumber, startingDb.stateVersion, isGenesisStart)) {
385-
return false;
373+
// 2. Initialize the blockchain, starting from `bootstrapSnapshotBlockNumber`.
374+
logger.info(
375+
`[${LOG_HEADER}] Initializing blockchain with bootstrap snapshot from ${this.bootstrapSnapshotSource}..`);
376+
const snapshotChunkSize = this.getBlockchainParam('resource/snapshot_chunk_size');
377+
const wasBlockDirEmpty = this.bc.initBlockchain(
378+
isFirstNode, this.bootstrapSnapshot, this.snapshotDir, snapshotChunkSize);
379+
380+
// 3. Execute the chain on the DB and finalize it.
381+
logger.info(`[${LOG_HEADER}] Executing chains on DB if needed..`);
382+
const isGenesisStart = (isFirstNode && wasBlockDirEmpty);
383+
if (!wasBlockDirEmpty || isGenesisStart || NodeConfigs.SYNC_MODE === SyncModeOptions.PEER) {
384+
if (!this.loadAndExecuteChainOnDb(
385+
this.bootstrapSnapshotBlockNumber, startingDb.stateVersion, isGenesisStart)) {
386+
return false;
387+
}
386388
}
387-
}
388389

389-
// 4. Execute transactions from the pool.
390-
logger.info(`[${LOG_HEADER}] Executing the transaction from the tx pool..`);
391-
this.db.executeTransactionList(
392-
this.tp.getValidTransactions(null, this.stateManager.getFinalVersion()), false, true,
393-
this.bc.lastBlockNumber() + 1, this.bc.lastBlockTimestamp());
390+
// 4. Execute transactions from the pool.
391+
logger.info(`[${LOG_HEADER}] Executing the transaction from the tx pool..`);
392+
this.db.executeTransactionList(
393+
this.tp.getValidTransactions(null, this.stateManager.getFinalVersion()), false, true,
394+
this.bc.lastBlockNumber() + 1, this.bc.lastBlockTimestamp());
394395

395-
// 5. Node status changed: READY_TO_START -> CHAIN_SYNCING.
396-
this.state = BlockchainNodeStates.CHAIN_SYNCING;
397-
logger.info(`[${LOG_HEADER}] Now node in CHAIN_SYNCING state!`);
396+
// 5. Node status changed: READY_TO_START -> CHAIN_SYNCING.
397+
this.state = BlockchainNodeStates.CHAIN_SYNCING;
398+
logger.info(`[${LOG_HEADER}] Now node in CHAIN_SYNCING state!`);
398399

399-
// 6. Reset bootstrap snapshot.
400-
this.resetBootstrapSnapshot();
400+
// 6. Reset bootstrap snapshot.
401+
this.resetBootstrapSnapshot();
402+
} catch (err) {
403+
logger.error(
404+
`[${LOG_HEADER}] Failed to start node with error: ${err} (${err.stack})`);
405+
return false;
406+
}
401407

402408
return true;
403409
}

0 commit comments

Comments
 (0)