2020-07-20 17:48:30 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-06 22:10:55 +00:00
|
|
|
"math"
|
2020-08-01 07:40:11 +00:00
|
|
|
"sort"
|
2020-07-20 17:48:30 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-07-22 15:46:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/messagepool"
|
2020-07-20 17:48:30 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-08-01 19:50:03 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
2020-07-20 17:48:30 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime/exitcode"
|
2020-08-01 19:50:03 +00:00
|
|
|
|
2020-07-20 17:48:30 +00:00
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GasAPI struct {
|
|
|
|
fx.In
|
|
|
|
Stmgr *stmgr.StateManager
|
2020-08-01 07:40:11 +00:00
|
|
|
Chain *store.ChainStore
|
2020-07-22 15:46:13 +00:00
|
|
|
Mpool *messagepool.MessagePool
|
2020-07-20 17:48:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 16:35:14 +00:00
|
|
|
const MinGasPremium = 10e3
|
2020-08-07 18:09:50 +00:00
|
|
|
const MaxSpendOnFeeDenom = 100
|
2020-08-06 22:10:55 +00:00
|
|
|
|
2020-08-07 18:09:50 +00:00
|
|
|
func (a *GasAPI) GasEstimateFeeCap(ctx context.Context, msg *types.Message, maxqueueblks int64,
|
2020-08-06 22:10:55 +00:00
|
|
|
tsk types.TipSetKey) (types.BigInt, error) {
|
|
|
|
ts := a.Chain.GetHeaviestTipSet()
|
|
|
|
|
2020-08-07 18:09:50 +00:00
|
|
|
var act types.Actor
|
|
|
|
err := a.Stmgr.WithParentState(ts, a.Stmgr.WithActor(msg.From, stmgr.GetActor(&act)))
|
|
|
|
if err != nil {
|
|
|
|
return types.NewInt(0), xerrors.Errorf("getting actor: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:10:55 +00:00
|
|
|
parentBaseFee := ts.Blocks()[0].ParentBaseFee
|
2020-08-07 22:41:57 +00:00
|
|
|
increaseFactor := math.Pow(1.+1./float64(build.BaseFeeMaxChangeDenom), float64(maxqueueblks))
|
2020-08-07 18:09:50 +00:00
|
|
|
|
|
|
|
feeInFuture := types.BigMul(parentBaseFee, types.NewInt(uint64(increaseFactor*(1<<8))))
|
|
|
|
feeInFuture = types.BigDiv(feeInFuture, types.NewInt(1<<8))
|
|
|
|
|
|
|
|
gasLimitBig := types.NewInt(uint64(msg.GasLimit))
|
|
|
|
maxAccepted := types.BigDiv(act.Balance, types.NewInt(MaxSpendOnFeeDenom))
|
|
|
|
expectedFee := types.BigMul(feeInFuture, gasLimitBig)
|
|
|
|
|
|
|
|
out := feeInFuture
|
|
|
|
if types.BigCmp(expectedFee, maxAccepted) > 0 {
|
|
|
|
log.Warnf("Expected fee for message higher than tolerance: %s > %s, setting to tolerance",
|
|
|
|
types.FIL(expectedFee), types.FIL(maxAccepted))
|
|
|
|
out = types.BigDiv(maxAccepted, gasLimitBig)
|
|
|
|
}
|
2020-08-06 22:10:55 +00:00
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
2020-07-20 17:48:30 +00:00
|
|
|
|
2020-08-06 21:08:42 +00:00
|
|
|
func (a *GasAPI) GasEsitmateGasPremium(ctx context.Context, nblocksincl uint64,
|
2020-08-06 22:10:55 +00:00
|
|
|
sender address.Address, gaslimit int64, _ types.TipSetKey) (types.BigInt, error) {
|
2020-07-20 17:48:30 +00:00
|
|
|
|
2020-08-01 07:40:11 +00:00
|
|
|
if nblocksincl == 0 {
|
|
|
|
nblocksincl = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
type gasMeta struct {
|
|
|
|
price big.Int
|
2020-08-07 16:35:14 +00:00
|
|
|
limit int64
|
2020-08-01 07:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var prices []gasMeta
|
|
|
|
var blocks int
|
|
|
|
|
|
|
|
ts := a.Chain.GetHeaviestTipSet()
|
2020-08-01 07:40:25 +00:00
|
|
|
for i := uint64(0); i < nblocksincl*2; i++ {
|
2020-08-01 07:40:11 +00:00
|
|
|
if len(ts.Parents().Cids()) == 0 {
|
|
|
|
break // genesis
|
|
|
|
}
|
|
|
|
|
2020-08-01 07:40:25 +00:00
|
|
|
pts, err := a.Chain.LoadTipSet(ts.Parents())
|
2020-08-01 07:40:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.BigInt{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks += len(pts.Blocks())
|
|
|
|
|
|
|
|
msgs, err := a.Chain.MessagesForTipset(pts)
|
|
|
|
if err != nil {
|
|
|
|
return types.BigInt{}, xerrors.Errorf("loading messages: %w", err)
|
|
|
|
}
|
2020-08-07 16:35:14 +00:00
|
|
|
for _, msg := range msgs {
|
2020-08-01 07:40:11 +00:00
|
|
|
prices = append(prices, gasMeta{
|
2020-08-06 21:08:42 +00:00
|
|
|
price: msg.VMMessage().GasPremium,
|
2020-08-07 16:35:14 +00:00
|
|
|
limit: msg.VMMessage().GasLimit,
|
2020-08-01 07:40:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ts = pts
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(prices, func(i, j int) bool {
|
|
|
|
// sort desc by price
|
|
|
|
return prices[i].price.GreaterThan(prices[j].price)
|
|
|
|
})
|
|
|
|
|
|
|
|
// todo: account for how full blocks are
|
|
|
|
|
2020-08-07 16:35:14 +00:00
|
|
|
at := build.BlockGasTarget * int64(blocks) / 2
|
2020-08-01 11:23:07 +00:00
|
|
|
prev := big.Zero()
|
2020-08-01 07:40:11 +00:00
|
|
|
|
|
|
|
for _, price := range prices {
|
2020-08-07 16:35:14 +00:00
|
|
|
at -= price.limit
|
2020-08-01 07:40:11 +00:00
|
|
|
if at > 0 {
|
2020-08-01 11:23:07 +00:00
|
|
|
prev = price.price
|
2020-08-01 07:40:11 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-07 16:35:14 +00:00
|
|
|
if prev.Equals(big.Zero()) {
|
|
|
|
return types.BigAdd(price.price, big.NewInt(1)), nil
|
|
|
|
}
|
|
|
|
|
2020-08-01 11:23:07 +00:00
|
|
|
return types.BigAdd(big.Div(types.BigAdd(price.price, prev), types.NewInt(2)), big.NewInt(1)), nil
|
2020-08-01 07:40:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:48:30 +00:00
|
|
|
switch nblocksincl {
|
|
|
|
case 1:
|
2020-08-07 16:35:14 +00:00
|
|
|
return types.NewInt(2 * MinGasPremium), nil
|
2020-08-01 07:40:11 +00:00
|
|
|
case 2:
|
2020-08-07 16:35:14 +00:00
|
|
|
return types.NewInt(1.5 * MinGasPremium), nil
|
2020-07-20 17:48:30 +00:00
|
|
|
default:
|
2020-08-07 16:35:14 +00:00
|
|
|
return types.NewInt(MinGasPremium), nil
|
2020-07-20 17:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 15:46:13 +00:00
|
|
|
func (a *GasAPI) GasEstimateGasLimit(ctx context.Context, msgIn *types.Message, _ types.TipSetKey) (int64, error) {
|
2020-07-20 17:48:30 +00:00
|
|
|
|
2020-07-20 19:41:05 +00:00
|
|
|
msg := *msgIn
|
2020-07-20 17:48:30 +00:00
|
|
|
msg.GasLimit = build.BlockGasLimit
|
2020-08-07 13:03:55 +00:00
|
|
|
msg.GasFeeCap = types.NewInt(uint64(build.MinimumBaseFee) + 1)
|
2020-08-06 19:28:47 +00:00
|
|
|
msg.GasPremium = types.NewInt(1)
|
2020-07-20 17:48:30 +00:00
|
|
|
|
2020-08-01 07:40:11 +00:00
|
|
|
currTs := a.Chain.GetHeaviestTipSet()
|
2020-07-22 15:46:13 +00:00
|
|
|
fromA, err := a.Stmgr.ResolveToKeyAddress(ctx, msgIn.From, currTs)
|
2020-07-20 17:48:30 +00:00
|
|
|
if err != nil {
|
2020-07-22 15:46:13 +00:00
|
|
|
return -1, xerrors.Errorf("getting key address: %w", err)
|
2020-07-20 17:48:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 15:46:13 +00:00
|
|
|
pending, ts := a.Mpool.PendingFor(fromA)
|
|
|
|
priorMsgs := make([]types.ChainMsg, 0, len(pending))
|
|
|
|
for _, m := range pending {
|
|
|
|
priorMsgs = append(priorMsgs, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := a.Stmgr.CallWithGas(ctx, &msg, priorMsgs, ts)
|
2020-07-20 18:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, xerrors.Errorf("CallWithGas failed: %w", err)
|
|
|
|
}
|
2020-07-20 17:48:30 +00:00
|
|
|
if res.MsgRct.ExitCode != exitcode.Ok {
|
2020-07-20 18:33:15 +00:00
|
|
|
return -1, xerrors.Errorf("message execution failed: exit %s, reason: %s", res.MsgRct.ExitCode, res.Error)
|
2020-07-20 17:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.MsgRct.GasUsed, nil
|
|
|
|
}
|