Skip to content

Commit

Permalink
fix debug trace to compute egp percentage based on tx execution values (
Browse files Browse the repository at this point in the history
#3503)

* fix debug trace to compute egp percentage based on tx execution values

* fix trace egp percentage for injected tx
  • Loading branch information
tclemos authored Mar 26, 2024
1 parent df539db commit 790e78a
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 76 deletions.
2 changes: 1 addition & 1 deletion jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func (z *ZKEVMEndpoints) internalEstimateGasPriceAndFee(ctx context.Context, arg

if txEGP.Cmp(txGasPrice) == -1 { // txEGP < txGasPrice
// We need to "round" the final effectiveGasPrice to a 256 fraction of the txGasPrice
txEGPPct, err = z.pool.CalculateEffectiveGasPricePercentage(txGasPrice, txEGP)
txEGPPct, err = state.CalculateEffectiveGasPricePercentage(txGasPrice, txEGP)
if err != nil {
return nil, nil, types.NewRPCError(types.DefaultErrorCode, "failed to calculate effective gas price percentage", err, false)
}
Expand Down
28 changes: 0 additions & 28 deletions jsonrpc/mocks/mock_pool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion jsonrpc/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type PoolInterface interface {
GetTransactionByHash(ctx context.Context, hash common.Hash) (*pool.Transaction, error)
GetTransactionByL2Hash(ctx context.Context, hash common.Hash) (*pool.Transaction, error)
CalculateEffectiveGasPrice(rawTx []byte, txGasPrice *big.Int, txGasUsed uint64, l1GasPrice uint64, l2GasPrice uint64) (*big.Int, error)
CalculateEffectiveGasPricePercentage(gasPrice *big.Int, effectiveGasPrice *big.Int) (uint8, error)
EffectiveGasPriceEnabled() bool
}

Expand Down
36 changes: 1 addition & 35 deletions pool/effectivegasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@ package pool

import (
"bytes"
"errors"
"math/big"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
)

var (
// ErrEffectiveGasPriceEmpty happens when the effectiveGasPrice or gasPrice is nil or zero
ErrEffectiveGasPriceEmpty = errors.New("effectiveGasPrice or gasPrice cannot be nil or zero")

// ErrEffectiveGasPriceIsZero happens when the calculated EffectiveGasPrice is zero
ErrEffectiveGasPriceIsZero = errors.New("effectiveGasPrice cannot be zero")
)

// EffectiveGasPrice implements the effective gas prices calculations and checks
type EffectiveGasPrice struct {
cfg EffectiveGasPriceCfg
Expand Down Expand Up @@ -122,33 +113,8 @@ func (e *EffectiveGasPrice) CalculateEffectiveGasPrice(rawTx []byte, txGasPrice
bfEffectiveGasPrice.Int(effectiveGasPrice)

if effectiveGasPrice.Cmp(new(big.Int).SetUint64(0)) == 0 {
return nil, ErrEffectiveGasPriceIsZero
return nil, state.ErrEffectiveGasPriceIsZero
}

return effectiveGasPrice, nil
}

// CalculateEffectiveGasPricePercentage calculates the gas price's effective percentage
func (e *EffectiveGasPrice) CalculateEffectiveGasPricePercentage(gasPrice *big.Int, effectiveGasPrice *big.Int) (uint8, error) {
const bits = 256
var bitsBigInt = big.NewInt(bits)

if effectiveGasPrice == nil || gasPrice == nil ||
gasPrice.Cmp(big.NewInt(0)) == 0 || effectiveGasPrice.Cmp(big.NewInt(0)) == 0 {
return 0, ErrEffectiveGasPriceEmpty
}

if gasPrice.Cmp(effectiveGasPrice) <= 0 {
return state.MaxEffectivePercentage, nil
}

// Simulate Ceil with integer division
b := new(big.Int).Mul(effectiveGasPrice, bitsBigInt)
b = b.Add(b, gasPrice)
b = b.Sub(b, big.NewInt(1)) //nolint:gomnd
b = b.Div(b, gasPrice)
// At this point we have a percentage between 1-256, we need to sub 1 to have it between 0-255 (byte)
b = b.Sub(b, big.NewInt(1)) //nolint:gomnd

return uint8(b.Uint64()), nil
}
9 changes: 4 additions & 5 deletions pool/effectivegasprice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/big"
"testing"

"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/stretchr/testify/assert"
)

Expand All @@ -23,8 +24,6 @@ var (
)

func TestCalculateEffectiveGasPricePercentage(t *testing.T) {
egp := NewEffectiveGasPrice(egpCfg)

testCases := []struct {
name string
breakEven *big.Int
Expand All @@ -37,14 +36,14 @@ func TestCalculateEffectiveGasPricePercentage(t *testing.T) {
name: "Nil breakEven or gasPrice",
gasPrice: big.NewInt(1),
expectedValue: uint8(0),
err: ErrEffectiveGasPriceEmpty,
err: state.ErrEffectiveGasPriceEmpty,
},
{
name: "Zero breakEven or gasPrice",
breakEven: big.NewInt(1),
gasPrice: big.NewInt(0),
expectedValue: uint8(0),
err: ErrEffectiveGasPriceEmpty,
err: state.ErrEffectiveGasPriceEmpty,
},
{
name: "Both positive, gasPrice less than breakEven",
Expand Down Expand Up @@ -104,7 +103,7 @@ func TestCalculateEffectiveGasPricePercentage(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual, err := egp.CalculateEffectiveGasPricePercentage(tc.gasPrice, tc.breakEven)
actual, err := state.CalculateEffectiveGasPricePercentage(tc.gasPrice, tc.breakEven)
assert.Equal(t, tc.err, err)
if actual != 0 {
assert.Equal(t, tc.expectedValue, actual)
Expand Down
2 changes: 1 addition & 1 deletion pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ func (p *Pool) CalculateEffectiveGasPrice(rawTx []byte, txGasPrice *big.Int, txG

// CalculateEffectiveGasPricePercentage calculates the gas price's effective percentage
func (p *Pool) CalculateEffectiveGasPricePercentage(gasPrice *big.Int, effectiveGasPrice *big.Int) (uint8, error) {
return p.effectiveGasPrice.CalculateEffectiveGasPricePercentage(gasPrice, effectiveGasPrice)
return state.CalculateEffectiveGasPricePercentage(gasPrice, effectiveGasPrice)
}

// EffectiveGasPriceEnabled returns if effective gas price calculation is enabled or not
Expand Down
4 changes: 2 additions & 2 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker, first
}
}

egpPercentage, err := f.effectiveGasPrice.CalculateEffectiveGasPricePercentage(txGasPrice, tx.EffectiveGasPrice)
egpPercentage, err := state.CalculateEffectiveGasPricePercentage(txGasPrice, tx.EffectiveGasPrice)
if err != nil {
if f.effectiveGasPrice.IsEnabled() {
return nil, err
Expand Down Expand Up @@ -549,7 +549,7 @@ func (f *finalizer) handleProcessTransactionResponse(ctx context.Context, tx *Tx

// If EffectiveGasPrice is disabled we will calculate the percentage and save it for later logging
if !egpEnabled {
effectivePercentage, err := f.effectiveGasPrice.CalculateEffectiveGasPricePercentage(txGasPrice, tx.EffectiveGasPrice)
effectivePercentage, err := state.CalculateEffectiveGasPricePercentage(txGasPrice, tx.EffectiveGasPrice)
if err != nil {
log.Warnf("effectiveGasPrice is disabled, but failed to calculate effective gas price percentage (#2), error: %v", err)
tx.EGPLog.Error = fmt.Sprintf("%s, CalculateEffectiveGasPricePercentage#2: %s", tx.EGPLog.Error, err)
Expand Down
44 changes: 44 additions & 0 deletions state/effectivegasprice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package state

import (
"errors"
"math/big"
)

const (
// MaxEffectivePercentage is the maximum value that can be used as effective percentage
MaxEffectivePercentage = uint8(255)
)

var (
// ErrEffectiveGasPriceEmpty happens when the effectiveGasPrice or gasPrice is nil or zero
ErrEffectiveGasPriceEmpty = errors.New("effectiveGasPrice or gasPrice cannot be nil or zero")

// ErrEffectiveGasPriceIsZero happens when the calculated EffectiveGasPrice is zero
ErrEffectiveGasPriceIsZero = errors.New("effectiveGasPrice cannot be zero")
)

// CalculateEffectiveGasPricePercentage calculates the gas price's effective percentage
func CalculateEffectiveGasPricePercentage(gasPrice *big.Int, effectiveGasPrice *big.Int) (uint8, error) {
const bits = 256
var bitsBigInt = big.NewInt(bits)

if effectiveGasPrice == nil || gasPrice == nil ||
gasPrice.Cmp(big.NewInt(0)) == 0 || effectiveGasPrice.Cmp(big.NewInt(0)) == 0 {
return 0, ErrEffectiveGasPriceEmpty
}

if gasPrice.Cmp(effectiveGasPrice) <= 0 {
return MaxEffectivePercentage, nil
}

// Simulate Ceil with integer division
b := new(big.Int).Mul(effectiveGasPrice, bitsBigInt)
b = b.Add(b, gasPrice)
b = b.Sub(b, big.NewInt(1)) //nolint:gomnd
b = b.Div(b, gasPrice)
// At this point we have a percentage between 1-256, we need to sub 1 to have it between 0-255 (byte)
b = b.Sub(b, big.NewInt(1)) //nolint:gomnd

return uint8(b.Uint64()), nil
}
2 changes: 0 additions & 2 deletions state/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const (
double = 2
ether155V = 27
etherPre155V = 35
// MaxEffectivePercentage is the maximum value that can be used as effective percentage
MaxEffectivePercentage = uint8(255)
// Decoding constants
headerByteLength uint64 = 1
sLength uint64 = 32
Expand Down
10 changes: 9 additions & 1 deletion state/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ func (s *State) DebugTransaction(ctx context.Context, transactionHash common.Has
var effectivePercentage []uint8
for i := 0; i <= count; i++ {
txsToEncode = append(txsToEncode, *l2Block.Transactions()[i])
effectivePercentage = append(effectivePercentage, MaxEffectivePercentage)
txGasPrice := tx.GasPrice()
effectiveGasPrice := receipt.EffectiveGasPrice
egpPercentage, err := CalculateEffectiveGasPricePercentage(txGasPrice, effectiveGasPrice)
if errors.Is(err, ErrEffectiveGasPriceEmpty) {
egpPercentage = MaxEffectivePercentage
} else if err != nil {
return nil, err
}
effectivePercentage = append(effectivePercentage, egpPercentage)
log.Debugf("trace will reprocess tx: %v", l2Block.Transactions()[i].Hash().String())
}

Expand Down

0 comments on commit 790e78a

Please sign in to comment.