remove cleanupBlocks from witnesses contract and move to produceBlock
This commit is contained in:
parent
7e8dae803d
commit
549df80bc7
8
app.js
8
app.js
|
@ -196,12 +196,8 @@ const initLightNode = async () => {
|
|||
}
|
||||
console.log('Initializing light node - this may take a while..');
|
||||
|
||||
// get the last verified block
|
||||
const params = await database.findOne({ contract: 'witnesses', table: 'params', query: {} });
|
||||
if (params && params.lastVerifiedBlockNumber) {
|
||||
// cleanup already verified blocks
|
||||
await database.cleanupBlocks(params.lastVerifiedBlockNumber - 1);
|
||||
}
|
||||
// cleanup already verified blocks
|
||||
await database.cleanupBlocks();
|
||||
// cleanup transactions
|
||||
await database.cleanupTransactions();
|
||||
};
|
||||
|
|
|
@ -814,9 +814,6 @@ actions.proposeRound = async (payload) => {
|
|||
|
||||
// calculate new schedule
|
||||
await manageWitnessesSchedule();
|
||||
|
||||
// cleanup blocks unneeded by light nodes
|
||||
await api.db.cleanupBlocks(lastVerifiedBlockNumber - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -104,8 +104,20 @@ class Block {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Try cleaning up blocks after every 100 blocks if node is a light node.
|
||||
* @param database
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async cleanupBlocks(database) {
|
||||
if (this.blockNumber % 100 === 0) {
|
||||
await database.cleanupBlocks();
|
||||
}
|
||||
}
|
||||
|
||||
// produce the block (deploy a smart contract or execute a smart contract)
|
||||
async produceBlock(database, jsVMTimeout, mainBlock) {
|
||||
await this.cleanupBlocks(database);
|
||||
await this.blockAdjustments(database);
|
||||
|
||||
const nbTransactions = this.transactions.length;
|
||||
|
|
|
@ -1029,15 +1029,18 @@ class Database {
|
|||
|
||||
/**
|
||||
* Used by light nodes to cleanup (unneeded) blocks already verified
|
||||
* by witnesses <= @cleanupUntilBlock
|
||||
* @param cleanupUntilBlock
|
||||
* by witnesses <= lastVerifiedBlockNumber - blocksToKeep
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async cleanupBlocks(cleanupUntilBlock) {
|
||||
async cleanupBlocks() {
|
||||
if (!this.lightNode) {
|
||||
return;
|
||||
}
|
||||
await this.chain.deleteMany({ $and: [{ _id: { $gt: 0 } }, { _id: { $lte: cleanupUntilBlock - this.blocksToKeep } }] }, { session: this.session });
|
||||
const params = await this.findOne({ contract: 'witnesses', table: 'params', query: {} });
|
||||
if (params && params.lastVerifiedBlockNumber) {
|
||||
const cleanupUntilBlock = params.lastVerifiedBlockNumber - 1 - this.blocksToKeep;
|
||||
await this.chain.deleteMany({ $and: [{ _id: { $gt: 0 } }, { _id: { $lte: cleanupUntilBlock } }] }, { session: this.session });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -351,8 +351,6 @@ class SmartContracts {
|
|||
tableExists: table => SmartContracts.tableExists(database, contract, table),
|
||||
// get block information
|
||||
getBlockInfo: blockNum => SmartContracts.getBlockInfo(database, blockNum),
|
||||
// cleanup already verified blocks not needed by light nodes anymore
|
||||
cleanupBlocks: lastVerifiedBlockNumber => SmartContracts.cleanupBlocks(database, lastVerifiedBlockNumber),
|
||||
};
|
||||
|
||||
// logs used to store events or errors
|
||||
|
@ -857,12 +855,6 @@ class SmartContracts {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
static async cleanupBlocks(database, lastVerifiedBlockNumber) {
|
||||
const result = await database.cleanupBlocks(lastVerifiedBlockNumber);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.SmartContracts = SmartContracts;
|
||||
|
|
Loading…
Reference in New Issue