mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-12 04:30:04 +00:00
95c528f0bc
* lint * add requests * add all new stuff * comment * preston's review * initial commit * reorder sync so it isn't required to wait until start * checkpoint * fix * improved handler API * Set up prechain start values * improved handler API * ooops * successful peer handshakes * successful peer handshakes * successful peer handshakes * checkpoint * chpkt * handle init after chain start * emit state initialized feed if existing db state * merge error * Done * Test * Fixed test * emit state initialized * force fork choice update * wait for genesis time * sync to current slot * Use saved head in DB * gaz * fix tests * lint * lint * lint * lint * Revert "Use saved head in DB" This reverts commit c5f3404fdf333c8aac20bce8c349b1978494616b. * remove db * lint * remove unused interfaces from composite * resolve comments
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package p2p
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
)
|
|
|
|
var handshakes = make(map[peer.ID]*pb.Hello)
|
|
var handshakeLock sync.Mutex
|
|
|
|
// AddHandshake to the local records for initial sync.
|
|
func (p *Service) AddHandshake(pid peer.ID, hello *pb.Hello) {
|
|
handshakeLock.Lock()
|
|
defer handshakeLock.Unlock()
|
|
handshakes[pid] = hello
|
|
}
|
|
|
|
// Handshakes has not been implemented yet and it may be moved to regular sync...
|
|
func (p *Service) Handshakes() map[peer.ID]*pb.Hello {
|
|
return nil
|
|
}
|
|
|
|
// AddConnectionHandler adds a callback function which handles the connection with a
|
|
// newly added peer. It performs a handshake with that peer by sending a hello request
|
|
// and validating the response from the peer.
|
|
func (p *Service) AddConnectionHandler(reqFunc func(ctx context.Context, id peer.ID) error) {
|
|
p.host.Network().Notify(&network.NotifyBundle{
|
|
ConnectedF: func(net network.Network, conn network.Conn) {
|
|
// Must be handled in a goroutine as this callback cannot be blocking.
|
|
go func() {
|
|
ctx := context.Background()
|
|
log.WithField("peer", conn.RemotePeer()).Debug(
|
|
"Performing handshake with to peer",
|
|
)
|
|
if err := reqFunc(ctx, conn.RemotePeer()); err != nil {
|
|
log.WithError(err).Error("Could not send successful hello rpc request")
|
|
if err := p.Disconnect(conn.RemotePeer()); err != nil {
|
|
log.WithError(err).Errorf("Unable to close peer %s", conn.RemotePeer())
|
|
return
|
|
}
|
|
log.WithField("peer", conn.RemotePeer().Pretty()).Info("New peer connected.")
|
|
}
|
|
}()
|
|
},
|
|
})
|
|
}
|