Skip to content

Commit

Permalink
change handling of non-zero padding for version 0 scalar (ethereum-op…
Browse files Browse the repository at this point in the history
…timism#9068)

Co-authored-by: Roberto Bayardo <[email protected]>
  • Loading branch information
protolambda and roberto-bayardo authored Jan 18, 2024
1 parent 705db87 commit db28a83
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
13 changes: 12 additions & 1 deletion op-service/eth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package eth
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
"math/big"
"reflect"
"strconv"
Expand All @@ -24,6 +26,10 @@ const (
InvalidPayloadAttributes ErrorCode = -38003 // Payload attributes are invalid / inconsistent.
)

var (
ErrBedrockScalarPaddingNotEmpty = errors.New("version 0 scalar value has non-empty padding")
)

// InputError distinguishes an user-input error from regular rpc errors,
// to help the (Engine) API user divert from accidental input mistakes.
type InputError struct {
Expand Down Expand Up @@ -347,6 +353,11 @@ const (

func (sysCfg *SystemConfig) EcotoneScalars() (blobBaseFeeScalar, baseFeeScalar uint32, err error) {
if err := CheckEcotoneL1SystemConfigScalar(sysCfg.Scalar); err != nil {
if errors.Is(err, ErrBedrockScalarPaddingNotEmpty) {
// L2 spec mandates we set baseFeeScalar to MaxUint32 if there are non-zero bytes in
// the padding area.
return 0, math.MaxUint32, nil
}
return 0, 0, err
}
switch sysCfg.Scalar[0] {
Expand All @@ -367,7 +378,7 @@ func CheckEcotoneL1SystemConfigScalar(scalar [32]byte) error {
switch versionByte {
case L1ScalarBedrock:
if ([27]byte)(scalar[1:28]) != ([27]byte{}) { // check padding
return fmt.Errorf("invalid version 0 scalar padding: %x", scalar[1:28])
return ErrBedrockScalarPaddingNotEmpty
}
return nil
case L1ScalarEcotone:
Expand Down
6 changes: 4 additions & 2 deletions op-service/eth/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eth

import (
"errors"
"math"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -29,9 +30,10 @@ type scalarTest struct {

func TestEcotoneScalars(t *testing.T) {
testCases := []scalarTest{
{"invalid v0 scalar", Bytes32{0: 0, 27: 1, 31: 2}, true, 0, 0},
{"dirty padding v0 scalar", Bytes32{0: 0, 27: 1, 31: 2}, false, 0, math.MaxUint32},
{"dirty padding v0 scalar v2", Bytes32{0: 0, 1: 1, 31: 2}, false, 0, math.MaxUint32},
{"valid v0 scalar", Bytes32{0: 0, 27: 0, 31: 2}, false, 0, 2},
{"invalid v1 scalar", Bytes32{0: 0, 7: 1, 31: 2}, true, 0, 0},
{"invalid v1 scalar", Bytes32{0: 1, 7: 1, 31: 2}, true, 0, 0},
{"valid v1 scalar with 0 blob scalar", Bytes32{0: 1, 27: 0, 31: 2}, false, 0, 2},
{"valid v1 scalar with non-0 blob scalar", Bytes32{0: 1, 27: 123, 31: 2}, false, 123, 2},
{"valid v1 scalar with non-0 blob scalar and 0 scalar", Bytes32{0: 1, 27: 123, 31: 0}, false, 123, 0},
Expand Down
3 changes: 2 additions & 1 deletion specs/system_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ The `scalar` is encoded as big-endian `uint256`, interpreted as `bytes32`, and c
- `0`: scalar-version byte
- `[1, 32)`: depending scalar-version:
- Scalar-version `0`:
- `[1, 28)`: padding, must be zero.
- `[1, 28)`: padding, should be zero.
- `[28, 32)`: big-endian `uint32`, encoding the L1-fee `baseFeeScalar`
- This version implies the L1-fee `blobBaseFeeScalar` is set to 0.
- In the event there are non-zero bytes in the padding area, `baseFeeScalar` must be set to MaxUint32.
- This version is compatible with the pre-Ecotone `scalar` value (assuming a `uint32` range).
- Scalar-version `1`:
- `[1, 24)`: padding, must be zero.
Expand Down

0 comments on commit db28a83

Please sign in to comment.