Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!(api/rpc): OpenRPC scaffolding, migrating OpenAPI gateway to api/gateway #1175

Merged
merged 8 commits into from
Oct 26, 2022
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
"encoding/json"
Expand Down
31 changes: 31 additions & 0 deletions api/gateway/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gateway

import (
"fmt"
"net"
"strconv"
)

type Config struct {
Address string
Port string
}

func DefaultConfig() Config {
return Config{
Address: "0.0.0.0",
// do NOT expose the same port as celestia-core by default so that both can run on the same machine
Port: "26658",
}
}

func (cfg *Config) Validate() error {
if ip := net.ParseIP(cfg.Address); ip == nil {
return fmt.Errorf("service/gateway: invalid listen address format: %s", cfg.Address)
}
_, err := strconv.Atoi(cfg.Port)
if err != nil {
return fmt.Errorf("service/gateway: invalid port: %s", err.Error())
}
return nil
}
2 changes: 1 addition & 1 deletion service/rpc/das.go → api/gateway/das.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion service/rpc/endpoints.go → api/gateway/endpoints.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions service/rpc/handler.go → api/gateway/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
logging "github.com/ipfs/go-log/v2"
Expand All @@ -9,7 +9,7 @@ import (
"github.com/celestiaorg/celestia-node/nodebuilder/state"
)

var log = logging.Logger("rpc")
var log = logging.Logger("gateway")

type Handler struct {
state state.Module
Expand Down
2 changes: 1 addition & 1 deletion service/rpc/header.go → api/gateway/header.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion service/rpc/middleware.go → api/gateway/middleware.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
"errors"
Expand Down
4 changes: 2 additions & 2 deletions service/rpc/server.go → api/gateway/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
"context"
Expand All @@ -11,7 +11,7 @@ import (
)

// Server represents an RPC server on the Node.
// TODO @renaynay: eventually, rpc server should be able to be toggled on and off.
// TODO @renaynay: eventually, gateway server should be able to be toggled on and off.
type Server struct {
cfg Config

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion service/rpc/share.go → api/gateway/share.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion service/rpc/state.go → api/gateway/state.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
"encoding/hex"
Expand Down
2 changes: 1 addition & 1 deletion service/rpc/util.go → api/gateway/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpc
package gateway

import (
"encoding/json"
Expand Down
60 changes: 60 additions & 0 deletions api/rpc/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package rpc

import (
"context"
"net/http"
"time"

"github.com/filecoin-project/go-jsonrpc"
logging "github.com/ipfs/go-log/v2"
)

var log = logging.Logger("rpc")

type Server struct {
http *http.Server
rpc *jsonrpc.RPCServer
}

func NewServer(address string, port string) *Server {
rpc := jsonrpc.NewServer()
return &Server{
rpc: rpc,
http: &http.Server{
Addr: address + ":" + port,
Handler: rpc,
// the amount of time allowed to read request headers. set to the default 2 seconds
ReadHeaderTimeout: 2 * time.Second,
},
}
}

// RegisterService registers a service onto the RPC server. All methods on the service will then be exposed over the
// RPC.
func (s *Server) RegisterService(namespace string, service interface{}) {
s.rpc.Register(namespace, service)
}

// Start starts the RPC Server.
func (s *Server) Start(context.Context) error {
//nolint:errcheck
go s.http.ListenAndServe()
log.Infow("RPC server started", "listening on", s.http.Addr)
return nil
}

// Stop stops the RPC Server.
func (s *Server) Stop(ctx context.Context) error {
// if server already stopped, return
err := s.http.Shutdown(ctx)
if err != nil {
return err
}
log.Info("RPC server stopped")
return nil
}

// ListenAddr returns the listen address of the server.
func (s *Server) ListenAddr() string {
return s.http.Addr
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/cosmos/cosmos-sdk v0.46.0
github.com/cosmos/cosmos-sdk/api v0.1.0
github.com/dgraph-io/badger/v2 v2.2007.4
github.com/filecoin-project/go-jsonrpc v0.1.8
github.com/gammazero/workerpool v1.1.3
github.com/gogo/protobuf v1.3.3
github.com/gorilla/mux v1.8.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/filecoin-project/go-jsonrpc v0.1.8 h1:uXX/ikAk3Q4f/k8DRd9Zw+fWnfiYb5I+UI1tzlQgHog=
github.com/filecoin-project/go-jsonrpc v0.1.8/go.mod h1:XBBpuKIMaXIIzeqzO1iucq4GvbF8CxmXRFoezRh+Cx4=
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/flynn/noise v1.0.0 h1:DlTHqmzmvcEiKj+4RYo/imoswx/4r6iBlCMfVtrMXpQ=
Expand Down Expand Up @@ -623,6 +625,7 @@ github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
Expand Down Expand Up @@ -819,6 +822,7 @@ github.com/ipfs/go-log v1.0.5/go.mod h1:j0b8ZoR+7+R99LD9jZ6+AJsrzkPbSXbZfGakb5JP
github.com/ipfs/go-log/v2 v2.0.2/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0=
github.com/ipfs/go-log/v2 v2.0.3/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0=
github.com/ipfs/go-log/v2 v2.0.5/go.mod h1:eZs4Xt4ZUJQFM3DlanGhy7TkwwawCZcSByscwkWG+dw=
github.com/ipfs/go-log/v2 v2.0.8/go.mod h1:eZs4Xt4ZUJQFM3DlanGhy7TkwwawCZcSByscwkWG+dw=
github.com/ipfs/go-log/v2 v2.1.1/go.mod h1:2v2nsGfZsvvAJz13SyFzf9ObaqwHiHxsPLEHntrv9KM=
github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g=
github.com/ipfs/go-log/v2 v2.3.0/go.mod h1:QqGoj30OTpnKaG/LKTGTxoP2mmQtjVMEnK72gynbe/g=
Expand Down
2 changes: 1 addition & 1 deletion nodebuilder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/celestiaorg/celestia-node/nodebuilder/header"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/celestiaorg/celestia-node/nodebuilder/rpc"
"github.com/celestiaorg/celestia-node/nodebuilder/share"
"github.com/celestiaorg/celestia-node/nodebuilder/state"
"github.com/celestiaorg/celestia-node/service/rpc"
)

// ConfigLoader defines a function that loads a config from any source.
Expand Down
2 changes: 1 addition & 1 deletion nodebuilder/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
"github.com/libp2p/go-libp2p/p2p/net/conngater"
"go.uber.org/fx"

"github.com/celestiaorg/celestia-node/api/rpc"
"github.com/celestiaorg/celestia-node/das"
"github.com/celestiaorg/celestia-node/nodebuilder/fraud"
"github.com/celestiaorg/celestia-node/nodebuilder/header"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/celestiaorg/celestia-node/nodebuilder/share"
"github.com/celestiaorg/celestia-node/nodebuilder/state"
"github.com/celestiaorg/celestia-node/service/rpc"
)

const Timeout = time.Second * 15
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions nodebuilder/rpc/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package rpc
import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/celestiaorg/celestia-node/service/rpc"
)

var (
Expand All @@ -31,7 +29,7 @@ func Flags() *flag.FlagSet {
}

// ParseFlags parses RPC flags from the given cmd and saves them to the passed config.
func ParseFlags(cmd *cobra.Command, cfg *rpc.Config) {
func ParseFlags(cmd *cobra.Command, cfg *Config) {
addr := cmd.Flag(addrFlag).Value.String()
if addr != "" {
cfg.Address = addr
Expand Down
18 changes: 9 additions & 9 deletions nodebuilder/rpc/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import (

"go.uber.org/fx"

"github.com/celestiaorg/celestia-node/api/rpc"
headerServ "github.com/celestiaorg/celestia-node/nodebuilder/header"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
shareServ "github.com/celestiaorg/celestia-node/nodebuilder/share"
stateServ "github.com/celestiaorg/celestia-node/nodebuilder/state"
rpcServ "github.com/celestiaorg/celestia-node/service/rpc"
)

func ConstructModule(tp node.Type, cfg *rpcServ.Config) fx.Option {
func ConstructModule(tp node.Type, cfg *Config) fx.Option {
// sanitize config values before constructing module
cfgErr := cfg.Validate()

baseComponents := fx.Options(
fx.Supply(*cfg),
fx.Supply(cfg),
fx.Error(cfgErr),
fx.Provide(fx.Annotate(
rpcServ.NewServer,
fx.OnStart(func(ctx context.Context, server *rpcServ.Server) error {
Server,
fx.OnStart(func(ctx context.Context, server *rpc.Server) error {
return server.Start(ctx)
}),
fx.OnStop(func(ctx context.Context, server *rpcServ.Server) error {
fx.OnStop(func(ctx context.Context, server *rpc.Server) error {
return server.Stop(ctx)
}),
)),
Expand All @@ -35,7 +35,7 @@ func ConstructModule(tp node.Type, cfg *rpcServ.Config) fx.Option {
return fx.Module(
"rpc",
baseComponents,
fx.Invoke(Handler),
fx.Invoke(RegisterEndpoints),
)
case node.Bridge:
return fx.Module(
Expand All @@ -45,9 +45,9 @@ func ConstructModule(tp node.Type, cfg *rpcServ.Config) fx.Option {
state stateServ.Module,
share shareServ.Module,
header headerServ.Module,
rpcSrv *rpcServ.Server,
rpcSrv *rpc.Server,
) {
Handler(state, share, header, rpcSrv, nil)
RegisterEndpoints(state, share, header, rpcSrv, nil)
}),
)
default:
Expand Down
17 changes: 11 additions & 6 deletions nodebuilder/rpc/rpc.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package rpc

import (
"github.com/celestiaorg/celestia-node/api/rpc"
"github.com/celestiaorg/celestia-node/das"
"github.com/celestiaorg/celestia-node/nodebuilder/header"
"github.com/celestiaorg/celestia-node/nodebuilder/share"
"github.com/celestiaorg/celestia-node/nodebuilder/state"
"github.com/celestiaorg/celestia-node/service/rpc"
)

// Handler constructs a new RPC Handler from the given services.
func Handler(
// RegisterEndpoints registers the given services on the rpc.
func RegisterEndpoints(
state state.Module,
share share.Module,
header header.Module,
serv *rpc.Server,
daser *das.DASer,
) {
handler := rpc.NewHandler(state, share, header, daser)
handler.RegisterEndpoints(serv)
handler.RegisterMiddleware(serv)
serv.RegisterService("state", state)
serv.RegisterService("share", share)
serv.RegisterService("header", header)
serv.RegisterService("daser", daser)
}

func Server(cfg *Config) *rpc.Server {
return rpc.NewServer(cfg.Address, cfg.Port)
}