From 6f0e09cd15adfed028ad6f3807d20e400e9abb1d Mon Sep 17 00:00:00 2001 From: Silas Davis <silas@erisindustries.com> Date: Wed, 22 Jun 2016 17:40:17 +0100 Subject: [PATCH] Try to comfort Ben --- core/core.go | 171 +++++++++++++++++++----------------- rpc/tendermint/core/pipe.go | 10 +-- rpc/tendermint/core/txs.go | 12 +-- 3 files changed, 101 insertions(+), 92 deletions(-) diff --git a/core/core.go b/core/core.go index f71559be..3ff05bea 100644 --- a/core/core.go +++ b/core/core.go @@ -17,53 +17,60 @@ package core import ( - "fmt" - - // TODO: [ben] swap out go-events with eris-db/event (currently unused) - events "github.com/tendermint/go-events" - - log "github.com/eris-ltd/eris-logger" - - config "github.com/eris-ltd/eris-db/config" - consensus "github.com/eris-ltd/eris-db/consensus" - definitions "github.com/eris-ltd/eris-db/definitions" - event "github.com/eris-ltd/eris-db/event" - manager "github.com/eris-ltd/eris-db/manager" - // rpc_v0 is carried over from Eris-DBv0.11 and before on port 1337 - rpc_v0 "github.com/eris-ltd/eris-db/rpc/v0" - // rpc_tendermint is carried over from Eris-DBv0.11 and before on port 46657 - // rpc_tendermint "github.com/eris-ltd/eris-db/rpc/tendermint" - server "github.com/eris-ltd/eris-db/server" + "fmt" + "net/http" + + // TODO: [ben] swap out go-events with eris-db/event (currently unused) + events "github.com/tendermint/go-events" + + log "github.com/eris-ltd/eris-logger" + + config "github.com/eris-ltd/eris-db/config" + consensus "github.com/eris-ltd/eris-db/consensus" + definitions "github.com/eris-ltd/eris-db/definitions" + event "github.com/eris-ltd/eris-db/event" + manager "github.com/eris-ltd/eris-db/manager" + erismint "github.com/eris-ltd/eris-db/manager/eris-mint" + rpccore "github.com/eris-ltd/eris-db/rpc/tendermint/core" + // rpc_v0 is carried over from Eris-DBv0.11 and before on port 1337 + rpc_v0 "github.com/eris-ltd/eris-db/rpc/v0" + // rpc_tendermint is carried over from Eris-DBv0.11 and before on port 46657 + // rpc_tendermint "github.com/eris-ltd/eris-db/rpc/tendermint" + server "github.com/eris-ltd/eris-db/server" + "github.com/tendermint/go-rpc/server" + "github.com/tendermint/tendermint/node" + "net" + "strings" ) // Core is the high-level structure type Core struct { - chainId string - evsw *events.EventSwitch - pipe definitions.Pipe + chainId string + evsw *events.EventSwitch + pipe definitions.Pipe } func NewCore(chainId string, consensusConfig *config.ModuleConfig, - managerConfig *config.ModuleConfig) (*Core, error) { - // start new event switch, TODO: [ben] replace with eris-db/event - evsw := events.NewEventSwitch() - evsw.Start() - - // start a new application pipe that will load an application manager - pipe, err := manager.NewApplicationPipe(managerConfig, evsw, - consensusConfig.Version) - if err != nil { - return nil, fmt.Errorf("Failed to load application pipe: %v", err) - } - log.Debug("Loaded pipe with application manager") - // pass the consensus engine into the pipe - consensus.LoadConsensusEngineInPipe(consensusConfig, pipe) - - return &Core{ - chainId: chainId, - evsw: evsw, - pipe: pipe, - }, nil + managerConfig *config.ModuleConfig) (*Core, error) { + // start new event switch, TODO: [ben] replace with eris-db/event + evsw := events.NewEventSwitch() + evsw.Start() + + // start a new application pipe that will load an application manager + pipe, err := manager.NewApplicationPipe(managerConfig, evsw, + consensusConfig.Version) + if err != nil { + return nil, fmt.Errorf("Failed to load application pipe: %v", err) + } + log.Debug("Loaded pipe with application manager") + // pass the consensus engine into the pipe + consensus.LoadConsensusEngineInPipe(consensusConfig, pipe) + + return &Core{ + chainId: chainId, + evsw: evsw, + pipe: pipe, + }, nil } //------------------------------------------------------------------------------ @@ -79,53 +86,55 @@ func NewCore(chainId string, consensusConfig *config.ModuleConfig, // from Eris-DB and Tendermint; This is a draft and will be overhauled. func (core *Core) NewGatewayV0(config *server.ServerConfig) (*server.ServeProcess, - error) { - codec := &rpc_v0.TCodec{} - eventSubscriptions := event.NewEventSubscriptions(core.pipe.Events()) - // The services. - tmwss := rpc_v0.NewErisDbWsService(codec, core.pipe) - tmjs := rpc_v0.NewErisDbJsonService(codec, core.pipe, eventSubscriptions) - // The servers. + error) { + codec := &rpc_v0.TCodec{} + eventSubscriptions := event.NewEventSubscriptions(core.pipe.Events()) + // The services. + tmwss := rpc_v0.NewErisDbWsService(codec, core.pipe) + tmjs := rpc_v0.NewErisDbJsonService(codec, core.pipe, eventSubscriptions) + // The servers. jsonServer := rpc_v0.NewJsonRpcServer(tmjs) restServer := rpc_v0.NewRestServer(codec, core.pipe, eventSubscriptions) wsServer := server.NewWebSocketServer(config.WebSocket.MaxWebSocketSessions, - tmwss) + tmwss) // Create a server process. proc, err := server.NewServeProcess(config, jsonServer, restServer, wsServer) - if err != nil { - return nil, fmt.Errorf("Failed to load gateway: %v", err) - } + if err != nil { + return nil, fmt.Errorf("Failed to load gateway: %v", err) + } return proc, nil } - -// func StartRPC(config cfg.Config, n *node.Node, edbApp *edbapp.ErisDBApp) ([]net.Listener, error) { -// rpccore.SetConfig(config) -// -// rpccore.SetErisDBApp(edbApp) -// rpccore.SetBlockStore(n.BlockStore()) -// rpccore.SetConsensusState(n.ConsensusState()) -// rpccore.SetConsensusReactor(n.ConsensusReactor()) -// rpccore.SetMempoolReactor(n.MempoolReactor()) -// rpccore.SetSwitch(n.Switch()) -// rpccore.SetPrivValidator(n.PrivValidator()) -// rpccore.SetGenDoc(LoadGenDoc(config.GetString("genesis_file"))) -// -// listenAddrs := strings.Split(config.GetString("rpc_laddr"), ",") -// -// // we may expose the rpc over both a unix and tcp socket -// listeners := make([]net.Listener, len(listenAddrs)) -// for i, listenAddr := range listenAddrs { -// mux := http.NewServeMux() -// wm := rpcserver.NewWebsocketManager(rpccore.Routes, n.EventSwitch()) -// mux.HandleFunc("/websocket", wm.WebsocketHandler) -// rpcserver.RegisterRPCFuncs(mux, rpccore.Routes) -// listener, err := rpcserver.StartHTTPServer(listenAddr, mux) -// if err != nil { -// return nil, err -// } -// listeners[i] = listener -// } -// return listeners, nil -// } +func StartRPC(config config.ModuleConfig, n *node.Node, erisMint *erismint.ErisMint) ([]net.Listener, error) { + rpccore.SetConfig(config) + + rpccore.SetErisMint(erisMint) + rpccore.SetBlockStore(n.BlockStore()) + rpccore.SetConsensusState(n.ConsensusState()) + rpccore.SetConsensusReactor(n.ConsensusReactor()) + rpccore.SetMempoolReactor(n.MempoolReactor()) + rpccore.SetSwitch(n.Switch()) + rpccore.SetPrivValidator(n.PrivValidator()) + // TODO: programming + //rpccore.SetGenDoc(LoadGenDoc(config.GetString("genesis_file"))) + + // TODO: also programming + //listenAddrs := strings.Split(config.GetString("rpc_laddr"), ",") + listenAddrs := strings.Split("127.0.0.1", ",") + + // we may expose the rpc over both a unix and tcp socket + listeners := make([]net.Listener, len(listenAddrs)) + for i, listenAddr := range listenAddrs { + mux := http.NewServeMux() + wm := rpcserver.NewWebsocketManager(rpccore.Routes, n.EventSwitch()) + mux.HandleFunc("/websocket", wm.WebsocketHandler) + rpcserver.RegisterRPCFuncs(mux, rpccore.Routes) + listener, err := rpcserver.StartHTTPServer(listenAddr, mux) + if err != nil { + return nil, err + } + listeners[i] = listener + } + return listeners, nil +} diff --git a/rpc/tendermint/core/pipe.go b/rpc/tendermint/core/pipe.go index 3e62f095..be78644b 100644 --- a/rpc/tendermint/core/pipe.go +++ b/rpc/tendermint/core/pipe.go @@ -5,11 +5,11 @@ import ( stypes "github.com/eris-ltd/eris-db/manager/eris-mint/state/types" bc "github.com/tendermint/tendermint/blockchain" + cfg "github.com/eris-ltd/eris-db/config" "github.com/tendermint/tendermint/consensus" mempl "github.com/tendermint/tendermint/mempool" tmtypes "github.com/tendermint/tendermint/types" - cfg "github.com/tendermint/go-config" "github.com/tendermint/go-p2p" ) @@ -22,14 +22,14 @@ var privValidator *tmtypes.PrivValidator var genDoc *stypes.GenesisDoc // cache the genesis structure var erisMint *erismint.ErisMint -var config cfg.Config = nil +var config cfg.ModuleConfig = cfg.ModuleConfig{} -func SetConfig(c cfg.Config) { +func SetConfig(c cfg.ModuleConfig) { config = c } -func SetErisDBApp(edbApp *erismint.ErisMint) { - erisMint = edbApp +func SetErisMint(em *erismint.ErisMint) { + erisMint = em } func SetBlockStore(bs *bc.BlockStore) { diff --git a/rpc/tendermint/core/txs.go b/rpc/tendermint/core/txs.go index ab9ed784..0111d1e7 100644 --- a/rpc/tendermint/core/txs.go +++ b/rpc/tendermint/core/txs.go @@ -93,27 +93,27 @@ func SignTx(tx txs.Tx, privAccounts []*acm.PrivAccount) (*ctypes.ResultSignTx, e sendTx := tx.(*txs.SendTx) for i, input := range sendTx.Inputs { input.PubKey = privAccounts[i].PubKey - input.Signature = privAccounts[i].Sign(config.GetString("erisdb.chain_id"), sendTx) + input.Signature = privAccounts[i].Sign(config.ChainId, sendTx) } case *txs.CallTx: callTx := tx.(*txs.CallTx) callTx.Input.PubKey = privAccounts[0].PubKey - callTx.Input.Signature = privAccounts[0].Sign(config.GetString("erisdb.chain_id"), callTx) + callTx.Input.Signature = privAccounts[0].Sign(config.ChainId, callTx) case *txs.BondTx: bondTx := tx.(*txs.BondTx) // the first privaccount corresponds to the BondTx pub key. // the rest to the inputs - bondTx.Signature = privAccounts[0].Sign(config.GetString("erisdb.chain_id"), bondTx).(crypto.SignatureEd25519) + bondTx.Signature = privAccounts[0].Sign(config.ChainId, bondTx).(crypto.SignatureEd25519) for i, input := range bondTx.Inputs { input.PubKey = privAccounts[i+1].PubKey - input.Signature = privAccounts[i+1].Sign(config.GetString("erisdb.chain_id"), bondTx) + input.Signature = privAccounts[i+1].Sign(config.ChainId, bondTx) } case *txs.UnbondTx: unbondTx := tx.(*txs.UnbondTx) - unbondTx.Signature = privAccounts[0].Sign(config.GetString("erisdb.chain_id"), unbondTx).(crypto.SignatureEd25519) + unbondTx.Signature = privAccounts[0].Sign(config.ChainId, unbondTx).(crypto.SignatureEd25519) case *txs.RebondTx: rebondTx := tx.(*txs.RebondTx) - rebondTx.Signature = privAccounts[0].Sign(config.GetString("erisdb.chain_id"), rebondTx).(crypto.SignatureEd25519) + rebondTx.Signature = privAccounts[0].Sign(config.ChainId, rebondTx).(crypto.SignatureEd25519) } return &ctypes.ResultSignTx{tx}, nil } -- GitLab