Skip to content

Commit

Permalink
fix peerID logging in mail server (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik authored Oct 28, 2018
1 parent 539fa01 commit 0961e10
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
32 changes: 24 additions & 8 deletions mailserver/mailserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (s *WMailServer) Archive(env *whisper.Envelope) {

// DeliverMail sends mail to specified whisper peer.
func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope) {
log.Info("Delivering mail", "peer", peer.ID())
log.Info("Delivering mail", "peerID", peerIDString(peer))
requestsMeter.Mark(1)

if peer == nil {
Expand All @@ -250,7 +250,7 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope)
}
if s.exceedsPeerRequests(peer.ID()) {
requestErrorsCounter.Inc(1)
log.Error("Peer exceeded request per seconds limit", "peerID", peer.ID())
log.Error("Peer exceeded request per seconds limit", "peerID", peerIDString(peer))
s.trySendHistoricMessageErrorResponse(peer, request, fmt.Errorf("rate limit exceeded"))
return
}
Expand All @@ -274,13 +274,13 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope)
limit = payload.Limit
batch = payload.Batch
} else {
log.Debug("Failed to decode request", "err", err, "peerID", peer.ID())
log.Debug("Failed to decode request", "err", err, "peerID", peerIDString(peer))
lower, upper, bloom, limit, cursor, err = s.validateRequest(peer.ID(), request)
}

if err != nil {
requestValidationErrorsCounter.Inc(1)
log.Error("Mailserver request failed validaton", "peerID", peer.ID())
log.Error("Mailserver request failed validaton", "peerID", peerIDString(peer))
s.trySendHistoricMessageErrorResponse(peer, request, err)
return
}
Expand All @@ -301,7 +301,7 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope)
batch)
if err != nil {
processRequestErrorsCounter.Inc(1)
log.Error("Error while processing mail server request", "err", err, "peerID", peer.ID())
log.Error("Error while processing mail server request", "err", err, "peerID", peerIDString(peer))
s.trySendHistoricMessageErrorResponse(peer, request, err)
return
}
Expand All @@ -310,7 +310,7 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope)

if err := s.sendHistoricMessageResponse(peer, request, lastEnvelopeHash, nextPageCursor); err != nil {
historicResponseErrorsCounter.Inc(1)
log.Error("Error sending historic message response", "err", err, "peerID", peer.ID())
log.Error("Error sending historic message response", "err", err, "peerID", peerIDString(peer))
// we still want to try to report error even it it is a p2p error and it is unlikely
s.trySendHistoricMessageErrorResponse(peer, request, err)
}
Expand Down Expand Up @@ -497,7 +497,7 @@ func (s *WMailServer) trySendHistoricMessageErrorResponse(peer *whisper.Peer, re
// if we can't report an error, probably something is wrong with p2p connection,
// so we just print a log entry to document this sad fact
if err != nil {
log.Error("Error while reporting error response", "err", err, "peerID", peer.ID())
log.Error("Error while reporting error response", "err", err, "peerID", peerIDString(peer))
}
}

Expand Down Expand Up @@ -547,7 +547,7 @@ func (s *WMailServer) decodeRequest(peerID []byte, request *whisper.Envelope) (s
lowerTime := time.Unix(int64(payload.Lower), 0)
upperTime := time.Unix(int64(payload.Upper), 0)
if upperTime.Sub(lowerTime) > maxQueryRange {
log.Warn("Query range too long", "peerID", peerID, "length", upperTime.Sub(lowerTime), "max", maxQueryRange)
log.Warn("Query range too long", "peerID", peerIDBytesString(peerID), "length", upperTime.Sub(lowerTime), "max", maxQueryRange)
return payload, fmt.Errorf("query range must be shorted than %d", maxQueryRange)
}

Expand Down Expand Up @@ -638,3 +638,19 @@ func (s *WMailServer) bloomFromReceivedMessage(msg *whisper.ReceivedMessage) ([]

return msg.Payload[8 : 8+whisper.BloomFilterSize], nil
}

// peerWithID is a generalization of whisper.Peer.
// whisper.Peer has all fields and methods, except for ID(), unexported.
// It makes it impossible to create an instance of it
// outside of whisper package and test properly.
type peerWithID interface {
ID() []byte
}

func peerIDString(peer peerWithID) string {
return fmt.Sprintf("%x", peer.ID())
}

func peerIDBytesString(id []byte) string {
return fmt.Sprintf("%x", id)
}
15 changes: 15 additions & 0 deletions mailserver/mailserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
Expand Down Expand Up @@ -637,3 +639,16 @@ func generateEnvelope(sentTime time.Time) (*whisper.Envelope, error) {
h := crypto.Keccak256Hash([]byte("test sample data"))
return generateEnvelopeWithKeys(sentTime, h[:], nil)
}

// mockPeerWithID is a struct that conforms to peerWithID interface.
type mockPeerWithID struct {
id []byte
}

func (p mockPeerWithID) ID() []byte { return p.id }

func TestPeerIDString(t *testing.T) {
a := []byte{0x01, 0x02, 0x03}
require.Equal(t, "010203", peerIDString(&mockPeerWithID{a}))
require.Equal(t, "010203", peerIDBytesString(a))
}

0 comments on commit 0961e10

Please sign in to comment.