frontend.go 3.67 KB
Newer Older
1 2 3 4 5
package frontend

import (
	"context"

6
	"github.com/ethereum-optimism/optimism/op-service/eth"
7
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/types"
8
	"github.com/ethereum/go-ethereum/common"
9
	"github.com/ethereum/go-ethereum/common/hexutil"
10 11 12 13 14
)

type AdminBackend interface {
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
15
	AddL2RPC(ctx context.Context, rpc string, jwtSecret eth.Bytes32) error
16 17 18 19
}

type QueryBackend interface {
	CheckMessage(identifier types.Identifier, payloadHash common.Hash) (types.SafetyLevel, error)
20
	CheckMessages(messages []types.Message, minSafety types.SafetyLevel) error
21 22 23 24
	CrossDerivedFrom(ctx context.Context, chainID eth.ChainID, derived eth.BlockID) (derivedFrom eth.BlockRef, err error)
	LocalUnsafe(ctx context.Context, chainID eth.ChainID) (eth.BlockID, error)
	CrossSafe(ctx context.Context, chainID eth.ChainID) (types.DerivedIDPair, error)
	Finalized(ctx context.Context, chainID eth.ChainID) (eth.BlockID, error)
25
	FinalizedL1() eth.BlockRef
26
	SuperRootAtTimestamp(ctx context.Context, timestamp hexutil.Uint64) (eth.SuperRootResponse, error)
27
	AllSafeDerivedAt(ctx context.Context, derivedFrom eth.BlockID) (derived map[eth.ChainID]eth.BlockID, err error)
28 29
}

30 31 32 33 34 35 36 37 38
type Backend interface {
	AdminBackend
	QueryBackend
}

type QueryFrontend struct {
	Supervisor QueryBackend
}

39 40
var _ QueryBackend = (*QueryFrontend)(nil)

41 42 43 44 45 46
// CheckMessage checks the safety-level of an individual message.
// The payloadHash references the hash of the message-payload of the message.
func (q *QueryFrontend) CheckMessage(identifier types.Identifier, payloadHash common.Hash) (types.SafetyLevel, error) {
	return q.Supervisor.CheckMessage(identifier, payloadHash)
}

47
// CheckMessages checks the safety-level of a collection of messages,
48 49 50 51 52 53 54
// and returns if the minimum safety-level is met for all messages.
func (q *QueryFrontend) CheckMessages(
	messages []types.Message,
	minSafety types.SafetyLevel) error {
	return q.Supervisor.CheckMessages(messages, minSafety)
}

55
func (q *QueryFrontend) LocalUnsafe(ctx context.Context, chainID eth.ChainID) (eth.BlockID, error) {
56
	return q.Supervisor.LocalUnsafe(ctx, chainID)
57 58
}

59
func (q *QueryFrontend) CrossSafe(ctx context.Context, chainID eth.ChainID) (types.DerivedIDPair, error) {
60
	return q.Supervisor.CrossSafe(ctx, chainID)
61 62
}

63
func (q *QueryFrontend) Finalized(ctx context.Context, chainID eth.ChainID) (eth.BlockID, error) {
64
	return q.Supervisor.Finalized(ctx, chainID)
65 66
}

67 68 69 70
func (q *QueryFrontend) FinalizedL1() eth.BlockRef {
	return q.Supervisor.FinalizedL1()
}

71
func (q *QueryFrontend) CrossDerivedFrom(ctx context.Context, chainID eth.ChainID, derived eth.BlockID) (derivedFrom eth.BlockRef, err error) {
72
	return q.Supervisor.CrossDerivedFrom(ctx, chainID, derived)
73 74
}

75
func (q *QueryFrontend) SuperRootAtTimestamp(ctx context.Context, timestamp hexutil.Uint64) (eth.SuperRootResponse, error) {
76 77 78
	return q.Supervisor.SuperRootAtTimestamp(ctx, timestamp)
}

79 80 81 82
func (q *QueryFrontend) AllSafeDerivedAt(ctx context.Context, derivedFrom eth.BlockID) (derived map[eth.ChainID]eth.BlockID, err error) {
	return q.Supervisor.AllSafeDerivedAt(ctx, derivedFrom)
}

83 84 85 86
type AdminFrontend struct {
	Supervisor Backend
}

87 88
var _ AdminBackend = (*AdminFrontend)(nil)

89 90 91 92 93 94 95 96 97
// Start starts the service, if it was previously stopped.
func (a *AdminFrontend) Start(ctx context.Context) error {
	return a.Supervisor.Start(ctx)
}

// Stop stops the service, if it was previously started.
func (a *AdminFrontend) Stop(ctx context.Context) error {
	return a.Supervisor.Stop(ctx)
}
98 99

// AddL2RPC adds a new L2 chain to the supervisor backend
100 101
func (a *AdminFrontend) AddL2RPC(ctx context.Context, rpc string, jwtSecret eth.Bytes32) error {
	return a.Supervisor.AddL2RPC(ctx, rpc, jwtSecret)
102
}