From 0697704f5fd130e3f3b21e86a62256af72e0f4e5 Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Thu, 3 Jan 2019 16:27:15 +0100 Subject: [PATCH] p2p/protocols: add ClonePeer --- p2p/protocols/protocol.go | 9 +++++++++ swarm/pss/pss.go | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/p2p/protocols/protocol.go b/p2p/protocols/protocol.go index 5b9384820d..f25ae1b3bf 100644 --- a/p2p/protocols/protocol.go +++ b/p2p/protocols/protocol.go @@ -222,6 +222,15 @@ func NewPeer(p *p2p.Peer, rw p2p.MsgReadWriter, spec *Spec) *Peer { } } +// ClonePeer constructs a peer object with an arbitrary Spec, based on an existing Peer +func ClonePeer(p *Peer, spec *Spec) *Peer { + return &Peer{ + Peer: p.Peer, + rw: p.rw, + spec: spec, + } +} + // Run starts the forever loop that handles incoming messages // called within the p2p.Protocol#Run function // the handler argument is a function which is called for each message received diff --git a/swarm/pss/pss.go b/swarm/pss/pss.go index 007b927a76..2b2ef45052 100644 --- a/swarm/pss/pss.go +++ b/swarm/pss/pss.go @@ -904,7 +904,12 @@ func sendMsg(p *Pss, sp *network.Peer, msg *PssMsg) bool { return false } - err := sp.Send(context.TODO(), msg) + // get a Peer value, which supports the `pss` protocol + // `sp.BzzPeer.Peer` is a protocols.Peer, which is linked to the `hive` protocol, and + // doesn't support `pss` protocol messages. + pp := protocols.ClonePeer(sp.BzzPeer.Peer, pssSpec) + + err := pp.Send(context.TODO(), msg) if err != nil { metrics.GetOrRegisterCounter("pss.pp.send.error", nil).Inc(1) log.Error(err.Error())