diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-04-23 23:47:24 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-04-24 16:23:20 +0800 |
commit | 5f735d6fce10b4552b0a6d3eb6503c5a302f4f61 (patch) | |
tree | 781d93a4c91ac664b95d97b96d3ac6605bfbb57c | |
parent | 936c8e19ff3db376fdd1ed53772c51c817a72112 (diff) | |
download | go-tangerine-5f735d6fce10b4552b0a6d3eb6503c5a302f4f61.tar.gz go-tangerine-5f735d6fce10b4552b0a6d3eb6503c5a302f4f61.tar.zst go-tangerine-5f735d6fce10b4552b0a6d3eb6503c5a302f4f61.zip |
cmd, eth, p2p, p2p/discover: init and clean up the seed cache
-rw-r--r-- | cmd/bootnode/main.go | 2 | ||||
-rw-r--r-- | eth/backend.go | 2 | ||||
-rw-r--r-- | p2p/discover/node.go | 4 | ||||
-rw-r--r-- | p2p/discover/table.go | 16 | ||||
-rw-r--r-- | p2p/discover/udp.go | 8 | ||||
-rw-r--r-- | p2p/server.go | 6 |
6 files changed, 29 insertions, 9 deletions
diff --git a/cmd/bootnode/main.go b/cmd/bootnode/main.go index dda9f34d4..26912525d 100644 --- a/cmd/bootnode/main.go +++ b/cmd/bootnode/main.go @@ -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) } select {} diff --git a/eth/backend.go b/eth/backend.go index 356e7fd1a..382cfc832 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -179,6 +179,7 @@ func New(config *Config) (*Ethereum, error) { if err != nil { return nil, err } + seedDbPath := path.Join(config.DataDir, "seeds") // Perform database sanity checks d, _ := blockDb.Get([]byte("ProtocolVersion")) @@ -243,6 +244,7 @@ func New(config *Config) (*Ethereum, error) { NAT: config.NAT, NoDial: !config.Dial, BootstrapNodes: config.parseBootNodes(), + SeedCache: seedDbPath, } if len(config.Port) > 0 { eth.net.ListenAddr = ":" + config.Port diff --git a/p2p/discover/node.go b/p2p/discover/node.go index d8a5cc351..3e79ddb53 100644 --- a/p2p/discover/node.go +++ b/p2p/discover/node.go @@ -387,3 +387,7 @@ func (db *nodeDB) add(id NodeID, addr *net.UDPAddr, tcpPort uint16) *Node { db.update(n) return n } + +func (db *nodeDB) close() { + db.ldb.Close() +} diff --git a/p2p/discover/table.go b/p2p/discover/table.go index ba2f9b8ec..3702a2114 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -11,6 +11,9 @@ import ( "sort" "sync" "time" + + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" ) const ( @@ -58,8 +61,14 @@ type bucket struct { entries []*Node } -func newTable(t transport, ourID NodeID, ourAddr *net.UDPAddr) *Table { - db, _ := newNodeDB("", Version) +func newTable(t transport, ourID NodeID, ourAddr *net.UDPAddr, seedCache string) *Table { + // 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{ net: t, db: db, @@ -81,9 +90,10 @@ func (tab *Table) Self() *Node { return tab.self } -// Close terminates the network listener. +// Close terminates the network listener and flushes the seed cache. func (tab *Table) Close() { tab.net.close() + tab.db.close() } // Bootstrap sets the bootstrap nodes. These nodes are used to connect diff --git a/p2p/discover/udp.go b/p2p/discover/udp.go index 07a1a739c..c26703f19 100644 --- a/p2p/discover/udp.go +++ b/p2p/discover/udp.go @@ -144,7 +144,7 @@ type reply struct { } // 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) if err != nil { return nil, err @@ -153,12 +153,12 @@ func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface) (*Table if err != nil { return nil, err } - tab, _ := newUDP(priv, conn, natm) + tab, _ := newUDP(priv, conn, natm, seedCache) glog.V(logger.Info).Infoln("Listening,", tab.self) 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{ conn: c, 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} } } - udp.Table = newTable(udp, PubkeyID(&priv.PublicKey), realaddr) + udp.Table = newTable(udp, PubkeyID(&priv.PublicKey), realaddr, seedCache) go udp.loop() go udp.readLoop() return udp.Table, udp diff --git a/p2p/server.go b/p2p/server.go index ecf418d13..39b0b8b6e 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -59,6 +59,10 @@ type Server struct { // with the rest of the network. 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 // by the server. Matching protocols are launched for // each peer. @@ -197,7 +201,7 @@ func (srv *Server) Start() (err error) { } // 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 { return err } |