Prevent dividing by zero when pool has no voted tickets.

This commit is contained in:
Jamie Holdstock 2021-06-12 10:36:01 +08:00 committed by Jamie Holdstock
parent b56dc26558
commit 5deb1abcfc

View File

@ -80,7 +80,18 @@ func updateCache(ctx context.Context, db *database.VspDatabase,
cache.Revoked = revoked
cache.BlockHeight = bestBlock.Height
cache.NetworkProportion = float32(voting) / float32(bestBlock.PoolSize)
cache.RevokedProportion = float32(revoked) / float32(voted)
// Prevent dividing by zero when pool has no voted tickets.
switch voted {
case 0:
if revoked == 0 {
cache.RevokedProportion = 0
} else {
cache.RevokedProportion = 1
}
default:
cache.RevokedProportion = float32(revoked) / float32(voted)
}
return nil
}