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

     1  package driver
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/ethereum/go-ethereum"
    10  	"github.com/ethereum/go-ethereum/common"
    11  
    12  	"github.com/ethereum-optimism/optimism/op-service/eth"
    13  	"github.com/ethereum-optimism/optimism/op-service/testutils"
    14  )
    15  
    16  var exHash = common.Hash{0xff}
    17  
    18  type confTest struct {
    19  	name  string
    20  	head  uint64
    21  	hash  common.Hash // hash of head block
    22  	req   uint64
    23  	depth uint64
    24  	pass  bool
    25  }
    26  
    27  func (ct *confTest) Run(t *testing.T) {
    28  	l1Fetcher := &testutils.MockL1Source{}
    29  	l1Head := eth.L1BlockRef{Number: ct.head, Hash: ct.hash}
    30  	l1HeadGetter := func() eth.L1BlockRef { return l1Head }
    31  
    32  	cd := NewConfDepth(ct.depth, l1HeadGetter, l1Fetcher)
    33  	if ct.pass {
    34  		// no calls to the l1Fetcher are made if the confirmation depth of the request is not met
    35  		l1Fetcher.ExpectL1BlockRefByNumber(ct.req, eth.L1BlockRef{Number: ct.req}, nil)
    36  	}
    37  	out, err := cd.L1BlockRefByNumber(context.Background(), ct.req)
    38  	l1Fetcher.AssertExpectations(t)
    39  	if ct.pass {
    40  		require.NoError(t, err)
    41  		require.Equal(t, out, eth.L1BlockRef{Number: ct.req})
    42  	} else {
    43  		require.Equal(t, ethereum.NotFound, err)
    44  	}
    45  }
    46  
    47  func TestConfDepth(t *testing.T) {
    48  	// note: we're not testing overflows.
    49  	// If a request is large enough to overflow the conf depth check, it's not returning anything anyway.
    50  	testCases := []confTest{
    51  		{name: "zero conf future", head: 4, hash: exHash, req: 5, depth: 0, pass: true},
    52  		{name: "zero conf present", head: 4, hash: exHash, req: 4, depth: 0, pass: true},
    53  		{name: "zero conf past", head: 4, hash: exHash, req: 4, depth: 0, pass: true},
    54  		{name: "one conf future", head: 4, hash: exHash, req: 5, depth: 1, pass: false},
    55  		{name: "one conf present", head: 4, hash: exHash, req: 4, depth: 1, pass: false},
    56  		{name: "one conf past", head: 4, hash: exHash, req: 3, depth: 1, pass: true},
    57  		{name: "two conf future", head: 4, hash: exHash, req: 5, depth: 2, pass: false},
    58  		{name: "two conf present", head: 4, hash: exHash, req: 4, depth: 2, pass: false},
    59  		{name: "two conf not like 1", head: 4, hash: exHash, req: 3, depth: 2, pass: false},
    60  		{name: "two conf pass", head: 4, hash: exHash, req: 2, depth: 2, pass: true},
    61  		{name: "easy pass", head: 100, hash: exHash, req: 20, depth: 5, pass: true},
    62  		{name: "genesis case", head: 0, hash: exHash, req: 0, depth: 4, pass: true},
    63  		{name: "no L1 state", req: 10, depth: 4, pass: true},
    64  	}
    65  	for _, tc := range testCases {
    66  		t.Run(tc.name, tc.Run)
    67  	}
    68  }