api.go 4.15 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 34 35
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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