• Tei Im's avatar
    Apply suggestions from code reviews · 23c658d6
    Tei Im authored
    Fix comments
    Check if the span batch is empty in LogContext()
    Check chain ID of the protected tx in newSpanBatchTxs()
    Write SpanBatchType explicitly when encoding span batch
    23c658d6
batch_tob_test.go 1.39 KB
package derive

import (
	"testing"

	"github.com/ethereum-optimism/optimism/op-service/testutils/fuzzerutils"
	fuzz "github.com/google/gofuzz"
	"github.com/stretchr/testify/require"
)

// FuzzBatchRoundTrip executes a fuzz test similar to TestBatchRoundTrip, which tests that arbitrary BatchData will be
// encoded and decoded without loss of its original values.
// Does not test the span batch type because the fuzzer is not aware of the structure of a span batch.
func FuzzBatchRoundTrip(f *testing.F) {
	f.Fuzz(func(t *testing.T, fuzzedData []byte) {
		// Create our fuzzer wrapper to generate complex values
		typeProvider := fuzz.NewFromGoFuzz(fuzzedData).NilChance(0).MaxDepth(10000).NumElements(0, 0x100).AllowUnexportedFields(true)
		fuzzerutils.AddFuzzerFunctions(typeProvider)

		// Create our batch data from fuzzed data
		var batchData BatchData
		typeProvider.Fuzz(&batchData)

		// force batchdata to only contain singular batch
		batchData.BatchType = SingularBatchType
		batchData.RawSpanBatch = RawSpanBatch{}

		// Encode our batch data
		enc, err := batchData.MarshalBinary()
		require.NoError(t, err)

		// Decode our encoded batch data
		var dec BatchData
		err = dec.UnmarshalBinary(enc)
		require.NoError(t, err)

		// Ensure the round trip encoding of batch data did not result in data loss
		require.Equal(t, &batchData, &dec, "round trip batch encoding/decoding did not match original values")
	})
}