2020-08-06 17:09:03 +00:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2020-08-06 17:09:03 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
2020-09-10 14:26:34 +00:00
|
|
|
func ComputeNextBaseFee(baseFee types.BigInt, gasLimitUsed int64, noOfBlocks int, epoch abi.ChainEpoch) types.BigInt {
|
2020-09-10 10:22:49 +00:00
|
|
|
// deta := gasLimitUsed/noOfBlocks - build.BlockGasTarget
|
|
|
|
// change := baseFee * deta / BlockGasTarget
|
2020-08-18 13:19:24 +00:00
|
|
|
// nextBaseFee = baseFee + change
|
|
|
|
// nextBaseFee = max(nextBaseFee, build.MinimumBaseFee)
|
|
|
|
|
2020-09-10 10:22:49 +00:00
|
|
|
var delta int64
|
|
|
|
if epoch > build.UpgradeSmokeHeight {
|
|
|
|
delta = gasLimitUsed / int64(noOfBlocks)
|
|
|
|
delta -= build.BlockGasTarget
|
|
|
|
} else {
|
|
|
|
delta = build.PackingEfficiencyDenom * gasLimitUsed / (int64(noOfBlocks) * build.PackingEfficiencyNum)
|
|
|
|
delta -= build.BlockGasTarget
|
|
|
|
}
|
2020-08-18 13:19:24 +00:00
|
|
|
|
|
|
|
// cap change at 12.5% (BaseFeeMaxChangeDenom) by capping delta
|
|
|
|
if delta > build.BlockGasTarget {
|
|
|
|
delta = build.BlockGasTarget
|
|
|
|
}
|
|
|
|
if delta < -build.BlockGasTarget {
|
|
|
|
delta = -build.BlockGasTarget
|
|
|
|
}
|
2020-08-07 01:34:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-06 17:09:03 +00:00
|
|
|
func (cs *ChainStore) ComputeBaseFee(ctx context.Context, ts *types.TipSet) (abi.TokenAmount, error) {
|
2020-10-06 20:00:06 +00:00
|
|
|
if build.UpgradeBreezeHeight >= 0 && ts.Height() > build.UpgradeBreezeHeight && ts.Height() < build.UpgradeBreezeHeight+build.BreezeGasTampingDuration {
|
2020-09-07 18:19:45 +00:00
|
|
|
return abi.NewTokenAmount(100), nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 17:09:03 +00:00
|
|
|
zero := abi.NewTokenAmount(0)
|
2020-08-18 13:19:24 +00:00
|
|
|
|
|
|
|
// totalLimit is sum of GasLimits of unique messages in a tipset
|
2020-08-06 17:09:03 +00:00
|
|
|
totalLimit := int64(0)
|
2020-08-18 13:19:24 +00:00
|
|
|
|
|
|
|
seen := make(map[cid.Cid]struct{})
|
|
|
|
|
2020-08-06 17:09:03 +00:00
|
|
|
for _, b := range ts.Blocks() {
|
2021-12-17 09:42:09 +00:00
|
|
|
msg1, msg2, err := cs.MessagesForBlock(ctx, b)
|
2020-08-06 17:09:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return zero, xerrors.Errorf("error getting messages for: %s: %w", b.Cid(), err)
|
|
|
|
}
|
|
|
|
for _, m := range msg1 {
|
2020-08-18 13:19:24 +00:00
|
|
|
c := m.Cid()
|
|
|
|
if _, ok := seen[c]; !ok {
|
|
|
|
totalLimit += m.GasLimit
|
|
|
|
seen[c] = struct{}{}
|
|
|
|
}
|
2020-08-06 17:09:03 +00:00
|
|
|
}
|
|
|
|
for _, m := range msg2 {
|
2020-08-18 13:19:24 +00:00
|
|
|
c := m.Cid()
|
|
|
|
if _, ok := seen[c]; !ok {
|
|
|
|
totalLimit += m.Message.GasLimit
|
|
|
|
seen[c] = struct{}{}
|
|
|
|
}
|
2020-08-06 17:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
parentBaseFee := ts.Blocks()[0].ParentBaseFee
|
|
|
|
|
2020-09-10 14:26:34 +00:00
|
|
|
return ComputeNextBaseFee(parentBaseFee, totalLimit, len(ts.Blocks()), ts.Height()), nil
|
2020-08-06 17:09:03 +00:00
|
|
|
}
|