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

import (
	"context"
	"fmt"
	"math/big"

8
	"github.com/ethereum-optimism/optimism/op-bindings/predeploys"
9 10
	"github.com/ethereum-optimism/optimism/op-node/eth"
	"github.com/ethereum-optimism/optimism/op-node/l2"
11
	"github.com/ethereum-optimism/optimism/op-node/metrics"
12
	"github.com/ethereum-optimism/optimism/op-node/rollup"
13 14
	"github.com/ethereum-optimism/optimism/op-node/rollup/driver"
	"github.com/ethereum-optimism/optimism/op-node/version"
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	"github.com/ethereum/go-ethereum"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/common/hexutil"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/log"
	"github.com/ethereum/go-ethereum/rpc"
)

type l2EthClient interface {
	GetBlockHeader(ctx context.Context, blockTag string) (*types.Header, error)
	// GetProof returns a proof of the account, it may return a nil result without error if the address was not found.
	GetProof(ctx context.Context, address common.Address, blockTag string) (*l2.AccountResult, error)

	BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
	L2BlockRefByNumber(ctx context.Context, l2Num *big.Int) (eth.L2BlockRef, error)
	L2BlockRefByHash(ctx context.Context, l2Hash common.Hash) (eth.L2BlockRef, error)
}

33 34
type driverClient interface {
	SyncStatus(ctx context.Context) (*driver.SyncStatus, error)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	ResetDerivationPipeline(context.Context) error
}

type adminAPI struct {
	dr driverClient
	m  *metrics.Metrics
}

func newAdminAPI(dr driverClient, m *metrics.Metrics) *adminAPI {
	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 57 58
type nodeAPI struct {
	config *rollup.Config
	client l2EthClient
59
	dr     driverClient
60
	log    log.Logger
61
	m      *metrics.Metrics
62 63
}

64
func newNodeAPI(config *rollup.Config, l2Client l2EthClient, dr driverClient, log log.Logger, m *metrics.Metrics) *nodeAPI {
65 66 67
	return &nodeAPI{
		config: config,
		client: l2Client,
68
		dr:     dr,
69
		log:    log,
70
		m:      m,
71 72 73
	}
}

74
func (n *nodeAPI) OutputAtBlock(ctx context.Context, number rpc.BlockNumber) ([]eth.Bytes32, error) {
75 76
	recordDur := n.m.RecordRPCServerRequest("optimism_outputAtBlock")
	defer recordDur()
77 78 79 80 81 82 83 84 85 86 87
	// TODO: rpc.BlockNumber doesn't support the "safe" tag. Need a new type

	head, err := n.client.GetBlockHeader(ctx, toBlockNumArg(number))
	if err != nil {
		n.log.Error("failed to get block", "err", err)
		return nil, err
	}
	if head == nil {
		return nil, ethereum.NotFound
	}

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

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

105
	return []eth.Bytes32{l2OutputRootVersion, l2OutputRoot}, nil
106 107
}

108
func (n *nodeAPI) SyncStatus(ctx context.Context) (*driver.SyncStatus, error) {
109 110
	recordDur := n.m.RecordRPCServerRequest("optimism_syncStatus")
	defer recordDur()
111 112 113
	return n.dr.SyncStatus(ctx)
}

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

120
func (n *nodeAPI) Version(ctx context.Context) (string, error) {
121 122
	recordDur := n.m.RecordRPCServerRequest("optimism_version")
	defer recordDur()
123 124 125 126 127 128 129 130 131 132 133 134
	return version.Version + "-" + version.Meta, nil
}

func toBlockNumArg(number rpc.BlockNumber) string {
	if number == rpc.LatestBlockNumber {
		return "latest"
	}
	if number == rpc.PendingBlockNumber {
		return "pending"
	}
	return hexutil.EncodeUint64(uint64(number.Int64()))
}