From 73c7edd588cd2661e18b86bcb1778d1994563ef2 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 12 Jan 2017 00:07:56 -0500 Subject: [PATCH] runs in-tendermint --- Makefile | 11 +++- cmd/basecoin/main.go | 125 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 127 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 2974e951a2..893fe21c04 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,13 @@ all: test install -install: get_deps +NOVENDOR = go list github.com/tendermint/basecoin/... | grep -v /vendor/ + +install: go install github.com/tendermint/basecoin/cmd/... test: - go test github.com/tendermint/basecoin/... + go test --race `${NOVENDOR}` go run tests/tmsp/*.go get_deps: @@ -14,3 +16,8 @@ get_deps: update_deps: go get -d -u github.com/tendermint/basecoin/... + +get_vendor_deps: + go get github.com/Masterminds/glide + glide install + diff --git a/cmd/basecoin/main.go b/cmd/basecoin/main.go index 963443eaec..9e81cc53ff 100644 --- a/cmd/basecoin/main.go +++ b/cmd/basecoin/main.go @@ -4,17 +4,41 @@ import ( "encoding/json" "flag" "fmt" + "net" + "net/http" "reflect" + "strings" "github.com/tendermint/basecoin/app" . "github.com/tendermint/go-common" + cfg "github.com/tendermint/go-config" + "github.com/tendermint/go-logger" + "github.com/tendermint/go-p2p" + rpcserver "github.com/tendermint/go-rpc/server" eyes "github.com/tendermint/merkleeyes/client" - "github.com/tendermint/tmsp/server" + tmcfg "github.com/tendermint/tendermint/config/tendermint" + "github.com/tendermint/tendermint/node" + "github.com/tendermint/tendermint/proxy" + rpccore "github.com/tendermint/tendermint/rpc/core" + tmtypes "github.com/tendermint/tendermint/types" ) +var safeRoutePoints = []string{ + "status", + "genesis", + "block", + "blockchain", + "validators", + "dump_consensus_state", + "broadcast_tx_sync", + "num_unconfirmed_txs", +} + func main() { - addrPtr := flag.String("address", "tcp://0.0.0.0:46658", "Listen address") + //////////////////////////////////// + // Load Basecoin + eyesPtr := flag.String("eyes", "local", "MerkleEyes address, or 'local' for embedded") genFilePath := flag.String("genesis", "", "Genesis file, if any") flag.Parse() @@ -37,20 +61,107 @@ func main() { } } - // Start the listener - svr, err := server.NewServer(*addrPtr, "socket", app) - if err != nil { - Exit("create listener: " + err.Error()) + //////////////////////////////////////////// + // Create & start tendermint node + + var config cfg.Config + config = tmcfg.GetConfig("") + + privValidatorFile := config.GetString("priv_validator_file") + privValidator := tmtypes.LoadOrGenPrivValidator(privValidatorFile) + n := node.NewNode(config, privValidator, proxy.NewLocalClientCreator(app)) + + protocol, address := node.ProtocolAndAddress(config.GetString("node_laddr")) + l := p2p.NewDefaultListener(protocol, address, config.GetBool("skip_upnp")) + n.AddListener(l) + if err := n.Start(); err != nil { + Exit(Fmt("Failed to start node: %v", err)) } + var log = logger.New("module", "main") + log.Notice("Started node", "nodeInfo", n.NodeInfo()) + + // If seedNode is provided by config, dial out. + if config.GetString("seeds") != "" { + seeds := strings.Split(config.GetString("seeds"), ",") + n.DialSeeds(seeds) + } + + // Run the tendermint RPC server. + if config.GetString("rpc_laddr") != "" { + _, err := StartRPC(n, config) + if err != nil { + PanicCrisis(err) + } + } + + // Start the listener + //svr, err := server.NewServer(*addrPtr, "socket", app) + //if err != nil { + // Exit("create listener: " + err.Error()) + //} + // Wait forever TrapSignal(func() { // Cleanup - svr.Stop() + n.Stop() }) } +// Only expose safe components to the internet +// Expose the rest to localhost +func StartRPC(n *node.Node, config cfg.Config) ([]net.Listener, error) { + rpccore.SetConfig(config) + rpccore.SetEventSwitch(n.EventSwitch()) + rpccore.SetBlockStore(n.BlockStore()) + rpccore.SetConsensusState(n.ConsensusState()) + rpccore.SetMempool(n.MempoolReactor().Mempool) + rpccore.SetSwitch(n.Switch()) + //rpccore.SetPrivValidator(n.PrivValidator()) + rpccore.SetGenesisDoc(n.GenesisDoc()) + rpccore.SetProxyAppQuery(n.ProxyApp().Query()) + + safeRoutes := make(map[string]*rpcserver.RPCFunc) + for _, k := range safeRoutePoints { + route, ok := rpccore.Routes[k] + if !ok { + PanicSanity(k) + } + safeRoutes[k] = route + } + + var listeners []net.Listener + + listenAddrs := strings.Split(config.GetString("rpc_laddr"), ",") + listenAddrSafe := listenAddrs[0] + + // the first listener is the public safe rpc + mux := http.NewServeMux() + rpcserver.RegisterRPCFuncs(mux, safeRoutes) + listener, err := rpcserver.StartHTTPServer(listenAddrSafe, mux) + if err != nil { + return nil, err + } + listeners = append(listeners, listener) + + if len(listenAddrs) > 1 { + listenAddrUnsafe := listenAddrs[1] + // expose the full rpc + mux := http.NewServeMux() + wm := rpcserver.NewWebsocketManager(rpccore.Routes, n.EventSwitch()) + mux.HandleFunc("/websocket", wm.WebsocketHandler) + rpcserver.RegisterRPCFuncs(mux, rpccore.Routes) + listener, err := rpcserver.StartHTTPServer(listenAddrUnsafe, mux) + if err != nil { + return nil, err + } + listeners = append(listeners, listener) + } + + return listeners, nil +} + //---------------------------------------- type KeyValue struct {