diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts index ea76b84517aa7314909d3615b24dc4e0203de852..7298bdcb66872c3014c054d585c5c21bcf7adfc1 100644 --- a/app/lib/blockchain/DuniterBlockchain.ts +++ b/app/lib/blockchain/DuniterBlockchain.ts @@ -179,13 +179,13 @@ export class DuniterBlockchain { return { index, HEAD } } - static async pushTheBlock(obj:BlockDTO, index:IndexEntry[], HEAD:DBHead | null, conf:ConfDTO, dal:FileDAL, logger:any) { + static async pushTheBlock(obj:BlockDTO, index:IndexEntry[], HEAD:DBHead | null, conf:ConfDTO, dal:FileDAL, 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); @@ -206,7 +206,7 @@ export class DuniterBlockchain { // await supra.recordIndex(index) } - static async saveBlockData(current:DBBlock|null, block:BlockDTO, conf:ConfDTO, dal:FileDAL, logger:any, index:IndexEntry[], HEAD:DBHead | null) { + static async saveBlockData(current:DBBlock|null, block:BlockDTO, conf:ConfDTO, dal:FileDAL, logger:any, index:IndexEntry[], HEAD:DBHead | null, trim: boolean) { if (block.number == 0) { await this.saveParametersForRoot(block, conf, dal); } @@ -229,20 +229,22 @@ export class DuniterBlockchain { // 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.archiveBlocks() - 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.archiveBlocks() + 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 07deaee845ec3b9eef46207dfd4b3df5608cdfc6..b99cb96c827f08ef95c7fafc291ab691892f355a 100644 --- a/app/lib/computation/BlockchainContext.ts +++ b/app/lib/computation/BlockchainContext.ts @@ -117,8 +117,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<BlockDTO> { - const block = await DuniterBlockchain.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<BlockDTO> { + const block = await DuniterBlockchain.pushTheBlock(obj, index, HEAD, this.conf, this.dal, this.logger, trim) this.vHEAD_1 = this.vHEAD = null return block } @@ -154,9 +154,9 @@ export class BlockchainContext { } } - 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/lib/computation/QuickSync.ts b/app/lib/computation/QuickSync.ts index 049b057f993a842bf1f4c6f6d3151bc532d6a1d0..05d2168236db44b0f1cca3f07a94e51be1c37b72 100644 --- a/app/lib/computation/QuickSync.ts +++ b/app/lib/computation/QuickSync.ts @@ -130,11 +130,8 @@ export class QuickSynchronizer { const local_cindex = Indexer.cindex(index); const local_sindex = Indexer.sindex(index); const local_mindex = Indexer.mindex(index); - sync_iindex = sync_iindex.concat(local_iindex); - sync_cindex = sync_cindex.concat(local_cindex); - sync_mindex = sync_mindex.concat(local_mindex); - const HEAD = await Indexer.quickCompleteGlobalScope(block, sync_currConf, sync_bindex, sync_iindex, sync_mindex, sync_cindex, this.dal) + const HEAD = await Indexer.quickCompleteGlobalScope(block, sync_currConf, sync_bindex, local_iindex, local_mindex, local_cindex, this.dal) sync_bindex.push(HEAD); // Remember expiration dates @@ -183,12 +180,12 @@ export class QuickSynchronizer { await this.dal.iindexDAL.insertBatch(sync_iindex); await this.dal.sindexDAL.insertBatch(sync_sindex); await this.dal.cindexDAL.insertBatch(sync_cindex); - sync_mindex = []; - sync_iindex = []; - sync_cindex = []; - sync_sindex = local_sindex; + sync_iindex = local_iindex + sync_cindex = local_cindex + sync_mindex = local_mindex + sync_sindex = local_sindex - sync_sindex = sync_sindex.concat(await Indexer.ruleIndexGenDividend(HEAD, this.dal)); + sync_sindex = sync_sindex.concat(await Indexer.ruleIndexGenDividend(HEAD, local_iindex, this.dal)); sync_sindex = sync_sindex.concat(await Indexer.ruleIndexGarbageSmallAccounts(HEAD, sync_sindex, sync_memoryDAL)); if (nextExpiringChanged) { sync_cindex = sync_cindex.concat(await Indexer.ruleIndexGenCertificationExpiry(HEAD, this.dal)); @@ -200,21 +197,26 @@ export class QuickSynchronizer { // Update balances with UD + local garbagings await DuniterBlockchain.updateWallets(sync_sindex, sync_memoryDAL) - // --> Update links - await this.dal.updateWotbLinks(local_cindex.concat(sync_cindex)); - - // Flush the INDEX again - await this.dal.mindexDAL.insertBatch(sync_mindex); + // Flush the INDEX again (needs to be done *before* the update of wotb links because of block#0) await this.dal.iindexDAL.insertBatch(sync_iindex); + await this.dal.mindexDAL.insertBatch(sync_mindex); await this.dal.sindexDAL.insertBatch(sync_sindex); await this.dal.cindexDAL.insertBatch(sync_cindex); - sync_mindex = []; + + // --> Update links + await this.dal.updateWotbLinks(local_cindex.concat(sync_cindex)); sync_iindex = []; + sync_mindex = []; sync_cindex = []; sync_sindex = []; // Create/Update nodes in wotb await DuniterBlockchain.updateMembers(block, this.dal) + } else { + // Concat the results to the pending data + sync_iindex = sync_iindex.concat(local_iindex); + sync_cindex = sync_cindex.concat(local_cindex); + sync_mindex = sync_mindex.concat(local_mindex); } // Trim the bindex diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts index 99756afbe9c4544024e3eba5f020f3edf503269e..b9e1cc9ef8139b94223ba92e920a621c38a439b4 100644 --- a/app/lib/dal/fileDAL.ts +++ b/app/lib/dal/fileDAL.ts @@ -959,7 +959,7 @@ export class FileDAL { let iindex = indexer.iindex(index); let sindex = indexer.sindex(index); let cindex = indexer.cindex(index); - sindex = sindex.concat(await indexer.ruleIndexGenDividend(HEAD, this)); + sindex = sindex.concat(await indexer.ruleIndexGenDividend(HEAD, iindex, this)); sindex = sindex.concat(await indexer.ruleIndexGarbageSmallAccounts(HEAD, sindex, this)); cindex = cindex.concat(await indexer.ruleIndexGenCertificationExpiry(HEAD, this)); mindex = mindex.concat(await indexer.ruleIndexGenMembershipExpiry(HEAD, this)); diff --git a/app/lib/indexer.ts b/app/lib/indexer.ts index c93ac3851b812e6decd1a61e1815ed051311f074..730b427557227a5ce76d2584ce592403c1e60188 100644 --- a/app/lib/indexer.ts +++ b/app/lib/indexer.ts @@ -1628,10 +1628,10 @@ export class Indexer { } // BR_G91 - static async ruleIndexGenDividend(HEAD: DBHead, dal:FileDAL) { + static async ruleIndexGenDividend(HEAD: DBHead, local_iindex: IindexEntry[], dal: FileDAL) { const dividends = []; if (HEAD.new_dividend) { - const members = await dal.iindexDAL.getMembersPubkeys() + const members = (await dal.iindexDAL.getMembersPubkeys()).concat(local_iindex.filter(i => i.member)) for (const MEMBER of members) { dividends.push({ index: constants.S_INDEX, diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts index af71c2658c7eb1068a11eb2d59b1e9439fd94624..5ec97e4e2026f5bdb0a18d307c748719dc47fa77 100644 --- a/app/service/BlockchainService.ts +++ b/app/service/BlockchainService.ts @@ -117,7 +117,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) diff --git a/doc/Protocol.md b/doc/Protocol.md index 7e99fcb56a5d13de07c59f9716986213b67f229e..84503c94495c4bde42a7e461946e01040c3c2e86 100644 --- a/doc/Protocol.md +++ b/doc/Protocol.md @@ -2678,6 +2678,21 @@ For each `REDUCE_BY(GLOBAL_IINDEX[member=true], 'pub') as IDTY` then if `IDTY.me consumed = false ) +For each `LOCAL_IINDEX[member=true] as IDTY` add a new LOCAL_SINDEX entry: + + SINDEX ( + op = 'CREATE' + identifier = IDTY.pub + pos = HEAD.number + written_on = BLOCKSTAMP + written_time = MedianTime + amount = HEAD.dividend + base = HEAD.unitBase + locktime = null + conditions = REQUIRE_SIG(MEMBER.pub) + consumed = false + ) + ###### BR_G106 - Low accounts Set: