2020-08-01 22:09:06 +00:00
|
|
|
package vm
|
|
|
|
|
2020-08-05 00:09:39 +00:00
|
|
|
import (
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2020-08-05 00:09:39 +00:00
|
|
|
)
|
|
|
|
|
2020-08-01 22:09:06 +00:00
|
|
|
const (
|
2020-08-05 00:09:39 +00:00
|
|
|
gasOveruseNum = 11
|
2020-08-01 22:09:06 +00:00
|
|
|
gasOveruseDenom = 10
|
|
|
|
)
|
|
|
|
|
2020-08-06 19:05:16 +00:00
|
|
|
type GasOutputs struct {
|
|
|
|
BaseFeeBurn abi.TokenAmount
|
|
|
|
OverEstimationBurn abi.TokenAmount
|
|
|
|
|
|
|
|
MinerPenalty abi.TokenAmount
|
|
|
|
MinerTip abi.TokenAmount
|
|
|
|
Refund abi.TokenAmount
|
|
|
|
|
|
|
|
GasRefund int64
|
|
|
|
GasBurned int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComputeGasOverestimationBurn computes amount of gas to be refunded and amount of gas to be burned
|
2020-08-01 22:09:06 +00:00
|
|
|
// Result is (refund, burn)
|
2020-08-06 19:05:16 +00:00
|
|
|
func ComputeGasOverestimationBurn(gasUsed, gasLimit int64) (int64, int64) {
|
2020-08-05 00:09:39 +00:00
|
|
|
if gasUsed == 0 {
|
|
|
|
return 0, gasLimit
|
|
|
|
}
|
|
|
|
|
2020-08-08 16:56:33 +00:00
|
|
|
// over = gasLimit/gasUsed - 1 - 0.1
|
2020-08-05 00:09:39 +00:00
|
|
|
// over = min(over, 1)
|
|
|
|
// gasToBurn = (gasLimit - gasUsed) * over
|
|
|
|
|
|
|
|
// so to factor out division from `over`
|
2020-08-08 16:56:33 +00:00
|
|
|
// over*gasUsed = min(gasLimit - (11*gasUsed)/10, gasUsed)
|
2020-08-05 00:09:39 +00:00
|
|
|
// gasToBurn = ((gasLimit - gasUsed)*over*gasUsed) / gasUsed
|
|
|
|
over := gasLimit - (gasOveruseNum*gasUsed)/gasOveruseDenom
|
|
|
|
if over < 0 {
|
|
|
|
return gasLimit - gasUsed, 0
|
2020-08-01 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 00:09:39 +00:00
|
|
|
// if we want sharper scaling it goes here:
|
|
|
|
// over *= 2
|
|
|
|
|
|
|
|
if over > gasUsed {
|
|
|
|
over = gasUsed
|
|
|
|
}
|
|
|
|
|
|
|
|
// needs bigint, as it overflows in pathological case gasLimit > 2^32 gasUsed = gasLimit / 2
|
|
|
|
gasToBurn := big.NewInt(gasLimit - gasUsed)
|
2020-08-06 19:05:16 +00:00
|
|
|
gasToBurn = big.Mul(gasToBurn, big.NewInt(over))
|
|
|
|
gasToBurn = big.Div(gasToBurn, big.NewInt(gasUsed))
|
2020-08-05 00:09:39 +00:00
|
|
|
|
|
|
|
return gasLimit - gasUsed - gasToBurn.Int64(), gasToBurn.Int64()
|
2020-08-01 22:09:06 +00:00
|
|
|
}
|
2020-08-06 19:05:16 +00:00
|
|
|
|
|
|
|
func ComputeGasOutputs(gasUsed, gasLimit int64, baseFee, feeCap, gasPremium abi.TokenAmount) GasOutputs {
|
|
|
|
gasUsedBig := big.NewInt(gasUsed)
|
|
|
|
out := GasOutputs{
|
|
|
|
BaseFeeBurn: big.Zero(),
|
|
|
|
OverEstimationBurn: big.Zero(),
|
|
|
|
MinerPenalty: big.Zero(),
|
|
|
|
MinerTip: big.Zero(),
|
|
|
|
Refund: big.Zero(),
|
|
|
|
}
|
|
|
|
|
|
|
|
baseFeeToPay := baseFee
|
|
|
|
if baseFee.Cmp(feeCap.Int) > 0 {
|
|
|
|
baseFeeToPay = feeCap
|
|
|
|
out.MinerPenalty = big.Mul(big.Sub(baseFee, feeCap), gasUsedBig)
|
|
|
|
}
|
2020-08-07 02:09:01 +00:00
|
|
|
out.BaseFeeBurn = big.Mul(baseFeeToPay, gasUsedBig)
|
2020-08-06 19:05:16 +00:00
|
|
|
|
|
|
|
minerTip := gasPremium
|
|
|
|
if big.Cmp(big.Add(baseFeeToPay, minerTip), feeCap) > 0 {
|
|
|
|
minerTip = big.Sub(feeCap, baseFeeToPay)
|
|
|
|
}
|
|
|
|
out.MinerTip = big.Mul(minerTip, big.NewInt(gasLimit))
|
|
|
|
|
|
|
|
out.GasRefund, out.GasBurned = ComputeGasOverestimationBurn(gasUsed, gasLimit)
|
|
|
|
|
|
|
|
if out.GasBurned != 0 {
|
|
|
|
gasBurnedBig := big.NewInt(out.GasBurned)
|
|
|
|
out.OverEstimationBurn = big.Mul(baseFeeToPay, gasBurnedBig)
|
|
|
|
minerPenalty := big.Mul(big.Sub(baseFee, baseFeeToPay), gasBurnedBig)
|
|
|
|
out.MinerPenalty = big.Add(out.MinerPenalty, minerPenalty)
|
|
|
|
}
|
|
|
|
|
|
|
|
requiredFunds := big.Mul(big.NewInt(gasLimit), feeCap)
|
|
|
|
refund := big.Sub(requiredFunds, out.BaseFeeBurn)
|
|
|
|
refund = big.Sub(refund, out.MinerTip)
|
|
|
|
refund = big.Sub(refund, out.OverEstimationBurn)
|
|
|
|
out.Refund = refund
|
|
|
|
return out
|
|
|
|
}
|