2015-04-07 11:44:25 -07:00
|
|
|
package node
|
2014-07-08 00:02:04 -07:00
|
|
|
|
|
|
|
import (
|
2015-06-14 15:18:17 -07:00
|
|
|
"bytes"
|
2015-10-20 17:39:03 -07:00
|
|
|
"io/ioutil"
|
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"
|
2015-05-06 10:50:57 -07:00
|
|
|
"time"
|
2014-07-08 15:33:26 -07:00
|
|
|
|
2015-10-22 17:39:06 -07:00
|
|
|
. "github.com/tendermint/go-common"
|
2016-05-08 15:00:58 -07:00
|
|
|
cfg "github.com/tendermint/go-config"
|
2015-11-01 11:34:08 -08:00
|
|
|
"github.com/tendermint/go-crypto"
|
2015-10-22 17:39:06 -07:00
|
|
|
dbm "github.com/tendermint/go-db"
|
2015-11-01 11:34:08 -08:00
|
|
|
"github.com/tendermint/go-p2p"
|
2016-01-12 13:54:27 -08:00
|
|
|
"github.com/tendermint/go-rpc"
|
|
|
|
"github.com/tendermint/go-rpc/server"
|
2015-11-01 11:34:08 -08:00
|
|
|
"github.com/tendermint/go-wire"
|
|
|
|
bc "github.com/tendermint/tendermint/blockchain"
|
|
|
|
"github.com/tendermint/tendermint/consensus"
|
2015-04-01 17:30:16 -07:00
|
|
|
mempl "github.com/tendermint/tendermint/mempool"
|
2015-12-01 20:12:01 -08:00
|
|
|
"github.com/tendermint/tendermint/proxy"
|
2016-01-20 10:12:42 -08:00
|
|
|
rpccore "github.com/tendermint/tendermint/rpc/core"
|
2016-06-21 10:19:49 -07:00
|
|
|
grpccore "github.com/tendermint/tendermint/rpc/grpc"
|
2015-04-01 17:30:16 -07:00
|
|
|
sm "github.com/tendermint/tendermint/state"
|
2015-04-20 15:29:01 -07:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2016-01-20 10:12:42 -08:00
|
|
|
"github.com/tendermint/tendermint/version"
|
2014-07-08 00:02:04 -07:00
|
|
|
)
|
|
|
|
|
2015-04-27 07:25:11 -07:00
|
|
|
import _ "net/http/pprof"
|
|
|
|
|
2014-07-09 18:33:44 -07:00
|
|
|
type Node struct {
|
2016-05-08 15:00:58 -07:00
|
|
|
config cfg.Config
|
2014-10-22 17:20:44 -07:00
|
|
|
sw *p2p.Switch
|
2016-10-09 23:58:13 -07:00
|
|
|
evsw types.EventSwitch
|
2015-03-25 00:15:18 -07:00
|
|
|
blockStore *bc.BlockStore
|
|
|
|
bcReactor *bc.BlockchainReactor
|
2015-01-14 20:34:53 -08:00
|
|
|
mempoolReactor *mempl.MempoolReactor
|
2015-01-11 14:27:46 -08:00
|
|
|
consensusState *consensus.ConsensusState
|
2014-10-22 17:20:44 -07:00
|
|
|
consensusReactor *consensus.ConsensusReactor
|
2015-08-10 20:38:45 -07:00
|
|
|
privValidator *types.PrivValidator
|
2015-12-01 20:12:01 -08:00
|
|
|
genesisDoc *types.GenesisDoc
|
2015-11-01 11:34:08 -08:00
|
|
|
privKey crypto.PrivKeyEd25519
|
2016-08-22 13:00:08 -07:00
|
|
|
proxyApp proxy.AppConns
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
2014-07-08 00:02:04 -07:00
|
|
|
|
2016-09-09 20:55:24 -07:00
|
|
|
func NewNodeDefault(config cfg.Config) *Node {
|
|
|
|
// Get PrivValidator
|
|
|
|
privValidatorFile := config.GetString("priv_validator_file")
|
|
|
|
privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
|
2016-09-10 14:14:55 -07:00
|
|
|
return NewNode(config, privValidator, proxy.DefaultClientCreator(config))
|
2016-09-09 20:55:24 -07:00
|
|
|
}
|
|
|
|
|
2016-09-10 14:14:55 -07:00
|
|
|
func NewNode(config cfg.Config, privValidator *types.PrivValidator, clientCreator proxy.ClientCreator) *Node {
|
2016-04-13 15:23:25 -07:00
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Get BlockStore
|
2016-05-08 15:00:58 -07:00
|
|
|
blockStoreDB := dbm.NewDB("blockstore", config.GetString("db_backend"), config.GetString("db_dir"))
|
2015-03-25 00:15:18 -07:00
|
|
|
blockStore := bc.NewBlockStore(blockStoreDB)
|
2014-10-22 17:20:44 -07:00
|
|
|
|
2016-05-08 15:00:58 -07:00
|
|
|
// Get State db
|
|
|
|
stateDB := dbm.NewDB("state", config.GetString("db_backend"), config.GetString("db_dir"))
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Get State
|
2016-08-24 21:18:03 -07:00
|
|
|
state := sm.GetState(config, stateDB)
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2016-08-23 18:44:07 -07:00
|
|
|
// Create the proxyApp, which manages connections (consensus, mempool, query)
|
2016-11-16 13:13:17 -08:00
|
|
|
proxyApp := proxy.NewAppConns(config, clientCreator, sm.NewHandshaker(config, state, blockStore))
|
2016-09-11 13:02:29 -07:00
|
|
|
if _, err := proxyApp.Start(); err != nil {
|
|
|
|
Exit(Fmt("Error starting proxy app connections: %v", err))
|
|
|
|
}
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2016-03-01 13:04:19 -08:00
|
|
|
// add the chainid and number of validators to the global config
|
2015-05-29 11:13:45 -07:00
|
|
|
config.Set("chain_id", state.ChainID)
|
2016-03-01 13:04:19 -08:00
|
|
|
config.Set("num_vals", state.Validators.Size())
|
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-09 23:58:13 -07:00
|
|
|
eventSwitch := types.NewEventSwitch()
|
2015-08-04 19:04:00 -07:00
|
|
|
_, err := eventSwitch.Start()
|
|
|
|
if err != nil {
|
|
|
|
Exit(Fmt("Failed to start switch: %v", err))
|
|
|
|
}
|
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.
|
|
|
|
fastSync := config.GetBool("fast_sync")
|
|
|
|
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
|
2016-09-16 09:20:07 -07:00
|
|
|
bcReactor := bc.NewBlockchainReactor(config, state.Copy(), proxyApp.Consensus(), blockStore, fastSync)
|
2015-03-25 00:15:18 -07:00
|
|
|
|
2015-07-20 14:40:41 -07:00
|
|
|
// Make MempoolReactor
|
2016-08-17 19:28:08 -07:00
|
|
|
mempool := mempl.NewMempool(config, proxyApp.Mempool())
|
2016-05-08 15:00:58 -07:00
|
|
|
mempoolReactor := mempl.NewMempoolReactor(config, mempool)
|
2014-10-22 17:20:44 -07:00
|
|
|
|
2015-07-20 14:40:41 -07:00
|
|
|
// Make ConsensusReactor
|
2016-08-17 19:28:08 -07:00
|
|
|
consensusState := consensus.NewConsensusState(config, state.Copy(), proxyApp.Consensus(), blockStore, mempool)
|
2014-10-24 14:37:12 -07:00
|
|
|
if privValidator != nil {
|
2016-11-16 17:52:08 -08:00
|
|
|
consensusState.SetPrivValidator(privValidator)
|
2014-10-24 14:37:12 -07:00
|
|
|
}
|
2016-11-16 17:52:08 -08:00
|
|
|
consensusReactor := consensus.NewConsensusReactor(consensusState, fastSync)
|
2014-10-22 17:20:44 -07:00
|
|
|
|
2015-07-20 14:40:41 -07:00
|
|
|
// Make p2p network switch
|
2016-05-11 20:24:26 -07:00
|
|
|
sw := p2p.NewSwitch(config.GetConfig("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-09-08 15:56:02 -07:00
|
|
|
// filter peers by addr or pubkey with a tmsp query.
|
|
|
|
// if the query return code is OK, add peer
|
|
|
|
// XXX: query format subject to change
|
|
|
|
if config.GetBool("filter_peers") {
|
2016-09-10 12:16:23 -07:00
|
|
|
// NOTE: addr is ip:port
|
2016-09-08 15:56:02 -07:00
|
|
|
sw.SetAddrFilter(func(addr net.Addr) error {
|
|
|
|
res := proxyApp.Query().QuerySync([]byte(Fmt("p2p/filter/addr/%s", addr.String())))
|
|
|
|
if res.IsOK() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
sw.SetPubKeyFilter(func(pubkey crypto.PubKeyEd25519) error {
|
|
|
|
res := proxyApp.Query().QuerySync([]byte(Fmt("p2p/filter/pubkey/%X", pubkey.Bytes())))
|
|
|
|
if res.IsOK() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
}
|
2016-08-25 11:17:15 -07:00
|
|
|
|
2015-04-02 17:35:27 -07:00
|
|
|
// add the event switch to all services
|
|
|
|
// they should all satisfy events.Eventable
|
2015-12-13 21:38:19 -08:00
|
|
|
SetEventSwitch(eventSwitch, bcReactor, mempoolReactor, consensusReactor)
|
2015-04-02 17:35:27 -07:00
|
|
|
|
2015-09-24 12:48:44 -07:00
|
|
|
// run the profile server
|
|
|
|
profileHost := config.GetString("prof_laddr")
|
|
|
|
if profileHost != "" {
|
2016-08-17 19:28:08 -07:00
|
|
|
|
2015-09-24 12:48:44 -07:00
|
|
|
go func() {
|
|
|
|
log.Warn("Profile server", "error", http.ListenAndServe(profileHost, nil))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:14:23 -07:00
|
|
|
return &Node{
|
2016-05-08 15:00:58 -07:00
|
|
|
config: config,
|
2014-10-22 17:20:44 -07:00
|
|
|
sw: sw,
|
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,
|
2014-10-24 14:37:12 -07:00
|
|
|
privValidator: privValidator,
|
2015-12-01 20:12:01 -08:00
|
|
|
genesisDoc: state.GenesisDoc,
|
2015-07-15 14:17:20 -07:00
|
|
|
privKey: privKey,
|
2016-08-22 13:00:08 -07:00
|
|
|
proxyApp: proxyApp,
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 15:29:01 -07:00
|
|
|
// Call Start() after adding the listeners.
|
2015-08-04 18:44:15 -07:00
|
|
|
func (n *Node) Start() error {
|
2016-05-08 15:00:58 -07:00
|
|
|
n.sw.SetNodeInfo(makeNodeInfo(n.config, n.sw, n.privKey))
|
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()
|
|
|
|
return err
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
|
|
|
|
2014-07-16 21:13:02 -07:00
|
|
|
func (n *Node) Stop() {
|
2015-07-19 14:49:13 -07:00
|
|
|
log.Notice("Stopping Node")
|
2014-07-16 21:13:02 -07:00
|
|
|
// TODO: gracefully disconnect from peers.
|
|
|
|
n.sw.Stop()
|
|
|
|
}
|
|
|
|
|
2015-04-02 17:35:27 -07:00
|
|
|
// Add the event switch to reactors, mempool, etc.
|
2016-10-09 23:58:13 -07:00
|
|
|
func SetEventSwitch(evsw types.EventSwitch, eventables ...types.Eventable) {
|
2015-04-02 17:35:27 -07:00
|
|
|
for _, e := range eventables {
|
2015-12-13 21:38:19 -08: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-07-19 14:49:13 -07:00
|
|
|
log.Notice(Fmt("Added %v", l))
|
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
|
|
|
|
2016-02-18 16:21:02 -08:00
|
|
|
func (n *Node) StartRPC() ([]net.Listener, error) {
|
2016-05-11 20:33:09 -07:00
|
|
|
rpccore.SetConfig(n.config)
|
|
|
|
|
2016-06-27 17:43:09 -07:00
|
|
|
rpccore.SetEventSwitch(n.evsw)
|
2016-01-20 10:12:42 -08:00
|
|
|
rpccore.SetBlockStore(n.blockStore)
|
|
|
|
rpccore.SetConsensusState(n.consensusState)
|
2016-10-14 18:36:42 -07:00
|
|
|
rpccore.SetMempool(n.mempoolReactor.Mempool)
|
2016-01-20 10:12:42 -08:00
|
|
|
rpccore.SetSwitch(n.sw)
|
2016-10-14 18:36:42 -07:00
|
|
|
rpccore.SetPubKey(n.privValidator.PubKey)
|
2016-01-20 10:12:42 -08:00
|
|
|
rpccore.SetGenesisDoc(n.genesisDoc)
|
2016-08-22 13:00:48 -07:00
|
|
|
rpccore.SetProxyAppQuery(n.proxyApp.Query())
|
2015-04-10 02:12:17 -07:00
|
|
|
|
2016-05-08 15:00:58 -07:00
|
|
|
listenAddrs := strings.Split(n.config.GetString("rpc_laddr"), ",")
|
2016-02-18 16:21:02 -08: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 10:19:49 -07:00
|
|
|
|
|
|
|
// we expose a simplified api over grpc for convenience to app devs
|
|
|
|
grpcListenAddr := n.config.GetString("grpc_laddr")
|
|
|
|
if grpcListenAddr != "" {
|
|
|
|
listener, err := grpccore.StartGRPCServer(grpcListenAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener)
|
|
|
|
}
|
|
|
|
|
2016-02-18 16:21:02 -08: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 07:27:22 -07: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 11:45:20 -07: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-09 23:58:13 -07:00
|
|
|
func (n *Node) EventSwitch() types.EventSwitch {
|
2015-04-17 13:18:50 -07:00
|
|
|
return n.evsw
|
|
|
|
}
|
|
|
|
|
2016-05-11 11:45:20 -07:00
|
|
|
// XXX: for convenience
|
|
|
|
func (n *Node) PrivValidator() *types.PrivValidator {
|
|
|
|
return n.privValidator
|
|
|
|
}
|
|
|
|
|
2016-10-14 18:36:42 -07:00
|
|
|
func (n *Node) GenesisDoc() *types.GenesisDoc {
|
|
|
|
return n.genesisDoc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) ProxyApp() proxy.AppConns {
|
|
|
|
return n.proxyApp
|
|
|
|
}
|
|
|
|
|
2016-05-08 15:00:58 -07:00
|
|
|
func makeNodeInfo(config cfg.Config, sw *p2p.Switch, privKey crypto.PrivKeyEd25519) *p2p.NodeInfo {
|
2015-07-15 14:17:20 -07:00
|
|
|
|
2015-11-01 11:34:08 -08:00
|
|
|
nodeInfo := &p2p.NodeInfo{
|
|
|
|
PubKey: privKey.PubKey().(crypto.PubKeyEd25519),
|
2015-05-15 18:05:09 -07:00
|
|
|
Moniker: config.GetString("moniker"),
|
2015-11-01 11:34:08 -08:00
|
|
|
Network: config.GetString("chain_id"),
|
2016-01-20 10:12:42 -08:00
|
|
|
Version: version.Version,
|
2015-11-01 11:34:08 -08:00
|
|
|
Other: []string{
|
2015-12-05 09:13:21 -08:00
|
|
|
Fmt("wire_version=%v", wire.Version),
|
2016-01-20 10:12:42 -08:00
|
|
|
Fmt("p2p_version=%v", p2p.Version),
|
2016-03-16 18:15:33 -07:00
|
|
|
Fmt("consensus_version=%v", consensus.Version),
|
2016-01-20 10:12:42 -08:00
|
|
|
Fmt("rpc_version=%v/%v", rpc.Version, rpccore.Version),
|
2015-09-15 16:11:45 -07:00
|
|
|
},
|
2015-04-20 15:29:01 -07:00
|
|
|
}
|
2015-07-10 08:50:58 -07:00
|
|
|
|
|
|
|
// include git hash in the nodeInfo if available
|
2015-10-11 18:18:53 -07:00
|
|
|
if rev, err := ReadFile(config.GetString("revision_file")); err == nil {
|
2015-11-01 11:34:08 -08:00
|
|
|
nodeInfo.Other = append(nodeInfo.Other, Fmt("revision=%v", string(rev)))
|
2015-07-10 08:50:58 -07:00
|
|
|
}
|
|
|
|
|
2015-04-20 15:29:01 -07:00
|
|
|
if !sw.IsListening() {
|
|
|
|
return nodeInfo
|
|
|
|
}
|
2015-07-10 08:50:58 -07:00
|
|
|
|
2015-04-20 15:29:01 -07:00
|
|
|
p2pListener := sw.Listeners()[0]
|
|
|
|
p2pHost := p2pListener.ExternalAddress().IP.String()
|
|
|
|
p2pPort := p2pListener.ExternalAddress().Port
|
2015-05-15 18:05:09 -07:00
|
|
|
rpcListenAddr := config.GetString("rpc_laddr")
|
2015-04-20 15:29:01 -07:00
|
|
|
|
|
|
|
// We assume that the rpcListener has the same ExternalAddress.
|
2015-07-10 08:39:49 -07:00
|
|
|
// This is probably true because both P2P and RPC listeners use UPnP,
|
|
|
|
// except of course if the rpc is only bound to localhost
|
2015-12-05 09:13:21 -08:00
|
|
|
nodeInfo.ListenAddr = Fmt("%v:%v", p2pHost, p2pPort)
|
|
|
|
nodeInfo.Other = append(nodeInfo.Other, Fmt("rpc_addr=%v", rpcListenAddr))
|
2015-04-20 15:29:01 -07:00
|
|
|
return nodeInfo
|
|
|
|
}
|
|
|
|
|
2015-04-01 14:52:25 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-09-09 20:55:24 -07:00
|
|
|
// Users wishing to:
|
|
|
|
// * use an external signer for their validators
|
|
|
|
// * supply an in-proc tmsp app
|
2015-11-04 16:09:43 -08:00
|
|
|
// should fork tendermint/tendermint and implement RunNode to
|
2016-09-09 20:55:24 -07:00
|
|
|
// call NewNode with their custom priv validator and/or custom
|
2016-09-10 14:14:55 -07:00
|
|
|
// proxy.ClientCreator interface
|
2016-05-08 15:00:58 -07:00
|
|
|
func RunNode(config cfg.Config) {
|
2015-10-18 07:20:05 -07:00
|
|
|
// Wait until the genesis doc becomes available
|
|
|
|
genDocFile := config.GetString("genesis_file")
|
|
|
|
if !FileExists(genDocFile) {
|
|
|
|
log.Notice(Fmt("Waiting for genesis file %v...", genDocFile))
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
2015-12-01 20:12:01 -08:00
|
|
|
if !FileExists(genDocFile) {
|
|
|
|
continue
|
2015-10-18 07:20:05 -07:00
|
|
|
}
|
2015-10-20 17:39:03 -07:00
|
|
|
jsonBlob, err := ioutil.ReadFile(genDocFile)
|
|
|
|
if err != nil {
|
|
|
|
Exit(Fmt("Couldn't read GenesisDoc file: %v", err))
|
|
|
|
}
|
2015-11-01 11:34:08 -08:00
|
|
|
genDoc := types.GenesisDocFromJSON(jsonBlob)
|
2015-10-20 17:39:03 -07:00
|
|
|
if genDoc.ChainID == "" {
|
|
|
|
PanicSanity(Fmt("Genesis doc %v must include non-empty chain_id", genDocFile))
|
|
|
|
}
|
|
|
|
config.Set("chain_id", genDoc.ChainID)
|
2015-10-18 07:20:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 14:52:31 -07:00
|
|
|
// Create & start node
|
2016-09-09 20:55:24 -07:00
|
|
|
n := NewNodeDefault(config)
|
2016-08-09 22:15:59 -07:00
|
|
|
|
|
|
|
protocol, address := ProtocolAndAddress(config.GetString("node_laddr"))
|
|
|
|
l := p2p.NewDefaultListener(protocol, address, config.GetBool("skip_upnp"))
|
2014-07-10 22:14:23 -07:00
|
|
|
n.AddListener(l)
|
2015-08-04 19:04:00 -07:00
|
|
|
err := n.Start()
|
|
|
|
if err != nil {
|
|
|
|
Exit(Fmt("Failed to start node: %v", err))
|
|
|
|
}
|
2014-07-09 18:33:44 -07:00
|
|
|
|
2015-07-19 14:15:45 -07:00
|
|
|
log.Notice("Started node", "nodeInfo", n.sw.NodeInfo())
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// If seedNode is provided by config, dial out.
|
2015-07-10 08:39:49 -07:00
|
|
|
if config.GetString("seeds") != "" {
|
2016-01-20 10:12:42 -08:00
|
|
|
seeds := strings.Split(config.GetString("seeds"), ",")
|
|
|
|
n.sw.DialSeeds(seeds)
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
2014-07-08 15:33:26 -07:00
|
|
|
|
2014-11-27 04:04:07 -08:00
|
|
|
// Run the RPC server.
|
2015-05-15 18:05:09 -07:00
|
|
|
if config.GetString("rpc_laddr") != "" {
|
2015-07-19 16:42:52 -07:00
|
|
|
_, err := n.StartRPC()
|
|
|
|
if err != nil {
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
2014-11-27 04:04:07 -08:00
|
|
|
}
|
|
|
|
|
2014-07-14 14:04:26 -07:00
|
|
|
// Sleep forever and then...
|
2015-04-08 11:35:17 -07:00
|
|
|
TrapSignal(func() {
|
2014-07-14 14:04:26 -07:00
|
|
|
n.Stop()
|
|
|
|
})
|
2014-07-08 15:33:26 -07:00
|
|
|
}
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2016-04-13 15:23:25 -07:00
|
|
|
func (n *Node) NodeInfo() *p2p.NodeInfo {
|
|
|
|
return n.sw.NodeInfo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) DialSeeds(seeds []string) {
|
|
|
|
n.sw.DialSeeds(seeds)
|
|
|
|
}
|
|
|
|
|
2016-01-18 11:10:05 -08:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// replay
|
|
|
|
|
|
|
|
// convenience for replay mode
|
2016-05-08 15:00:58 -07:00
|
|
|
func newConsensusState(config cfg.Config) *consensus.ConsensusState {
|
2015-12-22 12:23:22 -08:00
|
|
|
// Get BlockStore
|
2016-05-08 15:00:58 -07:00
|
|
|
blockStoreDB := dbm.NewDB("blockstore", config.GetString("db_backend"), config.GetString("db_dir"))
|
2015-12-22 12:23:22 -08:00
|
|
|
blockStore := bc.NewBlockStore(blockStoreDB)
|
|
|
|
|
|
|
|
// Get State
|
2016-05-08 15:00:58 -07:00
|
|
|
stateDB := dbm.NewDB("state", config.GetString("db_backend"), config.GetString("db_dir"))
|
2015-12-22 12:23:22 -08:00
|
|
|
state := sm.MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file"))
|
|
|
|
|
2016-08-23 18:44:07 -07:00
|
|
|
// Create proxyAppConn connection (consensus, mempool, query)
|
2016-11-16 13:13:17 -08:00
|
|
|
proxyApp := proxy.NewAppConns(config, proxy.DefaultClientCreator(config), sm.NewHandshaker(config, state, blockStore))
|
2016-08-23 18:44:07 -07:00
|
|
|
_, err := proxyApp.Start()
|
|
|
|
if err != nil {
|
|
|
|
Exit(Fmt("Error starting proxy app conns: %v", err))
|
|
|
|
}
|
2015-12-22 12:23:22 -08:00
|
|
|
|
|
|
|
// add the chainid to the global config
|
|
|
|
config.Set("chain_id", state.ChainID)
|
|
|
|
|
|
|
|
// Make event switch
|
2016-10-09 23:58:13 -07:00
|
|
|
eventSwitch := types.NewEventSwitch()
|
2016-11-03 17:38:09 -07:00
|
|
|
if _, err := eventSwitch.Start(); err != nil {
|
2015-12-22 12:23:22 -08:00
|
|
|
Exit(Fmt("Failed to start event switch: %v", err))
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
2016-08-17 19:28:08 -07:00
|
|
|
mempool := mempl.NewMempool(config, proxyApp.Mempool())
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2016-08-17 19:28:08 -07:00
|
|
|
consensusState := consensus.NewConsensusState(config, state.Copy(), proxyApp.Consensus(), blockStore, mempool)
|
2015-12-22 12:23:22 -08:00
|
|
|
consensusState.SetEventSwitch(eventSwitch)
|
2015-12-22 22:27:40 -08:00
|
|
|
return consensusState
|
|
|
|
}
|
|
|
|
|
2016-10-30 03:55:27 -07:00
|
|
|
func RunReplayConsole(config cfg.Config, walFile string) {
|
2016-05-08 15:00:58 -07:00
|
|
|
consensusState := newConsensusState(config)
|
2015-12-22 22:27:40 -08:00
|
|
|
|
2016-01-18 11:10:05 -08:00
|
|
|
if err := consensusState.ReplayConsole(walFile); err != nil {
|
2015-12-22 22:27:40 -08:00
|
|
|
Exit(Fmt("Error during consensus replay: %v", err))
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
2015-12-22 22:27:40 -08:00
|
|
|
}
|
|
|
|
|
2016-10-30 03:55:27 -07:00
|
|
|
func RunReplay(config cfg.Config, walFile string) {
|
2016-05-08 15:00:58 -07:00
|
|
|
consensusState := newConsensusState(config)
|
2015-12-22 12:23:22 -08:00
|
|
|
|
2016-01-18 11:10:05 -08:00
|
|
|
if err := consensusState.ReplayMessages(walFile); err != nil {
|
2015-12-22 12:23:22 -08:00
|
|
|
Exit(Fmt("Error during consensus replay: %v", err))
|
|
|
|
}
|
|
|
|
log.Notice("Replay run successfully")
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
2016-08-09 22:15:59 -07: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
|
|
|
|
}
|