controller_test.go 10.8 KB
Newer Older
1 2 3 4 5 6 7
package syncnode

import (
	"context"
	"testing"

	"github.com/ethereum-optimism/optimism/op-service/eth"
8
	"github.com/ethereum-optimism/optimism/op-service/testlog"
9 10
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/backend/depset"
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/types"
11 12
	"github.com/ethereum/go-ethereum"
	gethevent "github.com/ethereum/go-ethereum/event"
13 14 15 16 17
	"github.com/ethereum/go-ethereum/log"
	"github.com/stretchr/testify/require"
)

type mockChainsDB struct {
18
	localSafeFn       func(chainID types.ChainID) (types.DerivedBlockSealPair, error)
19
	updateLocalSafeFn func(chainID types.ChainID, ref eth.BlockRef, derived eth.BlockRef) error
20 21 22 23 24 25 26 27 28 29 30 31 32
	updateCrossSafeFn func(chainID types.ChainID, ref eth.BlockRef, derived eth.BlockRef) error
	openBlockFn       func(chainID types.ChainID, i uint64) (seal eth.BlockRef, logCount uint32, execMsgs map[uint32]*types.ExecutingMessage, err error)

	subscribeCrossUnsafe gethevent.FeedOf[types.BlockSeal]
	subscribeCrosSafe    gethevent.FeedOf[types.DerivedBlockSealPair]
	subscribeFinalized   gethevent.FeedOf[types.BlockSeal]
}

func (m *mockChainsDB) OpenBlock(chainID types.ChainID, i uint64) (seal eth.BlockRef, logCount uint32, execMsgs map[uint32]*types.ExecutingMessage, err error) {
	if m.openBlockFn != nil {
		return m.openBlockFn(chainID, i)
	}
	return eth.BlockRef{}, 0, nil, nil
33 34 35 36 37 38 39 40 41
}

func (m *mockChainsDB) UpdateLocalSafe(chainID types.ChainID, ref eth.BlockRef, derived eth.BlockRef) error {
	if m.updateLocalSafeFn != nil {
		return m.updateLocalSafeFn(chainID, ref, derived)
	}
	return nil
}

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
func (m *mockChainsDB) LocalSafe(chainID types.ChainID) (types.DerivedBlockSealPair, error) {
	if m.localSafeFn != nil {
		return m.localSafeFn(chainID)
	}
	return types.DerivedBlockSealPair{}, nil
}

func (m *mockChainsDB) UpdateCrossSafe(chainID types.ChainID, ref eth.BlockRef, derived eth.BlockRef) error {
	if m.updateCrossSafeFn != nil {
		return m.updateCrossSafeFn(chainID, ref, derived)
	}
	return nil
}

func (m *mockChainsDB) SubscribeCrossUnsafe(chainID types.ChainID, c chan<- types.BlockSeal) (gethevent.Subscription, error) {
	return m.subscribeCrossUnsafe.Subscribe(c), nil
}

func (m *mockChainsDB) SubscribeCrossSafe(chainID types.ChainID, c chan<- types.DerivedBlockSealPair) (gethevent.Subscription, error) {
	return m.subscribeCrosSafe.Subscribe(c), nil
}

func (m *mockChainsDB) SubscribeFinalized(chainID types.ChainID, c chan<- types.BlockSeal) (gethevent.Subscription, error) {
	return m.subscribeFinalized.Subscribe(c), nil
}

var _ chainsDB = (*mockChainsDB)(nil)

70
type mockSyncControl struct {
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 122 123 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
	anchorPointFn       func(ctx context.Context) (types.DerivedBlockRefPair, error)
	provideL1Fn         func(ctx context.Context, ref eth.BlockRef) error
	resetFn             func(ctx context.Context, unsafe, safe, finalized eth.BlockID) error
	updateCrossSafeFn   func(ctx context.Context, derived, derivedFrom eth.BlockID) error
	updateCrossUnsafeFn func(ctx context.Context, derived eth.BlockID) error
	updateFinalizedFn   func(ctx context.Context, id eth.BlockID) error
	pullEventFn         func(ctx context.Context) (*types.ManagedEvent, error)

	subscribeEvents gethevent.FeedOf[*types.ManagedEvent]
}

func (m *mockSyncControl) AnchorPoint(ctx context.Context) (types.DerivedBlockRefPair, error) {
	if m.anchorPointFn != nil {
		return m.anchorPointFn(ctx)
	}
	return types.DerivedBlockRefPair{}, nil
}

func (m *mockSyncControl) ProvideL1(ctx context.Context, ref eth.BlockRef) error {
	if m.provideL1Fn != nil {
		return m.provideL1Fn(ctx, ref)
	}
	return nil
}

