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

Revert "[R4R]-{estimateGas}feature: EstimateGas performance optimization (#38) #65

Merged
merged 1 commit into from
Mar 12, 2024
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
7 changes: 1 addition & 6 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (h UnprefixedHash) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(h[:])), nil
}

// ///////// Address
/////////// Address

// Address represents the 20 byte address of an Ethereum account.
type Address [AddressLength]byte
Expand Down Expand Up @@ -309,11 +309,6 @@ func (a *Address) SetBytes(b []byte) {
copy(a[AddressLength-len(b):], b)
}

// Cmp compares two addresses.
func (a Address) Cmp(other Address) int {
return bytes.Compare(a[:], other[:])
}

// MarshalText returns the hex representation of a.
func (a Address) MarshalText() ([]byte, error) {
return hexutil.Bytes(a[:]).MarshalText()
Expand Down
66 changes: 19 additions & 47 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ var (
// ExecutionResult includes all output after executing given evm
// message no matter the execution itself is successful or not.
type ExecutionResult struct {
UsedGas uint64 // Total used gas but include the refunded gas
RefundedGas uint64 // Total gas refunded after execution
Err error // Any error encountered during the execution(listed in core/vm/errors.go)
ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode)
UsedGas uint64 // Total used gas but include the refunded gas
Err error // Any error encountered during the execution(listed in core/vm/errors.go)
ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode)
}

// Unwrap returns the internal evm error which allows us for further
Expand Down Expand Up @@ -237,11 +236,6 @@ func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool) (*ExecutionResult, err
return NewStateTransition(evm, msg, gp).TransitionDb()
}

// CalculateL1Cost calculates the L1 cost for a transaction without modifying the state.
func CalculateL1Cost(evm *vm.EVM, msg *Message, gp *GasPool) (*big.Int, error) {
return NewStateTransition(evm, msg, gp).CalculateL1Cost()
}

// StateTransition represents a state transition.
//
// == The State Transitioning Model
Expand Down Expand Up @@ -291,23 +285,6 @@ func (st *StateTransition) to() common.Address {
return *st.msg.To
}

// CalculateL1Cost calculates the L1 cost for a transaction without modifying the state.
func (st *StateTransition) CalculateL1Cost() (*big.Int, error) {
var l1Cost *big.Int

// Calculate rollup gas data from the message if necessary
if st.msg.RunMode == GasEstimationMode || st.msg.RunMode == GasEstimationWithSkipCheckBalanceMode {
st.CalculateRollupGasDataFromMessage()
}

// Calculate L1 cost if L1CostFunc is defined and not in EthcallMode
if st.evm.Context.L1CostFunc != nil && st.msg.RunMode != EthcallMode {
l1Cost = st.evm.Context.L1CostFunc(st.evm.Context.BlockNumber.Uint64(), st.evm.Context.Time, st.msg.RollupDataGas, st.msg.IsDepositTx, st.msg.To)
}

return l1Cost, nil
}

func (st *StateTransition) buyGas() (*big.Int, error) {
if err := st.applyMetaTransaction(); err != nil {
return nil, err
Expand Down Expand Up @@ -468,9 +445,9 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
st.state.AddBalance(st.msg.From, mint)
}

// Mint BVM_ETH
//Mint BVM_ETH
rules := st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber, st.evm.Context.Random != nil, st.evm.Context.Time)
// add eth value
//add eth value
if ethValue := st.msg.ETHValue; ethValue != nil && ethValue.Cmp(big.NewInt(0)) != 0 {
st.mintBVMETH(ethValue, rules)
}
Expand Down Expand Up @@ -616,24 +593,22 @@ func (st *StateTransition) innerTransitionDb() (*ExecutionResult, error) {
// Note for deposit tx there is no ETH refunded for unused gas, but that's taken care of by the fact that gasPrice
// is always 0 for deposit tx. So calling refundGas will ensure the gasUsed accounting is correct without actually
// changing the sender's balance
var gasRefund uint64
if !st.msg.IsDepositTx && !st.msg.IsSystemTx {
if !rules.IsLondon {
// Before EIP-3529: refunds were capped to gasUsed / 2
gasRefund = st.refundGas(params.RefundQuotient, tokenRatio)
st.refundGas(params.RefundQuotient, tokenRatio)
} else {
// After EIP-3529: refunds are capped to gasUsed / 5
gasRefund = st.refundGas(params.RefundQuotientEIP3529, tokenRatio)
st.refundGas(params.RefundQuotientEIP3529, tokenRatio)
}
}

if st.msg.IsDepositTx && rules.IsOptimismRegolith {
// Skip coinbase payments for deposit tx in Regolith
return &ExecutionResult{
UsedGas: st.gasUsed(),
RefundedGas: gasRefund,
Err: vmerr,
ReturnData: ret,
UsedGas: st.gasUsed(),
Err: vmerr,
ReturnData: ret,
}, nil
}
effectiveTip := msg.GasPrice
Expand All @@ -656,24 +631,23 @@ func (st *StateTransition) innerTransitionDb() (*ExecutionResult, error) {
if optimismConfig := st.evm.ChainConfig().Optimism; optimismConfig != nil && rules.IsOptimismBedrock {
st.state.AddBalance(params.OptimismBaseFeeRecipient, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.evm.Context.BaseFee))
// Can not collect l1 fee here again, all l1 fee has been collected by CoinBase & OptimismBaseFeeRecipient
// if cost := st.evm.Context.L1CostFunc(st.evm.Context.BlockNumber.Uint64(), st.evm.Context.Time, st.msg.RollupDataGas, st.msg.IsDepositTx); cost != nil {
//if cost := st.evm.Context.L1CostFunc(st.evm.Context.BlockNumber.Uint64(), st.evm.Context.Time, st.msg.RollupDataGas, st.msg.IsDepositTx); cost != nil {
// st.state.AddBalance(params.OptimismL1FeeRecipient, cost)
// }
//}
}

return &ExecutionResult{
UsedGas: st.gasUsed(),
RefundedGas: gasRefund,
Err: vmerr,
ReturnData: ret,
UsedGas: st.gasUsed(),
Err: vmerr,
ReturnData: ret,
}, nil
}

