This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
network: Reconnect to the same peers on startup #1844
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,9 @@ import ( | |
"github.com/ethersphere/swarm/state" | ||
) | ||
|
||
const connectionsKey = "conns" | ||
const addressesKey = "peers" | ||
|
||
/* | ||
Hive is the logistic manager of the swarm | ||
|
||
|
@@ -135,32 +138,35 @@ func (h *Hive) Stop() error { | |
// at each iteration, ask the overlay driver to suggest the most preferred peer to connect to | ||
// as well as advertises saturation depth if needed | ||
func (h *Hive) connect() { | ||
loop: | ||
for { | ||
select { | ||
case <-h.ticker.C: | ||
addr, depth, changed := h.SuggestPeer() | ||
if h.Discovery && changed { | ||
NotifyDepth(uint8(depth), h.Kademlia) | ||
} | ||
if addr == nil { | ||
continue loop | ||
} | ||
|
||
log.Trace(fmt.Sprintf("%08x hive connect() suggested %08x", h.BaseAddr()[:4], addr.Address()[:4])) | ||
under, err := enode.ParseV4(string(addr.Under())) | ||
if err != nil { | ||
log.Warn(fmt.Sprintf("%08x unable to connect to bee %08x: invalid node URL: %v", h.BaseAddr()[:4], addr.Address()[:4], err)) | ||
continue loop | ||
} | ||
log.Trace(fmt.Sprintf("%08x attempt to connect to bee %08x", h.BaseAddr()[:4], addr.Address()[:4])) | ||
h.addPeer(under) | ||
h.tickHive() | ||
case <-h.done: | ||
break loop | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (h *Hive) tickHive() { | ||
addr, depth, changed := h.SuggestPeer() | ||
if h.Discovery && changed { | ||
NotifyDepth(uint8(depth), h.Kademlia) | ||
} | ||
if addr != nil { | ||
log.Trace(fmt.Sprintf("%08x hive connect() suggested %08x", h.BaseAddr()[:4], addr.Address()[:4])) | ||
underA := addr.Under() | ||
s := string(underA) | ||
under, err := enode.ParseV4(s) | ||
if err != nil { | ||
log.Warn(fmt.Sprintf("%08x unable to connect to bee %08x: invalid node URL: %v", h.BaseAddr()[:4], addr.Address()[:4], err)) | ||
return | ||
} | ||
log.Trace(fmt.Sprintf("%08x attempt to connect to bee %08x", h.BaseAddr()[:4], addr.Address()[:4])) | ||
h.addPeer(under) | ||
} | ||
} | ||
|
||
// Run protocol run function | ||
func (h *Hive) Run(p *BzzPeer) error { | ||
h.trackPeer(p) | ||
|
@@ -232,7 +238,7 @@ func (h *Hive) Peer(id enode.ID) *BzzPeer { | |
// loadPeers, savePeer implement persistence callback/ | ||
func (h *Hive) loadPeers() error { | ||
var as []*BzzAddr | ||
err := h.Store.Get("peers", &as) | ||
err := h.Store.Get(addressesKey, &as) | ||
if err != nil { | ||
if err == state.ErrNotFound { | ||
log.Info(fmt.Sprintf("hive %08x: no persisted peers found", h.BaseAddr()[:4])) | ||
|
@@ -249,13 +255,40 @@ func (h *Hive) loadPeers() error { | |
} | ||
} | ||
log.Info(fmt.Sprintf("hive %08x: peers loaded", h.BaseAddr()[:4])) | ||
errRegistering := h.Register(as...) | ||
var conns []*BzzAddr | ||
err = h.Store.Get(connectionsKey, &conns) | ||
if err != nil { | ||
if err == state.ErrNotFound { | ||
log.Info(fmt.Sprintf("hive %08x: no persisted peer connections found", h.BaseAddr()[:4])) | ||
} else { | ||
log.Warn(fmt.Sprintf("hive %08x: error loading connections: %v", h.BaseAddr()[:4], err)) | ||
} | ||
|
||
return h.Register(as...) | ||
} else { | ||
go h.connectInitialPeers(conns) | ||
} | ||
return errRegistering | ||
} | ||
|
||
func (h *Hive) connectInitialPeers(conns []*BzzAddr) { | ||
log.Info(fmt.Sprintf("%08x hive connectInitialPeers() With %v saved connections", h.BaseAddr()[:4], len(conns))) | ||
for _, addr := range conns { | ||
log.Trace(fmt.Sprintf("%08x hive connect() suggested initial %08x", h.BaseAddr()[:4], addr.Address()[:4])) | ||
under, err := enode.ParseV4(string(addr.Under())) | ||
if err != nil { | ||
log.Warn(fmt.Sprintf("%08x unable to connect to bee %08x: invalid node URL: %v", h.BaseAddr()[:4], addr.Address()[:4], err)) | ||
continue | ||
} | ||
log.Trace(fmt.Sprintf("%08x attempt to connect to bee %08x", h.BaseAddr()[:4], addr.Address()[:4])) | ||
h.addPeer(under) | ||
} | ||
} | ||
|
||
// savePeers, savePeer implement persistence callback/ | ||
func (h *Hive) savePeers() error { | ||
var peers []*BzzAddr | ||
var conns []*BzzAddr | ||
h.Kademlia.EachAddr(nil, 256, func(pa *BzzAddr, i int) bool { | ||
if pa == nil { | ||
log.Warn(fmt.Sprintf("empty addr: %v", i)) | ||
|
@@ -265,8 +298,18 @@ func (h *Hive) savePeers() error { | |
peers = append(peers, pa) | ||
return true | ||
}) | ||
if err := h.Store.Put("peers", peers); err != nil { | ||
|
||
h.Kademlia.EachConn(nil, 256, func(p *Peer, i int) bool { | ||
log.Trace("saving connected peer", "OAddr", hexutil.Encode(p.OAddr), "UAddr", p.UAddr) | ||
conns = append(conns, p.BzzAddr) | ||
return true | ||
}) | ||
if err := h.Store.Put(addressesKey, peers); err != nil { | ||
return fmt.Errorf("could not save peers: %v", err) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for consistency, i would change line 308 to have "addrs" instead of "peers" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will break backward compatibility. Are we sure we want that? |
||
if err := h.Store.Put(connectionsKey, conns); err != nil { | ||
return fmt.Errorf("could not save peer connections: %v", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not to use
fmt
in logupdate: I see now this is all over the original code, so TODO cleanup later.