api.go 4.11 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 15 16
)

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

24
type driverClient interface {
25
	SyncStatus(ctx context.Context) (*eth.SyncStatus, error)
26
	BlockRefWithStatus(ctx context.Context, num uint64) (eth.L2BlockRef, *eth.SyncStatus, error)
27
	ResetDerivationPipeline(context.Context) error
28 29
	StartSequencer(ctx context.Context, blockHash common.Hash) error
	StopSequencer(context.Context) (common.Hash, error)
30
	SequencerActive(context.Context) (bool, error)
31 32
}

33 34 35 36 37
type rpcMetrics interface {
	// RecordRPCServerRequest returns a function that records the duration of serving the given RPC method
	RecordRPCServerRequest(method string) func()
}

38 39
type adminAPI struct {
	dr driverClient
40
	m  rpcMetrics
41 42
}

43
func NewAdminAPI(dr driverClient, m rpcMetrics) *adminAPI {
44 45 46 47 48 49 50 51 52 53
	return &adminAPI{
		dr: dr,
		m:  m,
	}
}

func (n *adminAPI) ResetDerivationPipeline(ctx context.Context) error {
	recordDur := n.m.RecordRPCServerRequest("admin_resetDerivationPipeline")
	defer recordDur()
	return n.dr.ResetDerivationPipeline(ctx)
54 55
}

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

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

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

74 75 76
type nodeAPI struct {
	config *rollup.Config
	client l2EthClient
77
	dr     driverClient
78
	log    log.Logger
79
	m      rpcMetrics
80 81
}

82
func NewNodeAPI(config *rollup.Config, l2Client l2EthClient, dr driverClient, log log.Logger, m rpcMetrics) *nodeAPI {
83 84 85
	return &nodeAPI{
		config: config,
		client: l2Client,
86
		dr:     dr,
87
		log:    log,
88
		m:      m,
89 90 91
	}
}

92
func (n *nodeAPI) OutputAtBlock(ctx context.Context, number hexutil.Uint64) (*eth.OutputResponse, error) {
93 94
	recordDur := n.m.RecordRPCServerRequest("optimism_outputAtBlock")
	defer recordDur()
95

96
	ref, status, err := n.dr.BlockRefWithStatus(ctx, uint64(number))
97
	if err != nil {
98 99 100
		return nil, fmt.Errorf("failed to get L2 block ref with sync status: %w", err)
	}

101
	output, err := n.client.OutputV0AtBlock(ctx, ref.Hash)
102
	if err != nil {
103
		return nil, fmt.Errorf("failed to get L2 output at block %s: %w", ref, err)
104
	}
105
	return &eth.OutputResponse{
106 107
		Version:               output.Version(),
		OutputRoot:            eth.OutputRoot(output),
108
		BlockRef:              ref,
109 110
		WithdrawalStorageRoot: common.Hash(output.MessagePasserStorageRoot),
		StateRoot:             common.Hash(output.StateRoot),
111 112
		Status:                status,
	}, nil
113 114
}

115
func (n *nodeAPI) SyncStatus(ctx context.Context) (*eth.SyncStatus, error) {
116 117
	recordDur := n.m.RecordRPCServerRequest("optimism_syncStatus")
	defer recordDur()
118 119 120
	return n.dr.SyncStatus(ctx)
}

121 122 123 124 125 126
func (n *nodeAPI) RollupConfig(_ context.Context) (*rollup.Config, error) {
	recordDur := n.m.RecordRPCServerRequest("optimism_rollupConfig")
	defer recordDur()
	return n.config, nil
}

127
func (n *nodeAPI) Version(ctx context.Context) (string, error) {
128 129
	recordDur := n.m.RecordRPCServerRequest("optimism_version")
	defer recordDur()
130 131
	return version.Version + "-" + version.Meta, nil
}