retry_test.go 8.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
package prefetcher

import (
	"context"
	"errors"
	"testing"

	"github.com/ethereum-optimism/optimism/op-node/eth"
	"github.com/ethereum-optimism/optimism/op-node/testlog"
	"github.com/ethereum-optimism/optimism/op-node/testutils"
	"github.com/ethereum-optimism/optimism/op-service/backoff"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/log"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
)

func TestRetryingL1Source(t *testing.T) {
	ctx := context.Background()
	hash := common.Hash{0xab}
	info := &testutils.MockBlockInfo{InfoHash: hash}
	// The mock really doesn't like returning nil for a eth.BlockInfo so return a value we expect to be ignored instead
	wrongInfo := &testutils.MockBlockInfo{InfoHash: common.Hash{0x99}}
	txs := types.Transactions{
		&types.Transaction{},
	}
	rcpts := types.Receipts{
		&types.Receipt{},
	}

	t.Run("InfoByHash Success", func(t *testing.T) {
		source, mock := createL1Source(t)
		defer mock.AssertExpectations(t)
		mock.ExpectInfoByHash(hash, info, nil)

		result, err := source.InfoByHash(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, info, result)
	})

	t.Run("InfoByHash Error", func(t *testing.T) {
		source, mock := createL1Source(t)
		defer mock.AssertExpectations(t)
		expectedErr := errors.New("boom")
		mock.ExpectInfoByHash(hash, wrongInfo, expectedErr)
		mock.ExpectInfoByHash(hash, info, nil)

		result, err := source.InfoByHash(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, info, result)
	})

	t.Run("InfoAndTxsByHash Success", func(t *testing.T) {
		source, mock := createL1Source(t)
		defer mock.AssertExpectations(t)
		mock.ExpectInfoAndTxsByHash(hash, info, txs, nil)

		actualInfo, actualTxs, err := source.InfoAndTxsByHash(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, info, actualInfo)
		require.Equal(t, txs, actualTxs)
	})

	t.Run("InfoAndTxsByHash Error", func(t *testing.T) {
		source, mock := createL1Source(t)
		defer mock.AssertExpectations(t)
		expectedErr := errors.New("boom")
		mock.ExpectInfoAndTxsByHash(hash, wrongInfo, nil, expectedErr)
		mock.ExpectInfoAndTxsByHash(hash, info, txs, nil)

		actualInfo, actualTxs, err := source.InfoAndTxsByHash(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, info, actualInfo)
		require.Equal(t, txs, actualTxs)
	})

	t.Run("FetchReceipts Success", func(t *testing.T) {
		source, mock := createL1Source(t)
		defer mock.AssertExpectations(t)
		mock.ExpectFetchReceipts(hash, info, rcpts, nil)

		actualInfo, actualRcpts, err := source.FetchReceipts(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, info, actualInfo)
		require.Equal(t, rcpts, actualRcpts)
	})

	t.Run("FetchReceipts Error", func(t *testing.T) {
		source, mock := createL1Source(t)
		defer mock.AssertExpectations(t)
		expectedErr := errors.New("boom")
		mock.ExpectFetchReceipts(hash, wrongInfo, nil, expectedErr)
		mock.ExpectFetchReceipts(hash, info, rcpts, nil)

		actualInfo, actualRcpts, err := source.FetchReceipts(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, info, actualInfo)
		require.Equal(t, rcpts, actualRcpts)
	})
}

func createL1Source(t *testing.T) (*RetryingL1Source, *testutils.MockL1Source) {
	logger := testlog.Logger(t, log.LvlDebug)
	mock := &testutils.MockL1Source{}
	source := NewRetryingL1Source(logger, mock)
	// Avoid sleeping in tests by using a fixed backoff strategy with no delay
	source.strategy = backoff.Fixed(0)
	return source, mock
}

