tmpapi.go 2.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
package interop

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

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	gethrpc "github.com/ethereum/go-ethereum/rpc"

	"github.com/ethereum-optimism/optimism/op-service/eth"
	"github.com/ethereum-optimism/optimism/op-service/rpc"
	supervisortypes "github.com/ethereum-optimism/optimism/op-supervisor/supervisor/types"
)

// TemporaryInteropServer is a work-around to serve the "managed"-
// mode endpoints used by the op-supervisor for data,
// while still using the old interop deriver for syncing.
type TemporaryInteropServer struct {
	srv *rpc.Server
}

func NewTemporaryInteropServer(host string, port int, eng Engine) *TemporaryInteropServer {
	interopAPI := &TemporaryInteropAPI{Eng: eng}

	srv := rpc.NewServer(host, port, "v0.0.1",
		rpc.WithAPIs([]gethrpc.API{
			{
				Namespace:     "interop",
				Service:       interopAPI,
				Authenticated: false,
			},
		}))

	return &TemporaryInteropServer{srv: srv}
}

func (s *TemporaryInteropServer) Start() error {
	return s.srv.Start()
}

func (s *TemporaryInteropServer) Endpoint() string {
	return fmt.Sprintf("http://%s", s.srv.Endpoint())
}

func (s *TemporaryInteropServer) Close() error {
	return s.srv.Stop()
}

type Engine interface {
	FetchReceipts(ctx context.Context, blockHash common.Hash) (eth.BlockInfo, types.Receipts, error)
	BlockRefByNumber(ctx context.Context, num uint64) (eth.BlockRef, error)
	ChainID(ctx context.Context) (*big.Int, error)
}

type TemporaryInteropAPI struct {
	Eng Engine
}

func (ib *TemporaryInteropAPI) FetchReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
	_, receipts, err := ib.Eng.FetchReceipts(ctx, blockHash)
	return receipts, err
}

func (ib *TemporaryInteropAPI) BlockRefByNumber(ctx context.Context, num uint64) (eth.BlockRef, error) {
	return ib.Eng.BlockRefByNumber(ctx, num)
}

func (ib *TemporaryInteropAPI) ChainID(ctx context.Context) (supervisortypes.ChainID, error) {
	v, err := ib.Eng.ChainID(ctx)
	if err != nil {
		return supervisortypes.ChainID{}, err
	}
	return supervisortypes.ChainIDFromBig(v), nil
}