forked from TrueCloudLab/neoneo-go
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:
parent
258d8be1dd
commit
1b9968df67
2 changed files with 6 additions and 16 deletions
|
@ -1118,22 +1118,15 @@ func (bc *Blockchain) CalculateClaimable(value util.Fixed8, startHeight, endHeig
|
||||||
amount += util.Fixed8(iend-istart) * util.Fixed8(bc.generationAmount[ustart])
|
amount += util.Fixed8(iend-istart) * util.Fixed8(bc.generationAmount[ustart])
|
||||||
}
|
}
|
||||||
|
|
||||||
var sysFeeTotal util.Fixed8
|
|
||||||
if startHeight == 0 {
|
if startHeight == 0 {
|
||||||
startHeight++
|
startHeight++
|
||||||
}
|
}
|
||||||
for i := startHeight; i < endHeight; i++ {
|
h := bc.GetHeaderHash(int(startHeight - 1))
|
||||||
h := bc.GetHeaderHash(int(i))
|
feeStart := bc.getSystemFeeAmount(h)
|
||||||
b, err := bc.GetBlock(h)
|
h = bc.GetHeaderHash(int(endHeight - 1))
|
||||||
if err != nil {
|
feeEnd := bc.getSystemFeeAmount(h)
|
||||||
return 0, 0, err
|
|
||||||
}
|
|
||||||
for _, tx := range b.Transactions {
|
|
||||||
sysFeeTotal += bc.SystemFee(tx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sysFeeTotal /= 100000000
|
sysFeeTotal := util.Fixed8(feeEnd - feeStart)
|
||||||
ratio := value / 100000000
|
ratio := value / 100000000
|
||||||
return amount * ratio, sysFeeTotal * ratio, nil
|
return amount * ratio, sysFeeTotal * ratio, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -178,12 +178,9 @@ func TestGetTransaction(t *testing.T) {
|
||||||
func TestGetClaimable(t *testing.T) {
|
func TestGetClaimable(t *testing.T) {
|
||||||
bc := newTestChain(t)
|
bc := newTestChain(t)
|
||||||
|
|
||||||
_, _, err := bc.CalculateClaimable(util.Fixed8FromInt64(1), 0, 2)
|
|
||||||
require.Error(t, err)
|
|
||||||
|
|
||||||
bc.generationAmount = []int{4, 3, 2, 1}
|
bc.generationAmount = []int{4, 3, 2, 1}
|
||||||
bc.decrementInterval = 2
|
bc.decrementInterval = 2
|
||||||
_, err = bc.genBlocks(10)
|
_, err := bc.genBlocks(10)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
t.Run("first generation period", func(t *testing.T) {
|
t.Run("first generation period", func(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue