Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Commit

Permalink
improve: adding missing safe check for L2 Min Gas Price.
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
  • Loading branch information
Psykepro committed Jun 19, 2023
1 parent 42dd793 commit d438ffa
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
3 changes: 3 additions & 0 deletions pool/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ var (

// ErrGasPrice is returned if the transaction has specified lower gas price than the minimum allowed.
ErrGasPrice = errors.New("gas price too low")

// ErrReceivedZeroL1GasPrice is returned if the L1 gas price is 0.
ErrReceivedZeroL1GasPrice = errors.New("received L1 gas price 0")
)
6 changes: 3 additions & 3 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,16 +487,16 @@ func (p *Pool) CalculateTxBreakEvenGasPrice(ctx context.Context, txDataLength ui
}
if gasPrices.L1GasPrice == 0 {
log.Warn("Received L1 gas price 0. Skipping estimation...")
return nil, errors.New("received L1 gas price 0")
return nil, ErrReceivedZeroL1GasPrice
}

// Get L2 Min Gas Price
l2MinGasPrice := (gasPrices.L1GasPrice * p.cfg.EffectiveGasPrice.L1GasPriceFactor) / 100 //nolint:gomnd
if err != nil {
return nil, err
}
if l2MinGasPrice == 0 {
return nil, fmt.Errorf("error L2 Min Gas Price can't be 0")
if l2MinGasPrice < p.cfg.DefaultMinGasPriceAllowed {
l2MinGasPrice = p.cfg.DefaultMinGasPriceAllowed
}

// Calculate break even gas price
Expand Down
24 changes: 6 additions & 18 deletions pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ func TestCalculateTxBreakEvenGasPrice(t *testing.T) {
gasUsed uint64
l1GasPrice *big.Int
expectedBreakEvenGasPrice *big.Int
expectError bool
expectError error
cfg pool.Config
}{
{
Expand All @@ -1471,31 +1471,19 @@ func TestCalculateTxBreakEvenGasPrice(t *testing.T) {
{
desc: "L1 gas price is zero",
l1GasPrice: big.NewInt(0),
expectError: true,
expectError: pool.ErrReceivedZeroL1GasPrice,
gasUsed: 5000,
cfg: normalCfg,
},
{
desc: "L2 gas price is zero",
gasUsed: 5000,
expectError: true,
cfg: l1MinGasPricePercentageZeroCfg,
},
{
desc: "L1 gas price and L2 gas price are zero",
gasUsed: 5000,
expectError: true,
cfg: l1MinGasPricePercentageZeroCfg,
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
if tc.l1GasPrice != nil {
oldGasPrice := gasPrice
oldL1GasPrice := gasPrice
l1GasPrice = tc.l1GasPrice
defer func() {
l1GasPrice = oldGasPrice
l1GasPrice = oldL1GasPrice
}()
}

Expand Down Expand Up @@ -1538,8 +1526,8 @@ func TestCalculateTxBreakEvenGasPrice(t *testing.T) {
p := setupPool(t, tc.cfg, s, st, chainID, ctx, eventLog)

breakEvenGasPrice, err := p.CalculateTxBreakEvenGasPrice(ctx, tc.txDataLength, tc.gasUsed)
if tc.expectError {
assert.Error(t, err)
if tc.expectError != nil {
assert.ErrorIs(t, err, tc.expectError)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedBreakEvenGasPrice, breakEvenGasPrice)
Expand Down
4 changes: 2 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ run-node: ## Runs the node
$(RUNSEQUENCESENDER)
$(RUNL2GASPRICER)
$(RUNAGGREGATOR)
$(RUNJSONRPC)
#$(RUNJSONRPC)

.PHONY: stop-node
stop-node: ## Stops the node
Expand Down Expand Up @@ -449,7 +449,7 @@ run: ## Runs a full node
$(RUNSEQUENCESENDER)
$(RUNL2GASPRICER)
$(RUNAGGREGATOR)
$(RUNJSONRPC)
#$(RUNJSONRPC)

.PHONY: stop
stop: ## Stops all services
Expand Down
2 changes: 1 addition & 1 deletion test/config/test.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ PrivateKeys = [
]

[L2GasPriceSuggester]
Type = "default"
Type = "follower"
UpdatePeriod = "10s"
Factor = 0.5
DefaultGasPriceWei = 1000000000
Expand Down

0 comments on commit d438ffa

Please sign in to comment.