api.go 4.04 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
	GetProof(ctx context.Context, address common.Address, blockTag string) (*eth.AccountResult, 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 28 29
	ResetDerivationPipeline(context.Context) error
}

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

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

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

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

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

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

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

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

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

104 105 106 107 108 109 110 111
	return &eth.OutputResponse{
		Version:               l2OutputRootVersion,
		OutputRoot:            l2OutputRoot,
		BlockRef:              ref,
		WithdrawalStorageRoot: proof.StorageHash,
		StateRoot:             head.Root(),
		Status:                status,
	}, nil
112 113
}

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

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

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