func TestRetryingL2Source(t *testing.T) {
	ctx := context.Background()
	hash := common.Hash{0xab}
	info := &testutils.MockBlockInfo{InfoHash: hash}
	// The mock really doesn't like returning nil for a eth.BlockInfo so return a value we expect to be ignored instead
	wrongInfo := &testutils.MockBlockInfo{InfoHash: common.Hash{0x99}}
	txs := types.Transactions{
		&types.Transaction{},
	}
	data := []byte{1, 2, 3, 4, 5}
122 123
	output := &eth.OutputV0{}
	wrongOutput := &eth.OutputV0{BlockHash: common.Hash{0x99}}
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

	t.Run("InfoAndTxsByHash Success", func(t *testing.T) {
		source, mock := createL2Source(t)
		defer mock.AssertExpectations(t)
		mock.ExpectInfoAndTxsByHash(hash, info, txs, nil)

		actualInfo, actualTxs, err := source.InfoAndTxsByHash(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, info, actualInfo)
		require.Equal(t, txs, actualTxs)
	})

	t.Run("InfoAndTxsByHash Error", func(t *testing.T) {
		source, mock := createL2Source(t)
		defer mock.AssertExpectations(t)
		expectedErr := errors.New("boom")
		mock.ExpectInfoAndTxsByHash(hash, wrongInfo, nil, expectedErr)
		mock.ExpectInfoAndTxsByHash(hash, info, txs, nil)

		actualInfo, actualTxs, err := source.InfoAndTxsByHash(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, info, actualInfo)
		require.Equal(t, txs, actualTxs)
	})

	t.Run("NodeByHash Success", func(t *testing.T) {
		source, mock := createL2Source(t)
		defer mock.AssertExpectations(t)
		mock.ExpectNodeByHash(hash, data, nil)

		actual, err := source.NodeByHash(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, data, actual)
	})

	t.Run("NodeByHash Error", func(t *testing.T) {
		source, mock := createL2Source(t)
		defer mock.AssertExpectations(t)
		expectedErr := errors.New("boom")
		mock.ExpectNodeByHash(hash, nil, expectedErr)
		mock.ExpectNodeByHash(hash, data, nil)

		actual, err := source.NodeByHash(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, data, actual)
	})

	t.Run("CodeByHash Success", func(t *testing.T) {
		source, mock := createL2Source(t)
		defer mock.AssertExpectations(t)
		mock.ExpectCodeByHash(hash, data, nil)

		actual, err := source.CodeByHash(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, data, actual)
	})

	t.Run("CodeByHash Error", func(t *testing.T) {
		source, mock := createL2Source(t)
		defer mock.AssertExpectations(t)
		expectedErr := errors.New("boom")
		mock.ExpectCodeByHash(hash, nil, expectedErr)
		mock.ExpectCodeByHash(hash, data, nil)

		actual, err := source.CodeByHash(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, data, actual)
	})
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

	t.Run("OutputByRoot Success", func(t *testing.T) {
		source, mock := createL2Source(t)
		defer mock.AssertExpectations(t)
		mock.ExpectOutputByRoot(hash, output, nil)

		actualOutput, err := source.OutputByRoot(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, output, actualOutput)
	})

	t.Run("OutputByRoot Error", func(t *testing.T) {
		source, mock := createL2Source(t)
		defer mock.AssertExpectations(t)
		expectedErr := errors.New("boom")
		mock.ExpectOutputByRoot(hash, wrongOutput, expectedErr)
		mock.ExpectOutputByRoot(hash, output, nil)

		actualOutput, err := source.OutputByRoot(ctx, hash)
		require.NoError(t, err)
		require.Equal(t, output, actualOutput)
	})
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
}

func createL2Source(t *testing.T) (*RetryingL2Source, *MockL2Source) {
	logger := testlog.Logger(t, log.LvlDebug)
	mock := &MockL2Source{}
	source := NewRetryingL2Source(logger, mock)
	// Avoid sleeping in tests by using a fixed backoff strategy with no delay
	source.strategy = backoff.Fixed(0)
	return source, mock
}

type MockL2Source struct {
	mock.Mock
}

func (m *MockL2Source) InfoAndTxsByHash(ctx context.Context, blockHash common.Hash) (eth.BlockInfo, types.Transactions, error) {
	out := m.Mock.MethodCalled("InfoAndTxsByHash", blockHash)
	return out[0].(eth.BlockInfo), out[1].(types.Transactions), *out[2].(*error)
}

func (m *MockL2Source) NodeByHash(ctx context.Context, hash common.Hash) ([]byte, error) {
	out := m.Mock.MethodCalled("NodeByHash", hash)
	return out[0].([]byte), *out[1].(*error)
}

func (m *MockL2Source) CodeByHash(ctx context.Context, hash common.Hash) ([]byte, error) {
	out := m.Mock.MethodCalled("CodeByHash", hash)
	return out[0].([]byte), *out[1].(*error)
}

244 245 246 247 248
func (m *MockL2Source) OutputByRoot(ctx context.Context, root common.Hash) (eth.Output, error) {
	out := m.Mock.MethodCalled("OutputByRoot", root)
	return out[0].(eth.Output), *out[1].(*error)
}

249 250 251 252 253 254 255 256 257 258 259 260
func (m *MockL2Source) ExpectInfoAndTxsByHash(blockHash common.Hash, info eth.BlockInfo, txs types.Transactions, err error) {
	m.Mock.On("InfoAndTxsByHash", blockHash).Once().Return(info, txs, &err)
}

func (m *MockL2Source) ExpectNodeByHash(hash common.Hash, node []byte, err error) {
	m.Mock.On("NodeByHash", hash).Once().Return(node, &err)
}

func (m *MockL2Source) ExpectCodeByHash(hash common.Hash, code []byte, err error) {
	m.Mock.On("CodeByHash", hash).Once().Return(code, &err)
}

261 262 263 264
func (m *MockL2Source) ExpectOutputByRoot(root common.Hash, output eth.Output, err error) {
	m.Mock.On("OutputByRoot", root).Once().Return(output, &err)
}

265
var _ L2Source = (*MockL2Source)(nil)