Skip to content

Commit

Permalink
Merge pull request #896 from ainblockchain/feature/platfowner/feature
Browse files Browse the repository at this point in the history
Improve deployment related code and scripts
  • Loading branch information
platfowner authored Jan 10, 2022
2 parents 00f671b + f374ecc commit 9c208a4
Show file tree
Hide file tree
Showing 16 changed files with 630 additions and 558 deletions.
2 changes: 1 addition & 1 deletion block-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ class BlockPool {
return validLastVotes;
}
tempDb.destroyDb();
throw Error(`[${LOG_HEADER}] lastBlock doesn't have enough votes`);
throw Error(`[${LOG_HEADER}] lastBlock doesn't have enough votes: ${lastBlock.number}`);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions blockchain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,18 @@ class Blockchain {
}

writeBlock(block) {
const LOG_HEADER = 'writeBlock';

if (FileUtil.hasBlockFile(this.blockchainPath, block)) {
logger.error(
`[${LOG_HEADER}] Overwriting block file for block ${block.number} of hash ${block.hash}`);
}
FileUtil.writeBlockFile(this.blockchainPath, block);

if (FileUtil.hasH2nFile(this.blockchainPath, block.hash)) {
logger.error(
`[${LOG_HEADER}] Overwriting h2n file for block ${block.number} of hash ${block.hash}`);
}
FileUtil.writeH2nFile(this.blockchainPath, block.hash, block.number);
}

Expand Down
69 changes: 44 additions & 25 deletions common/file-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ class FileUtil {
}
}

static hasBlockFile(chainPath, blockNumber) {
const blockPath = FileUtil.getBlockPath(chainPath, blockNumber);
return fs.existsSync(blockPath);
}

static writeH2nFile(chainPath, blockHash, blockNumber) {
const LOG_HEADER = 'writeH2nFile';

Expand Down Expand Up @@ -241,46 +246,60 @@ class FileUtil {
}
}

static hasH2nFile(chainPath, blockHash) {
const h2nPath = FileUtil.getH2nPath(chainPath, blockHash);
return fs.existsSync(h2nPath);
}

static readH2nFile(chainPath, blockHash) {
const LOG_HEADER = 'readH2nFile';
const h2nPath = FileUtil.getH2nPath(chainPath, blockHash);
try {
const h2nPath = FileUtil.getH2nPath(chainPath, blockHash);
return Number(fs.readFileSync(h2nPath).toString());
} catch (err) {
logger.error(`[${LOG_HEADER}] Error while reading ${filePath}: ${err}`);
logger.error(`[${LOG_HEADER}] Error while reading ${h2nPath}: ${err}`);
return -1;
}
}

static async writeSnapshot(snapshotPath, blockNumber, snapshot, snapshotChunkSize, isDebug = false) {
const LOG_HEADER = 'writeSnapshot';
static async writeSnapshotFile(
snapshotPath, blockNumber, snapshot, snapshotChunkSize, isDebug = false) {
const LOG_HEADER = 'writeSnapshotFile';

const filePath = FileUtil.getSnapshotPathByBlockNumber(snapshotPath, blockNumber, isDebug);
if (snapshot === null) { // Delete
if (fs.existsSync(filePath)) {
try {
fs.unlinkSync(filePath);
} catch (err) {
logger.error(`[${LOG_HEADER}] Failed to delete ${filePath}: ${err.stack}`);
}
return new Promise((resolve) => {
new JsonStreamStringify({ docs: ObjectUtil.toChunks(snapshot, snapshotChunkSize) })
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream(filePath, { flags: 'w' }))
.on('finish', () => {
logger.debug(`[${LOG_HEADER}] Snapshot written at ${filePath}`);
resolve();
})
.on('error', (e) => {
logger.error(`[${LOG_HEADER}] Failed to write snapshot at ${filePath}: ${e}`);
resolve();
});
});
}

static deleteSnapshotFile(snapshotPath, blockNumber, isDebug = false) {
const LOG_HEADER = 'deleteSnapshotFile';

const filePath = FileUtil.getSnapshotPathByBlockNumber(snapshotPath, blockNumber, isDebug);
if (fs.existsSync(filePath)) {
try {
fs.unlinkSync(filePath);
} catch (err) {
logger.error(`[${LOG_HEADER}] Failed to delete snapshot at ${filePath}: ${err.stack}`);
}
} else {
return new Promise((resolve) => {
new JsonStreamStringify({ docs: ObjectUtil.toChunks(snapshot, snapshotChunkSize) })
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream(filePath, { flags: 'w' }))
.on('finish', () => {
logger.debug(`[${LOG_HEADER}] Snapshot written at ${filePath}`);
resolve();
})
.on('error', (e) => {
logger.error(`[${LOG_HEADER}] Failed to write snapshot at ${filePath}: ${e}`);
resolve();
});
});
}
}

static hasSnapshotFile(snapshotPath, blockNumber, isDebug = false) {
const filePath = FileUtil.getSnapshotPathByBlockNumber(snapshotPath, blockNumber, isDebug);
return fs.existsSync(filePath);
}

static getAccountFromKeystoreFile(keystorePath, password) {
const keystore = JSON.parse(fs.readFileSync(keystorePath));
return ainUtil.privateToAccount(ainUtil.v3KeystoreToPrivate(keystore, password));
Expand Down
2 changes: 1 addition & 1 deletion consensus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ class Consensus {
const snapshot = node.buildBlockchainSnapshot(number, db.stateRoot);
const snapshotChunkSize = node.getBlockchainParam('resource/snapshot_chunk_size');
// NOTE(liayoo): This write is not awaited.
FileUtil.writeSnapshot(node.snapshotDir, number, snapshot, snapshotChunkSize, true);
FileUtil.writeSnapshotFile(node.snapshotDir, number, snapshot, snapshotChunkSize, true);
}
throw new ConsensusError({
code: ConsensusErrorCode.INVALID_STATE_PROOF_HASH,
Expand Down
Loading

0 comments on commit 9c208a4

Please sign in to comment.