From 13e18498c0fb75b2847793333f3ed90a9ae7a836 Mon Sep 17 00:00:00 2001 From: Hoon <48665813+sh-cha@users.noreply.github.com> Date: Fri, 15 Nov 2024 13:11:17 +0900 Subject: [PATCH] Feat/cors (#42) * add cors server config * bug when deleting future withdrawals --- bot/bot.go | 6 ++---- challenger/challenger.go | 6 +++--- challenger/types/config.go | 19 +++++++++++++------ executor/child/withdraw.go | 3 +++ executor/executor.go | 6 +++--- executor/types/config.go | 20 ++++++++++++++------ server/server.go | 23 ++++++++++++++++++----- server/types/config.go | 17 +++++++++++++++++ 8 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 server/types/config.go diff --git a/bot/bot.go b/bot/bot.go index 20e1459..a2506a5 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -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 { @@ -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: @@ -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") } diff --git a/challenger/challenger.go b/challenger/challenger.go index 535d38f..8e3b7d3 100644 --- a/challenger/challenger.go +++ b/challenger/challenger.go @@ -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) @@ -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, @@ -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 { diff --git a/challenger/types/config.go b/challenger/types/config.go index 4865fc9..8cea786 100644 --- a/challenger/types/config.go +++ b/challenger/types/config.go @@ -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 { @@ -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"` @@ -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", @@ -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 { diff --git a/executor/child/withdraw.go b/executor/child/withdraw.go index 45fe609..a75a080 100644 --- a/executor/child/withdraw.go +++ b/executor/child/withdraw.go @@ -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) diff --git a/executor/executor.go b/executor/executor.go index ecb2c0a..5cee2a9 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -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) @@ -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, @@ -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) diff --git a/executor/types/config.go b/executor/types/config.go index c9b02e3..1617a69 100644 --- a/executor/types/config.go +++ b/executor/types/config.go @@ -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 { @@ -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"` @@ -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", @@ -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 { diff --git a/server/server.go b/server/server.go index 74e5fe1..a4e7812 100644 --- a/server/server.go +++ b/server/server.go @@ -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) { diff --git a/server/types/config.go b/server/types/config.go new file mode 100644 index 0000000..2f639f0 --- /dev/null +++ b/server/types/config.go @@ -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 +}