func (st *StateTransition) refundGas(refundQuotient, tokenRatio uint64) uint64 {
func (st *StateTransition) refundGas(refundQuotient, tokenRatio uint64) {
if st.msg.RunMode == GasEstimationWithSkipCheckBalanceMode || st.msg.RunMode == EthcallMode {
st.gasRemaining = st.gasRemaining * tokenRatio
st.gp.AddGas(st.gasRemaining)
return 0
return
}
// Apply refund counter, capped to a refund quotient
refund := st.gasUsed() / refundQuotient
Expand All @@ -699,8 +673,6 @@ func (st *StateTransition) refundGas(refundQuotient, tokenRatio uint64) uint64 {
// Also return remaining gas to the block gas counter so it is
// available for the next transaction.
st.gp.AddGas(st.gasRemaining)

return refund
}

// gasUsed returns the amount of gas used up by the state transition.
Expand Down Expand Up @@ -801,7 +773,7 @@ func (st *StateTransition) generateBVMETHMintEvent(mintAddress common.Address, m
topics := make([]common.Hash, 2)
topics[0] = methodHash
topics[1] = mintAddress.Hash()
// data means the mint amount in MINT EVENT.
//data means the mint amount in MINT EVENT.
d := common.HexToHash(common.Bytes2Hex(mintValue.Bytes())).Bytes()
st.evm.StateDB.AddLog(&types.Log{
Address: BVM_ETH_ADDR,
Expand All @@ -820,7 +792,7 @@ func (st *StateTransition) generateBVMETHTransferEvent(from, to common.Address,
topics[0] = methodHash
topics[1] = from.Hash()
topics[2] = to.Hash()
// data means the transfer amount in Transfer EVENT.
//data means the transfer amount in Transfer EVENT.
data := common.HexToHash(common.Bytes2Hex(amount.Bytes())).Bytes()
st.evm.StateDB.AddLog(&types.Log{
Address: BVM_ETH_ADDR,
Expand Down
2 changes: 1 addition & 1 deletion eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
config.BlockOverrides.Apply(&vmctx)
}
// Execute the trace
msg, err := args.ToMessage(api.backend.RPCGasCap(), block.BaseFee(), core.EthcallMode)
msg, err := args.ToMessage(api.backend.RPCGasCap(), block.BaseFee(), core.EthcallMode, args.GasPrice)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ func (b *Long) UnmarshalGraphQL(input interface{}) error {
switch input := input.(type) {
case string:
// uncomment to support hex values
// if strings.HasPrefix(input, "0x") {
//if strings.HasPrefix(input, "0x") {
// // apply leniency and support hex representations of longs.
// value, err := hexutil.DecodeUint64(input)
// *b = Long(value)
// return err
// } else {
//} else {
value, err := strconv.ParseInt(input, 10, 64)
*b = Long(value)
return err
// }
//}
case int32:
*b = Long(input)
case int64:
Expand Down Expand Up @@ -1070,7 +1070,7 @@ func (b *Block) Call(ctx context.Context, args struct {
return nil, err
}
}
result, err := ethapi.DoCall(ctx, b.r.backend, args.Data, *b.numberOrHash, nil, b.r.backend.RPCEVMTimeout(), b.r.backend.RPCGasCap(), core.EthcallMode)
result, err := ethapi.DoCall(ctx, b.r.backend, args.Data, *b.numberOrHash, nil, b.r.backend.RPCEVMTimeout(), b.r.backend.RPCGasCap(), core.EthcallMode, args.Data.GasPrice)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1140,7 +1140,7 @@ func (p *Pending) Call(ctx context.Context, args struct {
Data ethapi.TransactionArgs
}) (*CallResult, error) {
pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
result, err := ethapi.DoCall(ctx, p.r.backend, args.Data, pendingBlockNr, nil, p.r.backend.RPCEVMTimeout(), p.r.backend.RPCGasCap(), core.EthcallMode)
result, err := ethapi.DoCall(ctx, p.r.backend, args.Data, pendingBlockNr, nil, p.r.backend.RPCEVMTimeout(), p.r.backend.RPCGasCap(), core.EthcallMode, args.Data.GasPrice)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading