2015-04-07 11:44:25 -07:00
|
|
|
package node
|
2014-07-08 00:02:04 -07:00
|
|
|
|
|
|
|
import (
|
2015-06-14 18:18:17 -04:00
|
|
|
"bytes"
|
2017-01-28 08:27:13 -08:00
|
|
|
"errors"
|
2015-04-20 15:29:01 -07:00
|
|
|
"net"
|
2015-04-10 02:12:17 -07:00
|
|
|
"net/http"
|
2015-05-12 17:40:29 -07:00
|
|
|
"strings"
|
2014-07-08 15:33:26 -07:00
|
|
|
|
2017-04-08 22:04:06 -04:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2017-01-28 08:27:13 -08:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2017-02-22 14:53:59 +04:00
|
|
|
crypto "github.com/tendermint/go-crypto"
|
|
|
|
wire "github.com/tendermint/go-wire"
|
2015-11-01 11:34:08 -08:00
|
|
|
bc "github.com/tendermint/tendermint/blockchain"
|
2017-05-01 20:09:29 -04:00
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
|
|
|
// tmcfg "github.com/tendermint/tendermint/config/tendermint"
|
2015-11-01 11:34:08 -08:00
|
|
|
"github.com/tendermint/tendermint/consensus"
|
2015-04-01 17:30:16 -07:00
|
|
|
mempl "github.com/tendermint/tendermint/mempool"
|
2017-04-08 22:04:06 -04:00
|
|
|
p2p "github.com/tendermint/tendermint/p2p"
|
2015-12-01 20:12:01 -08:00
|
|
|
"github.com/tendermint/tendermint/proxy"
|
2017-04-26 19:57:33 -04:00
|
|
|
rpccore "github.com/tendermint/tendermint/rpc/core"
|
|
|
|
grpccore "github.com/tendermint/tendermint/rpc/grpc"
|
|
|
|
rpc "github.com/tendermint/tendermint/rpc/lib"
|
|
|
|
rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
|
2015-04-01 17:30:16 -07:00
|
|
|
sm "github.com/tendermint/tendermint/state"
|
2017-04-18 19:56:41 -04:00
|
|
|
"github.com/tendermint/tendermint/state/txindex"
|
|
|
|
"github.com/tendermint/tendermint/state/txindex/kv"
|
|
|
|
"github.com/tendermint/tendermint/state/txindex/null"
|
2015-04-20 15:29:01 -07:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2016-01-20 13:12:42 -05:00
|
|
|
"github.com/tendermint/tendermint/version"
|
2017-04-08 22:04:06 -04:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
2014-07-08 00:02:04 -07:00
|
|
|
|
2017-01-28 08:27:13 -08:00
|
|
|
_ "net/http/pprof"
|
|
|
|
)
|
2015-04-27 07:25:11 -07:00
|
|
|
|
2017-05-01 20:09:29 -04:00
|
|
|
type Config struct {
|
|
|
|
// Top level options use an anonymous struct
|
2017-05-01 22:14:37 -04:00
|
|
|
cfg.Config `mapstructure:",squash"`
|
2017-05-01 20:09:29 -04:00
|
|
|
|
|
|
|
// Options for services
|
2017-05-01 22:14:37 -04:00
|
|
|
P2P *p2p.Config `mapstructure:"p2p"`
|
|
|
|
Mempool *mempl.Config `mapstructure:"mempool"`
|
|
|
|
Consensus *consensus.Config `mapstructure:"consensus"`
|
2017-05-01 20:09:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultConfig(rootDir string) *Config {
|
|
|
|
return &Config{
|
2017-05-01 22:14:37 -04:00
|
|
|
Config: *cfg.NewDefaultConfig(rootDir),
|
2017-05-01 20:09:29 -04:00
|
|
|
P2P: p2p.NewDefaultConfig(rootDir),
|
|
|
|
Mempool: mempl.NewDefaultConfig(rootDir),
|
|
|
|
Consensus: consensus.NewDefaultConfig(rootDir),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 22:14:37 -04:00
|
|
|
func ConfigFromViper(viperConfig *viper.Viper) *Config {
|
|
|
|
tmConfig := new(Config)
|
|
|
|
if err := viperConfig.Unmarshal(tmConfig); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return tmConfig
|
|
|
|
}
|
|
|
|
|
2014-07-09 18:33:44 -07:00
|
|
|
type Node struct {
|
2017-01-15 16:59:10 -08:00
|
|
|
cmn.BaseService
|
|
|
|
|
2017-03-04 23:25:12 -05:00
|
|
|
// config
|
2017-05-01 20:09:29 -04:00
|
|
|
config *Config
|
2017-03-04 23:25:12 -05:00
|
|
|
genesisDoc *types.GenesisDoc // initial validator set
|
|
|
|
privValidator *types.PrivValidator // local node's validator key
|
|
|
|
|
|
|
|
// network
|
|
|
|
privKey crypto.PrivKeyEd25519 // local node's p2p key
|
|
|
|
sw *p2p.Switch // p2p connections
|
|
|
|
addrBook *p2p.AddrBook // known peers
|
|
|
|
|
|
|
|
// services
|
|
|
|
evsw types.EventSwitch // pub/sub for services
|
|
|
|
blockStore *bc.BlockStore // store the blockchain to disk
|
|
|
|
bcReactor *bc.BlockchainReactor // for fast-syncing
|
|
|
|
mempoolReactor *mempl.MempoolReactor // for gossipping transactions
|
|
|
|
consensusState *consensus.ConsensusState // latest consensus state
|
|
|
|
consensusReactor *consensus.ConsensusReactor // for participating in the consensus
|
|
|
|
proxyApp proxy.AppConns // connection to the application
|
|
|
|
rpcListeners []net.Listener // rpc servers
|
2017-04-18 19:56:41 -04:00
|
|
|
txIndexer txindex.TxIndexer
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
2014-07-08 00:02:04 -07:00
|
|
|
|
2017-05-01 20:09:29 -04:00
|
|
|
func NewNodeDefault(config *Config) *Node {
|
2016-09-09 23:55:24 -04:00
|
|
|
// Get PrivValidator
|
2017-05-01 20:09:29 -04:00
|
|
|
privValidator := types.LoadOrGenPrivValidator(config.PrivValidatorFile)
|
|
|
|
return NewNode(config, privValidator,
|
|
|
|
proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir))
|
2016-09-09 23:55:24 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 20:09:29 -04:00
|
|
|
func NewNode(config *Config, privValidator *types.PrivValidator, clientCreator proxy.ClientCreator) *Node {
|
2017-04-29 10:55:26 -04:00
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Get BlockStore
|
2017-05-01 20:09:29 -04:00
|
|
|
blockStoreDB := dbm.NewDB("blockstore", config.DBBackend, config.DBDir)
|
2015-03-25 00:15:18 -07:00
|
|
|
blockStore := bc.NewBlockStore(blockStoreDB)
|
2014-10-22 17:20:44 -07:00
|
|
|
|
|
|
|
// Get State
|
2017-05-01 20:09:29 -04:00
|
|
|
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir)
|
|
|
|
state := sm.GetState(stateDB, config.GenesisFile)
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2017-02-17 10:51:05 -05:00
|
|
|
// add the chainid and number of validators to the global config
|
2017-05-01 20:09:29 -04:00
|
|
|
// TODO: Set ChainID. eg:
|
|
|
|
// config.Consensus.SetChainID(state.ChainID) // ...
|
|
|
|
// but actually consensus doesnt need it since the cs has the state ...
|
2017-02-17 10:51:05 -05:00
|
|
|
|
2016-08-23 21:44:07 -04:00
|
|
|
// Create the proxyApp, which manages connections (consensus, mempool, query)
|
2017-02-17 10:51:05 -05:00
|
|
|
// and sync tendermint and the app by replaying any necessary blocks
|
2017-04-29 00:18:30 -04:00
|
|
|
proxyApp := proxy.NewAppConns(clientCreator, consensus.NewHandshaker(state, blockStore))
|
2016-09-11 16:02:29 -04:00
|
|
|
if _, err := proxyApp.Start(); err != nil {
|
2017-01-15 16:59:10 -08:00
|
|
|
cmn.Exit(cmn.Fmt("Error starting proxy app connections: %v", err))
|
2016-09-11 16:02:29 -04:00
|
|
|
}
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2017-02-17 10:51:05 -05:00
|
|
|
// reload the state (it may have been updated by the handshake)
|
2017-02-22 14:53:59 +04:00
|
|
|
state = sm.LoadState(stateDB)
|
|
|
|
|
|
|
|
// Transaction indexing
|
2017-04-18 19:56:41 -04:00
|
|
|
var txIndexer txindex.TxIndexer
|
2017-05-01 20:09:29 -04:00
|
|
|
switch config.TxIndex {
|
2017-02-22 14:53:59 +04:00
|
|
|
case "kv":
|
2017-05-01 20:09:29 -04:00
|
|
|
store := dbm.NewDB("tx_index", config.DBBackend, config.DBDir)
|
2017-04-18 19:56:41 -04:00
|
|
|
txIndexer = kv.NewTxIndex(store)
|
2017-02-22 14:53:59 +04:00
|
|
|
default:
|
2017-04-18 19:56:41 -04:00
|
|
|
txIndexer = &null.TxIndex{}
|
2017-02-22 14:53:59 +04:00
|
|
|
}
|
|
|
|
state.TxIndexer = txIndexer
|
2014-10-22 17:20:44 -07:00
|
|
|
|
2015-07-15 14:17:20 -07:00
|
|
|
// Generate node PrivKey
|
2015-11-01 11:34:08 -08:00
|
|
|
privKey := crypto.GenPrivKeyEd25519()
|
2015-07-15 14:17:20 -07:00
|
|
|
|
|
|
|
// Make event switch
|
2016-10-10 02:58:13 -04:00
|
|
|
eventSwitch := types.NewEventSwitch()
|
2015-08-04 19:04:00 -07:00
|
|
|
_, err := eventSwitch.Start()
|
|
|
|
if err != nil {
|
2017-01-15 16:59:10 -08:00
|
|
|
cmn.Exit(cmn.Fmt("Failed to start switch: %v", err))
|
2015-08-04 19:04:00 -07:00
|
|
|
}
|
2015-04-02 17:35:27 -07:00
|
|
|
|
2016-03-24 18:08:18 -07:00
|
|
|
// Decide whether to fast-sync or not
|
|
|
|
// We don't fast-sync when the only validator is us.
|
2017-05-01 20:09:29 -04:00
|
|
|
fastSync := config.FastSync
|
2016-03-24 18:08:18 -07:00
|
|
|
if state.Validators.Size() == 1 {
|
|
|
|
addr, _ := state.Validators.GetByIndex(0)
|
|
|
|
if bytes.Equal(privValidator.Address, addr) {
|
|
|
|
fastSync = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 14:40:41 -07:00
|
|
|
// Make BlockchainReactor
|
2017-04-28 23:50:24 -04:00
|
|
|
bcReactor := bc.NewBlockchainReactor(state.Copy(), proxyApp.Consensus(), blockStore, fastSync)
|
2015-03-25 00:15:18 -07:00
|
|
|
|
2015-07-20 14:40:41 -07:00
|
|
|
// Make MempoolReactor
|
2017-05-01 20:09:29 -04:00
|
|
|
mempool := mempl.NewMempool(config.Mempool, proxyApp.Mempool())
|
|
|
|
mempoolReactor := mempl.NewMempoolReactor(config.Mempool, mempool)
|
2014-10-22 17:20:44 -07:00
|
|
|
|
2015-07-20 14:40:41 -07:00
|
|
|
// Make ConsensusReactor
|
2017-05-01 20:09:29 -04:00
|
|
|
consensusState := consensus.NewConsensusState(config.Consensus, state.Copy(), proxyApp.Consensus(), blockStore, mempool)
|
2014-10-24 14:37:12 -07:00
|
|
|
if privValidator != nil {
|
2016-11-16 20:52:08 -05:00
|
|
|
consensusState.SetPrivValidator(privValidator)
|
2014-10-24 14:37:12 -07:00
|
|
|
}
|
2016-11-16 20:52:08 -05:00
|
|
|
consensusReactor := consensus.NewConsensusReactor(consensusState, fastSync)
|
2014-10-22 17:20:44 -07:00
|
|
|
|
2017-05-01 22:14:37 -04:00
|
|
|
sw := p2p.NewSwitch(config.P2P)
|
2015-03-30 22:28:34 -07:00
|
|
|
sw.AddReactor("MEMPOOL", mempoolReactor)
|
|
|
|
sw.AddReactor("BLOCKCHAIN", bcReactor)
|
|
|
|
sw.AddReactor("CONSENSUS", consensusReactor)
|
2014-07-08 00:02:04 -07:00
|
|
|
|
2016-11-30 22:58:47 -05:00
|
|
|
// Optionally, start the pex reactor
|
2017-03-04 23:25:12 -05:00
|
|
|
var addrBook *p2p.AddrBook
|
2017-05-01 20:09:29 -04:00
|
|
|
if config.P2P.PexReactor {
|
|
|
|
addrBook = p2p.NewAddrBook(config.P2P.AddrBookFile, config.P2P.AddrBookStrict)
|
2016-11-30 22:58:47 -05:00
|
|
|
pexReactor := p2p.NewPEXReactor(addrBook)
|
|
|
|
sw.AddReactor("PEX", pexReactor)
|
|
|
|
}
|
|
|
|
|
2017-01-28 08:27:13 -08:00
|
|
|
// Filter peers by addr or pubkey with an ABCI query.
|
|
|
|
// If the query return code is OK, add peer.
|
|
|
|
// XXX: Query format subject to change
|
2017-05-01 20:09:29 -04:00
|
|
|
if config.FilterPeers {
|
2016-09-10 15:16:23 -04:00
|
|
|
// NOTE: addr is ip:port
|
2016-09-08 18:56:02 -04:00
|
|
|
sw.SetAddrFilter(func(addr net.Addr) error {
|
2017-01-28 08:27:13 -08:00
|
|
|
resQuery, err := proxyApp.Query().QuerySync(abci.RequestQuery{Path: cmn.Fmt("/p2p/filter/addr/%s", addr.String())})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resQuery.Code.IsOK() {
|
2016-09-08 18:56:02 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-01-28 08:27:13 -08:00
|
|
|
return errors.New(resQuery.Code.String())
|
2016-09-08 18:56:02 -04:00
|
|
|
})
|
|
|
|
sw.SetPubKeyFilter(func(pubkey crypto.PubKeyEd25519) error {
|
2017-01-28 08:27:13 -08:00
|
|
|
resQuery, err := proxyApp.Query().QuerySync(abci.RequestQuery{Path: cmn.Fmt("/p2p/filter/pubkey/%X", pubkey.Bytes())})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resQuery.Code.IsOK() {
|
2016-09-08 18:56:02 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-01-28 08:27:13 -08:00
|
|
|
return errors.New(resQuery.Code.String())
|
2016-09-08 18:56:02 -04:00
|
|
|
})
|
|
|
|
}
|
2016-08-25 14:17:15 -04:00
|
|
|
|
2015-04-02 17:35:27 -07:00
|
|
|
// add the event switch to all services
|
|
|
|
// they should all satisfy events.Eventable
|
2015-12-14 00:38:19 -05:00
|
|
|
SetEventSwitch(eventSwitch, bcReactor, mempoolReactor, consensusReactor)
|
2015-04-02 17:35:27 -07:00
|
|
|
|
2015-09-24 15:48:44 -04:00
|
|
|
// run the profile server
|
2017-05-01 20:09:29 -04:00
|
|
|
profileHost := config.ProfListenAddress
|
2015-09-24 15:48:44 -04:00
|
|
|
if profileHost != "" {
|
2016-08-17 22:28:08 -04:00
|
|
|
|
2015-09-24 15:48:44 -04:00
|
|
|
go func() {
|
|
|
|
log.Warn("Profile server", "error", http.ListenAndServe(profileHost, nil))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-01-15 16:59:10 -08:00
|
|
|
node := &Node{
|
2017-03-04 23:25:12 -05:00
|
|
|
config: config,
|
|
|
|
genesisDoc: state.GenesisDoc,
|
|
|
|
privValidator: privValidator,
|
|
|
|
|
|
|
|
privKey: privKey,
|
|
|
|
sw: sw,
|
|
|
|
addrBook: addrBook,
|
|
|
|
|
2015-04-02 17:35:27 -07:00
|
|
|
evsw: eventSwitch,
|
2015-01-06 15:51:41 -08:00
|
|
|
blockStore: blockStore,
|
2015-03-25 00:15:18 -07:00
|
|
|
bcReactor: bcReactor,
|
2014-10-22 17:20:44 -07:00
|
|
|
mempoolReactor: mempoolReactor,
|
2015-01-11 14:27:46 -08:00
|
|
|
consensusState: consensusState,
|
2014-10-22 17:20:44 -07:00
|
|
|
consensusReactor: consensusReactor,
|
2016-08-22 16:00:08 -04:00
|
|
|
proxyApp: proxyApp,
|
2017-02-22 14:53:59 +04:00
|
|
|
txIndexer: txIndexer,
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
2017-01-15 16:59:10 -08:00
|
|
|
node.BaseService = *cmn.NewBaseService(log, "Node", node)
|
|
|
|
return node
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
|
|
|
|
2017-01-15 16:59:10 -08:00
|
|
|
func (n *Node) OnStart() error {
|
|
|
|
|
|
|
|
// Create & add listener
|
2017-05-01 20:09:29 -04:00
|
|
|
protocol, address := ProtocolAndAddress(n.config.P2P.ListenAddress)
|
|
|
|
l := p2p.NewDefaultListener(protocol, address, n.config.P2P.SkipUPNP)
|
2017-01-15 16:59:10 -08:00
|
|
|
n.sw.AddListener(l)
|
|
|
|
|
|
|
|
// Start the switch
|
2017-04-18 20:10:02 -04:00
|
|
|
n.sw.SetNodeInfo(n.makeNodeInfo())
|
2015-07-15 14:17:20 -07:00
|
|
|
n.sw.SetNodePrivKey(n.privKey)
|
2015-08-04 19:04:00 -07:00
|
|
|
_, err := n.sw.Start()
|
2017-01-15 16:59:10 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-04 23:25:12 -05:00
|
|
|
// If seeds exist, add them to the address book and dial out
|
2017-05-01 20:09:29 -04:00
|
|
|
if n.config.P2P.Seeds != "" {
|
2017-03-04 23:25:12 -05:00
|
|
|
// dial out
|
2017-05-01 20:09:29 -04:00
|
|
|
seeds := strings.Split(n.config.P2P.Seeds, ",")
|
2017-03-05 23:13:34 -05:00
|
|
|
if err := n.DialSeeds(seeds); err != nil {
|
2017-03-04 23:25:12 -05:00
|
|
|
return err
|
|
|
|
}
|
2017-01-15 16:59:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the RPC server
|
2017-05-01 20:09:29 -04:00
|
|
|
if n.config.RPCListenAddress != "" {
|
2017-03-03 19:28:17 -05:00
|
|
|
listeners, err := n.startRPC()
|
2017-01-15 16:59:10 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-03 19:28:17 -05:00
|
|
|
n.rpcListeners = listeners
|
2017-01-15 16:59:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
|
|
|
|
2017-01-15 16:59:10 -08:00
|
|
|
func (n *Node) OnStop() {
|
|
|
|
n.BaseService.OnStop()
|
|
|
|
|
2015-07-19 21:49:13 +00:00
|
|
|
log.Notice("Stopping Node")
|
2014-07-16 21:13:02 -07:00
|
|
|
// TODO: gracefully disconnect from peers.
|
|
|
|
n.sw.Stop()
|
2017-03-03 19:28:17 -05:00
|
|
|
|
|
|
|
for _, l := range n.rpcListeners {
|
|
|
|
log.Info("Closing rpc listener", "listener", l)
|
|
|
|
if err := l.Close(); err != nil {
|
|
|
|
log.Error("Error closing listener", "listener", l, "error", err)
|
|
|
|
}
|
|
|
|
}
|
2014-07-16 21:13:02 -07:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:13:34 -05:00
|
|
|
func (n *Node) RunForever() {
|
|
|
|
// Sleep forever and then...
|
|
|
|
cmn.TrapSignal(func() {
|
|
|
|
n.Stop()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-02 17:35:27 -07:00
|
|
|
// Add the event switch to reactors, mempool, etc.
|
2016-10-10 02:58:13 -04:00
|
|
|
func SetEventSwitch(evsw types.EventSwitch, eventables ...types.Eventable) {
|
2015-04-02 17:35:27 -07:00
|
|
|
for _, e := range eventables {
|
2015-12-14 00:38:19 -05:00
|
|
|
e.SetEventSwitch(evsw)
|
2015-04-02 17:35:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 14:06:12 -07:00
|
|
|
// Add a Listener to accept inbound peer connections.
|
2015-04-20 15:29:01 -07:00
|
|
|
// Add listeners before starting the Node.
|
|
|
|
// The first listener is the primary listener (in NodeInfo)
|
2014-07-10 22:14:23 -07:00
|
|
|
func (n *Node) AddListener(l p2p.Listener) {
|
2015-04-01 14:52:25 -07:00
|
|
|
n.sw.AddListener(l)
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
2014-07-08 00:02:04 -07:00
|
|
|
|
2017-02-22 14:41:10 +01:00
|
|
|
// ConfigureRPC sets all variables in rpccore so they will serve
|
|
|
|
// rpc calls from this node
|
|
|
|
func (n *Node) ConfigureRPC() {
|
2016-06-27 20:43:09 -04:00
|
|
|
rpccore.SetEventSwitch(n.evsw)
|
2016-01-20 13:12:42 -05:00
|
|
|
rpccore.SetBlockStore(n.blockStore)
|
|
|
|
rpccore.SetConsensusState(n.consensusState)
|
2016-10-14 21:36:42 -04:00
|
|
|
rpccore.SetMempool(n.mempoolReactor.Mempool)
|
2016-01-20 13:12:42 -05:00
|
|
|
rpccore.SetSwitch(n.sw)
|
2016-10-14 21:36:42 -04:00
|
|
|
rpccore.SetPubKey(n.privValidator.PubKey)
|
2016-01-20 13:12:42 -05:00
|
|
|
rpccore.SetGenesisDoc(n.genesisDoc)
|
2017-03-05 23:13:34 -05:00
|
|
|
rpccore.SetAddrBook(n.addrBook)
|
2016-08-22 16:00:48 -04:00
|
|
|
rpccore.SetProxyAppQuery(n.proxyApp.Query())
|
2017-02-22 14:53:59 +04:00
|
|
|
rpccore.SetTxIndexer(n.txIndexer)
|
2017-02-22 14:41:10 +01:00
|
|
|
}
|
2015-04-10 02:12:17 -07:00
|
|
|
|
2017-02-22 14:41:10 +01:00
|
|
|
func (n *Node) startRPC() ([]net.Listener, error) {
|
|
|
|
n.ConfigureRPC()
|
2017-05-01 20:09:29 -04:00
|
|
|
listenAddrs := strings.Split(n.config.RPCListenAddress, ",")
|
2016-02-19 00:21:02 +00:00
|
|
|
|
|
|
|
// 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.evsw)
|
|
|
|
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
|
|
|
|
}
|
2016-06-21 13:19:49 -04:00
|
|
|
|
|
|
|
// we expose a simplified api over grpc for convenience to app devs
|
2017-05-01 20:09:29 -04:00
|
|
|
grpcListenAddr := n.config.GRPCListenAddress
|
2016-06-21 13:19:49 -04:00
|
|
|
if grpcListenAddr != "" {
|
|
|
|
listener, err := grpccore.StartGRPCServer(grpcListenAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener)
|
|
|
|
}
|
|
|
|
|
2016-02-19 00:21:02 +00:00
|
|
|
return listeners, nil
|
2015-03-19 02:08:07 -07:00
|
|
|
}
|
|
|
|
|
2015-03-25 18:06:57 -07:00
|
|
|
func (n *Node) Switch() *p2p.Switch {
|
|
|
|
return n.sw
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:27:22 +02:00
|
|
|
func (n *Node) BlockStore() *bc.BlockStore {
|
|
|
|
return n.blockStore
|
|
|
|
}
|
|
|
|
|
2015-03-19 02:08:07 -07:00
|
|
|
func (n *Node) ConsensusState() *consensus.ConsensusState {
|
|
|
|
return n.consensusState
|
|
|
|
}
|
|
|
|
|
2016-05-11 14:45:20 -04:00
|
|
|
func (n *Node) ConsensusReactor() *consensus.ConsensusReactor {
|
|
|
|
return n.consensusReactor
|
|
|
|
}
|
|
|
|
|
2015-03-19 02:08:07 -07:00
|
|
|
func (n *Node) MempoolReactor() *mempl.MempoolReactor {
|
|
|
|
return n.mempoolReactor
|
|
|
|
}
|
|
|
|
|
2016-10-10 02:58:13 -04:00
|
|
|
func (n *Node) EventSwitch() types.EventSwitch {
|
2015-04-17 13:18:50 -07:00
|
|
|
return n.evsw
|
|
|
|
}
|
|
|
|
|
2016-05-11 14:45:20 -04:00
|
|
|
// XXX: for convenience
|
|
|
|
func (n *Node) PrivValidator() *types.PrivValidator {
|
|
|
|
return n.privValidator
|
|
|
|
}
|
|
|
|
|
2016-10-14 21:36:42 -04:00
|
|
|
func (n *Node) GenesisDoc() *types.GenesisDoc {
|
|
|
|
return n.genesisDoc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) ProxyApp() proxy.AppConns {
|
|
|
|
return n.proxyApp
|
|
|
|
}
|
|
|
|
|
2017-04-18 20:10:02 -04:00
|
|
|
func (n *Node) makeNodeInfo() *p2p.NodeInfo {
|
|
|
|
txIndexerStatus := "on"
|
|
|
|
if _, ok := n.txIndexer.(*null.TxIndex); ok {
|
|
|
|
txIndexerStatus = "off"
|
|
|
|
}
|
2015-07-15 14:17:20 -07:00
|
|
|
|
2015-11-01 11:34:08 -08:00
|
|
|
nodeInfo := &p2p.NodeInfo{
|
2017-03-21 22:46:15 +01:00
|
|
|
PubKey: n.privKey.PubKey().Unwrap().(crypto.PubKeyEd25519),
|
2017-05-01 20:09:29 -04:00
|
|
|
Moniker: n.config.Moniker,
|
|
|
|
Network: n.consensusState.GetState().ChainID,
|
2016-01-20 13:12:42 -05:00
|
|
|
Version: version.Version,
|
2015-11-01 11:34:08 -08:00
|
|
|
Other: []string{
|
2017-01-15 16:59:10 -08:00
|
|
|
cmn.Fmt("wire_version=%v", wire.Version),
|
|
|
|
cmn.Fmt("p2p_version=%v", p2p.Version),
|
|
|
|
cmn.Fmt("consensus_version=%v", consensus.Version),
|
|
|
|
cmn.Fmt("rpc_version=%v/%v", rpc.Version, rpccore.Version),
|
2017-04-18 20:55:40 -04:00
|
|
|
cmn.Fmt("tx_index=%v", txIndexerStatus),
|
2015-09-15 19:11:45 -04:00
|
|
|
},
|
2015-04-20 15:29:01 -07:00
|
|
|
}
|
2015-07-10 15:50:58 +00:00
|
|
|
|
|
|
|
// include git hash in the nodeInfo if available
|
2017-05-01 20:09:29 -04:00
|
|
|
// TODO: use ld-flags
|
|
|
|
/*if rev, err := cmn.ReadFile(n.config.GetString("revision_file")); err == nil {
|
2017-01-15 16:59:10 -08:00
|
|
|
nodeInfo.Other = append(nodeInfo.Other, cmn.Fmt("revision=%v", string(rev)))
|
2017-05-01 20:09:29 -04:00
|
|
|
}*/
|
2015-07-10 15:50:58 +00:00
|
|
|
|
2017-04-18 20:10:02 -04:00
|
|
|
if !n.sw.IsListening() {
|
2015-04-20 15:29:01 -07:00
|
|
|
return nodeInfo
|
|
|
|
}
|
2015-07-10 15:50:58 +00:00
|
|
|
|
2017-04-18 20:10:02 -04:00
|
|
|
p2pListener := n.sw.Listeners()[0]
|
2015-04-20 15:29:01 -07:00
|
|
|
p2pHost := p2pListener.ExternalAddress().IP.String()
|
|
|
|
p2pPort := p2pListener.ExternalAddress().Port
|
2017-05-01 20:09:29 -04:00
|
|
|
rpcListenAddr := n.config.RPCListenAddress
|
2015-04-20 15:29:01 -07:00
|
|
|
|
|
|
|
// We assume that the rpcListener has the same ExternalAddress.
|
2015-07-10 15:39:49 +00:00
|
|
|
// This is probably true because both P2P and RPC listeners use UPnP,
|
|
|
|
// except of course if the rpc is only bound to localhost
|
2017-01-15 16:59:10 -08:00
|
|
|
nodeInfo.ListenAddr = cmn.Fmt("%v:%v", p2pHost, p2pPort)
|
|
|
|
nodeInfo.Other = append(nodeInfo.Other, cmn.Fmt("rpc_addr=%v", rpcListenAddr))
|
2015-04-20 15:29:01 -07:00
|
|
|
return nodeInfo
|
|
|
|
}
|
|
|
|
|
2017-01-14 00:47:13 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-04-13 18:23:25 -04:00
|
|
|
func (n *Node) NodeInfo() *p2p.NodeInfo {
|
|
|
|
return n.sw.NodeInfo()
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:13:34 -05:00
|
|
|
func (n *Node) DialSeeds(seeds []string) error {
|
|
|
|
return n.sw.DialSeeds(n.addrBook, seeds)
|
2016-04-13 18:23:25 -04:00
|
|
|
}
|
|
|
|
|
2016-08-10 01:15:59 -04:00
|
|
|
// Defaults to tcp
|
|
|
|
func ProtocolAndAddress(listenAddr string) (string, string) {
|
|
|
|
protocol, address := "tcp", listenAddr
|
|
|
|
parts := strings.SplitN(address, "://", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
protocol, address = parts[0], parts[1]
|
|
|
|
}
|
|
|
|
return protocol, address
|
|
|
|
}
|
2017-04-28 23:59:02 -04:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|