diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts index 6d1ca03215a8d2eefdc530e375530ea572c3d37b..48a3215e80e231c5e07bd6ba20190101edf69025 100644 --- a/app/lib/blockchain/DuniterBlockchain.ts +++ b/app/lib/blockchain/DuniterBlockchain.ts @@ -174,13 +174,13 @@ export class DuniterBlockchain extends MiscIndexedBlockchain { return { index, HEAD } } - async pushTheBlock(obj:BlockDTO, index:IndexEntry[], HEAD:DBHead | null, conf:ConfDTO, dal:any, logger:any) { + async pushTheBlock(obj: BlockDTO, index: IndexEntry[], HEAD: DBHead | null, conf: ConfDTO, dal: any, logger: any, trim = true) { const start = Date.now(); const block = BlockDTO.fromJSONObject(obj) try { const currentBlock = await dal.getCurrentBlockOrNull(); block.fork = false; - const added = await this.saveBlockData(currentBlock, block, conf, dal, logger, index, HEAD); + const added = await this.saveBlockData(currentBlock, block, conf, dal, logger, index, HEAD, trim); try { await DuniterBlockchain.pushStatsForBlocks([block], dal); @@ -201,7 +201,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain { // await supra.recordIndex(index) } - async saveBlockData(current:DBBlock, block:BlockDTO, conf:ConfDTO, dal:any, logger:any, index:IndexEntry[], HEAD:DBHead | null) { + async saveBlockData(current: DBBlock, block: BlockDTO, conf: ConfDTO, dal: any, logger: any, index: IndexEntry[], HEAD: DBHead | null, trim: boolean) { if (block.number == 0) { await this.saveParametersForRoot(block, conf, dal); } @@ -224,19 +224,21 @@ export class DuniterBlockchain extends MiscIndexedBlockchain { // Update the wallets' blances await this.updateWallets(indexes.sindex, dal) - const TAIL = await dal.bindexDAL.tail(); - const bindexSize = [ - TAIL.issuersCount, - TAIL.issuersFrame, - conf.medianTimeBlocks, - conf.dtDiffEval - ].reduce((max, value) => { - return Math.max(max, value); - }, 0); - const MAX_BINDEX_SIZE = conf.forksize + bindexSize - const currentSize = indexes.HEAD.number - TAIL.number + 1 - if (currentSize > MAX_BINDEX_SIZE) { - await dal.trimIndexes(indexes.HEAD.number - MAX_BINDEX_SIZE); + if (trim) { + const TAIL = await dal.bindexDAL.tail(); + const bindexSize = [ + TAIL.issuersCount, + TAIL.issuersFrame, + conf.medianTimeBlocks, + conf.dtDiffEval + ].reduce((max, value) => { + return Math.max(max, value); + }, 0); + const MAX_BINDEX_SIZE = conf.forksize + bindexSize + const currentSize = indexes.HEAD.number - TAIL.number + 1 + if (currentSize > MAX_BINDEX_SIZE) { + await dal.trimIndexes(indexes.HEAD.number - MAX_BINDEX_SIZE); + } } const dbb = DBBlock.fromBlockDTO(block) diff --git a/app/lib/computation/BlockchainContext.ts b/app/lib/computation/BlockchainContext.ts index 09b387a0152d830514b2d465f5a7029ce8e9b925..5a511b3e2198c061fad7e26aad53e4961eece04d 100644 --- a/app/lib/computation/BlockchainContext.ts +++ b/app/lib/computation/BlockchainContext.ts @@ -118,8 +118,8 @@ export class BlockchainContext { return DuniterBlockchain.checkBlock(block, withPoWAndSignature, this.conf, this.dal) } - private async addBlock(obj: BlockDTO, index: any = null, HEAD: DBHead | null = null): Promise<any> { - const block = await this.blockchain.pushTheBlock(obj, index, HEAD, this.conf, this.dal, this.logger) + private async addBlock(obj: BlockDTO, index: any = null, HEAD: DBHead | null = null, trim: boolean): Promise<any> { + const block = await this.blockchain.pushTheBlock(obj, index, HEAD, this.conf, this.dal, this.logger, trim) this.vHEAD_1 = this.vHEAD = this.HEADrefreshed = null return block } @@ -150,9 +150,9 @@ export class BlockchainContext { this.logger.debug('Applied block #%s', block.number); } - async checkAndAddBlock(block:BlockDTO) { + async checkAndAddBlock(block:BlockDTO, trim = true) { const { index, HEAD } = await this.checkBlock(block, constants.WITH_SIGNATURES_AND_POW); - return await this.addBlock(block, index, HEAD); + return await this.addBlock(block, index, HEAD, trim); } current(): Promise<any> { diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts index 2d4f583dd724400db42dc0c2647f566b8ab0dbc8..1337f3ee83218e721d46d77ae9141e8ac1c619c1 100644 --- a/app/service/BlockchainService.ts +++ b/app/service/BlockchainService.ts @@ -104,7 +104,7 @@ export class BlockchainService extends FIFOService { } async addBlock(block: BlockDTO): Promise<BlockDTO> { - return await this.bcService.mainContext.checkAndAddBlock(block) + return await this.bcService.mainContext.checkAndAddBlock(block, false) } })(this)