Add CalculateBaseFee tests

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-08-07 03:34:50 +02:00
parent 05c527b681
commit 122ec8c9be
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA

View File

@ -10,6 +10,20 @@ import (
"golang.org/x/xerrors"
)
func computeNextBaseFee(baseFee types.BigInt, gasLimitUsed int64, noOfBlocks int) types.BigInt {
delta := gasLimitUsed/int64(noOfBlocks) - build.BlockGasTarget
change := big.Mul(baseFee, big.NewInt(delta))
change = big.Div(change, big.NewInt(build.BlockGasTarget))
change = big.Div(change, big.NewInt(build.BaseFeeMaxChangeDenom))
nextBaseFee := big.Add(baseFee, change)
if big.Cmp(nextBaseFee, big.NewInt(build.MinimumBaseFee)) < 0 {
nextBaseFee = big.NewInt(build.MinimumBaseFee)
}
return nextBaseFee
}
func (cs *ChainStore) ComputeBaseFee(ctx context.Context, ts *types.TipSet) (abi.TokenAmount, error) {
zero := abi.NewTokenAmount(0)
totalLimit := int64(0)
@ -27,16 +41,5 @@ func (cs *ChainStore) ComputeBaseFee(ctx context.Context, ts *types.TipSet) (abi
}
parentBaseFee := ts.Blocks()[0].ParentBaseFee
delta := totalLimit/int64(len(ts.Blocks())) - build.BlockGasTarget
change := big.Mul(parentBaseFee, big.NewInt(delta))
change = big.Div(change, big.NewInt(build.BlockGasTarget))
change = big.Div(change, big.NewInt(build.BaseFeeMaxChangeDenom))
baseFee := big.Add(parentBaseFee, change)
if big.Cmp(baseFee, big.NewInt(build.MinimumBaseFee)) < 0 {
baseFee = big.NewInt(build.MinimumBaseFee)
}
return baseFee, nil
return computeNextBaseFee(parentBaseFee, totalLimit, len(ts.Blocks())), nil
}