Commit 4317c093 authored by zhiqiangxu's avatar zhiqiangxu Committed by GitHub

refactor: single source of truth for `DerivationVersion` (#11901)

* single source of truth for DerivationVersion

* op-node/rollup: fix imports

---------
Co-authored-by: default avatarprotolambda <proto@protolambda.com>
parent cf1ce689
......@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive/params"
"github.com/ethereum/go-ethereum/crypto"
)
......@@ -115,7 +116,7 @@ func (c Keccak256Commitment) Encode() []byte {
// TxData adds an extra version byte to signal it's a commitment.
func (c Keccak256Commitment) TxData() []byte {
return append([]byte{TxDataVersion1}, c.Encode()...)
return append([]byte{params.DerivationVersion1}, c.Encode()...)
}
// Verify checks if the commitment matches the given input.
......@@ -155,7 +156,7 @@ func (c GenericCommitment) Encode() []byte {
// TxData adds an extra version byte to signal it's a commitment.
func (c GenericCommitment) TxData() []byte {
return append([]byte{TxDataVersion1}, c.Encode()...)
return append([]byte{params.DerivationVersion1}, c.Encode()...)
}
// Verify always returns true for GenericCommitment because the DA Server must validate the data before returning it to the op-node.
......
......@@ -3,6 +3,7 @@ package altda
import (
"testing"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive/params"
"github.com/stretchr/testify/require"
)
......@@ -54,7 +55,7 @@ func TestCommitmentData(t *testing.T) {
// Test that reencoding the commitment returns the same data
require.Equal(t, tc.commData, comm.Encode())
// Test that TxData() returns the same data as the original, prepended with a version byte
require.Equal(t, append([]byte{TxDataVersion1}, tc.commData...), comm.TxData())
require.Equal(t, append([]byte{params.DerivationVersion1}, tc.commData...), comm.TxData())
// Test that Verify() returns no error for the correct data
require.NoError(t, comm.Verify(tc.commData))
......
......@@ -4,8 +4,3 @@ package altda
// challenge in the Data Availability Challenge contract. Value in number of bytes.
// This value can only be changed in a hard fork.
const MaxInputSize = 130672
// TxDataVersion1 is the version number for batcher transactions containing
// altDA commitments. It should not collide with DerivationVersion which is still
// used downstream when parsing the frames.
const TxDataVersion1 = 1
......@@ -5,6 +5,7 @@ import (
"strings"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive/params"
"github.com/ethereum-optimism/optimism/op-service/eth"
)
......@@ -35,7 +36,7 @@ func (td *txData) ID() txID {
// It's a version byte (0) followed by the concatenated frames for this transaction.
func (td *txData) CallData() []byte {
data := make([]byte, 1, 1+td.Len())
data[0] = derive.DerivationVersion0
data[0] = params.DerivationVersion0
for _, f := range td.frames {
data = append(data, f.data...)
}
......@@ -46,7 +47,7 @@ func (td *txData) Blobs() ([]*eth.Blob, error) {
blobs := make([]*eth.Blob, 0, len(td.frames))
for _, f := range td.frames {
var blob eth.Blob
if err := blob.FromData(append([]byte{derive.DerivationVersion0}, f.data...)); err != nil {
if err := blob.FromData(append([]byte{params.DerivationVersion0}, f.data...)); err != nil {
return nil, err
}
blobs = append(blobs, &blob)
......
......@@ -26,6 +26,7 @@ import (
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
derive_params "github.com/ethereum-optimism/optimism/op-node/rollup/derive/params"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
)
......@@ -295,7 +296,7 @@ func (s *L2Batcher) ReadNextOutputFrame(t Testing) []byte {
}
// Collect the output frame
data := new(bytes.Buffer)
data.WriteByte(derive.DerivationVersion0)
data.WriteByte(derive_params.DerivationVersion0)
// subtract one, to account for the version byte
if _, err := s.L2ChannelOut.OutputFrame(data, s.l2BatcherCfg.MaxL1TxSize-1); err == io.EOF {
s.L2ChannelOut = nil
......@@ -400,7 +401,7 @@ func (s *L2Batcher) ActL2BatchSubmitMultiBlob(t Testing, numBlobs int) {
blobs := make([]*eth.Blob, numBlobs)
for i := 0; i < numBlobs; i++ {
data := new(bytes.Buffer)
data.WriteByte(derive.DerivationVersion0)
data.WriteByte(derive_params.DerivationVersion0)
// write only a few bytes to all but the last blob
l := uint64(derive.FrameV0OverHeadSize + 4) // 4 bytes content
if i == numBlobs-1 {
......
......@@ -6,6 +6,7 @@ import (
"fmt"
altda "github.com/ethereum-optimism/optimism/op-alt-da"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive/params"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/log"
)
......@@ -56,7 +57,7 @@ func (s *AltDADataSource) Next(ctx context.Context) (eth.Data, error) {
}
// If the tx data type is not altDA, we forward it downstream to let the next
// steps validate and potentially parse it as L1 DA inputs.
if data[0] != altda.TxDataVersion1 {
if data[0] != params.DerivationVersion1 {
return data, nil
}
......
......@@ -8,6 +8,7 @@ import (
"io"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive/params"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
......@@ -275,7 +276,7 @@ func ForceCloseTxData(frames []Frame) ([]byte, error) {
}
var out bytes.Buffer
out.WriteByte(DerivationVersion0)
out.WriteByte(params.DerivationVersion0)
if !closed {
f := Frame{
......
......@@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"io"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive/params"
)
// Frames cannot be larger than 1 MB.
......@@ -130,7 +132,7 @@ func ParseFrames(data []byte) ([]Frame, error) {
if len(data) == 0 {
return nil, errors.New("data array must not be empty")
}
if data[0] != DerivationVersion0 {
if data[0] != params.DerivationVersion0 {
return nil, fmt.Errorf("invalid derivation format byte: got %d", data[0])
}
buf := bytes.NewBuffer(data[1:])
......
......@@ -7,12 +7,14 @@ import (
"log/slog"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive/mocks"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive/params"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestPruneFrameQueue(t *testing.T) {
......@@ -126,7 +128,7 @@ func testFrameQueue_NextFrame(t *testing.T, holocene bool) {
}
var inBuf bytes.Buffer
inBuf.WriteByte(DerivationVersion0)
inBuf.WriteByte(params.DerivationVersion0)
for _, f := range inFrames {
require.NoError(t, f.MarshalBinary(&inBuf))
}
......
......@@ -9,8 +9,10 @@ import (
"testing"
"time"
"github.com/ethereum-optimism/optimism/op-service/testutils"
"github.com/stretchr/testify/require"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive/params"
"github.com/ethereum-optimism/optimism/op-service/testutils"
)
func FuzzFrameUnmarshalBinary(f *testing.F) {
......@@ -164,7 +166,7 @@ func TestParseFramesInvalidVer(t *testing.T) {
}
func TestParseFramesOnlyVersion(t *testing.T) {
frames, err := ParseFrames([]byte{DerivationVersion0})
frames, err := ParseFrames([]byte{params.DerivationVersion0})
require.Empty(t, frames)
require.Error(t, err)
}
......@@ -206,7 +208,7 @@ func TestParseFramesTruncated(t *testing.T) {
// frames.
func txMarshalFrames(frames []Frame) ([]byte, error) {
var data bytes.Buffer
if err := data.WriteByte(DerivationVersion0); err != nil {
if err := data.WriteByte(params.DerivationVersion0); err != nil {
return nil, err
}
for _, frame := range frames {
......
......@@ -4,8 +4,6 @@ import (
"encoding/hex"
"errors"
"fmt"
altda "github.com/ethereum-optimism/optimism/op-alt-da"
)
// count the tagging info as 200 in terms of buffer size.
......@@ -19,11 +17,6 @@ func frameSize(frame Frame) uint64 {
return uint64(len(frame.Data)) + frameOverhead
}
const DerivationVersion0 = 0
// DerivationVersion1 is reserved for batcher transactions containing altDA commitments.
const DerivationVersion1 = altda.TxDataVersion1
// MaxSpanBatchElementCount is the maximum number of blocks, transactions in total,
// or transaction per block allowed in a span batch.
const MaxSpanBatchElementCount = 10_000_000
......
package params
const DerivationVersion0 = 0
// DerivationVersion1 is reserved for batcher transactions containing altDA commitments.
const DerivationVersion1 = 1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment