api.go 4.91 KB
Newer Older
1 2 3 4 5 6
package node

import (
	"context"
	"fmt"

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

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

type l2EthClient interface {
19
	InfoByHash(ctx context.Context, hash common.Hash) (eth.BlockInfo, error)
20
	// GetProof returns a proof of the account, it may return a nil result without error if the address was not found.
21 22
	// 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)
23
	OutputV0AtBlock(ctx context.Context, blockHash common.Hash) (*eth.OutputV0, error)
24 25
}

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

type adminAPI struct {
37 38
	*rpc.CommonAdminAPI
	dr driverClient
39 40
}

41
func NewAdminAPI(dr driverClient, m metrics.RPCMetricer, log log.Logger) *adminAPI {
42
	return &adminAPI{
43
		CommonAdminAPI: rpc.NewCommonAdminAPI(m, log),
44
		dr:             dr,
45 46 47 48
	}
}

func (n *adminAPI) ResetDerivationPipeline(ctx context.Context) error {
49
	recordDur := n.M.RecordRPCServerRequest("admin_resetDerivationPipeline")
50 51
	defer recordDur()
	return n.dr.ResetDerivationPipeline(ctx)
52 53
}

54
func (n *adminAPI) StartSequencer(ctx context.Context, blockHash common.Hash) error {
55
	recordDur := n.M.RecordRPCServerRequest("admin_startSequencer")
56
	defer recordDur()
57
	return n.dr.StartSequencer(ctx, blockHash)
58 59
}

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

66
func (n *adminAPI) SequencerActive(ctx context.Context) (bool, error) {
67
	recordDur := n.M.RecordRPCServerRequest("admin_sequencerActive")
68 69 70 71
	defer recordDur()
	return n.dr.SequencerActive(ctx)
}

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

	if actual, ok := payload.CheckBlockHash(); !ok {
		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())
	}

	return n.dr.OnUnsafeL2Payload(ctx, payload)
}

86 87 88
type nodeAPI struct {
	config *rollup.Config
	client l2EthClient
89
	dr     driverClient
90
	log    log.Logger
91
	m      metrics.RPCMetricer
92 93
}

94
func NewNodeAPI(config *rollup.Config, l2Client l2EthClient, dr driverClient, log log.Logger, m metrics.RPCMetricer) *nodeAPI {
95 96 97
	return &nodeAPI{
		config: config,
		client: l2Client,
98
		dr:     dr,
99
		log:    log,
100
		m:      m,
101 102 103
	}
}

104
func (n *nodeAPI) OutputAtBlock(ctx context.Context, number hexutil.Uint64) (*eth.OutputResponse, error) {
105 106
	recordDur := n.m.RecordRPCServerRequest("optimism_outputAtBlock")
	defer recordDur()
107

108
	ref, status, err := n.dr.BlockRefWithStatus(ctx, uint64(number))
109
	if err != nil {
110 111 112
		return nil, fmt.Errorf("failed to get L2 block ref with sync status: %w", err)
	}

113
	output, err := n.client.OutputV0AtBlock(ctx, ref.Hash)
114
	if err != nil {
115
		return nil, fmt.Errorf("failed to get L2 output at block %s: %w", ref, err)
116
	}
117
	return &eth.OutputResponse{
118 119
		Version:               output.Version(),
		OutputRoot:            eth.OutputRoot(output),
120
		BlockRef:              ref,
121 122
		WithdrawalStorageRoot: common.Hash(output.MessagePasserStorageRoot),
		StateRoot:             common.Hash(output.StateRoot),
123 124
		Status:                status,
	}, nil
125 126
}

127
func (n *nodeAPI) SyncStatus(ctx context.Context) (*eth.SyncStatus, error) {
128 129
	recordDur := n.m.RecordRPCServerRequest("optimism_syncStatus")
	defer recordDur()
130 131 132
	return n.dr.SyncStatus(ctx)
}

133 134 135 136 137 138
func (n *nodeAPI) RollupConfig(_ context.Context) (*rollup.Config, error) {
	recordDur := n.m.RecordRPCServerRequest("optimism_rollupConfig")
	defer recordDur()
	return n.config, nil
}

139
func (n *nodeAPI) Version(ctx context.Context) (string, error) {
140 141
	recordDur := n.m.RecordRPCServerRequest("optimism_version")
	defer recordDur()
142 143
	return version.Version + "-" + version.Meta, nil
}