api.go 4.08 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/rpc"
15 16 17
)

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

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

type adminAPI struct {
35 36
	*rpc.CommonAdminAPI
	dr driverClient
37 38
}

39
func NewAdminAPI(dr driverClient, m rpc.RpcMetrics, log log.Logger) *adminAPI {
40
	return &adminAPI{
41 42
		dr:             dr,
		CommonAdminAPI: rpc.NewCommonAdminAPI(m, log),
43 44 45 46
	}
}

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

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

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

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

70 71 72
type nodeAPI struct {
	config *rollup.Config
	client l2EthClient
73
	dr     driverClient
74
	log    log.Logger
75
	m      rpc.RpcMetrics
76 77
}

78
func NewNodeAPI(config *rollup.Config, l2Client l2EthClient, dr driverClient, log log.Logger, m rpc.RpcMetrics) *nodeAPI {
79 80 81
	return &nodeAPI{
		config: config,
		client: l2Client,
82
		dr:     dr,
83
		log:    log,
84
		m:      m,
85 86 87
	}
}

88
func (n *nodeAPI) OutputAtBlock(ctx context.Context, number hexutil.Uint64) (*eth.OutputResponse, error) {
89 90
	recordDur := n.m.RecordRPCServerRequest("optimism_outputAtBlock")
	defer recordDur()
91

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

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

111
func (n *nodeAPI) SyncStatus(ctx context.Context) (*eth.SyncStatus, error) {
112 113
	recordDur := n.m.RecordRPCServerRequest("optimism_syncStatus")
	defer recordDur()
114 115 116
	return n.dr.SyncStatus(ctx)
}

117 118 119 120 121 122
func (n *nodeAPI) RollupConfig(_ context.Context) (*rollup.Config, error) {
	recordDur := n.m.RecordRPCServerRequest("optimism_rollupConfig")
	defer recordDur()
	return n.config, nil
}

123
func (n *nodeAPI) Version(ctx context.Context) (string, error) {
124 125
	recordDur := n.m.RecordRPCServerRequest("optimism_version")
	defer recordDur()
126 127
	return version.Version + "-" + version.Meta, nil
}