Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion eth/peerset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import (
"errors"
"math/big"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth/protocols/eth"
"github.com/ethereum/go-ethereum/eth/protocols/snap"
"github.com/ethereum/go-ethereum/p2p"
)

const (
// snapWaitTimeout is the amount of time to wait for the snap protocol to be started.
snapWaitTimeout = 5 * time.Second
)

var (
// errPeerSetClosed is returned if a peer is attempted to be added or removed
// from the peer set after it has been terminated.
Expand All @@ -43,6 +49,10 @@ var (
// errSnapWithoutEth is returned if a peer attempts to connect only on the
// snap protocol without advertising the eth main protocol.
errSnapWithoutEth = errors.New("peer connected on snap without compatible eth support")

// errSnapTimeout is returned if the peer takes too long to start the snap
// protocol.
errSnapTimeout = errors.New("peer timeout starting snap protococol")
)

// peerSet represents the collection of active peers currently participating in
Expand Down Expand Up @@ -128,7 +138,18 @@ func (ps *peerSet) waitSnapExtension(peer *eth.Peer) (*snap.Peer, error) {
ps.snapWait[id] = wait
ps.lock.Unlock()

return <-wait, nil
t := time.NewTimer(snapWaitTimeout)
select {
case p := <-wait:
return p, nil
case <-t.C:
ps.lock.Lock()
if _, ok := ps.snapWait[id]; ok {
delete(ps.snapWait, id)
}
ps.lock.Unlock()
return nil, errSnapTimeout
}
}

// registerPeer injects a new `eth` peer into the working set, or returns an error
Expand Down