From c14013755c4f8ca6aba895dc64bd3586f406f214 Mon Sep 17 00:00:00 2001 From: "ollie.j" Date: Fri, 9 Aug 2024 14:46:57 +0900 Subject: [PATCH] reward: Optimize SupplyManager.GetAccReward --- reward/supply_manager.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/reward/supply_manager.go b/reward/supply_manager.go index e8b0482d3..85301ea9a 100644 --- a/reward/supply_manager.go +++ b/reward/supply_manager.go @@ -389,12 +389,20 @@ func (sm *supplyManager) getAccRewardUncached(num uint64) (*database.AccReward, // Trace back to the last stored accumulated reward. var fromNum uint64 var fromAcc *database.AccReward - for i := uint64(1); i < supplyReaccLimit; i++ { - accReward = sm.db.ReadAccReward(num - i) - if accReward != nil { - fromNum = num - i - fromAcc = accReward - break + + // Fast path using checkpointInterval + if accReward := sm.db.ReadAccReward(num - num%sm.checkpointInterval); accReward != nil { + fromNum = num - num%sm.checkpointInterval + fromAcc = accReward + } else { + // Slow path in case the checkpoint has changed or checkpoint is missing. + for i := uint64(1); i < supplyReaccLimit; i++ { + accReward = sm.db.ReadAccReward(num - i) + if accReward != nil { + fromNum = num - i + fromAcc = accReward + break + } } } if fromAcc == nil { @@ -408,6 +416,8 @@ func (sm *supplyManager) getAccRewardUncached(num uint64) (*database.AccReward, // accumulateReward calculates the total supply from the last block to the current block. // Given supply at `from` is `fromSupply`, calculate the supply until `to`, inclusive. // If `write` is true, the result will be written to the database. +// If `write` is false, the result will not be written to the database, +// to prevent overwriting LastAccRewardBlockNumber (essentially rollback) and to keep the disk size small (only store at checkpointInterval). func (sm *supplyManager) accumulateReward(from, to uint64, fromAcc *database.AccReward, write bool) (*database.AccReward, error) { accReward := fromAcc.Copy() // make a copy because we're updating it in-place.