mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-24 12:27:18 +00:00
4bc2d628b1
* update naming * replace with updated version * more changes * fixed all tests * build and lint * regen protos * fix test * remove outdated code * prestons review * preston's comments * preston's review * preston's review * lint
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package sync
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
libp2pcore "github.com/libp2p/go-libp2p-core"
|
|
)
|
|
|
|
const (
|
|
codeClientShutdown uint64 = iota
|
|
codeWrongNetwork
|
|
codeGenericError
|
|
)
|
|
|
|
var goodByes = map[uint64]string{
|
|
codeClientShutdown: "client shutdown",
|
|
codeWrongNetwork: "irrelevant network",
|
|
codeGenericError: "fault/error",
|
|
}
|
|
|
|
// goodbyeRPCHandler reads the incoming goodbye rpc message from the peer.
|
|
func (r *RegularSync) goodbyeRPCHandler(ctx context.Context, msg interface{}, stream libp2pcore.Stream) error {
|
|
defer stream.Close()
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
setRPCStreamDeadlines(stream)
|
|
|
|
m := msg.(uint64)
|
|
log := log.WithField("Reason", goodbyeMessage(m))
|
|
log.WithField("peer", stream.Conn().RemotePeer()).Info("Peer has sent a goodbye message")
|
|
// closes all streams with the peer
|
|
return r.p2p.Disconnect(stream.Conn().RemotePeer())
|
|
}
|
|
|
|
func goodbyeMessage(num uint64) string {
|
|
reason, ok := goodByes[num]
|
|
if ok {
|
|
return reason
|
|
}
|
|
return fmt.Sprintf("unknown goodbye value of %d Received", num)
|
|
}
|