core: optimize CalculateClaimable()

Because accumulated system fee is stored for every block,
it is easy to calculate sum with just to reads.
This commit is contained in:
Evgenii Stratonikov 2020-02-27 16:48:24 +03:00
parent 258d8be1dd
commit 1b9968df67
2 changed files with 6 additions and 16 deletions

View file

@ -1118,22 +1118,15 @@ func (bc *Blockchain) CalculateClaimable(value util.Fixed8, startHeight, endHeig
amount += util.Fixed8(iend-istart) * util.Fixed8(bc.generationAmount[ustart])
}
var sysFeeTotal util.Fixed8
if startHeight == 0 {
startHeight++
}
for i := startHeight; i < endHeight; i++ {
h := bc.GetHeaderHash(int(i))
b, err := bc.GetBlock(h)
if err != nil {
return 0, 0, err
}
for _, tx := range b.Transactions {
sysFeeTotal += bc.SystemFee(tx)
}
}
h := bc.GetHeaderHash(int(startHeight - 1))
feeStart := bc.getSystemFeeAmount(h)
h = bc.GetHeaderHash(int(endHeight - 1))
feeEnd := bc.getSystemFeeAmount(h)
sysFeeTotal /= 100000000
sysFeeTotal := util.Fixed8(feeEnd - feeStart)
ratio := value / 100000000
return amount * ratio, sysFeeTotal * ratio, nil
}

View file

@ -178,12 +178,9 @@ func TestGetTransaction(t *testing.T) {
func TestGetClaimable(t *testing.T) {
bc := newTestChain(t)
_, _, err := bc.CalculateClaimable(util.Fixed8FromInt64(1), 0, 2)
require.Error(t, err)
bc.generationAmount = []int{4, 3, 2, 1}
bc.decrementInterval = 2
_, err = bc.genBlocks(10)
_, err := bc.genBlocks(10)
require.NoError(t, err)
t.Run("first generation period", func(t *testing.T) {