Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log stack trace for file handling or node starting errors #1310

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions common/file-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class FileUtil {
});
});
} catch (err) {
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err.stack}`);
return false;
}
}
Expand Down Expand Up @@ -288,7 +288,7 @@ class FileUtil {
});
});
} catch (err) {
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err.stack}`);
return null;
}
}
Expand All @@ -299,7 +299,7 @@ class FileUtil {
const zippedFs = fs.readFileSync(filePath);
return FileUtil.buildObjectFromChunks(JSON.parse(zlib.gunzipSync(zippedFs).toString()).docs);
} catch (err) {
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err.stack}`);
return null;
}
}
Expand All @@ -314,7 +314,7 @@ class FileUtil {
const zippedFs = fs.readFileSync(filePath);
return JSON.parse(zlib.gunzipSync(zippedFs).toString());
} catch (err) {
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err.stack}`);
return null;
}
}
Expand All @@ -325,7 +325,7 @@ class FileUtil {
const fileStr = fs.readFileSync(filePath);
return JSON.parse(fileStr);
} catch (err) {
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err.stack}`);
return null;
}
}
Expand Down Expand Up @@ -399,7 +399,7 @@ class FileUtil {
try {
return Number(fs.readFileSync(h2nPath).toString());
} catch (err) {
logger.error(`[${LOG_HEADER}] Error while reading ${h2nPath}: ${err}`);
logger.error(`[${LOG_HEADER}] Error while reading ${h2nPath}: ${err.stack}`);
return -1;
}
}
Expand Down
70 changes: 38 additions & 32 deletions node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,43 +361,49 @@ class BlockchainNode {
startNode(isFirstNode) {
const LOG_HEADER = 'startNode';

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

// 2. Initialize the blockchain, starting from `bootstrapSnapshotBlockNumber`.
logger.info(
`[${LOG_HEADER}] Initializing blockchain with bootstrap snapshot from ${this.bootstrapSnapshotSource}..`);
const snapshotChunkSize = this.getBlockchainParam('resource/snapshot_chunk_size');
const wasBlockDirEmpty = this.bc.initBlockchain(
isFirstNode, this.bootstrapSnapshot, this.snapshotDir, snapshotChunkSize);

// 3. Execute the chain on the DB and finalize it.
logger.info(`[${LOG_HEADER}] Executing chains on DB if needed..`);
const isGenesisStart = (isFirstNode && wasBlockDirEmpty);
if (!wasBlockDirEmpty || isGenesisStart || NodeConfigs.SYNC_MODE === SyncModeOptions.PEER) {
if (!this.loadAndExecuteChainOnDb(
this.bootstrapSnapshotBlockNumber, startingDb.stateVersion, isGenesisStart)) {
return false;
// 2. Initialize the blockchain, starting from `bootstrapSnapshotBlockNumber`.
logger.info(
`[${LOG_HEADER}] Initializing blockchain with bootstrap snapshot from ${this.bootstrapSnapshotSource}..`);
const snapshotChunkSize = this.getBlockchainParam('resource/snapshot_chunk_size');
const wasBlockDirEmpty = this.bc.initBlockchain(
isFirstNode, this.bootstrapSnapshot, this.snapshotDir, snapshotChunkSize);

// 3. Execute the chain on the DB and finalize it.
logger.info(`[${LOG_HEADER}] Executing chains on DB if needed..`);
const isGenesisStart = (isFirstNode && wasBlockDirEmpty);
if (!wasBlockDirEmpty || isGenesisStart || NodeConfigs.SYNC_MODE === SyncModeOptions.PEER) {
if (!this.loadAndExecuteChainOnDb(
this.bootstrapSnapshotBlockNumber, startingDb.stateVersion, isGenesisStart)) {
return false;
}
}
}

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

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

// 6. Reset bootstrap snapshot.
this.resetBootstrapSnapshot();
// 6. Reset bootstrap snapshot.
this.resetBootstrapSnapshot();
} catch (err) {
logger.error(
`[${LOG_HEADER}] Failed to start node with error: ${err} (${err.stack})`);
return false;
}

return true;
}
Expand Down
Loading