From fc6611b2d9db1359384dec9c50d1c97ada4cf255 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Wed, 24 May 2017 13:56:12 -0400 Subject: [PATCH] [config] RPCConfig --- cmd/tendermint/commands/run_node.go | 7 ++-- config/config.go | 65 +++++++++++++++++++++++------ config/toml.go | 10 +++-- node/node.go | 8 ++-- 4 files changed, 67 insertions(+), 23 deletions(-) diff --git a/cmd/tendermint/commands/run_node.go b/cmd/tendermint/commands/run_node.go index 5e38f2b5f..d476ccd57 100644 --- a/cmd/tendermint/commands/run_node.go +++ b/cmd/tendermint/commands/run_node.go @@ -30,15 +30,14 @@ func init() { runNodeCmd.Flags().String("abci", config.ABCI, "Specify abci transport (socket | grpc)") // rpc flags - runNodeCmd.Flags().String("rpc_laddr", config.RPCListenAddress, "RPC listen address. Port required") - runNodeCmd.Flags().String("grpc_laddr", config.GRPCListenAddress, "GRPC listen address (BroadcastTx only). Port required") + runNodeCmd.Flags().String("rpc.laddr", config.RPC.ListenAddress, "RPC listen address. Port required") + runNodeCmd.Flags().String("rpc.grpc_laddr", config.RPC.GRPCListenAddress, "GRPC listen address (BroadcastTx only). Port required") + runNodeCmd.Flags().Bool("rpc.unsafe", config.RPC.Unsafe, "Enabled unsafe rpc methods") // p2p flags runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)") runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes") runNodeCmd.Flags().Bool("p2p.skip_upnp", config.P2P.SkipUPNP, "Skip UPNP configuration") - - // feature flags runNodeCmd.Flags().Bool("p2p.pex", config.P2P.PexReactor, "Enable Peer-Exchange (dev feature)") RootCmd.AddCommand(runNodeCmd) diff --git a/config/config.go b/config/config.go index 1da45abab..eafea77e9 100644 --- a/config/config.go +++ b/config/config.go @@ -12,6 +12,7 @@ type Config struct { BaseConfig `mapstructure:",squash"` // Options for services + RPC *RPCConfig `mapstructure:"rpc"` P2P *P2PConfig `mapstructure:"p2p"` Mempool *MempoolConfig `mapstructure:"mempool"` Consensus *ConsensusConfig `mapstructure:"consensus"` @@ -20,6 +21,7 @@ type Config struct { func DefaultConfig() *Config { return &Config{ BaseConfig: DefaultBaseConfig(), + RPC: DefaultRPCConfig(), P2P: DefaultP2PConfig(), Mempool: DefaultMempoolConfig(), Consensus: DefaultConsensusConfig(), @@ -29,6 +31,7 @@ func DefaultConfig() *Config { func TestConfig() *Config { return &Config{ BaseConfig: TestBaseConfig(), + RPC: TestRPCConfig(), P2P: TestP2PConfig(), Mempool: DefaultMempoolConfig(), Consensus: TestConsensusConfig(), @@ -38,12 +41,16 @@ func TestConfig() *Config { // Set the RootDir for all Config structs func (cfg *Config) SetRoot(root string) *Config { cfg.BaseConfig.RootDir = root + cfg.RPC.RootDir = root cfg.P2P.RootDir = root cfg.Mempool.RootDir = root cfg.Consensus.RootDir = root return cfg } +//----------------------------------------------------------------------------- +// BaseConfig + // BaseConfig struct for a Tendermint node type BaseConfig struct { // The root directory for all data. @@ -92,13 +99,6 @@ type BaseConfig struct { // Database directory DBPath string `mapstructure:"db_dir"` - - // TCP or UNIX socket address for the RPC server to listen on - RPCListenAddress string `mapstructure:"rpc_laddr"` - - // TCP or UNIX socket address for the gRPC server to listen on - // NOTE: This server only supports /broadcast_tx_commit - GRPCListenAddress string `mapstructure:"grpc_laddr"` } func DefaultBaseConfig() BaseConfig { @@ -108,15 +108,13 @@ func DefaultBaseConfig() BaseConfig { Moniker: "anonymous", ProxyApp: "tcp://127.0.0.1:46658", ABCI: "socket", - LogLevel: "info", + LogLevel: "state:info,*:error", ProfListenAddress: "", FastSync: true, FilterPeers: false, TxIndex: "kv", DBBackend: "leveldb", DBPath: "data", - RPCListenAddress: "tcp://0.0.0.0:46657", - GRPCListenAddress: "", } } @@ -126,8 +124,6 @@ func TestBaseConfig() BaseConfig { conf.ProxyApp = "dummy" conf.FastSync = false conf.DBBackend = "memdb" - conf.RPCListenAddress = "tcp://0.0.0.0:36657" - conf.GRPCListenAddress = "tcp://0.0.0.0:36658" return conf } @@ -143,6 +139,42 @@ func (b BaseConfig) DBDir() string { return rootify(b.DBPath, b.RootDir) } +//----------------------------------------------------------------------------- +// RPCConfig + +type RPCConfig struct { + RootDir string `mapstructure:"home"` + + // TCP or UNIX socket address for the RPC server to listen on + ListenAddress string `mapstructure:"laddr"` + + // TCP or UNIX socket address for the gRPC server to listen on + // NOTE: This server only supports /broadcast_tx_commit + GRPCListenAddress string `mapstructure:"grpc_laddr"` + + // Activate unsafe RPC commands like /dial_seeds and /unsafe_flush_mempool + Unsafe bool `mapstructure:"unsafe"` +} + +func DefaultRPCConfig() *RPCConfig { + return &RPCConfig{ + ListenAddress: "tcp://0.0.0.0:46657", + GRPCListenAddress: "", + Unsafe: false, + } +} + +func TestRPCConfig() *RPCConfig { + conf := DefaultRPCConfig() + conf.ListenAddress = "tcp://0.0.0.0:36657" + conf.GRPCListenAddress = "tcp://0.0.0.0:36658" + conf.Unsafe = true + return conf +} + +//----------------------------------------------------------------------------- +// P2PConfig + type P2PConfig struct { RootDir string `mapstructure:"home"` ListenAddress string `mapstructure:"laddr"` @@ -174,6 +206,9 @@ func (p *P2PConfig) AddrBookFile() string { return rootify(p.AddrBook, p.RootDir) } +//----------------------------------------------------------------------------- +// MempoolConfig + type MempoolConfig struct { RootDir string `mapstructure:"home"` Recheck bool `mapstructure:"recheck"` @@ -195,6 +230,9 @@ func (m *MempoolConfig) WalDir() string { return rootify(m.WalPath, m.RootDir) } +//----------------------------------------------------------------------------- +// ConsensusConfig + // ConsensusConfig holds timeouts and details about the WAL, the block structure, // and timeouts in the consensus protocol. type ConsensusConfig struct { @@ -286,6 +324,9 @@ func (c *ConsensusConfig) SetWalFile(walFile string) { c.walFile = walFile } +//----------------------------------------------------------------------------- +// Utils + // helper function to make config creation independent of root dir func rootify(path, root string) string { if filepath.IsAbs(path) { diff --git a/config/toml.go b/config/toml.go index 4e2e99aa1..999a05946 100644 --- a/config/toml.go +++ b/config/toml.go @@ -32,8 +32,10 @@ proxy_app = "tcp://127.0.0.1:46658" moniker = "__MONIKER__" fast_sync = true db_backend = "leveldb" -log_level = "info" -rpc_laddr = "tcp://0.0.0.0:46657" +log_level = "state:info,*:error" + +[rpc] +laddr = "tcp://0.0.0.0:46657" [p2p] laddr = "tcp://0.0.0.0:46656" @@ -94,7 +96,9 @@ moniker = "__MONIKER__" fast_sync = false db_backend = "memdb" log_level = "info" -rpc_laddr = "tcp://0.0.0.0:36657" + +[rpc] +laddr = "tcp://0.0.0.0:36657" [p2p] laddr = "tcp://0.0.0.0:36656" diff --git a/node/node.go b/node/node.go index df052fefa..516772a6d 100644 --- a/node/node.go +++ b/node/node.go @@ -255,7 +255,7 @@ func (n *Node) OnStart() error { } // Run the RPC server - if n.config.RPCListenAddress != "" { + if n.config.RPC.ListenAddress != "" { listeners, err := n.startRPC() if err != nil { return err @@ -320,7 +320,7 @@ func (n *Node) ConfigureRPC() { func (n *Node) startRPC() ([]net.Listener, error) { n.ConfigureRPC() - listenAddrs := strings.Split(n.config.RPCListenAddress, ",") + listenAddrs := strings.Split(n.config.RPC.ListenAddress, ",") // we may expose the rpc over both a unix and tcp socket listeners := make([]net.Listener, len(listenAddrs)) @@ -339,7 +339,7 @@ func (n *Node) startRPC() ([]net.Listener, error) { } // we expose a simplified api over grpc for convenience to app devs - grpcListenAddr := n.config.GRPCListenAddress + grpcListenAddr := n.config.RPC.GRPCListenAddress if grpcListenAddr != "" { listener, err := grpccore.StartGRPCServer(grpcListenAddr) if err != nil { @@ -421,7 +421,7 @@ func (n *Node) makeNodeInfo() *p2p.NodeInfo { p2pListener := n.sw.Listeners()[0] p2pHost := p2pListener.ExternalAddress().IP.String() p2pPort := p2pListener.ExternalAddress().Port - rpcListenAddr := n.config.RPCListenAddress + rpcListenAddr := n.config.RPC.ListenAddress // We assume that the rpcListener has the same ExternalAddress. // This is probably true because both P2P and RPC listeners use UPnP,