2018-04-24 15:50:26 +00:00
|
|
|
diff --git i/whisper/whisperv6/doc.go w/whisper/whisperv6/doc.go
|
|
|
|
index 066a9766d..4bbf5546b 100644
|
|
|
|
--- i/whisper/whisperv6/doc.go
|
|
|
|
+++ w/whisper/whisperv6/doc.go
|
2018-02-21 17:36:04 +00:00
|
|
|
@@ -95,3 +95,40 @@ type MailServer interface {
|
|
|
|
Archive(env *Envelope)
|
|
|
|
DeliverMail(whisperPeer *Peer, request *Envelope)
|
2018-02-09 17:38:23 +00:00
|
|
|
}
|
|
|
|
+
|
|
|
|
+type envelopeSource int
|
|
|
|
+
|
|
|
|
+const (
|
|
|
|
+ _ = iota
|
|
|
|
+ // peerSource indicates a source as a regular peer.
|
|
|
|
+ peerSource envelopeSource = iota
|
|
|
|
+ // p2pSource indicates that envelop was received from a trusted peer.
|
|
|
|
+ p2pSource
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+// EnvelopeMeta keeps metadata of received envelopes.
|
|
|
|
+type EnvelopeMeta struct {
|
|
|
|
+ Hash string
|
|
|
|
+ Topic TopicType
|
|
|
|
+ Size uint32
|
|
|
|
+ Source envelopeSource
|
|
|
|
+ IsNew bool
|
|
|
|
+ Peer string
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// SourceString converts source to string.
|
|
|
|
+func (m *EnvelopeMeta) SourceString() string {
|
|
|
|
+ switch m.Source {
|
|
|
|
+ case peerSource:
|
|
|
|
+ return "peer"
|
|
|
|
+ case p2pSource:
|
|
|
|
+ return "p2p"
|
|
|
|
+ default:
|
|
|
|
+ return "unknown"
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// EnvelopeTracer tracks received envelopes.
|
|
|
|
+type EnvelopeTracer interface {
|
|
|
|
+ Trace(*EnvelopeMeta)
|
|
|
|
+}
|
2018-04-24 15:50:26 +00:00
|
|
|
diff --git i/whisper/whisperv6/whisper.go w/whisper/whisperv6/whisper.go
|
|
|
|
index 702556079..9aaf5687c 100644
|
|
|
|
--- i/whisper/whisperv6/whisper.go
|
|
|
|
+++ w/whisper/whisperv6/whisper.go
|
|
|
|
@@ -87,7 +87,8 @@ type Whisper struct {
|
2018-02-21 17:36:04 +00:00
|
|
|
statsMu sync.Mutex // guard stats
|
|
|
|
stats Statistics // Statistics of whisper node
|
2018-04-24 15:50:26 +00:00
|
|
|
|
2018-02-21 17:36:04 +00:00
|
|
|
- mailServer MailServer // MailServer interface
|
2018-04-24 15:50:26 +00:00
|
|
|
+ mailServer MailServer // MailServer interface
|
|
|
|
+ envelopeTracer EnvelopeTracer // Service collecting envelopes metadata
|
2018-02-09 17:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a Whisper client ready to communicate through the Ethereum P2P network.
|
2018-04-24 15:50:26 +00:00
|
|
|
@@ -211,6 +212,12 @@ func (whisper *Whisper) RegisterServer(server MailServer) {
|
2018-02-21 17:36:04 +00:00
|
|
|
whisper.mailServer = server
|
2018-02-09 17:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+// RegisterEnvelopeTracer registers an EnveloperTracer to collect information
|
|
|
|
+// about received envelopes.
|
|
|
|
+func (whisper *Whisper) RegisterEnvelopeTracer(tracer EnvelopeTracer) {
|
|
|
|
+ whisper.envelopeTracer = tracer
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
// Protocols returns the whisper sub-protocols ran by this particular client.
|
|
|
|
func (whisper *Whisper) Protocols() []p2p.Protocol {
|
|
|
|
return []p2p.Protocol{whisper.protocol}
|
2018-04-24 15:50:26 +00:00
|
|
|
@@ -736,6 +743,7 @@ func (whisper *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
|
|
|
|
|
2018-02-09 17:38:23 +00:00
|
|
|
trouble := false
|
|
|
|
for _, env := range envelopes {
|
|
|
|
+ whisper.traceEnvelope(env, !whisper.isEnvelopeCached(env.Hash()), peerSource, p)
|
2018-04-24 15:50:26 +00:00
|
|
|
cached, err := whisper.add(env, whisper.lightClient)
|
2018-02-09 17:38:23 +00:00
|
|
|
if err != nil {
|
|
|
|
trouble = true
|
2018-04-24 15:50:26 +00:00
|
|
|
@@ -786,6 +794,7 @@ func (whisper *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
|
2018-02-09 17:38:23 +00:00
|
|
|
return errors.New("invalid direct message")
|
|
|
|
}
|
|
|
|
whisper.postEvent(&envelope, true)
|
|
|
|
+ whisper.traceEnvelope(&envelope, false, p2pSource, p)
|
|
|
|
}
|
|
|
|
case p2pRequestCode:
|
|
|
|
// Must be processed if mail server is implemented. Otherwise ignore.
|
2018-04-24 15:50:26 +00:00
|
|
|
@@ -883,6 +892,22 @@ func (whisper *Whisper) add(envelope *Envelope, isP2P bool) (bool, error) {
|
2018-02-09 17:38:23 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
+// traceEnvelope collects basic metadata about an envelope and sender peer.
|
|
|
|
+func (whisper *Whisper) traceEnvelope(envelope *Envelope, isNew bool, source envelopeSource, peer *Peer) {
|
|
|
|
+ if whisper.envelopeTracer == nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ whisper.envelopeTracer.Trace(&EnvelopeMeta{
|
|
|
|
+ Hash: envelope.Hash().String(),
|
|
|
|
+ Topic: BytesToTopic(envelope.Topic[:]),
|
|
|
|
+ Size: uint32(envelope.size()),
|
|
|
|
+ Source: source,
|
|
|
|
+ IsNew: isNew,
|
|
|
|
+ Peer: peer.peer.Info().ID,
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
// postEvent queues the message for further processing.
|
|
|
|
func (whisper *Whisper) postEvent(envelope *Envelope, isP2P bool) {
|
|
|
|
if isP2P {
|