Merge branch 'release/v1.20.0' into asr/merge-release-into-master

This commit is contained in:
Aayush Rajasekaran
2023-02-07 11:03:01 -05:00
24 changed files with 5688 additions and 316 deletions
+136 -6
View File
@@ -24,6 +24,7 @@ import (
"github.com/filecoin-project/go-state-types/builtin/v10/eam"
"github.com/filecoin-project/go-state-types/builtin/v10/evm"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
@@ -446,6 +447,11 @@ func (a *EthModule) EthGetCode(ctx context.Context, ethAddr ethtypes.EthAddress,
return nil, xerrors.Errorf("cannot parse block param: %s", blkParam)
}
// StateManager.Call will panic if there is no parent
if ts.Height() == 0 {
return nil, xerrors.Errorf("block param must not specify genesis block")
}
// Try calling until we find a height with no migration.
var res *api.InvocResult
for {
@@ -636,7 +642,7 @@ func (a *EthModule) EthFeeHistory(ctx context.Context, blkCount ethtypes.EthUint
}
return ethtypes.EthFeeHistory{
OldestBlock: oldestBlkHeight,
OldestBlock: ethtypes.EthUint64(oldestBlkHeight),
BaseFeePerGas: baseFeeArray,
GasUsedRatio: gasUsedRatioArray,
}, nil
@@ -809,12 +815,137 @@ func (a *EthModule) EthEstimateGas(ctx context.Context, tx ethtypes.EthCall) (et
// gas estimation actually run.
msg.GasLimit = 0
msg, err = a.GasAPI.GasEstimateMessageGas(ctx, msg, nil, types.EmptyTSK)
ts := a.Chain.GetHeaviestTipSet()
msg, err = a.GasAPI.GasEstimateMessageGas(ctx, msg, nil, ts.Key())
if err != nil {
return ethtypes.EthUint64(0), err
return ethtypes.EthUint64(0), xerrors.Errorf("failed to estimate gas: %w", err)
}
return ethtypes.EthUint64(msg.GasLimit), nil
expectedGas, err := ethGasSearch(ctx, a.Chain, a.Stmgr, a.Mpool, msg, ts)
if err != nil {
log.Errorw("expected gas", "err", err)
}
return ethtypes.EthUint64(expectedGas), nil
}
// gasSearch does an exponential search to find a gas value to execute the
// message with. It first finds a high gas limit that allows the message to execute
// by doubling the previous gas limit until it succeeds then does a binary
// search till it gets within a range of 1%
func gasSearch(
ctx context.Context,
smgr *stmgr.StateManager,
msgIn *types.Message,
priorMsgs []types.ChainMsg,
ts *types.TipSet,
) (int64, error) {
msg := *msgIn
high := msg.GasLimit
low := msg.GasLimit
canSucceed := func(limit int64) (bool, error) {
msg.GasLimit = limit
res, err := smgr.CallWithGas(ctx, &msg, priorMsgs, ts)
if err != nil {
return false, xerrors.Errorf("CallWithGas failed: %w", err)
}
if res.MsgRct.ExitCode.IsSuccess() {
return true, nil
}
return false, nil
}
for {
ok, err := canSucceed(high)
if err != nil {
return -1, xerrors.Errorf("searching for high gas limit failed: %w", err)
}
if ok {
break
}
low = high
high = high * 2
if high > build.BlockGasLimit {
high = build.BlockGasLimit
break
}
}
checkThreshold := high / 100
for (high - low) > checkThreshold {
median := (low + high) / 2
ok, err := canSucceed(median)
if err != nil {
return -1, xerrors.Errorf("searching for optimal gas limit failed: %w", err)
}
if ok {
high = median
} else {
low = median
}
checkThreshold = median / 100
}
return high, nil
}
func traceContainsExitCode(et types.ExecutionTrace, ex exitcode.ExitCode) bool {
if et.MsgRct.ExitCode == ex {
return true
}
for _, et := range et.Subcalls {
if traceContainsExitCode(et, ex) {
return true
}
}
return false
}
// ethGasSearch executes a message for gas estimation using the previously estimated gas.
// If the message fails due to an out of gas error then a gas search is performed.
// See gasSearch.
func ethGasSearch(
ctx context.Context,
cstore *store.ChainStore,
smgr *stmgr.StateManager,
mpool *messagepool.MessagePool,
msgIn *types.Message,
ts *types.TipSet,
) (int64, error) {
msg := *msgIn
currTs := ts
res, priorMsgs, ts, err := gasEstimateCallWithGas(ctx, cstore, smgr, mpool, &msg, currTs)
if err != nil {
return -1, xerrors.Errorf("gas estimation failed: %w", err)
}
if res.MsgRct.ExitCode.IsSuccess() {
return msg.GasLimit, nil
}
if traceContainsExitCode(res.ExecutionTrace, exitcode.SysErrOutOfGas) {
ret, err := gasSearch(ctx, smgr, &msg, priorMsgs, ts)
if err != nil {
return -1, xerrors.Errorf("gas estimation search failed: %w", err)
}
ret = int64(float64(ret) * mpool.GetConfig().GasLimitOverestimation)
return ret, nil
}
return -1, xerrors.Errorf("message execution failed: exit %s, reason: %s", res.MsgRct.ExitCode, res.Error)
}
func (a *EthModule) EthCall(ctx context.Context, tx ethtypes.EthCall, blkParam string) (ethtypes.EthBytes, error) {
@@ -836,7 +967,6 @@ func (a *EthModule) EthCall(ctx context.Context, tx ethtypes.EthCall, blkParam s
if msg.To == builtintypes.EthereumAddressManagerActorAddr {
// As far as I can tell, the Eth API always returns empty on contract deployment
return ethtypes.EthBytes{}, nil
} else if len(invokeResult.MsgRct.Return) > 0 {
return cbg.ReadByteArray(bytes.NewReader(invokeResult.MsgRct.Return), uint64(len(invokeResult.MsgRct.Return)))
}
@@ -1468,7 +1598,7 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
return ethtypes.EthBlock{}, xerrors.Errorf("error loading messages for tipset: %v: %w", ts, err)
}
block := ethtypes.NewEthBlock()
block := ethtypes.NewEthBlock(len(msgs) > 0)
// this seems to be a very expensive way to get gasUsed of the block. may need to find an efficient way to do it
gasUsed := int64(0)
+34 -9
View File
@@ -248,22 +248,23 @@ func (m *GasModule) GasEstimateGasLimit(ctx context.Context, msgIn *types.Messag
}
return gasEstimateGasLimit(ctx, m.Chain, m.Stmgr, m.Mpool, msgIn, ts)
}
func gasEstimateGasLimit(
// gasEstimateCallWithGas invokes a message "msgIn" on the earliest available tipset with pending
// messages in the message pool. The function returns the result of the message invocation, the
// pending messages, the tipset used for the invocation, and an error if occurred.
// The returned information can be used to make subsequent calls to CallWithGas with the same parameters.
func gasEstimateCallWithGas(
ctx context.Context,
cstore *store.ChainStore,
smgr *stmgr.StateManager,
mpool *messagepool.MessagePool,
msgIn *types.Message,
currTs *types.TipSet,
) (int64, error) {
) (*api.InvocResult, []types.ChainMsg, *types.TipSet, error) {
msg := *msgIn
msg.GasLimit = build.BlockGasLimit
msg.GasFeeCap = big.Zero()
msg.GasPremium = big.Zero()
fromA, err := smgr.ResolveToDeterministicAddress(ctx, msgIn.From, currTs)
if err != nil {
return -1, xerrors.Errorf("getting key address: %w", err)
return nil, []types.ChainMsg{}, nil, xerrors.Errorf("getting key address: %w", err)
}
pending, ts := mpool.PendingFor(ctx, fromA)
@@ -284,12 +285,34 @@ func gasEstimateGasLimit(
}
ts, err = cstore.GetTipSetFromKey(ctx, ts.Parents())
if err != nil {
return -1, xerrors.Errorf("getting parent tipset: %w", err)
return nil, []types.ChainMsg{}, nil, xerrors.Errorf("getting parent tipset: %w", err)
}
}
if err != nil {
return -1, xerrors.Errorf("CallWithGas failed: %w", err)
return nil, []types.ChainMsg{}, nil, xerrors.Errorf("CallWithGas failed: %w", err)
}
return res, priorMsgs, ts, nil
}
func gasEstimateGasLimit(
ctx context.Context,
cstore *store.ChainStore,
smgr *stmgr.StateManager,
mpool *messagepool.MessagePool,
msgIn *types.Message,
currTs *types.TipSet,
) (int64, error) {
msg := *msgIn
msg.GasLimit = build.BlockGasLimit
msg.GasFeeCap = big.Zero()
msg.GasPremium = big.Zero()
res, _, ts, err := gasEstimateCallWithGas(ctx, cstore, smgr, mpool, &msg, currTs)
if err != nil {
return -1, xerrors.Errorf("gas estimation failed: %w", err)
}
if res.MsgRct.ExitCode == exitcode.SysErrOutOfGas {
return -1, &api.ErrOutOfGas{}
}
@@ -300,6 +323,8 @@ func gasEstimateGasLimit(
ret := res.MsgRct.GasUsed
log.Infow("GasEstimateMessageGas CallWithGas Result", "GasUsed", ret, "ExitCode", res.MsgRct.ExitCode)
transitionalMulti := 1.0
// Overestimate gas around the upgrade
if ts.Height() <= build.UpgradeSkyrHeight && (build.UpgradeSkyrHeight-ts.Height() <= 20) {