api.go 4.2 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 29 30
	ResetDerivationPipeline(context.Context) error
}

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

36 37
type adminAPI struct {
	dr driverClient
38
	m  rpcMetrics
39 40
}

41
func NewAdminAPI(dr driverClient, m rpcMetrics) *adminAPI {
42 43 44 45 46 47 48 49 50 51
	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)
52 53
}

54 55 56
type nodeAPI struct {
	config *rollup.Config
	client l2EthClient
57
	dr     driverClient
58
	log    log.Logger
59
	m      rpcMetrics
60 61
}

62
func NewNodeAPI(config *rollup.Config, l2Client l2EthClient, dr driverClient, log log.Logger, m rpcMetrics) *nodeAPI {
63 64 65
	return &nodeAPI{
		config: config,
		client: l2Client,
66
		dr:     dr,
67
		log:    log,
68
		m:      m,
69 70 71
	}
}

72
func (n *nodeAPI) OutputAtBlock(ctx context.Context, number hexutil.Uint64) (*eth.OutputResponse, error) {
73 74
	recordDur := n.m.RecordRPCServerRequest("optimism_outputAtBlock")
	defer recordDur()
75

76
	ref, status, err := n.dr.BlockRefWithStatus(ctx, uint64(number))
77
	if err != nil {
78 79 80 81 82 83
		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)
84 85 86 87 88
	}
	if head == nil {
		return nil, ethereum.NotFound
	}

89
	proof, err := n.client.GetProof(ctx, predeploys.L2ToL1MessagePasserAddr, []common.Hash{}, ref.Hash.String())
90
	if err != nil {
91
		return nil, fmt.Errorf("failed to get contract proof at block %s: %w", ref, err)
92 93
	}
	if proof == nil {
94
		return nil, fmt.Errorf("proof %w", ethereum.NotFound)
95 96
	}
	// make sure that the proof (including storage hash) that we retrieved is correct by verifying it against the state-root
97 98
	if err := proof.Verify(head.Root()); err != nil {
		n.log.Error("invalid withdrawal root detected in block", "stateRoot", head.Root(), "blocknum", number, "msg", err)
99
		return nil, fmt.Errorf("invalid withdrawal root hash, state root was %s: %w", head.Root(), err)
100 101
	}

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

105 106 107 108 109 110 111 112
	return &eth.OutputResponse{
		Version:               l2OutputRootVersion,
		OutputRoot:            l2OutputRoot,
		BlockRef:              ref,
		WithdrawalStorageRoot: proof.StorageHash,
		StateRoot:             head.Root(),
		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
}