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

import (
	"context"
	"fmt"

7 8 9 10 11
	"github.com/ethereum/go-ethereum"
	"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-bindings/predeploys"
13 14
	"github.com/ethereum-optimism/optimism/op-node/eth"
	"github.com/ethereum-optimism/optimism/op-node/rollup"
15
	"github.com/ethereum-optimism/optimism/op-node/version"
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 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 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
type nodeAPI struct {
	config *rollup.Config
	client l2EthClient
71
	dr     driverClient
72
	log    log.Logger
73
	m      rpcMetrics
74 75
}

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

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

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

	head, err := n.client.InfoByHash(ctx, ref.Hash)
	if err != nil {
		return nil, fmt.Errorf("failed to get L2 block by hash %s: %w", ref, err)
98 99 100 101 102
	}
	if head == nil {
		return nil, ethereum.NotFound
	}

103
	proof, err := n.client.GetProof(ctx, predeploys.L2ToL1MessagePasserAddr, []common.Hash{}, ref.Hash.String())
104
	if err != nil {
105
		return nil, fmt.Errorf("failed to get contract proof at block %s: %w", ref, err)
106 107
	}
	if proof == nil {
108
		return nil, fmt.Errorf("proof %w", ethereum.NotFound)
109 110
	}
	// make sure that the proof (including storage hash) that we retrieved is correct by verifying it against the state-root
111 112
	if err := proof.Verify(head.Root()); err != nil {
		n.log.Error("invalid withdrawal root detected in block", "stateRoot", head.Root(), "blocknum", number, "msg", err)
113
		return nil, fmt.Errorf("invalid withdrawal root hash, state root was %s: %w", head.Root(), err)
114 115
	}

116
	var l2OutputRootVersion eth.Bytes32 // it's zero for now
117
	l2OutputRoot := rollup.ComputeL2OutputRoot(l2OutputRootVersion, head.Hash(), head.Root(), proof.StorageHash)
118

119 120 121 122 123 124 125 126
	return &eth.OutputResponse{
		Version:               l2OutputRootVersion,
		OutputRoot:            l2OutputRoot,
		BlockRef:              ref,
		WithdrawalStorageRoot: proof.StorageHash,
		StateRoot:             head.Root(),
		Status:                status,
	}, nil
127 128
}

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

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

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