Skip to content

Commit

Permalink
reward: Optimize SupplyManager.GetAccReward
Browse files Browse the repository at this point in the history
  • Loading branch information
blukat29 committed Aug 9, 2024
1 parent 39687ed commit c140137
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions reward/supply_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.

Expand Down

0 comments on commit c140137

Please sign in to comment.