Skip to content

Commit

Permalink
broadcast_tx via grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman committed Jun 21, 2016
1 parent aaea0c5 commit 31b0ab6
Show file tree
Hide file tree
Showing 9 changed files with 511 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/tendermint/config.go
Expand Up @@ -66,6 +66,7 @@ func GetConfig(rootDir string) cfg.Config {
mapConfig.SetDefault("db_dir", rootDir+"/data")
mapConfig.SetDefault("log_level", "info")
mapConfig.SetDefault("rpc_laddr", "0.0.0.0:46657")
mapConfig.SetDefault("grpc_laddr", "")
mapConfig.SetDefault("prof_laddr", "")
mapConfig.SetDefault("revision_file", rootDir+"/revision")
mapConfig.SetDefault("cswal", rootDir+"/data/cswal")
Expand Down
1 change: 1 addition & 0 deletions config/tendermint_test/config.go
Expand Up @@ -79,6 +79,7 @@ func ResetConfig(localPath string) cfg.Config {
mapConfig.SetDefault("db_dir", rootDir+"/data")
mapConfig.SetDefault("log_level", "debug")
mapConfig.SetDefault("rpc_laddr", "0.0.0.0:36657")
mapConfig.SetDefault("grpc_laddr", "tcp://0.0.0.0:36658") // sigh
mapConfig.SetDefault("prof_laddr", "")
mapConfig.SetDefault("revision_file", rootDir+"/revision")
mapConfig.SetDefault("cswal", rootDir+"/data/cswal")
Expand Down
10 changes: 10 additions & 0 deletions node/node.go
Expand Up @@ -23,6 +23,7 @@ import (
mempl "github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/proxy"
rpccore "github.com/tendermint/tendermint/rpc/core"
grpccore "github.com/tendermint/tendermint/rpc/grpc"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
"github.com/tendermint/tendermint/version"
Expand Down Expand Up @@ -200,6 +201,15 @@ func (n *Node) StartRPC() ([]net.Listener, error) {
}
listeners[i] = listener
}

listenAddr := n.config.GetString("grpc_laddr")
if listenAddr != "" {
_, err := grpccore.NewGRPCServer(listenAddr)
if err != nil {
return nil, err
}
}

return listeners, nil
}

Expand Down
18 changes: 18 additions & 0 deletions rpc/grpc/api.go
@@ -0,0 +1,18 @@
package core_grpc

import (
core "github.com/tendermint/tendermint/rpc/core"

context "golang.org/x/net/context"
)

type broadcastAPI struct {
}

func (bapi *broadcastAPI) BroadcastTx(ctx context.Context, req *RequestBroadcastTx) (*ResponseBroadcastTx, error) {
res, err := core.BroadcastTxSync(req.Tx)
if err != nil {
return nil, err
}
return &ResponseBroadcastTx{uint64(res.Code), res.Data, res.Log}, nil
}
51 changes: 51 additions & 0 deletions rpc/grpc/server.go
@@ -0,0 +1,51 @@
package core_grpc

import (
"net"
"strings"

"google.golang.org/grpc"

. "github.com/tendermint/go-common"
)

// var maxNumberConnections = 2

type GRPCServer struct {
QuitService

proto string
addr string
listener net.Listener
}

func NewGRPCServer(protoAddr string) (Service, error) {
parts := strings.SplitN(protoAddr, "://", 2)
proto, addr := parts[0], parts[1]
s := &GRPCServer{
proto: proto,
addr: addr,
listener: nil,
}
s.QuitService = *NewQuitService(nil, "TendermintRPCServer", s)
_, err := s.Start() // Just start it
return s, err
}

func (s *GRPCServer) OnStart() error {
s.QuitService.OnStart()
ln, err := net.Listen(s.proto, s.addr)
if err != nil {
return err
}
s.listener = ln
grpcServer := grpc.NewServer()
RegisterBroadcastAPIServer(grpcServer, &broadcastAPI{})
go grpcServer.Serve(ln)
return nil
}

func (s *GRPCServer) OnStop() {
s.QuitService.OnStop()
s.listener.Close()
}

0 comments on commit 31b0ab6

Please sign in to comment.