service_test.go 2.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
package supervisor

import (
	"context"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/log"

	"github.com/ethereum-optimism/optimism/op-service/dial"
	oplog "github.com/ethereum-optimism/optimism/op-service/log"
	opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
	"github.com/ethereum-optimism/optimism/op-service/oppprof"
	oprpc "github.com/ethereum-optimism/optimism/op-service/rpc"
	"github.com/ethereum-optimism/optimism/op-service/testlog"
19
	"github.com/ethereum-optimism/optimism/op-supervisor/config"
20 21 22 23
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/types"
)

func TestSupervisorService(t *testing.T) {
24
	cfg := &config.Config{
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
		Version: "",
		LogConfig: oplog.CLIConfig{
			Level:  log.LevelError,
			Color:  false,
			Format: oplog.FormatLogFmt,
		},
		MetricsConfig: opmetrics.CLIConfig{
			Enabled:    true,
			ListenAddr: "127.0.0.1",
			ListenPort: 0, // pick a port automatically
		},
		PprofConfig: oppprof.CLIConfig{
			ListenEnabled:   true,
			ListenAddr:      "127.0.0.1",
			ListenPort:      0, // pick a port automatically
			ProfileType:     "",
			ProfileDir:      "",
			ProfileFilename: "",
		},
		RPC: oprpc.CLIConfig{
			ListenAddr:  "127.0.0.1",
			ListenPort:  0, // pick a port automatically
			EnableAdmin: true,
		},
		MockRun: true,
	}
	logger := testlog.Logger(t, log.LevelError)
52
	supervisor, err := SupervisorFromConfig(context.Background(), cfg, logger)
53 54 55 56 57 58 59 60 61 62
	require.NoError(t, err)
	require.NoError(t, supervisor.Start(context.Background()), "start service")
	// run some RPC tests against the service with the mock backend
	{
		endpoint := "http://" + supervisor.rpcServer.Endpoint()
		t.Logf("dialing %s", endpoint)
		cl, err := dial.DialRPCClientWithTimeout(context.Background(), time.Second*5, logger, endpoint)
		require.NoError(t, err)
		ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
		var dest types.SafetyLevel
63 64 65 66 67 68 69 70
		err = cl.CallContext(ctx, &dest, "supervisor_checkMessage",
			types.Identifier{
				Origin:      common.Address{0xaa},
				BlockNumber: 123,
				LogIndex:    42,
				Timestamp:   1234567,
				ChainID:     types.ChainID{0xbb},
			}, common.Hash{0xcc})
71 72 73 74 75 76 77
		cancel()
		require.NoError(t, err)
		require.Equal(t, types.CrossUnsafe, dest, "expecting mock to return cross-unsafe")
		cl.Close()
	}
	require.NoError(t, supervisor.Stop(context.Background()), "stop service")
}