2015-04-07 11:44:25 -07:00
|
|
|
package node
|
2014-07-08 00:02:04 -07:00
|
|
|
|
|
|
|
import (
|
2015-04-20 15:29:01 -07:00
|
|
|
"net"
|
2015-04-10 02:12:17 -07:00
|
|
|
"net/http"
|
2014-07-08 15:33:26 -07:00
|
|
|
"os"
|
2015-04-20 15:29:01 -07:00
|
|
|
"strconv"
|
2014-07-08 15:33:26 -07:00
|
|
|
|
2015-04-01 17:30:16 -07:00
|
|
|
bc "github.com/tendermint/tendermint/blockchain"
|
|
|
|
. "github.com/tendermint/tendermint/common"
|
|
|
|
"github.com/tendermint/tendermint/config"
|
|
|
|
"github.com/tendermint/tendermint/consensus"
|
|
|
|
dbm "github.com/tendermint/tendermint/db"
|
2015-04-02 17:35:27 -07:00
|
|
|
"github.com/tendermint/tendermint/events"
|
2015-04-01 17:30:16 -07:00
|
|
|
mempl "github.com/tendermint/tendermint/mempool"
|
|
|
|
"github.com/tendermint/tendermint/p2p"
|
|
|
|
"github.com/tendermint/tendermint/rpc"
|
|
|
|
"github.com/tendermint/tendermint/rpc/core"
|
|
|
|
sm "github.com/tendermint/tendermint/state"
|
2015-04-20 15:29:01 -07:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2014-07-08 00:02:04 -07:00
|
|
|
)
|
|
|
|
|
2014-07-09 18:33:44 -07:00
|
|
|
type Node struct {
|
2014-10-22 17:20:44 -07:00
|
|
|
sw *p2p.Switch
|
2015-04-02 17:35:27 -07:00
|
|
|
evsw *events.EventSwitch
|
2014-10-22 17:20:44 -07:00
|
|
|
book *p2p.AddrBook
|
2015-03-25 00:15:18 -07:00
|
|
|
blockStore *bc.BlockStore
|
2014-10-22 17:20:44 -07:00
|
|
|
pexReactor *p2p.PEXReactor
|
2015-03-25 00:15:18 -07:00
|
|
|
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-01-14 20:34:53 -08:00
|
|
|
privValidator *sm.PrivValidator
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
2014-07-08 00:02:04 -07:00
|
|
|
|
2014-07-09 18:33:44 -07:00
|
|
|
func NewNode() *Node {
|
2014-10-22 17:20:44 -07:00
|
|
|
// Get BlockStore
|
2015-01-14 20:34:53 -08:00
|
|
|
blockStoreDB := dbm.GetDB("blockstore")
|
2015-03-25 00:15:18 -07:00
|
|
|
blockStore := bc.NewBlockStore(blockStoreDB)
|
2014-10-22 17:20:44 -07:00
|
|
|
|
|
|
|
// Get State
|
2015-01-14 20:34:53 -08:00
|
|
|
stateDB := dbm.GetDB("state")
|
|
|
|
state := sm.LoadState(stateDB)
|
2014-10-22 17:20:44 -07:00
|
|
|
if state == nil {
|
2015-03-16 23:16:26 -07:00
|
|
|
state = sm.MakeGenesisStateFromFile(stateDB, config.App().GetString("GenesisFile"))
|
2014-10-25 14:27:53 -07:00
|
|
|
state.Save()
|
2014-10-22 17:20:44 -07:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
// Get PrivValidator
|
2015-01-14 20:34:53 -08:00
|
|
|
var privValidator *sm.PrivValidator
|
2015-04-18 14:46:44 -07:00
|
|
|
privValidatorFile := config.App().GetString("PrivValidatorFile")
|
|
|
|
if _, err := os.Stat(privValidatorFile); err == nil {
|
|
|
|
privValidator = sm.LoadPrivValidator(privValidatorFile)
|
|
|
|
log.Info("Loaded PrivValidator",
|
|
|
|
"file", privValidatorFile, "privValidator", privValidator)
|
2015-01-08 22:07:23 -08:00
|
|
|
} else {
|
2015-04-18 14:46:44 -07:00
|
|
|
privValidator = sm.GenPrivValidator()
|
|
|
|
privValidator.SetFile(privValidatorFile)
|
|
|
|
privValidator.Save()
|
|
|
|
log.Info("Generated PrivValidator", "file", privValidatorFile)
|
2014-10-24 14:37:12 -07:00
|
|
|
}
|
|
|
|
|
2015-04-02 17:35:27 -07:00
|
|
|
eventSwitch := new(events.EventSwitch)
|
|
|
|
eventSwitch.Start()
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Get PEXReactor
|
2015-03-16 23:16:26 -07:00
|
|
|
book := p2p.NewAddrBook(config.App().GetString("AddrBookFile"))
|
2014-10-22 17:20:44 -07:00
|
|
|
pexReactor := p2p.NewPEXReactor(book)
|
|
|
|
|
2015-03-25 00:15:18 -07:00
|
|
|
// Get BlockchainReactor
|
2015-03-26 00:35:16 -07:00
|
|
|
bcReactor := bc.NewBlockchainReactor(state, blockStore, config.App().GetBool("FastSync"))
|
2015-03-25 00:15:18 -07:00
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Get MempoolReactor
|
2015-01-14 20:34:53 -08:00
|
|
|
mempool := mempl.NewMempool(state.Copy())
|
|
|
|
mempoolReactor := mempl.NewMempoolReactor(mempool)
|
2014-10-22 17:20:44 -07:00
|
|
|
|
|
|
|
// Get ConsensusReactor
|
2015-01-11 14:27:46 -08:00
|
|
|
consensusState := consensus.NewConsensusState(state, blockStore, mempoolReactor)
|
|
|
|
consensusReactor := consensus.NewConsensusReactor(consensusState, blockStore)
|
2014-10-24 14:37:12 -07:00
|
|
|
if privValidator != nil {
|
|
|
|
consensusReactor.SetPrivValidator(privValidator)
|
|
|
|
}
|
2014-10-22 17:20:44 -07:00
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
// so the consensus reactor won't do anything until we're synced
|
|
|
|
if config.App().GetBool("FastSync") {
|
|
|
|
consensusReactor.SetSyncing(true)
|
|
|
|
}
|
|
|
|
|
2015-03-25 00:15:18 -07:00
|
|
|
sw := p2p.NewSwitch()
|
2015-03-30 22:28:34 -07:00
|
|
|
sw.AddReactor("PEX", pexReactor)
|
|
|
|
sw.AddReactor("MEMPOOL", mempoolReactor)
|
|
|
|
sw.AddReactor("BLOCKCHAIN", bcReactor)
|
|
|
|
sw.AddReactor("CONSENSUS", consensusReactor)
|
2014-07-08 00:02:04 -07:00
|
|
|
|
2015-04-02 17:35:27 -07:00
|
|
|
// add the event switch to all services
|
|
|
|
// they should all satisfy events.Eventable
|
2015-04-15 23:40:27 -07:00
|
|
|
SetFireable(eventSwitch, pexReactor, bcReactor, mempoolReactor, consensusReactor)
|
2015-04-02 17:35:27 -07:00
|
|
|
|
2014-07-10 22:14:23 -07:00
|
|
|
return &Node{
|
2014-10-22 17:20:44 -07:00
|
|
|
sw: sw,
|
2015-04-02 17:35:27 -07:00
|
|
|
evsw: eventSwitch,
|
2014-10-22 17:20:44 -07:00
|
|
|
book: book,
|
2015-01-06 15:51:41 -08:00
|
|
|
blockStore: blockStore,
|
2015-03-25 00:15:18 -07:00
|
|
|
pexReactor: pexReactor,
|
|
|
|
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,
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 15:29:01 -07:00
|
|
|
// Call Start() after adding the listeners.
|
2014-07-09 18:33:44 -07:00
|
|
|
func (n *Node) Start() {
|
2014-11-27 04:04:07 -08:00
|
|
|
log.Info("Starting Node")
|
2014-07-09 18:33:44 -07:00
|
|
|
n.book.Start()
|
2015-04-20 15:29:01 -07:00
|
|
|
nodeInfo := makeNodeInfo(n.sw)
|
|
|
|
n.sw.SetNodeInfo(nodeInfo)
|
2015-04-03 16:15:52 -07:00
|
|
|
n.sw.Start()
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
|
|
|
|
2014-07-16 21:13:02 -07:00
|
|
|
func (n *Node) Stop() {
|
2014-11-27 04:04:07 -08:00
|
|
|
log.Info("Stopping Node")
|
2014-07-16 21:13:02 -07:00
|
|
|
// TODO: gracefully disconnect from peers.
|
|
|
|
n.sw.Stop()
|
|
|
|
n.book.Stop()
|
|
|
|
}
|
|
|
|
|
2015-04-02 17:35:27 -07:00
|
|
|
// Add the event switch to reactors, mempool, etc.
|
2015-04-15 23:40:27 -07:00
|
|
|
func SetFireable(evsw *events.EventSwitch, eventables ...events.Eventable) {
|
2015-04-02 17:35:27 -07:00
|
|
|
for _, e := range eventables {
|
2015-04-15 23:40:27 -07:00
|
|
|
e.SetFireable(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) {
|
2014-12-29 18:09:06 -08:00
|
|
|
log.Info(Fmt("Added %v", l))
|
2015-04-01 14:52:25 -07:00
|
|
|
n.sw.AddListener(l)
|
2014-07-17 15:42:08 -07:00
|
|
|
n.book.AddOurAddress(l.ExternalAddress())
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
2014-07-08 00:02:04 -07:00
|
|
|
|
2015-03-19 02:08:07 -07:00
|
|
|
func (n *Node) DialSeed() {
|
|
|
|
addr := p2p.NewNetAddressString(config.App().GetString("SeedNode"))
|
|
|
|
peer, err := n.sw.DialPeerWithAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error dialing seed", "error", err)
|
|
|
|
//n.book.MarkAttempt(addr)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
log.Info("Connected to seed", "peer", peer)
|
|
|
|
n.book.AddAddress(addr, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 15:55:05 -07:00
|
|
|
func (n *Node) StartRPC() {
|
2015-03-29 18:06:55 -07:00
|
|
|
core.SetBlockStore(n.blockStore)
|
|
|
|
core.SetConsensusState(n.consensusState)
|
|
|
|
core.SetMempoolReactor(n.mempoolReactor)
|
|
|
|
core.SetSwitch(n.sw)
|
2015-04-10 02:12:17 -07:00
|
|
|
|
|
|
|
listenAddr := config.App().GetString("RPC.HTTP.ListenAddr")
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
rpc.RegisterEventsHandler(mux, n.evsw)
|
|
|
|
rpc.RegisterRPCFuncs(mux, core.Routes)
|
2015-04-13 13:14:37 -07:00
|
|
|
rpc.StartHTTPServer(listenAddr, mux)
|
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-03-19 02:08:07 -07:00
|
|
|
func (n *Node) ConsensusState() *consensus.ConsensusState {
|
|
|
|
return n.consensusState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) MempoolReactor() *mempl.MempoolReactor {
|
|
|
|
return n.mempoolReactor
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:18:50 -07:00
|
|
|
func (n *Node) EventSwitch() *events.EventSwitch {
|
|
|
|
return n.evsw
|
|
|
|
}
|
|
|
|
|
2015-04-20 15:29:01 -07:00
|
|
|
func makeNodeInfo(sw *p2p.Switch) *types.NodeInfo {
|
|
|
|
nodeInfo := &types.NodeInfo{
|
|
|
|
Moniker: config.App().GetString("Moniker"),
|
|
|
|
Network: config.App().GetString("Network"),
|
2015-04-22 13:50:37 -07:00
|
|
|
Version: "0.0.1",
|
2015-04-20 15:29:01 -07:00
|
|
|
}
|
|
|
|
if !sw.IsListening() {
|
|
|
|
return nodeInfo
|
|
|
|
}
|
|
|
|
p2pListener := sw.Listeners()[0]
|
|
|
|
p2pHost := p2pListener.ExternalAddress().IP.String()
|
|
|
|
p2pPort := p2pListener.ExternalAddress().Port
|
|
|
|
rpcListenAddr := config.App().GetString("RPC.HTTP.ListenAddr")
|
|
|
|
_, rpcPortStr, _ := net.SplitHostPort(rpcListenAddr)
|
|
|
|
rpcPort, err := strconv.Atoi(rpcPortStr)
|
|
|
|
if err != nil {
|
|
|
|
panic(Fmt("Expected numeric RPC.HTTP.ListenAddr port but got %v", rpcPortStr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// We assume that the rpcListener has the same ExternalAddress.
|
|
|
|
// This is probably true because both P2P and RPC listeners use UPnP.
|
|
|
|
nodeInfo.Host = p2pHost
|
|
|
|
nodeInfo.P2PPort = p2pPort
|
|
|
|
nodeInfo.RPCPort = uint16(rpcPort)
|
|
|
|
return nodeInfo
|
|
|
|
}
|
|
|
|
|
2015-04-01 14:52:25 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2015-04-07 11:44:25 -07:00
|
|
|
func RunNode() {
|
2014-07-12 14:52:31 -07:00
|
|
|
// Create & start node
|
2014-07-09 18:33:44 -07:00
|
|
|
n := NewNode()
|
2015-03-16 23:16:26 -07:00
|
|
|
l := p2p.NewDefaultListener("tcp", config.App().GetString("ListenAddr"), false)
|
2014-07-10 22:14:23 -07:00
|
|
|
n.AddListener(l)
|
|
|
|
n.Start()
|
2014-07-09 18:33:44 -07:00
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// If seedNode is provided by config, dial out.
|
2015-03-16 23:16:26 -07:00
|
|
|
if config.App().GetString("SeedNode") != "" {
|
2015-03-19 02:08:07 -07:00
|
|
|
n.DialSeed()
|
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-03-16 23:16:26 -07:00
|
|
|
if config.App().GetString("RPC.HTTP.ListenAddr") != "" {
|
2015-03-30 15:55:05 -07:00
|
|
|
n.StartRPC()
|
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
|
|
|
}
|