2020-07-20 17:48:30 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-01 07:40:11 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
|
|
|
"sort"
|
2020-07-20 17:48:30 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"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"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime/exitcode"
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
const MinGasPrice = 1
|
|
|
|
|
|
|
|
func (a *GasAPI) GasEstimateGasPrice(ctx context.Context, nblocksincl uint64,
|
|
|
|
sender address.Address, gaslimit int64, tsk types.TipSetKey) (types.BigInt, error) {
|
|
|
|
|
2020-08-01 07:40:11 +00:00
|
|
|
if nblocksincl == 0 {
|
|
|
|
nblocksincl = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
type gasMeta struct {
|
|
|
|
price big.Int
|
|
|
|
used int64
|
|
|
|
}
|
|
|
|
|
|
|
|
var prices []gasMeta
|
|
|
|
var gasUsed int64
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, msg := range msgs {
|
|
|
|
r, err := a.Chain.GetParentReceipt(ts.MinTicketBlock(), i)
|
|
|
|
if err != nil {
|
|
|
|
return types.BigInt{}, xerrors.Errorf("getting receipt: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
prices = append(prices, gasMeta{
|
|
|
|
price: msg.VMMessage().GasPrice,
|
|
|
|
used: r.GasUsed,
|
|
|
|
})
|
|
|
|
gasUsed += r.GasUsed
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
at := gasUsed / 2
|
|
|
|
|
|
|
|
for _, price := range prices {
|
|
|
|
at -= price.used
|
|
|
|
if at > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return price.price, nil
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:48:30 +00:00
|
|
|
switch nblocksincl {
|
|
|
|
case 1:
|
2020-08-01 07:40:11 +00:00
|
|
|
return types.NewInt(MinGasPrice + 2), nil
|
|
|
|
case 2:
|
2020-07-20 17:48:30 +00:00
|
|
|
return types.NewInt(MinGasPrice + 1), nil
|
|
|
|
default:
|
|
|
|
return types.NewInt(MinGasPrice), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-20 18:33:15 +00:00
|
|
|
msg.GasPrice = 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
|
|
|
|
}
|