remove cleanupBlocks from witnesses contract and move to produceBlock

This commit is contained in:
primersion 2022-05-23 20:44:15 +02:00
parent 7e8dae803d
commit 549df80bc7
5 changed files with 21 additions and 21 deletions

8
app.js
View File

@ -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();
};

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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 });
}
}
/**

View File

@ -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;