func (m *mockSyncControl) Reset(ctx context.Context, unsafe, safe, finalized eth.BlockID) error {
	if m.resetFn != nil {
		return m.resetFn(ctx, unsafe, safe, finalized)
	}
	return nil
}

func (m *mockSyncControl) PullEvent(ctx context.Context) (*types.ManagedEvent, error) {
	if m.pullEventFn != nil {
		return m.pullEventFn(ctx)
	}
	return nil, nil
}

func (m *mockSyncControl) SubscribeEvents(ctx context.Context, ch chan *types.ManagedEvent) (ethereum.Subscription, error) {
	return m.subscribeEvents.Subscribe(ch), nil
}

func (m *mockSyncControl) UpdateCrossSafe(ctx context.Context, derived eth.BlockID, derivedFrom eth.BlockID) error {
	if m.updateCrossSafeFn != nil {
		return m.updateCrossSafeFn(ctx, derived, derivedFrom)
	}
	return nil
}

func (m *mockSyncControl) UpdateCrossUnsafe(ctx context.Context, derived eth.BlockID) error {
	if m.updateCrossUnsafeFn != nil {
		return m.updateCrossUnsafeFn(ctx, derived)
	}
	return nil
}

func (m *mockSyncControl) UpdateFinalized(ctx context.Context, id eth.BlockID) error {
	if m.updateFinalizedFn != nil {
		return m.updateFinalizedFn(ctx, id)
	}
	return nil
}

var _ SyncControl = (*mockSyncControl)(nil)

type mockBackend struct {
	updateLocalUnsafeFn func(ctx context.Context, chainID types.ChainID, head eth.BlockRef) error
	updateLocalSafeFn   func(ctx context.Context, chainID types.ChainID, derivedFrom eth.BlockRef, lastDerived eth.BlockRef) error
}

func (m *mockBackend) LocalSafe(ctx context.Context, chainID types.ChainID) (pair types.DerivedIDPair, err error) {
	return types.DerivedIDPair{}, nil
}

func (m *mockBackend) LocalUnsafe(ctx context.Context, chainID types.ChainID) (eth.BlockID, error) {
	return eth.BlockID{}, nil
}

func (m *mockBackend) LatestUnsafe(ctx context.Context, chainID types.ChainID) (eth.BlockID, error) {
	return eth.BlockID{}, nil
}

func (m *mockBackend) SafeDerivedAt(ctx context.Context, chainID types.ChainID, derivedFrom eth.BlockID) (derived eth.BlockID, err error) {
	return eth.BlockID{}, nil
}

func (m *mockBackend) Finalized(ctx context.Context, chainID types.ChainID) (eth.BlockID, error) {
	return eth.BlockID{}, nil
}

func (m *mockBackend) UpdateLocalSafe(ctx context.Context, chainID types.ChainID, derivedFrom eth.BlockRef, lastDerived eth.BlockRef) error {
	if m.updateLocalSafeFn != nil {
		return m.updateLocalSafeFn(ctx, chainID, derivedFrom, lastDerived)
	}
	return nil
167 168
}

169 170 171
func (m *mockBackend) UpdateLocalUnsafe(ctx context.Context, chainID types.ChainID, head eth.BlockRef) error {
	if m.updateLocalUnsafeFn != nil {
		return m.updateLocalUnsafeFn(ctx, chainID, head)
172
	}
173 174 175 176 177
	return nil
}

func (m *mockBackend) L1BlockRefByNumber(ctx context.Context, number uint64) (eth.L1BlockRef, error) {
	return eth.L1BlockRef{}, nil
178
}
179 180 181

var _ backend = (*mockBackend)(nil)

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
func sampleDepSet(t *testing.T) depset.DependencySet {
	depSet, err := depset.NewStaticConfigDependencySet(
		map[types.ChainID]*depset.StaticConfigDependency{
			types.ChainIDFromUInt64(900): {
				ChainIndex:     900,
				ActivationTime: 42,
				HistoryMinTime: 100,
			},
			types.ChainIDFromUInt64(901): {
				ChainIndex:     901,
				ActivationTime: 30,
				HistoryMinTime: 20,
			},
		})
	require.NoError(t, err)
	return depSet
}

