github.com/ethereum-optimism/optimism@v1.7.2/op-node/rollup/derive/channel_test.go (about)

     1  package derive
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ethereum-optimism/optimism/op-service/eth"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  type frameValidityTC struct {
    11  	name      string
    12  	frames    []Frame
    13  	shouldErr []bool
    14  	sizes     []uint64
    15  }
    16  
    17  func (tc *frameValidityTC) Run(t *testing.T) {
    18  	id := [16]byte{0xff}
    19  	block := eth.L1BlockRef{}
    20  	ch := NewChannel(id, block)
    21  
    22  	if len(tc.frames) != len(tc.shouldErr) || len(tc.frames) != len(tc.sizes) {
    23  		t.Errorf("lengths should be the same. frames: %d, shouldErr: %d, sizes: %d", len(tc.frames), len(tc.shouldErr), len(tc.sizes))
    24  	}
    25  
    26  	for i, frame := range tc.frames {
    27  		err := ch.AddFrame(frame, block)
    28  		if tc.shouldErr[i] {
    29  			require.NotNil(t, err)
    30  		} else {
    31  			require.Nil(t, err)
    32  		}
    33  		require.Equal(t, tc.sizes[i], ch.Size())
    34  	}
    35  }
    36  
    37  // TestFrameValidity inserts a list of frames into the channel. It checks if an error
    38  // should be returned by `AddFrame` as well as checking the size of the channel.
    39  func TestFrameValidity(t *testing.T) {
    40  	id := [16]byte{0xff}
    41  	testCases := []frameValidityTC{
    42  		{
    43  			name:      "wrong channel",
    44  			frames:    []Frame{{ID: [16]byte{0xee}}},
    45  			shouldErr: []bool{true},
    46  			sizes:     []uint64{0},
    47  		},
    48  		{
    49  			name: "double close",
    50  			frames: []Frame{
    51  				{ID: id, FrameNumber: 2, IsLast: true, Data: []byte("four")},
    52  				{ID: id, FrameNumber: 1, IsLast: true}},
    53  			shouldErr: []bool{false, true},
    54  			sizes:     []uint64{204, 204},
    55  		},
    56  		{
    57  			name: "duplicate frame",
    58  			frames: []Frame{
    59  				{ID: id, FrameNumber: 2, Data: []byte("four")},
    60  				{ID: id, FrameNumber: 2, Data: []byte("seven__")}},
    61  			shouldErr: []bool{false, true},
    62  			sizes:     []uint64{204, 204},
    63  		},
    64  		{
    65  			name: "duplicate closing frames",
    66  			frames: []Frame{
    67  				{ID: id, FrameNumber: 2, IsLast: true, Data: []byte("four")},
    68  				{ID: id, FrameNumber: 2, IsLast: true, Data: []byte("seven__")}},
    69  			shouldErr: []bool{false, true},
    70  			sizes:     []uint64{204, 204},
    71  		},
    72  		{
    73  			name: "frame past closing",
    74  			frames: []Frame{
    75  				{ID: id, FrameNumber: 2, IsLast: true, Data: []byte("four")},
    76  				{ID: id, FrameNumber: 10, Data: []byte("seven__")}},
    77  			shouldErr: []bool{false, true},
    78  			sizes:     []uint64{204, 204},
    79  		},
    80  		{
    81  			name: "prune after close frame",
    82  			frames: []Frame{
    83  				{ID: id, FrameNumber: 10, IsLast: false, Data: []byte("seven__")},
    84  				{ID: id, FrameNumber: 2, IsLast: true, Data: []byte("four")}},
    85  			shouldErr: []bool{false, false},
    86  			sizes:     []uint64{207, 204},
    87  		},
    88  		{
    89  			name: "multiple valid frames",
    90  			frames: []Frame{
    91  				{ID: id, FrameNumber: 10, Data: []byte("seven__")},
    92  				{ID: id, FrameNumber: 2, Data: []byte("four")}},
    93  			shouldErr: []bool{false, false},
    94  			sizes:     []uint64{207, 411},
    95  		},
    96  	}
    97  
    98  	for _, tc := range testCases {
    99  		t.Run(tc.name, tc.Run)
   100  	}
   101  }