Skip to content

Commit

Permalink
Feat/cors (#42)
Browse files Browse the repository at this point in the history
* add cors server config

* bug when deleting future withdrawals
  • Loading branch information
sh-cha authored Nov 15, 2024
1 parent 05f8a32 commit 13e1849
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 27 deletions.
6 changes: 2 additions & 4 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/initia-labs/opinit-bots/db"
"github.com/initia-labs/opinit-bots/executor"
executortypes "github.com/initia-labs/opinit-bots/executor/types"
"github.com/initia-labs/opinit-bots/server"
)

func LoadJsonConfig(path string, config bottypes.Config) error {
Expand Down Expand Up @@ -46,7 +45,6 @@ func NewBot(botType bottypes.BotType, logger *zap.Logger, homePath string, confi
if err != nil {
return nil, err
}
server := server.NewServer()

switch botType {
case bottypes.BotTypeExecutor:
Expand All @@ -55,14 +53,14 @@ func NewBot(botType bottypes.BotType, logger *zap.Logger, homePath string, confi
if err != nil {
return nil, err
}
return executor.NewExecutor(cfg, db, server, logger.Named("executor"), homePath), nil
return executor.NewExecutor(cfg, db, logger.Named("executor"), homePath), nil
case bottypes.BotTypeChallenger:
cfg := &challengertypes.Config{}
err := LoadJsonConfig(configPath, cfg)
if err != nil {
return nil, err
}
return challenger.NewChallenger(cfg, db, server, logger.Named("challenger"), homePath), nil
return challenger.NewChallenger(cfg, db, logger.Named("challenger"), homePath), nil
}
return nil, errors.New("not providing bot name")
}
Expand Down
6 changes: 3 additions & 3 deletions challenger/challenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Challenger struct {
latestChallenges []challengertypes.Challenge
}

func NewChallenger(cfg *challengertypes.Config, db types.DB, sv *server.Server, logger *zap.Logger, homePath string) *Challenger {
func NewChallenger(cfg *challengertypes.Config, db types.DB, logger *zap.Logger, homePath string) *Challenger {
err := cfg.Validate()
if err != nil {
panic(err)
Expand All @@ -67,7 +67,7 @@ func NewChallenger(cfg *challengertypes.Config, db types.DB, sv *server.Server,

cfg: cfg,
db: db,
server: sv,
server: server.NewServer(cfg.Server),
logger: logger,

homePath: homePath,
Expand Down Expand Up @@ -162,7 +162,7 @@ func (c *Challenger) Start(ctx context.Context) error {
defer func() {
c.logger.Info("api server stopped")
}()
return c.server.Start(c.cfg.ListenAddress)
return c.server.Start()
})

errGrp.Go(func() error {
Expand Down
19 changes: 13 additions & 6 deletions challenger/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"

nodetypes "github.com/initia-labs/opinit-bots/node/types"
servertypes "github.com/initia-labs/opinit-bots/server/types"
)

type NodeConfig struct {
Expand All @@ -29,8 +30,8 @@ type Config struct {
// Version is the version used to build output root.
Version uint8 `json:"version"`

// ListenAddress is the address to listen for incoming requests.
ListenAddress string `json:"listen_address"`
// Server is the configuration for the server.
Server servertypes.ServerConfig `json:"server"`

// L1Node is the configuration for the l1 node.
L1Node NodeConfig `json:"l1_node"`
Expand All @@ -53,8 +54,14 @@ type Config struct {

func DefaultConfig() *Config {
return &Config{
Version: 1,
ListenAddress: "localhost:3001",
Version: 1,

Server: servertypes.ServerConfig{
Address: "localhost:3001",
AllowOrigins: "*",
AllowHeaders: "Origin, Content-Type, Accept",
AllowMethods: "GET",
},

L1Node: NodeConfig{
ChainID: "testnet-l1-1",
Expand Down Expand Up @@ -82,8 +89,8 @@ func (cfg Config) Validate() error {
return errors.New("only version 1 is supported")
}

if cfg.ListenAddress == "" {
return errors.New("listen address is required")
if err := cfg.Server.Validate(); err != nil {
return err
}

if err := cfg.L1Node.Validate(); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions executor/child/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ func (ch *Child) GetLastAddressIndex(address string) (lastIndex uint64, err erro

func (ch *Child) DeleteFutureWithdrawals(fromSequence uint64) error {
return ch.DB().PrefixedIterate(executortypes.WithdrawalKey, nil, func(key, _ []byte) (bool, error) {
if len(key) != len(executortypes.WithdrawalKey)+1+8 {
return false, nil
}
sequence := dbtypes.ToUint64Key(key[len(key)-8:])
if sequence >= fromSequence {
err := ch.DB().Delete(key)
Expand Down
6 changes: 3 additions & 3 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Executor struct {
homePath string
}

func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logger *zap.Logger, homePath string) *Executor {
func NewExecutor(cfg *executortypes.Config, db types.DB, logger *zap.Logger, homePath string) *Executor {
err := cfg.Validate()
if err != nil {
panic(err)
Expand All @@ -66,7 +66,7 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg

cfg: cfg,
db: db,
server: sv,
server: server.NewServer(cfg.Server),
logger: logger,

homePath: homePath,
Expand Down Expand Up @@ -135,7 +135,7 @@ func (ex *Executor) Start(ctx context.Context) error {
defer func() {
ex.logger.Info("api server stopped")
}()
return ex.server.Start(ex.cfg.ListenAddress)
return ex.server.Start()
})
ex.host.Start(ctx)
ex.child.Start(ctx)
Expand Down
20 changes: 14 additions & 6 deletions executor/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
nodetypes "github.com/initia-labs/opinit-bots/node/types"

servertypes "github.com/initia-labs/opinit-bots/server/types"
)

type NodeConfig struct {
Expand Down Expand Up @@ -34,8 +36,8 @@ type Config struct {
// Version is the version used to build output root.
Version uint8 `json:"version"`

// ListenAddress is the address to listen for incoming requests.
ListenAddress string `json:"listen_address"`
// Server is the configuration for the server.
Server servertypes.ServerConfig `json:"server"`

// L1Node is the configuration for the l1 node.
L1Node NodeConfig `json:"l1_node"`
Expand Down Expand Up @@ -84,8 +86,14 @@ type Config struct {

func DefaultConfig() *Config {
return &Config{
Version: 1,
ListenAddress: "localhost:3000",
Version: 1,

Server: servertypes.ServerConfig{
Address: "localhost:3000",
AllowOrigins: "*",
AllowHeaders: "Origin, Content-Type, Accept",
AllowMethods: "GET",
},

L1Node: NodeConfig{
ChainID: "testnet-l1-1",
Expand Down Expand Up @@ -138,8 +146,8 @@ func (cfg Config) Validate() error {
return errors.New("only version 1 is supported")
}

if cfg.ListenAddress == "" {
return errors.New("listen address is required")
if err := cfg.Server.Validate(); err != nil {
return err
}

if err := cfg.L1Node.Validate(); err != nil {
Expand Down
23 changes: 18 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package server

import "github.com/gofiber/fiber/v2"
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/initia-labs/opinit-bots/server/types"
)

type Server struct {
address string
*fiber.App
}

func NewServer() *Server {
func NewServer(cfg types.ServerConfig) *Server {
app := fiber.New()
app.Use(cors.New(cors.Config{
AllowOrigins: cfg.AllowOrigins,
AllowHeaders: cfg.AllowHeaders,
AllowMethods: cfg.AllowMethods,
}))

return &Server{
fiber.New(),
address: cfg.Address,
App: app,
}
}

func (s *Server) Start(address string) error {
return s.Listen(address)
func (s *Server) Start() error {
return s.Listen(s.address)
}

func (s *Server) RegisterQuerier(path string, fn func(c *fiber.Ctx) error) {
Expand Down
17 changes: 17 additions & 0 deletions server/types/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package types

import "errors"

type ServerConfig struct {
Address string `json:"address"`
AllowOrigins string `json:"allow_origins"`
AllowHeaders string `json:"allow_headers"`
AllowMethods string `json:"allow_methods"`
}

func (s ServerConfig) Validate() error {
if s.Address == "" {
return errors.New("address is required")
}
return nil
}

0 comments on commit 13e1849

Please sign in to comment.