Skip to content

Commit

Permalink
bugfix: metatx gas undercharge && estimate insufficient
Browse files Browse the repository at this point in the history
Signed-off-by: pandainzoo <gary.zhu@mantle.xyz>
  • Loading branch information
pandainzoo committed May 22, 2024
1 parent 12250d0 commit 757949a
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ func (st *StateTransition) CalculateRollupGasDataFromMessage() {
// add a constant to cover sigs(V,R,S) and other data to make sure that the gasLimit from eth_estimateGas can cover L1 cost
// just used for estimateGas and the actual L1 cost depends on users' tx when executing
st.msg.RollupDataGas.Ones += 80

// add a constant to cover meta tx sigs(V,R,S)
if st.msg.MetaTxParams != nil {
st.msg.RollupDataGas.Ones += 80
}
}

// ApplyMessage computes the new state by applying the given message
Expand Down Expand Up @@ -289,10 +294,13 @@ func (st *StateTransition) to() common.Address {
return *st.msg.To
}

func (st *StateTransition) buyGas() (*big.Int, error) {
if err := st.applyMetaTransaction(); err != nil {
return nil, err
func (st *StateTransition) buyGas(metaTxV3 bool) (*big.Int, error) {
if !metaTxV3 {
if err := st.applyMetaTransaction(); err != nil {
return nil, err
}
}

mgval := new(big.Int).SetUint64(st.msg.GasLimit)
mgval = mgval.Mul(mgval, st.msg.GasPrice)
var l1Cost *big.Int
Expand Down Expand Up @@ -371,7 +379,7 @@ func (st *StateTransition) applyMetaTransaction() error {
return nil
}

func (st *StateTransition) preCheck() (*big.Int, error) {
func (st *StateTransition) preCheck(metaTxV3 bool) (*big.Int, error) {
if st.msg.IsDepositTx {
// No fee fields to check, no nonce to check, and no need to check if EOA (L1 already verified it for us)
// Gas is free, but no refunds!
Expand Down Expand Up @@ -437,7 +445,7 @@ func (st *StateTransition) preCheck() (*big.Int, error) {
}
}
}
return st.buyGas()
return st.buyGas(metaTxV3)
}

// TransitionDb will transition the state by applying the current message and
Expand Down Expand Up @@ -510,7 +518,7 @@ func (st *StateTransition) innerTransitionDb() (*ExecutionResult, error) {

// Check clauses 1-3, buy gas if everything is correct
tokenRatio := st.state.GetState(types.GasOracleAddr, types.TokenRatioSlot).Big().Uint64()
l1Cost, err := st.preCheck()
l1Cost, err := st.preCheck(rules.IsMetaTxV3)
if err != nil {
return nil, err
}
Expand All @@ -534,6 +542,13 @@ func (st *StateTransition) innerTransitionDb() (*ExecutionResult, error) {
return nil, err
}

// after calculate intrinsic gas, apply meta tx data
if rules.IsMetaTxV3 {
if err := st.applyMetaTransaction(); err != nil {
return nil, err
}
}

if !st.msg.IsDepositTx && !st.msg.IsSystemTx {
gas = gas * tokenRatio
}
Expand Down

0 comments on commit 757949a

Please sign in to comment.