with seed node

This commit is contained in:
Jae Kwon
2014-07-12 14:52:31 -07:00
parent 4465b4b93c
commit cc9ea407aa
3 changed files with 55 additions and 37 deletions

View File

@ -3,6 +3,7 @@ package config
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -18,8 +19,14 @@ import (
var AppDir = os.Getenv("HOME") + "/.tendermint" var AppDir = os.Getenv("HOME") + "/.tendermint"
var Config Config_ var Config Config_
func init() { func initFlags(printHelp *bool) {
flag.BoolVar(printHelp, "help", false, "Print this help message.")
flag.StringVar(&Config.IP, "ip", Config.IP, "Listen IP. (0.0.0.0 means any)")
flag.IntVar(&Config.Port, "port", Config.Port, "Listen port. (0 means any)")
flag.StringVar(&Config.Seed, "seed", Config.Seed, "Address of seed node")
}
func init() {
configFile := AppDir + "/config.json" configFile := AppDir + "/config.json"
// try to read configuration. if missing, write default // try to read configuration. if missing, write default
@ -41,13 +48,25 @@ func init() {
if err != nil { if err != nil {
log.Panicf("Invalid configuration file %s: %v", configFile, err) log.Panicf("Invalid configuration file %s: %v", configFile, err)
} }
// try to parse arg flags, which can override file configuration.
var printHelp bool
initFlags(&printHelp)
flag.Parse()
if printHelp {
fmt.Println("----------------------------------")
flag.PrintDefaults()
fmt.Println("----------------------------------")
os.Exit(0)
}
} }
/* Default configuration */ /* Default configuration */
var defaultConfig = Config_{ var defaultConfig = Config_{
Host: "127.0.0.1", IP: "0.0.0.0",
Port: 8770, Port: 8770,
Seed: "",
Db: DbConfig{ Db: DbConfig{
Type: "level", Type: "level",
Dir: AppDir + "/data", Dir: AppDir + "/data",
@ -58,8 +77,9 @@ var defaultConfig = Config_{
/* Configuration types */ /* Configuration types */
type Config_ struct { type Config_ struct {
Host string IP string
Port int Port int
Seed string
Db DbConfig Db DbConfig
Twilio TwilioConfig Twilio TwilioConfig
} }
@ -78,8 +98,8 @@ type DbConfig struct {
} }
func (cfg *Config_) validate() error { func (cfg *Config_) validate() error {
if cfg.Host == "" { if cfg.IP == "" {
return errors.New("Host must be set") return errors.New("IP must be set")
} }
if cfg.Port == 0 { if cfg.Port == 0 {
return errors.New("Port must be set") return errors.New("Port must be set")

60
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"os" "os"
"os/signal" "os/signal"
"time" "time"
@ -94,6 +95,24 @@ func (n *Node) AddListener(l p2p.Listener) {
}() }()
} }
// threadsafe
func (n *Node) DialPeerWithAddress(addr *p2p.NetAddress) (*p2p.Peer, error) {
log.Infof("Dialing peer @ %v", addr)
n.dialing.Set(addr.String(), addr)
n.book.MarkAttempt(addr)
conn, err := addr.DialTimeout(peerDialTimeoutSeconds * time.Second)
n.dialing.Delete(addr.String())
if err != nil {
return nil, err
}
peer, err := n.sw.AddPeerWithConnection(conn, true)
if err != nil {
return nil, err
}
n.initPeer(peer)
return peer, nil
}
// Ensures that sufficient peers are connected. // Ensures that sufficient peers are connected.
func (n *Node) ensurePeers() { func (n *Node) ensurePeers() {
numPeers := n.sw.NumOutboundPeers() numPeers := n.sw.NumOutboundPeers()
@ -122,23 +141,7 @@ func (n *Node) ensurePeers() {
if picked == nil { if picked == nil {
continue continue
} }
n.dialing.Set(picked.String(), picked) go n.DialPeerWithAddress(picked)
n.book.MarkAttempt(picked)
go func() {
log.Infof("Dialing addr: %v", picked)
conn, err := picked.DialTimeout(peerDialTimeoutSeconds * time.Second)
n.dialing.Delete(picked.String())
if err != nil {
// ignore error.
return
}
peer, err := n.sw.AddPeerWithConnection(conn, true)
if err != nil {
log.Warnf("Error trying to add new outbound peer connection:%v", err)
return
}
n.initPeer(peer)
}()
} }
} }
@ -171,30 +174,23 @@ func (n *Node) Stop() {
func main() { func main() {
// Create & start node
n := NewNode() n := NewNode()
l := p2p.NewDefaultListener("tcp", ":8001") l := p2p.NewDefaultListener("tcp", fmt.Sprintf("%v:%v", config.Config.IP, config.Config.Port))
n.AddListener(l) n.AddListener(l)
n.Start() n.Start()
if false { // Seed?
// TODO: replace with a global list of addresses. if config.Config.Seed != "" {
// TODO remove peer, err := n.DialPeerWithAddress(p2p.NewNetAddressString(config.Config.Seed))
// let's connect to 66.175.218.199
conn, err := p2p.NewNetAddressString("66.175.218.199:8001").Dial()
if err != nil { if err != nil {
log.Infof("Error connecting to it: %v", err) log.Errorf("Error dialing seed: %v", err)
return return
} }
peer, err := n.sw.AddPeerWithConnection(conn, true) log.Infof("Connected to seed: %v", peer)
if err != nil {
log.Infof("Error adding peer with connection: %v", err)
return
}
log.Infof("Connected to peer: %v", peer)
// TODO remove
} }
// Sleep forever // Sleep
trapSignal() trapSignal()
select {} select {}
} }

View File

@ -540,6 +540,8 @@ func (a *AddrBook) expireNew(bucketIdx int) {
} }
// Promotes an address from new to old. // Promotes an address from new to old.
// TODO: Move to old probabilistically.
// The better a node is, the less likely it should be evicted from an old bucket.
func (a *AddrBook) moveToOld(ka *knownAddress) { func (a *AddrBook) moveToOld(ka *knownAddress) {
// Sanity check // Sanity check
if ka.isOld() { if ka.isOld() {