init_test.go 2.34 KB
Newer Older
1
package db
2 3 4 5 6 7

import (
	"fmt"
	"io"
	"testing"

8
	"github.com/ethereum-optimism/optimism/op-service/eth"
9 10
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/backend/db/entrydb"
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/backend/db/logs"
11
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/backend/types"
12 13 14 15 16
	"github.com/stretchr/testify/require"
)

func TestRecover(t *testing.T) {
	tests := []struct {
17 18 19
		name            string
		stubDB          *stubLogStore
		expectRewoundTo uint64
20 21
	}{
		{
22 23 24
			name:            "emptydb",
			stubDB:          &stubLogStore{closestBlockErr: fmt.Errorf("no entries: %w", io.EOF)},
			expectRewoundTo: 0,
25 26
		},
		{
27 28 29
			name:            "genesis",
			stubDB:          &stubLogStore{},
			expectRewoundTo: 0,
30 31
		},
		{
32 33 34
			name:            "with_blocks",
			stubDB:          &stubLogStore{closestBlockNumber: 15},
			expectRewoundTo: 14,
35 36 37 38 39
		},
	}
	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
40
			err := Resume(test.stubDB)
41 42 43 44 45 46 47 48 49 50 51 52
			require.NoError(t, err)
			require.Equal(t, test.expectRewoundTo, test.stubDB.rewoundTo)
		})
	}
}

type stubLogStore struct {
	closestBlockNumber uint64
	closestBlockErr    error
	rewoundTo          uint64
}

53 54 55 56
func (s *stubLogStore) Contains(blockNum uint64, logIdx uint32, loghash types.TruncatedHash) (bool, entrydb.EntryIdx, error) {
	panic("not supported")
}

57 58 59 60
func (s *stubLogStore) ClosestBlockIterator(blockNum uint64) (logs.Iterator, error) {
	panic("not supported")
}

61 62 63 64
func (s *stubLogStore) LastCheckpointBehind(entrydb.EntryIdx) (logs.Iterator, error) {
	panic("not supported")
}

65
func (s *stubLogStore) ClosestBlockInfo(blockNum uint64) (uint64, types.TruncatedHash, error) {
66
	if s.closestBlockErr != nil {
67
		return 0, types.TruncatedHash{}, s.closestBlockErr
68
	}
69
	return s.closestBlockNumber, types.TruncatedHash{}, nil
70 71
}

72 73 74 75
func (s *stubLogStore) NextExecutingMessage(logs.Iterator) (types.ExecutingMessage, error) {
	panic("not supported")
}

76 77 78 79
func (s *stubLogStore) Rewind(headBlockNum uint64) error {
	s.rewoundTo = headBlockNum
	return nil
}
80 81 82 83 84 85 86 87 88 89 90 91

func (s *stubLogStore) AddLog(logHash types.TruncatedHash, block eth.BlockID, timestamp uint64, logIdx uint32, execMsg *types.ExecutingMessage) error {
	panic("not supported")
}

func (s *stubLogStore) LatestBlockNum() uint64 {
	panic("not supported")
}

func (s *stubLogStore) Close() error {
	return nil
}