Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gas: estimate gas with a zero base-fee #5322

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions app/submodule/mpool/mpool_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (

var _ v1api.IMessagePool = &MessagePoolAPI{}

//MessagePoolAPI messsage pool api implement
// MessagePoolAPI messsage pool api implement
type MessagePoolAPI struct {
pushLocks *messagepool.MpoolLocker

mp *MessagePoolSubmodule
}

//MpoolDeleteByAdress delete msg in mpool of addr
// MpoolDeleteByAdress delete msg in mpool of addr
func (a *MessagePoolAPI) MpoolDeleteByAdress(ctx context.Context, addr address.Address) error {
return a.mp.MPool.DeleteByAdress(addr)
}

//MpoolPublish publish message of address
// MpoolPublish publish message of address
func (a *MessagePoolAPI) MpoolPublishByAddr(ctx context.Context, addr address.Address) error {
return a.mp.MPool.PublishMsgForWallet(ctx, addr)
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func (a *MessagePoolAPI) MpoolSelect(ctx context.Context, tsk types.TipSetKey, t
return a.mp.MPool.SelectMessages(ctx, ts, ticketQuality)
}

//MpoolSelects The batch selection message is used when multiple blocks need to select messages at the same time
// MpoolSelects The batch selection message is used when multiple blocks need to select messages at the same time
func (a *MessagePoolAPI) MpoolSelects(ctx context.Context, tsk types.TipSetKey, ticketQualitys []float64) ([][]*types.SignedMessage, error) {
ts, err := a.mp.chain.API().ChainGetTipSet(ctx, tsk)
if err != nil {
Expand Down Expand Up @@ -227,8 +227,9 @@ func (a *MessagePoolAPI) MpoolPushMessage(ctx context.Context, msg *types.Messag
return nil, fmt.Errorf("mpool push: getting origin balance: %w", err)
}

if b.LessThan(msg.Value) {
return nil, fmt.Errorf("mpool push: not enough funds: %s < %s", b, msg.Value)
requiredFunds := big.Add(msg.Value, msg.RequiredFunds())
if b.LessThan(requiredFunds) {
return nil, fmt.Errorf("mpool push: not enough funds: %s < %s", b, requiredFunds)
}

// Sign and push the message
Expand Down
4 changes: 2 additions & 2 deletions pkg/messagepool/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ func (mp *MessagePool) GasEstimateGasLimit(ctx context.Context, msgIn *types.Mes
func (mp *MessagePool) evalMessageGasLimit(ctx context.Context, msgIn *types.Message, priorMsgs []types.ChainMsg, ts *types.TipSet) (int64, error) {
msg := *msgIn
msg.GasLimit = constants.BlockGasLimit
msg.GasFeeCap = big.NewInt(int64(constants.MinimumBaseFee) + 1)
msg.GasPremium = big.NewInt(1)
msg.GasFeeCap = big.Zero()
msg.GasPremium = big.Zero()

// Try calling until we find a height with no migration.
var res *vm.Ret
Expand Down
18 changes: 18 additions & 0 deletions pkg/statemanger/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
acrypto "github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/venus/pkg/crypto"
"github.com/ipfs/go-cid"
Expand All @@ -35,6 +36,10 @@ func (s *Stmgr) CallWithGas(ctx context.Context, msg *types.Message, priorMsgs [
view *state.View
)

// Copy the message as we'll be modifying the nonce.
msgCopy := *msg
msg = &msgCopy

if ts == nil {
ts = s.cs.GetHead()
// Search back till we find a height with no fork, or we reach the beginning.
Expand Down Expand Up @@ -148,6 +153,19 @@ func (s *Stmgr) CallWithGas(ctx context.Context, msg *types.Message, priorMsgs [
},
}
}

// If the fee cap is set to zero, make gas free.
if msg.GasFeeCap.NilOrZero() {
// Now estimate with a new VM with no base fee.
vmOption.BaseFee = big.Zero()
vmOption.PRoot = stateRoot

vmi, err = fvm.NewVM(ctx, vmOption)
if err != nil {
return nil, fmt.Errorf("failed to set up estimation vm: %w", err)
}
}

return vmi.ApplyMessage(ctx, msgApply)
}

Expand Down