api.go 6.09 KB
Newer Older
1 2 3 4
package node

import (
	"context"
5
	"errors"
6 7
	"fmt"

8 9 10 11
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/common/hexutil"
	"github.com/ethereum/go-ethereum/log"

12
	"github.com/ethereum-optimism/optimism/op-node/node/safedb"
13
	"github.com/ethereum-optimism/optimism/op-node/rollup"
14
	"github.com/ethereum-optimism/optimism/op-node/version"
15
	"github.com/ethereum-optimism/optimism/op-service/eth"
16
	"github.com/ethereum-optimism/optimism/op-service/metrics"
17
	"github.com/ethereum-optimism/optimism/op-service/rpc"
18 19 20
)

type l2EthClient interface {
21
	InfoByHash(ctx context.Context, hash common.Hash) (eth.BlockInfo, error)
22
	// GetProof returns a proof of the account, it may return a nil result without error if the address was not found.
23 24
	// Optionally keys of the account storage trie can be specified to include with corresponding values in the proof.
	GetProof(ctx context.Context, address common.Address, storage []common.Hash, blockTag string) (*eth.AccountResult, error)
25
	OutputV0AtBlock(ctx context.Context, blockHash common.Hash) (*eth.OutputV0, error)
26 27
}

28
type driverClient interface {
29
	SyncStatus(ctx context.Context) (*eth.SyncStatus, error)
30
	BlockRefWithStatus(ctx context.Context, num uint64) (eth.L2BlockRef, *eth.SyncStatus, error)
31
	ResetDerivationPipeline(context.Context) error
32 33
	StartSequencer(ctx context.Context, blockHash common.Hash) error
	StopSequencer(context.Context) (common.Hash, error)
34
	SequencerActive(context.Context) (bool, error)
35
	OnUnsafeL2Payload(ctx context.Context, payload *eth.ExecutionPayloadEnvelope) error
36
	OverrideLeader(ctx context.Context) error
37 38
}

39 40 41 42
type SafeDBReader interface {
	SafeHeadAtL1(ctx context.Context, l1BlockNum uint64) (l1 eth.BlockID, l2 eth.BlockID, err error)
}

43
type adminAPI struct {
44 45
	*rpc.CommonAdminAPI
	dr driverClient
46 47
}

48
func NewAdminAPI(dr driverClient, m metrics.RPCMetricer, log log.Logger) *adminAPI {
49
	return &adminAPI{
50
		CommonAdminAPI: rpc.NewCommonAdminAPI(m, log),
51
		dr:             dr,
52 53 54 55
	}
}

func (n *adminAPI) ResetDerivationPipeline(ctx context.Context) error {
56
	recordDur := n.M.RecordRPCServerRequest("admin_resetDerivationPipeline")
57 58
	defer recordDur()
	return n.dr.ResetDerivationPipeline(ctx)
59 60
}

61
func (n *adminAPI) StartSequencer(ctx context.Context, blockHash common.Hash) error {
62
	recordDur := n.M.RecordRPCServerRequest("admin_startSequencer")
63
	defer recordDur()
64
	return n.dr.StartSequencer(ctx, blockHash)
65 66
}

67
func (n *adminAPI) StopSequencer(ctx context.Context) (common.Hash, error) {
68
	recordDur := n.M.RecordRPCServerRequest("admin_stopSequencer")
69 70 71 72
	defer recordDur()
	return n.dr.StopSequencer(ctx)
}

73
func (n *adminAPI) SequencerActive(ctx context.Context) (bool, error) {
74
	recordDur := n.M.RecordRPCServerRequest("admin_sequencerActive")
75 76 77 78
	defer recordDur()
	return n.dr.SequencerActive(ctx)
}