200 201 202
// TestInitFromAnchorPoint tests that the SyncNodesController uses the Anchor Point to initialize databases
func TestInitFromAnchorPoint(t *testing.T) {
	logger := testlog.Logger(t, log.LvlInfo)
203
	depSet := sampleDepSet(t)
204
	controller := NewSyncNodesController(logger, depSet, &mockChainsDB{}, &mockBackend{})
205 206 207 208

	require.Zero(t, controller.controllers.Len(), "controllers should be empty to start")

	// Attach a controller for chain 900
209
	// make the controller return an anchor point
210
	ctrl := mockSyncControl{}
211 212 213 214 215 216
	ctrl.anchorPointFn = func(ctx context.Context) (types.DerivedBlockRefPair, error) {
		return types.DerivedBlockRefPair{
			Derived:     eth.BlockRef{Number: 1},
			DerivedFrom: eth.BlockRef{Number: 0},
		}, nil
	}
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	// have the local safe return an error to trigger the initialization
	controller.db.(*mockChainsDB).localSafeFn = func(chainID types.ChainID) (types.DerivedBlockSealPair, error) {
		return types.DerivedBlockSealPair{}, types.ErrFuture
	}
	// record when the updateLocalSafe function is called
	localCalled := 0
	controller.db.(*mockChainsDB).updateLocalSafeFn = func(chainID types.ChainID, ref eth.BlockRef, derived eth.BlockRef) error {
		localCalled++
		return nil
	}
	// record when the updateCrossSafe function is called
	crossCalled := 0
	controller.db.(*mockChainsDB).updateCrossSafeFn = func(chainID types.ChainID, ref eth.BlockRef, derived eth.BlockRef) error {
		crossCalled++
		return nil
	}
234

235 236 237 238 239 240 241
	// have OpenBlock return an error to trigger the initialization
	controller.db.(*mockChainsDB).openBlockFn = func(chainID types.ChainID, i uint64) (seal eth.BlockRef, logCount uint32, execMsgs map[uint32]*types.ExecutingMessage, err error) {
		return eth.BlockRef{}, 0, nil, types.ErrFuture
	}
	unsafeCalled := 0
	controller.backend.(*mockBackend).updateLocalUnsafeFn = func(ctx context.Context, chainID types.ChainID, head eth.BlockRef) error {
		unsafeCalled++
242 243 244
		return nil
	}

245 246
	// after the first attach, both databases are called for update
	_, err := controller.AttachNodeController(types.ChainIDFromUInt64(900), &ctrl, false)
247 248

	require.NoError(t, err)
249 250 251
	require.Equal(t, 1, localCalled, "local safe should have been updated once")
	require.Equal(t, 1, crossCalled, "cross safe should have been updated twice")
	require.Equal(t, 1, unsafeCalled, "local unsafe should have been updated once")
252

253 254 255 256
	// reset the local safe function to return no error
	controller.db.(*mockChainsDB).localSafeFn = nil
	// reset the open block function to return no error
	controller.db.(*mockChainsDB).openBlockFn = nil
257

258 259 260 261 262 263 264
	// after the second attach, there are no additional updates (no empty signal from the DB)
	ctrl2 := mockSyncControl{}
	_, err = controller.AttachNodeController(types.ChainIDFromUInt64(901), &ctrl2, false)
	require.NoError(t, err)
	require.Equal(t, 1, localCalled, "local safe should have been updated once")
	require.Equal(t, 1, crossCalled, "cross safe should have been updated once")
	require.Equal(t, 1, unsafeCalled, "local unsafe should have been updated once")
265 266
}

267 268 269
// TestAttachNodeController tests the AttachNodeController function of the SyncNodesController.
// Only controllers for chains in the dependency set can be attached.
func TestAttachNodeController(t *testing.T) {
270 271
	logger := log.New()
	depSet := sampleDepSet(t)
272
	controller := NewSyncNodesController(logger, depSet, &mockChainsDB{}, &mockBackend{})
273

274 275 276 277 278
	require.Zero(t, controller.controllers.Len(), "controllers should be empty to start")

	// Attach a controller for chain 900
	ctrl := mockSyncControl{}
	_, err := controller.AttachNodeController(types.ChainIDFromUInt64(900), &ctrl, false)
279 280
	require.NoError(t, err)

281 282 283
	require.Equal(t, 1, controller.controllers.Len(), "controllers should have 1 entry")

	// Attach a controller for chain 901
284
	ctrl2 := mockSyncControl{}
285
	_, err = controller.AttachNodeController(types.ChainIDFromUInt64(901), &ctrl2, false)
286 287
	require.NoError(t, err)

288
	require.Equal(t, 2, controller.controllers.Len(), "controllers should have 2 entries")
289

290 291 292 293 294
	// Attach a controller for chain 902 (which is not in the dependency set)
	ctrl3 := mockSyncControl{}
	_, err = controller.AttachNodeController(types.ChainIDFromUInt64(902), &ctrl3, false)
	require.Error(t, err)
	require.Equal(t, 2, controller.controllers.Len(), "controllers should still have 2 entries")
295
}