diff options
Diffstat (limited to 'p2p')
-rw-r--r-- | p2p/discover/table.go | 7 | ||||
-rw-r--r-- | p2p/discover/table_test.go | 8 | ||||
-rw-r--r-- | p2p/discover/udp.go | 16 | ||||
-rw-r--r-- | p2p/discover/udp_test.go | 2 |
4 files changed, 20 insertions, 13 deletions
diff --git a/p2p/discover/table.go b/p2p/discover/table.go index c128c2ed1..298ba3fa6 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -90,12 +90,11 @@ type transport interface { // that was most recently active is the first element in entries. type bucket struct{ entries []*Node } -func newTable(t transport, ourID NodeID, ourAddr *net.UDPAddr, nodeDBPath string) *Table { +func newTable(t transport, ourID NodeID, ourAddr *net.UDPAddr, nodeDBPath string) (*Table, error) { // If no node database was given, use an in-memory one db, err := newNodeDB(nodeDBPath, Version, ourID) if err != nil { - glog.V(logger.Warn).Infoln("Failed to open node database:", err) - db, _ = newNodeDB("", Version, ourID) + return nil, err } tab := &Table{ net: t, @@ -114,7 +113,7 @@ func newTable(t transport, ourID NodeID, ourAddr *net.UDPAddr, nodeDBPath string tab.buckets[i] = new(bucket) } go tab.refreshLoop() - return tab + return tab, nil } // Self returns the local node. diff --git a/p2p/discover/table_test.go b/p2p/discover/table_test.go index 84962a1a5..13effaed6 100644 --- a/p2p/discover/table_test.go +++ b/p2p/discover/table_test.go @@ -34,7 +34,7 @@ import ( func TestTable_pingReplace(t *testing.T) { doit := func(newNodeIsResponding, lastInBucketIsResponding bool) { transport := newPingRecorder() - tab := newTable(transport, NodeID{}, &net.UDPAddr{}, "") + tab, _ := newTable(transport, NodeID{}, &net.UDPAddr{}, "") defer tab.Close() pingSender := newNode(MustHexID("a502af0f59b2aab7746995408c79e9ca312d2793cc997e44fc55eda62f0150bbb8c59a6f9269ba3a081518b62699ee807c7c19c20125ddfccca872608af9e370"), net.IP{}, 99, 99) @@ -177,7 +177,7 @@ func TestTable_closest(t *testing.T) { test := func(test *closeTest) bool { // for any node table, Target and N - tab := newTable(nil, test.Self, &net.UDPAddr{}, "") + tab, _ := newTable(nil, test.Self, &net.UDPAddr{}, "") defer tab.Close() tab.stuff(test.All) @@ -236,7 +236,7 @@ func TestTable_ReadRandomNodesGetAll(t *testing.T) { }, } test := func(buf []*Node) bool { - tab := newTable(nil, NodeID{}, &net.UDPAddr{}, "") + tab, _ := newTable(nil, NodeID{}, &net.UDPAddr{}, "") defer tab.Close() for i := 0; i < len(buf); i++ { ld := cfg.Rand.Intn(len(tab.buckets)) @@ -279,7 +279,7 @@ func (*closeTest) Generate(rand *rand.Rand, size int) reflect.Value { func TestTable_Lookup(t *testing.T) { self := nodeAtDistance(common.Hash{}, 0) - tab := newTable(lookupTestnet, self.ID, &net.UDPAddr{}, "") + tab, _ := newTable(lookupTestnet, self.ID, &net.UDPAddr{}, "") defer tab.Close() // lookup on empty table returns no nodes diff --git a/p2p/discover/udp.go b/p2p/discover/udp.go index 20f69cf08..fc7fa737c 100644 --- a/p2p/discover/udp.go +++ b/p2p/discover/udp.go @@ -200,12 +200,15 @@ func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, nodeDBP if err != nil { return nil, err } - tab, _ := newUDP(priv, conn, natm, nodeDBPath) + tab, _, err := newUDP(priv, conn, natm, nodeDBPath) + if err != nil { + return nil, err + } glog.V(logger.Info).Infoln("Listening,", tab.self) return tab, nil } -func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, nodeDBPath string) (*Table, *udp) { +func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, nodeDBPath string) (*Table, *udp, error) { udp := &udp{ conn: c, priv: priv, @@ -225,10 +228,15 @@ func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, nodeDBPath strin } // TODO: separate TCP port udp.ourEndpoint = makeEndpoint(realaddr, uint16(realaddr.Port)) - udp.Table = newTable(udp, PubkeyID(&priv.PublicKey), realaddr, nodeDBPath) + tab, err := newTable(udp, PubkeyID(&priv.PublicKey), realaddr, nodeDBPath) + if err != nil { + return nil, nil, err + } + udp.Table = tab + go udp.loop() go udp.readLoop() - return udp.Table, udp + return udp.Table, udp, nil } func (t *udp) close() { diff --git a/p2p/discover/udp_test.go b/p2p/discover/udp_test.go index 913199c26..944e73d6e 100644 --- a/p2p/discover/udp_test.go +++ b/p2p/discover/udp_test.go @@ -69,7 +69,7 @@ func newUDPTest(t *testing.T) *udpTest { remotekey: newkey(), remoteaddr: &net.UDPAddr{IP: net.IP{1, 2, 3, 4}, Port: 30303}, } - test.table, test.udp = newUDP(test.localkey, test.pipe, nil, "") + test.table, test.udp, _ = newUDP(test.localkey, test.pipe, nil, "") return test } |