update.go 4.44 KB
Newer Older
1 2 3 4 5 6 7 8
package db

import (
	"fmt"

	"github.com/ethereum/go-ethereum/common"

	"github.com/ethereum-optimism/optimism/op-service/eth"
9
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/backend/superevents"
10 11 12 13
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/types"
)

func (db *ChainsDB) AddLog(
14
	chain eth.ChainID,
15 16 17 18
	logHash common.Hash,
	parentBlock eth.BlockID,
	logIdx uint32,
	execMsg *types.ExecutingMessage) error {
19
	logDB, ok := db.logDBs.Get(chain)
20
	if !ok {
21
		return fmt.Errorf("cannot AddLog: %w: %v", types.ErrUnknownChain, chain)
22 23 24 25
	}
	return logDB.AddLog(logHash, parentBlock, logIdx, execMsg)
}

26
func (db *ChainsDB) SealBlock(chain eth.ChainID, block eth.BlockRef) error {
27
	logDB, ok := db.logDBs.Get(chain)
28
	if !ok {
29
		return fmt.Errorf("cannot SealBlock: %w: %v", types.ErrUnknownChain, chain)
30 31 32 33 34
	}
	err := logDB.SealBlock(block.ParentHash, block.ID(), block.Time)
	if err != nil {
		return fmt.Errorf("failed to seal block %v: %w", block, err)
	}
35
	db.logger.Info("Updated local unsafe", "chain", chain, "block", block)
36 37 38 39
	db.emitter.Emit(superevents.LocalUnsafeUpdateEvent{
		ChainID:        chain,
		NewLocalUnsafe: block,
	})
40 41 42
	return nil
}

43
func (db *ChainsDB) Rewind(chain eth.ChainID, headBlockNum uint64) error {
44
	logDB, ok := db.logDBs.Get(chain)
45
	if !ok {
46
		return fmt.Errorf("cannot Rewind: %w: %s", types.ErrUnknownChain, chain)
47 48 49 50
	}
	return logDB.Rewind(headBlockNum)
}

51
func (db *ChainsDB) UpdateLocalSafe(chain eth.ChainID, derivedFrom eth.BlockRef, lastDerived eth.BlockRef) {
52
	logger := db.logger.New("chain", chain, "derivedFrom", derivedFrom, "lastDerived", lastDerived)
53
	localDB, ok := db.localDBs.Get(chain)
54
	if !ok {
55 56
		logger.Error("Cannot update local-safe DB, unknown chain")
		return
57
	}
58
	logger.Debug("Updating local safe DB")
59
	if err := localDB.AddDerived(derivedFrom, lastDerived); err != nil {
60 61 62 63 64 65 66
		db.logger.Warn("Failed to update local safe")
		db.emitter.Emit(superevents.LocalSafeOutOfSyncEvent{
			ChainID: chain,
			L1Ref:   derivedFrom,
			Err:     err,
		})
		return
67
	}
68 69 70 71
	db.logger.Info("Updated local safe DB")
	db.emitter.Emit(superevents.LocalSafeUpdateEvent{
		ChainID: chain,
		NewLocalSafe: types.DerivedBlockSealPair{
72 73
			DerivedFrom: types.BlockSealFromRef(derivedFrom),
			Derived:     types.BlockSealFromRef(lastDerived),
74 75
		},
	})
76 77
}

78
func (db *ChainsDB) UpdateCrossUnsafe(chain eth.ChainID, crossUnsafe types.BlockSeal) error {
79 80
	v, ok := db.crossUnsafe.Get(chain)
	if !ok {
81
		return fmt.Errorf("cannot UpdateCrossUnsafe: %w: %s", types.ErrUnknownChain, chain)
82
	}
83
	v.Set(crossUnsafe)
84
	db.logger.Info("Updated cross-unsafe", "chain", chain, "crossUnsafe", crossUnsafe)
85 86 87 88
	db.emitter.Emit(superevents.CrossUnsafeUpdateEvent{
		ChainID:        chain,
		NewCrossUnsafe: crossUnsafe,
	})
89 90 91
	return nil
}

92
func (db *ChainsDB) UpdateCrossSafe(chain eth.ChainID, l1View eth.BlockRef, lastCrossDerived eth.BlockRef) error {
93
	crossDB, ok := db.crossDBs.Get(chain)
94
	if !ok {
95
		return fmt.Errorf("cannot UpdateCrossSafe: %w: %s", types.ErrUnknownChain, chain)
96
	}
97 98 99 100
	if err := crossDB.AddDerived(l1View, lastCrossDerived); err != nil {
		return err
	}
	db.logger.Info("Updated cross-safe", "chain", chain, "l1View", l1View, "lastCrossDerived", lastCrossDerived)
101 102 103
	db.emitter.Emit(superevents.CrossSafeUpdateEvent{
		ChainID: chain,
		NewCrossSafe: types.DerivedBlockSealPair{
104 105
			DerivedFrom: types.BlockSealFromRef(l1View),
			Derived:     types.BlockSealFromRef(lastCrossDerived),
106 107
		},
	})
108
	return nil
109 110
}

111
func (db *ChainsDB) onFinalizedL1(finalized eth.BlockRef) {
112
	// Lock, so we avoid race-conditions in-between getting (for comparison) and setting.
113
	// Unlock is managed explicitly, in this function so we can call NotifyL2Finalized after releasing the lock.
114
	db.finalizedL1.Lock()
115

116
	if v := db.finalizedL1.Value; v != (eth.BlockRef{}) && v.Number > finalized.Number {
117
		db.finalizedL1.Unlock()
118 119
		db.logger.Warn("Cannot rewind finalized L1 block", "current", v, "signal", finalized)
		return
120
	}
121
	db.finalizedL1.Value = finalized
122
	db.logger.Info("Updated finalized L1", "finalizedL1", finalized)
123 124
	db.finalizedL1.Unlock()

125 126 127 128
	db.emitter.Emit(superevents.FinalizedL1UpdateEvent{
		FinalizedL1: finalized,
	})
	// whenever the L1 Finalized changes, the L2 Finalized may change, notify subscribers
129
	for _, chain := range db.depSet.Chains() {
130 131 132 133 134 135
		fin, err := db.Finalized(chain)
		if err != nil {
			db.logger.Warn("Unable to determine finalized L2 block", "chain", chain, "l1Finalized", finalized)
			continue
		}
		db.emitter.Emit(superevents.FinalizedL2UpdateEvent{ChainID: chain, FinalizedL2: fin})
136
	}
137
}