cmd, eth, p2p, p2p/discover: init and clean up the seed cache
This commit is contained in:
		| @@ -71,7 +71,7 @@ func main() { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm); err != nil { | 	if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil { | ||||||
| 		log.Fatal(err) | 		log.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	select {} | 	select {} | ||||||
|   | |||||||
| @@ -179,6 +179,7 @@ func New(config *Config) (*Ethereum, error) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | 	seedDbPath := path.Join(config.DataDir, "seeds") | ||||||
|  |  | ||||||
| 	// Perform database sanity checks | 	// Perform database sanity checks | ||||||
| 	d, _ := blockDb.Get([]byte("ProtocolVersion")) | 	d, _ := blockDb.Get([]byte("ProtocolVersion")) | ||||||
| @@ -243,6 +244,7 @@ func New(config *Config) (*Ethereum, error) { | |||||||
| 		NAT:            config.NAT, | 		NAT:            config.NAT, | ||||||
| 		NoDial:         !config.Dial, | 		NoDial:         !config.Dial, | ||||||
| 		BootstrapNodes: config.parseBootNodes(), | 		BootstrapNodes: config.parseBootNodes(), | ||||||
|  | 		SeedCache:      seedDbPath, | ||||||
| 	} | 	} | ||||||
| 	if len(config.Port) > 0 { | 	if len(config.Port) > 0 { | ||||||
| 		eth.net.ListenAddr = ":" + config.Port | 		eth.net.ListenAddr = ":" + config.Port | ||||||
|   | |||||||
| @@ -387,3 +387,7 @@ func (db *nodeDB) add(id NodeID, addr *net.UDPAddr, tcpPort uint16) *Node { | |||||||
| 	db.update(n) | 	db.update(n) | ||||||
| 	return n | 	return n | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (db *nodeDB) close() { | ||||||
|  | 	db.ldb.Close() | ||||||
|  | } | ||||||
|   | |||||||
| @@ -11,6 +11,9 @@ import ( | |||||||
| 	"sort" | 	"sort" | ||||||
| 	"sync" | 	"sync" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
|  | 	"github.com/ethereum/go-ethereum/logger" | ||||||
|  | 	"github.com/ethereum/go-ethereum/logger/glog" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| const ( | const ( | ||||||
| @@ -58,8 +61,14 @@ type bucket struct { | |||||||
| 	entries    []*Node | 	entries    []*Node | ||||||
| } | } | ||||||
|  |  | ||||||
| func newTable(t transport, ourID NodeID, ourAddr *net.UDPAddr) *Table { | func newTable(t transport, ourID NodeID, ourAddr *net.UDPAddr, seedCache string) *Table { | ||||||
| 	db, _ := newNodeDB("", Version) | 	// Load the bootstrap seed cache (use in memory db upon failure) | ||||||
|  | 	db, err := newNodeDB(seedCache, Version) | ||||||
|  | 	if err != nil { | ||||||
|  | 		glog.V(logger.Warn).Infoln("Failed to open bootstrap seed cache:", err) | ||||||
|  | 		db, _ = newNodeDB("", Version) | ||||||
|  | 	} | ||||||
|  | 	// Create the bootstrap table | ||||||
| 	tab := &Table{ | 	tab := &Table{ | ||||||
| 		net:       t, | 		net:       t, | ||||||
| 		db:        db, | 		db:        db, | ||||||
| @@ -81,9 +90,10 @@ func (tab *Table) Self() *Node { | |||||||
| 	return tab.self | 	return tab.self | ||||||
| } | } | ||||||
|  |  | ||||||
| // Close terminates the network listener. | // Close terminates the network listener and flushes the seed cache. | ||||||
| func (tab *Table) Close() { | func (tab *Table) Close() { | ||||||
| 	tab.net.close() | 	tab.net.close() | ||||||
|  | 	tab.db.close() | ||||||
| } | } | ||||||
|  |  | ||||||
| // Bootstrap sets the bootstrap nodes. These nodes are used to connect | // Bootstrap sets the bootstrap nodes. These nodes are used to connect | ||||||
|   | |||||||
| @@ -144,7 +144,7 @@ type reply struct { | |||||||
| } | } | ||||||
|  |  | ||||||
| // ListenUDP returns a new table that listens for UDP packets on laddr. | // ListenUDP returns a new table that listens for UDP packets on laddr. | ||||||
| func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface) (*Table, error) { | func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, seedCache string) (*Table, error) { | ||||||
| 	addr, err := net.ResolveUDPAddr("udp", laddr) | 	addr, err := net.ResolveUDPAddr("udp", laddr) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| @@ -153,12 +153,12 @@ func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface) (*Table | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	tab, _ := newUDP(priv, conn, natm) | 	tab, _ := newUDP(priv, conn, natm, seedCache) | ||||||
| 	glog.V(logger.Info).Infoln("Listening,", tab.self) | 	glog.V(logger.Info).Infoln("Listening,", tab.self) | ||||||
| 	return tab, nil | 	return tab, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface) (*Table, *udp) { | func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, seedCache string) (*Table, *udp) { | ||||||
| 	udp := &udp{ | 	udp := &udp{ | ||||||
| 		conn:       c, | 		conn:       c, | ||||||
| 		priv:       priv, | 		priv:       priv, | ||||||
| @@ -176,7 +176,7 @@ func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface) (*Table, *udp) { | |||||||
| 			realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port} | 			realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	udp.Table = newTable(udp, PubkeyID(&priv.PublicKey), realaddr) | 	udp.Table = newTable(udp, PubkeyID(&priv.PublicKey), realaddr, seedCache) | ||||||
| 	go udp.loop() | 	go udp.loop() | ||||||
| 	go udp.readLoop() | 	go udp.readLoop() | ||||||
| 	return udp.Table, udp | 	return udp.Table, udp | ||||||
|   | |||||||
| @@ -59,6 +59,10 @@ type Server struct { | |||||||
| 	// with the rest of the network. | 	// with the rest of the network. | ||||||
| 	BootstrapNodes []*discover.Node | 	BootstrapNodes []*discover.Node | ||||||
|  |  | ||||||
|  | 	// SeedCache is the path to the database containing the previously seen live | ||||||
|  | 	// nodes in the network to use as potential bootstrap seeds. | ||||||
|  | 	SeedCache string | ||||||
|  |  | ||||||
| 	// Protocols should contain the protocols supported | 	// Protocols should contain the protocols supported | ||||||
| 	// by the server. Matching protocols are launched for | 	// by the server. Matching protocols are launched for | ||||||
| 	// each peer. | 	// each peer. | ||||||
| @@ -197,7 +201,7 @@ func (srv *Server) Start() (err error) { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// node table | 	// node table | ||||||
| 	ntab, err := discover.ListenUDP(srv.PrivateKey, srv.ListenAddr, srv.NAT) | 	ntab, err := discover.ListenUDP(srv.PrivateKey, srv.ListenAddr, srv.NAT, srv.SeedCache) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user