@@ -29,6 +29,8 @@ devp2p subprotocols by abstracting away code standardly shared by protocols.
2929package protocols
3030
3131import (
32+ "bufio"
33+ "bytes"
3234 "context"
3335 "fmt"
3436 "io"
@@ -39,6 +41,10 @@ import (
3941 "github.com/ethereum/go-ethereum/log"
4042 "github.com/ethereum/go-ethereum/metrics"
4143 "github.com/ethereum/go-ethereum/p2p"
44+ "github.com/ethereum/go-ethereum/rlp"
45+ "github.com/ethereum/go-ethereum/swarm/spancontext"
46+ "github.com/ethereum/go-ethereum/swarm/tracing"
47+ opentracing "github.com/opentracing/opentracing-go"
4248)
4349
4450// error codes used by this protocol scheme
@@ -109,6 +115,13 @@ func errorf(code int, format string, params ...interface{}) *Error {
109115 }
110116}
111117
118+ // WrappedMsg is used to propagate marshalled context alongside message payloads
119+ type WrappedMsg struct {
120+ Context []byte
121+ Size uint32
122+ Payload []byte
123+ }
124+
112125// Spec is a protocol specification including its name and version as well as
113126// the types of messages which are exchanged
114127type Spec struct {
@@ -201,7 +214,7 @@ func NewPeer(p *p2p.Peer, rw p2p.MsgReadWriter, spec *Spec) *Peer {
201214// the handler argument is a function which is called for each message received
202215// from the remote peer, a returned error causes the loop to exit
203216// resulting in disconnection
204- func (p * Peer ) Run (handler func (msg interface {}) error ) error {
217+ func (p * Peer ) Run (handler func (ctx context. Context , msg interface {}) error ) error {
205218 for {
206219 if err := p .handleIncoming (handler ); err != nil {
207220 if err != io .EOF {
@@ -225,14 +238,47 @@ func (p *Peer) Drop(err error) {
225238// message off to the peer
226239// this low level call will be wrapped by libraries providing routed or broadcast sends
227240// but often just used to forward and push messages to directly connected peers
228- func (p * Peer ) Send (msg interface {}) error {
241+ func (p * Peer ) Send (ctx context. Context , msg interface {}) error {
229242 defer metrics .GetOrRegisterResettingTimer ("peer.send_t" , nil ).UpdateSince (time .Now ())
230243 metrics .GetOrRegisterCounter ("peer.send" , nil ).Inc (1 )
244+
245+ var b bytes.Buffer
246+ if tracing .Enabled {
247+ writer := bufio .NewWriter (& b )
248+
249+ tracer := opentracing .GlobalTracer ()
250+
251+ sctx := spancontext .FromContext (ctx )
252+
253+ if sctx != nil {
254+ err := tracer .Inject (
255+ sctx ,
256+ opentracing .Binary ,
257+ writer )
258+ if err != nil {
259+ return err
260+ }
261+ }
262+
263+ writer .Flush ()
264+ }
265+
266+ r , err := rlp .EncodeToBytes (msg )
267+ if err != nil {
268+ return err
269+ }
270+
271+ wmsg := WrappedMsg {
272+ Context : b .Bytes (),
273+ Size : uint32 (len (r )),
274+ Payload : r ,
275+ }
276+
231277 code , found := p .spec .GetCode (msg )
232278 if ! found {
233279 return errorf (ErrInvalidMsgType , "%v" , code )
234280 }
235- return p2p .Send (p .rw , code , msg )
281+ return p2p .Send (p .rw , code , wmsg )
236282}
237283
238284// handleIncoming(code)
@@ -243,7 +289,7 @@ func (p *Peer) Send(msg interface{}) error {
243289// * checks for out-of-range message codes,
244290// * handles decoding with reflection,
245291// * call handlers as callbacks
246- func (p * Peer ) handleIncoming (handle func (msg interface {}) error ) error {
292+ func (p * Peer ) handleIncoming (handle func (ctx context. Context , msg interface {}) error ) error {
247293 msg , err := p .rw .ReadMsg ()
248294 if err != nil {
249295 return err
@@ -255,11 +301,38 @@ func (p *Peer) handleIncoming(handle func(msg interface{}) error) error {
255301 return errorf (ErrMsgTooLong , "%v > %v" , msg .Size , p .spec .MaxMsgSize )
256302 }
257303
304+ // unmarshal wrapped msg, which might contain context
305+ var wmsg WrappedMsg
306+ err = msg .Decode (& wmsg )
307+ if err != nil {
308+ log .Error (err .Error ())
309+ return err
310+ }
311+
312+ ctx := context .Background ()
313+
314+ // if tracing is enabled and the context coming within the request is
315+ // not empty, try to unmarshal it
316+ if tracing .Enabled && len (wmsg .Context ) > 0 {
317+ var sctx opentracing.SpanContext
318+
319+ tracer := opentracing .GlobalTracer ()
320+ sctx , err = tracer .Extract (
321+ opentracing .Binary ,
322+ bytes .NewReader (wmsg .Context ))
323+ if err != nil {
324+ log .Error (err .Error ())
325+ return err
326+ }
327+
328+ ctx = spancontext .WithContext (ctx , sctx )
329+ }
330+
258331 val , ok := p .spec .NewMsg (msg .Code )
259332 if ! ok {
260333 return errorf (ErrInvalidMsgCode , "%v" , msg .Code )
261334 }
262- if err := msg . Decode ( val ); err != nil {
335+ if err := rlp . DecodeBytes ( wmsg . Payload , val ); err != nil {
263336 return errorf (ErrDecode , "<= %v: %v" , msg , err )
264337 }
265338
@@ -268,7 +341,7 @@ func (p *Peer) handleIncoming(handle func(msg interface{}) error) error {
268341 // which the handler is supposed to cast to the appropriate type
269342 // it is entirely safe not to check the cast in the handler since the handler is
270343 // chosen based on the proper type in the first place
271- if err := handle (val ); err != nil {
344+ if err := handle (ctx , val ); err != nil {
272345 return errorf (ErrHandler , "(msg code %v): %v" , msg .Code , err )
273346 }
274347 return nil
@@ -288,14 +361,14 @@ func (p *Peer) Handshake(ctx context.Context, hs interface{}, verify func(interf
288361 return nil , errorf (ErrHandshake , "unknown handshake message type: %T" , hs )
289362 }
290363 errc := make (chan error , 2 )
291- handle := func (msg interface {}) error {
364+ handle := func (ctx context. Context , msg interface {}) error {
292365 rhs = msg
293366 if verify != nil {
294367 return verify (rhs )
295368 }
296369 return nil
297370 }
298- send := func () { errc <- p .Send (hs ) }
371+ send := func () { errc <- p .Send (ctx , hs ) }
299372 receive := func () { errc <- p .handleIncoming (handle ) }
300373
301374 go func () {
0 commit comments