79
// PostUnsafePayload is a special API that allows posting an unsafe payload to the L2 derivation pipeline.
80
// It should only be used by op-conductor for sequencer failover scenarios.
81
func (n *adminAPI) PostUnsafePayload(ctx context.Context, envelope *eth.ExecutionPayloadEnvelope) error {
82 83 84
	recordDur := n.M.RecordRPCServerRequest("admin_postUnsafePayload")
	defer recordDur()

85 86
	payload := envelope.ExecutionPayload
	if actual, ok := envelope.CheckBlockHash(); !ok {
87 88 89 90
		log.Error("payload has bad block hash", "bad_hash", payload.BlockHash.String(), "actual", actual.String())
		return fmt.Errorf("payload has bad block hash: %s, actual block hash is: %s", payload.BlockHash.String(), actual.String())
	}

91
	return n.dr.OnUnsafeL2Payload(ctx, envelope)
92 93
}

94 95 96 97 98 99 100
// OverrideLeader disables sequencer conductor interactions and allow sequencer to run in non-HA mode during disaster recovery scenarios.
func (n *adminAPI) OverrideLeader(ctx context.Context) error {
	recordDur := n.M.RecordRPCServerRequest("admin_overrideLeader")
	defer recordDur()
	return n.dr.OverrideLeader(ctx)
}

101 102 103
type nodeAPI struct {
	config *rollup.Config
	client l2EthClient
104
	dr     driverClient
105
	safeDB SafeDBReader
106
	log    log.Logger
107
	m      metrics.RPCMetricer
108 109
}

110
func NewNodeAPI(config *rollup.Config, l2Client l2EthClient, dr driverClient, safeDB SafeDBReader, log log.Logger, m metrics.RPCMetricer) *nodeAPI {
111 112 113
	return &nodeAPI{
		config: config,
		client: l2Client,
114
		dr:     dr,
115
		safeDB: safeDB,
116
		log:    log,
117
		m:      m,
118 119 120
	}
}

121
func (n *nodeAPI) OutputAtBlock(ctx context.Context, number hexutil.Uint64) (*eth.OutputResponse, error) {
122 123
	recordDur := n.m.RecordRPCServerRequest("optimism_outputAtBlock")
	defer recordDur()
124

125
	ref, status, err := n.dr.BlockRefWithStatus(ctx, uint64(number))
126
	if err != nil {
127 128 129
		return nil, fmt.Errorf("failed to get L2 block ref with sync status: %w", err)
	}

130
	output, err := n.client.OutputV0AtBlock(ctx, ref.Hash)
131
	if err != nil {
132
		return nil, fmt.Errorf("failed to get L2 output at block %s: %w", ref, err)
133
	}
134
	return &eth.OutputResponse{
135 136
		Version:               output.Version(),
		OutputRoot:            eth.OutputRoot(output),
137
		BlockRef:              ref,
138 139
		WithdrawalStorageRoot: common.Hash(output.MessagePasserStorageRoot),
		StateRoot:             common.Hash(output.StateRoot),
140 141
		Status:                status,
	}, nil
142 143
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
func (n *nodeAPI) SafeHeadAtL1Block(ctx context.Context, number hexutil.Uint64) (*eth.SafeHeadResponse, error) {
	recordDur := n.m.RecordRPCServerRequest("optimism_safeHeadAtL1Block")
	defer recordDur()
	l1Block, safeHead, err := n.safeDB.SafeHeadAtL1(ctx, uint64(number))
	if errors.Is(err, safedb.ErrNotFound) {
		return nil, err
	} else if err != nil {
		return nil, fmt.Errorf("failed to get safe head at l1 block %s: %w", number, err)
	}
	return &eth.SafeHeadResponse{
		L1Block:  l1Block,
		SafeHead: safeHead,
	}, nil
}

159
func (n *nodeAPI) SyncStatus(ctx context.Context) (*eth.SyncStatus, error) {
160 161
	recordDur := n.m.RecordRPCServerRequest("optimism_syncStatus")
	defer recordDur()
162 163 164
	return n.dr.SyncStatus(ctx)
}

165 166 167 168 169 170
func (n *nodeAPI) RollupConfig(_ context.Context) (*rollup.Config, error) {
	recordDur := n.m.RecordRPCServerRequest("optimism_rollupConfig")
	defer recordDur()
	return n.config, nil
}

171
func (n *nodeAPI) Version(ctx context.Context) (string, error) {
172 173
	recordDur := n.m.RecordRPCServerRequest("optimism_version")
	defer recordDur()
174 175
	return version.Version + "-" + version.Meta, nil
}