2014-07-08 00:02:04 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-07-08 15:33:26 -07:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
"github.com/tendermint/tendermint/blocks"
|
2014-07-08 15:33:26 -07:00
|
|
|
"github.com/tendermint/tendermint/config"
|
2014-10-22 17:20:44 -07:00
|
|
|
"github.com/tendermint/tendermint/consensus"
|
|
|
|
db_ "github.com/tendermint/tendermint/db"
|
|
|
|
mempool_ "github.com/tendermint/tendermint/mempool"
|
2014-07-08 00:02:04 -07:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2014-11-27 04:04:07 -08:00
|
|
|
"github.com/tendermint/tendermint/rpc"
|
2014-10-22 17:20:44 -07:00
|
|
|
state_ "github.com/tendermint/tendermint/state"
|
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
|
|
|
lz []p2p.Listener
|
|
|
|
sw *p2p.Switch
|
|
|
|
book *p2p.AddrBook
|
|
|
|
pexReactor *p2p.PEXReactor
|
|
|
|
mempoolReactor *mempool_.MempoolReactor
|
|
|
|
consensusReactor *consensus.ConsensusReactor
|
2014-10-24 14:37:12 -07:00
|
|
|
privValidator *consensus.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
|
|
|
|
blockStoreDB := db_.NewMemDB() // TODO configurable db.
|
|
|
|
blockStore := blocks.NewBlockStore(blockStoreDB)
|
|
|
|
|
|
|
|
// Get State
|
|
|
|
stateDB := db_.NewMemDB() // TODO configurable db.
|
|
|
|
state := state_.LoadState(stateDB)
|
|
|
|
if state == nil {
|
2014-12-09 18:49:04 -08:00
|
|
|
state = state_.GenesisStateFromFile(stateDB, config.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
|
2014-10-24 14:37:12 -07:00
|
|
|
var privValidator *consensus.PrivValidator
|
2014-12-09 18:49:04 -08:00
|
|
|
if _, err := os.Stat(config.PrivValidatorFile()); err == nil {
|
|
|
|
privValidator = consensus.LoadPrivValidator()
|
2014-10-24 14:37:12 -07:00
|
|
|
}
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Get PEXReactor
|
2014-12-09 18:49:04 -08:00
|
|
|
book := p2p.NewAddrBook(config.AddrBookFile())
|
2014-10-22 17:20:44 -07:00
|
|
|
pexReactor := p2p.NewPEXReactor(book)
|
|
|
|
|
|
|
|
// Get MempoolReactor
|
|
|
|
mempool := mempool_.NewMempool(state)
|
|
|
|
mempoolReactor := mempool_.NewMempoolReactor(mempool)
|
|
|
|
|
|
|
|
// Get ConsensusReactor
|
|
|
|
consensusReactor := consensus.NewConsensusReactor(blockStore, mempool, state)
|
2014-10-24 14:37:12 -07:00
|
|
|
if privValidator != nil {
|
|
|
|
consensusReactor.SetPrivValidator(privValidator)
|
|
|
|
}
|
2014-10-22 17:20:44 -07:00
|
|
|
|
|
|
|
sw := p2p.NewSwitch([]p2p.Reactor{pexReactor, mempoolReactor, consensusReactor})
|
2014-07-08 00:02:04 -07:00
|
|
|
|
2014-07-10 22:14:23 -07:00
|
|
|
return &Node{
|
2014-10-22 17:20:44 -07:00
|
|
|
sw: sw,
|
|
|
|
book: book,
|
|
|
|
pexReactor: pexReactor,
|
|
|
|
mempoolReactor: mempoolReactor,
|
|
|
|
consensusReactor: consensusReactor,
|
2014-10-24 14:37:12 -07:00
|
|
|
privValidator: privValidator,
|
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-14 14:04:26 -07:00
|
|
|
for _, l := range n.lz {
|
2014-08-10 16:35:08 -07:00
|
|
|
go n.inboundConnectionRoutine(l)
|
2014-07-14 14:04:26 -07:00
|
|
|
}
|
2014-07-09 18:33:44 -07:00
|
|
|
n.book.Start()
|
2014-10-22 17:20:44 -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()
|
|
|
|
}
|
|
|
|
|
2014-07-14 14:06:12 -07:00
|
|
|
// Add a Listener to accept inbound peer connections.
|
2014-07-10 22:14:23 -07:00
|
|
|
func (n *Node) AddListener(l p2p.Listener) {
|
2014-07-14 16:15:13 -07:00
|
|
|
log.Info("Added %v", l)
|
2014-07-14 14:04:26 -07:00
|
|
|
n.lz = append(n.lz, 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
|
|
|
|
2014-08-10 16:35:08 -07:00
|
|
|
func (n *Node) inboundConnectionRoutine(l p2p.Listener) {
|
2014-07-14 14:04:26 -07:00
|
|
|
for {
|
|
|
|
inConn, ok := <-l.Connections()
|
|
|
|
if !ok {
|
|
|
|
break
|
2014-07-10 22:14:23 -07:00
|
|
|
}
|
2014-07-14 14:06:12 -07:00
|
|
|
// New inbound connection!
|
2014-07-14 14:04:26 -07:00
|
|
|
peer, err := n.sw.AddPeerWithConnection(inConn, false)
|
|
|
|
if err != nil {
|
2014-07-14 16:15:13 -07:00
|
|
|
log.Info("Ignoring error from inbound connection: %v\n%v",
|
2014-07-14 14:04:26 -07:00
|
|
|
peer, err)
|
2014-07-10 22:14:23 -07:00
|
|
|
continue
|
|
|
|
}
|
2014-07-14 14:04:26 -07:00
|
|
|
// NOTE: We don't yet have the external address of the
|
|
|
|
// remote (if they have a listener at all).
|
2014-09-14 15:37:32 -07:00
|
|
|
// PEXReactor's pexRoutine will handle that.
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
2014-07-14 14:04:26 -07:00
|
|
|
|
|
|
|
// cleanup
|
2014-07-09 18:33:44 -07:00
|
|
|
}
|
2014-07-08 15:33:26 -07:00
|
|
|
|
2014-11-07 23:27:03 -08:00
|
|
|
func daemon() {
|
2014-10-07 00:43:34 -07:00
|
|
|
|
2014-07-12 14:52:31 -07:00
|
|
|
// Create & start node
|
2014-07-09 18:33:44 -07:00
|
|
|
n := NewNode()
|
2014-07-12 18:37:55 -07:00
|
|
|
l := p2p.NewDefaultListener("tcp", config.Config.LAddr)
|
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.
|
|
|
|
if config.Config.SeedNode != "" {
|
|
|
|
peer, err := n.sw.DialPeerWithAddress(p2p.NewNetAddressString(config.Config.SeedNode))
|
2014-07-09 18:33:44 -07:00
|
|
|
if err != nil {
|
2014-07-14 16:15:13 -07:00
|
|
|
log.Error("Error dialing seed: %v", err)
|
2014-07-14 14:04:26 -07:00
|
|
|
//n.book.MarkAttempt(addr)
|
2014-07-09 18:33:44 -07:00
|
|
|
return
|
2014-07-14 14:04:26 -07:00
|
|
|
} else {
|
2014-07-14 16:15:13 -07:00
|
|
|
log.Info("Connected to seed: %v", peer)
|
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.
|
|
|
|
if config.Config.RPC.HTTPPort != 0 {
|
|
|
|
rpc.StartHTTPServer()
|
|
|
|
}
|
|
|
|
|
2014-07-14 14:04:26 -07:00
|
|
|
// Sleep forever and then...
|
|
|
|
trapSignal(func() {
|
|
|
|
n.Stop()
|
|
|
|
})
|
2014-07-08 15:33:26 -07:00
|
|
|
}
|
|
|
|
|
2014-07-14 14:04:26 -07:00
|
|
|
func trapSignal(cb func()) {
|
2014-07-08 15:33:26 -07:00
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
for sig := range c {
|
2014-07-14 16:15:13 -07:00
|
|
|
log.Info("captured %v, exiting..", sig)
|
2014-07-14 14:04:26 -07:00
|
|
|
cb()
|
2014-07-08 15:33:26 -07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
2014-07-14 14:04:26 -07:00
|
|
|
select {}
|
2014-07-08 00:02:04 -07:00
|
|
|
}
|