@@ -18,7 +18,6 @@ package network
1818
1919import  (
2020	"fmt" 
21- 	"math/rand" 
2221	"sync" 
2322	"time" 
2423
@@ -71,7 +70,7 @@ func NewHiveParams() *HiveParams {
7170		Discovery :             true ,
7271		PeersBroadcastSetSize : 3 ,
7372		MaxPeersPerRequest :    5 ,
74- 		KeepAliveInterval :     1000  *  time .Millisecond ,
73+ 		KeepAliveInterval :     500  *  time .Millisecond ,
7574	}
7675}
7776
@@ -102,10 +101,12 @@ func NewHive(params *HiveParams, overlay Overlay, store state.Store) *Hive {
102101// server is used to connect to a peer based on its NodeID or enode URL 
103102// these are called on the p2p.Server which runs on the node 
104103func  (h  * Hive ) Start (server  * p2p.Server ) error  {
105- 	log .Trace (fmt .Sprintf ("%08x hive starting" , h .BaseAddr ()[:4 ]))
104+ 	log .Info (fmt .Sprintf ("%08x hive starting" , h .BaseAddr ()[:4 ]))
106105	// if state store is specified, load peers to prepopulate the overlay address book 
107106	if  h .Store  !=  nil  {
107+ 		log .Info ("detected an existing store. trying to load peers" )
108108		if  err  :=  h .loadPeers (); err  !=  nil  {
109+ 			log .Error (fmt .Sprintf ("%08x hive encoutered an error trying to load peers" , h .BaseAddr ()[:4 ]))
109110			return  err 
110111		}
111112	}
@@ -123,7 +124,12 @@ func (h *Hive) Stop() error {
123124	log .Info (fmt .Sprintf ("%08x hive stopping, saving peers" , h .BaseAddr ()[:4 ]))
124125	h .ticker .Stop ()
125126	if  h .Store  !=  nil  {
126- 		return  h .savePeers ()
127+ 		if  err  :=  h .savePeers (); err  !=  nil  {
128+ 			return  fmt .Errorf ("could not save peers to persistence store: %v" , err )
129+ 		}
130+ 		if  err  :=  h .Store .Close (); err  !=  nil  {
131+ 			return  fmt .Errorf ("could not close file handle to persistence store: %v" , err )
132+ 		}
127133	}
128134	log .Info (fmt .Sprintf ("%08x hive stopped, dropping peers" , h .BaseAddr ()[:4 ]))
129135	h .EachConn (nil , 255 , func (p  OverlayConn , _  int , _  bool ) bool  {
@@ -139,8 +145,9 @@ func (h *Hive) Stop() error {
139145// at each iteration, ask the overlay driver to suggest the most preferred peer to connect to 
140146// as well as advertises saturation depth if needed 
141147func  (h  * Hive ) connect () {
142- 	time .Sleep (time .Duration (rand .Intn (1000 )) *  time .Millisecond )
143148	for  range  h .ticker .C  {
149+ 		log .Trace (fmt .Sprintf ("%08x hive connect()" , h .BaseAddr ()[:4 ]))
150+ 
144151		addr , depth , changed  :=  h .SuggestPeer ()
145152		if  h .Discovery  &&  changed  {
146153			NotifyDepth (uint8 (depth ), h )
@@ -203,14 +210,16 @@ func ToAddr(pa OverlayPeer) *BzzAddr {
203210// loadPeers, savePeer implement persistence callback/ 
204211func  (h  * Hive ) loadPeers () error  {
205212	var  as  []* BzzAddr 
206- 
207213	err  :=  h .Store .Get ("peers" , & as )
208214	if  err  !=  nil  {
209215		if  err  ==  state .ErrNotFound  {
216+ 			log .Info (fmt .Sprintf ("hive %08x: no persisted peers found" , h .BaseAddr ()[:4 ]))
210217			return  nil 
211218		}
212219		return  err 
213220	}
221+ 	log .Info (fmt .Sprintf ("hive %08x: peers loaded" , h .BaseAddr ()[:4 ]))
222+ 
214223	return  h .Register (toOverlayAddrs (as ... ))
215224}
216225
0 commit comments