Commit 43a8447f authored by mergify[bot]'s avatar mergify[bot] Committed by GitHub

Merge branch 'develop' into dependabot/npm_and_yarn/codechecks/client-0.1.12

parents 31b23d48 8f56a3d9
docker-compose.dev.yml
.env
indexer
/indexer
package api
import (
"context"
"fmt"
"net/http"
"github.com/ethereum-optimism/optimism/indexer/api/routes"
"github.com/ethereum-optimism/optimism/indexer/database"
"github.com/ethereum-optimism/optimism/op-service/httputil"
"github.com/ethereum/go-ethereum/log"
"github.com/go-chi/chi/v5"
)
......@@ -13,25 +15,29 @@ import (
const ethereumAddressRegex = `^0x[a-fA-F0-9]{40}$`
type Api struct {
log log.Logger
Router *chi.Mux
}
func NewApi(bv database.BridgeTransfersView, logger log.Logger) *Api {
logger.Info("Initializing API...")
func NewApi(logger log.Logger, bv database.BridgeTransfersView) *Api {
r := chi.NewRouter()
h := routes.NewRoutes(logger, bv, r)
api := &Api{Router: r}
r.Get("/healthz", h.HealthzHandler)
r.Get(fmt.Sprintf("/api/v0/deposits/{address:%s}", ethereumAddressRegex), h.L1DepositsHandler)
r.Get(fmt.Sprintf("/api/v0/withdrawals/{address:%s}", ethereumAddressRegex), h.L2WithdrawalsHandler)
return api
return &Api{log: logger, Router: r}
}
func (a *Api) Listen(port string) error {
return http.ListenAndServe(port, a.Router)
func (a *Api) Listen(ctx context.Context, port int) error {
a.log.Info("starting api server", "port", port)
server := http.Server{Addr: fmt.Sprintf(":%d", port), Handler: a.Router}
err := httputil.ListenAndServeContext(ctx, &server)
if err != nil {
a.log.Error("api server shutdown", "err", err)
} else {
a.log.Info("api server shutdown")
}
return err
}
......@@ -77,7 +77,7 @@ func (mbv *MockBridgeTransfersView) L2BridgeWithdrawalsByAddress(address common.
}
func TestHealthz(t *testing.T) {
logger := testlog.Logger(t, log.LvlInfo)
api := NewApi(&MockBridgeTransfersView{}, logger)
api := NewApi(logger, &MockBridgeTransfersView{})
request, err := http.NewRequest("GET", "/healthz", nil)
assert.Nil(t, err)
......@@ -89,7 +89,7 @@ func TestHealthz(t *testing.T) {
func TestL1BridgeDepositsHandler(t *testing.T) {
logger := testlog.Logger(t, log.LvlInfo)
api := NewApi(&MockBridgeTransfersView{}, logger)
api := NewApi(logger, &MockBridgeTransfersView{})
request, err := http.NewRequest("GET", fmt.Sprintf("/api/v0/deposits/%s", mockAddress), nil)
assert.Nil(t, err)
......@@ -101,7 +101,7 @@ func TestL1BridgeDepositsHandler(t *testing.T) {
func TestL2BridgeWithdrawalsByAddressHandler(t *testing.T) {
logger := testlog.Logger(t, log.LvlInfo)
api := NewApi(&MockBridgeTransfersView{}, logger)
api := NewApi(logger, &MockBridgeTransfersView{})
request, err := http.NewRequest("GET", fmt.Sprintf("/api/v0/withdrawals/%s", mockAddress), nil)
assert.Nil(t, err)
......
package cli
package main
import (
"context"
"fmt"
"strconv"
"github.com/ethereum-optimism/optimism/indexer"
"github.com/ethereum-optimism/optimism/indexer/api"
......@@ -16,26 +14,25 @@ import (
"github.com/urfave/cli/v2"
)
type Cli struct {
GitVersion string
GitCommit string
GitDate string
app *cli.App
Flags []cli.Flag
}
var (
ConfigFlag = &cli.StringFlag{
Name: "config",
Value: "./indexer.toml",
Aliases: []string{"c"},
Usage: "path to config file",
EnvVars: []string{"INDEXER_CONFIG"},
}
)
func runIndexer(ctx *cli.Context) error {
logger := log.NewLogger(log.ReadCLIConfig(ctx))
configPath := ctx.String(ConfigFlag.Name)
cfg, err := config.LoadConfig(logger, configPath)
cfg, err := config.LoadConfig(logger, ctx.String(ConfigFlag.Name))
if err != nil {
logger.Error("failed to load config", "err", err)
return err
}
db, err := database.NewDB(cfg.DB)
if err != nil {
return err
}
......@@ -48,6 +45,7 @@ func runIndexer(ctx *cli.Context) error {
indexerCtx, indexerCancel := context.WithCancel(context.Background())
go func() {
opio.BlockOnInterrupts()
logger.Error("caught interrupt, shutting down...")
indexerCancel()
}()
......@@ -56,47 +54,35 @@ func runIndexer(ctx *cli.Context) error {
func runApi(ctx *cli.Context) error {
logger := log.NewLogger(log.ReadCLIConfig(ctx))
configPath := ctx.String(ConfigFlag.Name)
cfg, err := config.LoadConfig(logger, configPath)
cfg, err := config.LoadConfig(logger, ctx.String(ConfigFlag.Name))
if err != nil {
logger.Error("failed to load config", "err", err)
return err
}
db, err := database.NewDB(cfg.DB)
if err != nil {
logger.Crit("Failed to connect to database", "err", err)
}
server := api.NewApi(db.BridgeTransfers, logger)
return server.Listen(strconv.Itoa(cfg.API.Port))
}
var (
ConfigFlag = &cli.StringFlag{
Name: "config",
Value: "./indexer.toml",
Aliases: []string{"c"},
Usage: "path to config file",
EnvVars: []string{"INDEXER_CONFIG"},
}
)
apiCtx, apiCancel := context.WithCancel(context.Background())
api := api.NewApi(logger, db.BridgeTransfers)
go func() {
opio.BlockOnInterrupts()
logger.Error("caught interrupt, shutting down...")
apiCancel()
}()
// make a instance method on Cli called Run that runs cli
// and returns an error
func (c *Cli) Run(args []string) error {
return c.app.Run(args)
return api.Listen(apiCtx, cfg.API.Port)
}
func NewCli(GitVersion string, GitCommit string, GitDate string) *Cli {
func newCli(GitCommit string, GitDate string) *cli.App {
flags := []cli.Flag{ConfigFlag}
flags = append(flags, log.CLIFlags("INDEXER")...)
app := &cli.App{
Version: fmt.Sprintf("%s-%s", GitVersion, params.VersionWithCommit(GitCommit, GitDate)),
Description: "An indexer of all optimism events with a serving api layer",
return &cli.App{
Version: params.VersionWithCommit(GitCommit, GitDate),
Description: "An indexer of all optimism events with a serving api layer",
EnableBashCompletion: true,
Commands: []*cli.Command{
{
Name: "api",
......@@ -110,11 +96,14 @@ func NewCli(GitVersion string, GitCommit string, GitDate string) *Cli {
Description: "Runs the indexing service",
Action: runIndexer,
},
{
Name: "version",
Description: "print version",
Action: func(ctx *cli.Context) error {
cli.ShowVersion(ctx)
return nil
},
},
},
}
return &Cli{
app: app,
Flags: flags,
}
}
......@@ -3,20 +3,17 @@ package main
import (
"os"
"github.com/ethereum-optimism/optimism/indexer/cli"
"github.com/ethereum/go-ethereum/log"
)
var (
GitVersion = ""
GitCommit = ""
GitDate = ""
GitCommit = ""
GitDate = ""
)
func main() {
app := cli.NewCli(GitVersion, GitCommit, GitDate)
app := newCli(GitCommit, GitDate)
if err := app.Run(os.Args); err != nil {
log.Crit("Application failed", "message", err)
log.Crit("application failed", "err", err)
}
}
......@@ -99,20 +99,18 @@ type MetricsConfig struct {
// LoadConfig loads the `indexer.toml` config file from a given path
func LoadConfig(logger geth_log.Logger, path string) (Config, error) {
logger.Info("Loading config file", "path", path)
var conf Config
logger.Debug("loading config", "path", path)
var conf Config
data, err := os.ReadFile(path)
if err != nil {
return conf, err
}
data = []byte(os.ExpandEnv(string(data)))
logger.Debug("Decoding config file", "data", string(data))
logger.Debug("parsed config file", "data", string(data))
if _, err := toml.Decode(string(data), &conf); err != nil {
logger.Info("Failed to decode config file", "message", err)
logger.Info("failed to decode config file", "err", err)
return conf, err
}
......@@ -125,7 +123,6 @@ func LoadConfig(logger geth_log.Logger, path string) (Config, error) {
}
}
logger.Debug("Loaded config file", conf)
logger.Info("loaded config")
return conf, nil
}
......@@ -13,7 +13,7 @@ import (
const gameDirPrefix = "game-"
// diskManager coordinates
// diskManager coordinates the storage of game data on disk.
type diskManager struct {
datadir string
}
......@@ -42,11 +42,11 @@ func (d *diskManager) RemoveAllExcept(keep []common.Address) error {
name := entry.Name()[len(gameDirPrefix):]
addr := common.HexToAddress(name)
if addr == (common.Address{}) {
// Couldn't parse the directory name to an address so mustn't be a game directory
// Ignore directories with non-address names.
continue
}
if slices.Contains(keep, addr) {
// We need to preserve this data
// Preserve data for games we should keep.
continue
}
if err := os.RemoveAll(filepath.Join(d.datadir, entry.Name())); err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment