-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rahul0tripathi/ft-json-rpc
add json rpc server
- Loading branch information
Showing
9 changed files
with
258 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"unicode" | ||
|
||
"github.com/rahul0tripathi/go-jsonrpc" | ||
"github.com/rahul0tripathi/smelter/controller" | ||
"github.com/rahul0tripathi/smelter/pkg/log" | ||
"github.com/rahul0tripathi/smelter/pkg/server" | ||
"github.com/rahul0tripathi/smelter/services" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func Run() error { | ||
var err error | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
logger, err := log.NewZapLogger(false) | ||
if err != nil { | ||
return fmt.Errorf("failed to create logger, %w", err) | ||
} | ||
|
||
httpserver := server.New(":6969", logger) | ||
|
||
rpcService := &services.Rpc{} | ||
rpcServer := jsonrpc.NewServer(jsonrpc.WithNamespaceSeparator("_"), jsonrpc.WithMethodTransformer(func(s string) string { | ||
r := []rune(s) | ||
r[0] = unicode.ToLower(r[0]) | ||
return string(r) | ||
})) | ||
|
||
rpcServer.Register("eth", rpcService) | ||
|
||
controller.SetupRouter(httpserver.Router(), rpcServer, logger) | ||
|
||
httpserver.Start() | ||
|
||
// Waiting signal | ||
interrupt := make(chan os.Signal, 1) | ||
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) | ||
|
||
select { | ||
case <-ctx.Done(): | ||
logger.Info("M::context canceled") | ||
case s := <-interrupt: | ||
logger.Info("M::signal -> " + s.String()) | ||
case err = <-httpserver.Notify(): | ||
return fmt.Errorf("M::notify ->, %w", err) | ||
} | ||
|
||
err = httpserver.Shutdown() | ||
if err != nil { | ||
logger.Error("APP::shutdown, %s", zap.Error(err)) | ||
} | ||
|
||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"github.com/rahul0tripathi/go-jsonrpc" | ||
"github.com/rahul0tripathi/smelter/pkg/log" | ||
"github.com/rahul0tripathi/smelter/pkg/server" | ||
) | ||
|
||
func SetupRouter( | ||
router server.Router, | ||
rpcServer *jsonrpc.RPCServer, | ||
logger log.Logger, | ||
) { | ||
router.POST("/v1/rpc", echo.WrapHandler(rpcServer), func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
c.Response().Header().Set("Content-Type", "application/json") | ||
return next(c) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package log | ||
|
||
import "go.uber.org/zap" | ||
|
||
type Logger interface { | ||
Info(msg string, fields ...zap.Field) | ||
Error(msg string, fields ...zap.Field) | ||
Warn(msg string, fields ...zap.Field) | ||
Debug(msg string, fields ...zap.Field) | ||
} | ||
|
||
func NewZapLogger(dev bool, options ...zap.Option) (Logger, error) { | ||
if dev { | ||
return zap.NewDevelopment(options...) | ||
} | ||
|
||
return zap.NewProduction(options...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/rahul0tripathi/smelter/pkg/log" | ||
) | ||
|
||
type Server struct { | ||
app *echo.Echo | ||
notify chan error | ||
address string | ||
} | ||
|
||
type Router interface { | ||
GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route | ||
PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route | ||
POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route | ||
DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route | ||
} | ||
|
||
func New(address string, logger log.Logger) *Server { | ||
app := echo.New() | ||
app.HideBanner = true | ||
|
||
return &Server{ | ||
app: app, | ||
notify: make(chan error), | ||
address: address, | ||
} | ||
|
||
} | ||
|
||
func (s *Server) Router() Router { | ||
return s.app | ||
} | ||
|
||
func (s *Server) Start() { | ||
go func() { | ||
s.notify <- s.app.Start(s.address) | ||
}() | ||
} | ||
|
||
func (s *Server) Notify() <-chan error { | ||
return s.notify | ||
} | ||
|
||
func (s *Server) Shutdown() error { | ||
return s.app.Shutdown(context.Background()) | ||
} | ||
|
||
func ResponseJSON(c echo.Context, status int, response interface{}) error { | ||
return c.JSON(status, response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ethereum/go-ethereum" | ||
"github.com/ethereum/go-ethereum/core/tracing" | ||
"github.com/rahul0tripathi/smelter/entity" | ||
) | ||
|
||
type executor interface { | ||
CallAndPersist( | ||
ctx context.Context, | ||
tx ethereum.CallMsg, | ||
hooks *tracing.Hooks, | ||
overrides entity.StateOverrides, | ||
) (ret []byte, leftOverGas uint64, err error) | ||
Call( | ||
ctx context.Context, | ||
tx ethereum.CallMsg, | ||
hooks *tracing.Hooks, | ||
overrides entity.StateOverrides, | ||
) (ret []byte, leftOverGas uint64, err error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
) | ||
|
||
type Rpc struct { | ||
executor executor | ||
} | ||
|
||
func NewRpcService(executor executor) *Rpc { | ||
return &Rpc{executor: executor} | ||
} | ||
|
||
func (r *Rpc) ChainId(ctx context.Context) string { | ||
return hexutil.Encode(new(big.Int).SetInt64(69).Bytes()) | ||
} |