From 1b9968df676e97be1da111446b05f28dd86aaad9 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 27 Feb 2020 16:48:24 +0300 Subject: [PATCH] core: optimize CalculateClaimable() Because accumulated system fee is stored for every block, it is easy to calculate sum with just to reads. --- pkg/core/blockchain.go | 17 +++++------------ pkg/core/blockchain_test.go | 5 +---- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index b47c08e10..8011a2af8 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -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 } diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index 0f1f38888..614218737 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -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) {