package full import ( "context" "math" "math/rand" "sort" "github.com/filecoin-project/lotus/chain/actors/builtin" "github.com/filecoin-project/lotus/chain/actors/builtin/paych" "go.uber.org/fx" "golang.org/x/xerrors" "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/go-state-types/exitcode" "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/chain/messagepool" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/node/modules/dtypes" ) type GasModuleAPI interface { GasEstimateMessageGas(ctx context.Context, msg *types.Message, spec *api.MessageSendSpec, tsk types.TipSetKey) (*types.Message, error) } // GasModule provides a default implementation of GasModuleAPI. // It can be swapped out with another implementation through Dependency // Injection (for example with a thin RPC client). type GasModule struct { fx.In Stmgr *stmgr.StateManager Chain *store.ChainStore Mpool *messagepool.MessagePool GetMaxFee dtypes.DefaultMaxFeeFunc } var _ GasModuleAPI = (*GasModule)(nil) type GasAPI struct { fx.In GasModuleAPI Stmgr *stmgr.StateManager Chain *store.ChainStore Mpool *messagepool.MessagePool } const MinGasPremium = 100e3 const MaxSpendOnFeeDenom = 100 func (a *GasAPI) GasEstimateFeeCap( ctx context.Context, msg *types.Message, maxqueueblks int64, tsk types.TipSetKey, ) (types.BigInt, error) { return gasEstimateFeeCap(a.Chain, msg, maxqueueblks) } func (m *GasModule) GasEstimateFeeCap( ctx context.Context, msg *types.Message, maxqueueblks int64, tsk types.TipSetKey, ) (types.BigInt, error) { return gasEstimateFeeCap(m.Chain, msg, maxqueueblks) } func gasEstimateFeeCap(cstore *store.ChainStore, msg *types.Message, maxqueueblks int64) (types.BigInt, error) { ts := cstore.GetHeaviestTipSet() parentBaseFee := ts.Blocks()[0].ParentBaseFee increaseFactor := math.Pow(1.+1./float64(build.BaseFeeMaxChangeDenom), float64(maxqueueblks)) feeInFuture := types.BigMul(parentBaseFee, types.NewInt(uint64(increaseFactor*(1<<8)))) out := types.BigDiv(feeInFuture, types.NewInt(1<<8)) if msg.GasPremium != types.EmptyInt { out = types.BigAdd(out, msg.GasPremium) } return out, nil } type gasMeta struct { price big.Int limit int64 } func medianGasPremium(prices []gasMeta, blocks int) abi.TokenAmount { sort.Slice(prices, func(i, j int) bool { // sort desc by price return prices[i].price.GreaterThan(prices[j].price) }) at := build.BlockGasTarget * int64(blocks) / 2 prev1, prev2 := big.Zero(), big.Zero() for _, price := range prices { prev1, prev2 = price.price, prev1 at -= price.limit if at < 0 { break } } premium := prev1 if prev2.Sign() != 0 { premium = big.Div(types.BigAdd(prev1, prev2), types.NewInt(2)) } return premium } func (a *GasAPI) GasEstimateGasPremium( ctx context.Context, nblocksincl uint64, sender address.Address, gaslimit int64, _ types.TipSetKey, ) (types.BigInt, error) { return gasEstimateGasPremium(a.Chain, nblocksincl) } func (m *GasModule) GasEstimateGasPremium( ctx context.Context, nblocksincl uint64, sender address.Address, gaslimit int64, _ types.TipSetKey, ) (types.BigInt, error) { return gasEstimateGasPremium(m.Chain, nblocksincl) } func gasEstimateGasPremium(cstore *store.ChainStore, nblocksincl uint64) (types.BigInt, error) { if nblocksincl == 0 { nblocksincl = 1 } var prices []gasMeta var blocks int ts := cstore.GetHeaviestTipSet() for i := uint64(0); i < nblocksincl*2; i++ { if ts.Height() == 0 { break // genesis } pts, err := cstore.LoadTipSet(ts.Parents()) if err != nil { return types.BigInt{}, err } blocks += len(pts.Blocks()) msgs, err := cstore.MessagesForTipset(pts) if err != nil { return types.BigInt{}, xerrors.Errorf("loading messages: %w", err) } for _, msg := range msgs { prices = append(prices, gasMeta{ price: msg.VMMessage().GasPremium, limit: msg.VMMessage().GasLimit, }) } ts = pts } premium := medianGasPremium(prices, blocks) if types.BigCmp(premium, types.NewInt(MinGasPremium)) < 0 { switch nblocksincl { case 1: premium = types.NewInt(2 * MinGasPremium) case 2: premium = types.NewInt(1.5 * MinGasPremium) default: premium = types.NewInt(MinGasPremium) } } // add some noise to normalize behaviour of message selection const precision = 32 // mean 1, stddev 0.005 => 95% within +-1% noise := 1 + rand.NormFloat64()*0.005 premium = types.BigMul(premium, types.NewInt(uint64(noise*(1<