Skip to content

Commit

Permalink
calculate effective gas percentage after mordor (#36)
Browse files Browse the repository at this point in the history
* calculate effective gas percentage after mordor

* removed commented code
  • Loading branch information
V-Staykov authored Jan 8, 2024
1 parent 3bbdfb6 commit 9584b25
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
12 changes: 3 additions & 9 deletions cmd/rpcdaemon/commands/zkevm_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import (

"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/common/u256"
"github.com/ledgerwatch/erigon/core"
eritypes "github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/sync_stages"
"github.com/ledgerwatch/erigon/zk/hermez_db"
types "github.com/ledgerwatch/erigon/zk/rpcdaemon"
zktypes "github.com/ledgerwatch/erigon/zk/types"
"github.com/ledgerwatch/erigon/zkevm/jsonrpc/client"
)

Expand Down Expand Up @@ -534,13 +533,8 @@ func convertReceipt(
}

var effectiveGasPrice *types.ArgBig
gas := gasPrice.Clone()
if effectiveGasPricePercentage > zktypes.EFFECTIVE_GAS_PRICE_PERCENTAGE_DISABLED && gasPrice != nil {
clone := gasPrice.Clone()
effectiveGasPricePerc := new(uint256.Int).SetUint64(uint64(effectiveGasPricePercentage))
effectiveGasPricePerc.Add(effectiveGasPricePerc, u256.Num1)
clone.Mul(clone, effectiveGasPricePerc)
gas.Div(clone, zktypes.EFFECTIVE_GAS_PRICE_MAX_VAL)
if gasPrice != nil {
gas := core.CalculateEffectiveGas(gasPrice, effectiveGasPricePercentage)
asBig := types.ArgBig(*gas.ToBig())
effectiveGasPrice = &asBig
}
Expand Down
21 changes: 13 additions & 8 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,9 @@ func NewStateTransition(evm vm.VMInterface, msg Message, gp *GasPool) *StateTran

gas := msg.GasPrice()

// check if we have an effective gas price percentage in the message and apply it
ep := msg.EffectiveGasPricePercentage()
if ep > zktypes.EFFECTIVE_GAS_PRICE_PERCENTAGE_DISABLED {
val := gas.Clone()
epi := new(uint256.Int).SetUint64(uint64(ep))
epi = epi.Add(epi, u256.Num1)
val = val.Mul(val, epi)
gas = gas.Div(val, zktypes.EFFECTIVE_GAS_PRICE_MAX_VAL)
if evm.ChainRules().IsMordor {
ep := msg.EffectiveGasPricePercentage()
gas = CalculateEffectiveGas(gas, ep)
}

return &StateTransition{
Expand All @@ -185,6 +180,16 @@ func NewStateTransition(evm vm.VMInterface, msg Message, gp *GasPool) *StateTran
}
}

func CalculateEffectiveGas(gas *uint256.Int, ep uint8) *uint256.Int {
val := gas.Clone()
epi := new(uint256.Int).SetUint64(uint64(ep))
epi = epi.Add(epi, u256.Num1)
val = val.Mul(val, epi)
gas = gas.Div(val, zktypes.EFFECTIVE_GAS_PRICE_MAX_VAL)

return gas
}

// ApplyMessage computes the new state by applying the given message
// against the old state within the environment.
//
Expand Down

0 comments on commit 9584b25

Please sign in to comment.